2020-03-18 08:24:49 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020, STMicroelectronics - All Rights Reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <dfu.h>
|
2020-03-18 08:25:02 +00:00
|
|
|
#include <image.h>
|
2020-03-18 08:24:55 +00:00
|
|
|
#include <asm/arch/stm32prog.h>
|
2020-03-18 08:24:49 +00:00
|
|
|
#include "stm32prog.h"
|
|
|
|
|
|
|
|
struct stm32prog_data *stm32prog_data;
|
|
|
|
|
2020-03-18 08:25:01 +00:00
|
|
|
static void enable_vidconsole(void)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc,
|
2020-03-18 08:24:49 +00:00
|
|
|
char * const argv[])
|
|
|
|
{
|
|
|
|
ulong addr, size;
|
|
|
|
int dev, ret;
|
|
|
|
enum stm32prog_link_t link = LINK_UNDEFINED;
|
|
|
|
bool reset = false;
|
2020-03-18 08:25:02 +00:00
|
|
|
struct image_header_s header;
|
2020-03-18 08:24:49 +00:00
|
|
|
struct stm32prog_data *data;
|
|
|
|
|
|
|
|
if (argc < 3 || argc > 5)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
2021-02-25 12:37:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && !strcmp(argv[1], "usb"))
|
2020-03-18 08:24:49 +00:00
|
|
|
link = LINK_USB;
|
2021-02-25 12:37:01 +00:00
|
|
|
else if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && !strcmp(argv[1], "serial"))
|
2020-03-18 08:25:00 +00:00
|
|
|
link = LINK_SERIAL;
|
2020-03-18 08:24:49 +00:00
|
|
|
|
|
|
|
if (link == LINK_UNDEFINED) {
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("not supported link=%s\n", argv[1]);
|
2020-03-18 08:24:49 +00:00
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2020-03-18 08:25:00 +00:00
|
|
|
|
2021-07-24 15:03:30 +00:00
|
|
|
dev = (int)dectoul(argv[2], NULL);
|
2020-03-18 08:24:49 +00:00
|
|
|
|
2022-09-06 16:53:18 +00:00
|
|
|
addr = CONFIG_SYS_LOAD_ADDR;
|
2020-03-18 08:24:49 +00:00
|
|
|
size = 0;
|
|
|
|
if (argc > 3) {
|
2021-07-24 15:03:29 +00:00
|
|
|
addr = hextoul(argv[3], NULL);
|
2020-03-18 08:24:49 +00:00
|
|
|
if (!addr)
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
if (argc > 4)
|
2021-07-24 15:03:29 +00:00
|
|
|
size = hextoul(argv[4], NULL);
|
2020-03-18 08:24:49 +00:00
|
|
|
|
2020-03-18 08:25:02 +00:00
|
|
|
/* check STM32IMAGE presence */
|
2021-04-02 12:05:17 +00:00
|
|
|
if (size == 0) {
|
2022-03-28 17:25:29 +00:00
|
|
|
stm32prog_header_check(addr, &header);
|
2021-04-02 12:05:17 +00:00
|
|
|
if (header.type == HEADER_STM32IMAGE) {
|
2022-03-28 17:25:29 +00:00
|
|
|
size = header.image_length + header.length;
|
2021-04-02 12:05:17 +00:00
|
|
|
}
|
2020-03-18 08:25:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 13:46:31 +00:00
|
|
|
if (IS_ENABLED(CONFIG_VIDEO))
|
2020-07-31 14:31:52 +00:00
|
|
|
enable_vidconsole();
|
2020-03-18 08:25:01 +00:00
|
|
|
|
2020-03-18 08:24:49 +00:00
|
|
|
data = (struct stm32prog_data *)malloc(sizeof(*data));
|
|
|
|
|
|
|
|
if (!data) {
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("Alloc failed.");
|
2020-03-18 08:24:49 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
stm32prog_data = data;
|
|
|
|
|
|
|
|
ret = stm32prog_init(data, addr, size);
|
|
|
|
if (ret)
|
2021-05-18 13:12:11 +00:00
|
|
|
log_debug("Invalid or missing layout file at 0x%lx.\n", addr);
|
2020-03-18 08:24:49 +00:00
|
|
|
|
|
|
|
/* prepare DFU for device read/write */
|
|
|
|
ret = stm32prog_dfu_init(data);
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
switch (link) {
|
2020-03-18 08:25:00 +00:00
|
|
|
case LINK_SERIAL:
|
|
|
|
ret = stm32prog_serial_init(data, dev);
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
reset = stm32prog_serial_loop(data);
|
|
|
|
break;
|
2020-03-18 08:24:49 +00:00
|
|
|
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");
|
2020-03-18 08:25:03 +00:00
|
|
|
|
2021-05-18 13:12:06 +00:00
|
|
|
if (data->uimage) {
|
2020-03-18 08:25:03 +00:00
|
|
|
char boot_addr_start[20];
|
|
|
|
char dtb_addr[20];
|
2021-05-18 13:12:06 +00:00
|
|
|
char initrd_addr[40];
|
2020-03-18 08:25:03 +00:00
|
|
|
char *bootm_argv[5] = {
|
|
|
|
"bootm", boot_addr_start, "-", dtb_addr, NULL
|
|
|
|
};
|
2022-09-06 16:53:19 +00:00
|
|
|
const void *uimage = (void *)data->uimage;
|
|
|
|
const void *dtb = (void *)data->dtb;
|
|
|
|
const void *initrd = (void *)data->initrd;
|
2021-05-18 13:12:06 +00:00
|
|
|
|
2020-03-18 08:25:03 +00:00
|
|
|
if (!dtb)
|
|
|
|
bootm_argv[3] = env_get("fdtcontroladdr");
|
|
|
|
else
|
|
|
|
snprintf(dtb_addr, sizeof(dtb_addr) - 1,
|
2022-09-06 16:53:19 +00:00
|
|
|
"0x%p", dtb);
|
2020-03-18 08:25:03 +00:00
|
|
|
|
|
|
|
snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
|
2022-09-06 16:53:19 +00:00
|
|
|
"0x%p", uimage);
|
2021-05-18 13:12:06 +00:00
|
|
|
|
|
|
|
if (initrd) {
|
2022-09-06 16:53:19 +00:00
|
|
|
snprintf(initrd_addr, sizeof(initrd_addr) - 1, "0x%p:0x%zx",
|
2021-05-18 13:12:06 +00:00
|
|
|
initrd, data->initrd_size);
|
|
|
|
bootm_argv[2] = initrd_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Booting kernel at %s %s %s...\n\n\n",
|
|
|
|
boot_addr_start, bootm_argv[2], bootm_argv[3]);
|
2020-03-18 08:25:03 +00:00
|
|
|
/* Try bootm for legacy and FIT format image */
|
2022-09-06 16:53:19 +00:00
|
|
|
if (genimg_get_format(uimage) != IMAGE_FORMAT_INVALID)
|
2020-03-18 08:25:03 +00:00
|
|
|
do_bootm(cmdtp, 0, 4, bootm_argv);
|
2023-02-05 22:36:25 +00:00
|
|
|
else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
|
2020-03-18 08:25:03 +00:00
|
|
|
do_bootz(cmdtp, 0, 4, bootm_argv);
|
|
|
|
}
|
2022-03-28 17:25:32 +00:00
|
|
|
if (data->script)
|
2023-01-06 14:52:28 +00:00
|
|
|
cmd_source_script(data->script, NULL, NULL);
|
2020-03-18 08:25:03 +00:00
|
|
|
|
2020-03-18 08:24:49 +00:00
|
|
|
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,
|
2021-06-28 12:44:33 +00:00
|
|
|
"start communication with tools STM32Cubeprogrammer",
|
2020-03-18 08:24:49 +00:00
|
|
|
"<link> <dev> [<addr>] [<size>]\n"
|
2021-06-28 12:44:33 +00:00
|
|
|
" <link> = serial|usb\n"
|
|
|
|
" <dev> = device instance\n"
|
|
|
|
" <addr> = address of flashlayout\n"
|
|
|
|
" <size> = size of flashlayout (optional for image with STM32 header)\n"
|
2020-03-18 08:24:49 +00:00
|
|
|
);
|
2020-03-18 08:24:55 +00:00
|
|
|
|
2021-07-26 09:21:38 +00:00
|
|
|
#ifdef CONFIG_STM32MP15x_STM32IMAGE
|
2020-03-18 08:24:55 +00:00
|
|
|
bool stm32prog_get_tee_partitions(void)
|
|
|
|
{
|
|
|
|
if (stm32prog_data)
|
|
|
|
return stm32prog_data->tee_detected;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-26 09:21:38 +00:00
|
|
|
#endif
|
2020-03-18 08:24:55 +00:00
|
|
|
|
|
|
|
bool stm32prog_get_fsbl_nor(void)
|
|
|
|
{
|
|
|
|
if (stm32prog_data)
|
|
|
|
return stm32prog_data->fsbl_nor_detected;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|