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> |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 7 | #include <bootm.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 8 | #include <command.h> |
| 9 | #include <dfu.h> |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 10 | #include <image.h> |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 11 | #include <asm/arch/stm32prog.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 12 | #include <linux/printk.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 13 | #include "stm32prog.h" |
| 14 | |
| 15 | struct stm32prog_data *stm32prog_data; |
| 16 | |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 17 | static void enable_vidconsole(void) |
| 18 | { |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 19 | 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 Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 41 | static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 42 | 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 Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 48 | struct image_header_s header; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 49 | struct stm32prog_data *data; |
| 50 | |
| 51 | if (argc < 3 || argc > 5) |
| 52 | return CMD_RET_USAGE; |
| 53 | |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 54 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && !strcmp(argv[1], "usb")) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 55 | link = LINK_USB; |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 56 | else if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && !strcmp(argv[1], "serial")) |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 57 | link = LINK_SERIAL; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 58 | |
| 59 | if (link == LINK_UNDEFINED) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 60 | log_err("not supported link=%s\n", argv[1]); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 61 | return CMD_RET_USAGE; |
| 62 | } |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 63 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 64 | dev = (int)dectoul(argv[2], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 65 | |
Patrick Delaunay | e334d32 | 2022-09-06 18:53:18 +0200 | [diff] [blame] | 66 | addr = CONFIG_SYS_LOAD_ADDR; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 67 | size = 0; |
| 68 | if (argc > 3) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 69 | addr = hextoul(argv[3], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 70 | if (!addr) |
| 71 | return CMD_RET_FAILURE; |
| 72 | } |
| 73 | if (argc > 4) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 74 | size = hextoul(argv[4], NULL); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 75 | |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 76 | /* check STM32IMAGE presence */ |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 77 | if (size == 0) { |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 78 | stm32prog_header_check(addr, &header); |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 79 | if (header.type == HEADER_STM32IMAGE) { |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 80 | size = header.image_length + header.length; |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 81 | } |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Simon Glass | 52cb504 | 2022-10-18 07:46:31 -0600 | [diff] [blame] | 84 | if (IS_ENABLED(CONFIG_VIDEO)) |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 85 | enable_vidconsole(); |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 86 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 87 | data = (struct stm32prog_data *)malloc(sizeof(*data)); |
| 88 | |
| 89 | if (!data) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 90 | log_err("Alloc failed."); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 91 | return CMD_RET_FAILURE; |
| 92 | } |
| 93 | stm32prog_data = data; |
| 94 | |
| 95 | ret = stm32prog_init(data, addr, size); |
| 96 | if (ret) |
Patrick Delaunay | be7d21e | 2021-05-18 15:12:11 +0200 | [diff] [blame] | 97 | log_debug("Invalid or missing layout file at 0x%lx.\n", addr); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 98 | |
| 99 | /* prepare DFU for device read/write */ |
| 100 | ret = stm32prog_dfu_init(data); |
| 101 | if (ret) |
| 102 | goto cleanup; |
| 103 | |
| 104 | switch (link) { |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 105 | 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 Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 111 | 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 Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 123 | |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 124 | if (data->uimage) { |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 125 | char boot_addr_start[20]; |
| 126 | char dtb_addr[20]; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 127 | char initrd_addr[40]; |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 128 | char *fdt_arg, *initrd_arg; |
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; |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 132 | struct bootm_info bmi; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 133 | |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 134 | fdt_arg = dtb_addr; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 135 | if (!dtb) |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 136 | fdt_arg = env_get("fdtcontroladdr"); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 137 | else |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 138 | snprintf(dtb_addr, sizeof(dtb_addr) - 1, "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 | |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 143 | initrd_arg = NULL; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 144 | if (initrd) { |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 145 | snprintf(initrd_addr, sizeof(initrd_addr) - 1, |
| 146 | "0x%p:0x%zx", initrd, data->initrd_size); |
| 147 | initrd_arg = initrd_addr; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Simon Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 150 | printf("Booting kernel at %s %s %s...\n\n\n", boot_addr_start, |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 151 | 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 Glass | 43e88a6 | 2023-12-15 20:14:22 -0700 | [diff] [blame] | 157 | |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 158 | /* Try bootm for legacy and FIT format image */ |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 159 | if (genimg_get_format(uimage) != IMAGE_FORMAT_INVALID) |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 160 | bootm_run(&bmi); |
Simon Glass | a5de997 | 2023-02-05 15:36:25 -0700 | [diff] [blame] | 161 | else if (IS_ENABLED(CONFIG_CMD_BOOTZ)) |
Simon Glass | 260b965 | 2023-12-15 20:14:24 -0700 | [diff] [blame] | 162 | bootz_run(&bmi); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 163 | } |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 164 | if (data->script) |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 165 | cmd_source_script(data->script, NULL, NULL); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 166 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 167 | if (reset) { |
| 168 | puts("Reset...\n"); |
| 169 | run_command("reset", 0); |
| 170 | } |
| 171 | |
| 172 | return CMD_RET_SUCCESS; |
| 173 | |
| 174 | cleanup: |
| 175 | stm32prog_clean(data); |
| 176 | free(stm32prog_data); |
| 177 | stm32prog_data = NULL; |
| 178 | |
| 179 | return CMD_RET_FAILURE; |
| 180 | } |
| 181 | |
| 182 | U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, |
Patrick Delaunay | 9707698 | 2021-06-28 14:44:33 +0200 | [diff] [blame] | 183 | "start communication with tools STM32Cubeprogrammer", |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 184 | "<link> <dev> [<addr>] [<size>]\n" |
Patrick Delaunay | 9707698 | 2021-06-28 14:44:33 +0200 | [diff] [blame] | 185 | " <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 Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 189 | ); |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 190 | |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 191 | |
| 192 | bool stm32prog_get_fsbl_nor(void) |
| 193 | { |
| 194 | if (stm32prog_data) |
| 195 | return stm32prog_data->fsbl_nor_detected; |
| 196 | |
| 197 | return false; |
| 198 | } |