spl_ext.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <image.h>
  9. #ifdef CONFIG_SPL_EXT_SUPPORT
  10. int spl_load_image_ext(block_dev_desc_t *block_dev,
  11. int partition,
  12. const char *filename)
  13. {
  14. s32 err;
  15. struct image_header *header;
  16. loff_t filelen, actlen;
  17. disk_partition_t part_info = {};
  18. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  19. sizeof(struct image_header));
  20. if (get_partition_info(block_dev,
  21. 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(block_dev_desc_t *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 (get_partition_info(block_dev,
  61. 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) && defined(CONFIG_SPL_OS_BOOT)
  74. file = getenv("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, 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 = getenv("falcon_image_file");
  88. if (file) {
  89. err = spl_load_image_ext(block_dev, partition, file);
  90. if (err != 0) {
  91. puts("spl: falling back to default\n");
  92. goto defaults;
  93. }
  94. return 0;
  95. } else {
  96. puts("spl: falcon_image_file not set in environment, falling back to default\n");
  97. }
  98. } else {
  99. puts("spl: falcon_args_file not set in environment, falling back to default\n");
  100. }
  101. defaults:
  102. #endif
  103. err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
  104. if (err < 0)
  105. puts("spl: ext4fs_open failed\n");
  106. err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
  107. if (err < 0) {
  108. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  109. printf("%s: error reading image %s, err - %d\n",
  110. __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
  111. #endif
  112. return -1;
  113. }
  114. return spl_load_image_ext(block_dev, partition,
  115. CONFIG_SPL_FS_LOAD_KERNEL_NAME);
  116. }
  117. #endif
  118. #endif