spl_ext.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(block_dev_desc_t *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 (get_partition_info(block_dev,
  22. partition, &part_info)) {
  23. printf("spl: no partition table found\n");
  24. return -1;
  25. }
  26. ext4fs_set_blk_dev(block_dev, &part_info);
  27. err = ext4fs_mount(0);
  28. if (!err) {
  29. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  30. printf("%s: ext4fs mount err - %d\n", __func__, err);
  31. #endif
  32. goto end;
  33. }
  34. err = ext4fs_open(filename, &filelen);
  35. if (err < 0) {
  36. puts("spl: ext4fs_open failed\n");
  37. goto end;
  38. }
  39. err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen);
  40. if (err < 0) {
  41. puts("spl: ext4fs_read failed\n");
  42. goto end;
  43. }
  44. spl_parse_image_header(header);
  45. err = ext4fs_read((char *)spl_image.load_addr, 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(block_dev_desc_t *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 (get_partition_info(block_dev,
  62. 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) && defined(CONFIG_SPL_OS_BOOT)
  75. file = getenv("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, 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 = getenv("falcon_image_file");
  89. if (file) {
  90. err = spl_load_image_ext(block_dev, 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, 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(block_dev, partition,
  116. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  117. }
  118. #else
  119. int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition)
  120. {
  121. return -ENOSYS;
  122. }
  123. #endif
  124. #endif