blob: c8edb336422c450faeea806483e855e25ebfdaf3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0
Alison Chaiken1b11fb02017-09-09 23:47:13 -07002# Copyright (c) 2017 Alison Chaiken
Stephen Warren97dcbde2017-09-15 12:19:38 -06003# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
Alison Chaiken1b11fb02017-09-09 23:47:13 -07004
5# Test GPT manipulation commands.
6
7import os
8import pytest
9import u_boot_utils
Alison Chaiken1b11fb02017-09-09 23:47:13 -070010
11"""
Stephen Warren97dcbde2017-09-15 12:19:38 -060012These tests rely on a 4 MB disk image, which is automatically created by
13the test.
Alison Chaiken1b11fb02017-09-09 23:47:13 -070014"""
15
Stephen Warren97dcbde2017-09-15 12:19:38 -060016class GptTestDiskImage(object):
17 """Disk Image used by the GPT tests."""
18
19 def __init__(self, u_boot_console):
20 """Initialize a new GptTestDiskImage object.
21
22 Args:
23 u_boot_console: A U-Boot console.
24
25 Returns:
26 Nothing.
27 """
28
29 filename = 'test_gpt_disk_image.bin'
Stephen Warren97dcbde2017-09-15 12:19:38 -060030
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020031 persistent = u_boot_console.config.persistent_data_dir + '/' + filename
32 self.path = u_boot_console.config.result_dir + '/' + filename
33
Stephen Warrena3b8f992017-10-26 18:23:35 -060034 with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent):
35 if os.path.exists(persistent):
36 u_boot_console.log.action('Disk image file ' + persistent +
37 ' already exists')
38 else:
39 u_boot_console.log.action('Generating ' + persistent)
40 fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
41 os.ftruncate(fd, 4194304)
42 os.close(fd)
43 cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
44 persistent)
45 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunayde7200a2017-12-06 18:08:20 +010046 # part1 offset 1MB size 1MB
47 cmd = ('sgdisk', '--new=1:2048:4095', '-c 1:part1', persistent)
48 # part2 offset 2MB size 1.5MB
Stephen Warrena3b8f992017-10-26 18:23:35 -060049 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunayde7200a2017-12-06 18:08:20 +010050 cmd = ('sgdisk', '--new=2:4096:7167', '-c 2:part2', persistent)
Stephen Warrena3b8f992017-10-26 18:23:35 -060051 u_boot_utils.run_and_log(u_boot_console, cmd)
52 cmd = ('sgdisk', '-l', persistent)
53 u_boot_utils.run_and_log(u_boot_console, cmd)
Stephen Warren97dcbde2017-09-15 12:19:38 -060054
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020055 cmd = ('cp', persistent, self.path)
56 u_boot_utils.run_and_log(u_boot_console, cmd)
57
Stephen Warren97dcbde2017-09-15 12:19:38 -060058gtdi = None
59@pytest.fixture(scope='function')
60def state_disk_image(u_boot_console):
61 """pytest fixture to provide a GptTestDiskImage object to tests.
62 This is function-scoped because it uses u_boot_console, which is also
63 function-scoped. However, we don't need to actually do any function-scope
64 work, so this simply returns the same object over and over each time."""
65
66 global gtdi
67 if not gtdi:
68 gtdi = GptTestDiskImage(u_boot_console)
69 return gtdi
70
71@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -070072@pytest.mark.buildconfigspec('cmd_gpt')
Patrick Delaunayc06b7842017-10-18 15:11:04 +020073@pytest.mark.buildconfigspec('cmd_part')
74@pytest.mark.requiredtool('sgdisk')
75def test_gpt_read(state_disk_image, u_boot_console):
76 """Test the gpt read command."""
77
78 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
79 output = u_boot_console.run_command('gpt read host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +010080 assert 'Start 1MiB, size 1MiB' in output
Patrick Delaunayc06b7842017-10-18 15:11:04 +020081 assert 'Block size 512, name part1' in output
Patrick Delaunayde7200a2017-12-06 18:08:20 +010082 assert 'Start 2MiB, size 1MiB' in output
Patrick Delaunayc06b7842017-10-18 15:11:04 +020083 assert 'Block size 512, name part2' in output
84 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +010085 assert '0x00000800 0x00000fff "part1"' in output
86 assert '0x00001000 0x00001bff "part2"' in output
Patrick Delaunayc06b7842017-10-18 15:11:04 +020087
88@pytest.mark.boardspec('sandbox')
89@pytest.mark.buildconfigspec('cmd_gpt')
90@pytest.mark.requiredtool('sgdisk')
91def test_gpt_verify(state_disk_image, u_boot_console):
92 """Test the gpt verify command."""
93
94 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
95 output = u_boot_console.run_command('gpt verify host 0')
96 assert 'Verify GPT: success!' in output
97
98@pytest.mark.boardspec('sandbox')
99@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2079db32017-09-18 11:11:49 -0600100@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600101def test_gpt_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -0700102 """Test the gpt guid command."""
103
Stephen Warren97dcbde2017-09-15 12:19:38 -0600104 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -0700105 output = u_boot_console.run_command('gpt guid host 0')
106 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
107
Stephen Warren97dcbde2017-09-15 12:19:38 -0600108@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -0700109@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2079db32017-09-18 11:11:49 -0600110@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600111def test_gpt_save_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -0700112 """Test the gpt guid command to save GUID into a string."""
113
114 if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
115 pytest.skip('gpt command not supported')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600116 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -0700117 output = u_boot_console.run_command('gpt guid host 0 newguid')
118 output = u_boot_console.run_command('printenv newguid')
119 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
Alison Chaikencb392962017-09-09 23:54:51 -0700120
Stephen Warren97dcbde2017-09-15 12:19:38 -0600121@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -0700122@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600123@pytest.mark.buildconfigspec('cmd_gpt_rename')
Patrick Delaunay0ffbb642017-10-18 15:11:07 +0200124@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2079db32017-09-18 11:11:49 -0600125@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600126def test_gpt_rename_partition(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -0700127 """Test the gpt rename command to write partition names."""
128
Stephen Warren97dcbde2017-09-15 12:19:38 -0600129 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -0700130 u_boot_console.run_command('gpt rename host 0 1 first')
131 output = u_boot_console.run_command('gpt read host 0')
132 assert 'name first' in output
133 u_boot_console.run_command('gpt rename host 0 2 second')
134 output = u_boot_console.run_command('gpt read host 0')
135 assert 'name second' in output
Patrick Delaunay0ffbb642017-10-18 15:11:07 +0200136 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +0100137 assert '0x00000800 0x00000fff "first"' in output
138 assert '0x00001000 0x00001bff "second"' in output
Alison Chaikencb392962017-09-09 23:54:51 -0700139
Stephen Warren97dcbde2017-09-15 12:19:38 -0600140@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -0700141@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600142@pytest.mark.buildconfigspec('cmd_gpt_rename')
143@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2079db32017-09-18 11:11:49 -0600144@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600145def test_gpt_swap_partitions(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -0700146 """Test the gpt swap command to exchange two partition names."""
147
Stephen Warren97dcbde2017-09-15 12:19:38 -0600148 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -0700149 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +0100150 assert '0x00000800 0x00000fff "first"' in output
151 assert '0x00001000 0x00001bff "second"' in output
Alison Chaikencb392962017-09-09 23:54:51 -0700152 u_boot_console.run_command('gpt swap host 0 first second')
153 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +0100154 assert '0x00000800 0x00000fff "second"' in output
155 assert '0x00001000 0x00001bff "first"' in output
Patrick Delaunay5751c052017-10-18 15:11:06 +0200156
157@pytest.mark.boardspec('sandbox')
158@pytest.mark.buildconfigspec('cmd_gpt')
159@pytest.mark.buildconfigspec('cmd_part')
160@pytest.mark.requiredtool('sgdisk')
161def test_gpt_write(state_disk_image, u_boot_console):
162 """Test the gpt write command."""
163
164 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
165 output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
166 assert 'Writing GPT: success!' in output
167 output = u_boot_console.run_command('part list host 0')
168 assert '0x00000022 0x00001fde "all"' in output
Patrick Delaunayde7200a2017-12-06 18:08:20 +0100169 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 Delaunay5751c052017-10-18 15:11:06 +0200170 assert 'Writing GPT: success!' in output
171 output = u_boot_console.run_command('part list host 0')
Patrick Delaunayde7200a2017-12-06 18:08:20 +0100172 assert '0x00000800 0x00000fff "first"' in output
173 assert '0x00001000 0x00001bff "second"' in output
Patrick Delaunay5751c052017-10-18 15:11:06 +0200174 output = u_boot_console.run_command('gpt guid host 0')
175 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output