xref: /openbmc/u-boot/common/spl/spl_ext.c (revision 592cd5de)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2592f9222SGuillaume GARDET 
3592f9222SGuillaume GARDET #include <common.h>
4592f9222SGuillaume GARDET #include <spl.h>
5592f9222SGuillaume GARDET #include <asm/u-boot.h>
6592f9222SGuillaume GARDET #include <ext4fs.h>
7339245b7SNikita Kiryanov #include <errno.h>
8592f9222SGuillaume GARDET #include <image.h>
9592f9222SGuillaume GARDET 
spl_load_image_ext(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition,const char * filename)10b4a6c2aaSSimon Glass int spl_load_image_ext(struct spl_image_info *spl_image,
11b4a6c2aaSSimon Glass 		       struct blk_desc *block_dev, int partition,
12592f9222SGuillaume GARDET 		       const char *filename)
13592f9222SGuillaume GARDET {
14592f9222SGuillaume GARDET 	s32 err;
15592f9222SGuillaume GARDET 	struct image_header *header;
169f12cd0eSSuriyan Ramasami 	loff_t filelen, actlen;
17592f9222SGuillaume GARDET 	disk_partition_t part_info = {};
18592f9222SGuillaume GARDET 
19*04ce5427SMarek Vasut 	header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
20592f9222SGuillaume GARDET 
213e8bd469SSimon Glass 	if (part_get_info(block_dev, partition, &part_info)) {
22592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
23592f9222SGuillaume GARDET 		return -1;
24592f9222SGuillaume GARDET 	}
25592f9222SGuillaume GARDET 
26592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
27592f9222SGuillaume GARDET 
28592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
29592f9222SGuillaume GARDET 	if (!err) {
30592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
31592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
32592f9222SGuillaume GARDET #endif
33592f9222SGuillaume GARDET 		goto end;
34592f9222SGuillaume GARDET 	}
35592f9222SGuillaume GARDET 
369f12cd0eSSuriyan Ramasami 	err = ext4fs_open(filename, &filelen);
37592f9222SGuillaume GARDET 	if (err < 0) {
38592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
39592f9222SGuillaume GARDET 		goto end;
40592f9222SGuillaume GARDET 	}
4166a47ff2SStefan Brüns 	err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
42d3e488eaSGuillaume GARDET 	if (err < 0) {
43592f9222SGuillaume GARDET 		puts("spl: ext4fs_read failed\n");
44592f9222SGuillaume GARDET 		goto end;
45592f9222SGuillaume GARDET 	}
46592f9222SGuillaume GARDET 
47b4a6c2aaSSimon Glass 	err = spl_parse_image_header(spl_image, header);
487e0f2267SMarek Vasut 	if (err < 0) {
499ab165d8SPetr Kulhavy 		puts("spl: ext: failed to parse image header\n");
507e0f2267SMarek Vasut 		goto end;
517e0f2267SMarek Vasut 	}
52592f9222SGuillaume GARDET 
5366a47ff2SStefan Brüns 	err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
54592f9222SGuillaume GARDET 
55592f9222SGuillaume GARDET end:
56592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
57d3e488eaSGuillaume GARDET 	if (err < 0)
58592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
59592f9222SGuillaume GARDET 		       __func__, filename, err);
60592f9222SGuillaume GARDET #endif
61592f9222SGuillaume GARDET 
62d3e488eaSGuillaume GARDET 	return err < 0;
63592f9222SGuillaume GARDET }
64592f9222SGuillaume GARDET 
65592f9222SGuillaume GARDET #ifdef CONFIG_SPL_OS_BOOT
spl_load_image_ext_os(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition)66b4a6c2aaSSimon Glass int spl_load_image_ext_os(struct spl_image_info *spl_image,
67b4a6c2aaSSimon Glass 			  struct blk_desc *block_dev, int partition)
68592f9222SGuillaume GARDET {
69592f9222SGuillaume GARDET 	int err;
709f12cd0eSSuriyan Ramasami 	__maybe_unused loff_t filelen, actlen;
71592f9222SGuillaume GARDET 	disk_partition_t part_info = {};
72592f9222SGuillaume GARDET 	__maybe_unused char *file;
73592f9222SGuillaume GARDET 
743e8bd469SSimon Glass 	if (part_get_info(block_dev, partition, &part_info)) {
75592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
76592f9222SGuillaume GARDET 		return -1;
77592f9222SGuillaume GARDET 	}
78592f9222SGuillaume GARDET 
79592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
80592f9222SGuillaume GARDET 
81592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
82592f9222SGuillaume GARDET 	if (!err) {
83592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
84592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
85592f9222SGuillaume GARDET #endif
86592f9222SGuillaume GARDET 		return -1;
87592f9222SGuillaume GARDET 	}
8858c95d53SPetr Kulhavy #if defined(CONFIG_SPL_ENV_SUPPORT)
8900caae6dSSimon Glass 	file = env_get("falcon_args_file");
90592f9222SGuillaume GARDET 	if (file) {
919f12cd0eSSuriyan Ramasami 		err = ext4fs_open(file, &filelen);
92592f9222SGuillaume GARDET 		if (err < 0) {
93592f9222SGuillaume GARDET 			puts("spl: ext4fs_open failed\n");
94592f9222SGuillaume GARDET 			goto defaults;
95592f9222SGuillaume GARDET 		}
9666a47ff2SStefan Brüns 		err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
97d3e488eaSGuillaume GARDET 		if (err < 0) {
98592f9222SGuillaume GARDET 			printf("spl: error reading image %s, err - %d, falling back to default\n",
99592f9222SGuillaume GARDET 			       file, err);
100592f9222SGuillaume GARDET 			goto defaults;
101592f9222SGuillaume GARDET 		}
10200caae6dSSimon Glass 		file = env_get("falcon_image_file");
103592f9222SGuillaume GARDET 		if (file) {
104b4a6c2aaSSimon Glass 			err = spl_load_image_ext(spl_image, block_dev,
105b4a6c2aaSSimon Glass 						 partition, file);
106592f9222SGuillaume GARDET 			if (err != 0) {
107592f9222SGuillaume GARDET 				puts("spl: falling back to default\n");
108592f9222SGuillaume GARDET 				goto defaults;
109592f9222SGuillaume GARDET 			}
110592f9222SGuillaume GARDET 
111592f9222SGuillaume GARDET 			return 0;
112592f9222SGuillaume GARDET 		} else {
113592f9222SGuillaume GARDET 			puts("spl: falcon_image_file not set in environment, falling back to default\n");
114592f9222SGuillaume GARDET 		}
115592f9222SGuillaume GARDET 	} else {
116592f9222SGuillaume GARDET 		puts("spl: falcon_args_file not set in environment, falling back to default\n");
117592f9222SGuillaume GARDET 	}
118592f9222SGuillaume GARDET 
119592f9222SGuillaume GARDET defaults:
120592f9222SGuillaume GARDET #endif
121592f9222SGuillaume GARDET 
1229f12cd0eSSuriyan Ramasami 	err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
123592f9222SGuillaume GARDET 	if (err < 0)
124592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
125592f9222SGuillaume GARDET 
12666a47ff2SStefan Brüns 	err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
127d3e488eaSGuillaume GARDET 	if (err < 0) {
128592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
130592f9222SGuillaume GARDET 		       __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
131592f9222SGuillaume GARDET #endif
132592f9222SGuillaume GARDET 		return -1;
133592f9222SGuillaume GARDET 	}
134592f9222SGuillaume GARDET 
135b4a6c2aaSSimon Glass 	return spl_load_image_ext(spl_image, block_dev, partition,
136592f9222SGuillaume GARDET 			CONFIG_SPL_FS_LOAD_KERNEL_NAME);
137592f9222SGuillaume GARDET }
138339245b7SNikita Kiryanov #else
spl_load_image_ext_os(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition)139b4a6c2aaSSimon Glass int spl_load_image_ext_os(struct spl_image_info *spl_image,
140b4a6c2aaSSimon Glass 			  struct blk_desc *block_dev, int partition)
141339245b7SNikita Kiryanov {
142339245b7SNikita Kiryanov 	return -ENOSYS;
143339245b7SNikita Kiryanov }
144592f9222SGuillaume GARDET #endif
145