Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
| 2 | # (C) Copyright 2018 Texas Instruments, <www.ti.com> |
| 3 | |
| 4 | # Test A/B update commands. |
| 5 | |
| 6 | import os |
| 7 | import pytest |
| 8 | import u_boot_utils |
| 9 | |
| 10 | class ABTestDiskImage(object): |
| 11 | """Disk Image used by the A/B tests.""" |
| 12 | |
| 13 | def __init__(self, u_boot_console): |
| 14 | """Initialize a new ABTestDiskImage object. |
| 15 | |
| 16 | Args: |
| 17 | u_boot_console: A U-Boot console. |
| 18 | |
| 19 | Returns: |
| 20 | Nothing. |
| 21 | """ |
| 22 | |
| 23 | filename = 'test_ab_disk_image.bin' |
| 24 | |
| 25 | persistent = u_boot_console.config.persistent_data_dir + '/' + filename |
| 26 | self.path = u_boot_console.config.result_dir + '/' + filename |
| 27 | |
| 28 | with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent): |
| 29 | if os.path.exists(persistent): |
| 30 | u_boot_console.log.action('Disk image file ' + persistent + |
| 31 | ' already exists') |
| 32 | else: |
| 33 | u_boot_console.log.action('Generating ' + persistent) |
| 34 | fd = os.open(persistent, os.O_RDWR | os.O_CREAT) |
| 35 | os.ftruncate(fd, 524288) |
| 36 | os.close(fd) |
| 37 | cmd = ('sgdisk', persistent) |
| 38 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 39 | |
| 40 | cmd = ('sgdisk', '--new=1:64:512', '--change-name=1:misc', |
| 41 | persistent) |
| 42 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 43 | cmd = ('sgdisk', '--load-backup=' + persistent) |
| 44 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 45 | |
| 46 | cmd = ('cp', persistent, self.path) |
| 47 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 48 | |
| 49 | di = None |
| 50 | @pytest.fixture(scope='function') |
| 51 | def ab_disk_image(u_boot_console): |
| 52 | global di |
| 53 | if not di: |
| 54 | di = ABTestDiskImage(u_boot_console) |
| 55 | return di |
| 56 | |
Dmitry Rokosov | 52f93d3 | 2024-10-17 17:12:10 +0300 | [diff] [blame] | 57 | def ab_dump(u_boot_console, slot_num, crc): |
| 58 | output = u_boot_console.run_command('bcb ab_dump host 0#misc') |
| 59 | header, slot0, slot1 = output.split('\r\r\n\r\r\n') |
| 60 | slots = [slot0, slot1] |
| 61 | slot_suffixes = ['_a', '_b'] |
| 62 | |
| 63 | header = dict(map(lambda x: map(str.strip, x.split(':')), header.split('\r\r\n'))) |
| 64 | assert header['Bootloader Control'] == '[misc]' |
| 65 | assert header['Active Slot'] == slot_suffixes[slot_num] |
| 66 | assert header['Magic Number'] == '0x42414342' |
| 67 | assert header['Version'] == '1' |
| 68 | assert header['Number of Slots'] == '2' |
| 69 | assert header['Recovery Tries Remaining'] == '0' |
| 70 | assert header['CRC'] == '{} (Valid)'.format(crc) |
| 71 | |
| 72 | slot = dict(map(lambda x: map(str.strip, x.split(':')), slots[slot_num].split('\r\r\n\t- ')[1:])) |
| 73 | assert slot['Priority'] == '15' |
| 74 | assert slot['Tries Remaining'] == '6' |
| 75 | assert slot['Successful Boot'] == '0' |
| 76 | assert slot['Verity Corrupted'] == '0' |
| 77 | |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 78 | @pytest.mark.boardspec('sandbox') |
| 79 | @pytest.mark.buildconfigspec('android_ab') |
Dmitry Rokosov | ea143cb | 2024-10-17 17:12:08 +0300 | [diff] [blame] | 80 | @pytest.mark.buildconfigspec('cmd_bcb') |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 81 | @pytest.mark.requiredtool('sgdisk') |
| 82 | def test_ab(ab_disk_image, u_boot_console): |
Dmitry Rokosov | ea143cb | 2024-10-17 17:12:08 +0300 | [diff] [blame] | 83 | """Test the 'bcb ab_select' command.""" |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 84 | |
| 85 | u_boot_console.run_command('host bind 0 ' + ab_disk_image.path) |
| 86 | |
Dmitry Rokosov | ea143cb | 2024-10-17 17:12:08 +0300 | [diff] [blame] | 87 | output = u_boot_console.run_command('bcb ab_select slot_name host 0#misc') |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 88 | assert 're-initializing A/B metadata' in output |
| 89 | assert 'Attempting slot a, tries remaining 7' in output |
| 90 | output = u_boot_console.run_command('printenv slot_name') |
| 91 | assert 'slot_name=a' in output |
Dmitry Rokosov | 52f93d3 | 2024-10-17 17:12:10 +0300 | [diff] [blame] | 92 | ab_dump(u_boot_console, 0, '0xd438d1b9') |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 93 | |
Dmitry Rokosov | ea143cb | 2024-10-17 17:12:08 +0300 | [diff] [blame] | 94 | output = u_boot_console.run_command('bcb ab_select slot_name host 0:1') |
Ruslan Trofymenko | ec264b4 | 2019-07-05 15:37:34 +0300 | [diff] [blame] | 95 | assert 'Attempting slot b, tries remaining 7' in output |
| 96 | output = u_boot_console.run_command('printenv slot_name') |
| 97 | assert 'slot_name=b' in output |
Dmitry Rokosov | 52f93d3 | 2024-10-17 17:12:10 +0300 | [diff] [blame] | 98 | ab_dump(u_boot_console, 1, '0x011ec016') |