blob: 5876a137463fbfbcbdedb7611ab374109a715a7c [file] [log] [blame]
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +03001# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2018 Texas Instruments, <www.ti.com>
3
4# Test A/B update commands.
5
6import os
7import pytest
Simon Glassfb916372025-02-09 09:07:15 -07008import utils
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +03009
10class ABTestDiskImage(object):
11 """Disk Image used by the A/B tests."""
12
Simon Glassddba5202025-02-09 09:07:14 -070013 def __init__(self, ubman):
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030014 """Initialize a new ABTestDiskImage object.
15
16 Args:
Simon Glassddba5202025-02-09 09:07:14 -070017 ubman: A U-Boot console.
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030018
19 Returns:
20 Nothing.
21 """
22
23 filename = 'test_ab_disk_image.bin'
24
Simon Glassddba5202025-02-09 09:07:14 -070025 persistent = ubman.config.persistent_data_dir + '/' + filename
26 self.path = ubman.config.result_dir + '/' + filename
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030027
Simon Glassfb916372025-02-09 09:07:15 -070028 with utils.persistent_file_helper(ubman.log, persistent):
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030029 if os.path.exists(persistent):
Simon Glassddba5202025-02-09 09:07:14 -070030 ubman.log.action('Disk image file ' + persistent +
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030031 ' already exists')
32 else:
Simon Glassddba5202025-02-09 09:07:14 -070033 ubman.log.action('Generating ' + persistent)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030034 fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
35 os.ftruncate(fd, 524288)
36 os.close(fd)
37 cmd = ('sgdisk', persistent)
Simon Glassfb916372025-02-09 09:07:15 -070038 utils.run_and_log(ubman, cmd)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030039
40 cmd = ('sgdisk', '--new=1:64:512', '--change-name=1:misc',
41 persistent)
Simon Glassfb916372025-02-09 09:07:15 -070042 utils.run_and_log(ubman, cmd)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030043 cmd = ('sgdisk', '--load-backup=' + persistent)
Simon Glassfb916372025-02-09 09:07:15 -070044 utils.run_and_log(ubman, cmd)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030045
46 cmd = ('cp', persistent, self.path)
Simon Glassfb916372025-02-09 09:07:15 -070047 utils.run_and_log(ubman, cmd)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030048
49di = None
50@pytest.fixture(scope='function')
Simon Glassddba5202025-02-09 09:07:14 -070051def ab_disk_image(ubman):
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030052 global di
53 if not di:
Simon Glassddba5202025-02-09 09:07:14 -070054 di = ABTestDiskImage(ubman)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030055 return di
56
Simon Glassddba5202025-02-09 09:07:14 -070057def ab_dump(ubman, slot_num, crc):
58 output = ubman.run_command('bcb ab_dump host 0#misc')
Dmitry Rokosov52f93d32024-10-17 17:12:10 +030059 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 Trofymenkoec264b42019-07-05 15:37:34 +030078@pytest.mark.boardspec('sandbox')
79@pytest.mark.buildconfigspec('android_ab')
Dmitry Rokosovea143cb2024-10-17 17:12:08 +030080@pytest.mark.buildconfigspec('cmd_bcb')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030081@pytest.mark.requiredtool('sgdisk')
Simon Glassddba5202025-02-09 09:07:14 -070082def test_ab(ab_disk_image, ubman):
Dmitry Rokosovea143cb2024-10-17 17:12:08 +030083 """Test the 'bcb ab_select' command."""
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030084
Simon Glassddba5202025-02-09 09:07:14 -070085 ubman.run_command('host bind 0 ' + ab_disk_image.path)
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030086
Simon Glassddba5202025-02-09 09:07:14 -070087 output = ubman.run_command('bcb ab_select slot_name host 0#misc')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030088 assert 're-initializing A/B metadata' in output
89 assert 'Attempting slot a, tries remaining 7' in output
Simon Glassddba5202025-02-09 09:07:14 -070090 output = ubman.run_command('printenv slot_name')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030091 assert 'slot_name=a' in output
Simon Glassddba5202025-02-09 09:07:14 -070092 ab_dump(ubman, 0, '0xd438d1b9')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030093
Simon Glassddba5202025-02-09 09:07:14 -070094 output = ubman.run_command('bcb ab_select slot_name host 0:1')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030095 assert 'Attempting slot b, tries remaining 7' in output
Simon Glassddba5202025-02-09 09:07:14 -070096 output = ubman.run_command('printenv slot_name')
Ruslan Trofymenkoec264b42019-07-05 15:37:34 +030097 assert 'slot_name=b' in output
Simon Glassddba5202025-02-09 09:07:14 -070098 ab_dump(ubman, 1, '0x011ec016')