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
14 #include <fs_loader.h>
16 #include <asm/global_data.h>
17 #include <dm/device-internal.h>
19 #include <linux/string.h>
24 #ifdef CONFIG_CMD_UBIFS
25 #include <ubi_uboot.h>
28 DECLARE_GLOBAL_DATA_PTR;
31 * struct firmware - A place for storing firmware and its attribute data.
33 * This holds information about a firmware and its content.
35 * @size: Size of a file
36 * @data: Buffer for file
37 * @priv: Firmware loader private fields
39 * @offset: Offset of reading a file
48 #ifdef CONFIG_CMD_UBIFS
49 static int mount_ubifs(char *mtdpart, char *ubivol)
51 int ret = ubi_part(mtdpart, NULL);
54 debug("Cannot find mtd partition %s\n", mtdpart);
58 return cmd_ubifs_mount(ubivol);
61 static int umount_ubifs(void)
63 return cmd_ubifs_umount();
66 static int mount_ubifs(char *mtdpart, char *ubivol)
68 debug("Error: Cannot load image: no UBIFS support\n");
73 static int select_fs_dev(struct device_plat *plat)
77 if (plat->phandlepart.phandle) {
80 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
84 ret = device_get_global_by_ofnode(node, &dev);
86 struct blk_desc *desc = blk_get_by_device(dev);
88 ret = fs_set_blk_dev_with_part(desc,
89 plat->phandlepart.partition);
91 debug("%s: No device found\n", __func__);
95 } else if (plat->mtdpart && plat->ubivol) {
96 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
100 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
102 debug("Error: unsupported storage device.\n");
107 debug("Error: could not access storage.\n");
113 * _request_firmware_prepare - Prepare firmware struct.
115 * @dev: An instance of a driver.
116 * @name: Name of firmware file.
117 * @dbuf: Address of buffer to load firmware into.
118 * @size: Size of buffer.
119 * @offset: Offset of a file for start reading into buffer.
121 * Return: Negative value if fail, 0 for successful.
123 static int _request_firmware_prepare(struct udevice *dev,
124 const char *name, void *dbuf,
125 size_t size, u32 offset)
127 if (!name || name[0] == '\0')
130 struct firmware *firmwarep = dev_get_priv(dev);
135 firmwarep->name = name;
136 firmwarep->offset = offset;
137 firmwarep->data = dbuf;
138 firmwarep->size = size;
144 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
145 * @dev: An instance of a driver.
147 * Return: Size of total read, negative value when error.
149 static int fw_get_filesystem_firmware(struct udevice *dev)
152 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
155 storage_interface = env_get("storage_interface");
156 dev_part = env_get("fw_dev_part");
157 ubi_mtdpart = env_get("fw_ubi_mtdpart");
158 ubi_volume = env_get("fw_ubi_volume");
160 if (storage_interface && dev_part) {
161 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
162 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
163 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
167 if (!strcmp("ubi", storage_interface))
168 ret = fs_set_blk_dev(storage_interface, NULL,
173 ret = select_fs_dev(dev_get_plat(dev));
179 struct firmware *firmwarep = dev_get_priv(dev);
184 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
185 firmwarep->offset, firmwarep->size, &actread);
188 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
189 ret, firmwarep->name, actread, firmwarep->size);
195 #ifdef CONFIG_CMD_UBIFS
202 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
203 * @dev: An instance of a driver.
204 * @name: Name of firmware file.
205 * @buf: Address of buffer to load firmware into.
206 * @size: Size of buffer.
207 * @offset: Offset of a file for start reading into buffer.
209 * The firmware is loaded directly into the buffer pointed to by @buf.
211 * Return: Size of total read, negative value when error.
213 int request_firmware_into_buf(struct udevice *dev,
215 void *buf, size_t size, u32 offset)
222 ret = _request_firmware_prepare(dev, name, buf, size, offset);
223 if (ret < 0) /* error */
226 ret = fw_get_filesystem_firmware(dev);
231 static int fs_loader_of_to_plat(struct udevice *dev)
235 ofnode fs_loader_node = dev_ofnode(dev);
237 if (ofnode_valid(fs_loader_node)) {
238 struct device_plat *plat;
240 plat = dev_get_plat(dev);
241 if (!ofnode_read_u32_array(fs_loader_node,
244 plat->phandlepart.phandle = phandlepart[0];
245 plat->phandlepart.partition = phandlepart[1];
248 plat->mtdpart = (char *)ofnode_read_string(
249 fs_loader_node, "mtdpart");
251 plat->ubivol = (char *)ofnode_read_string(
252 fs_loader_node, "ubivol");
258 static int fs_loader_probe(struct udevice *dev)
260 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
262 struct device_plat *plat = dev_get_plat(dev);
264 if (plat->phandlepart.phandle) {
265 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
266 struct udevice *parent_dev = NULL;
268 ret = device_get_global_by_ofnode(node, &parent_dev);
272 ret = blk_get_from_parent(parent_dev, &dev);
274 debug("fs_loader: No block device: %d\n",
286 static const struct udevice_id fs_loader_ids[] = {
287 { .compatible = "u-boot,fs-loader"},
291 U_BOOT_DRIVER(fs_loader) = {
293 .id = UCLASS_FS_FIRMWARE_LOADER,
294 .of_match = fs_loader_ids,
295 .probe = fs_loader_probe,
296 .of_to_plat = fs_loader_of_to_plat,
297 .plat_auto = sizeof(struct device_plat),
298 .priv_auto = sizeof(struct firmware),
301 static struct device_plat default_plat = { 0 };
303 int get_fs_loader(struct udevice **dev)
306 ofnode node = ofnode_get_chosen_node("firmware-loader");
308 if (ofnode_valid(node))
309 return uclass_get_device_by_ofnode(UCLASS_FS_FIRMWARE_LOADER,
312 /* Try the first device if none was chosen */
313 ret = uclass_first_device_err(UCLASS_FS_FIRMWARE_LOADER, dev);
317 /* Just create a new device */
318 ret = device_bind(dm_root(), DM_DRIVER_REF(fs_loader), "default-loader",
319 &default_plat, ofnode_null(), dev);
323 return device_probe(*dev);
326 UCLASS_DRIVER(fs_loader) = {
327 .id = UCLASS_FS_FIRMWARE_LOADER,