1*83d290c5STom Rini# SPDX-License-Identifier: GPL-2.0 2a2f42255SAlison Chaiken# Copyright (c) 2017 Alison Chaiken 3110ba625SStephen Warren# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 4a2f42255SAlison Chaiken 5a2f42255SAlison Chaiken# Test GPT manipulation commands. 6a2f42255SAlison Chaiken 7a2f42255SAlison Chaikenimport os 8a2f42255SAlison Chaikenimport pytest 9a2f42255SAlison Chaikenimport u_boot_utils 10a2f42255SAlison Chaiken 11a2f42255SAlison Chaiken""" 12110ba625SStephen WarrenThese tests rely on a 4 MB disk image, which is automatically created by 13110ba625SStephen Warrenthe test. 14a2f42255SAlison Chaiken""" 15a2f42255SAlison Chaiken 16110ba625SStephen Warrenclass GptTestDiskImage(object): 17110ba625SStephen Warren """Disk Image used by the GPT tests.""" 18110ba625SStephen Warren 19110ba625SStephen Warren def __init__(self, u_boot_console): 20110ba625SStephen Warren """Initialize a new GptTestDiskImage object. 21110ba625SStephen Warren 22110ba625SStephen Warren Args: 23110ba625SStephen Warren u_boot_console: A U-Boot console. 24110ba625SStephen Warren 25110ba625SStephen Warren Returns: 26110ba625SStephen Warren Nothing. 27110ba625SStephen Warren """ 28110ba625SStephen Warren 29110ba625SStephen Warren filename = 'test_gpt_disk_image.bin' 30110ba625SStephen Warren 31da4c4bbdSPatrick Delaunay persistent = u_boot_console.config.persistent_data_dir + '/' + filename 32da4c4bbdSPatrick Delaunay self.path = u_boot_console.config.result_dir + '/' + filename 33da4c4bbdSPatrick Delaunay 34ac122efdSStephen Warren with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent): 35da4c4bbdSPatrick Delaunay if os.path.exists(persistent): 36da4c4bbdSPatrick Delaunay u_boot_console.log.action('Disk image file ' + persistent + 37110ba625SStephen Warren ' already exists') 38110ba625SStephen Warren else: 39da4c4bbdSPatrick Delaunay u_boot_console.log.action('Generating ' + persistent) 40da4c4bbdSPatrick Delaunay fd = os.open(persistent, os.O_RDWR | os.O_CREAT) 41110ba625SStephen Warren os.ftruncate(fd, 4194304) 42110ba625SStephen Warren os.close(fd) 432d26bf6cSStephen Warren cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe', 44da4c4bbdSPatrick Delaunay persistent) 45110ba625SStephen Warren u_boot_utils.run_and_log(u_boot_console, cmd) 46f0314648SPatrick Delaunay # part1 offset 1MB size 1MB 47f0314648SPatrick Delaunay cmd = ('sgdisk', '--new=1:2048:4095', '-c 1:part1', persistent) 48f0314648SPatrick Delaunay # part2 offset 2MB size 1.5MB 49110ba625SStephen Warren u_boot_utils.run_and_log(u_boot_console, cmd) 50f0314648SPatrick Delaunay cmd = ('sgdisk', '--new=2:4096:7167', '-c 2:part2', persistent) 51110ba625SStephen Warren u_boot_utils.run_and_log(u_boot_console, cmd) 52da4c4bbdSPatrick Delaunay cmd = ('sgdisk', '-l', persistent) 53da4c4bbdSPatrick Delaunay u_boot_utils.run_and_log(u_boot_console, cmd) 54da4c4bbdSPatrick Delaunay 55da4c4bbdSPatrick Delaunay cmd = ('cp', persistent, self.path) 56110ba625SStephen Warren u_boot_utils.run_and_log(u_boot_console, cmd) 57110ba625SStephen Warren 58110ba625SStephen Warrengtdi = None 59110ba625SStephen Warren@pytest.fixture(scope='function') 60110ba625SStephen Warrendef state_disk_image(u_boot_console): 61110ba625SStephen Warren """pytest fixture to provide a GptTestDiskImage object to tests. 62110ba625SStephen Warren This is function-scoped because it uses u_boot_console, which is also 63110ba625SStephen Warren function-scoped. However, we don't need to actually do any function-scope 64110ba625SStephen Warren work, so this simply returns the same object over and over each time.""" 65110ba625SStephen Warren 66110ba625SStephen Warren global gtdi 67110ba625SStephen Warren if not gtdi: 68110ba625SStephen Warren gtdi = GptTestDiskImage(u_boot_console) 69110ba625SStephen Warren return gtdi 70110ba625SStephen Warren 71110ba625SStephen Warren@pytest.mark.boardspec('sandbox') 72a2f42255SAlison Chaiken@pytest.mark.buildconfigspec('cmd_gpt') 7330ef7cbbSPatrick Delaunay@pytest.mark.buildconfigspec('cmd_part') 7430ef7cbbSPatrick Delaunay@pytest.mark.requiredtool('sgdisk') 7530ef7cbbSPatrick Delaunaydef test_gpt_read(state_disk_image, u_boot_console): 7630ef7cbbSPatrick Delaunay """Test the gpt read command.""" 7730ef7cbbSPatrick Delaunay 7830ef7cbbSPatrick Delaunay u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 7930ef7cbbSPatrick Delaunay output = u_boot_console.run_command('gpt read host 0') 80f0314648SPatrick Delaunay assert 'Start 1MiB, size 1MiB' in output 8130ef7cbbSPatrick Delaunay assert 'Block size 512, name part1' in output 82f0314648SPatrick Delaunay assert 'Start 2MiB, size 1MiB' in output 8330ef7cbbSPatrick Delaunay assert 'Block size 512, name part2' in output 8430ef7cbbSPatrick Delaunay output = u_boot_console.run_command('part list host 0') 85f0314648SPatrick Delaunay assert '0x00000800 0x00000fff "part1"' in output 86f0314648SPatrick Delaunay assert '0x00001000 0x00001bff "part2"' in output 8730ef7cbbSPatrick Delaunay 8830ef7cbbSPatrick Delaunay@pytest.mark.boardspec('sandbox') 8930ef7cbbSPatrick Delaunay@pytest.mark.buildconfigspec('cmd_gpt') 9030ef7cbbSPatrick Delaunay@pytest.mark.requiredtool('sgdisk') 9130ef7cbbSPatrick Delaunaydef test_gpt_verify(state_disk_image, u_boot_console): 9230ef7cbbSPatrick Delaunay """Test the gpt verify command.""" 9330ef7cbbSPatrick Delaunay 9430ef7cbbSPatrick Delaunay u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 9530ef7cbbSPatrick Delaunay output = u_boot_console.run_command('gpt verify host 0') 9630ef7cbbSPatrick Delaunay assert 'Verify GPT: success!' in output 9730ef7cbbSPatrick Delaunay 9830ef7cbbSPatrick Delaunay@pytest.mark.boardspec('sandbox') 9930ef7cbbSPatrick Delaunay@pytest.mark.buildconfigspec('cmd_gpt') 1002d26bf6cSStephen Warren@pytest.mark.requiredtool('sgdisk') 101110ba625SStephen Warrendef test_gpt_guid(state_disk_image, u_boot_console): 102a2f42255SAlison Chaiken """Test the gpt guid command.""" 103a2f42255SAlison Chaiken 104110ba625SStephen Warren u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 105a2f42255SAlison Chaiken output = u_boot_console.run_command('gpt guid host 0') 106a2f42255SAlison Chaiken assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output 107a2f42255SAlison Chaiken 108110ba625SStephen Warren@pytest.mark.boardspec('sandbox') 109a2f42255SAlison Chaiken@pytest.mark.buildconfigspec('cmd_gpt') 1102d26bf6cSStephen Warren@pytest.mark.requiredtool('sgdisk') 111110ba625SStephen Warrendef test_gpt_save_guid(state_disk_image, u_boot_console): 112a2f42255SAlison Chaiken """Test the gpt guid command to save GUID into a string.""" 113a2f42255SAlison Chaiken 114a2f42255SAlison Chaiken if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': 115a2f42255SAlison Chaiken pytest.skip('gpt command not supported') 116110ba625SStephen Warren u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 117a2f42255SAlison Chaiken output = u_boot_console.run_command('gpt guid host 0 newguid') 118a2f42255SAlison Chaiken output = u_boot_console.run_command('printenv newguid') 119a2f42255SAlison Chaiken assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output 120c5772188SAlison Chaiken 121110ba625SStephen Warren@pytest.mark.boardspec('sandbox') 122c5772188SAlison Chaiken@pytest.mark.buildconfigspec('cmd_gpt') 123110ba625SStephen Warren@pytest.mark.buildconfigspec('cmd_gpt_rename') 1240cf02ff6SPatrick Delaunay@pytest.mark.buildconfigspec('cmd_part') 1252d26bf6cSStephen Warren@pytest.mark.requiredtool('sgdisk') 126110ba625SStephen Warrendef test_gpt_rename_partition(state_disk_image, u_boot_console): 127c5772188SAlison Chaiken """Test the gpt rename command to write partition names.""" 128c5772188SAlison Chaiken 129110ba625SStephen Warren u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 130c5772188SAlison Chaiken u_boot_console.run_command('gpt rename host 0 1 first') 131c5772188SAlison Chaiken output = u_boot_console.run_command('gpt read host 0') 132c5772188SAlison Chaiken assert 'name first' in output 133c5772188SAlison Chaiken u_boot_console.run_command('gpt rename host 0 2 second') 134c5772188SAlison Chaiken output = u_boot_console.run_command('gpt read host 0') 135c5772188SAlison Chaiken assert 'name second' in output 1360cf02ff6SPatrick Delaunay output = u_boot_console.run_command('part list host 0') 137f0314648SPatrick Delaunay assert '0x00000800 0x00000fff "first"' in output 138f0314648SPatrick Delaunay assert '0x00001000 0x00001bff "second"' in output 139c5772188SAlison Chaiken 140110ba625SStephen Warren@pytest.mark.boardspec('sandbox') 141c5772188SAlison Chaiken@pytest.mark.buildconfigspec('cmd_gpt') 142110ba625SStephen Warren@pytest.mark.buildconfigspec('cmd_gpt_rename') 143110ba625SStephen Warren@pytest.mark.buildconfigspec('cmd_part') 1442d26bf6cSStephen Warren@pytest.mark.requiredtool('sgdisk') 145110ba625SStephen Warrendef test_gpt_swap_partitions(state_disk_image, u_boot_console): 146c5772188SAlison Chaiken """Test the gpt swap command to exchange two partition names.""" 147c5772188SAlison Chaiken 148110ba625SStephen Warren u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 149c5772188SAlison Chaiken output = u_boot_console.run_command('part list host 0') 150f0314648SPatrick Delaunay assert '0x00000800 0x00000fff "first"' in output 151f0314648SPatrick Delaunay assert '0x00001000 0x00001bff "second"' in output 152c5772188SAlison Chaiken u_boot_console.run_command('gpt swap host 0 first second') 153c5772188SAlison Chaiken output = u_boot_console.run_command('part list host 0') 154f0314648SPatrick Delaunay assert '0x00000800 0x00000fff "second"' in output 155f0314648SPatrick Delaunay assert '0x00001000 0x00001bff "first"' in output 156b61a3b5cSPatrick Delaunay 157b61a3b5cSPatrick Delaunay@pytest.mark.boardspec('sandbox') 158b61a3b5cSPatrick Delaunay@pytest.mark.buildconfigspec('cmd_gpt') 159b61a3b5cSPatrick Delaunay@pytest.mark.buildconfigspec('cmd_part') 160b61a3b5cSPatrick Delaunay@pytest.mark.requiredtool('sgdisk') 161b61a3b5cSPatrick Delaunaydef test_gpt_write(state_disk_image, u_boot_console): 162b61a3b5cSPatrick Delaunay """Test the gpt write command.""" 163b61a3b5cSPatrick Delaunay 164b61a3b5cSPatrick Delaunay u_boot_console.run_command('host bind 0 ' + state_disk_image.path) 165b61a3b5cSPatrick Delaunay output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"') 166b61a3b5cSPatrick Delaunay assert 'Writing GPT: success!' in output 167b61a3b5cSPatrick Delaunay output = u_boot_console.run_command('part list host 0') 168b61a3b5cSPatrick Delaunay assert '0x00000022 0x00001fde "all"' in output 169f0314648SPatrick Delaunay output = u_boot_console.run_command('gpt write host 0 "uuid_disk=375a56f7-d6c9-4e81-b5f0-09d41ca89efe;name=first,start=1M,size=1M;name=second,start=0x200000,size=0x180000;"') 170b61a3b5cSPatrick Delaunay assert 'Writing GPT: success!' in output 171b61a3b5cSPatrick Delaunay output = u_boot_console.run_command('part list host 0') 172f0314648SPatrick Delaunay assert '0x00000800 0x00000fff "first"' in output 173f0314648SPatrick Delaunay assert '0x00001000 0x00001bff "second"' in output 174b61a3b5cSPatrick Delaunay output = u_boot_console.run_command('gpt guid host 0') 175b61a3b5cSPatrick Delaunay assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output 176