bootstd_common.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test for bootdev functions. All start with 'bootdev'
  4. *
  5. * Copyright 2021 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <bootdev.h>
  10. #include <bootstd.h>
  11. #include <dm.h>
  12. #include <memalign.h>
  13. #include <mmc.h>
  14. #include <linux/log2.h>
  15. #include <test/suites.h>
  16. #include <test/ut.h>
  17. #include <u-boot/crc.h>
  18. #include "bootstd_common.h"
  19. /* tracks whether bootstd_setup_for_tests() has been run yet */
  20. bool vbe_setup_done;
  21. /* set up MMC for VBE tests */
  22. int bootstd_setup_for_tests(void)
  23. {
  24. ALLOC_CACHE_ALIGN_BUFFER(u8, buf, MMC_MAX_BLOCK_LEN);
  25. struct udevice *mmc;
  26. struct blk_desc *desc;
  27. int ret;
  28. if (vbe_setup_done)
  29. return 0;
  30. /* Set up the version string */
  31. ret = uclass_get_device(UCLASS_MMC, 1, &mmc);
  32. if (ret)
  33. return log_msg_ret("mmc", -EIO);
  34. desc = blk_get_by_device(mmc);
  35. memset(buf, '\0', MMC_MAX_BLOCK_LEN);
  36. strcpy(buf, TEST_VERSION);
  37. if (blk_dwrite(desc, VERSION_START_BLK, 1, buf) != 1)
  38. return log_msg_ret("wr1", -EIO);
  39. /* Set up the nvdata */
  40. memset(buf, '\0', MMC_MAX_BLOCK_LEN);
  41. buf[1] = ilog2(0x40) << 4 | 1;
  42. *(u32 *)(buf + 4) = TEST_VERNUM;
  43. buf[0] = crc8(0, buf + 1, 0x3f);
  44. if (blk_dwrite(desc, NVDATA_START_BLK, 1, buf) != 1)
  45. return log_msg_ret("wr2", -EIO);
  46. vbe_setup_done = true;
  47. return 0;
  48. }
  49. int bootstd_test_drop_bootdev_order(struct unit_test_state *uts)
  50. {
  51. struct bootstd_priv *priv;
  52. struct udevice *bootstd;
  53. ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
  54. priv = dev_get_priv(bootstd);
  55. priv->bootdev_order = NULL;
  56. return 0;
  57. }
  58. int bootstd_test_check_mmc_hunter(struct unit_test_state *uts)
  59. {
  60. struct bootdev_hunter *start, *mmc;
  61. struct bootstd_priv *std;
  62. uint seq;
  63. /* get access to the used hunters */
  64. ut_assertok(bootstd_get_priv(&std));
  65. /* check that the hunter was used */
  66. start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
  67. mmc = BOOTDEV_HUNTER_GET(mmc_bootdev_hunter);
  68. seq = mmc - start;
  69. ut_asserteq(BIT(seq), std->hunters_used);
  70. return 0;
  71. }
  72. int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  73. {
  74. struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd_test);
  75. const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd_test);
  76. int ret;
  77. ret = bootstd_setup_for_tests();
  78. if (ret) {
  79. printf("Failed to set up for bootstd tests (err=%d)\n", ret);
  80. return CMD_RET_FAILURE;
  81. }
  82. return cmd_ut_category("bootstd", "bootstd_test_",
  83. tests, n_ents, argc, argv);
  84. }