test_gpt.py 7.2 KB

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