Best Python code snippet using avocado_python
bootloader.py
Source:bootloader.py
1# Copyright (C) 2013 Linaro Limited2#3# Author: Tyler Baker <tyler.baker@linaro.org>4#5# This file is part of LAVA Dispatcher.6#7# LAVA Dispatcher is free software; you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation; either version 2 of the License, or10# (at your option) any later version.11#12# LAVA Dispatcher is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along19# with this program; if not, see <http://www.gnu.org/licenses>.20import logging21import contextlib22import subprocess23from lava_dispatcher.device.master import (24 MasterImageTarget25)26from lava_dispatcher.client.base import (27 NetworkCommandRunner,28)29from lava_dispatcher.utils import (30 finalize_process,31 connect_to_serial,32 extract_overlay,33 extract_ramdisk,34 create_ramdisk,35 ensure_directory,36 append_dtb,37 create_uimage,38 is_uimage,39)40from lava_dispatcher.errors import (41 CriticalError,42)43from lava_dispatcher.downloader import (44 download_image,45)46from lava_dispatcher import deployment_data47class BootloaderTarget(MasterImageTarget):48 def __init__(self, context, config):49 super(BootloaderTarget, self).__init__(context, config)50 self._booted = False51 self._reset_boot = False52 self._in_test_shell = False53 self._default_boot_cmds = 'boot_cmds_ramdisk'54 self._lava_nfsrootfs = None55 self._uboot_boot = False56 self._ipxe_boot = False57 self._uefi_boot = False58 self._boot_tags = {}59 self._base_tmpdir, self._tmpdir = self._setup_tmpdir()60 def _get_http_url(self, path):61 prefix = self.context.config.lava_image_url62 return prefix + '/' + self._get_rel_path(path, self._base_tmpdir)63 def _set_load_addresses(self, bootz):64 meta = {}65 if not bootz and self.config.u_load_addrs and len(self.config.u_load_addrs) == 3:66 logging.info("Attempting to set uImage Load Addresses")67 self._boot_tags['{KERNEL_ADDR}'] = self.config.u_load_addrs[0]68 self._boot_tags['{RAMDISK_ADDR}'] = self.config.u_load_addrs[1]69 self._boot_tags['{DTB_ADDR}'] = self.config.u_load_addrs[2]70 # Set boot metadata71 meta['kernel-image'] = 'uImage'72 meta['kernel-addr'] = self.config.u_load_addrs[0]73 meta['initrd-addr'] = self.config.u_load_addrs[1]74 meta['dtb-addr'] = self.config.u_load_addrs[2]75 self.context.test_data.add_metadata(meta)76 elif bootz and self.config.z_load_addrs and len(self.config.z_load_addrs) == 3:77 logging.info("Attempting to set zImage Load Addresses")78 self._boot_tags['{KERNEL_ADDR}'] = self.config.z_load_addrs[0]79 self._boot_tags['{RAMDISK_ADDR}'] = self.config.z_load_addrs[1]80 self._boot_tags['{DTB_ADDR}'] = self.config.z_load_addrs[2]81 # Set boot metadata82 meta['kernel-image'] = 'zImage'83 meta['kernel-addr'] = self.config.z_load_addrs[0]84 meta['initrd-addr'] = self.config.z_load_addrs[1]85 meta['dtb-addr'] = self.config.z_load_addrs[2]86 self.context.test_data.add_metadata(meta)87 else:88 logging.debug("Undefined u_load_addrs or z_load_addrs. Three values required!")89 def _get_uboot_boot_command(self, kernel, ramdisk, dtb):90 bootz = False91 bootx = []92 if is_uimage(kernel, self.context):93 logging.info('Attempting to set boot command as bootm')94 bootx.append('bootm')95 else:96 logging.info('Attempting to set boot command as bootz')97 bootx.append('bootz')98 bootz = True99 # At minimal we have a kernel100 bootx.append('${kernel_addr_r}')101 if ramdisk is not None:102 bootx.append('${initrd_addr_r}')103 elif ramdisk is None and dtb is not None:104 bootx.append('-')105 if dtb is not None:106 bootx.append('${fdt_addr_r}')107 self._set_load_addresses(bootz)108 return ' '.join(bootx)109 def _is_uboot(self):110 if self._uboot_boot:111 return True112 else:113 return False114 def _is_ipxe(self):115 if self._ipxe_boot:116 return True117 else:118 return False119 def _is_uefi(self):120 if self._uefi_boot:121 return True122 else:123 return False124 def _is_bootloader(self):125 if self._is_uboot() or self._is_ipxe() or self._is_uefi():126 return True127 else:128 return False129 def _set_boot_type(self, bootloadertype):130 if bootloadertype == "u_boot":131 self._uboot_boot = True132 elif bootloadertype == 'ipxe':133 self._ipxe_boot = True134 elif bootloadertype == 'uefi':135 self._uefi_boot = True136 else:137 raise CriticalError("Unknown bootloader type")138 def deploy_linaro_kernel(self, kernel, ramdisk, dtb, overlays, rootfs, nfsrootfs, image, bootloader, firmware, bl0, bl1,139 bl2, bl31, rootfstype, bootloadertype, target_type, qemu_pflash=None):140 if self.__deployment_data__ is None:141 # Get deployment data142 logging.debug("Attempting to set deployment data")143 self.deployment_data = deployment_data.get(target_type)144 else:145 # Reset deployment data146 logging.debug("Attempting to reset deployment data")147 self.power_off(self.proc)148 self.__init__(self.context, self.config)149 # Get deployment data150 self.deployment_data = deployment_data.get(target_type)151 # We set the boot type152 self._set_boot_type(bootloadertype)153 # At a minimum we must have a kernel154 if kernel is None:155 raise CriticalError("No kernel image to boot")156 if self._is_uboot() or self._is_uefi() or self._is_ipxe():157 # Set the server IP (Dispatcher)158 self._boot_tags['{SERVER_IP}'] = self.context.config.lava_server_ip159 # We have been passed kernel image160 kernel = download_image(kernel, self.context,161 self._tmpdir, decompress=False)162 if self._is_uboot() or self._is_uefi():163 if self.config.uimage_only and not is_uimage(kernel, self.context):164 if len(self.config.u_load_addrs) == 3:165 if self.config.text_offset:166 load_addr = self.config.text_offset167 else:168 load_addr = self.config.u_load_addrs[0]169 kernel = create_uimage(kernel, load_addr,170 self._tmpdir, self.config.uimage_xip)171 logging.info('uImage created successfully')172 else:173 logging.error('Undefined u_load_addrs, aborting uImage creation')174 self._boot_tags['{KERNEL}'] = self._get_rel_path(kernel, self._base_tmpdir)175 if ramdisk is not None:176 # We have been passed a ramdisk177 ramdisk = download_image(ramdisk, self.context,178 self._tmpdir,179 decompress=False)180 if overlays is not None:181 ramdisk_dir = extract_ramdisk(ramdisk, self._tmpdir,182 is_uboot=self._is_uboot_ramdisk(ramdisk))183 for overlay in overlays:184 overlay = download_image(overlay, self.context,185 self._tmpdir,186 decompress=False)187 extract_overlay(overlay, ramdisk_dir)188 ramdisk = create_ramdisk(ramdisk_dir, self._tmpdir)189 if self._is_uboot():190 # Ensure ramdisk has u-boot header191 if not self._is_uboot_ramdisk(ramdisk):192 ramdisk_uboot = ramdisk + ".uboot"193 logging.info("RAMdisk needs u-boot header. Adding.")194 cmd = "mkimage -A arm -T ramdisk -C none -d %s %s > /dev/null" \195 % (ramdisk, ramdisk_uboot)196 r = subprocess.call(cmd, shell=True)197 if r == 0:198 ramdisk = ramdisk_uboot199 else:200 logging.warning("Unable to add u-boot header to ramdisk. Tried %s", cmd)201 self._boot_tags['{RAMDISK}'] = self._get_rel_path(ramdisk, self._base_tmpdir)202 if dtb is not None:203 # We have been passed a device tree blob204 dtb = download_image(dtb, self.context,205 self._tmpdir, decompress=False)206 if self.config.append_dtb:207 kernel = append_dtb(kernel, dtb, self._tmpdir)208 logging.info('Appended dtb to kernel image successfully')209 self._boot_tags['{KERNEL}'] = self._get_rel_path(kernel, self._base_tmpdir)210 else:211 self._boot_tags['{DTB}'] = self._get_rel_path(dtb, self._base_tmpdir)212 if rootfs is not None:213 # We have been passed a rootfs214 rootfs = download_image(rootfs, self.context,215 self._tmpdir, decompress=False)216 self._boot_tags['{ROOTFS}'] = self._get_rel_path(rootfs, self._base_tmpdir)217 if nfsrootfs is not None:218 # Extract rootfs into nfsrootfs directory219 nfsrootfs = download_image(nfsrootfs, self.context,220 self._tmpdir,221 decompress=False)222 self._lava_nfsrootfs = self._setup_nfs(nfsrootfs, self._tmpdir)223 self._default_boot_cmds = 'boot_cmds_nfs'224 self._boot_tags['{NFSROOTFS}'] = self._lava_nfsrootfs225 if overlays is not None and ramdisk is None:226 for overlay in overlays:227 overlay = download_image(overlay, self.context,228 self._tmpdir,229 decompress=False)230 extract_overlay(overlay, self._lava_nfsrootfs)231 if bootloader is not None:232 # We have been passed a bootloader233 bootloader = download_image(bootloader, self.context,234 self._tmpdir,235 decompress=False)236 self._boot_tags['{BOOTLOADER}'] = self._get_rel_path(bootloader, self._base_tmpdir)237 if firmware is not None:238 # We have been passed firmware239 firmware = download_image(firmware, self.context,240 self._tmpdir,241 decompress=False)242 self._boot_tags['{FIRMWARE}'] = self._get_rel_path(firmware, self._base_tmpdir)243 if self._is_uboot():244 self._boot_tags['{BOOTX}'] = self._get_uboot_boot_command(kernel,245 ramdisk,246 dtb)247 def deploy_linaro(self, hwpack, rfs, dtb, rootfstype, bootfstype,248 bootloadertype, qemu_pflash=None):249 self._uboot_boot = False250 super(BootloaderTarget, self).deploy_linaro(hwpack, rfs, dtb,251 rootfstype, bootfstype,252 bootloadertype, qemu_pflash=qemu_pflash)253 def deploy_linaro_prebuilt(self, image, dtb, rootfstype, bootfstype, bootloadertype, qemu_pflash=None):254 self._uboot_boot = False255 if self._is_ipxe():256 if image is not None:257 self._ipxe_boot = True258 # We are not booted yet259 self._booted = False260 # We specify OE deployment data, vanilla as possible261 self.deployment_data = deployment_data.oe262 # We have been passed a image263 image = download_image(image, self.context,264 self._tmpdir,265 decompress=False)266 image_url = self._get_http_url(image)267 # We are booting an image, can be iso or whole disk268 self._boot_tags['{IMAGE}'] = image_url269 else:270 raise CriticalError("No image to boot")271 else:272 super(BootloaderTarget, self).deploy_linaro_prebuilt(image,273 dtb,274 rootfstype,275 bootfstype,276 bootloadertype,277 qemu_pflash=qemu_pflash)278 def _run_boot(self):279 self._load_test_firmware()280 self._enter_bootloader(self.proc)281 boot_cmds = self._load_boot_cmds(default=self._default_boot_cmds,282 boot_tags=self._boot_tags)283 # Sometimes a command must be run to clear u-boot console buffer284 if self.config.pre_boot_cmd:285 self.proc.sendline(self.config.pre_boot_cmd,286 send_char=self.config.send_char)287 self._customize_bootloader(self.proc, boot_cmds)288 self._monitor_boot(self.proc, self.tester_ps1, self.tester_ps1_pattern)289 def _boot_linaro_image(self):290 if self.proc:291 logging.info(self.config.connection_command_terminate)292 if self.config.connection_command_terminate:293 self.proc.sendline(self.config.connection_command_terminate)294 finalize_process(self.proc)295 self.proc = None296 self.proc = connect_to_serial(self.context)297 if self._is_bootloader() and not self._booted:298 if self.config.hard_reset_command or self.config.hard_reset_command == "":299 self._hard_reboot(self.proc)300 self._run_boot()301 else:302 self._soft_reboot(self.proc)303 self._run_boot()304 self._booted = True305 elif self._is_bootloader() and self._booted:306 self.proc.sendline('export PS1="%s"'307 % self.tester_ps1,308 send_char=self.config.send_char)309 else:310 super(BootloaderTarget, self)._boot_linaro_image()311 def is_booted(self):312 return self._booted313 def reset_boot(self, in_test_shell=True):314 self._reset_boot = True315 self._booted = False316 self._in_test_shell = in_test_shell317 @contextlib.contextmanager318 def file_system(self, partition, directory):319 if self._is_bootloader() and self._reset_boot:320 self._reset_boot = False321 if self._in_test_shell:322 self._in_test_shell = False323 raise Exception("Operation timed out, resetting platform!")324 if self._is_bootloader() and not self._booted:325 self.context.client.boot_linaro_image()326 if self._is_bootloader() and self._lava_nfsrootfs:327 path = '%s/%s' % (self._lava_nfsrootfs, directory)328 ensure_directory(path)329 yield path330 elif self._is_bootloader():331 pat = self.tester_ps1_pattern332 incrc = self.tester_ps1_includes_rc333 runner = NetworkCommandRunner(self, pat, incrc)334 with self._busybox_file_system(runner, directory) as path:335 yield path336 else:337 with super(BootloaderTarget, self).file_system(338 partition, directory) as path:339 yield path...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!