blob: e58bf61eb1155b0ebfbfe89cda1e922dccde8494 [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'
Stephen Warren97dcbde2017-09-15 12:19:38 -060031
Patrick Delaunaye9e3a3e2017-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
35 if os.path.exists(persistent):
36 u_boot_console.log.action('Disk image file ' + persistent +
Stephen Warren97dcbde2017-09-15 12:19:38 -060037 ' already exists')
38 else:
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020039 u_boot_console.log.action('Generating ' + persistent)
40 fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
Stephen Warren97dcbde2017-09-15 12:19:38 -060041 os.ftruncate(fd, 4194304)
42 os.close(fd)
Stephen Warren2079db32017-09-18 11:11:49 -060043 cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020044 persistent)
Stephen Warren97dcbde2017-09-15 12:19:38 -060045 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020046 cmd = ('sgdisk', '--new=1:2048:2560', persistent)
Stephen Warren97dcbde2017-09-15 12:19:38 -060047 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020048 cmd = ('sgdisk', '--new=2:4096:4608', persistent)
Stephen Warren97dcbde2017-09-15 12:19:38 -060049 u_boot_utils.run_and_log(u_boot_console, cmd)
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020050 cmd = ('sgdisk', '-l', persistent)
Stephen Warren97dcbde2017-09-15 12:19:38 -060051 u_boot_utils.run_and_log(u_boot_console, cmd)
52
Patrick Delaunaye9e3a3e2017-10-18 15:11:03 +020053 cmd = ('cp', persistent, self.path)
54 u_boot_utils.run_and_log(u_boot_console, cmd)
55
Stephen Warren97dcbde2017-09-15 12:19:38 -060056gtdi = None
57@pytest.fixture(scope='function')
58def state_disk_image(u_boot_console):
59 """pytest fixture to provide a GptTestDiskImage object to tests.
60 This is function-scoped because it uses u_boot_console, which is also
61 function-scoped. However, we don't need to actually do any function-scope
62 work, so this simply returns the same object over and over each time."""
63
64 global gtdi
65 if not gtdi:
66 gtdi = GptTestDiskImage(u_boot_console)
67 return gtdi
68
69@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -070070@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2079db32017-09-18 11:11:49 -060071@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -060072def test_gpt_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -070073 """Test the gpt guid command."""
74
Stephen Warren97dcbde2017-09-15 12:19:38 -060075 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -070076 output = u_boot_console.run_command('gpt guid host 0')
77 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
78
Stephen Warren97dcbde2017-09-15 12:19:38 -060079@pytest.mark.boardspec('sandbox')
Alison Chaiken1b11fb02017-09-09 23:47:13 -070080@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren2079db32017-09-18 11:11:49 -060081@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -060082def test_gpt_save_guid(state_disk_image, u_boot_console):
Alison Chaiken1b11fb02017-09-09 23:47:13 -070083 """Test the gpt guid command to save GUID into a string."""
84
85 if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
86 pytest.skip('gpt command not supported')
Stephen Warren97dcbde2017-09-15 12:19:38 -060087 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaiken1b11fb02017-09-09 23:47:13 -070088 output = u_boot_console.run_command('gpt guid host 0 newguid')
89 output = u_boot_console.run_command('printenv newguid')
90 assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
Alison Chaikencb392962017-09-09 23:54:51 -070091
Stephen Warren97dcbde2017-09-15 12:19:38 -060092@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -070093@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -060094@pytest.mark.buildconfigspec('cmd_gpt_rename')
Stephen Warren2079db32017-09-18 11:11:49 -060095@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -060096def test_gpt_rename_partition(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -070097 """Test the gpt rename command to write partition names."""
98
Stephen Warren97dcbde2017-09-15 12:19:38 -060099 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -0700100 u_boot_console.run_command('gpt rename host 0 1 first')
101 output = u_boot_console.run_command('gpt read host 0')
102 assert 'name first' in output
103 u_boot_console.run_command('gpt rename host 0 2 second')
104 output = u_boot_console.run_command('gpt read host 0')
105 assert 'name second' in output
106
Stephen Warren97dcbde2017-09-15 12:19:38 -0600107@pytest.mark.boardspec('sandbox')
Alison Chaikencb392962017-09-09 23:54:51 -0700108@pytest.mark.buildconfigspec('cmd_gpt')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600109@pytest.mark.buildconfigspec('cmd_gpt_rename')
110@pytest.mark.buildconfigspec('cmd_part')
Stephen Warren2079db32017-09-18 11:11:49 -0600111@pytest.mark.requiredtool('sgdisk')
Stephen Warren97dcbde2017-09-15 12:19:38 -0600112def test_gpt_swap_partitions(state_disk_image, u_boot_console):
Alison Chaikencb392962017-09-09 23:54:51 -0700113 """Test the gpt swap command to exchange two partition names."""
114
Stephen Warren97dcbde2017-09-15 12:19:38 -0600115 u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
Alison Chaikencb392962017-09-09 23:54:51 -0700116 output = u_boot_console.run_command('part list host 0')
117 assert '0x000007ff "first"' in output
118 assert '0x000017ff "second"' in output
119 u_boot_console.run_command('gpt swap host 0 first second')
120 output = u_boot_console.run_command('part list host 0')
121 assert '0x000007ff "second"' in output
122 assert '0x000017ff "first"' in output