1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
12 #include <fs_loader.h>
13 #include <linux/string.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * struct firmware - A place for storing firmware and its attribute data.
23 * This holds information about a firmware and its content.
25 * @size: Size of a file
26 * @data: Buffer for file
27 * @priv: Firmware loader private fields
29 * @offset: Offset of reading a file
38 #ifdef CONFIG_CMD_UBIFS
39 static int mount_ubifs(char *mtdpart, char *ubivol)
41 int ret = ubi_part(mtdpart, NULL);
44 debug("Cannot find mtd partition %s\n", mtdpart);
48 return cmd_ubifs_mount(ubivol);
51 static int umount_ubifs(void)
53 return cmd_ubifs_umount();
56 static int mount_ubifs(char *mtdpart, char *ubivol)
58 debug("Error: Cannot load image: no UBIFS support\n");
63 static int select_fs_dev(struct device_platdata *plat)
67 if (plat->phandlepart.phandle) {
70 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
74 ret = device_get_global_by_ofnode(node, &dev);
76 struct blk_desc *desc = blk_get_by_device(dev);
78 ret = fs_set_blk_dev_with_part(desc,
79 plat->phandlepart.partition);
81 debug("%s: No device found\n", __func__);
85 } else if (plat->mtdpart && plat->ubivol) {
86 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
90 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
92 debug("Error: unsupported storage device.\n");
97 debug("Error: could not access storage.\n");
103 * _request_firmware_prepare - Prepare firmware struct.
105 * @dev: An instance of a driver.
106 * @name: Name of firmware file.
107 * @dbuf: Address of buffer to load firmware into.
108 * @size: Size of buffer.
109 * @offset: Offset of a file for start reading into buffer.
111 * Return: Negative value if fail, 0 for successful.
113 static int _request_firmware_prepare(struct udevice *dev,
114 const char *name, void *dbuf,
115 size_t size, u32 offset)
117 if (!name || name[0] == '\0')
120 struct firmware *firmwarep = dev_get_priv(dev);
125 firmwarep->name = name;
126 firmwarep->offset = offset;
127 firmwarep->data = dbuf;
128 firmwarep->size = size;
134 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
135 * @dev: An instance of a driver.
137 * Return: Size of total read, negative value when error.
139 static int fw_get_filesystem_firmware(struct udevice *dev)
142 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
145 storage_interface = env_get("storage_interface");
146 dev_part = env_get("fw_dev_part");
147 ubi_mtdpart = env_get("fw_ubi_mtdpart");
148 ubi_volume = env_get("fw_ubi_volume");
150 if (storage_interface && dev_part) {
151 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
152 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
153 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
157 if (!strcmp("ubi", storage_interface))
158 ret = fs_set_blk_dev(storage_interface, NULL,
163 ret = select_fs_dev(dev->platdata);
169 struct firmware *firmwarep = dev_get_priv(dev);
174 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
175 firmwarep->offset, firmwarep->size, &actread);
178 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
179 ret, firmwarep->name, actread, firmwarep->size);
185 #ifdef CONFIG_CMD_UBIFS
192 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
193 * @dev: An instance of a driver.
194 * @name: Name of firmware file.
195 * @buf: Address of buffer to load firmware into.
196 * @size: Size of buffer.
197 * @offset: Offset of a file for start reading into buffer.
199 * The firmware is loaded directly into the buffer pointed to by @buf.
201 * Return: Size of total read, negative value when error.
203 int request_firmware_into_buf(struct udevice *dev,
205 void *buf, size_t size, u32 offset)
212 ret = _request_firmware_prepare(dev, name, buf, size, offset);
213 if (ret < 0) /* error */
216 ret = fw_get_filesystem_firmware(dev);
221 static int fs_loader_ofdata_to_platdata(struct udevice *dev)
225 ofnode fs_loader_node = dev_ofnode(dev);
227 if (ofnode_valid(fs_loader_node)) {
228 struct device_platdata *plat;
230 plat = dev->platdata;
231 if (!ofnode_read_u32_array(fs_loader_node,
234 plat->phandlepart.phandle = phandlepart[0];
235 plat->phandlepart.partition = phandlepart[1];
238 plat->mtdpart = (char *)ofnode_read_string(
239 fs_loader_node, "mtdpart");
241 plat->ubivol = (char *)ofnode_read_string(
242 fs_loader_node, "ubivol");
248 static int fs_loader_probe(struct udevice *dev)
250 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
252 struct device_platdata *plat = dev->platdata;
254 if (plat->phandlepart.phandle) {
255 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
256 struct udevice *parent_dev = NULL;
258 ret = device_get_global_by_ofnode(node, &parent_dev);
262 ret = blk_get_from_parent(parent_dev, &dev);
264 debug("fs_loader: No block device: %d\n",
276 static const struct udevice_id fs_loader_ids[] = {
277 { .compatible = "u-boot,fs-loader"},
281 U_BOOT_DRIVER(fs_loader) = {
283 .id = UCLASS_FS_FIRMWARE_LOADER,
284 .of_match = fs_loader_ids,
285 .probe = fs_loader_probe,
286 .ofdata_to_platdata = fs_loader_ofdata_to_platdata,
287 .platdata_auto_alloc_size = sizeof(struct device_platdata),
288 .priv_auto_alloc_size = sizeof(struct firmware),
291 UCLASS_DRIVER(fs_loader) = {
292 .id = UCLASS_FS_FIRMWARE_LOADER,