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 | */ |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 8 | #include <env.h> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <blk.h> |
| 11 | #include <fs.h> |
| 12 | #include <fs_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 14 | #include <linux/string.h> |
| 15 | #include <mapmem.h> |
| 16 | #include <malloc.h> |
| 17 | #include <spl.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 21 | /** |
| 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 | */ |
| 32 | struct firmware { |
| 33 | size_t size; |
| 34 | const u8 *data; |
| 35 | const char *name; |
| 36 | u32 offset; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | #ifdef CONFIG_CMD_UBIFS |
| 40 | static 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 | |
| 52 | static int umount_ubifs(void) |
| 53 | { |
| 54 | return cmd_ubifs_umount(); |
| 55 | } |
| 56 | #else |
| 57 | static 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame^] | 64 | static int select_fs_dev(struct device_plat *plat) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 65 | { |
| 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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 73 | struct udevice *dev; |
| 74 | |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 75 | ret = device_get_global_by_ofnode(node, &dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 76 | 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 Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 106 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 107 | * @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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 111 | * |
| 112 | * Return: Negative value if fail, 0 for successful. |
| 113 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 114 | static int _request_firmware_prepare(struct udevice *dev, |
| 115 | const char *name, void *dbuf, |
| 116 | size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 117 | { |
| 118 | if (!name || name[0] == '\0') |
| 119 | return -EINVAL; |
| 120 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 121 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 122 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 123 | if (!firmwarep) |
| 124 | return -ENOMEM; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 125 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 126 | firmwarep->name = name; |
| 127 | firmwarep->offset = offset; |
| 128 | firmwarep->data = dbuf; |
| 129 | firmwarep->size = size; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /** |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 135 | * fw_get_filesystem_firmware - load firmware into an allocated buffer. |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 136 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 137 | * |
| 138 | * Return: Size of total read, negative value when error. |
| 139 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 140 | static int fw_get_filesystem_firmware(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 141 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 142 | 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 Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 164 | ret = select_fs_dev(dev->plat); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (ret) |
| 168 | goto out; |
| 169 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 170 | struct firmware *firmwarep = dev_get_priv(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 171 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 172 | if (!firmwarep) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data), |
| 176 | firmwarep->offset, firmwarep->size, &actread); |
Keerthy | b5cdbd8 | 2018-11-05 11:34:53 +0530 | [diff] [blame] | 177 | |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 178 | if (ret) { |
Keerthy | 0bc2085 | 2018-11-05 11:34:54 +0530 | [diff] [blame] | 179 | 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] | 180 | ret, firmwarep->name, actread, firmwarep->size); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 181 | } else { |
| 182 | ret = actread; |
| 183 | } |
| 184 | |
| 185 | out: |
| 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 Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 194 | * @dev: An instance of a driver. |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 195 | * @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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 199 | * |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 200 | * 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] | 201 | * |
| 202 | * Return: Size of total read, negative value when error. |
| 203 | */ |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 204 | int request_firmware_into_buf(struct udevice *dev, |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 205 | const char *name, |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 206 | void *buf, size_t size, u32 offset) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 207 | { |
| 208 | int ret; |
| 209 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 210 | if (!dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 213 | ret = _request_firmware_prepare(dev, name, buf, size, offset); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 214 | if (ret < 0) /* error */ |
| 215 | return ret; |
| 216 | |
Tien Fong Chee | e8b7970 | 2018-12-10 21:29:44 +0800 | [diff] [blame] | 217 | ret = fw_get_filesystem_firmware(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 222 | static int fs_loader_of_to_plat(struct udevice *dev) |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 223 | { |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 224 | u32 phandlepart[2]; |
| 225 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 226 | ofnode fs_loader_node = dev_ofnode(dev); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 227 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 228 | if (ofnode_valid(fs_loader_node)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame^] | 229 | struct device_plat *plat; |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 230 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 231 | plat = dev->plat; |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 232 | 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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 238 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 239 | plat->mtdpart = (char *)ofnode_read_string( |
| 240 | fs_loader_node, "mtdpart"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 241 | |
Tien Fong Chee | 789aa9c | 2019-03-05 23:29:38 +0800 | [diff] [blame] | 242 | plat->ubivol = (char *)ofnode_read_string( |
| 243 | fs_loader_node, "ubivol"); |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int fs_loader_probe(struct udevice *dev) |
| 250 | { |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 251 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK) |
| 252 | int ret; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame^] | 253 | struct device_plat *plat = dev->plat; |
Tien Fong Chee | 5689ff1 | 2019-01-31 19:34:13 +0800 | [diff] [blame] | 254 | |
| 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 Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 274 | return 0; |
| 275 | }; |
| 276 | |
| 277 | static const struct udevice_id fs_loader_ids[] = { |
| 278 | { .compatible = "u-boot,fs-loader"}, |
| 279 | { } |
| 280 | }; |
| 281 | |
| 282 | U_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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 287 | .of_to_plat = fs_loader_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame^] | 288 | .plat_auto = sizeof(struct device_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 289 | .priv_auto = sizeof(struct firmware), |
Tien Fong Chee | 5ca878b | 2018-07-06 16:28:03 +0800 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | UCLASS_DRIVER(fs_loader) = { |
| 293 | .id = UCLASS_FS_FIRMWARE_LOADER, |
| 294 | .name = "fs-loader", |
| 295 | }; |