spl_ext.c 3.4 KB

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