spl_ext.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 blk_desc *block_dev,
  12. 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, sizeof(struct image_header), &actlen);
  39. if (err < 0) {
  40. puts("spl: ext4fs_read failed\n");
  41. goto end;
  42. }
  43. spl_parse_image_header(header);
  44. err = ext4fs_read((char *)spl_image.load_addr, 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 blk_desc *block_dev, int partition)
  55. {
  56. int err;
  57. __maybe_unused loff_t filelen, actlen;
  58. disk_partition_t part_info = {};
  59. __maybe_unused char *file;
  60. if (part_get_info(block_dev, partition, &part_info)) {
  61. printf("spl: no partition table found\n");
  62. return -1;
  63. }
  64. ext4fs_set_blk_dev(block_dev, &part_info);
  65. err = ext4fs_mount(0);
  66. if (!err) {
  67. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  68. printf("%s: ext4fs mount err - %d\n", __func__, err);
  69. #endif
  70. return -1;
  71. }
  72. #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
  73. file = getenv("falcon_args_file");
  74. if (file) {
  75. err = ext4fs_open(file, &filelen);
  76. if (err < 0) {
  77. puts("spl: ext4fs_open failed\n");
  78. goto defaults;
  79. }
  80. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
  81. if (err < 0) {
  82. printf("spl: error reading image %s, err - %d, falling back to default\n",
  83. file, err);
  84. goto defaults;
  85. }
  86. file = getenv("falcon_image_file");
  87. if (file) {
  88. err = spl_load_image_ext(block_dev, partition, file);
  89. if (err != 0) {
  90. puts("spl: falling back to default\n");
  91. goto defaults;
  92. }
  93. return 0;
  94. } else {
  95. puts("spl: falcon_image_file not set in environment, falling back to default\n");
  96. }
  97. } else {
  98. puts("spl: falcon_args_file not set in environment, falling back to default\n");
  99. }
  100. defaults:
  101. #endif
  102. err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
  103. if (err < 0)
  104. puts("spl: ext4fs_open failed\n");
  105. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
  106. if (err < 0) {
  107. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  108. printf("%s: error reading image %s, err - %d\n",
  109. __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
  110. #endif
  111. return -1;
  112. }
  113. return spl_load_image_ext(block_dev, partition,
  114. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  115. }
  116. #else
  117. int spl_load_image_ext_os(struct blk_desc *block_dev, int partition)
  118. {
  119. return -ENOSYS;
  120. }
  121. #endif
  122. #endif