Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017 Alison Chaiken |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 2 | # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0 |
| 5 | |
| 6 | # Test GPT manipulation commands. |
| 7 | |
| 8 | import os |
| 9 | import pytest |
| 10 | import u_boot_utils |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 13 | These tests rely on a 4 MB disk image, which is automatically created by |
| 14 | the test. |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 15 | """ |
| 16 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 17 | class GptTestDiskImage(object): |
| 18 | """Disk Image used by the GPT tests.""" |
| 19 | |
| 20 | def __init__(self, u_boot_console): |
| 21 | """Initialize a new GptTestDiskImage object. |
| 22 | |
| 23 | Args: |
| 24 | u_boot_console: A U-Boot console. |
| 25 | |
| 26 | Returns: |
| 27 | Nothing. |
| 28 | """ |
| 29 | |
| 30 | filename = 'test_gpt_disk_image.bin' |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 31 | |
Patrick Delaunay | da4c4bb | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 32 | persistent = u_boot_console.config.persistent_data_dir + '/' + filename |
| 33 | self.path = u_boot_console.config.result_dir + '/' + filename |
| 34 | |
| 35 | if os.path.exists(persistent): |
| 36 | u_boot_console.log.action('Disk image file ' + persistent + |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 37 | ' already exists') |
| 38 | else: |
Patrick Delaunay | da4c4bb | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 39 | u_boot_console.log.action('Generating ' + persistent) |
| 40 | fd = os.open(persistent, os.O_RDWR | os.O_CREAT) |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 41 | os.ftruncate(fd, 4194304) |
| 42 | os.close(fd) |
Stephen Warren | 2d26bf6 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 43 | cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe', |
Patrick Delaunay | da4c4bb | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 44 | persistent) |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 45 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | 30ef7cb | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 46 | cmd = ('sgdisk', '--new=1:2048:2560', '-c 1:part1', persistent) |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 47 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | 30ef7cb | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 48 | cmd = ('sgdisk', '--new=2:4096:4608', '-c 2:part2', persistent) |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 49 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | da4c4bb | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 50 | cmd = ('sgdisk', '-l', persistent) |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 51 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 52 | |
Patrick Delaunay | da4c4bb | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 53 | cmd = ('cp', persistent, self.path) |
| 54 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 55 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 56 | gtdi = None |
| 57 | @pytest.fixture(scope='function') |
| 58 | def state_disk_image(u_boot_console): |
| 59 | """pytest fixture to provide a GptTestDiskImage object to tests. |
| 60 | This is function-scoped because it uses u_boot_console, which is also |
| 61 | function-scoped. However, we don't need to actually do any function-scope |
| 62 | work, so this simply returns the same object over and over each time.""" |
| 63 | |
| 64 | global gtdi |
| 65 | if not gtdi: |
| 66 | gtdi = GptTestDiskImage(u_boot_console) |
| 67 | return gtdi |
| 68 | |
| 69 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 70 | @pytest.mark.buildconfigspec('cmd_gpt') |
Patrick Delaunay | 30ef7cb | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 71 | @pytest.mark.buildconfigspec('cmd_part') |
| 72 | @pytest.mark.requiredtool('sgdisk') |
| 73 | def test_gpt_read(state_disk_image, u_boot_console): |
| 74 | """Test the gpt read command.""" |
| 75 | |
| 76 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 77 | output = u_boot_console.run_command('gpt read host 0') |
| 78 | assert 'Start 1MiB, size 0MiB' in output |
| 79 | assert 'Block size 512, name part1' in output |
| 80 | assert 'Start 2MiB, size 0MiB' in output |
| 81 | assert 'Block size 512, name part2' in output |
| 82 | output = u_boot_console.run_command('part list host 0') |
| 83 | assert '0x00000800 0x00000a00 "part1"' in output |
| 84 | assert '0x00001000 0x00001200 "part2"' in output |
| 85 | |
| 86 | @pytest.mark.boardspec('sandbox') |
| 87 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 88 | @pytest.mark.requiredtool('sgdisk') |
| 89 | def test_gpt_verify(state_disk_image, u_boot_console): |
| 90 | """Test the gpt verify command.""" |
| 91 | |
| 92 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 93 | output = u_boot_console.run_command('gpt verify host 0') |
| 94 | assert 'Verify GPT: success!' in output |
| 95 | |
| 96 | @pytest.mark.boardspec('sandbox') |
| 97 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2d26bf6 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 98 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 99 | def test_gpt_guid(state_disk_image, u_boot_console): |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 100 | """Test the gpt guid command.""" |
| 101 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 102 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 103 | output = u_boot_console.run_command('gpt guid host 0') |
| 104 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
| 105 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 106 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 107 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2d26bf6 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 108 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 109 | def test_gpt_save_guid(state_disk_image, u_boot_console): |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 110 | """Test the gpt guid command to save GUID into a string.""" |
| 111 | |
| 112 | if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': |
| 113 | pytest.skip('gpt command not supported') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 114 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | a2f4225 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 115 | output = u_boot_console.run_command('gpt guid host 0 newguid') |
| 116 | output = u_boot_console.run_command('printenv newguid') |
| 117 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 118 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 119 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 120 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 121 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
Patrick Delaunay | 0cf02ff | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 122 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2d26bf6 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 123 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 124 | def test_gpt_rename_partition(state_disk_image, u_boot_console): |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 125 | """Test the gpt rename command to write partition names.""" |
| 126 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 127 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 128 | u_boot_console.run_command('gpt rename host 0 1 first') |
| 129 | output = u_boot_console.run_command('gpt read host 0') |
| 130 | assert 'name first' in output |
| 131 | u_boot_console.run_command('gpt rename host 0 2 second') |
| 132 | output = u_boot_console.run_command('gpt read host 0') |
| 133 | assert 'name second' in output |
Patrick Delaunay | 0cf02ff | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 134 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | 3a2605f | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 135 | assert '0x00000800 0x00000a00 "first"' in output |
| 136 | assert '0x00001000 0x00001200 "second"' in output |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 137 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 138 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 139 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 140 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
| 141 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2d26bf6 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 142 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 143 | def test_gpt_swap_partitions(state_disk_image, u_boot_console): |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 144 | """Test the gpt swap command to exchange two partition names.""" |
| 145 | |
Stephen Warren | 110ba62 | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 146 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 147 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | 3a2605f | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 148 | assert '0x00000800 0x00000a00 "first"' in output |
| 149 | assert '0x00001000 0x00001200 "second"' in output |
Alison Chaiken | c577218 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 150 | u_boot_console.run_command('gpt swap host 0 first second') |
| 151 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | 3a2605f | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 152 | assert '0x00000800 0x00000a00 "second"' in output |
| 153 | assert '0x00001000 0x00001200 "first"' in output |
Patrick Delaunay | b61a3b5 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 154 | |
| 155 | @pytest.mark.boardspec('sandbox') |
| 156 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 157 | @pytest.mark.buildconfigspec('cmd_part') |
| 158 | @pytest.mark.requiredtool('sgdisk') |
| 159 | def test_gpt_write(state_disk_image, u_boot_console): |
| 160 | """Test the gpt write command.""" |
| 161 | |
| 162 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 163 | output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"') |
| 164 | assert 'Writing GPT: success!' in output |
| 165 | output = u_boot_console.run_command('part list host 0') |
| 166 | assert '0x00000022 0x00001fde "all"' in output |
| 167 | output = u_boot_console.run_command('gpt write host 0 "uuid_disk=375a56f7-d6c9-4e81-b5f0-09d41ca89efe;name=first,start=0x100000,size=0x40200;name=second,start=0x200000,size=0x40200;"') |
| 168 | assert 'Writing GPT: success!' in output |
| 169 | output = u_boot_console.run_command('part list host 0') |
| 170 | assert '0x00000800 0x00000a00 "first"' in output |
| 171 | assert '0x00001000 0x00001200 "second"' in output |
| 172 | output = u_boot_console.run_command('gpt guid host 0') |
| 173 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |