test_gpt.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. class GptTestDiskImage(object):
  13. """Disk Image used by the GPT tests."""
  14. def __init__(self, u_boot_console):
  15. """Initialize a new GptTestDiskImage object.
  16. Args:
  17. u_boot_console: A U-Boot console.
  18. Returns:
  19. Nothing.
  20. """
  21. filename = 'test_gpt_disk_image.bin'
  22. persistent = u_boot_console.config.persistent_data_dir + '/' + filename
  23. self.path = u_boot_console.config.result_dir + '/' + filename
  24. with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent):
  25. if os.path.exists(persistent):
  26. u_boot_console.log.action('Disk image file ' + persistent +
  27. ' already exists')
  28. else:
  29. u_boot_console.log.action('Generating ' + persistent)
  30. fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
  31. os.ftruncate(fd, 4194304)
  32. os.close(fd)
  33. cmd = ('sgdisk',
  34. '--disk-guid=375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
  35. persistent)
  36. u_boot_utils.run_and_log(u_boot_console, cmd)
  37. # part1 offset 1MB size 1MB
  38. cmd = ('sgdisk', '--new=1:2048:4095', '--change-name=1:part1',
  39. persistent)
  40. # part2 offset 2MB size 1.5MB
  41. u_boot_utils.run_and_log(u_boot_console, cmd)
  42. cmd = ('sgdisk', '--new=2:4096:7167', '--change-name=2:part2',
  43. persistent)
  44. u_boot_utils.run_and_log(u_boot_console, cmd)
  45. cmd = ('sgdisk', '--load-backup=' + persistent)
  46. u_boot_utils.run_and_log(u_boot_console, cmd)
  47. cmd = ('cp', persistent, self.path)
  48. u_boot_utils.run_and_log(u_boot_console, cmd)
  49. gtdi = None
  50. @pytest.fixture(scope='function')
  51. def state_disk_image(u_boot_console):
  52. """pytest fixture to provide a GptTestDiskImage object to tests.
  53. This is function-scoped because it uses u_boot_console, which is also
  54. function-scoped. However, we don't need to actually do any function-scope
  55. work, so this simply returns the same object over and over each time."""
  56. global gtdi
  57. if not gtdi:
  58. gtdi = GptTestDiskImage(u_boot_console)
  59. return gtdi
  60. @pytest.mark.boardspec('sandbox')
  61. @pytest.mark.buildconfigspec('cmd_gpt')
  62. @pytest.mark.buildconfigspec('cmd_part')
  63. @pytest.mark.requiredtool('sgdisk')
  64. def test_gpt_read(state_disk_image, u_boot_console):
  65. """Test the gpt read command."""
  66. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  67. output = u_boot_console.run_command('gpt read host 0')
  68. assert 'Start 1MiB, size 1MiB' in output
  69. assert 'Block size 512, name part1' in output
  70. assert 'Start 2MiB, size 1MiB' in output
  71. assert 'Block size 512, name part2' in output
  72. output = u_boot_console.run_command('part list host 0')
  73. assert '0x00000800 0x00000fff "part1"' in output
  74. assert '0x00001000 0x00001bff "part2"' in output
  75. @pytest.mark.boardspec('sandbox')
  76. @pytest.mark.buildconfigspec('cmd_gpt')
  77. @pytest.mark.requiredtool('sgdisk')
  78. def test_gpt_verify(state_disk_image, u_boot_console):
  79. """Test the gpt verify command."""
  80. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  81. output = u_boot_console.run_command('gpt verify host 0')
  82. assert 'Verify GPT: success!' in output
  83. @pytest.mark.boardspec('sandbox')
  84. @pytest.mark.buildconfigspec('cmd_gpt')
  85. @pytest.mark.requiredtool('sgdisk')
  86. def test_gpt_guid(state_disk_image, u_boot_console):
  87. """Test the gpt guid command."""
  88. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  89. output = u_boot_console.run_command('gpt guid host 0')
  90. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  91. @pytest.mark.boardspec('sandbox')
  92. @pytest.mark.buildconfigspec('cmd_gpt')
  93. @pytest.mark.requiredtool('sgdisk')
  94. def test_gpt_save_guid(state_disk_image, u_boot_console):
  95. """Test the gpt guid command to save GUID into a string."""
  96. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  97. pytest.skip('gpt command not supported')
  98. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  99. output = u_boot_console.run_command('gpt guid host 0 newguid')
  100. output = u_boot_console.run_command('printenv newguid')
  101. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  102. @pytest.mark.boardspec('sandbox')
  103. @pytest.mark.buildconfigspec('cmd_gpt')
  104. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  105. @pytest.mark.buildconfigspec('cmd_part')
  106. @pytest.mark.requiredtool('sgdisk')
  107. def test_gpt_rename_partition(state_disk_image, u_boot_console):
  108. """Test the gpt rename command to write partition names."""
  109. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  110. u_boot_console.run_command('gpt rename host 0 1 first')
  111. output = u_boot_console.run_command('gpt read host 0')
  112. assert 'name first' in output
  113. u_boot_console.run_command('gpt rename host 0 2 second')
  114. output = u_boot_console.run_command('gpt read host 0')
  115. assert 'name second' in output
  116. output = u_boot_console.run_command('part list host 0')
  117. assert '0x00000800 0x00000fff "first"' in output
  118. assert '0x00001000 0x00001bff "second"' in output
  119. @pytest.mark.boardspec('sandbox')
  120. @pytest.mark.buildconfigspec('cmd_gpt')
  121. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  122. @pytest.mark.buildconfigspec('cmd_part')
  123. @pytest.mark.requiredtool('sgdisk')
  124. def test_gpt_swap_partitions(state_disk_image, u_boot_console):
  125. """Test the gpt swap command to exchange two partition names."""
  126. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  127. output = u_boot_console.run_command('part list host 0')
  128. assert '0x00000800 0x00000fff "first"' in output
  129. assert '0x00001000 0x00001bff "second"' in output
  130. u_boot_console.run_command('gpt swap host 0 first second')
  131. output = u_boot_console.run_command('part list host 0')
  132. assert '0x00000800 0x00000fff "second"' in output
  133. assert '0x00001000 0x00001bff "first"' in output
  134. @pytest.mark.boardspec('sandbox')
  135. @pytest.mark.buildconfigspec('cmd_gpt')
  136. @pytest.mark.buildconfigspec('cmd_part')
  137. @pytest.mark.requiredtool('sgdisk')
  138. def test_gpt_write(state_disk_image, u_boot_console):
  139. """Test the gpt write command."""
  140. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  141. output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
  142. assert 'Writing GPT: success!' in output
  143. output = u_boot_console.run_command('part list host 0')
  144. assert '0x00000022 0x00001fde "all"' in output
  145. 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;"')
  146. assert 'Writing GPT: success!' in output
  147. output = u_boot_console.run_command('part list host 0')
  148. assert '0x00000800 0x00000fff "first"' in output
  149. assert '0x00001000 0x00001bff "second"' in output
  150. output = u_boot_console.run_command('gpt guid host 0')
  151. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output