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