stm32mp1: dynamically build DFU_ALT_INFO
authorPatrick Delaunay <patrick.delaunay@st.com>
Wed, 18 Mar 2020 08:22:46 +0000 (09:22 +0100)
committerPatrick Delaunay <patrick.delaunay@st.com>
Thu, 14 May 2020 07:02:12 +0000 (09:02 +0200)
This patch reduces the stm32mp1 environment size and
builds dynamically the DFU board configuration with gpt
and mtd partitions and information from defconfig
(CONFIG_DFU_ALT_RAM0).

Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
Reviewed-by: Patrice Chotard <patrice.chotard@st.com>
board/dhelectronics/dh_stm32mp1/Kconfig
board/st/common/Kconfig
board/st/common/stm32mp_dfu.c
include/configs/stm32mp1.h

index 0a839f2546e6d33abc7ab3866a198348c8016603..1fc792c9d1ae08fe730b52d174b3c5d889070a77 100644 (file)
@@ -18,4 +18,5 @@ config ENV_OFFSET
 config ENV_OFFSET_REDUND
        default 0x1F0000 if ENV_IS_IN_SPI_FLASH
 
+source "board/st/common/Kconfig"
 endif
index af01ca4891cbfe0059167ecdbab64f0248104bde..08df8459827b05c4ef4090d2d0a81e17ed7842eb 100644 (file)
@@ -5,3 +5,10 @@ config CMD_STBOARD
        help
          This compile the stboard command to
          read and write the board in the OTP.
+
+config DFU_ALT_RAM0
+       string "dfu for ram0"
+       default "uImage ram 0xc2000000 0x2000000;devicetree.dtb ram 0xc4000000 0x100000;uramdisk.image.gz ram 0xc4400000 0x10000000"
+       depends on ARCH_STM32MP && SET_DFU_ALT_INFO
+       help
+         This defines the partitions of ram used to build dfu dynamically.
index 99ea21ce15f55be678e921b2b0baea9869c3a126..e129f8c8b5b7f3661921475881574edf0ca73bdd 100644 (file)
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <blk.h>
 #include <dfu.h>
 #include <env.h>
 #include <memalign.h>
 
 #define DFU_ALT_BUF_LEN SZ_1K
 
-static void board_get_alt_info(const char *dev, char *buff)
+static void board_get_alt_info_mmc(struct udevice *dev, char *buf)
 {
-       char var_name[32] = "dfu_alt_info_";
-       int ret;
+       disk_partition_t info;
+       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;
+
+       name = blk_get_if_type_name(desc->if_type);
+       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;
+       }
 
-       ALLOC_CACHE_ALIGN_BUFFER(char, tmp_alt, DFU_ALT_BUF_LEN);
+       for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) {
+               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);
+       }
+}
 
-       /* name of env variable to read = dfu_alt_info_<dev> */
-       strcat(var_name, dev);
-       ret = env_get_f(var_name, tmp_alt, DFU_ALT_BUF_LEN);
-       if (ret) {
-               if (buff[0] != '\0')
-                       strcat(buff, "&");
-               strncat(buff, tmp_alt, DFU_ALT_BUF_LEN);
+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);
        }
 }
 
@@ -42,27 +109,34 @@ void set_dfu_alt_info(char *interface, char *devstr)
 
        memset(buf, 0, sizeof(buf));
 
-       /* probe all MTD devices */
-       mtd_probe_devices();
-
-       board_get_alt_info("ram", buf);
+       snprintf(buf, DFU_ALT_BUF_LEN,
+                "ram 0=%s", CONFIG_DFU_ALT_RAM0);
 
        if (!uclass_get_device(UCLASS_MMC, 0, &dev))
-               board_get_alt_info("mmc0", buf);
+               board_get_alt_info_mmc(dev, buf);
 
        if (!uclass_get_device(UCLASS_MMC, 1, &dev))
-               board_get_alt_info("mmc1", buf);
-
-       if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
-               board_get_alt_info("nor0", buf);
-
-       mtd = get_mtd_device_nm("nand0");
-       if (!IS_ERR_OR_NULL(mtd))
-               board_get_alt_info("nand0", buf);
-
-       mtd = get_mtd_device_nm("spi-nand0");
-       if (!IS_ERR_OR_NULL(mtd))
-               board_get_alt_info("spi-nand0", buf);
+               board_get_alt_info_mmc(dev, buf);
+
+       if (CONFIG_IS_ENABLED(MTD)) {
+               /* 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);
+               }
+
+               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);
+       }
 
 #ifdef CONFIG_DFU_VIRT
        strncat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
index be5afe9c6fa2adbcdcca20f93b293f9256c4f2e8..0319ebf4d87d783f1b6e63f1089b9080ab3e8df5 100644 (file)
 #define STM32MP_MTDPARTS
 #endif
 
-#define STM32MP_DFU_ALT_RAM \
-       "dfu_alt_info_ram=ram 0=" \
-               "uImage ram ${kernel_addr_r} 0x2000000;" \
-               "devicetree.dtb ram ${fdt_addr_r} 0x100000;" \
-               "uramdisk.image.gz ram ${ramdisk_addr_r} 0x10000000\0"
-
-#ifdef CONFIG_SET_DFU_ALT_INFO
-#define STM32MP_DFU_ALT_INFO \
-       "dfu_alt_info_nor0=mtd nor0=" \
-               "nor_fsbl1 part 1;nor_fsbl2 part 2;" \
-               "nor_ssbl part 3;nor_env part 4\0" \
-       "dfu_alt_info_nand0=mtd nand0="\
-               "nand_fsbl part 1;nand_ssbl1 part 2;" \
-               "nand_ssbl2 part 3;nand_UBI partubi 4\0" \
-       "dfu_alt_info_spi-nand0=mtd spi-nand0="\
-               "spi-nand_fsbl part 1;spi-nand_ssbl1 part 2;" \
-               "spi-nand_ssbl2 part 3;spi-nand_UBI partubi 4\0" \
-       "dfu_alt_info_mmc0=mmc 0=" \
-               "sdcard_fsbl1 part 0 1;sdcard_fsbl2 part 0 2;" \
-               "sdcard_ssbl part 0 3;sdcard_bootfs part 0 4;" \
-               "sdcard_vendorfs part 0 5;sdcard_rootfs part 0 6;" \
-               "sdcard_userfs part 0 7\0" \
-       "dfu_alt_info_mmc1=mmc 1=" \
-               "emmc_fsbl1 raw 0x0 0x200 mmcpart 1;" \
-               "emmc_fsbl2 raw 0x0 0x200 mmcpart 2;emmc_ssbl part 1 1;" \
-               "emmc_bootfs part 1 2;emmc_vendorfs part 1 3;" \
-               "emmc_rootfs part 1 4;emmc_userfs part 1 5\0"
-#else
-#define STM32MP_DFU_ALT_INFO
-#endif
-
 /*
  * memory layout for 32M uncompressed/compressed kernel,
  * 1M fdt, 1M script, 1M pxe and 1M for splashimage
                " then env set env_default 0;env save;fi\0" \
        STM32MP_BOOTCMD \
        STM32MP_MTDPARTS \
-       STM32MP_DFU_ALT_RAM \
-       STM32MP_DFU_ALT_INFO \
        BOOTENV \
        "boot_net_usb_start=true\0"