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