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