1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Intel Corporation <www.intel.com>
11 #include <fs_loader.h>
12 #include <linux/string.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 * struct firmware - A place for storing firmware and its attribute data.
22 * This holds information about a firmware and its content.
24 * @size: Size of a file
25 * @data: Buffer for file
26 * @priv: Firmware loader private fields
28 * @offset: Offset of reading a file
37 #ifdef CONFIG_CMD_UBIFS
38 static int mount_ubifs(char *mtdpart, char *ubivol)
40 int ret = ubi_part(mtdpart, NULL);
43 debug("Cannot find mtd partition %s\n", mtdpart);
47 return cmd_ubifs_mount(ubivol);
50 static int umount_ubifs(void)
52 return cmd_ubifs_umount();
55 static int mount_ubifs(char *mtdpart, char *ubivol)
57 debug("Error: Cannot load image: no UBIFS support\n");
62 static int select_fs_dev(struct device_platdata *plat)
66 if (plat->phandlepart.phandle) {
69 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
73 ret = device_get_global_by_ofnode(node, &dev);
75 struct blk_desc *desc = blk_get_by_device(dev);
77 ret = fs_set_blk_dev_with_part(desc,
78 plat->phandlepart.partition);
80 debug("%s: No device found\n", __func__);
84 } else if (plat->mtdpart && plat->ubivol) {
85 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
89 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
91 debug("Error: unsupported storage device.\n");
96 debug("Error: could not access storage.\n");
102 * _request_firmware_prepare - Prepare firmware struct.
104 * @dev: An instance of a driver.
105 * @name: Name of firmware file.
106 * @dbuf: Address of buffer to load firmware into.
107 * @size: Size of buffer.
108 * @offset: Offset of a file for start reading into buffer.
110 * Return: Negative value if fail, 0 for successful.
112 static int _request_firmware_prepare(struct udevice *dev,
113 const char *name, void *dbuf,
114 size_t size, u32 offset)
116 if (!name || name[0] == '\0')
119 struct firmware *firmwarep = dev_get_priv(dev);
124 firmwarep->name = name;
125 firmwarep->offset = offset;
126 firmwarep->data = dbuf;
127 firmwarep->size = size;
133 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
134 * @dev: An instance of a driver.
136 * Return: Size of total read, negative value when error.
138 static int fw_get_filesystem_firmware(struct udevice *dev)
141 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
144 storage_interface = env_get("storage_interface");
145 dev_part = env_get("fw_dev_part");
146 ubi_mtdpart = env_get("fw_ubi_mtdpart");
147 ubi_volume = env_get("fw_ubi_volume");
149 if (storage_interface && dev_part) {
150 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
151 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
152 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
156 if (!strcmp("ubi", storage_interface))
157 ret = fs_set_blk_dev(storage_interface, NULL,
162 ret = select_fs_dev(dev->platdata);
168 struct firmware *firmwarep = dev_get_priv(dev);
173 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
174 firmwarep->offset, firmwarep->size, &actread);
177 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
178 ret, firmwarep->name, actread, firmwarep->size);
184 #ifdef CONFIG_CMD_UBIFS
191 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
192 * @dev: An instance of a driver.
193 * @name: Name of firmware file.
194 * @buf: Address of buffer to load firmware into.
195 * @size: Size of buffer.
196 * @offset: Offset of a file for start reading into buffer.
198 * The firmware is loaded directly into the buffer pointed to by @buf.
200 * Return: Size of total read, negative value when error.
202 int request_firmware_into_buf(struct udevice *dev,
204 void *buf, size_t size, u32 offset)
211 ret = _request_firmware_prepare(dev, name, buf, size, offset);
212 if (ret < 0) /* error */
215 ret = fw_get_filesystem_firmware(dev);
220 static int fs_loader_ofdata_to_platdata(struct udevice *dev)
222 const char *fs_loader_path;
225 fs_loader_path = ofnode_get_chosen_prop("firmware-loader");
227 if (fs_loader_path) {
228 ofnode fs_loader_node;
230 fs_loader_node = ofnode_path(fs_loader_path);
231 if (ofnode_valid(fs_loader_node)) {
232 struct device_platdata *plat;
233 plat = dev->platdata;
235 if (!ofnode_read_u32_array(fs_loader_node,
238 plat->phandlepart.phandle = phandlepart[0];
239 plat->phandlepart.partition = phandlepart[1];
242 plat->mtdpart = (char *)ofnode_read_string(
243 fs_loader_node, "mtdpart");
245 plat->ubivol = (char *)ofnode_read_string(
246 fs_loader_node, "ubivol");
253 static int fs_loader_probe(struct udevice *dev)
258 static const struct udevice_id fs_loader_ids[] = {
259 { .compatible = "u-boot,fs-loader"},
263 U_BOOT_DRIVER(fs_loader) = {
265 .id = UCLASS_FS_FIRMWARE_LOADER,
266 .of_match = fs_loader_ids,
267 .probe = fs_loader_probe,
268 .ofdata_to_platdata = fs_loader_ofdata_to_platdata,
269 .platdata_auto_alloc_size = sizeof(struct device_platdata),
270 .priv_auto_alloc_size = sizeof(struct firmware),
273 UCLASS_DRIVER(fs_loader) = {
274 .id = UCLASS_FS_FIRMWARE_LOADER,