spl_ext.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. 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, 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 blk_desc *block_dev, int partition)
  59. {
  60. int err;
  61. __maybe_unused loff_t filelen, actlen;
  62. disk_partition_t part_info = {};
  63. __maybe_unused char *file;
  64. if (part_get_info(block_dev, partition, &part_info)) {
  65. printf("spl: no partition table found\n");
  66. return -1;
  67. }
  68. ext4fs_set_blk_dev(block_dev, &part_info);
  69. err = ext4fs_mount(0);
  70. if (!err) {
  71. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  72. printf("%s: ext4fs mount err - %d\n", __func__, err);
  73. #endif
  74. return -1;
  75. }
  76. #if defined(CONFIG_SPL_ENV_SUPPORT)
  77. file = getenv("falcon_args_file");
  78. if (file) {
  79. err = ext4fs_open(file, &filelen);
  80. if (err < 0) {
  81. puts("spl: ext4fs_open failed\n");
  82. goto defaults;
  83. }
  84. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
  85. if (err < 0) {
  86. printf("spl: error reading image %s, err - %d, falling back to default\n",
  87. file, err);
  88. goto defaults;
  89. }
  90. file = getenv("falcon_image_file");
  91. if (file) {
  92. err = spl_load_image_ext(block_dev, 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, 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(block_dev, partition,
  118. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  119. }
  120. #else
  121. int spl_load_image_ext_os(struct blk_desc *block_dev, int partition)
  122. {
  123. return -ENOSYS;
  124. }
  125. #endif
  126. #endif