blob: e2bbd08e6d42a6cbc4bb9da73684c37f46c2b95a [file] [log] [blame]
Alison Chaiken1b11fb02017-09-09 23:47:13 -07001# Copyright (c) 2017 Alison Chaiken
Stephen Warren97dcbde2017-09-15 12:19:38 -06002# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
Alison Chaiken1b11fb02017-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 Chaiken1b11fb02017-09-09 23:47:13 -070011
12"""
Stephen Warren97dcbde2017-09-15 12:19:38 -060013These tests rely on a 4 MB disk image, which is automatically created by
14the test.
Alison Chaiken1b11fb02017-09-09 23:47:13 -070015"""
16
Stephen Warren97dcbde2017-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'
31 self.path = u_boot_console.config.persistent_data_dir + '/' + filename
32
33 if os.path.exists(self.path):
34 u_boot_console.log.action('Disk image file ' + self.path +
35 ' already exists')
36 else:
37 u_boot_console.log.action('Generating ' + self.path)
38 fd = os.open(self.path, os.O_RDWR | os.O_CREAT)
39 os.ftruncate(fd, 4194304)
40 os.close(fd)
41 sgdisk = '/sbin/sgdisk'
42 cmd = (sgdisk, '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
43 self.path)
44 u_boot_utils.run_and_log(u_boot_console, cmd)
45 cmd = (sgdisk, '--new=1:2048:2560', self.path)
46 u_boot_utils.run_and_log(u_boot_console, cmd)
47 cmd = (sgdisk, '--new=2:4096:4608', self.path)
48 u_boot_utils.run_and_log(u_boot_console, cmd)
49 cmd = (sgdisk, '-l', self.path)
50 u_boot_utils.run_and_log(u_boot_console, cmd)
51
52gtdi = None
53@pytest.fixture(scope='function')
54def state_disk_image(u_boot_console):
55 """pytest fixture to provide a GptTestDiskImage object to tests.
56 This is function-scoped because it uses u_boot_console, which is also
57 function-scoped. However, we don't need to actually do any function-scope
58 work, so this simply returns the same object over and over each time."""
59
60 global gtdi
61 if not gtdi:
62 gtdi = GptTestDiskImage(u_boot_console)
63 return gtdi
64
65@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -070066@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -060067def test_gpt_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -070068 """Test the gpt guid command."""
69
Stephen Warren97dcbde2017-09-15 12:19:38 -060070 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -070071 output = u_boot_console.run_command('gpt guid host 0')
72 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
73
Stephen Warren97dcbde2017-09-15 12:19:38 -060074@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -070075@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -060076def test_gpt_save_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -070077 """Test the gpt guid command to save GUID into a string."""
78
79 if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
80 pytest.skip('gpt command not supported')
Stephen Warren97dcbde2017-09-15 12:19:38 -060081 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -070082 output = u_boot_console.run_command('gpt guid host 0 newguid')
83 output = u_boot_console.run_command('printenv newguid')
84 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
Alison Chaikencb392962017-09-09 23:54:51 -070085
Stephen Warren97dcbde2017-09-15 12:19:38 -060086@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -070087@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -060088@pytest.mark.buildconfigspec('cmd_gpt_rename')
89def test_gpt_rename_partition(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -070090 """Test the gpt rename command to write partition names."""
91
Stephen Warren97dcbde2017-09-15 12:19:38 -060092 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -070093 u_boot_console.run_command('gpt rename host 0 1 first')
94 output = u_boot_console.run_command('gpt read host 0')
95 assert 'name first' in output
96 u_boot_console.run_command('gpt rename host 0 2 second')
97 output = u_boot_console.run_command('gpt read host 0')
98 assert 'name second' in output
99
Stephen Warren97dcbde2017-09-15 12:19:38 -0600100@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -0700101@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600102@pytest.mark.buildconfigspec('cmd_gpt_rename')
103@pytest.mark.buildconfigspec('cmd_part')
104def test_gpt_swap_partitions(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -0700105 """Test the gpt swap command to exchange two partition names."""
106
Stephen Warren97dcbde2017-09-15 12:19:38 -0600107 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -0700108 output = u_boot_console.run_command('part list host 0')
109 assert '0x000007ff "first"' in output
110 assert '0x000017ff "second"' in output
111 u_boot_console.run_command('gpt swap host 0 first second')
112 output = u_boot_console.run_command('part list host 0')
113 assert '0x000007ff "second"' in output
114 assert '0x000017ff "first"' in output