bootmeth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test for bootdev functions. All start with 'bootmeth'
  4. *
  5. * Copyright 2021 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <bootmeth.h>
  10. #include <bootstd.h>
  11. #include <dm.h>
  12. #include <test/suites.h>
  13. #include <test/ut.h>
  14. #include "bootstd_common.h"
  15. /* Check 'bootmeth list' command */
  16. static int bootmeth_cmd_list(struct unit_test_state *uts)
  17. {
  18. console_record_reset_enable();
  19. ut_assertok(run_command("bootmeth list", 0));
  20. ut_assert_nextline("Order Seq Name Description");
  21. ut_assert_nextlinen("---");
  22. ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
  23. ut_assert_nextline(" 1 1 efi EFI boot from an .efi file");
  24. if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
  25. ut_assert_nextline(" glob 2 firmware0 VBE simple");
  26. ut_assert_nextlinen("---");
  27. ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
  28. "(3 bootmeths)" : "(2 bootmeths)");
  29. ut_assert_console_end();
  30. return 0;
  31. }
  32. BOOTSTD_TEST(bootmeth_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  33. /* Check 'bootmeth order' command */
  34. static int bootmeth_cmd_order(struct unit_test_state *uts)
  35. {
  36. /* Select just one bootmethod */
  37. console_record_reset_enable();
  38. ut_assertok(run_command("bootmeth order extlinux", 0));
  39. ut_assert_console_end();
  40. ut_assertnonnull(env_get("bootmeths"));
  41. ut_asserteq_str("extlinux", env_get("bootmeths"));
  42. /* Only that one should be listed */
  43. ut_assertok(run_command("bootmeth list", 0));
  44. ut_assert_nextline("Order Seq Name Description");
  45. ut_assert_nextlinen("---");
  46. ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
  47. ut_assert_nextlinen("---");
  48. ut_assert_nextline("(1 bootmeth)");
  49. ut_assert_console_end();
  50. /* Check the -a flag, efi should show as not in the order ("-") */
  51. ut_assertok(run_command("bootmeth list -a", 0));
  52. ut_assert_nextline("Order Seq Name Description");
  53. ut_assert_nextlinen("---");
  54. ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
  55. ut_assert_nextline(" - 1 efi EFI boot from an .efi file");
  56. if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
  57. ut_assert_nextline(" glob 2 firmware0 VBE simple");
  58. ut_assert_nextlinen("---");
  59. ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
  60. "(3 bootmeths)" : "(2 bootmeths)");
  61. ut_assert_console_end();
  62. /* Check the -a flag with the reverse order */
  63. ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
  64. ut_assert_console_end();
  65. ut_assertok(run_command("bootmeth list -a", 0));
  66. ut_assert_nextline("Order Seq Name Description");
  67. ut_assert_nextlinen("---");
  68. ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
  69. ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
  70. if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
  71. ut_assert_nextline(" glob 2 firmware0 VBE simple");
  72. ut_assert_nextlinen("---");
  73. ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
  74. "(3 bootmeths)" : "(2 bootmeths)");
  75. ut_assert_console_end();
  76. /* Now reset the order to empty, which should show all of them again */
  77. ut_assertok(run_command("bootmeth order", 0));
  78. ut_assert_console_end();
  79. ut_assertnull(env_get("bootmeths"));
  80. ut_assertok(run_command("bootmeth list", 0));
  81. ut_assert_skip_to_line(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
  82. "(3 bootmeths)" : "(2 bootmeths)");
  83. /* Try reverse order */
  84. ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
  85. ut_assert_console_end();
  86. ut_assertok(run_command("bootmeth list", 0));
  87. ut_assert_nextline("Order Seq Name Description");
  88. ut_assert_nextlinen("---");
  89. ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
  90. ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
  91. ut_assert_nextlinen("---");
  92. ut_assert_nextline("(2 bootmeths)");
  93. ut_assertnonnull(env_get("bootmeths"));
  94. ut_asserteq_str("efi extlinux", env_get("bootmeths"));
  95. ut_assert_console_end();
  96. return 0;
  97. }
  98. BOOTSTD_TEST(bootmeth_cmd_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  99. /* Check 'bootmeth order' command with global bootmeths */
  100. static int bootmeth_cmd_order_glob(struct unit_test_state *uts)
  101. {
  102. if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
  103. return -EAGAIN;
  104. console_record_reset_enable();
  105. ut_assertok(run_command("bootmeth order \"efi firmware0\"", 0));
  106. ut_assert_console_end();
  107. ut_assertok(run_command("bootmeth list", 0));
  108. ut_assert_nextline("Order Seq Name Description");
  109. ut_assert_nextlinen("---");
  110. ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
  111. ut_assert_nextline(" glob 2 firmware0 VBE simple");
  112. ut_assert_nextlinen("---");
  113. ut_assert_nextline("(2 bootmeths)");
  114. ut_assertnonnull(env_get("bootmeths"));
  115. ut_asserteq_str("efi firmware0", env_get("bootmeths"));
  116. ut_assert_console_end();
  117. return 0;
  118. }
  119. BOOTSTD_TEST(bootmeth_cmd_order_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  120. /* Check 'bootmeths' env var */
  121. static int bootmeth_env(struct unit_test_state *uts)
  122. {
  123. struct bootstd_priv *std;
  124. ut_assertok(bootstd_get_priv(&std));
  125. /* Select just one bootmethod */
  126. console_record_reset_enable();
  127. ut_assertok(env_set("bootmeths", "extlinux"));
  128. ut_asserteq(1, std->bootmeth_count);
  129. /* Select an invalid bootmethod */
  130. ut_asserteq(1, run_command("setenv bootmeths fred", 0));
  131. ut_assert_nextline("Unknown bootmeth 'fred'");
  132. ut_assert_nextlinen("## Error inserting");
  133. ut_assert_console_end();
  134. ut_assertok(env_set("bootmeths", "efi extlinux"));
  135. ut_asserteq(2, std->bootmeth_count);
  136. ut_assert_console_end();
  137. return 0;
  138. }
  139. BOOTSTD_TEST(bootmeth_env, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  140. /* Check the get_state_desc() method */
  141. static int bootmeth_state(struct unit_test_state *uts)
  142. {
  143. struct udevice *dev;
  144. char buf[50];
  145. ut_assertok(uclass_first_device_err(UCLASS_BOOTMETH, &dev));
  146. ut_assertnonnull(dev);
  147. ut_assertok(bootmeth_get_state_desc(dev, buf, sizeof(buf)));
  148. ut_asserteq_str("OK", buf);
  149. return 0;
  150. }
  151. BOOTSTD_TEST(bootmeth_state, UT_TESTF_DM | UT_TESTF_SCAN_FDT);