capsule_common.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2023, Linaro Limited
  3. """Common function for UEFI capsule test."""
  4. from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
  5. def capsule_setup(u_boot_console, disk_img, osindications):
  6. """setup the test
  7. Args:
  8. u_boot_console -- A console connection to U-Boot.
  9. disk_img -- A path to disk image to be used for testing.
  10. osindications -- String of osindications value.
  11. """
  12. u_boot_console.run_command_list([
  13. f'host bind 0 {disk_img}',
  14. 'printenv -e PlatformLangCodes', # workaround for terminal size determination
  15. 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
  16. 'efidebug boot order 1',
  17. 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;'
  18. 'u-boot-env raw 0x150000 0x200000"'])
  19. if osindications is None:
  20. u_boot_console.run_command('env set -e OsIndications')
  21. else:
  22. u_boot_console.run_command(f'env set -e -nv -bs -rt OsIndications ={osindications}')
  23. u_boot_console.run_command('env save')
  24. def init_content(u_boot_console, target, filename, expected):
  25. """initialize test content
  26. Args:
  27. u_boot_console -- A console connection to U-Boot.
  28. target -- Target address to place the content.
  29. filename -- File name of the content.
  30. expected -- Expected string of the content.
  31. """
  32. output = u_boot_console.run_command_list([
  33. 'sf probe 0:0',
  34. f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{filename}',
  35. f'sf write 4000000 {target} 10',
  36. 'sf read 5000000 100000 10',
  37. 'md.b 5000000 10'])
  38. assert expected in ''.join(output)
  39. def place_capsule_file(u_boot_console, filenames):
  40. """place the capsule file
  41. Args:
  42. u_boot_console -- A console connection to U-Boot.
  43. filenames -- File name array of the target capsule files.
  44. """
  45. for name in filenames:
  46. u_boot_console.run_command_list([
  47. f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{name}',
  48. f'fatwrite host 0:1 4000000 {CAPSULE_INSTALL_DIR}/{name} $filesize'])
  49. output = u_boot_console.run_command(f'fatls host 0:1 {CAPSULE_INSTALL_DIR}')
  50. for name in filenames:
  51. assert name in ''.join(output)
  52. def exec_manual_update(u_boot_console, disk_img, filenames, need_reboot = True):
  53. """execute capsule update manually
  54. Args:
  55. u_boot_console -- A console connection to U-Boot.
  56. disk_img -- A path to disk image to be used for testing.
  57. filenames -- File name array of the target capsule files.
  58. need_reboot -- Flag indicates whether system reboot is required.
  59. """
  60. # make sure that dfu_alt_info exists even persistent variables
  61. # are not available.
  62. output = u_boot_console.run_command_list([
  63. 'env set dfu_alt_info '
  64. '"sf 0:0=u-boot-bin raw 0x100000 0x50000;'
  65. 'u-boot-env raw 0x150000 0x200000"',
  66. f'host bind 0 {disk_img}',
  67. f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
  68. for name in filenames:
  69. assert name in ''.join(output)
  70. # need to run uefi command to initiate capsule handling
  71. u_boot_console.run_command(
  72. 'env print -e Capsule0000', wait_for_reboot = need_reboot)
  73. def check_file_removed(u_boot_console, disk_img, filenames):
  74. """check files are removed
  75. Args:
  76. u_boot_console -- A console connection to U-Boot.
  77. disk_img -- A path to disk image to be used for testing.
  78. filenames -- File name array of the target capsule files.
  79. """
  80. output = u_boot_console.run_command_list([
  81. f'host bind 0 {disk_img}',
  82. f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
  83. for name in filenames:
  84. assert name not in ''.join(output)
  85. def check_file_exist(u_boot_console, disk_img, filenames):
  86. """check files exist
  87. Args:
  88. u_boot_console -- A console connection to U-Boot.
  89. disk_img -- A path to disk image to be used for testing.
  90. filenames -- File name array of the target capsule files.
  91. """
  92. output = u_boot_console.run_command_list([
  93. f'host bind 0 {disk_img}',
  94. f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
  95. for name in filenames:
  96. assert name in ''.join(output)
  97. def verify_content(u_boot_console, target, expected):
  98. """verify the content
  99. Args:
  100. u_boot_console -- A console connection to U-Boot.
  101. target -- Target address to verify.
  102. expected -- Expected string of the content.
  103. """
  104. output = u_boot_console.run_command_list([
  105. 'sf probe 0:0',
  106. f'sf read 4000000 {target} 10',
  107. 'md.b 4000000 10'])
  108. assert expected in ''.join(output)
  109. def do_reboot_dtb_specified(u_boot_config, u_boot_console, dtb_filename):
  110. """do reboot with specified DTB
  111. Args:
  112. u_boot_config -- U-boot configuration.
  113. u_boot_console -- A console connection to U-Boot.
  114. dtb_filename -- DTB file name.
  115. """
  116. mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
  117. u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
  118. + f'/{dtb_filename}'
  119. u_boot_console.restart_uboot()