spl_fat.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * (C) Copyright 2014
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Dan Murphy <dmurphy@ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. * FAT Image Functions copied from spl_mmc.c
  10. */
  11. #include <common.h>
  12. #include <spl.h>
  13. #include <asm/u-boot.h>
  14. #include <fat.h>
  15. #include <errno.h>
  16. #include <image.h>
  17. #include <libfdt.h>
  18. static int fat_registered;
  19. static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
  20. {
  21. int err = 0;
  22. if (fat_registered)
  23. return err;
  24. err = fat_register_device(block_dev, partition);
  25. if (err) {
  26. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  27. printf("%s: fat register err - %d\n", __func__, err);
  28. #endif
  29. return err;
  30. }
  31. fat_registered = 1;
  32. return err;
  33. }
  34. static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
  35. ulong size, void *buf)
  36. {
  37. loff_t actread;
  38. int ret;
  39. char *filename = (char *)load->filename;
  40. ret = fat_read_file(filename, buf, file_offset, size, &actread);
  41. if (ret)
  42. return ret;
  43. return actread;
  44. }
  45. int spl_load_image_fat(struct spl_image_info *spl_image,
  46. struct blk_desc *block_dev, int partition,
  47. const char *filename)
  48. {
  49. int err;
  50. struct image_header *header;
  51. err = spl_register_fat_device(block_dev, partition);
  52. if (err)
  53. goto end;
  54. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  55. sizeof(struct image_header));
  56. err = file_fat_read(filename, header, sizeof(struct image_header));
  57. if (err <= 0)
  58. goto end;
  59. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  60. image_get_magic(header) == FDT_MAGIC) {
  61. struct spl_load_info load;
  62. debug("Found FIT\n");
  63. load.read = spl_fit_read;
  64. load.bl_len = 1;
  65. load.filename = (void *)filename;
  66. load.priv = NULL;
  67. return spl_load_simple_fit(spl_image, &load, 0, header);
  68. } else {
  69. err = spl_parse_image_header(spl_image, header);
  70. if (err)
  71. goto end;
  72. err = file_fat_read(filename,
  73. (u8 *)(uintptr_t)spl_image->load_addr, 0);
  74. }
  75. end:
  76. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  77. if (err <= 0)
  78. printf("%s: error reading image %s, err - %d\n",
  79. __func__, filename, err);
  80. #endif
  81. return (err <= 0);
  82. }
  83. #ifdef CONFIG_SPL_OS_BOOT
  84. int spl_load_image_fat_os(struct spl_image_info *spl_image,
  85. struct blk_desc *block_dev, int partition)
  86. {
  87. int err;
  88. __maybe_unused char *file;
  89. err = spl_register_fat_device(block_dev, partition);
  90. if (err)
  91. return err;
  92. #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
  93. file = getenv("falcon_args_file");
  94. if (file) {
  95. err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  96. if (err <= 0) {
  97. printf("spl: error reading image %s, err - %d, falling back to default\n",
  98. file, err);
  99. goto defaults;
  100. }
  101. file = getenv("falcon_image_file");
  102. if (file) {
  103. err = spl_load_image_fat(spl_image, block_dev,
  104. partition, file);
  105. if (err != 0) {
  106. puts("spl: falling back to default\n");
  107. goto defaults;
  108. }
  109. return 0;
  110. } else
  111. puts("spl: falcon_image_file not set in environment, falling back to default\n");
  112. } else
  113. puts("spl: falcon_args_file not set in environment, falling back to default\n");
  114. defaults:
  115. #endif
  116. err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
  117. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  118. if (err <= 0) {
  119. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  120. printf("%s: error reading image %s, err - %d\n",
  121. __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
  122. #endif
  123. return -1;
  124. }
  125. return spl_load_image_fat(spl_image, block_dev, partition,
  126. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  127. }
  128. #else
  129. int spl_load_image_fat_os(struct spl_image_info *spl_image,
  130. struct blk_desc *block_dev, int partition)
  131. {
  132. return -ENOSYS;
  133. }
  134. #endif