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; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 48 | u32 uimage, dtb; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 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 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 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 | |
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) { |
| 77 | stm32prog_header_check((struct raw_header_s *)addr, &header); |
| 78 | if (header.type == HEADER_STM32IMAGE) { |
| 79 | size = header.image_length + BL_HEADER_SIZE; |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 80 | |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 81 | /* uImage detected in STM32IMAGE, execute the script */ |
| 82 | if (IMAGE_FORMAT_LEGACY == |
| 83 | genimg_get_format((void *)(addr + BL_HEADER_SIZE))) |
| 84 | return image_source_script(addr + BL_HEADER_SIZE, "script@1"); |
| 85 | } |
Patrick Delaunay | c2fdd57 | 2020-03-18 09:25:02 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 88 | if (IS_ENABLED(CONFIG_DM_VIDEO)) |
| 89 | enable_vidconsole(); |
Patrick Delaunay | 8610f22 | 2020-03-18 09:25:01 +0100 | [diff] [blame] | 90 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 91 | data = (struct stm32prog_data *)malloc(sizeof(*data)); |
| 92 | |
| 93 | if (!data) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 94 | log_err("Alloc failed."); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 95 | return CMD_RET_FAILURE; |
| 96 | } |
| 97 | stm32prog_data = data; |
| 98 | |
| 99 | ret = stm32prog_init(data, addr, size); |
| 100 | if (ret) |
| 101 | printf("Invalid or missing layout file."); |
| 102 | |
| 103 | /* prepare DFU for device read/write */ |
| 104 | ret = stm32prog_dfu_init(data); |
| 105 | if (ret) |
| 106 | goto cleanup; |
| 107 | |
| 108 | switch (link) { |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 109 | case LINK_SERIAL: |
| 110 | ret = stm32prog_serial_init(data, dev); |
| 111 | if (ret) |
| 112 | goto cleanup; |
| 113 | reset = stm32prog_serial_loop(data); |
| 114 | break; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 115 | case LINK_USB: |
| 116 | reset = stm32prog_usb_loop(data, dev); |
| 117 | break; |
| 118 | default: |
| 119 | goto cleanup; |
| 120 | } |
| 121 | |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 122 | uimage = data->uimage; |
| 123 | dtb = data->dtb; |
| 124 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 125 | stm32prog_clean(data); |
| 126 | free(stm32prog_data); |
| 127 | stm32prog_data = NULL; |
| 128 | |
| 129 | puts("Download done\n"); |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 130 | |
| 131 | if (uimage) { |
| 132 | char boot_addr_start[20]; |
| 133 | char dtb_addr[20]; |
| 134 | char *bootm_argv[5] = { |
| 135 | "bootm", boot_addr_start, "-", dtb_addr, NULL |
| 136 | }; |
| 137 | if (!dtb) |
| 138 | bootm_argv[3] = env_get("fdtcontroladdr"); |
| 139 | else |
| 140 | snprintf(dtb_addr, sizeof(dtb_addr) - 1, |
| 141 | "0x%x", dtb); |
| 142 | |
| 143 | snprintf(boot_addr_start, sizeof(boot_addr_start) - 1, |
| 144 | "0x%x", uimage); |
| 145 | printf("Booting kernel at %s - %s...\n\n\n", |
| 146 | boot_addr_start, bootm_argv[3]); |
| 147 | /* Try bootm for legacy and FIT format image */ |
| 148 | if (genimg_get_format((void *)uimage) != IMAGE_FORMAT_INVALID) |
| 149 | do_bootm(cmdtp, 0, 4, bootm_argv); |
Bin Meng | d0d1df6 | 2020-07-08 00:19:22 -0700 | [diff] [blame] | 150 | else if (CONFIG_IS_ENABLED(CMD_BOOTZ)) |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 151 | do_bootz(cmdtp, 0, 4, bootm_argv); |
| 152 | } |
| 153 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 154 | if (reset) { |
| 155 | puts("Reset...\n"); |
| 156 | run_command("reset", 0); |
| 157 | } |
| 158 | |
| 159 | return CMD_RET_SUCCESS; |
| 160 | |
| 161 | cleanup: |
| 162 | stm32prog_clean(data); |
| 163 | free(stm32prog_data); |
| 164 | stm32prog_data = NULL; |
| 165 | |
| 166 | return CMD_RET_FAILURE; |
| 167 | } |
| 168 | |
| 169 | U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, |
| 170 | "<link> <dev> [<addr>] [<size>]\n" |
| 171 | "start communication with tools STM32Cubeprogrammer on <link> with Flashlayout at <addr>", |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 172 | "<link> = serial|usb\n" |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 173 | "<dev> = device instance\n" |
| 174 | "<addr> = address of flashlayout\n" |
| 175 | "<size> = size of flashlayout\n" |
| 176 | ); |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 177 | |
| 178 | bool stm32prog_get_tee_partitions(void) |
| 179 | { |
| 180 | if (stm32prog_data) |
| 181 | return stm32prog_data->tee_detected; |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | bool stm32prog_get_fsbl_nor(void) |
| 187 | { |
| 188 | if (stm32prog_data) |
| 189 | return stm32prog_data->fsbl_nor_detected; |
| 190 | |
| 191 | return false; |
| 192 | } |