blob: 5ed8ab6530f504826f47b6fd7df2125e56fa9579 [file] [log] [blame]
Patrick Delaunayd6e53c72018-10-26 09:02:52 +02001// SPDX-License-Identifier: GPL-2.0
2 /*
Tien Fong Chee789aa9c2019-03-05 23:29:38 +08003 * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
Tien Fong Chee5ca878b2018-07-06 16:28:03 +08004 *
Tien Fong Chee5ca878b2018-07-06 16:28:03 +08005 */
6#include <common.h>
7#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06008#include <env.h>
Tien Fong Chee5ca878b2018-07-06 16:28:03 +08009#include <errno.h>
10#include <blk.h>
11#include <fs.h>
12#include <fs_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Tien Fong Chee5ca878b2018-07-06 16:28:03 +080014#include <linux/string.h>
15#include <mapmem.h>
16#include <malloc.h>
17#include <spl.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Tien Fong Cheee8b79702018-12-10 21:29:44 +080021/**
22 * struct firmware - A place for storing firmware and its attribute data.
23 *
24 * This holds information about a firmware and its content.
25 *
26 * @size: Size of a file
27 * @data: Buffer for file
28 * @priv: Firmware loader private fields
29 * @name: Filename
30 * @offset: Offset of reading a file
31 */
32struct firmware {
33 size_t size;
34 const u8 *data;
35 const char *name;
36 u32 offset;
Tien Fong Chee5ca878b2018-07-06 16:28:03 +080037};
38
39#ifdef CONFIG_CMD_UBIFS
40static int mount_ubifs(char *mtdpart, char *ubivol)
41{
42 int ret = ubi_part(mtdpart, NULL);
43
44 if (ret) {
45 debug("Cannot find mtd partition %s\n", mtdpart);
46 return ret;
47 }
48
49 return cmd_ubifs_mount(ubivol);
50}
51
52static int umount_ubifs(void)
53{
54 return cmd_ubifs_umount();
55}
56#else
57static int mount_ubifs(char *mtdpart, char *ubivol)
58{
59 debug("Error: Cannot load image: no UBIFS support\n");
60 return -ENOSYS;
61}
62#endif
63
Simon Glassb75b15b2020-12-03 16:55:23 -070064static int select_fs_dev(struct device_plat *plat)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +080065{
66 int ret;
67
68 if (plat->phandlepart.phandle) {
69 ofnode node;
70
71 node = ofnode_get_by_phandle(plat->phandlepart.phandle);
72
Tien Fong Chee5ca878b2018-07-06 16:28:03 +080073 struct udevice *dev;
74
Keerthyb5cdbd82018-11-05 11:34:53 +053075 ret = device_get_global_by_ofnode(node, &dev);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +080076 if (!ret) {
77 struct blk_desc *desc = blk_get_by_device(dev);
78 if (desc) {
79 ret = fs_set_blk_dev_with_part(desc,
80 plat->phandlepart.partition);
81 } else {
82 debug("%s: No device found\n", __func__);
83 return -ENODEV;
84 }
85 }
86 } else if (plat->mtdpart && plat->ubivol) {
87 ret = mount_ubifs(plat->mtdpart, plat->ubivol);
88 if (ret)
89 return ret;
90
91 ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
92 } else {
93 debug("Error: unsupported storage device.\n");
94 return -ENODEV;
95 }
96
97 if (ret)
98 debug("Error: could not access storage.\n");
99
100 return ret;
101}
102
103/**
104 * _request_firmware_prepare - Prepare firmware struct.
105 *
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800106 * @dev: An instance of a driver.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800107 * @name: Name of firmware file.
108 * @dbuf: Address of buffer to load firmware into.
109 * @size: Size of buffer.
110 * @offset: Offset of a file for start reading into buffer.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800111 *
112 * Return: Negative value if fail, 0 for successful.
113 */
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800114static int _request_firmware_prepare(struct udevice *dev,
115 const char *name, void *dbuf,
116 size_t size, u32 offset)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800117{
118 if (!name || name[0] == '\0')
119 return -EINVAL;
120
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800121 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800122
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800123 if (!firmwarep)
124 return -ENOMEM;
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800125
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800126 firmwarep->name = name;
127 firmwarep->offset = offset;
128 firmwarep->data = dbuf;
129 firmwarep->size = size;
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800130
131 return 0;
132}
133
134/**
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800135 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800136 * @dev: An instance of a driver.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800137 *
138 * Return: Size of total read, negative value when error.
139 */
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800140static int fw_get_filesystem_firmware(struct udevice *dev)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800141{
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800142 loff_t actread;
143 char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
144 int ret;
145
146 storage_interface = env_get("storage_interface");
147 dev_part = env_get("fw_dev_part");
148 ubi_mtdpart = env_get("fw_ubi_mtdpart");
149 ubi_volume = env_get("fw_ubi_volume");
150
151 if (storage_interface && dev_part) {
152 ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
153 } else if (storage_interface && ubi_mtdpart && ubi_volume) {
154 ret = mount_ubifs(ubi_mtdpart, ubi_volume);
155 if (ret)
156 return ret;
157
158 if (!strcmp("ubi", storage_interface))
159 ret = fs_set_blk_dev(storage_interface, NULL,
160 FS_TYPE_UBIFS);
161 else
162 ret = -ENODEV;
163 } else {
Simon Glass71fa5b42020-12-03 16:55:18 -0700164 ret = select_fs_dev(dev->plat);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800165 }
166
167 if (ret)
168 goto out;
169
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800170 struct firmware *firmwarep = dev_get_priv(dev);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800171
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800172 if (!firmwarep)
173 return -ENOMEM;
174
175 ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
176 firmwarep->offset, firmwarep->size, &actread);
Keerthyb5cdbd82018-11-05 11:34:53 +0530177
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800178 if (ret) {
Keerthy0bc20852018-11-05 11:34:54 +0530179 debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800180 ret, firmwarep->name, actread, firmwarep->size);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800181 } else {
182 ret = actread;
183 }
184
185out:
186#ifdef CONFIG_CMD_UBIFS
187 umount_ubifs();
188#endif
189 return ret;
190}
191
192/**
193 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800194 * @dev: An instance of a driver.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800195 * @name: Name of firmware file.
196 * @buf: Address of buffer to load firmware into.
197 * @size: Size of buffer.
198 * @offset: Offset of a file for start reading into buffer.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800199 *
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800200 * The firmware is loaded directly into the buffer pointed to by @buf.
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800201 *
202 * Return: Size of total read, negative value when error.
203 */
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800204int request_firmware_into_buf(struct udevice *dev,
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800205 const char *name,
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800206 void *buf, size_t size, u32 offset)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800207{
208 int ret;
209
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800210 if (!dev)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800211 return -EINVAL;
212
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800213 ret = _request_firmware_prepare(dev, name, buf, size, offset);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800214 if (ret < 0) /* error */
215 return ret;
216
Tien Fong Cheee8b79702018-12-10 21:29:44 +0800217 ret = fw_get_filesystem_firmware(dev);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800218
219 return ret;
220}
221
Simon Glassaad29ae2020-12-03 16:55:21 -0700222static int fs_loader_of_to_plat(struct udevice *dev)
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800223{
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800224 u32 phandlepart[2];
225
Tien Fong Chee789aa9c2019-03-05 23:29:38 +0800226 ofnode fs_loader_node = dev_ofnode(dev);
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800227
Tien Fong Chee789aa9c2019-03-05 23:29:38 +0800228 if (ofnode_valid(fs_loader_node)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700229 struct device_plat *plat;
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800230
Simon Glass71fa5b42020-12-03 16:55:18 -0700231 plat = dev->plat;
Tien Fong Chee789aa9c2019-03-05 23:29:38 +0800232 if (!ofnode_read_u32_array(fs_loader_node,
233 "phandlepart",
234 phandlepart, 2)) {
235 plat->phandlepart.phandle = phandlepart[0];
236 plat->phandlepart.partition = phandlepart[1];
237 }
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800238
Tien Fong Chee789aa9c2019-03-05 23:29:38 +0800239 plat->mtdpart = (char *)ofnode_read_string(
240 fs_loader_node, "mtdpart");
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800241
Tien Fong Chee789aa9c2019-03-05 23:29:38 +0800242 plat->ubivol = (char *)ofnode_read_string(
243 fs_loader_node, "ubivol");
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800244 }
245
246 return 0;
247}
248
249static int fs_loader_probe(struct udevice *dev)
250{
Tien Fong Chee5689ff12019-01-31 19:34:13 +0800251#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
252 int ret;
Simon Glassb75b15b2020-12-03 16:55:23 -0700253 struct device_plat *plat = dev->plat;
Tien Fong Chee5689ff12019-01-31 19:34:13 +0800254
255 if (plat->phandlepart.phandle) {
256 ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
257 struct udevice *parent_dev = NULL;
258
259 ret = device_get_global_by_ofnode(node, &parent_dev);
260 if (!ret) {
261 struct udevice *dev;
262
263 ret = blk_get_from_parent(parent_dev, &dev);
264 if (ret) {
265 debug("fs_loader: No block device: %d\n",
266 ret);
267
268 return ret;
269 }
270 }
271 }
272#endif
273
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800274 return 0;
275};
276
277static const struct udevice_id fs_loader_ids[] = {
278 { .compatible = "u-boot,fs-loader"},
279 { }
280};
281
282U_BOOT_DRIVER(fs_loader) = {
283 .name = "fs-loader",
284 .id = UCLASS_FS_FIRMWARE_LOADER,
285 .of_match = fs_loader_ids,
286 .probe = fs_loader_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700287 .of_to_plat = fs_loader_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700288 .plat_auto = sizeof(struct device_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700289 .priv_auto = sizeof(struct firmware),
Tien Fong Chee5ca878b2018-07-06 16:28:03 +0800290};
291
292UCLASS_DRIVER(fs_loader) = {
293 .id = UCLASS_FS_FIRMWARE_LOADER,
294 .name = "fs-loader",
295};