1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
10 #include <asm/arch/stm32prog.h>
11 #include "stm32prog.h"
13 struct stm32prog_data *stm32prog_data;
15 static void enable_vidconsole(void)
17 #ifdef CONFIG_DM_VIDEO
21 stdname = env_get("stdout");
22 if (!stdname || !strstr(stdname, "vidconsole")) {
24 snprintf(buf, sizeof(buf), "serial,vidconsole");
26 snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
27 env_set("stdout", buf);
30 stdname = env_get("stderr");
31 if (!stdname || !strstr(stdname, "vidconsole")) {
33 snprintf(buf, sizeof(buf), "serial,vidconsole");
35 snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
36 env_set("stderr", buf);
41 static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc,
46 enum stm32prog_link_t link = LINK_UNDEFINED;
48 struct image_header_s header;
49 struct stm32prog_data *data;
52 if (argc < 3 || argc > 5)
55 if (!strcmp(argv[1], "usb"))
57 else if (!strcmp(argv[1], "serial"))
60 if (link == LINK_UNDEFINED) {
61 pr_err("not supported link=%s\n", argv[1]);
65 dev = (int)simple_strtoul(argv[2], NULL, 10);
67 addr = STM32_DDR_BASE;
70 addr = simple_strtoul(argv[3], NULL, 16);
72 return CMD_RET_FAILURE;
75 size = simple_strtoul(argv[4], NULL, 16);
77 /* check STM32IMAGE presence */
79 !stm32prog_header_check((struct raw_header_s *)addr, &header)) {
80 size = header.image_length + BL_HEADER_SIZE;
82 /* uImage detected in STM32IMAGE, execute the script */
83 if (IMAGE_FORMAT_LEGACY ==
84 genimg_get_format((void *)(addr + BL_HEADER_SIZE)))
85 return image_source_script(addr + BL_HEADER_SIZE,
91 data = (struct stm32prog_data *)malloc(sizeof(*data));
94 pr_err("Alloc failed.");
95 return CMD_RET_FAILURE;
97 stm32prog_data = data;
99 ret = stm32prog_init(data, addr, size);
101 printf("Invalid or missing layout file.");
103 /* prepare DFU for device read/write */
104 ret = stm32prog_dfu_init(data);
110 ret = stm32prog_serial_init(data, dev);
113 reset = stm32prog_serial_loop(data);
116 reset = stm32prog_usb_loop(data, dev);
122 uimage = data->uimage;
125 stm32prog_clean(data);
126 free(stm32prog_data);
127 stm32prog_data = NULL;
129 puts("Download done\n");
132 char boot_addr_start[20];
134 char *bootm_argv[5] = {
135 "bootm", boot_addr_start, "-", dtb_addr, NULL
138 bootm_argv[3] = env_get("fdtcontroladdr");
140 snprintf(dtb_addr, sizeof(dtb_addr) - 1,
143 snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
145 printf("Booting kernel at %s - %s...\n\n\n",
146 boot_addr_start, bootm_argv[3]);
147 /* Try bootm for legacy and FIT format image */
148 if (genimg_get_format((void *)uimage) != IMAGE_FORMAT_INVALID)
149 do_bootm(cmdtp, 0, 4, bootm_argv);
150 else if CONFIG_IS_ENABLED(CMD_BOOTZ)
151 do_bootz(cmdtp, 0, 4, bootm_argv);
156 run_command("reset", 0);
159 return CMD_RET_SUCCESS;
162 stm32prog_clean(data);
163 free(stm32prog_data);
164 stm32prog_data = NULL;
166 return CMD_RET_FAILURE;
169 U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog,
170 "<link> <dev> [<addr>] [<size>]\n"
171 "start communication with tools STM32Cubeprogrammer on <link> with Flashlayout at <addr>",
172 "<link> = serial|usb\n"
173 "<dev> = device instance\n"
174 "<addr> = address of flashlayout\n"
175 "<size> = size of flashlayout\n"
178 bool stm32prog_get_tee_partitions(void)
181 return stm32prog_data->tee_detected;
186 bool stm32prog_get_fsbl_nor(void)
189 return stm32prog_data->fsbl_nor_detected;