Patrick Delaunay | d6e53c7 | 2018-10-26 09:02:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 3 | * Copyright (C) 2018-2019 Intel Corporation <www.intel.com> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 4 | * |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 5 | */ |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | |
| 7 | #define LOG_CATEGORY UCLASS_FS_FIRMWARE_LOADER |
| 8 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 11 | #include <env.h> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 12 | #include <errno.h> |
| 13 | #include <blk.h> |
| 14 | #include <fs.h> |
| 15 | #include <fs_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Sean Anderson | 5cd0cb3 | 2022-12-29 11:52:59 -0500 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/root.h> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <mapmem.h> |
| 22 | #include <malloc.h> |
| 23 | #include <spl.h> |
| 24 | |
Pali Rohár | 6981311 | 2022-04-29 16:36:23 +0200 | [diff] [blame] | 25 | #ifdef CONFIG_CMD_UBIFS |
| 26 | #include <ubi_uboot.h> |
| 27 | #endif |
| 28 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 31 | /** |
| 32 | * struct firmware - A place for storing firmware and its attribute data. |
| 33 | * |
| 34 | * This holds information about a firmware and its content. |
| 35 | * |
| 36 | * @size: Size of a file |
| 37 | * @data: Buffer for file |
| 38 | * @priv: Firmware loader private fields |
| 39 | * @name: Filename |
| 40 | * @offset: Offset of reading a file |
| 41 | */ |
| 42 | struct firmware { |
| 43 | size_t size; |
| 44 | const u8 *data; |
| 45 | const char *name; |
| 46 | u32 offset; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | #ifdef CONFIG_CMD_UBIFS |
| 50 | static int mount_ubifs(char *mtdpart, char *ubivol) |
| 51 | { |
| 52 | int ret = ubi_part(mtdpart, NULL); |
| 53 | |
| 54 | if (ret) { |
| 55 | debug("Cannot find mtd partition %s\n", mtdpart); |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | return cmd_ubifs_mount(ubivol); |
| 60 | } |
| 61 | |
| 62 | static int umount_ubifs(void) |
| 63 | { |
| 64 | return cmd_ubifs_umount(); |
| 65 | } |
| 66 | #else |
| 67 | static int mount_ubifs(char *mtdpart, char *ubivol) |
| 68 | { |
| 69 | debug("Error: Cannot load image: no UBIFS support\n"); |
| 70 | return -ENOSYS; |
| 71 | } |
| 72 | #endif |
| 73 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 74 | static int select_fs_dev(struct device_plat *plat) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 75 | { |
| 76 | int ret; |
| 77 | |
| 78 | if (plat->phandlepart.phandle) { |
| 79 | ofnode node; |
| 80 | |
| 81 | node = ofnode_get_by_phandle(plat->phandlepart.phandle); |
| 82 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 83 | struct udevice *dev; |
| 84 | |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 85 | ret = device_get_global_by_ofnode(node, &dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 86 | if (!ret) { |
| 87 | struct blk_desc *desc = blk_get_by_device(dev); |
| 88 | if (desc) { |
| 89 | ret = fs_set_blk_dev_with_part(desc, |
| 90 | plat->phandlepart.partition); |
| 91 | } else { |
| 92 | debug("%s: No device found\n", __func__); |
| 93 | return -ENODEV; |
| 94 | } |
| 95 | } |
| 96 | } else if (plat->mtdpart && plat->ubivol) { |
| 97 | ret = mount_ubifs(plat->mtdpart, plat->ubivol); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS); |
| 102 | } else { |
| 103 | debug("Error: unsupported storage device.\n"); |
| 104 | return -ENODEV; |
| 105 | } |
| 106 | |
| 107 | if (ret) |
| 108 | debug("Error: could not access storage.\n"); |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * _request_firmware_prepare - Prepare firmware struct. |
| 115 | * |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 116 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 117 | * @name: Name of firmware file. |
| 118 | * @dbuf: Address of buffer to load firmware into. |
| 119 | * @size: Size of buffer. |
| 120 | * @offset: Offset of a file for start reading into buffer. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 121 | * |
| 122 | * Return: Negative value if fail, 0 for successful. |
| 123 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 124 | static int _request_firmware_prepare(struct udevice *dev, |
| 125 | const char *name, void *dbuf, |
| 126 | size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 127 | { |
| 128 | if (!name || name[0] == '\0') |
| 129 | return -EINVAL; |
| 130 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 131 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 132 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 133 | if (!firmwarep) |
| 134 | return -ENOMEM; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 135 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 136 | firmwarep->name = name; |
| 137 | firmwarep->offset = offset; |
| 138 | firmwarep->data = dbuf; |
| 139 | firmwarep->size = size; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /** |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 145 | * fw_get_filesystem_firmware - load firmware into an allocated buffer. |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 146 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 147 | * |
| 148 | * Return: Size of total read, negative value when error. |
| 149 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 150 | static int fw_get_filesystem_firmware(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 151 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 152 | loff_t actread; |
| 153 | char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume; |
| 154 | int ret; |
| 155 | |
| 156 | storage_interface = env_get("storage_interface"); |
| 157 | dev_part = env_get("fw_dev_part"); |
| 158 | ubi_mtdpart = env_get("fw_ubi_mtdpart"); |
| 159 | ubi_volume = env_get("fw_ubi_volume"); |
| 160 | |
| 161 | if (storage_interface && dev_part) { |
| 162 | ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY); |
| 163 | } else if (storage_interface && ubi_mtdpart && ubi_volume) { |
| 164 | ret = mount_ubifs(ubi_mtdpart, ubi_volume); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | if (!strcmp("ubi", storage_interface)) |
| 169 | ret = fs_set_blk_dev(storage_interface, NULL, |
| 170 | FS_TYPE_UBIFS); |
| 171 | else |
| 172 | ret = -ENODEV; |
| 173 | } else { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 174 | ret = select_fs_dev(dev_get_plat(dev)); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (ret) |
| 178 | goto out; |
| 179 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 180 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 181 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 182 | if (!firmwarep) |
| 183 | return -ENOMEM; |
| 184 | |
| 185 | ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data), |
| 186 | firmwarep->offset, firmwarep->size, &actread); |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 187 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 188 | if (ret) { |
Keerthy | 0bc2085 | 2018-11-05 11:34:54 +0530 | [diff] [blame] | 189 | debug("Error: %d Failed to read %s from flash %lld != %zu.\n", |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 190 | ret, firmwarep->name, actread, firmwarep->size); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 191 | } else { |
| 192 | ret = actread; |
| 193 | } |
| 194 | |
| 195 | out: |
| 196 | #ifdef CONFIG_CMD_UBIFS |
| 197 | umount_ubifs(); |
| 198 | #endif |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * request_firmware_into_buf - Load firmware into a previously allocated buffer. |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 204 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 205 | * @name: Name of firmware file. |
| 206 | * @buf: Address of buffer to load firmware into. |
| 207 | * @size: Size of buffer. |
| 208 | * @offset: Offset of a file for start reading into buffer. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 209 | * |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 210 | * The firmware is loaded directly into the buffer pointed to by @buf. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 211 | * |
| 212 | * Return: Size of total read, negative value when error. |
| 213 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 214 | int request_firmware_into_buf(struct udevice *dev, |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 215 | const char *name, |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 216 | void *buf, size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 217 | { |
| 218 | int ret; |
| 219 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 220 | if (!dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 221 | return -EINVAL; |
| 222 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 223 | ret = _request_firmware_prepare(dev, name, buf, size, offset); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 224 | if (ret < 0) /* error */ |
| 225 | return ret; |
| 226 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 227 | ret = fw_get_filesystem_firmware(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 232 | static int fs_loader_of_to_plat(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 233 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 234 | u32 phandlepart[2]; |
| 235 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 236 | ofnode fs_loader_node = dev_ofnode(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 237 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 238 | if (ofnode_valid(fs_loader_node)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 239 | struct device_plat *plat; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 240 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 241 | plat = dev_get_plat(dev); |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 242 | if (!ofnode_read_u32_array(fs_loader_node, |
| 243 | "phandlepart", |
| 244 | phandlepart, 2)) { |
| 245 | plat->phandlepart.phandle = phandlepart[0]; |
| 246 | plat->phandlepart.partition = phandlepart[1]; |
| 247 | } |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 248 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 249 | plat->mtdpart = (char *)ofnode_read_string( |
| 250 | fs_loader_node, "mtdpart"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 251 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 252 | plat->ubivol = (char *)ofnode_read_string( |
| 253 | fs_loader_node, "ubivol"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int fs_loader_probe(struct udevice *dev) |
| 260 | { |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 261 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK) |
| 262 | int ret; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 263 | struct device_plat *plat = dev_get_plat(dev); |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 264 | |
| 265 | if (plat->phandlepart.phandle) { |
| 266 | ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle); |
| 267 | struct udevice *parent_dev = NULL; |
| 268 | |
| 269 | ret = device_get_global_by_ofnode(node, &parent_dev); |
| 270 | if (!ret) { |
| 271 | struct udevice *dev; |
| 272 | |
| 273 | ret = blk_get_from_parent(parent_dev, &dev); |
| 274 | if (ret) { |
| 275 | debug("fs_loader: No block device: %d\n", |
| 276 | ret); |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | #endif |
| 283 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 284 | return 0; |
| 285 | }; |
| 286 | |
| 287 | static const struct udevice_id fs_loader_ids[] = { |
| 288 | { .compatible = "u-boot,fs-loader"}, |
| 289 | { } |
| 290 | }; |
| 291 | |
| 292 | U_BOOT_DRIVER(fs_loader) = { |
| 293 | .name = "fs-loader", |
| 294 | .id = UCLASS_FS_FIRMWARE_LOADER, |
| 295 | .of_match = fs_loader_ids, |
| 296 | .probe = fs_loader_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 297 | .of_to_plat = fs_loader_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 298 | .plat_auto = sizeof(struct device_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 299 | .priv_auto = sizeof(struct firmware), |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 300 | }; |
| 301 | |
Sean Anderson | 5cd0cb3 | 2022-12-29 11:52:59 -0500 | [diff] [blame] | 302 | static struct device_plat default_plat = { 0 }; |
| 303 | |
| 304 | int get_fs_loader(struct udevice **dev) |
| 305 | { |
| 306 | int ret; |
| 307 | ofnode node = ofnode_get_chosen_node("firmware-loader"); |
| 308 | |
| 309 | if (ofnode_valid(node)) |
| 310 | return uclass_get_device_by_ofnode(UCLASS_FS_FIRMWARE_LOADER, |
| 311 | node, dev); |
| 312 | |
| 313 | /* Try the first device if none was chosen */ |
| 314 | ret = uclass_first_device_err(UCLASS_FS_FIRMWARE_LOADER, dev); |
| 315 | if (ret != -ENODEV) |
| 316 | return ret; |
| 317 | |
| 318 | /* Just create a new device */ |
Sean Anderson | 7edccc4 | 2023-09-30 16:45:46 -0400 | [diff] [blame] | 319 | ret = device_bind(dm_root(), DM_DRIVER_REF(fs_loader), "default-loader", |
Sean Anderson | 5cd0cb3 | 2022-12-29 11:52:59 -0500 | [diff] [blame] | 320 | &default_plat, ofnode_null(), dev); |
| 321 | if (ret) |
| 322 | return ret; |
| 323 | |
| 324 | return device_probe(*dev); |
| 325 | } |
| 326 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 327 | UCLASS_DRIVER(fs_loader) = { |
| 328 | .id = UCLASS_FS_FIRMWARE_LOADER, |
| 329 | .name = "fs-loader", |
| 330 | }; |