2020-03-18 08:22:45 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020, STMicroelectronics - All Rights Reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-03-18 08:22:46 +00:00
|
|
|
#include <blk.h>
|
2020-07-19 16:15:40 +00:00
|
|
|
#include <dm.h>
|
2020-03-18 08:22:45 +00:00
|
|
|
#include <dfu.h>
|
|
|
|
#include <env.h>
|
2020-11-06 18:02:00 +00:00
|
|
|
#include <log.h>
|
2020-03-18 08:22:45 +00:00
|
|
|
#include <memalign.h>
|
|
|
|
#include <misc.h>
|
|
|
|
#include <mtd.h>
|
|
|
|
#include <mtd_node.h>
|
2020-03-18 08:24:49 +00:00
|
|
|
#include <asm/arch/stm32prog.h>
|
2020-03-18 08:22:45 +00:00
|
|
|
|
|
|
|
#define DFU_ALT_BUF_LEN SZ_1K
|
|
|
|
|
2020-03-18 08:22:46 +00:00
|
|
|
static void board_get_alt_info_mmc(struct udevice *dev, char *buf)
|
2020-03-18 08:22:45 +00:00
|
|
|
{
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition info;
|
2020-03-18 08:22:46 +00:00
|
|
|
int p, len, devnum;
|
|
|
|
bool first = true;
|
|
|
|
const char *name;
|
|
|
|
struct mmc *mmc;
|
|
|
|
struct blk_desc *desc;
|
|
|
|
|
|
|
|
mmc = mmc_get_mmc_dev(dev);
|
|
|
|
if (!mmc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mmc_init(mmc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
desc = mmc_get_blk_desc(mmc);
|
|
|
|
if (!desc)
|
|
|
|
return;
|
|
|
|
|
2022-09-17 15:00:09 +00:00
|
|
|
name = blk_get_uclass_name(desc->uclass_id);
|
2020-03-18 08:22:46 +00:00
|
|
|
devnum = desc->devnum;
|
|
|
|
len = strlen(buf);
|
|
|
|
|
|
|
|
if (buf[0] != '\0')
|
|
|
|
len += snprintf(buf + len,
|
|
|
|
DFU_ALT_BUF_LEN - len, "&");
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s %d=", name, devnum);
|
|
|
|
|
|
|
|
if (IS_MMC(mmc) && mmc->capacity_boot) {
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s%d_boot1 raw 0x0 0x%llx mmcpart 1;",
|
|
|
|
name, devnum, mmc->capacity_boot);
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s%d_boot2 raw 0x0 0x%llx mmcpart 2",
|
|
|
|
name, devnum, mmc->capacity_boot);
|
|
|
|
first = false;
|
|
|
|
}
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2022-01-11 14:58:08 +00:00
|
|
|
for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
|
2020-03-18 08:22:46 +00:00
|
|
|
if (part_get_info(desc, p, &info))
|
|
|
|
continue;
|
|
|
|
if (!first)
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
|
|
|
|
first = false;
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s%d_%s part %d %d",
|
|
|
|
name, devnum, info.name, devnum, p);
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2020-03-18 08:22:46 +00:00
|
|
|
static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf)
|
|
|
|
{
|
|
|
|
struct mtd_info *part;
|
|
|
|
bool first = true;
|
|
|
|
const char *name;
|
|
|
|
int len, partnum = 0;
|
|
|
|
|
|
|
|
name = mtd->name;
|
|
|
|
len = strlen(buf);
|
|
|
|
|
|
|
|
if (buf[0] != '\0')
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&");
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"mtd %s=", name);
|
|
|
|
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s raw 0x0 0x%llx ",
|
|
|
|
name, mtd->size);
|
|
|
|
|
|
|
|
list_for_each_entry(part, &mtd->partitions, node) {
|
|
|
|
partnum++;
|
|
|
|
if (!first)
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
|
|
|
|
"%s_%s part %d",
|
|
|
|
name, part->name, partnum);
|
2020-03-18 08:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_dfu_alt_info(char *interface, char *devstr)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
|
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
|
|
|
|
|
|
|
|
if (env_get("dfu_alt_info"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
2020-03-18 08:22:46 +00:00
|
|
|
snprintf(buf, DFU_ALT_BUF_LEN,
|
|
|
|
"ram 0=%s", CONFIG_DFU_ALT_RAM0);
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2020-07-31 14:31:46 +00:00
|
|
|
if (CONFIG_IS_ENABLED(MMC)) {
|
|
|
|
if (!uclass_get_device(UCLASS_MMC, 0, &dev))
|
|
|
|
board_get_alt_info_mmc(dev, buf);
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2020-07-31 14:31:46 +00:00
|
|
|
if (!uclass_get_device(UCLASS_MMC, 1, &dev))
|
|
|
|
board_get_alt_info_mmc(dev, buf);
|
|
|
|
}
|
2020-03-18 08:22:46 +00:00
|
|
|
|
2023-02-05 22:40:17 +00:00
|
|
|
if (IS_ENABLED(CONFIG_MTD)) {
|
2020-03-18 08:22:46 +00:00
|
|
|
/* probe all MTD devices */
|
|
|
|
mtd_probe_devices();
|
|
|
|
|
|
|
|
/* probe SPI flash device on a bus */
|
|
|
|
if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
|
|
|
|
mtd = get_mtd_device_nm("nor0");
|
|
|
|
if (!IS_ERR_OR_NULL(mtd))
|
|
|
|
board_get_alt_info_mtd(mtd, buf);
|
2021-11-25 10:54:53 +00:00
|
|
|
|
|
|
|
mtd = get_mtd_device_nm("nor1");
|
|
|
|
if (!IS_ERR_OR_NULL(mtd))
|
|
|
|
board_get_alt_info_mtd(mtd, buf);
|
2020-03-18 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtd = get_mtd_device_nm("nand0");
|
|
|
|
if (!IS_ERR_OR_NULL(mtd))
|
|
|
|
board_get_alt_info_mtd(mtd, buf);
|
|
|
|
|
|
|
|
mtd = get_mtd_device_nm("spi-nand0");
|
|
|
|
if (!IS_ERR_OR_NULL(mtd))
|
|
|
|
board_get_alt_info_mtd(mtd, buf);
|
|
|
|
}
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2021-02-25 12:37:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_DFU_VIRT) &&
|
|
|
|
IS_ENABLED(CMD_STM32PROG_USB)) {
|
2020-07-31 14:31:46 +00:00
|
|
|
strncat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
|
2020-03-18 08:22:45 +00:00
|
|
|
|
2020-07-31 14:31:46 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PMIC_STPMIC1))
|
|
|
|
strncat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN);
|
|
|
|
}
|
2020-03-18 08:22:45 +00:00
|
|
|
|
|
|
|
env_set("dfu_alt_info", buf);
|
|
|
|
puts("DFU alt info setting: done\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_IS_ENABLED(DFU_VIRT)
|
|
|
|
#include <dfu.h>
|
|
|
|
#include <power/stpmic1.h>
|
|
|
|
|
|
|
|
static int dfu_otp_read(u64 offset, u8 *buffer, long *size)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_driver(UCLASS_MISC,
|
2020-12-29 03:34:56 +00:00
|
|
|
DM_DRIVER_GET(stm32mp_bsec),
|
2020-03-18 08:22:45 +00:00
|
|
|
&dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = misc_read(dev, offset + STM32_BSEC_OTP_OFFSET, buffer, *size);
|
|
|
|
if (ret >= 0) {
|
|
|
|
*size = ret;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dfu_pmic_read(u64 offset, u8 *buffer, long *size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
#ifdef CONFIG_PMIC_STPMIC1
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_driver(UCLASS_MISC,
|
2020-12-29 03:34:56 +00:00
|
|
|
DM_DRIVER_GET(stpmic1_nvm),
|
2020-03-18 08:22:45 +00:00
|
|
|
&dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = misc_read(dev, 0xF8 + offset, buffer, *size);
|
|
|
|
if (ret >= 0) {
|
|
|
|
*size = ret;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
if (ret == -EACCES) {
|
|
|
|
*size = 0;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#else
|
2020-11-06 18:02:00 +00:00
|
|
|
log_err("PMIC update not supported");
|
2020-03-18 08:22:45 +00:00
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset,
|
|
|
|
void *buf, long *len)
|
|
|
|
{
|
|
|
|
switch (dfu->data.virt.dev_num) {
|
|
|
|
case 0x0:
|
|
|
|
return dfu_otp_read(offset, buf, len);
|
|
|
|
case 0x1:
|
|
|
|
return dfu_pmic_read(offset, buf, len);
|
|
|
|
}
|
2020-03-18 08:24:49 +00:00
|
|
|
|
2021-02-25 12:37:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
|
2020-03-18 08:24:49 +00:00
|
|
|
dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
|
|
|
|
return stm32prog_read_medium_virt(dfu, offset, buf, len);
|
|
|
|
|
2020-03-18 08:22:45 +00:00
|
|
|
*len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:24:49 +00:00
|
|
|
int dfu_write_medium_virt(struct dfu_entity *dfu, u64 offset,
|
|
|
|
void *buf, long *len)
|
|
|
|
{
|
2021-02-25 12:37:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
|
2020-03-18 08:24:49 +00:00
|
|
|
dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
|
|
|
|
return stm32prog_write_medium_virt(dfu, offset, buf, len);
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:22:45 +00:00
|
|
|
int __weak dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
|
|
|
|
{
|
2021-02-25 12:37:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
|
2020-03-18 08:24:49 +00:00
|
|
|
dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
|
|
|
|
return stm32prog_get_medium_size_virt(dfu, size);
|
|
|
|
|
2020-03-18 08:22:45 +00:00
|
|
|
*size = SZ_1K;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|