test_gpt.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright (c) 2017 Alison Chaiken
  2. # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0
  5. # Test GPT manipulation commands.
  6. import os
  7. import pytest
  8. import u_boot_utils
  9. """
  10. These tests rely on a 4 MB disk image, which is automatically created by
  11. the test.
  12. """
  13. class GptTestDiskImage(object):
  14. """Disk Image used by the GPT tests."""
  15. def __init__(self, u_boot_console):
  16. """Initialize a new GptTestDiskImage object.
  17. Args:
  18. u_boot_console: A U-Boot console.
  19. Returns:
  20. Nothing.
  21. """
  22. filename = 'test_gpt_disk_image.bin'
  23. persistent = u_boot_console.config.persistent_data_dir + '/' + filename
  24. self.path = u_boot_console.config.result_dir + '/' + filename
  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', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
  34. persistent)
  35. u_boot_utils.run_and_log(u_boot_console, cmd)
  36. cmd = ('sgdisk', '--new=1:2048:2560', '-c 1:part1', persistent)
  37. u_boot_utils.run_and_log(u_boot_console, cmd)
  38. cmd = ('sgdisk', '--new=2:4096:4608', '-c 2:part2', persistent)
  39. u_boot_utils.run_and_log(u_boot_console, cmd)
  40. cmd = ('sgdisk', '-l', persistent)
  41. u_boot_utils.run_and_log(u_boot_console, cmd)
  42. cmd = ('cp', persistent, self.path)
  43. u_boot_utils.run_and_log(u_boot_console, cmd)
  44. gtdi = None
  45. @pytest.fixture(scope='function')
  46. def state_disk_image(u_boot_console):
  47. """pytest fixture to provide a GptTestDiskImage object to tests.
  48. This is function-scoped because it uses u_boot_console, which is also
  49. function-scoped. However, we don't need to actually do any function-scope
  50. work, so this simply returns the same object over and over each time."""
  51. global gtdi
  52. if not gtdi:
  53. gtdi = GptTestDiskImage(u_boot_console)
  54. return gtdi
  55. @pytest.mark.boardspec('sandbox')
  56. @pytest.mark.buildconfigspec('cmd_gpt')
  57. @pytest.mark.buildconfigspec('cmd_part')
  58. @pytest.mark.requiredtool('sgdisk')
  59. def test_gpt_read(state_disk_image, u_boot_console):
  60. """Test the gpt read command."""
  61. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  62. output = u_boot_console.run_command('gpt read host 0')
  63. assert 'Start 1MiB, size 0MiB' in output
  64. assert 'Block size 512, name part1' in output
  65. assert 'Start 2MiB, size 0MiB' in output
  66. assert 'Block size 512, name part2' in output
  67. output = u_boot_console.run_command('part list host 0')
  68. assert '0x00000800 0x00000a00 "part1"' in output
  69. assert '0x00001000 0x00001200 "part2"' in output
  70. @pytest.mark.boardspec('sandbox')
  71. @pytest.mark.buildconfigspec('cmd_gpt')
  72. @pytest.mark.requiredtool('sgdisk')
  73. def test_gpt_verify(state_disk_image, u_boot_console):
  74. """Test the gpt verify command."""
  75. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  76. output = u_boot_console.run_command('gpt verify host 0')
  77. assert 'Verify GPT: success!' in output
  78. @pytest.mark.boardspec('sandbox')
  79. @pytest.mark.buildconfigspec('cmd_gpt')
  80. @pytest.mark.requiredtool('sgdisk')
  81. def test_gpt_guid(state_disk_image, u_boot_console):
  82. """Test the gpt guid command."""
  83. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  84. output = u_boot_console.run_command('gpt guid host 0')
  85. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  86. @pytest.mark.boardspec('sandbox')
  87. @pytest.mark.buildconfigspec('cmd_gpt')
  88. @pytest.mark.requiredtool('sgdisk')
  89. def test_gpt_save_guid(state_disk_image, u_boot_console):
  90. """Test the gpt guid command to save GUID into a string."""
  91. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  92. pytest.skip('gpt command not supported')
  93. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  94. output = u_boot_console.run_command('gpt guid host 0 newguid')
  95. output = u_boot_console.run_command('printenv newguid')
  96. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  97. @pytest.mark.boardspec('sandbox')
  98. @pytest.mark.buildconfigspec('cmd_gpt')
  99. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  100. @pytest.mark.buildconfigspec('cmd_part')
  101. @pytest.mark.requiredtool('sgdisk')
  102. def test_gpt_rename_partition(state_disk_image, u_boot_console):
  103. """Test the gpt rename command to write partition names."""
  104. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  105. u_boot_console.run_command('gpt rename host 0 1 first')
  106. output = u_boot_console.run_command('gpt read host 0')
  107. assert 'name first' in output
  108. u_boot_console.run_command('gpt rename host 0 2 second')
  109. output = u_boot_console.run_command('gpt read host 0')
  110. assert 'name second' in output
  111. output = u_boot_console.run_command('part list host 0')
  112. assert '0x00000800 0x000007ff "first"' in output
  113. assert '0x00001000 0x000017ff "second"' in output
  114. # command error here because 'end LBA' (column 2) change after rename
  115. # (previous value can be found in test_gpt_read)
  116. # "first" 0xa00 => 0x7ff : it is an invalid value < start LBA !
  117. # "seconf" 0x1200 => 0x17ff : size is increasing !
  118. @pytest.mark.boardspec('sandbox')
  119. @pytest.mark.buildconfigspec('cmd_gpt')
  120. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  121. @pytest.mark.buildconfigspec('cmd_part')
  122. @pytest.mark.requiredtool('sgdisk')
  123. def test_gpt_swap_partitions(state_disk_image, u_boot_console):
  124. """Test the gpt swap command to exchange two partition names."""
  125. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  126. output = u_boot_console.run_command('part list host 0')
  127. assert '0x00000800 0x000007ff "first"' in output
  128. assert '0x00001000 0x000017ff "second"' in output
  129. u_boot_console.run_command('gpt swap host 0 first second')
  130. output = u_boot_console.run_command('part list host 0')
  131. assert '0x00000800 0x000007ff "second"' in output
  132. assert '0x00001000 0x000017ff "first"' in output
  133. @pytest.mark.boardspec('sandbox')
  134. @pytest.mark.buildconfigspec('cmd_gpt')
  135. @pytest.mark.buildconfigspec('cmd_part')
  136. @pytest.mark.requiredtool('sgdisk')
  137. def test_gpt_write(state_disk_image, u_boot_console):
  138. """Test the gpt write command."""
  139. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  140. output = u_boot_console.run_command('gpt write host 0 "name=all,size=0"')
  141. assert 'Writing GPT: success!' in output
  142. output = u_boot_console.run_command('part list host 0')
  143. assert '0x00000022 0x00001fde "all"' in output
  144. output = u_boot_console.run_command('gpt write host 0 "uuid_disk=375a56f7-d6c9-4e81-b5f0-09d41ca89efe;name=first,start=0x100000,size=0x40200;name=second,start=0x200000,size=0x40200;"')
  145. assert 'Writing GPT: success!' in output
  146. output = u_boot_console.run_command('part list host 0')
  147. assert '0x00000800 0x00000a00 "first"' in output
  148. assert '0x00001000 0x00001200 "second"' in output
  149. output = u_boot_console.run_command('gpt guid host 0')
  150. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output