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