1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
7 #define LOG_CATEGORY UCLASS_FS_FIRMWARE_LOADER
15 #include <fs_loader.h>
17 #include <asm/global_data.h>
18 #include <linux/string.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 * struct firmware - A place for storing firmware and its attribute data.
28 * This holds information about a firmware and its content.
30 * @size: Size of a file
31 * @data: Buffer for file
32 * @priv: Firmware loader private fields
34 * @offset: Offset of reading a file
43 #ifdef CONFIG_CMD_UBIFS
44 static int mount_ubifs(char *mtdpart, char *ubivol)
46 int ret = ubi_part(mtdpart, NULL);
49 debug("Cannot find mtd partition %s\n", mtdpart);
53 return cmd_ubifs_mount(ubivol);
56 static int umount_ubifs(void)
58 return cmd_ubifs_umount();
61 static int mount_ubifs(char *mtdpart, char *ubivol)
63 debug("Error: Cannot load image: no UBIFS support\n");
68 static int select_fs_dev(struct device_plat *plat)
72 if (plat->phandlepart.phandle) {
75 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
79 ret = device_get_global_by_ofnode(node, &dev);
81 struct blk_desc *desc = blk_get_by_device(dev);
83 ret = fs_set_blk_dev_with_part(desc,
84 plat->phandlepart.partition);
86 debug("%s: No device found\n", __func__);
90 } else if (plat->mtdpart && plat->ubivol) {
91 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
95 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
97 debug("Error: unsupported storage device.\n");
102 debug("Error: could not access storage.\n");
108 * _request_firmware_prepare - Prepare firmware struct.
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.
116 * Return: Negative value if fail, 0 for successful.
118 static int _request_firmware_prepare(struct udevice *dev,
119 const char *name, void *dbuf,
120 size_t size, u32 offset)
122 if (!name || name[0] == '\0')
125 struct firmware *firmwarep = dev_get_priv(dev);
130 firmwarep->name = name;
131 firmwarep->offset = offset;
132 firmwarep->data = dbuf;
133 firmwarep->size = size;
139 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
140 * @dev: An instance of a driver.
142 * Return: Size of total read, negative value when error.
144 static int fw_get_filesystem_firmware(struct udevice *dev)
147 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
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");
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);
162 if (!strcmp("ubi", storage_interface))
163 ret = fs_set_blk_dev(storage_interface, NULL,
168 ret = select_fs_dev(dev_get_plat(dev));
174 struct firmware *firmwarep = dev_get_priv(dev);
179 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
180 firmwarep->offset, firmwarep->size, &actread);
183 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
184 ret, firmwarep->name, actread, firmwarep->size);
190 #ifdef CONFIG_CMD_UBIFS
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.
204 * The firmware is loaded directly into the buffer pointed to by @buf.
206 * Return: Size of total read, negative value when error.
208 int request_firmware_into_buf(struct udevice *dev,
210 void *buf, size_t size, u32 offset)
217 ret = _request_firmware_prepare(dev, name, buf, size, offset);
218 if (ret < 0) /* error */
221 ret = fw_get_filesystem_firmware(dev);
226 static int fs_loader_of_to_plat(struct udevice *dev)
230 ofnode fs_loader_node = dev_ofnode(dev);
232 if (ofnode_valid(fs_loader_node)) {
233 struct device_plat *plat;
235 plat = dev_get_plat(dev);
236 if (!ofnode_read_u32_array(fs_loader_node,
239 plat->phandlepart.phandle = phandlepart[0];
240 plat->phandlepart.partition = phandlepart[1];
243 plat->mtdpart = (char *)ofnode_read_string(
244 fs_loader_node, "mtdpart");
246 plat->ubivol = (char *)ofnode_read_string(
247 fs_loader_node, "ubivol");
253 static int fs_loader_probe(struct udevice *dev)
255 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
257 struct device_plat *plat = dev_get_plat(dev);
259 if (plat->phandlepart.phandle) {
260 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
261 struct udevice *parent_dev = NULL;
263 ret = device_get_global_by_ofnode(node, &parent_dev);
267 ret = blk_get_from_parent(parent_dev, &dev);
269 debug("fs_loader: No block device: %d\n",
281 static const struct udevice_id fs_loader_ids[] = {
282 { .compatible = "u-boot,fs-loader"},
286 U_BOOT_DRIVER(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),
296 UCLASS_DRIVER(fs_loader) = {
297 .id = UCLASS_FS_FIRMWARE_LOADER,