blob: 4329b69b7adda59f8c1d0bf495e096d6d026e24d [file] [log] [blame]
Alison Chaikena2f42252017-09-09 23:47:13 -07001# Copyright (c) 2017 Alison Chaiken
Stephen Warren110ba622017-09-15 12:19:38 -06002# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
Alison Chaikena2f42252017-09-09 23:47:13 -07003#
4# SPDX-License-Identifier: GPL-2.0
5
6# Test GPT manipulation commands.
7
8import os
9import pytest
10import u_boot_utils
Alison Chaikena2f42252017-09-09 23:47:13 -070011
12"""
Stephen Warren110ba622017-09-15 12:19:38 -060013These tests rely on a 4 MB disk image, which is automatically created by
14the test.
Alison Chaikena2f42252017-09-09 23:47:13 -070015"""
16
Stephen Warren110ba622017-09-15 12:19:38 -060017class 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 Warren110ba622017-09-15 12:19:38 -060031
Patrick Delaunayda4c4bb2017-10-18 15:11:03 +020032 persistent = u_boot_console.config.persistent_data_dir + '/' + filename
33 self.path = u_boot_console.config.result_dir + '/' + filename
34
Stephen Warrenac122ef2017-10-26 18:23:35 -060035 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)
47 cmd = ('sgdisk', '--new=1:2048:2560', '-c 1:part1', persistent)
48 u_boot_utils.run_and_log(u_boot_console, cmd)
49 cmd = ('sgdisk', '--new=2:4096:4608', '-c 2:part2', persistent)
50 u_boot_utils.run_and_log(u_boot_console, cmd)
51 cmd = ('sgdisk', '-l', persistent)
52 u_boot_utils.run_and_log(u_boot_console, cmd)
Stephen Warren110ba622017-09-15 12:19:38 -060053
Patrick Delaunayda4c4bb2017-10-18 15:11:03 +020054 cmd = ('cp', persistent, self.path)
55 u_boot_utils.run_and_log(u_boot_console, cmd)
56
Stephen Warren110ba622017-09-15 12:19:38 -060057gtdi = None
58@pytest.fixture(scope='function')
59def state_disk_image(u_boot_console):
60 """pytest fixture to provide a GptTestDiskImage object to tests.
61 This is function-scoped because it uses u_boot_console, which is also
62 function-scoped. However, we don't need to actually do any function-scope
63 work, so this simply returns the same object over and over each time."""
64
65 global gtdi
66 if not gtdi:
67 gtdi = GptTestDiskImage(u_boot_console)
68 return gtdi
69
70@pytest.mark.boardspec('sandbox')
Alison Chaikena2f42252017-09-09 23:47:13 -070071@pytest.mark.buildconfigspec('cmd_gpt')
Patrick Delaunay30ef7cb2017-10-18 15:11:04 +020072@pytest.mark.buildconfigspec('cmd_part')
73@pytest.mark.requiredtool('sgdisk')
74def test_gpt_read(state_disk_image, u_boot_console):
75 """Test the gpt read command."""
76
77 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
78 output = u_boot_console.run_command('gpt read host 0')
79 assert 'Start 1MiB, size 0MiB' in output
80 assert 'Block size 512, name part1' in output
81 assert 'Start 2MiB, size 0MiB' in output
82 assert 'Block size 512, name part2' in output
83 output = u_boot_console.run_command('part list host 0')
84 assert '0x00000800 0x00000a00 "part1"' in output
85 assert '0x00001000 0x00001200 "part2"' in output
86
87@pytest.mark.boardspec('sandbox')
88@pytest.mark.buildconfigspec('cmd_gpt')
89@pytest.mark.requiredtool('sgdisk')
90def test_gpt_verify(state_disk_image, u_boot_console):
91 """Test the gpt verify command."""
92
93 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
94 output = u_boot_console.run_command('gpt verify host 0')
95 assert 'Verify GPT: success!' in output
96
97@pytest.mark.boardspec('sandbox')
98@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2d26bf62017-09-18 11:11:49 -060099@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600100def test_gpt_guid(state_disk_image, u_boot_console):
Alison Chaikena2f42252017-09-09 23:47:13 -0700101 """Test the gpt guid command."""
102
Stephen Warren110ba622017-09-15 12:19:38 -0600103 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikena2f42252017-09-09 23:47:13 -0700104 output = u_boot_console.run_command('gpt guid host 0')
105 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
106
Stephen Warren110ba622017-09-15 12:19:38 -0600107@pytest.mark.boardspec('sandbox')
Alison Chaikena2f42252017-09-09 23:47:13 -0700108@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600109@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600110def test_gpt_save_guid(state_disk_image, u_boot_console):
Alison Chaikena2f42252017-09-09 23:47:13 -0700111 """Test the gpt guid command to save GUID into a string."""
112
113 if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
114 pytest.skip('gpt command not supported')
Stephen Warren110ba622017-09-15 12:19:38 -0600115 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikena2f42252017-09-09 23:47:13 -0700116 output = u_boot_console.run_command('gpt guid host 0 newguid')
117 output = u_boot_console.run_command('printenv newguid')
118 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700119
Stephen Warren110ba622017-09-15 12:19:38 -0600120@pytest.mark.boardspec('sandbox')
Alison Chaikenc5772182017-09-09 23:54:51 -0700121@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren110ba622017-09-15 12:19:38 -0600122@pytest.mark.buildconfigspec('cmd_gpt_rename')
Patrick Delaunay0cf02ff2017-10-18 15:11:07 +0200123@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600124@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600125def test_gpt_rename_partition(state_disk_image, u_boot_console):
Alison Chaikenc5772182017-09-09 23:54:51 -0700126 """Test the gpt rename command to write partition names."""
127
Stephen Warren110ba622017-09-15 12:19:38 -0600128 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikenc5772182017-09-09 23:54:51 -0700129 u_boot_console.run_command('gpt rename host 0 1 first')
130 output = u_boot_console.run_command('gpt read host 0')
131 assert 'name first' in output
132 u_boot_console.run_command('gpt rename host 0 2 second')
133 output = u_boot_console.run_command('gpt read host 0')
134 assert 'name second' in output
Patrick Delaunay0cf02ff2017-10-18 15:11:07 +0200135 output = u_boot_console.run_command('part list host 0')
Patrick Delaunay3a2605f2017-10-18 15:11:08 +0200136 assert '0x00000800 0x00000a00 "first"' in output
137 assert '0x00001000 0x00001200 "second"' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700138
Stephen Warren110ba622017-09-15 12:19:38 -0600139@pytest.mark.boardspec('sandbox')
Alison Chaikenc5772182017-09-09 23:54:51 -0700140@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren110ba622017-09-15 12:19:38 -0600141@pytest.mark.buildconfigspec('cmd_gpt_rename')
142@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600143@pytest.mark.requiredtool('sgdisk')
Stephen Warren110ba622017-09-15 12:19:38 -0600144def test_gpt_swap_partitions(state_disk_image, u_boot_console):
Alison Chaikenc5772182017-09-09 23:54:51 -0700145 """Test the gpt swap command to exchange two partition names."""
146
Stephen Warren110ba622017-09-15 12:19:38 -0600147 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikenc5772182017-09-09 23:54:51 -0700148 output = u_boot_console.run_command('part list host 0')
Patrick Delaunay3a2605f2017-10-18 15:11:08 +0200149 assert '0x00000800 0x00000a00 "first"' in output
150 assert '0x00001000 0x00001200 "second"' in output
Alison Chaikenc5772182017-09-09 23:54:51 -0700151 u_boot_console.run_command('gpt swap host 0 first second')
152 output = u_boot_console.run_command('part list host 0')
Patrick Delaunay3a2605f2017-10-18 15:11:08 +0200153 assert '0x00000800 0x00000a00 "second"' in output
154 assert '0x00001000 0x00001200 "first"' in output
Patrick Delaunayb61a3b52017-10-18 15:11:06 +0200155
156@pytest.mark.boardspec('sandbox')
157@pytest.mark.buildconfigspec('cmd_gpt')
158@pytest.mark.buildconfigspec('cmd_part')
159@pytest.mark.requiredtool('sgdisk')
160def test_gpt_write(state_disk_image, u_boot_console):
161 """Test the gpt write command."""
162
163 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
164 output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
165 assert 'Writing GPT: success!' in output
166 output = u_boot_console.run_command('part list host 0')
167 assert '0x00000022 0x00001fde "all"' in output
168 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;"')
169 assert 'Writing GPT: success!' in output
170 output = u_boot_console.run_command('part list host 0')
171 assert '0x00000800 0x00000a00 "first"' in output
172 assert '0x00001000 0x00001200 "second"' in output
173 output = u_boot_console.run_command('gpt guid host 0')
174 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output