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