From 63d23445abf0b7fb0cc43941c79efec7453d1611 Mon Sep 17 00:00:00 2001 From: Vaishnav Achath Date: Thu, 13 Jul 2023 00:45:24 +0530 Subject: [PATCH] board: ti: common: Add support for dynamic dfu_alt_info Add support for setting dfu_alt_info environment variable dynamically for SPI flash devices, the dfu_alt_info is populated from existing mtd partitions, there is no modification made to the environment variable if it already is defined. Signed-off-by: Vaishnav Achath --- board/ti/common/Makefile | 1 + board/ti/common/k3_dfu.c | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 board/ti/common/k3_dfu.c diff --git a/board/ti/common/Makefile b/board/ti/common/Makefile index 3172d87b46..515d8c2e8d 100644 --- a/board/ti/common/Makefile +++ b/board/ti/common/Makefile @@ -3,3 +3,4 @@ obj-${CONFIG_TI_I2C_BOARD_DETECT} += board_detect.o obj-${CONFIG_CMD_EXTENSION} += cape_detect.o +obj-$(CONFIG_SET_DFU_ALT_INFO) += k3_dfu.o diff --git a/board/ti/common/k3_dfu.c b/board/ti/common/k3_dfu.c new file mode 100644 index 0000000000..5954590944 --- /dev/null +++ b/board/ti/common/k3_dfu.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2023, Texas Instruments Incorporated - https://www.ti.com/ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DFU_ALT_BUF_LEN 512 + +static void board_get_alt_info_sf(struct mtd_info *mtd, char *buf) +{ + struct mtd_info *part; + bool first = true; + const char *name; + int len; + + name = mtd->name; + len = strlen(buf); + + list_for_each_entry(part, &mtd->partitions, node) { + if (!first) + len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "; "); + first = false; + len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, + "%s raw %llx %llx", part->name, part->offset, part->size); + } +} + +void set_dfu_alt_info(char *interface, char *devstr) +{ + struct udevice *dev; + struct mtd_info *mtd; + char *st, *devstr_bkup; + unsigned int bus; + + ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN); + + if (env_get("dfu_alt_info")) + return; + + memset(buf, 0, sizeof(buf)); + + if (!strcmp(interface, "sf")) { + devstr_bkup = strdup(devstr); + st = strsep(&devstr_bkup, ":"); + if (!st || !*st) { + printf("Invalid SPI bus %s\n", st); + return; + } + bus = simple_strtoul(st, NULL, 0); + + if (uclass_get_device(UCLASS_SPI_FLASH, bus, &dev)) { + printf("Failed to get device on bus %d\n", bus); + return; + } + if (CONFIG_IS_ENABLED(MTD)) { + mtd_probe_devices(); + mtd_for_each_device(mtd) { + if (!mtd_is_partition(mtd) && mtd->dev && dev == mtd->dev) + board_get_alt_info_sf(mtd, buf); + } + } + } else { + printf("dynamic dfu_alt_info supported only for sf\n"); + return; + } + + env_set("dfu_alt_info", buf); +}