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