1 // SPDX-License-Identifier: GPL-2.0
2  /*
3  * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
4  *
5  */
6 
7 #define LOG_CATEGORY UCLASS_FS_FIRMWARE_LOADER
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <errno.h>
13 #include <blk.h>
14 #include <fs.h>
15 #include <fs_loader.h>
16 #include <log.h>
17 #include <asm/global_data.h>
18 #include <linux/string.h>
19 #include <mapmem.h>
20 #include <malloc.h>
21 #include <spl.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 /**
26  * struct firmware - A place for storing firmware and its attribute data.
27  *
28  * This holds information about a firmware and its content.
29  *
30  * @size: Size of a file
31  * @data: Buffer for file
32  * @priv: Firmware loader private fields
33  * @name: Filename
34  * @offset: Offset of reading a file
35  */
36 struct firmware {
37 	size_t size;
38 	const u8 *data;
39 	const char *name;
40 	u32 offset;
41 };
42 
43 #ifdef CONFIG_CMD_UBIFS
mount_ubifs(char * mtdpart,char * ubivol)44 static int mount_ubifs(char *mtdpart, char *ubivol)
45 {
46 	int ret = ubi_part(mtdpart, NULL);
47 
48 	if (ret) {
49 		debug("Cannot find mtd partition %s\n", mtdpart);
50 		return ret;
51 	}
52 
53 	return cmd_ubifs_mount(ubivol);
54 }
55 
umount_ubifs(void)56 static int umount_ubifs(void)
57 {
58 	return cmd_ubifs_umount();
59 }
60 #else
mount_ubifs(char * mtdpart,char * ubivol)61 static int mount_ubifs(char *mtdpart, char *ubivol)
62 {
63 	debug("Error: Cannot load image: no UBIFS support\n");
64 	return -ENOSYS;
65 }
66 #endif
67 
select_fs_dev(struct device_plat * plat)68 static int select_fs_dev(struct device_plat *plat)
69 {
70 	int ret;
71 
72 	if (plat->phandlepart.phandle) {
73 		ofnode node;
74 
75 		node = ofnode_get_by_phandle(plat->phandlepart.phandle);
76 
77 		struct udevice *dev;
78 
79 		ret = device_get_global_by_ofnode(node, &dev);
80 		if (!ret) {
81 			struct blk_desc *desc = blk_get_by_device(dev);
82 			if (desc) {
83 				ret = fs_set_blk_dev_with_part(desc,
84 					plat->phandlepart.partition);
85 			} else {
86 				debug("%s: No device found\n", __func__);
87 				return -ENODEV;
88 			}
89 		}
90 	} else if (plat->mtdpart && plat->ubivol) {
91 		ret = mount_ubifs(plat->mtdpart, plat->ubivol);
92 		if (ret)
93 			return ret;
94 
95 		ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
96 	} else {
97 		debug("Error: unsupported storage device.\n");
98 		return -ENODEV;
99 	}
100 
101 	if (ret)
102 		debug("Error: could not access storage.\n");
103 
104 	return ret;
105 }
106 
107 /**
108  * _request_firmware_prepare - Prepare firmware struct.
109  *
110  * @dev: An instance of a driver.
111  * @name: Name of firmware file.
112  * @dbuf: Address of buffer to load firmware into.
113  * @size: Size of buffer.
114  * @offset: Offset of a file for start reading into buffer.
115  *
116  * Return: Negative value if fail, 0 for successful.
117  */
_request_firmware_prepare(struct udevice * dev,const char * name,void * dbuf,size_t size,u32 offset)118 static int _request_firmware_prepare(struct udevice *dev,
119 				    const char *name, void *dbuf,
120 				    size_t size, u32 offset)
121 {
122 	if (!name || name[0] == '\0')
123 		return -EINVAL;
124 
125 	struct firmware *firmwarep = dev_get_priv(dev);
126 
127 	if (!firmwarep)
128 		return -ENOMEM;
129 
130 	firmwarep->name = name;
131 	firmwarep->offset = offset;
132 	firmwarep->data = dbuf;
133 	firmwarep->size = size;
134 
135 	return 0;
136 }
137 
138 /**
139  * fw_get_filesystem_firmware - load firmware into an allocated buffer.
140  * @dev: An instance of a driver.
141  *
142  * Return: Size of total read, negative value when error.
143  */
fw_get_filesystem_firmware(struct udevice * dev)144 static int fw_get_filesystem_firmware(struct udevice *dev)
145 {
146 	loff_t actread;
147 	char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
148 	int ret;
149 
150 	storage_interface = env_get("storage_interface");
151 	dev_part = env_get("fw_dev_part");
152 	ubi_mtdpart = env_get("fw_ubi_mtdpart");
153 	ubi_volume = env_get("fw_ubi_volume");
154 
155 	if (storage_interface && dev_part) {
156 		ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
157 	} else if (storage_interface && ubi_mtdpart && ubi_volume) {
158 		ret = mount_ubifs(ubi_mtdpart, ubi_volume);
159 		if (ret)
160 			return ret;
161 
162 		if (!strcmp("ubi", storage_interface))
163 			ret = fs_set_blk_dev(storage_interface, NULL,
164 				FS_TYPE_UBIFS);
165 		else
166 			ret = -ENODEV;
167 	} else {
168 		ret = select_fs_dev(dev_get_plat(dev));
169 	}
170 
171 	if (ret)
172 		goto out;
173 
174 	struct firmware *firmwarep = dev_get_priv(dev);
175 
176 	if (!firmwarep)
177 		return -ENOMEM;
178 
179 	ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
180 			firmwarep->offset, firmwarep->size, &actread);
181 
182 	if (ret) {
183 		debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
184 		      ret, firmwarep->name, actread, firmwarep->size);
185 	} else {
186 		ret = actread;
187 	}
188 
189 out:
190 #ifdef CONFIG_CMD_UBIFS
191 	umount_ubifs();
192 #endif
193 	return ret;
194 }
195 
196 /**
197  * request_firmware_into_buf - Load firmware into a previously allocated buffer.
198  * @dev: An instance of a driver.
199  * @name: Name of firmware file.
200  * @buf: Address of buffer to load firmware into.
201  * @size: Size of buffer.
202  * @offset: Offset of a file for start reading into buffer.
203  *
204  * The firmware is loaded directly into the buffer pointed to by @buf.
205  *
206  * Return: Size of total read, negative value when error.
207  */
request_firmware_into_buf(struct udevice * dev,const char * name,void * buf,size_t size,u32 offset)208 int request_firmware_into_buf(struct udevice *dev,
209 			      const char *name,
210 			      void *buf, size_t size, u32 offset)
211 {
212 	int ret;
213 
214 	if (!dev)
215 		return -EINVAL;
216 
217 	ret = _request_firmware_prepare(dev, name, buf, size, offset);
218 	if (ret < 0) /* error */
219 		return ret;
220 
221 	ret = fw_get_filesystem_firmware(dev);
222 
223 	return ret;
224 }
225 
fs_loader_of_to_plat(struct udevice * dev)226 static int fs_loader_of_to_plat(struct udevice *dev)
227 {
228 	u32 phandlepart[2];
229 
230 	ofnode fs_loader_node = dev_ofnode(dev);
231 
232 	if (ofnode_valid(fs_loader_node)) {
233 		struct device_plat *plat;
234 
235 		plat = dev_get_plat(dev);
236 		if (!ofnode_read_u32_array(fs_loader_node,
237 					  "phandlepart",
238 					  phandlepart, 2)) {
239 			plat->phandlepart.phandle = phandlepart[0];
240 			plat->phandlepart.partition = phandlepart[1];
241 		}
242 
243 		plat->mtdpart = (char *)ofnode_read_string(
244 				 fs_loader_node, "mtdpart");
245 
246 		plat->ubivol = (char *)ofnode_read_string(
247 				 fs_loader_node, "ubivol");
248 	}
249 
250 	return 0;
251 }
252 
fs_loader_probe(struct udevice * dev)253 static int fs_loader_probe(struct udevice *dev)
254 {
255 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
256 	int ret;
257 	struct device_plat *plat = dev_get_plat(dev);
258 
259 	if (plat->phandlepart.phandle) {
260 		ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
261 		struct udevice *parent_dev = NULL;
262 
263 		ret = device_get_global_by_ofnode(node, &parent_dev);
264 		if (!ret) {
265 			struct udevice *dev;
266 
267 			ret = blk_get_from_parent(parent_dev, &dev);
268 			if (ret) {
269 				debug("fs_loader: No block device: %d\n",
270 					ret);
271 
272 				return ret;
273 			}
274 		}
275 	}
276 #endif
277 
278 	return 0;
279 };
280 
281 static const struct udevice_id fs_loader_ids[] = {
282 	{ .compatible = "u-boot,fs-loader"},
283 	{ }
284 };
285 
286 U_BOOT_DRIVER(fs_loader) = {
287 	.name			= "fs-loader",
288 	.id			= UCLASS_FS_FIRMWARE_LOADER,
289 	.of_match		= fs_loader_ids,
290 	.probe			= fs_loader_probe,
291 	.of_to_plat	= fs_loader_of_to_plat,
292 	.plat_auto	= sizeof(struct device_plat),
293 	.priv_auto	= sizeof(struct firmware),
294 };
295 
296 UCLASS_DRIVER(fs_loader) = {
297 	.id		= UCLASS_FS_FIRMWARE_LOADER,
298 	.name		= "fs-loader",
299 };
300