Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 2 | # Copyright (c) 2017 Alison Chaiken |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 3 | # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 4 | |
| 5 | # Test GPT manipulation commands. |
| 6 | |
| 7 | import os |
| 8 | import pytest |
| 9 | import u_boot_utils |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 10 | |
| 11 | """ |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 12 | These tests rely on a 4 MB disk image, which is automatically created by |
| 13 | the test. |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 14 | """ |
| 15 | |
Simon Glass | dddd077 | 2022-08-06 17:51:53 -0600 | [diff] [blame] | 16 | # Mark all tests here as slow |
| 17 | pytestmark = pytest.mark.slow |
| 18 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 19 | class GptTestDiskImage(object): |
| 20 | """Disk Image used by the GPT tests.""" |
| 21 | |
| 22 | def __init__(self, u_boot_console): |
| 23 | """Initialize a new GptTestDiskImage object. |
| 24 | |
| 25 | Args: |
| 26 | u_boot_console: A U-Boot console. |
| 27 | |
| 28 | Returns: |
| 29 | Nothing. |
| 30 | """ |
| 31 | |
| 32 | filename = 'test_gpt_disk_image.bin' |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 33 | |
Patrick Delaunay | e9e3a3e | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 34 | persistent = u_boot_console.config.persistent_data_dir + '/' + filename |
| 35 | self.path = u_boot_console.config.result_dir + '/' + filename |
| 36 | |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 37 | with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent): |
| 38 | if os.path.exists(persistent): |
| 39 | u_boot_console.log.action('Disk image file ' + persistent + |
| 40 | ' already exists') |
| 41 | else: |
| 42 | u_boot_console.log.action('Generating ' + persistent) |
| 43 | fd = os.open(persistent, os.O_RDWR | os.O_CREAT) |
| 44 | os.ftruncate(fd, 4194304) |
| 45 | os.close(fd) |
Sam Protsenko | 13616a9 | 2019-07-02 21:20:32 +0300 | [diff] [blame] | 46 | cmd = ('sgdisk', |
| 47 | '--disk-guid=375a56f7-d6c9-4e81-b5f0-09d41ca89efe', |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 48 | persistent) |
| 49 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 50 | # part1 offset 1MB size 1MB |
Sam Protsenko | 13616a9 | 2019-07-02 21:20:32 +0300 | [diff] [blame] | 51 | cmd = ('sgdisk', '--new=1:2048:4095', '--change-name=1:part1', |
Joshua Watt | fd1134e | 2023-08-31 10:51:37 -0600 | [diff] [blame^] | 52 | '-A 1:set:2', |
Sam Protsenko | 13616a9 | 2019-07-02 21:20:32 +0300 | [diff] [blame] | 53 | persistent) |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 54 | # part2 offset 2MB size 1.5MB |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 55 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Sam Protsenko | 13616a9 | 2019-07-02 21:20:32 +0300 | [diff] [blame] | 56 | cmd = ('sgdisk', '--new=2:4096:7167', '--change-name=2:part2', |
| 57 | persistent) |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 58 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Sam Protsenko | 13616a9 | 2019-07-02 21:20:32 +0300 | [diff] [blame] | 59 | cmd = ('sgdisk', '--load-backup=' + persistent) |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 60 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 61 | |
Patrick Delaunay | e9e3a3e | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 62 | cmd = ('cp', persistent, self.path) |
| 63 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 64 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 65 | @pytest.fixture(scope='function') |
| 66 | def state_disk_image(u_boot_console): |
| 67 | """pytest fixture to provide a GptTestDiskImage object to tests. |
| 68 | This is function-scoped because it uses u_boot_console, which is also |
Joshua Watt | d64d579 | 2023-08-31 10:51:36 -0600 | [diff] [blame] | 69 | function-scoped. A new disk is returned each time to prevent tests from |
| 70 | interfering with each other.""" |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 71 | |
Joshua Watt | d64d579 | 2023-08-31 10:51:36 -0600 | [diff] [blame] | 72 | return GptTestDiskImage(u_boot_console) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 73 | |
| 74 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 75 | @pytest.mark.buildconfigspec('cmd_gpt') |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 76 | @pytest.mark.buildconfigspec('cmd_part') |
| 77 | @pytest.mark.requiredtool('sgdisk') |
| 78 | def test_gpt_read(state_disk_image, u_boot_console): |
| 79 | """Test the gpt read command.""" |
| 80 | |
| 81 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 82 | output = u_boot_console.run_command('gpt read host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 83 | assert 'Start 1MiB, size 1MiB' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 84 | assert 'Block size 512, name part1' in output |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 85 | assert 'Start 2MiB, size 1MiB' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 86 | assert 'Block size 512, name part2' in output |
| 87 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 88 | assert '0x00000800 0x00000fff "part1"' in output |
| 89 | assert '0x00001000 0x00001bff "part2"' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 90 | |
| 91 | @pytest.mark.boardspec('sandbox') |
| 92 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 93 | @pytest.mark.requiredtool('sgdisk') |
| 94 | def test_gpt_verify(state_disk_image, u_boot_console): |
| 95 | """Test the gpt verify command.""" |
| 96 | |
| 97 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 98 | output = u_boot_console.run_command('gpt verify host 0') |
| 99 | assert 'Verify GPT: success!' in output |
| 100 | |
| 101 | @pytest.mark.boardspec('sandbox') |
| 102 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 103 | @pytest.mark.requiredtool('sgdisk') |
Philippe Reynes | dbeedc3 | 2022-04-22 17:46:50 +0200 | [diff] [blame] | 104 | def test_gpt_repair(state_disk_image, u_boot_console): |
| 105 | """Test the gpt repair command.""" |
| 106 | |
| 107 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 108 | output = u_boot_console.run_command('gpt repair host 0') |
| 109 | assert 'Repairing GPT: success!' in output |
| 110 | |
| 111 | @pytest.mark.boardspec('sandbox') |
| 112 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 113 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 114 | def test_gpt_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 115 | """Test the gpt guid command.""" |
| 116 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 117 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 118 | output = u_boot_console.run_command('gpt guid host 0') |
| 119 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
| 120 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 121 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 122 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 123 | @pytest.mark.requiredtool('sgdisk') |
Joshua Watt | fd1134e | 2023-08-31 10:51:37 -0600 | [diff] [blame^] | 124 | def test_gpt_setenv(state_disk_image, u_boot_console): |
| 125 | """Test the gpt setenv command.""" |
| 126 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 127 | output = u_boot_console.run_command('gpt setenv host 0 part1') |
| 128 | assert 'success!' in output |
| 129 | output = u_boot_console.run_command('echo ${gpt_partition_addr}') |
| 130 | assert output.rstrip() == '800' |
| 131 | output = u_boot_console.run_command('echo ${gpt_partition_size}') |
| 132 | assert output.rstrip() == '800' |
| 133 | output = u_boot_console.run_command('echo ${gpt_partition_name}') |
| 134 | assert output.rstrip() == 'part1' |
| 135 | output = u_boot_console.run_command('echo ${gpt_partition_entry}') |
| 136 | assert output.rstrip() == '1' |
| 137 | output = u_boot_console.run_command('echo ${gpt_partition_bootable}') |
| 138 | assert output.rstrip() == '1' |
| 139 | |
| 140 | output = u_boot_console.run_command('gpt setenv host 0 part2') |
| 141 | assert 'success!' in output |
| 142 | output = u_boot_console.run_command('echo ${gpt_partition_addr}') |
| 143 | assert output.rstrip() == '1000' |
| 144 | output = u_boot_console.run_command('echo ${gpt_partition_size}') |
| 145 | assert output.rstrip() == 'c00' |
| 146 | output = u_boot_console.run_command('echo ${gpt_partition_name}') |
| 147 | assert output.rstrip() == 'part2' |
| 148 | output = u_boot_console.run_command('echo ${gpt_partition_entry}') |
| 149 | assert output.rstrip() == '2' |
| 150 | output = u_boot_console.run_command('echo ${gpt_partition_bootable}') |
| 151 | assert output.rstrip() == '0' |
| 152 | |
| 153 | @pytest.mark.boardspec('sandbox') |
| 154 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 155 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 156 | def test_gpt_save_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 157 | """Test the gpt guid command to save GUID into a string.""" |
| 158 | |
| 159 | if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': |
| 160 | pytest.skip('gpt command not supported') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 161 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 162 | output = u_boot_console.run_command('gpt guid host 0 newguid') |
| 163 | output = u_boot_console.run_command('printenv newguid') |
| 164 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 165 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 166 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 167 | @pytest.mark.buildconfigspec('cmd_gpt') |
Enric Balletbo i Serra | bfd98c6 | 2023-01-10 17:19:35 +0100 | [diff] [blame] | 168 | @pytest.mark.requiredtool('sgdisk') |
| 169 | def test_gpt_part_type_uuid(state_disk_image, u_boot_console): |
| 170 | """Test the gpt partittion type UUID command.""" |
| 171 | |
| 172 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 173 | output = u_boot_console.run_command('part type host 0:1') |
| 174 | assert '0fc63daf-8483-4772-8e79-3d69d8477de4' in output |
| 175 | |
| 176 | @pytest.mark.boardspec('sandbox') |
| 177 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 178 | @pytest.mark.requiredtool('sgdisk') |
| 179 | def test_gpt_part_type_save_uuid(state_disk_image, u_boot_console): |
| 180 | """Test the gpt partittion type to save UUID into a string.""" |
| 181 | |
| 182 | if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': |
| 183 | pytest.skip('gpt command not supported') |
| 184 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 185 | output = u_boot_console.run_command('part type host 0:1 newguid') |
| 186 | output = u_boot_console.run_command('printenv newguid') |
| 187 | assert '0fc63daf-8483-4772-8e79-3d69d8477de4' in output |
| 188 | |
| 189 | @pytest.mark.boardspec('sandbox') |
| 190 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 191 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
Patrick Delaunay | 0ffbb64 | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 192 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 193 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 194 | def test_gpt_rename_partition(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 195 | """Test the gpt rename command to write partition names.""" |
| 196 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 197 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 198 | u_boot_console.run_command('gpt rename host 0 1 first') |
| 199 | output = u_boot_console.run_command('gpt read host 0') |
| 200 | assert 'name first' in output |
| 201 | u_boot_console.run_command('gpt rename host 0 2 second') |
| 202 | output = u_boot_console.run_command('gpt read host 0') |
| 203 | assert 'name second' in output |
Patrick Delaunay | 0ffbb64 | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 204 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 205 | assert '0x00000800 0x00000fff "first"' in output |
| 206 | assert '0x00001000 0x00001bff "second"' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 207 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 208 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 209 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 210 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
| 211 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 212 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 213 | def test_gpt_swap_partitions(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 214 | """Test the gpt swap command to exchange two partition names.""" |
| 215 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 216 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 217 | output = u_boot_console.run_command('part list host 0') |
Joshua Watt | d64d579 | 2023-08-31 10:51:36 -0600 | [diff] [blame] | 218 | assert '0x00000800 0x00000fff "part1"' in output |
| 219 | assert '0x00001000 0x00001bff "part2"' in output |
| 220 | u_boot_console.run_command('gpt swap host 0 part1 part2') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 221 | output = u_boot_console.run_command('part list host 0') |
Joshua Watt | d64d579 | 2023-08-31 10:51:36 -0600 | [diff] [blame] | 222 | assert '0x00000800 0x00000fff "part2"' in output |
| 223 | assert '0x00001000 0x00001bff "part1"' in output |
Patrick Delaunay | 5751c05 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 224 | |
| 225 | @pytest.mark.boardspec('sandbox') |
| 226 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 227 | @pytest.mark.buildconfigspec('cmd_part') |
| 228 | @pytest.mark.requiredtool('sgdisk') |
| 229 | def test_gpt_write(state_disk_image, u_boot_console): |
| 230 | """Test the gpt write command.""" |
| 231 | |
| 232 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 233 | output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"') |
| 234 | assert 'Writing GPT: success!' in output |
| 235 | output = u_boot_console.run_command('part list host 0') |
| 236 | assert '0x00000022 0x00001fde "all"' in output |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 237 | 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;"') |
Patrick Delaunay | 5751c05 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 238 | assert 'Writing GPT: success!' in output |
| 239 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 240 | assert '0x00000800 0x00000fff "first"' in output |
| 241 | assert '0x00001000 0x00001bff "second"' in output |
Patrick Delaunay | 5751c05 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 242 | output = u_boot_console.run_command('gpt guid host 0') |
| 243 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |