blob: cb44e1d7896fb60f5a34441e0a2f238e88f03ab5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0
Alison Chaikena2f42252017-09-09 23:47:13 -07002# Copyright (c) 2017 Alison Chaiken
Stephen Warren110ba622017-09-15 12:19:38 -06003# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
Alison Chaikena2f42252017-09-09 23:47:13 -07004
5# Test GPT manipulation commands.
6
7import os
8import pytest
9import u_boot_utils
Alison Chaikena2f42252017-09-09 23:47:13 -070010
11"""
Stephen Warren110ba622017-09-15 12:19:38 -060012These tests rely on a 4 MB disk image, which is automatically created by
13the test.
Alison Chaikena2f42252017-09-09 23:47:13 -070014"""
15
Simon Glassd4011872022-08-06 17:51:53 -060016# Mark all tests here as slow
17pytestmark = pytest.mark.slow
18
Stephen Warren110ba622017-09-15 12:19:38 -060019class 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 Warren110ba622017-09-15 12:19:38 -060033
Patrick Delaunayda4c4bb2017-10-18 15:11:03 +020034 persistent = u_boot_console.config.persistent_data_dir + '/' + filename
35 self.path = u_boot_console.config.result_dir + '/' + filename
36
Stephen Warrenac122ef2017-10-26 18:23:35 -060037 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 Protsenkobdfc9e82019-07-02 21:20:32 +030046 cmd = ('sgdisk',
47 '--disk-guid=375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
Stephen Warrenac122ef2017-10-26 18:23:35 -060048 persistent)
49 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunayf0314642017-12-06 18:08:20 +010050 # part1 offset 1MB size 1MB
Sam Protsenkobdfc9e82019-07-02 21:20:32 +030051 cmd = ('sgdisk', '--new=1:2048:4095', '--change-name=1:part1',
52 persistent)
Patrick Delaunayf0314642017-12-06 18:08:20 +010053 # part2 offset 2MB size 1.5MB
Stephen Warrenac122ef2017-10-26 18:23:35 -060054 u_boot_utils.run_and_log(u_boot_console, cmd)
Sam Protsenkobdfc9e82019-07-02 21:20:32 +030055 cmd = ('sgdisk', '--new=2:4096:7167', '--change-name=2:part2',
56 persistent)
Stephen Warrenac122ef2017-10-26 18:23:35 -060057 u_boot_utils.run_and_log(u_boot_console, cmd)
Sam Protsenkobdfc9e82019-07-02 21:20:32 +030058 cmd = ('sgdisk', '--load-backup=' + persistent)
Stephen Warrenac122ef2017-10-26 18:23:35 -060059 u_boot_utils.run_and_log(u_boot_console, cmd)
Stephen Warren110ba622017-09-15 12:19:38 -060060
Patrick Delaunayda4c4bb2017-10-18 15:11:03 +020061 cmd = ('cp', persistent, self.path)
62 u_boot_utils.run_and_log(u_boot_console, cmd)
63
Stephen Warren110ba622017-09-15 12:19:38 -060064gtdi = None
65@pytest.fixture(scope='function')
66def 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
69 function-scoped. However, we don't need to actually do any function-scope
70 work, so this simply returns the same object over and over each time."""
71
72 global gtdi
73 if not gtdi:
74 gtdi = GptTestDiskImage(u_boot_console)
75 return gtdi
76
77@pytest.mark.boardspec('sandbox')
Alison Chaikena2f42252017-09-09 23:47:13 -070078@pytest.mark.buildconfigspec('cmd_gpt')
Patrick Delaunay30ef7cb2017-10-18 15:11:04 +020079@pytest.mark.buildconfigspec('cmd_part')
80@pytest.mark.requiredtool('sgdisk')
81def test_gpt_read(state_disk_image, u_boot_console):
82 """Test the gpt read command."""
83
84 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
85 output = u_boot_console.run_command('gpt read host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +010086 assert 'Start 1MiB, size 1MiB' in output
Patrick Delaunay30ef7cb2017-10-18 15:11:04 +020087 assert 'Block size 512, name part1' in output
Patrick Delaunayf0314642017-12-06 18:08:20 +010088 assert 'Start 2MiB, size 1MiB' in output
Patrick Delaunay30ef7cb2017-10-18 15:11:04 +020089 assert 'Block size 512, name part2' in output
90 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +010091 assert '0x00000800 0x00000fff "part1"' in output
92 assert '0x00001000 0x00001bff "part2"' in output
Patrick Delaunay30ef7cb2017-10-18 15:11:04 +020093
94@pytest.mark.boardspec('sandbox')
95@pytest.mark.buildconfigspec('cmd_gpt')
96@pytest.mark.requiredtool('sgdisk')
97def test_gpt_verify(state_disk_image, u_boot_console):
98 """Test the gpt verify command."""
99
100 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
101 output = u_boot_console.run_command('gpt verify host 0')
102 assert 'Verify GPT: success!' in output
103
104@pytest.mark.boardspec('sandbox')
105@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600106@pytest.mark.requiredtool('sgdisk')
Philippe Reynesb6b6a902022-04-22 17:46:50 +0200107def test_gpt_repair(state_disk_image, u_boot_console):
108 """Test the gpt repair command."""
109
110 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
111 output = u_boot_console.run_command('gpt repair host 0')
112 assert 'Repairing GPT: success!' in output
113
114@pytest.mark.boardspec('sandbox')
115@pytest.mark.buildconfigspec('cmd_gpt')
116@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600117def test_gpt_guid(state_disk_image, u_boot_console):
Alison Chaikena2f42252017-09-09 23:47:13 -0700118 """Test the gpt guid command."""
119
Stephen Warren110ba622017-09-15 12:19:38 -0600120 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikena2f42252017-09-09 23:47:13 -0700121 output = u_boot_console.run_command('gpt guid host 0')
122 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
123
Stephen Warren110ba622017-09-15 12:19:38 -0600124@pytest.mark.boardspec('sandbox')
Alison Chaikena2f42252017-09-09 23:47:13 -0700125@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600126@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600127def test_gpt_save_guid(state_disk_image, u_boot_console):
Alison Chaikena2f42252017-09-09 23:47:13 -0700128 """Test the gpt guid command to save GUID into a string."""
129
130 if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
131 pytest.skip('gpt command not supported')
Stephen Warren110ba622017-09-15 12:19:38 -0600132 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikena2f42252017-09-09 23:47:13 -0700133 output = u_boot_console.run_command('gpt guid host 0 newguid')
134 output = u_boot_console.run_command('printenv newguid')
135 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700136
Stephen Warren110ba622017-09-15 12:19:38 -0600137@pytest.mark.boardspec('sandbox')
Alison Chaikenc5772182017-09-09 23:54:51 -0700138@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren110ba622017-09-15 12:19:38 -0600139@pytest.mark.buildconfigspec('cmd_gpt_rename')
Patrick Delaunay0cf02ff2017-10-18 15:11:07 +0200140@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600141@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600142def test_gpt_rename_partition(state_disk_image, u_boot_console):
Alison Chaikenc5772182017-09-09 23:54:51 -0700143 """Test the gpt rename command to write partition names."""
144
Stephen Warren110ba622017-09-15 12:19:38 -0600145 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikenc5772182017-09-09 23:54:51 -0700146 u_boot_console.run_command('gpt rename host 0 1 first')
147 output = u_boot_console.run_command('gpt read host 0')
148 assert 'name first' in output
149 u_boot_console.run_command('gpt rename host 0 2 second')
150 output = u_boot_console.run_command('gpt read host 0')
151 assert 'name second' in output
Patrick Delaunay0cf02ff2017-10-18 15:11:07 +0200152 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +0100153 assert '0x00000800 0x00000fff "first"' in output
154 assert '0x00001000 0x00001bff "second"' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700155
Stephen Warren110ba622017-09-15 12:19:38 -0600156@pytest.mark.boardspec('sandbox')
Alison Chaikenc5772182017-09-09 23:54:51 -0700157@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren110ba622017-09-15 12:19:38 -0600158@pytest.mark.buildconfigspec('cmd_gpt_rename')
159@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600160@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600161def test_gpt_swap_partitions(state_disk_image, u_boot_console):
Alison Chaikenc5772182017-09-09 23:54:51 -0700162 """Test the gpt swap command to exchange two partition names."""
163
Stephen Warren110ba622017-09-15 12:19:38 -0600164 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikenc5772182017-09-09 23:54:51 -0700165 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +0100166 assert '0x00000800 0x00000fff "first"' in output
167 assert '0x00001000 0x00001bff "second"' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700168 u_boot_console.run_command('gpt swap host 0 first second')
169 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +0100170 assert '0x00000800 0x00000fff "second"' in output
171 assert '0x00001000 0x00001bff "first"' in output
Patrick Delaunayb61a3b52017-10-18 15:11:06 +0200172
173@pytest.mark.boardspec('sandbox')
174@pytest.mark.buildconfigspec('cmd_gpt')
175@pytest.mark.buildconfigspec('cmd_part')
176@pytest.mark.requiredtool('sgdisk')
177def test_gpt_write(state_disk_image, u_boot_console):
178 """Test the gpt write command."""
179
180 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
181 output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
182 assert 'Writing GPT: success!' in output
183 output = u_boot_console.run_command('part list host 0')
184 assert '0x00000022 0x00001fde "all"' in output
Patrick Delaunayf0314642017-12-06 18:08:20 +0100185 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 Delaunayb61a3b52017-10-18 15:11:06 +0200186 assert 'Writing GPT: success!' in output
187 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayf0314642017-12-06 18:08:20 +0100188 assert '0x00000800 0x00000fff "first"' in output
189 assert '0x00001000 0x00001bff "second"' in output
Patrick Delaunayb61a3b52017-10-18 15:11:06 +0200190 output = u_boot_console.run_command('gpt guid host 0')
191 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output