blob: adee6e05b636bd611af656353a506d9dd18ccfbd [file] [log] [blame]
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001// 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>
Simon Glass260b9652023-12-15 20:14:24 -07007#include <bootm.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01008#include <command.h>
9#include <dfu.h>
Patrick Delaunayc2fdd572020-03-18 09:25:02 +010010#include <image.h>
Patrick Delaunayc5112242020-03-18 09:24:55 +010011#include <asm/arch/stm32prog.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010013#include "stm32prog.h"
14
15struct stm32prog_data *stm32prog_data;
16
Patrick Delaunay8610f222020-03-18 09:25:01 +010017static void enable_vidconsole(void)
18{
Patrick Delaunay8610f222020-03-18 09:25:01 +010019 char *stdname;
20 char buf[64];
21
22 stdname = env_get("stdout");
23 if (!stdname || !strstr(stdname, "vidconsole")) {
24 if (!stdname)
25 snprintf(buf, sizeof(buf), "serial,vidconsole");
26 else
27 snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
28 env_set("stdout", buf);
29 }
30
31 stdname = env_get("stderr");
32 if (!stdname || !strstr(stdname, "vidconsole")) {
33 if (!stdname)
34 snprintf(buf, sizeof(buf), "serial,vidconsole");
35 else
36 snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
37 env_set("stderr", buf);
38 }
Patrick Delaunay8610f222020-03-18 09:25:01 +010039}
40
Simon Glassed38aef2020-05-10 11:40:03 -060041static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010042 char * const argv[])
43{
44 ulong addr, size;
45 int dev, ret;
46 enum stm32prog_link_t link = LINK_UNDEFINED;
47 bool reset = false;
Patrick Delaunayc2fdd572020-03-18 09:25:02 +010048 struct image_header_s header;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010049 struct stm32prog_data *data;
50
51 if (argc < 3 || argc > 5)
52 return CMD_RET_USAGE;
53
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +010054 if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && !strcmp(argv[1], "usb"))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010055 link = LINK_USB;
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +010056 else if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && !strcmp(argv[1], "serial"))
Patrick Delaunayb823d992020-03-18 09:25:00 +010057 link = LINK_SERIAL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010058
59 if (link == LINK_UNDEFINED) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010060 log_err("not supported link=%s\n", argv[1]);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010061 return CMD_RET_USAGE;
62 }
Patrick Delaunayb823d992020-03-18 09:25:00 +010063
Simon Glassff9b9032021-07-24 09:03:30 -060064 dev = (int)dectoul(argv[2], NULL);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010065
Patrick Delaunaye334d322022-09-06 18:53:18 +020066 addr = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010067 size = 0;
68 if (argc > 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060069 addr = hextoul(argv[3], NULL);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010070 if (!addr)
71 return CMD_RET_FAILURE;
72 }
73 if (argc > 4)
Simon Glass3ff49ec2021-07-24 09:03:29 -060074 size = hextoul(argv[4], NULL);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010075
Patrick Delaunayc2fdd572020-03-18 09:25:02 +010076 /* check STM32IMAGE presence */
Patrick Delaunay19676ef2021-04-02 14:05:17 +020077 if (size == 0) {
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020078 stm32prog_header_check(addr, &header);
Patrick Delaunay19676ef2021-04-02 14:05:17 +020079 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020080 size = header.image_length + header.length;
Patrick Delaunay19676ef2021-04-02 14:05:17 +020081 }
Patrick Delaunayc2fdd572020-03-18 09:25:02 +010082 }
83
Simon Glass52cb5042022-10-18 07:46:31 -060084 if (IS_ENABLED(CONFIG_VIDEO))
Patrick Delaunay8040da12020-07-31 16:31:52 +020085 enable_vidconsole();
Patrick Delaunay8610f222020-03-18 09:25:01 +010086
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010087 data = (struct stm32prog_data *)malloc(sizeof(*data));
88
89 if (!data) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010090 log_err("Alloc failed.");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010091 return CMD_RET_FAILURE;
92 }
93 stm32prog_data = data;
94
95 ret = stm32prog_init(data, addr, size);
96 if (ret)
Patrick Delaunaybe7d21e2021-05-18 15:12:11 +020097 log_debug("Invalid or missing layout file at 0x%lx.\n", addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010098
99 /* prepare DFU for device read/write */
100 ret = stm32prog_dfu_init(data);
101 if (ret)
102 goto cleanup;
103
104 switch (link) {
Patrick Delaunayb823d992020-03-18 09:25:00 +0100105 case LINK_SERIAL:
106 ret = stm32prog_serial_init(data, dev);
107 if (ret)
108 goto cleanup;
109 reset = stm32prog_serial_loop(data);
110 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100111 case LINK_USB:
112 reset = stm32prog_usb_loop(data, dev);
113 break;
114 default:
115 goto cleanup;
116 }
117
118 stm32prog_clean(data);
119 free(stm32prog_data);
120 stm32prog_data = NULL;
121
122 puts("Download done\n");
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100123
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200124 if (data->uimage) {
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100125 char boot_addr_start[20];
126 char dtb_addr[20];
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200127 char initrd_addr[40];
Simon Glass43e88a62023-12-15 20:14:22 -0700128 char *fdt_arg, *initrd_arg;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200129 const void *uimage = (void *)data->uimage;
130 const void *dtb = (void *)data->dtb;
131 const void *initrd = (void *)data->initrd;
Simon Glass260b9652023-12-15 20:14:24 -0700132 struct bootm_info bmi;
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200133
Simon Glass43e88a62023-12-15 20:14:22 -0700134 fdt_arg = dtb_addr;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100135 if (!dtb)
Simon Glass43e88a62023-12-15 20:14:22 -0700136 fdt_arg = env_get("fdtcontroladdr");
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100137 else
Simon Glass43e88a62023-12-15 20:14:22 -0700138 snprintf(dtb_addr, sizeof(dtb_addr) - 1, "0x%p", dtb);
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100139
140 snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200141 "0x%p", uimage);
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200142
Simon Glass260b9652023-12-15 20:14:24 -0700143 initrd_arg = NULL;
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200144 if (initrd) {
Simon Glass43e88a62023-12-15 20:14:22 -0700145 snprintf(initrd_addr, sizeof(initrd_addr) - 1,
146 "0x%p:0x%zx", initrd, data->initrd_size);
147 initrd_arg = initrd_addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200148 }
149
Simon Glass43e88a62023-12-15 20:14:22 -0700150 printf("Booting kernel at %s %s %s...\n\n\n", boot_addr_start,
Simon Glass260b9652023-12-15 20:14:24 -0700151 initrd_arg ?: "-", fdt_arg);
152
153 bootm_init(&bmi);
154 bmi.addr_img = boot_addr_start;
155 bmi.conf_ramdisk = initrd_arg;
156 bmi.conf_fdt = fdt_arg;
Simon Glass43e88a62023-12-15 20:14:22 -0700157
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100158 /* Try bootm for legacy and FIT format image */
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200159 if (genimg_get_format(uimage) != IMAGE_FORMAT_INVALID)
Simon Glass260b9652023-12-15 20:14:24 -0700160 bootm_run(&bmi);
Simon Glassa5de9972023-02-05 15:36:25 -0700161 else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
Simon Glass260b9652023-12-15 20:14:24 -0700162 bootz_run(&bmi);
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100163 }
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +0200164 if (data->script)
Simon Glassdaee3ba2023-01-06 08:52:28 -0600165 cmd_source_script(data->script, NULL, NULL);
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100166
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100167 if (reset) {
168 puts("Reset...\n");
169 run_command("reset", 0);
170 }
171
172 return CMD_RET_SUCCESS;
173
174cleanup:
175 stm32prog_clean(data);
176 free(stm32prog_data);
177 stm32prog_data = NULL;
178
179 return CMD_RET_FAILURE;
180}
181
182U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog,
Patrick Delaunay97076982021-06-28 14:44:33 +0200183 "start communication with tools STM32Cubeprogrammer",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100184 "<link> <dev> [<addr>] [<size>]\n"
Patrick Delaunay97076982021-06-28 14:44:33 +0200185 " <link> = serial|usb\n"
186 " <dev> = device instance\n"
187 " <addr> = address of flashlayout\n"
188 " <size> = size of flashlayout (optional for image with STM32 header)\n"
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100189);
Patrick Delaunayc5112242020-03-18 09:24:55 +0100190
Patrick Delaunayc5112242020-03-18 09:24:55 +0100191
192bool stm32prog_get_fsbl_nor(void)
193{
194 if (stm32prog_data)
195 return stm32prog_data->fsbl_nor_detected;
196
197 return false;
198}