blob: e77b3af770e5bf490d82a8d3ff71b4e967817678 [file] [log] [blame]
Patrick Delaunay22929e12018-10-26 09:02:52 +02001// SPDX-License-Identifier: GPL-2.0
2 /*
Tien Fong Cheec1ef7362019-03-05 23:29:38 +08003 * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
Tien Fong Chee62030002018-07-06 16:28:03 +08004 *
Tien Fong Chee62030002018-07-06 16:28:03 +08005 */
6#include <common.h>
7#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -06008#include <env.h>
Tien Fong Chee62030002018-07-06 16:28:03 +08009#include <errno.h>
10#include <blk.h>
11#include <fs.h>
12#include <fs_loader.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Tien Fong Chee62030002018-07-06 16:28:03 +080015#include <linux/string.h>
16#include <mapmem.h>
17#include <malloc.h>
18#include <spl.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
Tien Fong Chee31a2cf12018-12-10 21:29:44 +080022/**
23 * struct firmware - A place for storing firmware and its attribute data.
24 *
25 * This holds information about a firmware and its content.
26 *
27 * @size: Size of a file
28 * @data: Buffer for file
29 * @priv: Firmware loader private fields
30 * @name: Filename
31 * @offset: Offset of reading a file
32 */
33struct firmware {
34 size_t size;
35 const u8 *data;
36 const char *name;
37 u32 offset;
Tien Fong Chee62030002018-07-06 16:28:03 +080038};
39
40#ifdef CONFIG_CMD_UBIFS
41static int mount_ubifs(char *mtdpart, char *ubivol)
42{
43 int ret = ubi_part(mtdpart, NULL);
44
45 if (ret) {
46 debug("Cannot find mtd partition %s\n", mtdpart);
47 return ret;
48 }
49
50 return cmd_ubifs_mount(ubivol);
51}
52
53static int umount_ubifs(void)
54{
55 return cmd_ubifs_umount();
56}
57#else
58static int mount_ubifs(char *mtdpart, char *ubivol)
59{
60 debug("Error: Cannot load image: no UBIFS support\n");
61 return -ENOSYS;
62}
63#endif
64
Simon Glass8a8d24b2020-12-03 16:55:23 -070065static int select_fs_dev(struct device_plat *plat)
Tien Fong Chee62030002018-07-06 16:28:03 +080066{
67 int ret;
68
69 if (plat->phandlepart.phandle) {
70 ofnode node;
71
72 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
73
Tien Fong Chee62030002018-07-06 16:28:03 +080074 struct udevice *dev;
75
Keerthy7c096ea2018-11-05 11:34:53 +053076 ret = device_get_global_by_ofnode(node, &dev);
Tien Fong Chee62030002018-07-06 16:28:03 +080077 if (!ret) {
78 struct blk_desc *desc = blk_get_by_device(dev);
79 if (desc) {
80 ret = fs_set_blk_dev_with_part(desc,
81 plat->phandlepart.partition);
82 } else {
83 debug("%s: No device found\n", __func__);
84 return -ENODEV;
85 }
86 }
87 } else if (plat->mtdpart && plat->ubivol) {
88 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
89 if (ret)
90 return ret;
91
92 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
93 } else {
94 debug("Error: unsupported storage device.\n");
95 return -ENODEV;
96 }
97
98 if (ret)
99 debug("Error: could not access storage.\n");
100
101 return ret;
102}
103
104/**
105 * _request_firmware_prepare - Prepare firmware struct.
106 *
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800107 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800108 * @name: Name of firmware file.
109 * @dbuf: Address of buffer to load firmware into.
110 * @size: Size of buffer.
111 * @offset: Offset of a file for start reading into buffer.
Tien Fong Chee62030002018-07-06 16:28:03 +0800112 *
113 * Return: Negative value if fail, 0 for successful.
114 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800115static int _request_firmware_prepare(struct udevice *dev,
116 const char *name, void *dbuf,
117 size_t size, u32 offset)
Tien Fong Chee62030002018-07-06 16:28:03 +0800118{
119 if (!name || name[0] == '\0')
120 return -EINVAL;
121
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800122 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800123
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800124 if (!firmwarep)
125 return -ENOMEM;
Tien Fong Chee62030002018-07-06 16:28:03 +0800126
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800127 firmwarep->name = name;
128 firmwarep->offset = offset;
129 firmwarep->data = dbuf;
130 firmwarep->size = size;
Tien Fong Chee62030002018-07-06 16:28:03 +0800131
132 return 0;
133}
134
135/**
Tien Fong Chee62030002018-07-06 16:28:03 +0800136 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800137 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800138 *
139 * Return: Size of total read, negative value when error.
140 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800141static int fw_get_filesystem_firmware(struct udevice *dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800142{
Tien Fong Chee62030002018-07-06 16:28:03 +0800143 loff_t actread;
144 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
145 int ret;
146
147 storage_interface = env_get("storage_interface");
148 dev_part = env_get("fw_dev_part");
149 ubi_mtdpart = env_get("fw_ubi_mtdpart");
150 ubi_volume = env_get("fw_ubi_volume");
151
152 if (storage_interface && dev_part) {
153 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
154 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
155 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
156 if (ret)
157 return ret;
158
159 if (!strcmp("ubi", storage_interface))
160 ret = fs_set_blk_dev(storage_interface, NULL,
161 FS_TYPE_UBIFS);
162 else
163 ret = -ENODEV;
164 } else {
Simon Glass0fd3d912020-12-22 19:30:28 -0700165 ret = select_fs_dev(dev_get_plat(dev));
Tien Fong Chee62030002018-07-06 16:28:03 +0800166 }
167
168 if (ret)
169 goto out;
170
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800171 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800172
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800173 if (!firmwarep)
174 return -ENOMEM;
175
176 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
177 firmwarep->offset, firmwarep->size, &actread);
Keerthy7c096ea2018-11-05 11:34:53 +0530178
Tien Fong Chee62030002018-07-06 16:28:03 +0800179 if (ret) {
Keerthy907837d2018-11-05 11:34:54 +0530180 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800181 ret, firmwarep->name, actread, firmwarep->size);
Tien Fong Chee62030002018-07-06 16:28:03 +0800182 } else {
183 ret = actread;
184 }
185
186out:
187#ifdef CONFIG_CMD_UBIFS
188 umount_ubifs();
189#endif
190 return ret;
191}
192
193/**
194 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800195 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800196 * @name: Name of firmware file.
197 * @buf: Address of buffer to load firmware into.
198 * @size: Size of buffer.
199 * @offset: Offset of a file for start reading into buffer.
Tien Fong Chee62030002018-07-06 16:28:03 +0800200 *
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800201 * The firmware is loaded directly into the buffer pointed to by @buf.
Tien Fong Chee62030002018-07-06 16:28:03 +0800202 *
203 * Return: Size of total read, negative value when error.
204 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800205int request_firmware_into_buf(struct udevice *dev,
Tien Fong Chee62030002018-07-06 16:28:03 +0800206 const char *name,
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800207 void *buf, size_t size, u32 offset)
Tien Fong Chee62030002018-07-06 16:28:03 +0800208{
209 int ret;
210
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800211 if (!dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800212 return -EINVAL;
213
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800214 ret = _request_firmware_prepare(dev, name, buf, size, offset);
Tien Fong Chee62030002018-07-06 16:28:03 +0800215 if (ret < 0) /* error */
216 return ret;
217
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800218 ret = fw_get_filesystem_firmware(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800219
220 return ret;
221}
222
Simon Glassd1998a92020-12-03 16:55:21 -0700223static int fs_loader_of_to_plat(struct udevice *dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800224{
Tien Fong Chee62030002018-07-06 16:28:03 +0800225 u32 phandlepart[2];
226
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800227 ofnode fs_loader_node = dev_ofnode(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800228
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800229 if (ofnode_valid(fs_loader_node)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700230 struct device_plat *plat;
Tien Fong Chee62030002018-07-06 16:28:03 +0800231
Simon Glass0fd3d912020-12-22 19:30:28 -0700232 plat = dev_get_plat(dev);
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800233 if (!ofnode_read_u32_array(fs_loader_node,
234 "phandlepart",
235 phandlepart, 2)) {
236 plat->phandlepart.phandle = phandlepart[0];
237 plat->phandlepart.partition = phandlepart[1];
Tien Fong Chee62030002018-07-06 16:28:03 +0800238 }
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800239
240 plat->mtdpart = (char *)ofnode_read_string(
241 fs_loader_node, "mtdpart");
242
243 plat->ubivol = (char *)ofnode_read_string(
244 fs_loader_node, "ubivol");
Tien Fong Chee62030002018-07-06 16:28:03 +0800245 }
246
247 return 0;
248}
249
250static int fs_loader_probe(struct udevice *dev)
251{
Tien Fong Cheedb32a442019-01-31 19:34:13 +0800252#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
253 int ret;
Simon Glass0fd3d912020-12-22 19:30:28 -0700254 struct device_plat *plat = dev_get_plat(dev);
Tien Fong Cheedb32a442019-01-31 19:34:13 +0800255
256 if (plat->phandlepart.phandle) {
257 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
258 struct udevice *parent_dev = NULL;
259
260 ret = device_get_global_by_ofnode(node, &parent_dev);
261 if (!ret) {
262 struct udevice *dev;
263
264 ret = blk_get_from_parent(parent_dev, &dev);
265 if (ret) {
266 debug("fs_loader: No block device: %d\n",
267 ret);
268
269 return ret;
270 }
271 }
272 }
273#endif
274
Tien Fong Chee62030002018-07-06 16:28:03 +0800275 return 0;
276};
277
278static const struct udevice_id fs_loader_ids[] = {
279 { .compatible = "u-boot,fs-loader"},
280 { }
281};
282
283U_BOOT_DRIVER(fs_loader) = {
284 .name = "fs-loader",
285 .id = UCLASS_FS_FIRMWARE_LOADER,
286 .of_match = fs_loader_ids,
287 .probe = fs_loader_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700288 .of_to_plat = fs_loader_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700289 .plat_auto = sizeof(struct device_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700290 .priv_auto = sizeof(struct firmware),
Tien Fong Chee62030002018-07-06 16:28:03 +0800291};
292
293UCLASS_DRIVER(fs_loader) = {
294 .id = UCLASS_FS_FIRMWARE_LOADER,
295 .name = "fs-loader",
296};