xref: /rk3399_rockchip-uboot/common/spl/spl_ext.c (revision 339245b70d408bc648cb7b3dc2d5d1bb6d58e232)
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>
9*339245b7SNikita Kiryanov #include <errno.h>
10592f9222SGuillaume GARDET #include <image.h>
11592f9222SGuillaume GARDET 
12592f9222SGuillaume GARDET #ifdef CONFIG_SPL_EXT_SUPPORT
13592f9222SGuillaume GARDET int spl_load_image_ext(block_dev_desc_t *block_dev,
14592f9222SGuillaume GARDET 						int partition,
15592f9222SGuillaume GARDET 						const char *filename)
16592f9222SGuillaume GARDET {
17592f9222SGuillaume GARDET 	s32 err;
18592f9222SGuillaume GARDET 	struct image_header *header;
199f12cd0eSSuriyan Ramasami 	loff_t filelen, actlen;
20592f9222SGuillaume GARDET 	disk_partition_t part_info = {};
21592f9222SGuillaume GARDET 
22592f9222SGuillaume GARDET 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
23592f9222SGuillaume GARDET 						sizeof(struct image_header));
24592f9222SGuillaume GARDET 
25592f9222SGuillaume GARDET 	if (get_partition_info(block_dev,
26592f9222SGuillaume GARDET 			       partition, &part_info)) {
27592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
28592f9222SGuillaume GARDET 		return -1;
29592f9222SGuillaume GARDET 	}
30592f9222SGuillaume GARDET 
31592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
32592f9222SGuillaume GARDET 
33592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
34592f9222SGuillaume GARDET 	if (!err) {
35592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
36592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
37592f9222SGuillaume GARDET #endif
38592f9222SGuillaume GARDET 		goto end;
39592f9222SGuillaume GARDET 	}
40592f9222SGuillaume GARDET 
419f12cd0eSSuriyan Ramasami 	err = ext4fs_open(filename, &filelen);
42592f9222SGuillaume GARDET 	if (err < 0) {
43592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
44592f9222SGuillaume GARDET 		goto end;
45592f9222SGuillaume GARDET 	}
469f12cd0eSSuriyan Ramasami 	err = ext4fs_read((char *)header, sizeof(struct image_header), &actlen);
47d3e488eaSGuillaume GARDET 	if (err < 0) {
48592f9222SGuillaume GARDET 		puts("spl: ext4fs_read failed\n");
49592f9222SGuillaume GARDET 		goto end;
50592f9222SGuillaume GARDET 	}
51592f9222SGuillaume GARDET 
52592f9222SGuillaume GARDET 	spl_parse_image_header(header);
53592f9222SGuillaume GARDET 
549f12cd0eSSuriyan Ramasami 	err = ext4fs_read((char *)spl_image.load_addr, filelen, &actlen);
55592f9222SGuillaume GARDET 
56592f9222SGuillaume GARDET end:
57592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
58d3e488eaSGuillaume GARDET 	if (err < 0)
59592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
60592f9222SGuillaume GARDET 		       __func__, filename, err);
61592f9222SGuillaume GARDET #endif
62592f9222SGuillaume GARDET 
63d3e488eaSGuillaume GARDET 	return err < 0;
64592f9222SGuillaume GARDET }
65592f9222SGuillaume GARDET 
66592f9222SGuillaume GARDET #ifdef CONFIG_SPL_OS_BOOT
67592f9222SGuillaume GARDET int spl_load_image_ext_os(block_dev_desc_t *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 
74592f9222SGuillaume GARDET 	if (get_partition_info(block_dev,
75592f9222SGuillaume GARDET 			       partition, &part_info)) {
76592f9222SGuillaume GARDET 		printf("spl: no partition table found\n");
77592f9222SGuillaume GARDET 		return -1;
78592f9222SGuillaume GARDET 	}
79592f9222SGuillaume GARDET 
80592f9222SGuillaume GARDET 	ext4fs_set_blk_dev(block_dev, &part_info);
81592f9222SGuillaume GARDET 
82592f9222SGuillaume GARDET 	err = ext4fs_mount(0);
83592f9222SGuillaume GARDET 	if (!err) {
84592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85592f9222SGuillaume GARDET 		printf("%s: ext4fs mount err - %d\n", __func__, err);
86592f9222SGuillaume GARDET #endif
87592f9222SGuillaume GARDET 		return -1;
88592f9222SGuillaume GARDET 	}
89592f9222SGuillaume GARDET 
90592f9222SGuillaume GARDET #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
91592f9222SGuillaume GARDET 	file = getenv("falcon_args_file");
92592f9222SGuillaume GARDET 	if (file) {
939f12cd0eSSuriyan Ramasami 		err = ext4fs_open(file, &filelen);
94592f9222SGuillaume GARDET 		if (err < 0) {
95592f9222SGuillaume GARDET 			puts("spl: ext4fs_open failed\n");
96592f9222SGuillaume GARDET 			goto defaults;
97592f9222SGuillaume GARDET 		}
989f12cd0eSSuriyan Ramasami 		err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
99d3e488eaSGuillaume GARDET 		if (err < 0) {
100592f9222SGuillaume GARDET 			printf("spl: error reading image %s, err - %d, falling back to default\n",
101592f9222SGuillaume GARDET 			       file, err);
102592f9222SGuillaume GARDET 			goto defaults;
103592f9222SGuillaume GARDET 		}
104592f9222SGuillaume GARDET 		file = getenv("falcon_image_file");
105592f9222SGuillaume GARDET 		if (file) {
106592f9222SGuillaume GARDET 			err = spl_load_image_ext(block_dev, partition, file);
107592f9222SGuillaume GARDET 			if (err != 0) {
108592f9222SGuillaume GARDET 				puts("spl: falling back to default\n");
109592f9222SGuillaume GARDET 				goto defaults;
110592f9222SGuillaume GARDET 			}
111592f9222SGuillaume GARDET 
112592f9222SGuillaume GARDET 			return 0;
113592f9222SGuillaume GARDET 		} else {
114592f9222SGuillaume GARDET 			puts("spl: falcon_image_file not set in environment, falling back to default\n");
115592f9222SGuillaume GARDET 		}
116592f9222SGuillaume GARDET 	} else {
117592f9222SGuillaume GARDET 		puts("spl: falcon_args_file not set in environment, falling back to default\n");
118592f9222SGuillaume GARDET 	}
119592f9222SGuillaume GARDET 
120592f9222SGuillaume GARDET defaults:
121592f9222SGuillaume GARDET #endif
122592f9222SGuillaume GARDET 
1239f12cd0eSSuriyan Ramasami 	err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
124592f9222SGuillaume GARDET 	if (err < 0)
125592f9222SGuillaume GARDET 		puts("spl: ext4fs_open failed\n");
126592f9222SGuillaume GARDET 
1279f12cd0eSSuriyan Ramasami 	err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, filelen, &actlen);
128d3e488eaSGuillaume GARDET 	if (err < 0) {
129592f9222SGuillaume GARDET #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
130592f9222SGuillaume GARDET 		printf("%s: error reading image %s, err - %d\n",
131592f9222SGuillaume GARDET 		       __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
132592f9222SGuillaume GARDET #endif
133592f9222SGuillaume GARDET 		return -1;
134592f9222SGuillaume GARDET 	}
135592f9222SGuillaume GARDET 
136592f9222SGuillaume GARDET 	return spl_load_image_ext(block_dev, partition,
137592f9222SGuillaume GARDET 			CONFIG_SPL_FS_LOAD_KERNEL_NAME);
138592f9222SGuillaume GARDET }
139*339245b7SNikita Kiryanov #else
140*339245b7SNikita Kiryanov int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition)
141*339245b7SNikita Kiryanov {
142*339245b7SNikita Kiryanov 	return -ENOSYS;
143*339245b7SNikita Kiryanov }
144592f9222SGuillaume GARDET #endif
145592f9222SGuillaume GARDET #endif
146