2018-10-26 07:02:52 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2019-03-05 15:29:38 +00:00
|
|
|
* Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
|
2018-07-06 08:28:03 +00:00
|
|
|
*
|
|
|
|
*/
|
2021-04-27 09:02:19 +00:00
|
|
|
|
|
|
|
#define LOG_CATEGORY UCLASS_FS_FIRMWARE_LOADER
|
|
|
|
|
2018-07-06 08:28:03 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2019-08-01 15:46:52 +00:00
|
|
|
#include <env.h>
|
2018-07-06 08:28:03 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <blk.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <fs_loader.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2022-12-29 16:52:59 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/root.h>
|
2018-07-06 08:28:03 +00:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <mapmem.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <spl.h>
|
|
|
|
|
2022-04-29 14:36:23 +00:00
|
|
|
#ifdef CONFIG_CMD_UBIFS
|
|
|
|
#include <ubi_uboot.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 08:28:03 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
/**
|
|
|
|
* struct firmware - A place for storing firmware and its attribute data.
|
|
|
|
*
|
|
|
|
* This holds information about a firmware and its content.
|
|
|
|
*
|
|
|
|
* @size: Size of a file
|
|
|
|
* @data: Buffer for file
|
|
|
|
* @priv: Firmware loader private fields
|
|
|
|
* @name: Filename
|
|
|
|
* @offset: Offset of reading a file
|
|
|
|
*/
|
|
|
|
struct firmware {
|
|
|
|
size_t size;
|
|
|
|
const u8 *data;
|
|
|
|
const char *name;
|
|
|
|
u32 offset;
|
2018-07-06 08:28:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_CMD_UBIFS
|
|
|
|
static int mount_ubifs(char *mtdpart, char *ubivol)
|
|
|
|
{
|
|
|
|
int ret = ubi_part(mtdpart, NULL);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
debug("Cannot find mtd partition %s\n", mtdpart);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd_ubifs_mount(ubivol);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int umount_ubifs(void)
|
|
|
|
{
|
|
|
|
return cmd_ubifs_umount();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int mount_ubifs(char *mtdpart, char *ubivol)
|
|
|
|
{
|
|
|
|
debug("Error: Cannot load image: no UBIFS support\n");
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static int select_fs_dev(struct device_plat *plat)
|
2018-07-06 08:28:03 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (plat->phandlepart.phandle) {
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_get_by_phandle(plat->phandlepart.phandle);
|
|
|
|
|
|
|
|
struct udevice *dev;
|
|
|
|
|
2018-11-05 06:04:53 +00:00
|
|
|
ret = device_get_global_by_ofnode(node, &dev);
|
2018-07-06 08:28:03 +00:00
|
|
|
if (!ret) {
|
|
|
|
struct blk_desc *desc = blk_get_by_device(dev);
|
|
|
|
if (desc) {
|
|
|
|
ret = fs_set_blk_dev_with_part(desc,
|
|
|
|
plat->phandlepart.partition);
|
|
|
|
} else {
|
|
|
|
debug("%s: No device found\n", __func__);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (plat->mtdpart && plat->ubivol) {
|
|
|
|
ret = mount_ubifs(plat->mtdpart, plat->ubivol);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
|
|
|
|
} else {
|
|
|
|
debug("Error: unsupported storage device.\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
debug("Error: could not access storage.\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _request_firmware_prepare - Prepare firmware struct.
|
|
|
|
*
|
2018-12-10 13:29:44 +00:00
|
|
|
* @dev: An instance of a driver.
|
2018-07-06 08:28:03 +00:00
|
|
|
* @name: Name of firmware file.
|
|
|
|
* @dbuf: Address of buffer to load firmware into.
|
|
|
|
* @size: Size of buffer.
|
|
|
|
* @offset: Offset of a file for start reading into buffer.
|
|
|
|
*
|
|
|
|
* Return: Negative value if fail, 0 for successful.
|
|
|
|
*/
|
2018-12-10 13:29:44 +00:00
|
|
|
static int _request_firmware_prepare(struct udevice *dev,
|
|
|
|
const char *name, void *dbuf,
|
|
|
|
size_t size, u32 offset)
|
2018-07-06 08:28:03 +00:00
|
|
|
{
|
|
|
|
if (!name || name[0] == '\0')
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
struct firmware *firmwarep = dev_get_priv(dev);
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
if (!firmwarep)
|
|
|
|
return -ENOMEM;
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
firmwarep->name = name;
|
|
|
|
firmwarep->offset = offset;
|
|
|
|
firmwarep->data = dbuf;
|
|
|
|
firmwarep->size = size;
|
2018-07-06 08:28:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fw_get_filesystem_firmware - load firmware into an allocated buffer.
|
2018-12-10 13:29:44 +00:00
|
|
|
* @dev: An instance of a driver.
|
2018-07-06 08:28:03 +00:00
|
|
|
*
|
|
|
|
* Return: Size of total read, negative value when error.
|
|
|
|
*/
|
2018-12-10 13:29:44 +00:00
|
|
|
static int fw_get_filesystem_firmware(struct udevice *dev)
|
2018-07-06 08:28:03 +00:00
|
|
|
{
|
|
|
|
loff_t actread;
|
|
|
|
char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
storage_interface = env_get("storage_interface");
|
|
|
|
dev_part = env_get("fw_dev_part");
|
|
|
|
ubi_mtdpart = env_get("fw_ubi_mtdpart");
|
|
|
|
ubi_volume = env_get("fw_ubi_volume");
|
|
|
|
|
|
|
|
if (storage_interface && dev_part) {
|
|
|
|
ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
|
|
|
|
} else if (storage_interface && ubi_mtdpart && ubi_volume) {
|
|
|
|
ret = mount_ubifs(ubi_mtdpart, ubi_volume);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!strcmp("ubi", storage_interface))
|
|
|
|
ret = fs_set_blk_dev(storage_interface, NULL,
|
|
|
|
FS_TYPE_UBIFS);
|
|
|
|
else
|
|
|
|
ret = -ENODEV;
|
|
|
|
} else {
|
2020-12-23 02:30:28 +00:00
|
|
|
ret = select_fs_dev(dev_get_plat(dev));
|
2018-07-06 08:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
struct firmware *firmwarep = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (!firmwarep)
|
|
|
|
return -ENOMEM;
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
|
|
|
|
firmwarep->offset, firmwarep->size, &actread);
|
2018-11-05 06:04:53 +00:00
|
|
|
|
2018-07-06 08:28:03 +00:00
|
|
|
if (ret) {
|
2018-11-05 06:04:54 +00:00
|
|
|
debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
|
2018-12-10 13:29:44 +00:00
|
|
|
ret, firmwarep->name, actread, firmwarep->size);
|
2018-07-06 08:28:03 +00:00
|
|
|
} else {
|
|
|
|
ret = actread;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
#ifdef CONFIG_CMD_UBIFS
|
|
|
|
umount_ubifs();
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* request_firmware_into_buf - Load firmware into a previously allocated buffer.
|
2018-12-10 13:29:44 +00:00
|
|
|
* @dev: An instance of a driver.
|
2018-07-06 08:28:03 +00:00
|
|
|
* @name: Name of firmware file.
|
|
|
|
* @buf: Address of buffer to load firmware into.
|
|
|
|
* @size: Size of buffer.
|
|
|
|
* @offset: Offset of a file for start reading into buffer.
|
|
|
|
*
|
2018-12-10 13:29:44 +00:00
|
|
|
* The firmware is loaded directly into the buffer pointed to by @buf.
|
2018-07-06 08:28:03 +00:00
|
|
|
*
|
|
|
|
* Return: Size of total read, negative value when error.
|
|
|
|
*/
|
2018-12-10 13:29:44 +00:00
|
|
|
int request_firmware_into_buf(struct udevice *dev,
|
2018-07-06 08:28:03 +00:00
|
|
|
const char *name,
|
2018-12-10 13:29:44 +00:00
|
|
|
void *buf, size_t size, u32 offset)
|
2018-07-06 08:28:03 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
if (!dev)
|
2018-07-06 08:28:03 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
ret = _request_firmware_prepare(dev, name, buf, size, offset);
|
2018-07-06 08:28:03 +00:00
|
|
|
if (ret < 0) /* error */
|
|
|
|
return ret;
|
|
|
|
|
2018-12-10 13:29:44 +00:00
|
|
|
ret = fw_get_filesystem_firmware(dev);
|
2018-07-06 08:28:03 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int fs_loader_of_to_plat(struct udevice *dev)
|
2018-07-06 08:28:03 +00:00
|
|
|
{
|
|
|
|
u32 phandlepart[2];
|
|
|
|
|
2019-03-05 15:29:38 +00:00
|
|
|
ofnode fs_loader_node = dev_ofnode(dev);
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2019-03-05 15:29:38 +00:00
|
|
|
if (ofnode_valid(fs_loader_node)) {
|
2020-12-03 23:55:23 +00:00
|
|
|
struct device_plat *plat;
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2020-12-23 02:30:28 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2019-03-05 15:29:38 +00:00
|
|
|
if (!ofnode_read_u32_array(fs_loader_node,
|
|
|
|
"phandlepart",
|
|
|
|
phandlepart, 2)) {
|
|
|
|
plat->phandlepart.phandle = phandlepart[0];
|
|
|
|
plat->phandlepart.partition = phandlepart[1];
|
|
|
|
}
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2019-03-05 15:29:38 +00:00
|
|
|
plat->mtdpart = (char *)ofnode_read_string(
|
|
|
|
fs_loader_node, "mtdpart");
|
2018-07-06 08:28:03 +00:00
|
|
|
|
2019-03-05 15:29:38 +00:00
|
|
|
plat->ubivol = (char *)ofnode_read_string(
|
|
|
|
fs_loader_node, "ubivol");
|
2018-07-06 08:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fs_loader_probe(struct udevice *dev)
|
|
|
|
{
|
2019-01-31 11:34:13 +00:00
|
|
|
#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
|
|
|
|
int ret;
|
2020-12-23 02:30:28 +00:00
|
|
|
struct device_plat *plat = dev_get_plat(dev);
|
2019-01-31 11:34:13 +00:00
|
|
|
|
|
|
|
if (plat->phandlepart.phandle) {
|
|
|
|
ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
|
|
|
|
struct udevice *parent_dev = NULL;
|
|
|
|
|
|
|
|
ret = device_get_global_by_ofnode(node, &parent_dev);
|
|
|
|
if (!ret) {
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
ret = blk_get_from_parent(parent_dev, &dev);
|
|
|
|
if (ret) {
|
|
|
|
debug("fs_loader: No block device: %d\n",
|
|
|
|
ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 08:28:03 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id fs_loader_ids[] = {
|
|
|
|
{ .compatible = "u-boot,fs-loader"},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(fs_loader) = {
|
|
|
|
.name = "fs-loader",
|
|
|
|
.id = UCLASS_FS_FIRMWARE_LOADER,
|
|
|
|
.of_match = fs_loader_ids,
|
|
|
|
.probe = fs_loader_probe,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = fs_loader_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct device_plat),
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct firmware),
|
2018-07-06 08:28:03 +00:00
|
|
|
};
|
|
|
|
|
2022-12-29 16:52:59 +00:00
|
|
|
static struct device_plat default_plat = { 0 };
|
|
|
|
|
|
|
|
int get_fs_loader(struct udevice **dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ofnode node = ofnode_get_chosen_node("firmware-loader");
|
|
|
|
|
|
|
|
if (ofnode_valid(node))
|
|
|
|
return uclass_get_device_by_ofnode(UCLASS_FS_FIRMWARE_LOADER,
|
|
|
|
node, dev);
|
|
|
|
|
|
|
|
/* Try the first device if none was chosen */
|
|
|
|
ret = uclass_first_device_err(UCLASS_FS_FIRMWARE_LOADER, dev);
|
|
|
|
if (ret != -ENODEV)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Just create a new device */
|
|
|
|
ret = device_bind(dm_root(), DM_DRIVER_GET(fs_loader), "default-loader",
|
|
|
|
&default_plat, ofnode_null(), dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return device_probe(*dev);
|
|
|
|
}
|
|
|
|
|
2018-07-06 08:28:03 +00:00
|
|
|
UCLASS_DRIVER(fs_loader) = {
|
|
|
|
.id = UCLASS_FS_FIRMWARE_LOADER,
|
|
|
|
.name = "fs-loader",
|
|
|
|
};
|