blob: 0139bd66ba7642ff4fc3f464bdc39aa803327163 [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 */
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006
7#define LOG_CATEGORY UCLASS_FS_FIRMWARE_LOADER
8
Tien Fong Chee62030002018-07-06 16:28:03 +08009#include <common.h>
10#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Tien Fong Chee62030002018-07-06 16:28:03 +080012#include <errno.h>
13#include <blk.h>
14#include <fs.h>
15#include <fs_loader.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Tien Fong Chee62030002018-07-06 16:28:03 +080018#include <linux/string.h>
19#include <mapmem.h>
20#include <malloc.h>
21#include <spl.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
Tien Fong Chee31a2cf12018-12-10 21:29:44 +080025/**
26 * struct firmware - A place for storing firmware and its attribute data.
27 *
28 * This holds information about a firmware and its content.
29 *
30 * @size: Size of a file
31 * @data: Buffer for file
32 * @priv: Firmware loader private fields
33 * @name: Filename
34 * @offset: Offset of reading a file
35 */
36struct firmware {
37 size_t size;
38 const u8 *data;
39 const char *name;
40 u32 offset;
Tien Fong Chee62030002018-07-06 16:28:03 +080041};
42
43#ifdef CONFIG_CMD_UBIFS
44static int mount_ubifs(char *mtdpart, char *ubivol)
45{
46 int ret = ubi_part(mtdpart, NULL);
47
48 if (ret) {
49 debug("Cannot find mtd partition %s\n", mtdpart);
50 return ret;
51 }
52
53 return cmd_ubifs_mount(ubivol);
54}
55
56static int umount_ubifs(void)
57{
58 return cmd_ubifs_umount();
59}
60#else
61static int mount_ubifs(char *mtdpart, char *ubivol)
62{
63 debug("Error: Cannot load image: no UBIFS support\n");
64 return -ENOSYS;
65}
66#endif
67
Simon Glass8a8d24b2020-12-03 16:55:23 -070068static int select_fs_dev(struct device_plat *plat)
Tien Fong Chee62030002018-07-06 16:28:03 +080069{
70 int ret;
71
72 if (plat->phandlepart.phandle) {
73 ofnode node;
74
75 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
76
Tien Fong Chee62030002018-07-06 16:28:03 +080077 struct udevice *dev;
78
Keerthy7c096ea2018-11-05 11:34:53 +053079 ret = device_get_global_by_ofnode(node, &dev);
Tien Fong Chee62030002018-07-06 16:28:03 +080080 if (!ret) {
81 struct blk_desc *desc = blk_get_by_device(dev);
82 if (desc) {
83 ret = fs_set_blk_dev_with_part(desc,
84 plat->phandlepart.partition);
85 } else {
86 debug("%s: No device found\n", __func__);
87 return -ENODEV;
88 }
89 }
90 } else if (plat->mtdpart && plat->ubivol) {
91 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
92 if (ret)
93 return ret;
94
95 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
96 } else {
97 debug("Error: unsupported storage device.\n");
98 return -ENODEV;
99 }
100
101 if (ret)
102 debug("Error: could not access storage.\n");
103
104 return ret;
105}
106
107/**
108 * _request_firmware_prepare - Prepare firmware struct.
109 *
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800110 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800111 * @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.
Tien Fong Chee62030002018-07-06 16:28:03 +0800115 *
116 * Return: Negative value if fail, 0 for successful.
117 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800118static int _request_firmware_prepare(struct udevice *dev,
119 const char *name, void *dbuf,
120 size_t size, u32 offset)
Tien Fong Chee62030002018-07-06 16:28:03 +0800121{
122 if (!name || name[0] == '\0')
123 return -EINVAL;
124
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800125 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800126
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800127 if (!firmwarep)
128 return -ENOMEM;
Tien Fong Chee62030002018-07-06 16:28:03 +0800129
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800130 firmwarep->name = name;
131 firmwarep->offset = offset;
132 firmwarep->data = dbuf;
133 firmwarep->size = size;
Tien Fong Chee62030002018-07-06 16:28:03 +0800134
135 return 0;
136}
137
138/**
Tien Fong Chee62030002018-07-06 16:28:03 +0800139 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800140 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800141 *
142 * Return: Size of total read, negative value when error.
143 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800144static int fw_get_filesystem_firmware(struct udevice *dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800145{
Tien Fong Chee62030002018-07-06 16:28:03 +0800146 loff_t actread;
147 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
148 int ret;
149
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");
154
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);
159 if (ret)
160 return ret;
161
162 if (!strcmp("ubi", storage_interface))
163 ret = fs_set_blk_dev(storage_interface, NULL,
164 FS_TYPE_UBIFS);
165 else
166 ret = -ENODEV;
167 } else {
Simon Glass0fd3d912020-12-22 19:30:28 -0700168 ret = select_fs_dev(dev_get_plat(dev));
Tien Fong Chee62030002018-07-06 16:28:03 +0800169 }
170
171 if (ret)
172 goto out;
173
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800174 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800175
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800176 if (!firmwarep)
177 return -ENOMEM;
178
179 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
180 firmwarep->offset, firmwarep->size, &actread);
Keerthy7c096ea2018-11-05 11:34:53 +0530181
Tien Fong Chee62030002018-07-06 16:28:03 +0800182 if (ret) {
Keerthy907837d2018-11-05 11:34:54 +0530183 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800184 ret, firmwarep->name, actread, firmwarep->size);
Tien Fong Chee62030002018-07-06 16:28:03 +0800185 } else {
186 ret = actread;
187 }
188
189out:
190#ifdef CONFIG_CMD_UBIFS
191 umount_ubifs();
192#endif
193 return ret;
194}
195
196/**
197 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800198 * @dev: An instance of a driver.
Tien Fong Chee62030002018-07-06 16:28:03 +0800199 * @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.
Tien Fong Chee62030002018-07-06 16:28:03 +0800203 *
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800204 * The firmware is loaded directly into the buffer pointed to by @buf.
Tien Fong Chee62030002018-07-06 16:28:03 +0800205 *
206 * Return: Size of total read, negative value when error.
207 */
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800208int request_firmware_into_buf(struct udevice *dev,
Tien Fong Chee62030002018-07-06 16:28:03 +0800209 const char *name,
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800210 void *buf, size_t size, u32 offset)
Tien Fong Chee62030002018-07-06 16:28:03 +0800211{
212 int ret;
213
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800214 if (!dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800215 return -EINVAL;
216
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800217 ret = _request_firmware_prepare(dev, name, buf, size, offset);
Tien Fong Chee62030002018-07-06 16:28:03 +0800218 if (ret < 0) /* error */
219 return ret;
220
Tien Fong Chee31a2cf12018-12-10 21:29:44 +0800221 ret = fw_get_filesystem_firmware(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800222
223 return ret;
224}
225
Simon Glassd1998a92020-12-03 16:55:21 -0700226static int fs_loader_of_to_plat(struct udevice *dev)
Tien Fong Chee62030002018-07-06 16:28:03 +0800227{
Tien Fong Chee62030002018-07-06 16:28:03 +0800228 u32 phandlepart[2];
229
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800230 ofnode fs_loader_node = dev_ofnode(dev);
Tien Fong Chee62030002018-07-06 16:28:03 +0800231
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800232 if (ofnode_valid(fs_loader_node)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700233 struct device_plat *plat;
Tien Fong Chee62030002018-07-06 16:28:03 +0800234
Simon Glass0fd3d912020-12-22 19:30:28 -0700235 plat = dev_get_plat(dev);
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800236 if (!ofnode_read_u32_array(fs_loader_node,
237 "phandlepart",
238 phandlepart, 2)) {
239 plat->phandlepart.phandle = phandlepart[0];
240 plat->phandlepart.partition = phandlepart[1];
Tien Fong Chee62030002018-07-06 16:28:03 +0800241 }
Tien Fong Cheec1ef7362019-03-05 23:29:38 +0800242
243 plat->mtdpart = (char *)ofnode_read_string(
244 fs_loader_node, "mtdpart");
245
246 plat->ubivol = (char *)ofnode_read_string(
247 fs_loader_node, "ubivol");
Tien Fong Chee62030002018-07-06 16:28:03 +0800248 }
249
250 return 0;
251}
252
253static int fs_loader_probe(struct udevice *dev)
254{
Tien Fong Cheedb32a442019-01-31 19:34:13 +0800255#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
256 int ret;
Simon Glass0fd3d912020-12-22 19:30:28 -0700257 struct device_plat *plat = dev_get_plat(dev);
Tien Fong Cheedb32a442019-01-31 19:34:13 +0800258
259 if (plat->phandlepart.phandle) {
260 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
261 struct udevice *parent_dev = NULL;
262
263 ret = device_get_global_by_ofnode(node, &parent_dev);
264 if (!ret) {
265 struct udevice *dev;
266
267 ret = blk_get_from_parent(parent_dev, &dev);
268 if (ret) {
269 debug("fs_loader: No block device: %d\n",
270 ret);
271
272 return ret;
273 }
274 }
275 }
276#endif
277
Tien Fong Chee62030002018-07-06 16:28:03 +0800278 return 0;
279};
280
281static const struct udevice_id fs_loader_ids[] = {
282 { .compatible = "u-boot,fs-loader"},
283 { }
284};
285
286U_BOOT_DRIVER(fs_loader) = {
287 .name = "fs-loader",
288 .id = UCLASS_FS_FIRMWARE_LOADER,
289 .of_match = fs_loader_ids,
290 .probe = fs_loader_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700291 .of_to_plat = fs_loader_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700292 .plat_auto = sizeof(struct device_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700293 .priv_auto = sizeof(struct firmware),
Tien Fong Chee62030002018-07-06 16:28:03 +0800294};
295
296UCLASS_DRIVER(fs_loader) = {
297 .id = UCLASS_FS_FIRMWARE_LOADER,
298 .name = "fs-loader",
299};