Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 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> |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 9 | #include <image.h> |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 10 | #include <asm/arch/stm32prog.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 11 | #include "stm32prog.h" |
| 12 | |
| 13 | struct stm32prog_data *stm32prog_data; |
| 14 | |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 15 | static void enable_vidconsole(void) |
| 16 | { |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 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 | } |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 39 | static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 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; |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 46 | struct image_header_s header; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 47 | struct stm32prog_data *data; |
| 48 | |
| 49 | if (argc < 3 || argc > 5) |
| 50 | return CMD_RET_USAGE; |
| 51 | |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 52 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && !strcmp(argv[1], "usb")) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 53 | link = LINK_USB; |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 54 | else if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && !strcmp(argv[1], "serial")) |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 55 | link = LINK_SERIAL; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 56 | |
| 57 | if (link == LINK_UNDEFINED) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 58 | log_err("not supported link=%s\n", argv[1]); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 59 | return CMD_RET_USAGE; |
| 60 | } |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 61 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 62 | dev = (int)dectoul(argv[2], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 63 | |
Patrick Delaunay | e334d32 | 2022-09-06 18:53:18 +0200 | [diff] [blame] | 64 | addr = CONFIG_SYS_LOAD_ADDR; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 65 | size = 0; |
| 66 | if (argc > 3) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 67 | addr = hextoul(argv[3], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 68 | if (!addr) |
| 69 | return CMD_RET_FAILURE; |
| 70 | } |
| 71 | if (argc > 4) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 72 | size = hextoul(argv[4], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 73 | |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 74 | /* check STM32IMAGE presence */ |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 75 | if (size == 0) { |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 76 | stm32prog_header_check(addr, &header); |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 77 | if (header.type == HEADER_STM32IMAGE) { |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 78 | size = header.image_length + header.length; |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 79 | } |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 82 | if (IS_ENABLED(CONFIG_DM_VIDEO)) |
| 83 | enable_vidconsole(); |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 84 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 85 | data = (struct stm32prog_data *)malloc(sizeof(*data)); |
| 86 | |
| 87 | if (!data) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 88 | log_err("Alloc failed."); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 89 | return CMD_RET_FAILURE; |
| 90 | } |
| 91 | stm32prog_data = data; |
| 92 | |
| 93 | ret = stm32prog_init(data, addr, size); |
| 94 | if (ret) |
Patrick Delaunay | be7d21e | 2021-05-18 15:12:11 +0200 | [diff] [blame] | 95 | log_debug("Invalid or missing layout file at 0x%lx.\n", addr); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 96 | |
| 97 | /* prepare DFU for device read/write */ |
| 98 | ret = stm32prog_dfu_init(data); |
| 99 | if (ret) |
| 100 | goto cleanup; |
| 101 | |
| 102 | switch (link) { |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 103 | case LINK_SERIAL: |
| 104 | ret = stm32prog_serial_init(data, dev); |
| 105 | if (ret) |
| 106 | goto cleanup; |
| 107 | reset = stm32prog_serial_loop(data); |
| 108 | break; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 109 | case LINK_USB: |
| 110 | reset = stm32prog_usb_loop(data, dev); |
| 111 | break; |
| 112 | default: |
| 113 | goto cleanup; |
| 114 | } |
| 115 | |
| 116 | stm32prog_clean(data); |
| 117 | free(stm32prog_data); |
| 118 | stm32prog_data = NULL; |
| 119 | |
| 120 | puts("Download done\n"); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 121 | |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 122 | if (data->uimage) { |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 123 | char boot_addr_start[20]; |
| 124 | char dtb_addr[20]; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 125 | char initrd_addr[40]; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 126 | char *bootm_argv[5] = { |
| 127 | "bootm", boot_addr_start, "-", dtb_addr, NULL |
| 128 | }; |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 129 | const void *uimage = (void *)data->uimage; |
| 130 | const void *dtb = (void *)data->dtb; |
| 131 | const void *initrd = (void *)data->initrd; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 132 | |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 133 | if (!dtb) |
| 134 | bootm_argv[3] = env_get("fdtcontroladdr"); |
| 135 | else |
| 136 | snprintf(dtb_addr, sizeof(dtb_addr) - 1, |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 137 | "0x%p", dtb); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 138 | |
| 139 | snprintf(boot_addr_start, sizeof(boot_addr_start) - 1, |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 140 | "0x%p", uimage); |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 141 | |
| 142 | if (initrd) { |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 143 | snprintf(initrd_addr, sizeof(initrd_addr) - 1, "0x%p:0x%zx", |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 144 | initrd, data->initrd_size); |
| 145 | bootm_argv[2] = initrd_addr; |
| 146 | } |
| 147 | |
| 148 | printf("Booting kernel at %s %s %s...\n\n\n", |
| 149 | boot_addr_start, bootm_argv[2], bootm_argv[3]); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 150 | /* Try bootm for legacy and FIT format image */ |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 151 | if (genimg_get_format(uimage) != IMAGE_FORMAT_INVALID) |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 152 | do_bootm(cmdtp, 0, 4, bootm_argv); |
Bin Meng | d0d1df6 | 2020-07-08 00:19:22 -0700 | [diff] [blame] | 153 | else if (CONFIG_IS_ENABLED(CMD_BOOTZ)) |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 154 | do_bootz(cmdtp, 0, 4, bootm_argv); |
| 155 | } |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 156 | if (data->script) |
| 157 | image_source_script(data->script, "script@stm32prog"); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 158 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 159 | if (reset) { |
| 160 | puts("Reset...\n"); |
| 161 | run_command("reset", 0); |
| 162 | } |
| 163 | |
| 164 | return CMD_RET_SUCCESS; |
| 165 | |
| 166 | cleanup: |
| 167 | stm32prog_clean(data); |
| 168 | free(stm32prog_data); |
| 169 | stm32prog_data = NULL; |
| 170 | |
| 171 | return CMD_RET_FAILURE; |
| 172 | } |
| 173 | |
| 174 | U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, |
Patrick Delaunay | 9707698 | 2021-06-28 14:44:33 +0200 | [diff] [blame] | 175 | "start communication with tools STM32Cubeprogrammer", |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 176 | "<link> <dev> [<addr>] [<size>]\n" |
Patrick Delaunay | 9707698 | 2021-06-28 14:44:33 +0200 | [diff] [blame] | 177 | " <link> = serial|usb\n" |
| 178 | " <dev> = device instance\n" |
| 179 | " <addr> = address of flashlayout\n" |
| 180 | " <size> = size of flashlayout (optional for image with STM32 header)\n" |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 181 | ); |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 182 | |
Patrick Delaunay | 1008a50 | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 183 | #ifdef CONFIG_STM32MP15x_STM32IMAGE |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 184 | bool stm32prog_get_tee_partitions(void) |
| 185 | { |
| 186 | if (stm32prog_data) |
| 187 | return stm32prog_data->tee_detected; |
| 188 | |
| 189 | return false; |
| 190 | } |
Patrick Delaunay | 1008a50 | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 191 | #endif |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 192 | |
| 193 | bool stm32prog_get_fsbl_nor(void) |
| 194 | { |
| 195 | if (stm32prog_data) |
| 196 | return stm32prog_data->fsbl_nor_detected; |
| 197 | |
| 198 | return false; |
| 199 | } |