Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017 Alison Chaiken |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 2 | # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0 |
| 5 | |
| 6 | # Test GPT manipulation commands. |
| 7 | |
| 8 | import os |
| 9 | import pytest |
| 10 | import u_boot_utils |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 13 | These tests rely on a 4 MB disk image, which is automatically created by |
| 14 | the test. |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 15 | """ |
| 16 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 17 | class 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) |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 41 | cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe', |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 42 | self.path) |
| 43 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 44 | cmd = ('sgdisk', '--new=1:2048:2560', self.path) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 45 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 46 | cmd = ('sgdisk', '--new=2:4096:4608', self.path) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 47 | u_boot_utils.run_and_log(u_boot_console, cmd) |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 48 | cmd = ('sgdisk', '-l', self.path) |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 49 | u_boot_utils.run_and_log(u_boot_console, cmd) |
| 50 | |
| 51 | gtdi = None |
| 52 | @pytest.fixture(scope='function') |
| 53 | def state_disk_image(u_boot_console): |
| 54 | """pytest fixture to provide a GptTestDiskImage object to tests. |
| 55 | This is function-scoped because it uses u_boot_console, which is also |
| 56 | function-scoped. However, we don't need to actually do any function-scope |
| 57 | work, so this simply returns the same object over and over each time.""" |
| 58 | |
| 59 | global gtdi |
| 60 | if not gtdi: |
| 61 | gtdi = GptTestDiskImage(u_boot_console) |
| 62 | return gtdi |
| 63 | |
| 64 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 65 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 66 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 67 | def test_gpt_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 68 | """Test the gpt guid command.""" |
| 69 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 70 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 71 | output = u_boot_console.run_command('gpt guid host 0') |
| 72 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
| 73 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 74 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 75 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 76 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 77 | def test_gpt_save_guid(state_disk_image, u_boot_console): |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 78 | """Test the gpt guid command to save GUID into a string.""" |
| 79 | |
| 80 | if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y': |
| 81 | pytest.skip('gpt command not supported') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 82 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | 1b11fb0 | 2017-09-09 23:47:13 -0700 | [diff] [blame] | 83 | output = u_boot_console.run_command('gpt guid host 0 newguid') |
| 84 | output = u_boot_console.run_command('printenv newguid') |
| 85 | assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 86 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 87 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 88 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 89 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 90 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 91 | def test_gpt_rename_partition(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 92 | """Test the gpt rename command to write partition names.""" |
| 93 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 94 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 95 | u_boot_console.run_command('gpt rename host 0 1 first') |
| 96 | output = u_boot_console.run_command('gpt read host 0') |
| 97 | assert 'name first' in output |
| 98 | u_boot_console.run_command('gpt rename host 0 2 second') |
| 99 | output = u_boot_console.run_command('gpt read host 0') |
| 100 | assert 'name second' in output |
| 101 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 102 | @pytest.mark.boardspec('sandbox') |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 103 | @pytest.mark.buildconfigspec('cmd_gpt') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 104 | @pytest.mark.buildconfigspec('cmd_gpt_rename') |
| 105 | @pytest.mark.buildconfigspec('cmd_part') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame^] | 106 | @pytest.mark.requiredtool('sgdisk') |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 107 | def test_gpt_swap_partitions(state_disk_image, u_boot_console): |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 108 | """Test the gpt swap command to exchange two partition names.""" |
| 109 | |
Stephen Warren | 97dcbde | 2017-09-15 12:19:38 -0600 | [diff] [blame] | 110 | u_boot_console.run_command('host bind 0 ' + state_disk_image.path) |
Alison Chaiken | cb39296 | 2017-09-09 23:54:51 -0700 | [diff] [blame] | 111 | output = u_boot_console.run_command('part list host 0') |
| 112 | assert '0x000007ff "first"' in output |
| 113 | assert '0x000017ff "second"' in output |
| 114 | u_boot_console.run_command('gpt swap host 0 first second') |
| 115 | output = u_boot_console.run_command('part list host 0') |
| 116 | assert '0x000007ff "second"' in output |
| 117 | assert '0x000017ff "first"' in output |