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;
19 struct firmware_priv {
20 const char *name; /* Filename */
21 u32 offset; /* Offset of reading a file */
24 #ifdef CONFIG_CMD_UBIFS
25 static int mount_ubifs(char *mtdpart, char *ubivol)
27 int ret = ubi_part(mtdpart, NULL);
30 debug("Cannot find mtd partition %s\n", mtdpart);
34 return cmd_ubifs_mount(ubivol);
37 static int umount_ubifs(void)
39 return cmd_ubifs_umount();
42 static int mount_ubifs(char *mtdpart, char *ubivol)
44 debug("Error: Cannot load image: no UBIFS support\n");
49 static int select_fs_dev(struct device_platdata *plat)
53 if (plat->phandlepart.phandle) {
56 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
60 ret = device_get_global_by_ofnode(node, &dev);
62 struct blk_desc *desc = blk_get_by_device(dev);
64 ret = fs_set_blk_dev_with_part(desc,
65 plat->phandlepart.partition);
67 debug("%s: No device found\n", __func__);
71 } else if (plat->mtdpart && plat->ubivol) {
72 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
76 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
78 debug("Error: unsupported storage device.\n");
83 debug("Error: could not access storage.\n");
89 * _request_firmware_prepare - Prepare firmware struct.
91 * @name: Name of firmware file.
92 * @dbuf: Address of buffer to load firmware into.
93 * @size: Size of buffer.
94 * @offset: Offset of a file for start reading into buffer.
95 * @firmwarep: Pointer to pointer to firmware image.
97 * Return: Negative value if fail, 0 for successful.
99 static int _request_firmware_prepare(const char *name, void *dbuf,
100 size_t size, u32 offset,
101 struct firmware **firmwarep)
103 if (!name || name[0] == '\0')
106 /* No memory allocation is required if *firmwarep is allocated */
108 (*firmwarep) = calloc(1, sizeof(struct firmware));
112 (*firmwarep)->priv = calloc(1, sizeof(struct firmware_priv));
113 if (!(*firmwarep)->priv) {
117 } else if (!(*firmwarep)->priv) {
118 (*firmwarep)->priv = calloc(1, sizeof(struct firmware_priv));
119 if (!(*firmwarep)->priv) {
125 ((struct firmware_priv *)((*firmwarep)->priv))->name = name;
126 ((struct firmware_priv *)((*firmwarep)->priv))->offset = offset;
127 (*firmwarep)->data = dbuf;
128 (*firmwarep)->size = size;
134 * release_firmware - Release the resource associated with a firmware image
135 * @firmware: Firmware resource to release
137 void release_firmware(struct firmware *firmware)
140 if (firmware->priv) {
141 free(firmware->priv);
142 firmware->priv = NULL;
149 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
150 * @plat: Platform data such as storage and partition firmware loading from.
151 * @firmware: pointer to firmware image.
153 * Return: Size of total read, negative value when error.
155 static int fw_get_filesystem_firmware(struct device_platdata *plat,
156 struct firmware *firmware)
158 struct firmware_priv *fw_priv = NULL;
160 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
163 storage_interface = env_get("storage_interface");
164 dev_part = env_get("fw_dev_part");
165 ubi_mtdpart = env_get("fw_ubi_mtdpart");
166 ubi_volume = env_get("fw_ubi_volume");
168 if (storage_interface && dev_part) {
169 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
170 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
171 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
175 if (!strcmp("ubi", storage_interface))
176 ret = fs_set_blk_dev(storage_interface, NULL,
181 ret = select_fs_dev(plat);
187 fw_priv = firmware->priv;
189 ret = fs_read(fw_priv->name, (ulong)map_to_sysmem(firmware->data),
190 fw_priv->offset, firmware->size, &actread);
193 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
194 ret, fw_priv->name, actread, firmware->size);
200 #ifdef CONFIG_CMD_UBIFS
207 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
208 * @plat: Platform data such as storage and partition firmware loading from.
209 * @name: Name of firmware file.
210 * @buf: Address of buffer to load firmware into.
211 * @size: Size of buffer.
212 * @offset: Offset of a file for start reading into buffer.
213 * @firmwarep: Pointer to firmware image.
215 * The firmware is loaded directly into the buffer pointed to by @buf and
216 * the @firmwarep data member is pointed at @buf.
218 * Return: Size of total read, negative value when error.
220 int request_firmware_into_buf(struct device_platdata *plat,
222 void *buf, size_t size, u32 offset,
223 struct firmware **firmwarep)
230 ret = _request_firmware_prepare(name, buf, size, offset, firmwarep);
231 if (ret < 0) /* error */
234 ret = fw_get_filesystem_firmware(plat, *firmwarep);
239 static int fs_loader_ofdata_to_platdata(struct udevice *dev)
241 const char *fs_loader_path;
244 fs_loader_path = ofnode_get_chosen_prop("firmware-loader");
246 if (fs_loader_path) {
247 ofnode fs_loader_node;
249 fs_loader_node = ofnode_path(fs_loader_path);
250 if (ofnode_valid(fs_loader_node)) {
251 struct device_platdata *plat;
252 plat = dev->platdata;
254 if (!ofnode_read_u32_array(fs_loader_node,
257 plat->phandlepart.phandle = phandlepart[0];
258 plat->phandlepart.partition = phandlepart[1];
261 plat->mtdpart = (char *)ofnode_read_string(
262 fs_loader_node, "mtdpart");
264 plat->ubivol = (char *)ofnode_read_string(
265 fs_loader_node, "ubivol");
272 static int fs_loader_probe(struct udevice *dev)
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),
291 UCLASS_DRIVER(fs_loader) = {
292 .id = UCLASS_FS_FIRMWARE_LOADER,