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> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <mapmem.h> |
| 20 | #include <malloc.h> |
| 21 | #include <spl.h> |
| 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 25 | /** |
| 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 | */ |
| 36 | struct firmware { |
| 37 | size_t size; |
| 38 | const u8 *data; |
| 39 | const char *name; |
| 40 | u32 offset; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #ifdef CONFIG_CMD_UBIFS |
| 44 | static 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 | |
| 56 | static int umount_ubifs(void) |
| 57 | { |
| 58 | return cmd_ubifs_umount(); |
| 59 | } |
| 60 | #else |
| 61 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 68 | static int select_fs_dev(struct device_plat *plat) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 69 | { |
| 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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 77 | struct udevice *dev; |
| 78 | |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 79 | ret = device_get_global_by_ofnode(node, &dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 80 | 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 Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 110 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 111 | * @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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 115 | * |
| 116 | * Return: Negative value if fail, 0 for successful. |
| 117 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 118 | static int _request_firmware_prepare(struct udevice *dev, |
| 119 | const char *name, void *dbuf, |
| 120 | size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 121 | { |
| 122 | if (!name || name[0] == '\0') |
| 123 | return -EINVAL; |
| 124 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 125 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 126 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 127 | if (!firmwarep) |
| 128 | return -ENOMEM; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 129 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 130 | firmwarep->name = name; |
| 131 | firmwarep->offset = offset; |
| 132 | firmwarep->data = dbuf; |
| 133 | firmwarep->size = size; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 139 | * fw_get_filesystem_firmware - load firmware into an allocated buffer. |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 140 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 141 | * |
| 142 | * Return: Size of total read, negative value when error. |
| 143 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 144 | static int fw_get_filesystem_firmware(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 145 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 146 | 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 Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 168 | ret = select_fs_dev(dev_get_plat(dev)); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (ret) |
| 172 | goto out; |
| 173 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 174 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 175 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 176 | if (!firmwarep) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data), |
| 180 | firmwarep->offset, firmwarep->size, &actread); |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 181 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 182 | if (ret) { |
Keerthy | 0bc2085 | 2018-11-05 11:34:54 +0530 | [diff] [blame] | 183 | 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] | 184 | ret, firmwarep->name, actread, firmwarep->size); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 185 | } else { |
| 186 | ret = actread; |
| 187 | } |
| 188 | |
| 189 | out: |
| 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 Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 198 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 199 | * @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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 203 | * |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 204 | * 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] | 205 | * |
| 206 | * Return: Size of total read, negative value when error. |
| 207 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 208 | int request_firmware_into_buf(struct udevice *dev, |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 209 | const char *name, |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 210 | void *buf, size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 211 | { |
| 212 | int ret; |
| 213 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 214 | if (!dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 215 | return -EINVAL; |
| 216 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 217 | ret = _request_firmware_prepare(dev, name, buf, size, offset); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 218 | if (ret < 0) /* error */ |
| 219 | return ret; |
| 220 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 221 | ret = fw_get_filesystem_firmware(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 226 | static int fs_loader_of_to_plat(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 227 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 228 | u32 phandlepart[2]; |
| 229 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 230 | ofnode fs_loader_node = dev_ofnode(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 231 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 232 | if (ofnode_valid(fs_loader_node)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 233 | struct device_plat *plat; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 234 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 235 | plat = dev_get_plat(dev); |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 236 | 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]; |
| 241 | } |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 242 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 243 | plat->mtdpart = (char *)ofnode_read_string( |
| 244 | fs_loader_node, "mtdpart"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 245 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 246 | plat->ubivol = (char *)ofnode_read_string( |
| 247 | fs_loader_node, "ubivol"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int fs_loader_probe(struct udevice *dev) |
| 254 | { |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 255 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK) |
| 256 | int ret; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 257 | struct device_plat *plat = dev_get_plat(dev); |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 258 | |
| 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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 278 | return 0; |
| 279 | }; |
| 280 | |
| 281 | static const struct udevice_id fs_loader_ids[] = { |
| 282 | { .compatible = "u-boot,fs-loader"}, |
| 283 | { } |
| 284 | }; |
| 285 | |
| 286 | U_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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 291 | .of_to_plat = fs_loader_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 292 | .plat_auto = sizeof(struct device_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 293 | .priv_auto = sizeof(struct firmware), |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | UCLASS_DRIVER(fs_loader) = { |
| 297 | .id = UCLASS_FS_FIRMWARE_LOADER, |
| 298 | .name = "fs-loader", |
| 299 | }; |