1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <dfu.h>
9 #include <image.h>
10 #include <asm/arch/stm32prog.h>
11 #include "stm32prog.h"
12 
13 struct stm32prog_data *stm32prog_data;
14 
enable_vidconsole(void)15 static void enable_vidconsole(void)
16 {
17 	char *stdname;
18 	char buf[64];
19 
20 	stdname = env_get("stdout");
21 	if (!stdname || !strstr(stdname, "vidconsole")) {
22 		if (!stdname)
23 			snprintf(buf, sizeof(buf), "serial,vidconsole");
24 		else
25 			snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
26 		env_set("stdout", buf);
27 	}
28 
29 	stdname = env_get("stderr");
30 	if (!stdname || !strstr(stdname, "vidconsole")) {
31 		if (!stdname)
32 			snprintf(buf, sizeof(buf), "serial,vidconsole");
33 		else
34 			snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
35 		env_set("stderr", buf);
36 	}
37 }
38 
do_stm32prog(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])39 static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc,
40 			char * const argv[])
41 {
42 	ulong	addr, size;
43 	int dev, ret;
44 	enum stm32prog_link_t link = LINK_UNDEFINED;
45 	bool reset = false;
46 	struct image_header_s header;
47 	struct stm32prog_data *data;
48 	u32 uimage, dtb;
49 
50 	if (argc < 3 ||  argc > 5)
51 		return CMD_RET_USAGE;
52 
53 	if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && !strcmp(argv[1], "usb"))
54 		link = LINK_USB;
55 	else if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && !strcmp(argv[1], "serial"))
56 		link = LINK_SERIAL;
57 
58 	if (link == LINK_UNDEFINED) {
59 		log_err("not supported link=%s\n", argv[1]);
60 		return CMD_RET_USAGE;
61 	}
62 
63 	dev = (int)simple_strtoul(argv[2], NULL, 10);
64 
65 	addr = STM32_DDR_BASE;
66 	size = 0;
67 	if (argc > 3) {
68 		addr = simple_strtoul(argv[3], NULL, 16);
69 		if (!addr)
70 			return CMD_RET_FAILURE;
71 	}
72 	if (argc > 4)
73 		size = simple_strtoul(argv[4], NULL, 16);
74 
75 	/* check STM32IMAGE presence */
76 	if (size == 0 &&
77 	    !stm32prog_header_check((struct raw_header_s *)addr, &header)) {
78 		size = header.image_length + BL_HEADER_SIZE;
79 
80 		/* uImage detected in STM32IMAGE, execute the script */
81 		if (IMAGE_FORMAT_LEGACY ==
82 		    genimg_get_format((void *)(addr + BL_HEADER_SIZE)))
83 			return image_source_script(addr + BL_HEADER_SIZE,
84 						   "script@1");
85 	}
86 
87 	if (IS_ENABLED(CONFIG_DM_VIDEO))
88 		enable_vidconsole();
89 
90 	data = (struct stm32prog_data *)malloc(sizeof(*data));
91 
92 	if (!data) {
93 		log_err("Alloc failed.");
94 		return CMD_RET_FAILURE;
95 	}
96 	stm32prog_data = data;
97 
98 	ret = stm32prog_init(data, addr, size);
99 	if (ret)
100 		printf("Invalid or missing layout file.");
101 
102 	/* prepare DFU for device read/write */
103 	ret = stm32prog_dfu_init(data);
104 	if (ret)
105 		goto cleanup;
106 
107 	switch (link) {
108 	case LINK_SERIAL:
109 		ret = stm32prog_serial_init(data, dev);
110 		if (ret)
111 			goto cleanup;
112 		reset = stm32prog_serial_loop(data);
113 		break;
114 	case LINK_USB:
115 		reset = stm32prog_usb_loop(data, dev);
116 		break;
117 	default:
118 		goto cleanup;
119 	}
120 
121 	uimage = data->uimage;
122 	dtb = data->dtb;
123 
124 	stm32prog_clean(data);
125 	free(stm32prog_data);
126 	stm32prog_data = NULL;
127 
128 	puts("Download done\n");
129 
130 	if (uimage) {
131 		char boot_addr_start[20];
132 		char dtb_addr[20];
133 		char *bootm_argv[5] = {
134 			"bootm", boot_addr_start, "-", dtb_addr, NULL
135 		};
136 		if (!dtb)
137 			bootm_argv[3] = env_get("fdtcontroladdr");
138 		else
139 			snprintf(dtb_addr, sizeof(dtb_addr) - 1,
140 				 "0x%x", dtb);
141 
142 		snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
143 			 "0x%x", uimage);
144 		printf("Booting kernel at %s - %s...\n\n\n",
145 		       boot_addr_start, bootm_argv[3]);
146 		/* Try bootm for legacy and FIT format image */
147 		if (genimg_get_format((void *)uimage) != IMAGE_FORMAT_INVALID)
148 			do_bootm(cmdtp, 0, 4, bootm_argv);
149 		else if (CONFIG_IS_ENABLED(CMD_BOOTZ))
150 			do_bootz(cmdtp, 0, 4, bootm_argv);
151 	}
152 
153 	if (reset) {
154 		puts("Reset...\n");
155 		run_command("reset", 0);
156 	}
157 
158 	return CMD_RET_SUCCESS;
159 
160 cleanup:
161 	stm32prog_clean(data);
162 	free(stm32prog_data);
163 	stm32prog_data = NULL;
164 
165 	return CMD_RET_FAILURE;
166 }
167 
168 U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog,
169 	   "<link> <dev> [<addr>] [<size>]\n"
170 	   "start communication with tools STM32Cubeprogrammer on <link> with Flashlayout at <addr>",
171 	   "<link> = serial|usb\n"
172 	   "<dev>  = device instance\n"
173 	   "<addr> = address of flashlayout\n"
174 	   "<size> = size of flashlayout\n"
175 );
176 
stm32prog_get_tee_partitions(void)177 bool stm32prog_get_tee_partitions(void)
178 {
179 	if (stm32prog_data)
180 		return stm32prog_data->tee_detected;
181 
182 	return false;
183 }
184 
stm32prog_get_fsbl_nor(void)185 bool stm32prog_get_fsbl_nor(void)
186 {
187 	if (stm32prog_data)
188 		return stm32prog_data->fsbl_nor_detected;
189 
190 	return false;
191 }
192