1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
12 #include <fs_loader.h>
14 #include <linux/string.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 * struct firmware - A place for storing firmware and its attribute data.
24 * This holds information about a firmware and its content.
26 * @size: Size of a file
27 * @data: Buffer for file
28 * @priv: Firmware loader private fields
30 * @offset: Offset of reading a file
39 #ifdef CONFIG_CMD_UBIFS
40 static int mount_ubifs(char *mtdpart, char *ubivol)
42 int ret = ubi_part(mtdpart, NULL);
45 debug("Cannot find mtd partition %s\n", mtdpart);
49 return cmd_ubifs_mount(ubivol);
52 static int umount_ubifs(void)
54 return cmd_ubifs_umount();
57 static int mount_ubifs(char *mtdpart, char *ubivol)
59 debug("Error: Cannot load image: no UBIFS support\n");
64 static int select_fs_dev(struct device_platdata *plat)
68 if (plat->phandlepart.phandle) {
71 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
75 ret = device_get_global_by_ofnode(node, &dev);
77 struct blk_desc *desc = blk_get_by_device(dev);
79 ret = fs_set_blk_dev_with_part(desc,
80 plat->phandlepart.partition);
82 debug("%s: No device found\n", __func__);
86 } else if (plat->mtdpart && plat->ubivol) {
87 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
91 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
93 debug("Error: unsupported storage device.\n");
98 debug("Error: could not access storage.\n");
104 * _request_firmware_prepare - Prepare firmware struct.
106 * @dev: An instance of a driver.
107 * @name: Name of firmware file.
108 * @dbuf: Address of buffer to load firmware into.
109 * @size: Size of buffer.
110 * @offset: Offset of a file for start reading into buffer.
112 * Return: Negative value if fail, 0 for successful.
114 static int _request_firmware_prepare(struct udevice *dev,
115 const char *name, void *dbuf,
116 size_t size, u32 offset)
118 if (!name || name[0] == '\0')
121 struct firmware *firmwarep = dev_get_priv(dev);
126 firmwarep->name = name;
127 firmwarep->offset = offset;
128 firmwarep->data = dbuf;
129 firmwarep->size = size;
135 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
136 * @dev: An instance of a driver.
138 * Return: Size of total read, negative value when error.
140 static int fw_get_filesystem_firmware(struct udevice *dev)
143 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
146 storage_interface = env_get("storage_interface");
147 dev_part = env_get("fw_dev_part");
148 ubi_mtdpart = env_get("fw_ubi_mtdpart");
149 ubi_volume = env_get("fw_ubi_volume");
151 if (storage_interface && dev_part) {
152 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
153 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
154 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
158 if (!strcmp("ubi", storage_interface))
159 ret = fs_set_blk_dev(storage_interface, NULL,
164 ret = select_fs_dev(dev->platdata);
170 struct firmware *firmwarep = dev_get_priv(dev);
175 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
176 firmwarep->offset, firmwarep->size, &actread);
179 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
180 ret, firmwarep->name, actread, firmwarep->size);
186 #ifdef CONFIG_CMD_UBIFS
193 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
194 * @dev: An instance of a driver.
195 * @name: Name of firmware file.
196 * @buf: Address of buffer to load firmware into.
197 * @size: Size of buffer.
198 * @offset: Offset of a file for start reading into buffer.
200 * The firmware is loaded directly into the buffer pointed to by @buf.
202 * Return: Size of total read, negative value when error.
204 int request_firmware_into_buf(struct udevice *dev,
206 void *buf, size_t size, u32 offset)
213 ret = _request_firmware_prepare(dev, name, buf, size, offset);
214 if (ret < 0) /* error */
217 ret = fw_get_filesystem_firmware(dev);
222 static int fs_loader_ofdata_to_platdata(struct udevice *dev)
226 ofnode fs_loader_node = dev_ofnode(dev);
228 if (ofnode_valid(fs_loader_node)) {
229 struct device_platdata *plat;
231 plat = dev->platdata;
232 if (!ofnode_read_u32_array(fs_loader_node,
235 plat->phandlepart.phandle = phandlepart[0];
236 plat->phandlepart.partition = phandlepart[1];
239 plat->mtdpart = (char *)ofnode_read_string(
240 fs_loader_node, "mtdpart");
242 plat->ubivol = (char *)ofnode_read_string(
243 fs_loader_node, "ubivol");
249 static int fs_loader_probe(struct udevice *dev)
251 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
253 struct device_platdata *plat = dev->platdata;
255 if (plat->phandlepart.phandle) {
256 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
257 struct udevice *parent_dev = NULL;
259 ret = device_get_global_by_ofnode(node, &parent_dev);
263 ret = blk_get_from_parent(parent_dev, &dev);
265 debug("fs_loader: No block device: %d\n",
277 static const struct udevice_id fs_loader_ids[] = {
278 { .compatible = "u-boot,fs-loader"},
282 U_BOOT_DRIVER(fs_loader) = {
284 .id = UCLASS_FS_FIRMWARE_LOADER,
285 .of_match = fs_loader_ids,
286 .probe = fs_loader_probe,
287 .ofdata_to_platdata = fs_loader_ofdata_to_platdata,
288 .platdata_auto_alloc_size = sizeof(struct device_platdata),
289 .priv_auto_alloc_size = sizeof(struct firmware),
292 UCLASS_DRIVER(fs_loader) = {
293 .id = UCLASS_FS_FIRMWARE_LOADER,