xref: /rk3399_rockchip-uboot/common/spl/spl_ext.c (revision 00caae6d47645e68d6e5277aceb69592b49381a6)
1592f9222SGuillaume GARDET /*
2592f9222SGuillaume GARDET  * SPDX-License-Identifier:	GPL-2.0+
3592f9222SGuillaume GARDET  */
4592f9222SGuillaume GARDET 
5592f9222SGuillaume GARDET #include <common.h>
6592f9222SGuillaume GARDET #include <spl.h>
7592f9222SGuillaume GARDET #include <asm/u-boot.h>
8592f9222SGuillaume GARDET #include <ext4fs.h>
9339245b7SNikita Kiryanov #include <errno.h>
10592f9222SGuillaume GARDET #include <image.h>
11592f9222SGuillaume GARDET 
spl_load_image_ext(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition,const char * filename)12b4a6c2aaSSimon Glass int spl_load_image_ext(struct spl_image_info *spl_image,
13b4a6c2aaSSimon Glass 		       struct blk_desc *block_dev, int partition,
14592f9222SGuillaume GARDET 		       const char *filename)
15592f9222SGuillaume GARDET {
16592f9222SGuillaume GARDET 	s32 err;
17592f9222SGuillaume GARDET 	struct image_header *header;
189f12cd0eSSuriyan Ramasami 	loff_t filelen, actlen;
19592f9222SGuillaume GARDET 	disk_partition_t part_info = {};
20592f9222SGuillaume GARDET 
21592f9222SGuillaume GARDET 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
22592f9222SGuillaume GARDET 						sizeof(struct image_header));
23592f9222SGuillaume GARDET 
243e8bd469SSimon Glass 	if (part_get_info(block_dev, partition, &part_info)) {
25592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
26592f9222SGuillaume GARDET 		return -1;
27592f9222SGuillaume GARDET 	}
28592f9222SGuillaume GARDET 
29592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
30592f9222SGuillaume GARDET 
31592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
32592f9222SGuillaume GARDET 	if (!err) {
33592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
34592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
35592f9222SGuillaume GARDET #endif
36592f9222SGuillaume GARDET 		goto end;
37592f9222SGuillaume GARDET 	}
38592f9222SGuillaume GARDET 
399f12cd0eSSuriyan Ramasami 	err = ext4fs_open(filename, &filelen);
40592f9222SGuillaume GARDET 	if (err < 0) {
41592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
42592f9222SGuillaume GARDET 		goto end;
43592f9222SGuillaume GARDET 	}
4466a47ff2SStefan Brüns 	err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
45d3e488eaSGuillaume GARDET 	if (err < 0) {
46592f9222SGuillaume GARDET 		puts("spl: ext4fs_read failed\n");
47592f9222SGuillaume GARDET 		goto end;
48592f9222SGuillaume GARDET 	}
49592f9222SGuillaume GARDET 
50b4a6c2aaSSimon Glass 	err = spl_parse_image_header(spl_image, header);
517e0f2267SMarek Vasut 	if (err < 0) {
529ab165d8SPetr Kulhavy 		puts("spl: ext: failed to parse image header\n");
537e0f2267SMarek Vasut 		goto end;
547e0f2267SMarek Vasut 	}
55592f9222SGuillaume GARDET 
5666a47ff2SStefan Brüns 	err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
57592f9222SGuillaume GARDET 
58592f9222SGuillaume GARDET end:
59592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
60d3e488eaSGuillaume GARDET 	if (err < 0)
61592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
62592f9222SGuillaume GARDET 		       __func__, filename, err);
63592f9222SGuillaume GARDET #endif
64592f9222SGuillaume GARDET 
65d3e488eaSGuillaume GARDET 	return err < 0;
66592f9222SGuillaume GARDET }
67592f9222SGuillaume GARDET 
68592f9222SGuillaume GARDET #ifdef CONFIG_SPL_OS_BOOT
spl_load_image_ext_os(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition)69b4a6c2aaSSimon Glass int spl_load_image_ext_os(struct spl_image_info *spl_image,
70b4a6c2aaSSimon Glass 			  struct blk_desc *block_dev, int partition)
71592f9222SGuillaume GARDET {
72592f9222SGuillaume GARDET 	int err;
739f12cd0eSSuriyan Ramasami 	__maybe_unused loff_t filelen, actlen;
74592f9222SGuillaume GARDET 	disk_partition_t part_info = {};
75592f9222SGuillaume GARDET 	__maybe_unused char *file;
76592f9222SGuillaume GARDET 
773e8bd469SSimon Glass 	if (part_get_info(block_dev, partition, &part_info)) {
78592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
79592f9222SGuillaume GARDET 		return -1;
80592f9222SGuillaume GARDET 	}
81592f9222SGuillaume GARDET 
82592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
83592f9222SGuillaume GARDET 
84592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
85592f9222SGuillaume GARDET 	if (!err) {
86592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
87592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
88592f9222SGuillaume GARDET #endif
89592f9222SGuillaume GARDET 		return -1;
90592f9222SGuillaume GARDET 	}
9158c95d53SPetr Kulhavy #if defined(CONFIG_SPL_ENV_SUPPORT)
92*00caae6dSSimon Glass 	file = env_get("falcon_args_file");
93592f9222SGuillaume GARDET 	if (file) {
949f12cd0eSSuriyan Ramasami 		err = ext4fs_open(file, &filelen);
95592f9222SGuillaume GARDET 		if (err < 0) {
96592f9222SGuillaume GARDET 			puts("spl: ext4fs_open failed\n");
97592f9222SGuillaume GARDET 			goto defaults;
98592f9222SGuillaume GARDET 		}
9966a47ff2SStefan Brüns 		err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
100d3e488eaSGuillaume GARDET 		if (err < 0) {
101592f9222SGuillaume GARDET 			printf("spl: error reading image %s, err - %d, falling back to default\n",
102592f9222SGuillaume GARDET 			       file, err);
103592f9222SGuillaume GARDET 			goto defaults;
104592f9222SGuillaume GARDET 		}
105*00caae6dSSimon Glass 		file = env_get("falcon_image_file");
106592f9222SGuillaume GARDET 		if (file) {
107b4a6c2aaSSimon Glass 			err = spl_load_image_ext(spl_image, block_dev,
108b4a6c2aaSSimon Glass 						 partition, file);
109592f9222SGuillaume GARDET 			if (err != 0) {
110592f9222SGuillaume GARDET 				puts("spl: falling back to default\n");
111592f9222SGuillaume GARDET 				goto defaults;
112592f9222SGuillaume GARDET 			}
113592f9222SGuillaume GARDET 
114592f9222SGuillaume GARDET 			return 0;
115592f9222SGuillaume GARDET 		} else {
116592f9222SGuillaume GARDET 			puts("spl: falcon_image_file not set in environment, falling back to default\n");
117592f9222SGuillaume GARDET 		}
118592f9222SGuillaume GARDET 	} else {
119592f9222SGuillaume GARDET 		puts("spl: falcon_args_file not set in environment, falling back to default\n");
120592f9222SGuillaume GARDET 	}
121592f9222SGuillaume GARDET 
122592f9222SGuillaume GARDET defaults:
123592f9222SGuillaume GARDET #endif
124592f9222SGuillaume GARDET 
1259f12cd0eSSuriyan Ramasami 	err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
126592f9222SGuillaume GARDET 	if (err < 0)
127592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
128592f9222SGuillaume GARDET 
12966a47ff2SStefan Brüns 	err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
130d3e488eaSGuillaume GARDET 	if (err < 0) {
131592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
132592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
133592f9222SGuillaume GARDET 		       __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
134592f9222SGuillaume GARDET #endif
135592f9222SGuillaume GARDET 		return -1;
136592f9222SGuillaume GARDET 	}
137592f9222SGuillaume GARDET 
138b4a6c2aaSSimon Glass 	return spl_load_image_ext(spl_image, block_dev, partition,
139592f9222SGuillaume GARDET 			CONFIG_SPL_FS_LOAD_KERNEL_NAME);
140592f9222SGuillaume GARDET }
141339245b7SNikita Kiryanov #else
spl_load_image_ext_os(struct spl_image_info * spl_image,struct blk_desc * block_dev,int partition)142b4a6c2aaSSimon Glass int spl_load_image_ext_os(struct spl_image_info *spl_image,
143b4a6c2aaSSimon Glass 			  struct blk_desc *block_dev, int partition)
144339245b7SNikita Kiryanov {
145339245b7SNikita Kiryanov 	return -ENOSYS;
146339245b7SNikita Kiryanov }
147592f9222SGuillaume GARDET #endif
148