spl_fat.c 3.5 KB

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