From 954bd1a923a686089c62195781616768f52eacc7 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:49 +0100 Subject: stm32mp: add the command stm32prog Add a specific command stm32prog for STM32MP soc family witch allows to program the boot devices with the tool STM32CubeProgrammer (http://www.st.com/STM32CubeProg). This command uses the same UART STM32 protocol than MCU STM32 with or USB with DFU protocol v1.1 (ithe MCU ST extension are no supported). The executed actions are based on a tab separated value file with a stm32 header, the FlashLayout file (https://wiki.st.com/stm32mpu/wiki/STM32CubeProgrammer_flashlayout). This file is parsed by the U-Boot command to: - initialize the devices - create the partition table on each device - initialize the DFU backend to access to not volatile memory (NOR/NAND/SD/eMMC) or to virtual device (OTP/PMIC) Up to STM32PROG_MAX_DEV (5) devices can be updated with a FlashLayout. The communication between U-Boot and STM32CubeProgrammer is done with the specific alternate configuration (see "AN5275: USB DFU/USART protocols used in STM32MP1 Series bootloaders" for details). The command stm32prog is executed when a boot from USB is detected (selected with bootpins) and we can program the boot devices with a simple command (on Windows or Linux): PC $> STM32_Programmer_CLI -c port=usb1 -w flaslayout.tsv 1/ the ROM code loads TF-A in embedded RAM (DFU or uart) 2/ TF-A loads flashlayout file and U-Boot in DDR (DFU or uart) 3/ U-Boot executes the stm32prog command (DFU or uart) Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard Reviewed-by: Patrice Chotard --- .../arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c new file mode 100644 index 00000000000..3e8b426444d --- /dev/null +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause +/* + * Copyright (C) 2020, STMicroelectronics - All Rights Reserved + */ + +#include +#include +#include +#include "stm32prog.h" + +struct stm32prog_data *stm32prog_data; + +static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + ulong addr, size; + int dev, ret; + enum stm32prog_link_t link = LINK_UNDEFINED; + bool reset = false; + struct stm32prog_data *data; + + if (argc < 3 || argc > 5) + return CMD_RET_USAGE; + + if (!strcmp(argv[1], "usb")) + link = LINK_USB; + + if (link == LINK_UNDEFINED) { + pr_err("not supported link=%s\n", argv[1]); + return CMD_RET_USAGE; + } + dev = (int)simple_strtoul(argv[2], NULL, 10); + + addr = STM32_DDR_BASE; + size = 0; + if (argc > 3) { + addr = simple_strtoul(argv[3], NULL, 16); + if (!addr) + return CMD_RET_FAILURE; + } + if (argc > 4) + size = simple_strtoul(argv[4], NULL, 16); + + data = (struct stm32prog_data *)malloc(sizeof(*data)); + + if (!data) { + pr_err("Alloc failed."); + return CMD_RET_FAILURE; + } + stm32prog_data = data; + + ret = stm32prog_init(data, addr, size); + if (ret) + printf("Invalid or missing layout file."); + + /* prepare DFU for device read/write */ + ret = stm32prog_dfu_init(data); + if (ret) + goto cleanup; + + switch (link) { + case LINK_USB: + reset = stm32prog_usb_loop(data, dev); + break; + default: + goto cleanup; + } + + stm32prog_clean(data); + free(stm32prog_data); + stm32prog_data = NULL; + + puts("Download done\n"); + if (reset) { + puts("Reset...\n"); + run_command("reset", 0); + } + + return CMD_RET_SUCCESS; + +cleanup: + stm32prog_clean(data); + free(stm32prog_data); + stm32prog_data = NULL; + + return CMD_RET_FAILURE; +} + +U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, + " [] []\n" + "start communication with tools STM32Cubeprogrammer on with Flashlayout at ", + " = usb\n" + " = device instance\n" + " = address of flashlayout\n" + " = size of flashlayout\n" +); -- cgit v1.2.3 From 8f035f7b48f79460d296824b37b69dd5cc0b9567 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:55 +0100 Subject: stm32mp: stm32prog: adapt the MTD partitions Dynamically adapt the MTD partitions in NOR/NAND/SPI-NAND when stm32prog command detects in the parsed flash layout files: - a fsbl partition in NOR. - a tee partition in NOR/NAND/SPI-NAND Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index 3e8b426444d..581f97e0b50 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "stm32prog.h" struct stm32prog_data *stm32prog_data; @@ -94,3 +95,19 @@ U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, " = address of flashlayout\n" " = size of flashlayout\n" ); + +bool stm32prog_get_tee_partitions(void) +{ + if (stm32prog_data) + return stm32prog_data->tee_detected; + + return false; +} + +bool stm32prog_get_fsbl_nor(void) +{ + if (stm32prog_data) + return stm32prog_data->fsbl_nor_detected; + + return false; +} -- cgit v1.2.3 From 468f0508b58b02943942c47b66645a05244f2bbf Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:00 +0100 Subject: stm32mp: stm32prog: add serial link support Add a support of UART, using the same protocol than MCU STM32. See "AN5275: USB DFU/USART protocols used in STM32MP1 Series bootloaders" for details. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index 581f97e0b50..1769ba05f28 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -25,11 +25,14 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, if (!strcmp(argv[1], "usb")) link = LINK_USB; + else if (!strcmp(argv[1], "serial")) + link = LINK_SERIAL; if (link == LINK_UNDEFINED) { pr_err("not supported link=%s\n", argv[1]); return CMD_RET_USAGE; } + dev = (int)simple_strtoul(argv[2], NULL, 10); addr = STM32_DDR_BASE; @@ -60,6 +63,12 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, goto cleanup; switch (link) { + case LINK_SERIAL: + ret = stm32prog_serial_init(data, dev); + if (ret) + goto cleanup; + reset = stm32prog_serial_loop(data); + break; case LINK_USB: reset = stm32prog_usb_loop(data, dev); break; @@ -90,7 +99,7 @@ cleanup: U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, " [] []\n" "start communication with tools STM32Cubeprogrammer on with Flashlayout at ", - " = usb\n" + " = serial|usb\n" " = device instance\n" " = address of flashlayout\n" " = size of flashlayout\n" -- cgit v1.2.3 From 99d643cb411775037453bf24cac6b8eb0917bfe7 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:01 +0100 Subject: stm32mp: stm32prog: enable videoconsole Enable the videoconsole during the stm32prog command execution to have information without UART. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- .../arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index 1769ba05f28..15bbdc2cb65 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -11,6 +11,32 @@ struct stm32prog_data *stm32prog_data; +static void enable_vidconsole(void) +{ +#ifdef CONFIG_DM_VIDEO + char *stdname; + char buf[64]; + + stdname = env_get("stdout"); + if (!stdname || !strstr(stdname, "vidconsole")) { + if (!stdname) + snprintf(buf, sizeof(buf), "serial,vidconsole"); + else + snprintf(buf, sizeof(buf), "%s,vidconsole", stdname); + env_set("stdout", buf); + } + + stdname = env_get("stderr"); + if (!stdname || !strstr(stdname, "vidconsole")) { + if (!stdname) + snprintf(buf, sizeof(buf), "serial,vidconsole"); + else + snprintf(buf, sizeof(buf), "%s,vidconsole", stdname); + env_set("stderr", buf); + } +#endif +} + static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { @@ -45,6 +71,8 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, if (argc > 4) size = simple_strtoul(argv[4], NULL, 16); + enable_vidconsole(); + data = (struct stm32prog_data *)malloc(sizeof(*data)); if (!data) { -- cgit v1.2.3 From d0686c69ff6cb8d8e63052bde8ca00e6468dd88d Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:02 +0100 Subject: stm32mp: stm32prog: support for script Support an U-Boot script included in uimage instead of flashlayout file (text file in tsv format). This feature is used to execute this script directly when U-Boot is loaded in DDR (for update without STM32CubeProgrammer for example). A simple example with dfu-util only is: $> echo "dfu 0" > script.cmd $> mkimage -C none -A arm -T script -d script.cmd script.uimg $> mkimage -T stm32image -a 0xC0000000 -e 0xC0000000 -d script.uimg \ script.stm32 $> dfu-util -d 0483:df11 -a 1 -D tf-a.stm32 $> dfu-util -d 0483:df11 -a 0 -D script.stm32 $> dfu-util -d 0483:df11 -a 0 -D u-boot.stm32 $> dfu-util -d 0483:df11 -a 0 -e Then you can used dfu-utils to update your device To increase speed, you can also switch to fastboot protocol with: echo "fastboot 0" > script.cmd Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index 15bbdc2cb65..baf9b6bd1ea 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "stm32prog.h" @@ -44,6 +45,7 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, int dev, ret; enum stm32prog_link_t link = LINK_UNDEFINED; bool reset = false; + struct image_header_s header; struct stm32prog_data *data; if (argc < 3 || argc > 5) @@ -71,6 +73,18 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, if (argc > 4) size = simple_strtoul(argv[4], NULL, 16); + /* check STM32IMAGE presence */ + if (size == 0 && + !stm32prog_header_check((struct raw_header_s *)addr, &header)) { + size = header.image_length + BL_HEADER_SIZE; + + /* uImage detected in STM32IMAGE, execute the script */ + if (IMAGE_FORMAT_LEGACY == + genimg_get_format((void *)(addr + BL_HEADER_SIZE))) + return image_source_script(addr + BL_HEADER_SIZE, + "script@1"); + } + enable_vidconsole(); data = (struct stm32prog_data *)malloc(sizeof(*data)); -- cgit v1.2.3 From 306a5cf24f30b9eea1c7ca04bba4af11c097b2d9 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:03 +0100 Subject: stm32mp: stm32prog: add support of RAM target Add support of RAM target in flashlayout to load kernel image ("system") and device tree ("filesystem") in DDR with DFU and start these images. The flashlayout.tsv is: - 0x01 fsbl Binary none 0x00000000 tf-a.stm32 - 0x03 ssbl Binary none 0x00000000 u-boot.stm32 P 0x10 kernel System ram0 0xC2000000 uImage.bin P 0x11 dtb FileSystem ram0 0xC4000000 dtb.bin Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- .../arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index baf9b6bd1ea..6bebea7ad5e 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -47,6 +47,7 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, bool reset = false; struct image_header_s header; struct stm32prog_data *data; + u32 uimage, dtb; if (argc < 3 || argc > 5) return CMD_RET_USAGE; @@ -118,11 +119,38 @@ static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, goto cleanup; } + uimage = data->uimage; + dtb = data->dtb; + stm32prog_clean(data); free(stm32prog_data); stm32prog_data = NULL; puts("Download done\n"); + + if (uimage) { + char boot_addr_start[20]; + char dtb_addr[20]; + char *bootm_argv[5] = { + "bootm", boot_addr_start, "-", dtb_addr, NULL + }; + if (!dtb) + bootm_argv[3] = env_get("fdtcontroladdr"); + else + snprintf(dtb_addr, sizeof(dtb_addr) - 1, + "0x%x", dtb); + + snprintf(boot_addr_start, sizeof(boot_addr_start) - 1, + "0x%x", uimage); + printf("Booting kernel at %s - %s...\n\n\n", + boot_addr_start, bootm_argv[3]); + /* Try bootm for legacy and FIT format image */ + if (genimg_get_format((void *)uimage) != IMAGE_FORMAT_INVALID) + do_bootm(cmdtp, 0, 4, bootm_argv); + else if CONFIG_IS_ENABLED(CMD_BOOTZ) + do_bootz(cmdtp, 0, 4, bootm_argv); + } + if (reset) { puts("Reset...\n"); run_command("reset", 0); -- cgit v1.2.3