test_gpt.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2017 Alison Chaiken
  3. # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
  4. # Test GPT manipulation commands.
  5. import os
  6. import pytest
  7. import u_boot_utils
  8. """
  9. These tests rely on a 4 MB disk image, which is automatically created by
  10. the test.
  11. """
  12. # Mark all tests here as slow
  13. pytestmark = pytest.mark.slow
  14. class GptTestDiskImage(object):
  15. """Disk Image used by the GPT tests."""
  16. def __init__(self, u_boot_console):
  17. """Initialize a new GptTestDiskImage object.
  18. Args:
  19. u_boot_console: A U-Boot console.
  20. Returns:
  21. Nothing.
  22. """
  23. filename = 'test_gpt_disk_image.bin'
  24. persistent = u_boot_console.config.persistent_data_dir + '/' + filename
  25. self.path = u_boot_console.config.result_dir + '/' + filename
  26. with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent):
  27. if os.path.exists(persistent):
  28. u_boot_console.log.action('Disk image file ' + persistent +
  29. ' already exists')
  30. else:
  31. u_boot_console.log.action('Generating ' + persistent)
  32. fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
  33. os.ftruncate(fd, 4194304)
  34. os.close(fd)
  35. cmd = ('sgdisk',
  36. '--disk-guid=375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
  37. persistent)
  38. u_boot_utils.run_and_log(u_boot_console, cmd)
  39. # part1 offset 1MB size 1MB
  40. cmd = ('sgdisk', '--new=1:2048:4095', '--change-name=1:part1',
  41. persistent)
  42. # part2 offset 2MB size 1.5MB
  43. u_boot_utils.run_and_log(u_boot_console, cmd)
  44. cmd = ('sgdisk', '--new=2:4096:7167', '--change-name=2:part2',
  45. persistent)
  46. u_boot_utils.run_and_log(u_boot_console, cmd)
  47. cmd = ('sgdisk', '--load-backup=' + persistent)
  48. u_boot_utils.run_and_log(u_boot_console, cmd)
  49. cmd = ('cp', persistent, self.path)
  50. u_boot_utils.run_and_log(u_boot_console, cmd)
  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. global gtdi
  59. if not gtdi:
  60. gtdi = GptTestDiskImage(u_boot_console)
  61. return gtdi
  62. @pytest.mark.boardspec('sandbox')
  63. @pytest.mark.buildconfigspec('cmd_gpt')
  64. @pytest.mark.buildconfigspec('cmd_part')
  65. @pytest.mark.requiredtool('sgdisk')
  66. def test_gpt_read(state_disk_image, u_boot_console):
  67. """Test the gpt read command."""
  68. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  69. output = u_boot_console.run_command('gpt read host 0')
  70. assert 'Start 1MiB, size 1MiB' in output
  71. assert 'Block size 512, name part1' in output
  72. assert 'Start 2MiB, size 1MiB' in output
  73. assert 'Block size 512, name part2' in output
  74. output = u_boot_console.run_command('part list host 0')
  75. assert '0x00000800 0x00000fff "part1"' in output
  76. assert '0x00001000 0x00001bff "part2"' in output
  77. @pytest.mark.boardspec('sandbox')
  78. @pytest.mark.buildconfigspec('cmd_gpt')
  79. @pytest.mark.requiredtool('sgdisk')
  80. def test_gpt_verify(state_disk_image, u_boot_console):
  81. """Test the gpt verify command."""
  82. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  83. output = u_boot_console.run_command('gpt verify host 0')
  84. assert 'Verify GPT: success!' in output
  85. @pytest.mark.boardspec('sandbox')
  86. @pytest.mark.buildconfigspec('cmd_gpt')
  87. @pytest.mark.requiredtool('sgdisk')
  88. def test_gpt_repair(state_disk_image, u_boot_console):
  89. """Test the gpt repair command."""
  90. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  91. output = u_boot_console.run_command('gpt repair host 0')
  92. assert 'Repairing GPT: success!' in output
  93. @pytest.mark.boardspec('sandbox')
  94. @pytest.mark.buildconfigspec('cmd_gpt')
  95. @pytest.mark.requiredtool('sgdisk')
  96. def test_gpt_guid(state_disk_image, u_boot_console):
  97. """Test the gpt guid command."""
  98. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  99. output = u_boot_console.run_command('gpt guid host 0')
  100. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  101. @pytest.mark.boardspec('sandbox')
  102. @pytest.mark.buildconfigspec('cmd_gpt')
  103. @pytest.mark.requiredtool('sgdisk')
  104. def test_gpt_save_guid(state_disk_image, u_boot_console):
  105. """Test the gpt guid command to save GUID into a string."""
  106. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  107. pytest.skip('gpt command not supported')
  108. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  109. output = u_boot_console.run_command('gpt guid host 0 newguid')
  110. output = u_boot_console.run_command('printenv newguid')
  111. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  112. @pytest.mark.boardspec('sandbox')
  113. @pytest.mark.buildconfigspec('cmd_gpt')
  114. @pytest.mark.requiredtool('sgdisk')
  115. def test_gpt_part_type_uuid(state_disk_image, u_boot_console):
  116. """Test the gpt partittion type UUID command."""
  117. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  118. output = u_boot_console.run_command('part type host 0:1')
  119. assert '0fc63daf-8483-4772-8e79-3d69d8477de4' in output
  120. @pytest.mark.boardspec('sandbox')
  121. @pytest.mark.buildconfigspec('cmd_gpt')
  122. @pytest.mark.requiredtool('sgdisk')
  123. def test_gpt_part_type_save_uuid(state_disk_image, u_boot_console):
  124. """Test the gpt partittion type to save UUID into a string."""
  125. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  126. pytest.skip('gpt command not supported')
  127. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  128. output = u_boot_console.run_command('part type host 0:1 newguid')
  129. output = u_boot_console.run_command('printenv newguid')
  130. assert '0fc63daf-8483-4772-8e79-3d69d8477de4' in output
  131. @pytest.mark.boardspec('sandbox')
  132. @pytest.mark.buildconfigspec('cmd_gpt')
  133. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  134. @pytest.mark.buildconfigspec('cmd_part')
  135. @pytest.mark.requiredtool('sgdisk')
  136. def test_gpt_rename_partition(state_disk_image, u_boot_console):
  137. """Test the gpt rename command to write partition names."""
  138. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  139. u_boot_console.run_command('gpt rename host 0 1 first')
  140. output = u_boot_console.run_command('gpt read host 0')
  141. assert 'name first' in output
  142. u_boot_console.run_command('gpt rename host 0 2 second')
  143. output = u_boot_console.run_command('gpt read host 0')
  144. assert 'name second' in output
  145. output = u_boot_console.run_command('part list host 0')
  146. assert '0x00000800 0x00000fff "first"' in output
  147. assert '0x00001000 0x00001bff "second"' in output
  148. @pytest.mark.boardspec('sandbox')
  149. @pytest.mark.buildconfigspec('cmd_gpt')
  150. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  151. @pytest.mark.buildconfigspec('cmd_part')
  152. @pytest.mark.requiredtool('sgdisk')
  153. def test_gpt_swap_partitions(state_disk_image, u_boot_console):
  154. """Test the gpt swap command to exchange two partition names."""
  155. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  156. output = u_boot_console.run_command('part list host 0')
  157. assert '0x00000800 0x00000fff "first"' in output
  158. assert '0x00001000 0x00001bff "second"' in output
  159. u_boot_console.run_command('gpt swap host 0 first second')
  160. output = u_boot_console.run_command('part list host 0')
  161. assert '0x00000800 0x00000fff "second"' in output
  162. assert '0x00001000 0x00001bff "first"' in output
  163. @pytest.mark.boardspec('sandbox')
  164. @pytest.mark.buildconfigspec('cmd_gpt')
  165. @pytest.mark.buildconfigspec('cmd_part')
  166. @pytest.mark.requiredtool('sgdisk')
  167. def test_gpt_write(state_disk_image, u_boot_console):
  168. """Test the gpt write command."""
  169. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  170. output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
  171. assert 'Writing GPT: success!' in output
  172. output = u_boot_console.run_command('part list host 0')
  173. assert '0x00000022 0x00001fde "all"' in output
  174. 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;"')
  175. assert 'Writing GPT: success!' in output
  176. output = u_boot_console.run_command('part list host 0')
  177. assert '0x00000800 0x00000fff "first"' in output
  178. assert '0x00001000 0x00001bff "second"' in output
  179. output = u_boot_console.run_command('gpt guid host 0')
  180. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output