spl_ext.c 3.4 KB

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