From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lothar =?UTF-8?B?V2HDn21hbm4=?= Date: Thu, 21 Dec 2017 09:48:17 +0100 Subject: [U-Boot] [PATCH v5 2/2] common: Generic firmware loader for file system In-Reply-To: <1513841129-4862-4-git-send-email-tien.fong.chee@intel.com> References: <1513841129-4862-1-git-send-email-tien.fong.chee@intel.com> <1513841129-4862-4-git-send-email-tien.fong.chee@intel.com> Message-ID: <20171221094817.4811722c@karo-electronics.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: quoted-printable To: u-boot@lists.denx.de Hi, On Thu, 21 Dec 2017 15:25:29 +0800 tien.fong.chee at intel.com wrote: > From: Tien Fong Chee >=20 > This is file system generic loader which can be used to load > the file image from the storage into target such as memory. > The consumer driver would then use this loader to program whatever, > ie. the FPGA device. >=20 > Signed-off-by: Tien Fong Chee > --- > common/Makefile | 1 + > common/fs_loader.c | 311 +++++++++++++++++++++++++++++++++++++++= ++++++ > doc/README.firmware_loader | 76 +++++++++++ > include/fs_loader.h | 28 ++++ > 4 files changed, 416 insertions(+) > create mode 100644 common/fs_loader.c > create mode 100644 doc/README.firmware_loader > create mode 100644 include/fs_loader.h >=20 > diff --git a/common/Makefile b/common/Makefile > index cec506f..2934221 100644 > --- a/common/Makefile > +++ b/common/Makefile > @@ -130,3 +130,4 @@ obj-$(CONFIG_CMD_DFU) +=3D dfu.o > obj-y +=3D command.o > obj-y +=3D s_record.o > obj-y +=3D xyzModem.o > +obj-y +=3D fs_loader.o > diff --git a/common/fs_loader.c b/common/fs_loader.c > new file mode 100644 > index 0000000..ddfce58 > --- /dev/null > +++ b/common/fs_loader.c > @@ -0,0 +1,311 @@ > +/* > + * Copyright (C) 2017 Intel Corporation > + * > + * SPDX-License-Identifier: GPL-2.0 > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +struct firmware_priv { > + const char *name; /* Filename */ > + u32 offset; /* Offset of reading a file */ > +}; > + > +static struct device_location default_locations[] =3D { > + { > + .name =3D "mmc", > + .devpart =3D "0:1", > + }, > + { > + .name =3D "usb", > + .devpart =3D "0:1", > + }, > + { > + .name =3D "sata", > + .devpart =3D "0:1", > + }, > +}; > + > +/* USB build is not supported yet in SPL */ > +#ifndef CONFIG_SPL_BUILD > +#ifdef CONFIG_USB_STORAGE > +static int init_usb(void) > +{ > + int err; > + > + err =3D usb_init(); > + if (err) > + return err; > + > +#ifndef CONFIG_DM_USB > + err =3D usb_stor_scan(1) < 0 ? -ENODEV : 0; > +#endif > + > + return err; > +} > +#else > +static int init_usb(void) > +{ > + printf("Error: Cannot load flash image: no USB support\n"); > + return -ENOSYS; > +} > +#endif > +#endif > + > +#ifdef CONFIG_SATA > +static int init_storage_sata(void) > +{ > + return sata_probe(0); > +} > +#else > +static int init_storage_sata(void) > +{ > + printf("Error: Cannot load image: no SATA support\n"); > + return -ENOSYS; > +} > +#endif > + > +#ifdef CONFIG_CMD_UBIFS > +static int mount_ubifs(struct device_location *location) > +{ > + int ret; > + char cmd[32]; > + > + sprintf(cmd, "ubi part %s", location->mtdpart); > + > + ret =3D run_command(cmd, 0); > + if (ret) > + return ret; > + > + sprintf(cmd, "ubifsmount %s", location->ubivol); > + > + ret =3D run_command(cmd, 0); > + > + return ret; > +} > + > +static int umount_ubifs(void) > +{ > + return run_command("ubifsumount", 0); > +} > +#else > +static int mount_ubifs(struct device_location *location) > +{ > + printf("Error: Cannot load image: no UBIFS support\n"); > + return -ENOSYS; > +} > +#endif > + > +#if defined(CONFIG_SPL_MMC_SUPPORT) && defined(CONFIG_SPL_BUILD) > +static int init_mmc(void) > +{ > + /* Just for the case MMC is not yet initialized */ > + struct mmc *mmc =3D NULL; > + int err; > + > + spl_mmc_find_device(&mmc, spl_boot_device()); > + > + err =3D mmc_init(mmc); > + if (err) { > + printf("spl: mmc init failed with error: %d\n", err); > + return err; > + } > + > + return err; > +} > +#else > +static int init_mmc(void) > +{ > + /* Expect somewhere already initialize MMC */ > + return 0; > +} > +#endif > + > +static int select_fs_dev(struct device_location *location) > +{ > + int ret; > + > + if (!strcmp("mmc", location->name)) { > + ret =3D fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY); > + } else if (!strcmp("usb", location->name)) { > + ret =3D fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY); > + } else if (!strcmp("sata", location->name)) { > + ret =3D fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY); > + } else if (!strcmp("ubi", location->name)) { > + if (location->ubivol !=3D NULL) > + ret =3D fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS); > + else > + ret =3D -ENODEV; > + } else { > + printf("Error: unsupported location storage.\n"); > + return -ENODEV; > + } > + > + if (ret) > + printf("Error: could not access storage.\n"); > + > + return ret; > +} > + > +static int init_storage_device(struct device_location *location) > +{ > + int ret; > + > + if (!strcmp("mmc", location->name)) { > + ret =3D init_mmc(); > + } else if (!strcmp("sata", location->name)) { > + ret =3D init_storage_sata(); > + } else if (location->ubivol !=3D NULL) { > + ret =3D mount_ubifs(location); > +#ifndef CONFIG_SPL_BUILD > + /* USB build is not supported yet in SPL */ > + } else if (!strcmp("usb", location->name)) { > + ret =3D init_usb(); > +#endif > + } else { > + printf("Error: no supported storage device is available.\n"); > + ret =3D -ENODEV; > + } > + > + return ret; > +} > + > +static void set_storage_devpart(char *name, char *devpart) > +{ > + size_t i; > + > + for (i =3D 0; i < ARRAY_SIZE(default_locations); i++) { > + if (!strcmp(default_locations[i].name, name)) > + default_locations[i].devpart =3D devpart; > + } > +} > + > +/* > + * Prepare firmware struct; > + * return -ve if fail. > + */ > +static int _request_firmware_prepare(struct firmware **firmware_p, > + const char *name, void *dbuf, > + size_t size, u32 offset) > +{ > + struct firmware *firmware; > + struct firmware_priv *fw_priv; > + > + *firmware_p =3D NULL; > + > + if (!name || name[0] =3D=3D '\0') > + return -EINVAL; > + > + firmware =3D calloc(1, sizeof(*firmware)); > + if (!firmware) { > + printf("%s: calloc(struct firmware) failed\n", __func__); > + return -ENOMEM; > + } > + > + fw_priv =3D calloc(1, sizeof(*fw_priv)); > + if (!fw_priv) { > + printf("%s: calloc(struct fw_priv) failed\n", __func__); > + free(firmware); > + return -ENOMEM; > + } > + > + fw_priv->name =3D name; > + fw_priv->offset =3D offset; > + firmware->data =3D dbuf; > + firmware->size =3D size; > + firmware->priv =3D fw_priv; > + *firmware_p =3D firmware; > + > + return 0; > +} > + > +/* > + * fw_get_filesystem_firmware - load firmware into an allocated buffer > + * @location: An array of supported firmware location > + * @firmware_p: pointer to firmware image > + * > + * @return: size of total read > + * -ve when error > + */ > +static int fw_get_filesystem_firmware(struct device_location *location, > + struct firmware *firmware_p) > +{ > + struct firmware_priv *fw_priv =3D NULL; > + loff_t actread; > + char *dev_part; > + int ret; > + > + dev_part =3D env_get("fw_dev_part"); > + if (dev_part) > + set_storage_devpart(location->name, dev_part); > + > + ret =3D init_storage_device(location); > + if (ret) > + goto out; > + > + select_fs_dev(location); > 'ret =3D ' is missing. > + if (ret) > + goto out; > + > + fw_priv =3D firmware_p->priv; > + > + ret =3D fs_read(fw_priv->name, (ulong)firmware_p->data, fw_priv->offset, > + firmware_p->size, &actread); > + > + if (ret) { > + printf("Error: %d Failed to read %s from flash %lld !=3D %d.\n", > + ret, fw_priv->name, actread, firmware_p->size); > + return ret; Y Shouldn't this be 'goto out', do do the umount_ubifs() as in all other error cases? > + } else { > + ret =3D actread; > + } > What if actread !=3D firmware_p->size? You handled it as an error before, now you happily return the number of bytes read in case of a short read. > + > +out: > +#ifdef CONFIG_CMD_UBIFS > + if (location->ubivol !=3D NULL) > + umount_ubifs(); > +#endif > + > + return ret; > +} > + Lothar Wa=C3=9Fmann