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