spl_ext.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0+
  3. */
  4. #include <common.h>
  5. #include <spl.h>
  6. #include <asm/u-boot.h>
  7. #include <ext4fs.h>
  8. #include <errno.h>
  9. #include <image.h>
  10. int spl_load_image_ext(struct spl_image_info *spl_image,
  11. struct blk_desc *block_dev, int partition,
  12. const char *filename)
  13. {
  14. s32 err;
  15. struct image_header *header;
  16. loff_t filelen, actlen;
  17. disk_partition_t part_info = {};
  18. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  19. sizeof(struct image_header));
  20. if (part_get_info(block_dev, partition, &part_info)) {
  21. printf("spl: no partition table found\n");
  22. return -1;
  23. }
  24. ext4fs_set_blk_dev(block_dev, &part_info);
  25. err = ext4fs_mount(0);
  26. if (!err) {
  27. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  28. printf("%s: ext4fs mount err - %d\n", __func__, err);
  29. #endif
  30. goto end;
  31. }
  32. err = ext4fs_open(filename, &filelen);
  33. if (err < 0) {
  34. puts("spl: ext4fs_open failed\n");
  35. goto end;
  36. }
  37. err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
  38. if (err < 0) {
  39. puts("spl: ext4fs_read failed\n");
  40. goto end;
  41. }
  42. err = spl_parse_image_header(spl_image, header);
  43. if (err < 0) {
  44. puts("spl: ext: failed to parse image header\n");
  45. goto end;
  46. }
  47. err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
  48. end:
  49. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  50. if (err < 0)
  51. printf("%s: error reading image %s, err - %d\n",
  52. __func__, filename, err);
  53. #endif
  54. return err < 0;
  55. }
  56. #ifdef CONFIG_SPL_OS_BOOT
  57. int spl_load_image_ext_os(struct spl_image_info *spl_image,
  58. struct blk_desc *block_dev, int partition)
  59. {
  60. int err;
  61. __maybe_unused loff_t filelen, actlen;
  62. disk_partition_t part_info = {};
  63. __maybe_unused char *file;
  64. if (part_get_info(block_dev, partition, &part_info)) {
  65. printf("spl: no partition table found\n");
  66. return -1;
  67. }
  68. ext4fs_set_blk_dev(block_dev, &part_info);
  69. err = ext4fs_mount(0);
  70. if (!err) {
  71. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  72. printf("%s: ext4fs mount err - %d\n", __func__, err);
  73. #endif
  74. return -1;
  75. }
  76. #if defined(CONFIG_SPL_ENV_SUPPORT)
  77. file = getenv("falcon_args_file");
  78. if (file) {
  79. err = ext4fs_open(file, &filelen);
  80. if (err < 0) {
  81. puts("spl: ext4fs_open failed\n");
  82. goto defaults;
  83. }
  84. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
  85. if (err < 0) {
  86. printf("spl: error reading image %s, err - %d, falling back to default\n",
  87. file, err);
  88. goto defaults;
  89. }
  90. file = getenv("falcon_image_file");
  91. if (file) {
  92. err = spl_load_image_ext(spl_image, block_dev,
  93. partition, file);
  94. if (err != 0) {
  95. puts("spl: falling back to default\n");
  96. goto defaults;
  97. }
  98. return 0;
  99. } else {
  100. puts("spl: falcon_image_file not set in environment, falling back to default\n");
  101. }
  102. } else {
  103. puts("spl: falcon_args_file not set in environment, falling back to default\n");
  104. }
  105. defaults:
  106. #endif
  107. err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
  108. if (err < 0)
  109. puts("spl: ext4fs_open failed\n");
  110. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
  111. if (err < 0) {
  112. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  113. printf("%s: error reading image %s, err - %d\n",
  114. __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
  115. #endif
  116. return -1;
  117. }
  118. return spl_load_image_ext(spl_image, block_dev, partition,
  119. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  120. }
  121. #else
  122. int spl_load_image_ext_os(struct spl_image_info *spl_image,
  123. struct blk_desc *block_dev, int partition)
  124. {
  125. return -ENOSYS;
  126. }
  127. #endif