|
| 1 | +""" |
| 2 | +Bootloader implementation for Aboot used on Arista devices |
| 3 | +""" |
| 4 | + |
| 5 | +import collections |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | + |
| 10 | +import click |
| 11 | + |
| 12 | +from ..common import ( |
| 13 | + HOST_PATH, |
| 14 | + IMAGE_DIR_PREFIX, |
| 15 | + IMAGE_PREFIX, |
| 16 | + run_command, |
| 17 | +) |
| 18 | +from .bootloader import Bootloader |
| 19 | + |
| 20 | +_secureboot = None |
| 21 | +def isSecureboot(): |
| 22 | + global _secureboot |
| 23 | + if _secureboot is None: |
| 24 | + with open('/proc/cmdline') as f: |
| 25 | + m = re.search(r"secure_boot_enable=[y1]", f.read()) |
| 26 | + _secureboot = bool(m) |
| 27 | + return _secureboot |
| 28 | + |
| 29 | +class AbootBootloader(Bootloader): |
| 30 | + |
| 31 | + NAME = 'aboot' |
| 32 | + BOOT_CONFIG_PATH = os.path.join(HOST_PATH, 'boot-config') |
| 33 | + DEFAULT_IMAGE_PATH = '/tmp/sonic_image.swi' |
| 34 | + |
| 35 | + def _boot_config_read(self, path=BOOT_CONFIG_PATH): |
| 36 | + config = collections.OrderedDict() |
| 37 | + with open(path) as f: |
| 38 | + for line in f.readlines(): |
| 39 | + line = line.strip() |
| 40 | + if not line or line.startswith('#') or '=' not in line: |
| 41 | + continue |
| 42 | + key, value = line.split('=', 1) |
| 43 | + config[key] = value |
| 44 | + return config |
| 45 | + |
| 46 | + def _boot_config_write(self, config, path=BOOT_CONFIG_PATH): |
| 47 | + with open(path, 'w') as f: |
| 48 | + f.write(''.join('%s=%s\n' % (k, v) for k, v in config.items())) |
| 49 | + |
| 50 | + def _boot_config_set(self, **kwargs): |
| 51 | + path = kwargs.pop('path', self.BOOT_CONFIG_PATH) |
| 52 | + config = self._boot_config_read(path=path) |
| 53 | + for key, value in kwargs.items(): |
| 54 | + config[key] = value |
| 55 | + self._boot_config_write(config, path=path) |
| 56 | + |
| 57 | + def _swi_image_path(self, image): |
| 58 | + image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX) |
| 59 | + if isSecureboot(): |
| 60 | + return 'flash:%s/sonic.swi' % image_dir |
| 61 | + return 'flash:%s/.sonic-boot.swi' % image_dir |
| 62 | + |
| 63 | + def get_current_image(self): |
| 64 | + with open('/proc/cmdline') as f: |
| 65 | + current = re.search(r"loop=/*(\S+)/", f.read()).group(1) |
| 66 | + return current.replace(IMAGE_DIR_PREFIX, IMAGE_PREFIX) |
| 67 | + |
| 68 | + def get_installed_images(self): |
| 69 | + images = [] |
| 70 | + for filename in os.listdir(HOST_PATH): |
| 71 | + if filename.startswith(IMAGE_DIR_PREFIX): |
| 72 | + images.append(filename.replace(IMAGE_DIR_PREFIX, IMAGE_PREFIX)) |
| 73 | + return images |
| 74 | + |
| 75 | + def get_next_image(self): |
| 76 | + config = self._boot_config_read() |
| 77 | + match = re.search(r"flash:/*(\S+)/", config['SWI']) |
| 78 | + return match.group(1).replace(IMAGE_DIR_PREFIX, IMAGE_PREFIX) |
| 79 | + |
| 80 | + def set_default_image(self, image): |
| 81 | + image_path = self._swi_image_path(image) |
| 82 | + self._boot_config_set(SWI=image_path, SWI_DEFAULT=image_path) |
| 83 | + return True |
| 84 | + |
| 85 | + def set_next_image(self, image): |
| 86 | + image_path = self._swi_image_path(image) |
| 87 | + self._boot_config_set(SWI=image_path) |
| 88 | + return True |
| 89 | + |
| 90 | + def install_image(self, image_path): |
| 91 | + run_command("/usr/bin/unzip -od /tmp %s boot0" % image_path) |
| 92 | + run_command("swipath=%s target_path=/host sonic_upgrade=1 . /tmp/boot0" % image_path) |
| 93 | + |
| 94 | + def remove_image(self, image): |
| 95 | + nextimage = self.get_next_image() |
| 96 | + current = self.get_current_image() |
| 97 | + if image == nextimage: |
| 98 | + image_path = self._swi_image_path(current) |
| 99 | + self._boot_config_set(SWI=image_path, SWI_DEFAULT=image_path) |
| 100 | + click.echo("Set next and default boot to current image %s" % current) |
| 101 | + |
| 102 | + image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX) |
| 103 | + click.echo('Removing image root filesystem...') |
| 104 | + subprocess.call(['rm','-rf', os.path.join(HOST_PATH, image_dir)]) |
| 105 | + click.echo('Image removed') |
| 106 | + |
| 107 | + def get_binary_image_version(self, image_path): |
| 108 | + try: |
| 109 | + version = subprocess.check_output(['/usr/bin/unzip', '-qop', image_path, '.imagehash']) |
| 110 | + except subprocess.CalledProcessError: |
| 111 | + return None |
| 112 | + return IMAGE_PREFIX + version.strip() |
| 113 | + |
| 114 | + def verify_binary_image(self, image_path): |
| 115 | + try: |
| 116 | + subprocess.check_call(['/usr/bin/unzip', '-tq', image_path]) |
| 117 | + # TODO: secureboot check signature |
| 118 | + except subprocess.CalledProcessError: |
| 119 | + return False |
| 120 | + return True |
| 121 | + |
| 122 | + @classmethod |
| 123 | + def detect(cls): |
| 124 | + with open('/proc/cmdline') as f: |
| 125 | + return 'Aboot=' in f.read() |
0 commit comments