spl_ext.c 3.4 KB

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