spl_load.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <image.h>
  8. #include <mapmem.h>
  9. #include <os.h>
  10. #include <spl.h>
  11. #include <test/ut.h>
  12. /* Declare a new SPL test */
  13. #define SPL_TEST(_name, _flags) UNIT_TEST(_name, _flags, spl_test)
  14. /* Context used for this test */
  15. struct text_ctx {
  16. int fd;
  17. };
  18. static ulong read_fit_image(struct spl_load_info *load, ulong sector,
  19. ulong count, void *buf)
  20. {
  21. struct text_ctx *text_ctx = load->priv;
  22. off_t offset, ret;
  23. ssize_t res;
  24. offset = sector * load->bl_len;
  25. ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
  26. if (ret != offset) {
  27. printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
  28. ret, errno);
  29. return 0;
  30. }
  31. res = os_read(text_ctx->fd, buf, count * load->bl_len);
  32. if (res == -1) {
  33. printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
  34. count * load->bl_len, res, errno);
  35. return 0;
  36. }
  37. return count;
  38. }
  39. int board_fit_config_name_match(const char *name)
  40. {
  41. return 0;
  42. }
  43. struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
  44. {
  45. return map_sysmem(0x100000, 0);
  46. }
  47. static int spl_test_load(struct unit_test_state *uts)
  48. {
  49. struct spl_image_info image;
  50. struct legacy_img_hdr *header;
  51. struct text_ctx text_ctx;
  52. struct spl_load_info load;
  53. char fname[256];
  54. int ret;
  55. int fd;
  56. memset(&load, '\0', sizeof(load));
  57. load.bl_len = 512;
  58. load.read = read_fit_image;
  59. ret = sandbox_find_next_phase(fname, sizeof(fname), true);
  60. if (ret) {
  61. printf("(%s not found, error %d)\n", fname, ret);
  62. return ret;
  63. }
  64. load.filename = fname;
  65. header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
  66. fd = os_open(fname, OS_O_RDONLY);
  67. ut_assert(fd >= 0);
  68. ut_asserteq(512, os_read(fd, header, 512));
  69. text_ctx.fd = fd;
  70. load.priv = &text_ctx;
  71. ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
  72. return 0;
  73. }
  74. SPL_TEST(spl_test_load, 0);