spl_ext.c 3.4 KB

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