Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017 Alison Chaiken |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 2 | # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. |
Alison Chaiken | 1b11fb0 | 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 | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
Stephen Warren | 97dcbde | 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 | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 15 | """ |
| 16 | |
Stephen Warren | 97dcbde | 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 | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 31 | |
Patrick Delaunay | e9e3a3e | 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 | |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 35 | with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent): |
| 36 | if os.path.exists(persistent): |
| 37 | u_boot_console.log.action('Disk image file ' + persistent + |
| 38 | ' already exists') |
| 39 | else: |
| 40 | u_boot_console.log.action('Generating ' + persistent) |
| 41 | fd = os.open(persistent, os.O_RDWR | os.O_CREAT) |
| 42 | os.ftruncate(fd, 4194304) |
| 43 | os.close(fd) |
| 44 | cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe', |
| 45 | persistent) |
| 46 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 47 | # part1 offset 1MB size 1MB |
| 48 | cmd = ('sgdisk', '--new=1:2048:4095', '-c 1:part1', persistent) |
| 49 | # part2 offset 2MB size 1.5MB |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 50 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 51 | cmd = ('sgdisk', '--new=2:4096:7167', '-c 2:part2', persistent) |
Stephen Warren | a3b8f99 | 2017-10-26 18:23:35 -0600 | [diff] [blame] | 52 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 53 | cmd = ('sgdisk', '-l', persistent) |
| 54 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 55 | |
Patrick Delaunay | e9e3a3e | 2017-10-18 15:11:03 +0200 | [diff] [blame] | 56 | cmd = ('cp', persistent, self.path) |
| 57 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 58 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 59 | gtdi = None |
| 60 | @pytest.fixture(scope='function') |
| 61 | def state_disk_image(u_boot_console): |
| 62 | """pytest fixture to provide a GptTestDiskImage object to tests. |
| 63 | This is function-scoped because it uses u_boot_console, which is also |
| 64 | function-scoped. However, we don't need to actually do any function-scope |
| 65 | work, so this simply returns the same object over and over each time.""" |
| 66 | |
| 67 | global gtdi |
| 68 | if not gtdi: |
| 69 | gtdi = GptTestDiskImage(u_boot_console) |
| 70 | return gtdi |
| 71 | |
| 72 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 73 | @pytest.mark.buildconfigspec('cmd_gpt') |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 74 | @pytest.mark.buildconfigspec('cmd_part') |
| 75 | @pytest.mark.requiredtool('sgdisk') |
| 76 | def test_gpt_read(state_disk_image, u_boot_console): |
| 77 | """Test the gpt read command.""" |
| 78 | |
| 79 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 80 | output = u_boot_console.run_command('gpt read host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 81 | assert 'Start 1MiB, size 1MiB' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 82 | assert 'Block size 512, name part1' in output |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 83 | assert 'Start 2MiB, size 1MiB' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 84 | assert 'Block size 512, name part2' in output |
| 85 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 86 | assert '0x00000800 0x00000fff "part1"' in output |
| 87 | assert '0x00001000 0x00001bff "part2"' in output |
Patrick Delaunay | c06b784 | 2017-10-18 15:11:04 +0200 | [diff] [blame] | 88 | |
| 89 | @pytest.mark.boardspec('sandbox') |
| 90 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 91 | @pytest.mark.requiredtool('sgdisk') |
| 92 | def test_gpt_verify(state_disk_image, u_boot_console): |
| 93 | """Test the gpt verify command.""" |
| 94 | |
| 95 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 96 | output = u_boot_console.run_command('gpt verify host 0') |
| 97 | assert 'Verify GPT: success!' in output |
| 98 | |
| 99 | @pytest.mark.boardspec('sandbox') |
| 100 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 101 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 102 | def test_gpt_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 103 | """Test the gpt guid command.""" |
| 104 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 105 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 106 | output = u_boot_console.run_command('gpt guid host 0') |
| 107 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
| 108 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 109 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 110 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 111 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 112 | def test_gpt_save_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 113 | """Test the gpt guid command to save GUID into a string.""" |
| 114 | |
| 115 | if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': |
| 116 | pytest.skip('gpt command not supported') |
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 newguid') |
| 119 | output = u_boot_console.run_command('printenv newguid') |
| 120 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 121 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 122 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 123 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 124 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
Patrick Delaunay | 0ffbb64 | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 125 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 126 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 127 | def test_gpt_rename_partition(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 128 | """Test the gpt rename command to write partition names.""" |
| 129 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 130 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 131 | u_boot_console.run_command('gpt rename host 0 1 first') |
| 132 | output = u_boot_console.run_command('gpt read host 0') |
| 133 | assert 'name first' in output |
| 134 | u_boot_console.run_command('gpt rename host 0 2 second') |
| 135 | output = u_boot_console.run_command('gpt read host 0') |
| 136 | assert 'name second' in output |
Patrick Delaunay | 0ffbb64 | 2017-10-18 15:11:07 +0200 | [diff] [blame] | 137 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 138 | assert '0x00000800 0x00000fff "first"' in output |
| 139 | assert '0x00001000 0x00001bff "second"' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 140 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 141 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 142 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 143 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
| 144 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 145 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 146 | def test_gpt_swap_partitions(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 147 | """Test the gpt swap command to exchange two partition names.""" |
| 148 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 149 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 150 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 151 | assert '0x00000800 0x00000fff "first"' in output |
| 152 | assert '0x00001000 0x00001bff "second"' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 153 | u_boot_console.run_command('gpt swap host 0 first second') |
| 154 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 155 | assert '0x00000800 0x00000fff "second"' in output |
| 156 | assert '0x00001000 0x00001bff "first"' in output |
Patrick Delaunay | 5751c05 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 157 | |
| 158 | @pytest.mark.boardspec('sandbox') |
| 159 | @pytest.mark.buildconfigspec('cmd_gpt') |
| 160 | @pytest.mark.buildconfigspec('cmd_part') |
| 161 | @pytest.mark.requiredtool('sgdisk') |
| 162 | def test_gpt_write(state_disk_image, u_boot_console): |
| 163 | """Test the gpt write command.""" |
| 164 | |
| 165 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
| 166 | output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"') |
| 167 | assert 'Writing GPT: success!' in output |
| 168 | output = u_boot_console.run_command('part list host 0') |
| 169 | assert '0x00000022 0x00001fde "all"' in output |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 170 | 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] | 171 | assert 'Writing GPT: success!' in output |
| 172 | output = u_boot_console.run_command('part list host 0') |
Patrick Delaunay | de7200a | 2017-12-06 18:08:20 +0100 | [diff] [blame] | 173 | assert '0x00000800 0x00000fff "first"' in output |
| 174 | assert '0x00001000 0x00001bff "second"' in output |
Patrick Delaunay | 5751c05 | 2017-10-18 15:11:06 +0200 | [diff] [blame] | 175 | output = u_boot_console.run_command('gpt guid host 0') |
| 176 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |