def dfu_write_read_check(size): """Test DFU transfers of a specific size of data This function first writes data to the board then reads it back and compares the written and read back data. Measures are taken to avoid certain types of false positives. Args: size: The data size to test. Returns: Nothing. """ test_f = u_boot_utils.PersistentRandomFile(u_boot_console, 'dfu_%d.bin' % size, size) readback_fn = u_boot_console.config.result_dir + '/dfu_readback.bin' u_boot_console.log.action('Writing test data to DFU primary ' + 'altsetting') dfu_write(0, test_f.abs_fn) u_boot_console.log.action('Writing dummy data to DFU secondary ' + 'altsetting to clear DFU buffers') dfu_write(1, dummy_f.abs_fn) u_boot_console.log.action('Reading DFU primary altsetting for ' + 'comparison') dfu_read(0, readback_fn) u_boot_console.log.action('Comparing written and read data') written_hash = test_f.content_hash read_back_hash = u_boot_utils.md5sum_file(readback_fn, size) assert (written_hash == read_back_hash)
def test_dfu(u_boot_console, env__usb_dev_port, env__dfu_config): """Test the "dfu" command; the host system must be able to enumerate a USB device when "dfu" is running, various DFU transfers are tested, and the USB device must disappear when "dfu" is aborted. Args: u_boot_console: A U-Boot console connection. env__usb_dev_port: The single USB device-mode port specification on which to run the test. See the file-level comment above for details of the format. env__dfu_config: The single DFU (memory region) configuration on which to run the test. See the file-level comment above for details of the format. Returns: Nothing. """ def start_dfu(): """Start U-Boot's dfu shell command. This also waits for the host-side USB enumeration process to complete. Args: None. Returns: Nothing. """ u_boot_utils.wait_until_file_open_fails( env__usb_dev_port['host_usb_dev_node'], True) fh = u_boot_utils.attempt_to_open_file( env__usb_dev_port['host_usb_dev_node']) if fh: fh.close() raise Exception('USB device present before dfu command invoked') u_boot_console.log.action( 'Starting long-running U-Boot dfu shell command') cmd = 'setenv dfu_alt_info "%s"' % env__dfu_config['alt_info'] u_boot_console.run_command(cmd) cmd = 'dfu 0 ' + env__dfu_config['cmd_params'] u_boot_console.run_command(cmd, wait_for_prompt=False) u_boot_console.log.action('Waiting for DFU USB device to appear') fh = u_boot_utils.wait_until_open_succeeds( env__usb_dev_port['host_usb_dev_node']) fh.close() def stop_dfu(ignore_errors): """Stop U-Boot's dfu shell command from executing. This also waits for the host-side USB de-enumeration process to complete. Args: ignore_errors: Ignore any errors. This is useful if an error has already been detected, and the code is performing best-effort cleanup. In this case, we do not want to mask the original error by "honoring" any new errors. Returns: Nothing. """ try: u_boot_console.log.action( 'Stopping long-running U-Boot dfu shell command') u_boot_console.ctrlc() u_boot_console.log.action( 'Waiting for DFU USB device to disappear') u_boot_utils.wait_until_file_open_fails( env__usb_dev_port['host_usb_dev_node'], ignore_errors) except: if not ignore_errors: raise def run_dfu_util(alt_setting, fn, up_dn_load_arg): """Invoke dfu-util on the host. Args: alt_setting: The DFU "alternate setting" identifier to interact with. fn: The host-side file name to transfer. up_dn_load_arg: '-U' or '-D' depending on whether a DFU upload or download operation should be performed. Returns: Nothing. """ cmd = ['dfu-util', '-a', str(alt_setting), up_dn_load_arg, fn] if 'host_usb_port_path' in env__usb_dev_port: cmd += ['-p', env__usb_dev_port['host_usb_port_path']] u_boot_utils.run_and_log(u_boot_console, cmd) u_boot_console.wait_for('Ctrl+C to exit ...') def dfu_write(alt_setting, fn): """Write a file to the target board using DFU. Args: alt_setting: The DFU "alternate setting" identifier to interact with. fn: The host-side file name to transfer. Returns: Nothing. """ run_dfu_util(alt_setting, fn, '-D') def dfu_read(alt_setting, fn): """Read a file from the target board using DFU. Args: alt_setting: The DFU "alternate setting" identifier to interact with. fn: The host-side file name to transfer. Returns: Nothing. """ # dfu-util fails reads/uploads if the host file already exists if os.path.exists(fn): os.remove(fn) run_dfu_util(alt_setting, fn, '-U') def dfu_write_read_check(size): """Test DFU transfers of a specific size of data This function first writes data to the board then reads it back and compares the written and read back data. Measures are taken to avoid certain types of false positives. Args: size: The data size to test. Returns: Nothing. """ test_f = u_boot_utils.PersistentRandomFile(u_boot_console, 'dfu_%d.bin' % size, size) readback_fn = u_boot_console.config.result_dir + '/dfu_readback.bin' u_boot_console.log.action('Writing test data to DFU primary ' + 'altsetting') dfu_write(0, test_f.abs_fn) u_boot_console.log.action('Writing dummy data to DFU secondary ' + 'altsetting to clear DFU buffers') dfu_write(1, dummy_f.abs_fn) u_boot_console.log.action('Reading DFU primary altsetting for ' + 'comparison') dfu_read(0, readback_fn) u_boot_console.log.action('Comparing written and read data') written_hash = test_f.content_hash read_back_hash = u_boot_utils.md5sum_file(readback_fn, size) assert (written_hash == read_back_hash) # This test may be executed against multiple USB ports. The test takes a # long time, so we don't want to do the whole thing each time. Instead, # execute the full test on the first USB port, and perform a very limited # test on other ports. In the limited case, we solely validate that the # host PC can enumerate the U-Boot USB device. global first_usb_dev_port if not first_usb_dev_port: first_usb_dev_port = env__usb_dev_port if env__usb_dev_port == first_usb_dev_port: sizes = env__dfu_config.get('test_sizes', test_sizes_default) else: sizes = [] dummy_f = u_boot_utils.PersistentRandomFile(u_boot_console, 'dfu_dummy.bin', 1024) ignore_cleanup_errors = True try: start_dfu() u_boot_console.log.action( 'Overwriting DFU primary altsetting with dummy data') dfu_write(0, dummy_f.abs_fn) for size in sizes: with u_boot_console.log.section('Data size %d' % size): dfu_write_read_check(size) # Make the status of each sub-test obvious. If the test didn't # pass, an exception was thrown so this code isn't executed. u_boot_console.log.status_pass('OK') ignore_cleanup_errors = False finally: stop_dfu(ignore_cleanup_errors)
def test_ums(u_boot_console, env__usb_dev_port, env__block_devs): """Test the "ums" command; the host system must be able to enumerate a UMS device when "ums" is running, block and optionally file I/O are tested, and this device must disappear when "ums" is aborted. Args: u_boot_console: A U-Boot console connection. env__usb_dev_port: The single USB device-mode port specification on which to run the test. See the file-level comment above for details of the format. env__block_devs: The list of block devices that the target U-Boot device has attached. See the file-level comment above for details of the format. Returns: Nothing. """ have_writable_fs_partition = 'writable_fs_partition' in env__block_devs[0] if not have_writable_fs_partition: # If 'writable_fs_subdir' is missing, we'll skip all parts of the # testing which mount filesystems. u_boot_console.log.warning( 'boardenv missing "writable_fs_partition"; ' + 'UMS testing will be limited.') tgt_usb_ctlr = env__usb_dev_port['tgt_usb_ctlr'] host_ums_dev_node = env__usb_dev_port['host_ums_dev_node'] # We're interested in testing USB device mode on each port, not the cross- # product of that with each device. So, just pick the first entry in the # device list here. We'll test each block device somewhere else. tgt_dev_type = env__block_devs[0]['type'] tgt_dev_id = env__block_devs[0]['id'] if have_writable_fs_partition: mount_point = u_boot_console.config.env['env__mount_points'][0] mount_subdir = env__block_devs[0]['writable_fs_subdir'] part_num = env__block_devs[0]['writable_fs_partition'] host_ums_part_node = '%s-part%d' % (host_ums_dev_node, part_num) else: host_ums_part_node = host_ums_dev_node test_f = u_boot_utils.PersistentRandomFile(u_boot_console, 'ums.bin', 1024 * 1024); if have_writable_fs_partition: mounted_test_fn = mount_point + '/' + mount_subdir + test_f.fn def start_ums(): """Start U-Boot's ums shell command. This also waits for the host-side USB enumeration process to complete. Args: None. Returns: Nothing. """ u_boot_console.log.action( 'Starting long-running U-Boot ums shell command') cmd = 'ums %s %s %s' % (tgt_usb_ctlr, tgt_dev_type, tgt_dev_id) u_boot_console.run_command(cmd, wait_for_prompt=False) u_boot_console.wait_for(re.compile('UMS: LUN.*[\r\n]')) fh = u_boot_utils.wait_until_open_succeeds(host_ums_part_node) u_boot_console.log.action('Reading raw data from UMS device') fh.read(4096) fh.close() def mount(): """Mount the block device that U-Boot exports. Args: None. Returns: Nothing. """ u_boot_console.log.action('Mounting exported UMS device') cmd = ('/bin/mount', host_ums_part_node) u_boot_utils.run_and_log(u_boot_console, cmd) def umount(ignore_errors): """Unmount the block device that U-Boot exports. Args: ignore_errors: Ignore any errors. This is useful if an error has already been detected, and the code is performing best-effort cleanup. In this case, we do not want to mask the original error by "honoring" any new errors. Returns: Nothing. """ u_boot_console.log.action('Unmounting UMS device') cmd = ('/bin/umount', host_ums_part_node) u_boot_utils.run_and_log(u_boot_console, cmd, ignore_errors) def stop_ums(ignore_errors): """Stop U-Boot's ums shell command from executing. This also waits for the host-side USB de-enumeration process to complete. Args: ignore_errors: Ignore any errors. This is useful if an error has already been detected, and the code is performing best-effort cleanup. In this case, we do not want to mask the original error by "honoring" any new errors. Returns: Nothing. """ u_boot_console.log.action( 'Stopping long-running U-Boot ums shell command') u_boot_console.ctrlc() u_boot_utils.wait_until_file_open_fails(host_ums_part_node, ignore_errors) ignore_cleanup_errors = True try: start_ums() if not have_writable_fs_partition: # Skip filesystem-based testing if not configured return try: mount() u_boot_console.log.action('Writing test file via UMS') cmd = ('rm', '-f', mounted_test_fn) u_boot_utils.run_and_log(u_boot_console, cmd) if os.path.exists(mounted_test_fn): raise Exception('Could not rm target UMS test file') cmd = ('cp', test_f.abs_fn, mounted_test_fn) u_boot_utils.run_and_log(u_boot_console, cmd) ignore_cleanup_errors = False finally: umount(ignore_errors=ignore_cleanup_errors) finally: stop_ums(ignore_errors=ignore_cleanup_errors) ignore_cleanup_errors = True try: start_ums() try: mount() u_boot_console.log.action('Reading test file back via UMS') read_back_hash = u_boot_utils.md5sum_file(mounted_test_fn) cmd = ('rm', '-f', mounted_test_fn) u_boot_utils.run_and_log(u_boot_console, cmd) ignore_cleanup_errors = False finally: umount(ignore_errors=ignore_cleanup_errors) finally: stop_ums(ignore_errors=ignore_cleanup_errors) written_hash = test_f.content_hash assert(written_hash == read_back_hash)