Patrick Delaunay | 954bd1a | 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 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 7 | #include <console.h> |
| 8 | #include <dfu.h> |
| 9 | #include <malloc.h> |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 10 | #include <misc.h> |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 11 | #include <mmc.h> |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 12 | #include <part.h> |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 13 | #include <tee.h> |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 14 | #include <asm/arch/stm32mp1_smc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 17 | #include <dm/uclass.h> |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 18 | #include <jffs2/load_kernel.h> |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 19 | #include <linux/list.h> |
| 20 | #include <linux/list_sort.h> |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 21 | #include <linux/mtd/mtd.h> |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 22 | #include <linux/sizes.h> |
| 23 | |
| 24 | #include "stm32prog.h" |
| 25 | |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 26 | /* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */ |
| 27 | #define GPT_HEADER_SZ 34 |
| 28 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 29 | #define OPT_SELECT BIT(0) |
| 30 | #define OPT_EMPTY BIT(1) |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 31 | #define OPT_DELETE BIT(2) |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 32 | |
| 33 | #define IS_SELECT(part) ((part)->option & OPT_SELECT) |
| 34 | #define IS_EMPTY(part) ((part)->option & OPT_EMPTY) |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 35 | #define IS_DELETE(part) ((part)->option & OPT_DELETE) |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 36 | |
| 37 | #define ALT_BUF_LEN SZ_1K |
| 38 | |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 39 | #define ROOTFS_MMC0_UUID \ |
| 40 | EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \ |
| 41 | 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82) |
| 42 | |
| 43 | #define ROOTFS_MMC1_UUID \ |
| 44 | EFI_GUID(0x491F6117, 0x415D, 0x4F53, \ |
| 45 | 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6) |
| 46 | |
| 47 | #define ROOTFS_MMC2_UUID \ |
| 48 | EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \ |
| 49 | 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18) |
| 50 | |
Patrick Delaunay | d983e50 | 2022-03-28 19:25:26 +0200 | [diff] [blame] | 51 | /* RAW partition (binary / bootloader) used Linux - reserved UUID */ |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 52 | #define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908" |
| 53 | |
| 54 | /* |
| 55 | * unique partition guid (uuid) for partition named "rootfs" |
| 56 | * on each MMC instance = SD Card or eMMC |
| 57 | * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..." |
| 58 | */ |
| 59 | static const efi_guid_t uuid_mmc[3] = { |
| 60 | ROOTFS_MMC0_UUID, |
| 61 | ROOTFS_MMC1_UUID, |
| 62 | ROOTFS_MMC2_UUID |
| 63 | }; |
| 64 | |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 65 | /* order of column in flash layout file */ |
| 66 | enum stm32prog_col_t { |
| 67 | COL_OPTION, |
| 68 | COL_ID, |
| 69 | COL_NAME, |
| 70 | COL_TYPE, |
| 71 | COL_IP, |
| 72 | COL_OFFSET, |
| 73 | COL_NB_STM32 |
| 74 | }; |
| 75 | |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 76 | #define FIP_TOC_HEADER_NAME 0xAA640001 |
| 77 | |
| 78 | struct fip_toc_header { |
| 79 | u32 name; |
| 80 | u32 serial_number; |
| 81 | u64 flags; |
| 82 | }; |
| 83 | |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 84 | #define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \ |
| 85 | { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } } |
| 86 | |
| 87 | /* |
| 88 | * Read NVMEM memory for STM32CubeProgrammer |
| 89 | * |
| 90 | * [in] value[0].a: Type (0 for OTP access) |
| 91 | * [out] memref[1].buffer Output buffer to return all read values |
| 92 | * [out] memref[1].size Size of buffer to be read |
| 93 | * |
| 94 | * Return codes: |
| 95 | * TEE_SUCCESS - Invoke command success |
| 96 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 97 | */ |
| 98 | #define TA_NVMEM_READ 0x0 |
| 99 | |
| 100 | /* |
| 101 | * Write NVMEM memory for STM32CubeProgrammer |
| 102 | * |
| 103 | * [in] value[0].a Type (0 for OTP access) |
| 104 | * [in] memref[1].buffer Input buffer with the values to write |
| 105 | * [in] memref[1].size Size of buffer to be written |
| 106 | * |
| 107 | * Return codes: |
| 108 | * TEE_SUCCESS - Invoke command success |
| 109 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 110 | */ |
| 111 | #define TA_NVMEM_WRITE 0x1 |
| 112 | |
| 113 | /* value of TA_NVMEM type = value[in] a */ |
| 114 | #define NVMEM_OTP 0 |
| 115 | |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 116 | DECLARE_GLOBAL_DATA_PTR; |
| 117 | |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 118 | /* OPTEE TA NVMEM open helper */ |
| 119 | static int optee_ta_open(struct stm32prog_data *data) |
| 120 | { |
| 121 | const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID; |
| 122 | struct tee_open_session_arg arg; |
| 123 | struct udevice *tee = NULL; |
| 124 | int rc; |
| 125 | |
| 126 | if (data->tee) |
| 127 | return 0; |
| 128 | |
| 129 | tee = tee_find_device(NULL, NULL, NULL, NULL); |
| 130 | if (!tee) |
| 131 | return -ENODEV; |
| 132 | |
| 133 | memset(&arg, 0, sizeof(arg)); |
| 134 | tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); |
| 135 | rc = tee_open_session(tee, &arg, 0, NULL); |
| 136 | if (rc < 0) |
| 137 | return -ENODEV; |
| 138 | |
| 139 | data->tee = tee; |
| 140 | data->tee_session = arg.session; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* OPTEE TA NVMEM invoke helper */ |
| 146 | static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type, |
| 147 | void *buff, ulong size) |
| 148 | { |
| 149 | struct tee_invoke_arg arg; |
| 150 | struct tee_param param[2]; |
| 151 | struct tee_shm *buff_shm; |
| 152 | int rc; |
| 153 | |
| 154 | rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm); |
| 155 | if (rc) |
| 156 | return rc; |
| 157 | |
| 158 | memset(&arg, 0, sizeof(arg)); |
| 159 | arg.func = cmd; |
| 160 | arg.session = data->tee_session; |
| 161 | |
| 162 | memset(param, 0, sizeof(param)); |
| 163 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 164 | param[0].u.value.a = type; |
| 165 | |
| 166 | if (cmd == TA_NVMEM_WRITE) |
| 167 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; |
| 168 | else |
| 169 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| 170 | |
| 171 | param[1].u.memref.shm = buff_shm; |
| 172 | param[1].u.memref.size = size; |
| 173 | |
| 174 | rc = tee_invoke_func(data->tee, &arg, 2, param); |
| 175 | if (rc < 0 || arg.ret != 0) { |
| 176 | dev_err(data->tee, |
| 177 | "TA_NVMEM invoke failed TEE err: %x, err:%x\n", |
| 178 | arg.ret, rc); |
| 179 | if (!rc) |
| 180 | rc = -EIO; |
| 181 | } |
| 182 | |
| 183 | tee_shm_free(buff_shm); |
| 184 | |
| 185 | return rc; |
| 186 | } |
| 187 | |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 188 | /* partition handling routines : CONFIG_CMD_MTDPARTS */ |
| 189 | int mtdparts_init(void); |
| 190 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
| 191 | u8 *part_num, struct part_info **part); |
| 192 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 193 | char *stm32prog_get_error(struct stm32prog_data *data) |
| 194 | { |
| 195 | static const char error_msg[] = "Unspecified"; |
| 196 | |
| 197 | if (strlen(data->error) == 0) |
| 198 | strcpy(data->error, error_msg); |
| 199 | |
| 200 | return data->error; |
| 201 | } |
| 202 | |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 203 | static bool stm32prog_is_fip_header(struct fip_toc_header *header) |
| 204 | { |
| 205 | return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number; |
| 206 | } |
| 207 | |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 208 | static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header) |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 209 | { |
| 210 | unsigned int i; |
| 211 | |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 212 | if (header->magic_number != |
| 213 | (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) { |
| 214 | log_debug("%s:invalid magic number : 0x%x\n", |
| 215 | __func__, header->magic_number); |
| 216 | return false; |
| 217 | } |
| 218 | if (header->header_version != 0x00010000) { |
| 219 | log_debug("%s:invalid header version : 0x%x\n", |
| 220 | __func__, header->header_version); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | if (header->reserved1 || header->reserved2) { |
| 225 | log_debug("%s:invalid reserved field\n", __func__); |
| 226 | return false; |
| 227 | } |
| 228 | for (i = 0; i < sizeof(header->padding); i++) { |
| 229 | if (header->padding[i] != 0) { |
| 230 | log_debug("%s:invalid padding field\n", __func__); |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header) |
| 239 | { |
| 240 | unsigned int i; |
| 241 | |
| 242 | if (header->magic_number != |
| 243 | (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) { |
| 244 | log_debug("%s:invalid magic number : 0x%x\n", |
| 245 | __func__, header->magic_number); |
| 246 | return false; |
| 247 | } |
| 248 | if (header->header_version != 0x00020000) { |
| 249 | log_debug("%s:invalid header version : 0x%x\n", |
| 250 | __func__, header->header_version); |
| 251 | return false; |
| 252 | } |
| 253 | if (header->reserved1 || header->reserved2) |
| 254 | return false; |
| 255 | |
| 256 | for (i = 0; i < sizeof(header->padding); i++) { |
| 257 | if (header->padding[i] != 0) { |
| 258 | log_debug("%s:invalid padding field\n", __func__); |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header) |
| 267 | { |
| 268 | struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header; |
| 269 | struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header; |
| 270 | |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 271 | if (!raw_header || !header) { |
| 272 | log_debug("%s:no header data\n", __func__); |
| 273 | return; |
| 274 | } |
| 275 | |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 276 | if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) { |
| 277 | header->type = HEADER_FIP; |
| 278 | header->length = 0; |
| 279 | return; |
| 280 | } |
| 281 | if (stm32prog_is_stm32_header_v1(v1_header)) { |
| 282 | header->type = HEADER_STM32IMAGE; |
| 283 | header->image_checksum = le32_to_cpu(v1_header->image_checksum); |
| 284 | header->image_length = le32_to_cpu(v1_header->image_length); |
| 285 | header->length = sizeof(struct stm32_header_v1); |
| 286 | return; |
| 287 | } |
| 288 | if (stm32prog_is_stm32_header_v2(v2_header)) { |
| 289 | header->type = HEADER_STM32IMAGE_V2; |
| 290 | header->image_checksum = le32_to_cpu(v2_header->image_checksum); |
| 291 | header->image_length = le32_to_cpu(v2_header->image_length); |
| 292 | header->length = sizeof(struct stm32_header_v1) + |
| 293 | v2_header->extension_headers_length; |
| 294 | return; |
| 295 | } |
| 296 | |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 297 | header->type = HEADER_NONE; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 298 | header->image_checksum = 0x0; |
| 299 | header->image_length = 0x0; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static u32 stm32prog_header_checksum(u32 addr, struct image_header_s *header) |
| 303 | { |
| 304 | u32 i, checksum; |
| 305 | u8 *payload; |
| 306 | |
| 307 | /* compute checksum on payload */ |
| 308 | payload = (u8 *)addr; |
| 309 | checksum = 0; |
| 310 | for (i = header->image_length; i > 0; i--) |
| 311 | checksum += *(payload++); |
| 312 | |
| 313 | return checksum; |
| 314 | } |
| 315 | |
| 316 | /* FLASHLAYOUT PARSING *****************************************/ |
| 317 | static int parse_option(struct stm32prog_data *data, |
| 318 | int i, char *p, struct stm32prog_part_t *part) |
| 319 | { |
| 320 | int result = 0; |
| 321 | char *c = p; |
| 322 | |
| 323 | part->option = 0; |
| 324 | if (!strcmp(p, "-")) |
| 325 | return 0; |
| 326 | |
| 327 | while (*c) { |
| 328 | switch (*c) { |
| 329 | case 'P': |
| 330 | part->option |= OPT_SELECT; |
| 331 | break; |
| 332 | case 'E': |
| 333 | part->option |= OPT_EMPTY; |
| 334 | break; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 335 | case 'D': |
| 336 | part->option |= OPT_DELETE; |
| 337 | break; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 338 | default: |
| 339 | result = -EINVAL; |
| 340 | stm32prog_err("Layout line %d: invalid option '%c' in %s)", |
| 341 | i, *c, p); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | c++; |
| 345 | } |
| 346 | if (!(part->option & OPT_SELECT)) { |
| 347 | stm32prog_err("Layout line %d: missing 'P' in option %s", i, p); |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | return result; |
| 352 | } |
| 353 | |
| 354 | static int parse_id(struct stm32prog_data *data, |
| 355 | int i, char *p, struct stm32prog_part_t *part) |
| 356 | { |
| 357 | int result = 0; |
| 358 | unsigned long value; |
| 359 | |
| 360 | result = strict_strtoul(p, 0, &value); |
| 361 | part->id = value; |
| 362 | if (result || value > PHASE_LAST_USER) { |
| 363 | stm32prog_err("Layout line %d: invalid phase value = %s", i, p); |
| 364 | result = -EINVAL; |
| 365 | } |
| 366 | |
| 367 | return result; |
| 368 | } |
| 369 | |
| 370 | static int parse_name(struct stm32prog_data *data, |
| 371 | int i, char *p, struct stm32prog_part_t *part) |
| 372 | { |
| 373 | int result = 0; |
| 374 | |
| 375 | if (strlen(p) < sizeof(part->name)) { |
| 376 | strcpy(part->name, p); |
| 377 | } else { |
| 378 | stm32prog_err("Layout line %d: partition name too long [%d]: %s", |
| 379 | i, strlen(p), p); |
| 380 | result = -EINVAL; |
| 381 | } |
| 382 | |
| 383 | return result; |
| 384 | } |
| 385 | |
| 386 | static int parse_type(struct stm32prog_data *data, |
| 387 | int i, char *p, struct stm32prog_part_t *part) |
| 388 | { |
| 389 | int result = 0; |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 390 | int len = 0; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 391 | |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 392 | part->bin_nb = 0; |
| 393 | if (!strncmp(p, "Binary", 6)) { |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 394 | part->part_type = PART_BINARY; |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 395 | |
| 396 | /* search for Binary(X) case */ |
| 397 | len = strlen(p); |
| 398 | part->bin_nb = 1; |
| 399 | if (len > 6) { |
| 400 | if (len < 8 || |
| 401 | (p[6] != '(') || |
| 402 | (p[len - 1] != ')')) |
| 403 | result = -EINVAL; |
| 404 | else |
| 405 | part->bin_nb = |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 406 | dectoul(&p[7], NULL); |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 407 | } |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 408 | } else if (!strcmp(p, "System")) { |
| 409 | part->part_type = PART_SYSTEM; |
| 410 | } else if (!strcmp(p, "FileSystem")) { |
| 411 | part->part_type = PART_FILESYSTEM; |
| 412 | } else if (!strcmp(p, "RawImage")) { |
| 413 | part->part_type = RAW_IMAGE; |
| 414 | } else { |
| 415 | result = -EINVAL; |
| 416 | } |
| 417 | if (result) |
| 418 | stm32prog_err("Layout line %d: type parsing error : '%s'", |
| 419 | i, p); |
| 420 | |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | static int parse_ip(struct stm32prog_data *data, |
| 425 | int i, char *p, struct stm32prog_part_t *part) |
| 426 | { |
| 427 | int result = 0; |
| 428 | unsigned int len = 0; |
| 429 | |
| 430 | part->dev_id = 0; |
| 431 | if (!strcmp(p, "none")) { |
| 432 | part->target = STM32PROG_NONE; |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 433 | } else if (!strncmp(p, "mmc", 3)) { |
| 434 | part->target = STM32PROG_MMC; |
| 435 | len = 3; |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 436 | } else if (!strncmp(p, "nor", 3)) { |
| 437 | part->target = STM32PROG_NOR; |
| 438 | len = 3; |
| 439 | } else if (!strncmp(p, "nand", 4)) { |
| 440 | part->target = STM32PROG_NAND; |
| 441 | len = 4; |
| 442 | } else if (!strncmp(p, "spi-nand", 8)) { |
| 443 | part->target = STM32PROG_SPI_NAND; |
| 444 | len = 8; |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 445 | } else if (!strncmp(p, "ram", 3)) { |
| 446 | part->target = STM32PROG_RAM; |
| 447 | len = 0; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 448 | } else { |
| 449 | result = -EINVAL; |
| 450 | } |
| 451 | if (len) { |
| 452 | /* only one digit allowed for device id */ |
| 453 | if (strlen(p) != len + 1) { |
| 454 | result = -EINVAL; |
| 455 | } else { |
| 456 | part->dev_id = p[len] - '0'; |
| 457 | if (part->dev_id > 9) |
| 458 | result = -EINVAL; |
| 459 | } |
| 460 | } |
| 461 | if (result) |
| 462 | stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p); |
| 463 | |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | static int parse_offset(struct stm32prog_data *data, |
| 468 | int i, char *p, struct stm32prog_part_t *part) |
| 469 | { |
| 470 | int result = 0; |
| 471 | char *tail; |
| 472 | |
| 473 | part->part_id = 0; |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 474 | part->addr = 0; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 475 | part->size = 0; |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 476 | /* eMMC boot parttion */ |
| 477 | if (!strncmp(p, "boot", 4)) { |
| 478 | if (strlen(p) != 5) { |
| 479 | result = -EINVAL; |
| 480 | } else { |
| 481 | if (p[4] == '1') |
| 482 | part->part_id = -1; |
| 483 | else if (p[4] == '2') |
| 484 | part->part_id = -2; |
| 485 | else |
| 486 | result = -EINVAL; |
| 487 | } |
| 488 | if (result) |
| 489 | stm32prog_err("Layout line %d: invalid part '%s'", |
| 490 | i, p); |
| 491 | } else { |
| 492 | part->addr = simple_strtoull(p, &tail, 0); |
| 493 | if (tail == p || *tail != '\0') { |
| 494 | stm32prog_err("Layout line %d: invalid offset '%s'", |
| 495 | i, p); |
| 496 | result = -EINVAL; |
| 497 | } |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | static |
| 504 | int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p, |
| 505 | struct stm32prog_part_t *part) = { |
| 506 | [COL_OPTION] = parse_option, |
| 507 | [COL_ID] = parse_id, |
| 508 | [COL_NAME] = parse_name, |
| 509 | [COL_TYPE] = parse_type, |
| 510 | [COL_IP] = parse_ip, |
| 511 | [COL_OFFSET] = parse_offset, |
| 512 | }; |
| 513 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 514 | static int parse_flash_layout(struct stm32prog_data *data, |
| 515 | ulong addr, |
| 516 | ulong size) |
| 517 | { |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 518 | int column = 0, part_nb = 0, ret; |
| 519 | bool end_of_line, eof; |
| 520 | char *p, *start, *last, *col; |
| 521 | struct stm32prog_part_t *part; |
Patrick Delaunay | 5f14e2f | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 522 | struct image_header_s header; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 523 | int part_list_size; |
| 524 | int i; |
| 525 | |
| 526 | data->part_nb = 0; |
| 527 | |
| 528 | /* check if STM32image is detected */ |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 529 | stm32prog_header_check(addr, &header); |
Patrick Delaunay | 5f14e2f | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 530 | if (header.type == HEADER_STM32IMAGE) { |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 531 | u32 checksum; |
| 532 | |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 533 | addr = addr + header.length; |
Patrick Delaunay | 5f14e2f | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 534 | size = header.image_length; |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 535 | |
Patrick Delaunay | 5f14e2f | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 536 | checksum = stm32prog_header_checksum(addr, &header); |
| 537 | if (checksum != header.image_checksum) { |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 538 | stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x", |
Patrick Delaunay | 5f14e2f | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 539 | checksum, header.image_checksum); |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 540 | return -EIO; |
| 541 | } |
| 542 | } |
| 543 | if (!size) |
| 544 | return -EINVAL; |
| 545 | |
| 546 | start = (char *)addr; |
| 547 | last = start + size; |
| 548 | |
| 549 | *last = 0x0; /* force null terminated string */ |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 550 | log_debug("flash layout =\n%s\n", start); |
Patrick Delaunay | 6ee6839 | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 551 | |
| 552 | /* calculate expected number of partitions */ |
| 553 | part_list_size = 1; |
| 554 | p = start; |
| 555 | while (*p && (p < last)) { |
| 556 | if (*p++ == '\n') { |
| 557 | part_list_size++; |
| 558 | if (p < last && *p == '#') |
| 559 | part_list_size--; |
| 560 | } |
| 561 | } |
| 562 | if (part_list_size > PHASE_LAST_USER) { |
| 563 | stm32prog_err("Layout: too many partition (%d)", |
| 564 | part_list_size); |
| 565 | return -1; |
| 566 | } |
| 567 | part = calloc(sizeof(struct stm32prog_part_t), part_list_size); |
| 568 | if (!part) { |
| 569 | stm32prog_err("Layout: alloc failed"); |
| 570 | return -ENOMEM; |
| 571 | } |
| 572 | data->part_array = part; |
| 573 | |
| 574 | /* main parsing loop */ |
| 575 | i = 1; |
| 576 | eof = false; |
| 577 | p = start; |
| 578 | col = start; /* 1st column */ |
| 579 | end_of_line = false; |
| 580 | while (!eof) { |
| 581 | switch (*p) { |
| 582 | /* CR is ignored and replaced by NULL character */ |
| 583 | case '\r': |
| 584 | *p = '\0'; |
| 585 | p++; |
| 586 | continue; |
| 587 | case '\0': |
| 588 | end_of_line = true; |
| 589 | eof = true; |
| 590 | break; |
| 591 | case '\n': |
| 592 | end_of_line = true; |
| 593 | break; |
| 594 | case '\t': |
| 595 | break; |
| 596 | case '#': |
| 597 | /* comment line is skipped */ |
| 598 | if (column == 0 && p == col) { |
| 599 | while ((p < last) && *p) |
| 600 | if (*p++ == '\n') |
| 601 | break; |
| 602 | col = p; |
| 603 | i++; |
| 604 | if (p >= last || !*p) { |
| 605 | eof = true; |
| 606 | end_of_line = true; |
| 607 | } |
| 608 | continue; |
| 609 | } |
| 610 | /* fall through */ |
| 611 | /* by default continue with the next character */ |
| 612 | default: |
| 613 | p++; |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | /* replace by \0: allow string parsing for each column */ |
| 618 | *p = '\0'; |
| 619 | p++; |
| 620 | if (p >= last) { |
| 621 | eof = true; |
| 622 | end_of_line = true; |
| 623 | } |
| 624 | |
| 625 | /* skip empty line and multiple TAB in tsv file */ |
| 626 | if (strlen(col) == 0) { |
| 627 | col = p; |
| 628 | /* skip empty line */ |
| 629 | if (column == 0 && end_of_line) { |
| 630 | end_of_line = false; |
| 631 | i++; |
| 632 | } |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | if (column < COL_NB_STM32) { |
| 637 | ret = parse[column](data, i, col, part); |
| 638 | if (ret) |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | /* save the beginning of the next column */ |
| 643 | column++; |
| 644 | col = p; |
| 645 | |
| 646 | if (!end_of_line) |
| 647 | continue; |
| 648 | |
| 649 | /* end of the line detected */ |
| 650 | end_of_line = false; |
| 651 | |
| 652 | if (column < COL_NB_STM32) { |
| 653 | stm32prog_err("Layout line %d: no enought column", i); |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | column = 0; |
| 657 | part_nb++; |
| 658 | part++; |
| 659 | i++; |
| 660 | if (part_nb >= part_list_size) { |
| 661 | part = NULL; |
| 662 | if (!eof) { |
| 663 | stm32prog_err("Layout: no enought memory for %d part", |
| 664 | part_nb); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | data->part_nb = part_nb; |
| 670 | if (data->part_nb == 0) { |
| 671 | stm32prog_err("Layout: no partition found"); |
| 672 | return -ENODEV; |
| 673 | } |
| 674 | |
| 675 | return 0; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 679 | { |
| 680 | struct stm32prog_part_t *parta, *partb; |
| 681 | |
| 682 | parta = container_of(a, struct stm32prog_part_t, list); |
| 683 | partb = container_of(b, struct stm32prog_part_t, list); |
| 684 | |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 685 | if (parta->part_id != partb->part_id) |
| 686 | return parta->part_id - partb->part_id; |
| 687 | else |
| 688 | return parta->addr > partb->addr ? 1 : -1; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 691 | static void get_mtd_by_target(char *string, enum stm32prog_target target, |
| 692 | int dev_id) |
| 693 | { |
| 694 | const char *dev_str; |
| 695 | |
| 696 | switch (target) { |
| 697 | case STM32PROG_NOR: |
| 698 | dev_str = "nor"; |
| 699 | break; |
| 700 | case STM32PROG_NAND: |
| 701 | dev_str = "nand"; |
| 702 | break; |
| 703 | case STM32PROG_SPI_NAND: |
| 704 | dev_str = "spi-nand"; |
| 705 | break; |
| 706 | default: |
| 707 | dev_str = "invalid"; |
| 708 | break; |
| 709 | } |
| 710 | sprintf(string, "%s%d", dev_str, dev_id); |
| 711 | } |
| 712 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 713 | static int init_device(struct stm32prog_data *data, |
| 714 | struct stm32prog_dev_t *dev) |
| 715 | { |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 716 | struct mmc *mmc = NULL; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 717 | struct blk_desc *block_dev = NULL; |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 718 | struct mtd_info *mtd = NULL; |
| 719 | char mtd_id[16]; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 720 | int part_id; |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 721 | int ret; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 722 | u64 first_addr = 0, last_addr = 0; |
| 723 | struct stm32prog_part_t *part, *next_part; |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 724 | u64 part_addr, part_size; |
| 725 | bool part_found; |
| 726 | const char *part_name; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 727 | |
| 728 | switch (dev->target) { |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 729 | case STM32PROG_MMC: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 730 | if (!IS_ENABLED(CONFIG_MMC)) { |
| 731 | stm32prog_err("unknown device type = %d", dev->target); |
| 732 | return -ENODEV; |
| 733 | } |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 734 | mmc = find_mmc_device(dev->dev_id); |
Patrick Delaunay | ded48be | 2020-07-06 13:20:58 +0200 | [diff] [blame] | 735 | if (!mmc || mmc_init(mmc)) { |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 736 | stm32prog_err("mmc device %d not found", dev->dev_id); |
| 737 | return -ENODEV; |
| 738 | } |
| 739 | block_dev = mmc_get_blk_desc(mmc); |
| 740 | if (!block_dev) { |
| 741 | stm32prog_err("mmc device %d not probed", dev->dev_id); |
| 742 | return -ENODEV; |
| 743 | } |
| 744 | dev->erase_size = mmc->erase_grp_size * block_dev->blksz; |
| 745 | dev->mmc = mmc; |
| 746 | |
| 747 | /* reserve a full erase group for each GTP headers */ |
| 748 | if (mmc->erase_grp_size > GPT_HEADER_SZ) { |
| 749 | first_addr = dev->erase_size; |
| 750 | last_addr = (u64)(block_dev->lba - |
| 751 | mmc->erase_grp_size) * |
| 752 | block_dev->blksz; |
| 753 | } else { |
| 754 | first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz; |
| 755 | last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) * |
| 756 | block_dev->blksz; |
| 757 | } |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 758 | log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id, |
| 759 | block_dev->lba, block_dev->blksz); |
| 760 | log_debug(" available address = 0x%llx..0x%llx\n", |
| 761 | first_addr, last_addr); |
| 762 | log_debug(" full_update = %d\n", dev->full_update); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 763 | break; |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 764 | case STM32PROG_NOR: |
| 765 | case STM32PROG_NAND: |
| 766 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 767 | if (!IS_ENABLED(CONFIG_MTD)) { |
| 768 | stm32prog_err("unknown device type = %d", dev->target); |
| 769 | return -ENODEV; |
| 770 | } |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 771 | get_mtd_by_target(mtd_id, dev->target, dev->dev_id); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 772 | log_debug("%s\n", mtd_id); |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 773 | |
| 774 | mtdparts_init(); |
| 775 | mtd = get_mtd_device_nm(mtd_id); |
| 776 | if (IS_ERR(mtd)) { |
| 777 | stm32prog_err("MTD device %s not found", mtd_id); |
| 778 | return -ENODEV; |
| 779 | } |
| 780 | first_addr = 0; |
| 781 | last_addr = mtd->size; |
| 782 | dev->erase_size = mtd->erasesize; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 783 | log_debug("MTD device %s: size=%lld erasesize=%d\n", |
| 784 | mtd_id, mtd->size, mtd->erasesize); |
| 785 | log_debug(" available address = 0x%llx..0x%llx\n", |
| 786 | first_addr, last_addr); |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 787 | dev->mtd = mtd; |
| 788 | break; |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 789 | case STM32PROG_RAM: |
| 790 | first_addr = gd->bd->bi_dram[0].start; |
| 791 | last_addr = first_addr + gd->bd->bi_dram[0].size; |
| 792 | dev->erase_size = 1; |
| 793 | break; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 794 | default: |
| 795 | stm32prog_err("unknown device type = %d", dev->target); |
| 796 | return -ENODEV; |
| 797 | } |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 798 | log_debug(" erase size = 0x%x\n", dev->erase_size); |
| 799 | log_debug(" full_update = %d\n", dev->full_update); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 800 | |
| 801 | /* order partition list in offset order */ |
| 802 | list_sort(NULL, &dev->part_list, &part_cmp); |
| 803 | part_id = 1; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 804 | log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n"); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 805 | list_for_each_entry(part, &dev->part_list, list) { |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 806 | if (part->bin_nb > 1) { |
| 807 | if ((dev->target != STM32PROG_NAND && |
| 808 | dev->target != STM32PROG_SPI_NAND) || |
| 809 | part->id >= PHASE_FIRST_USER || |
| 810 | strncmp(part->name, "fsbl", 4)) { |
| 811 | stm32prog_err("%s (0x%x): multiple binary %d not supported", |
| 812 | part->name, part->id, |
| 813 | part->bin_nb); |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 817 | if (part->part_type == RAW_IMAGE) { |
| 818 | part->part_id = 0x0; |
| 819 | part->addr = 0x0; |
| 820 | if (block_dev) |
| 821 | part->size = block_dev->lba * block_dev->blksz; |
| 822 | else |
| 823 | part->size = last_addr; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 824 | log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n", |
| 825 | part->option, part->id, part->name, |
| 826 | part->part_type, part->bin_nb, part->target, |
| 827 | part->dev_id, part->addr, part->size); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 828 | continue; |
| 829 | } |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 830 | if (part->part_id < 0) { /* boot hw partition for eMMC */ |
| 831 | if (mmc) { |
| 832 | part->size = mmc->capacity_boot; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 833 | } else { |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 834 | stm32prog_err("%s (0x%x): hw partition not expected : %d", |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 835 | part->name, part->id, |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 836 | part->part_id); |
| 837 | return -ENODEV; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 838 | } |
| 839 | } else { |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 840 | part->part_id = part_id++; |
| 841 | |
| 842 | /* last partition : size to the end of the device */ |
| 843 | if (part->list.next != &dev->part_list) { |
| 844 | next_part = |
| 845 | container_of(part->list.next, |
| 846 | struct stm32prog_part_t, |
| 847 | list); |
| 848 | if (part->addr < next_part->addr) { |
| 849 | part->size = next_part->addr - |
| 850 | part->addr; |
| 851 | } else { |
| 852 | stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx", |
| 853 | part->name, part->id, |
| 854 | part->addr, |
| 855 | next_part->name, |
| 856 | next_part->id, |
| 857 | next_part->addr); |
| 858 | return -EINVAL; |
| 859 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 860 | } else { |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 861 | if (part->addr <= last_addr) { |
| 862 | part->size = last_addr - part->addr; |
| 863 | } else { |
| 864 | stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)", |
| 865 | part->name, part->id, |
| 866 | part->addr, last_addr); |
| 867 | return -EINVAL; |
| 868 | } |
| 869 | } |
| 870 | if (part->addr < first_addr) { |
| 871 | stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)", |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 872 | part->name, part->id, |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 873 | part->addr, first_addr); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 874 | return -EINVAL; |
| 875 | } |
| 876 | } |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 877 | if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) { |
| 878 | stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x", |
| 879 | part->name, part->id, part->addr, |
| 880 | part->dev->erase_size); |
| 881 | return -EINVAL; |
| 882 | } |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 883 | log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx", |
| 884 | part->part_id, part->option, part->id, part->name, |
| 885 | part->part_type, part->bin_nb, part->target, |
| 886 | part->dev_id, part->addr, part->size); |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 887 | |
| 888 | part_addr = 0; |
| 889 | part_size = 0; |
| 890 | part_found = false; |
| 891 | |
| 892 | /* check coherency with existing partition */ |
| 893 | if (block_dev) { |
| 894 | /* |
| 895 | * block devices with GPT: check user partition size |
| 896 | * only for partial update, the GPT partions are be |
| 897 | * created for full update |
| 898 | */ |
| 899 | if (dev->full_update || part->part_id < 0) { |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 900 | log_debug("\n"); |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 901 | continue; |
| 902 | } |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 903 | struct disk_partition partinfo; |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 904 | |
| 905 | ret = part_get_info(block_dev, part->part_id, |
| 906 | &partinfo); |
| 907 | |
| 908 | if (ret) { |
| 909 | stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d", |
| 910 | part->name, part->id, |
| 911 | part_id, part->dev_id); |
| 912 | return -ENODEV; |
| 913 | } |
| 914 | part_addr = (u64)partinfo.start * partinfo.blksz; |
| 915 | part_size = (u64)partinfo.size * partinfo.blksz; |
| 916 | part_name = (char *)partinfo.name; |
| 917 | part_found = true; |
| 918 | } |
| 919 | |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 920 | if (IS_ENABLED(CONFIG_MTD) && mtd) { |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 921 | char mtd_part_id[32]; |
| 922 | struct part_info *mtd_part; |
| 923 | struct mtd_device *mtd_dev; |
| 924 | u8 part_num; |
| 925 | |
| 926 | sprintf(mtd_part_id, "%s,%d", mtd_id, |
| 927 | part->part_id - 1); |
| 928 | ret = find_dev_and_part(mtd_part_id, &mtd_dev, |
| 929 | &part_num, &mtd_part); |
| 930 | if (ret != 0) { |
| 931 | stm32prog_err("%s (0x%x): Invalid MTD partition %s", |
| 932 | part->name, part->id, |
| 933 | mtd_part_id); |
| 934 | return -ENODEV; |
| 935 | } |
| 936 | part_addr = mtd_part->offset; |
| 937 | part_size = mtd_part->size; |
| 938 | part_name = mtd_part->name; |
| 939 | part_found = true; |
| 940 | } |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 941 | |
Patrick Delaunay | 53de79f | 2020-10-15 14:28:17 +0200 | [diff] [blame] | 942 | /* no partition for this device */ |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 943 | if (!part_found) { |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 944 | log_debug("\n"); |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 945 | continue; |
| 946 | } |
| 947 | |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 948 | log_debug(" %08llx %08llx\n", part_addr, part_size); |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 949 | |
| 950 | if (part->addr != part_addr) { |
| 951 | stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected", |
| 952 | part->name, part->id, part->part_id, |
| 953 | part_name, part->addr, part_addr); |
| 954 | return -ENODEV; |
| 955 | } |
| 956 | if (part->size != part_size) { |
| 957 | stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected", |
| 958 | part->name, part->id, part->part_id, |
| 959 | part_name, part->addr, part->size, |
| 960 | part_size); |
| 961 | return -ENODEV; |
| 962 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 963 | } |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static int treat_partition_list(struct stm32prog_data *data) |
| 968 | { |
| 969 | int i, j; |
| 970 | struct stm32prog_part_t *part; |
| 971 | |
| 972 | for (j = 0; j < STM32PROG_MAX_DEV; j++) { |
| 973 | data->dev[j].target = STM32PROG_NONE; |
| 974 | INIT_LIST_HEAD(&data->dev[j].part_list); |
| 975 | } |
| 976 | |
Patrick Delaunay | f64d32a | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 977 | #ifdef CONFIG_STM32MP15x_STM32IMAGE |
Patrick Delaunay | 8f035f7 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 978 | data->tee_detected = false; |
Patrick Delaunay | f64d32a | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 979 | #endif |
Patrick Delaunay | 8f035f7 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 980 | data->fsbl_nor_detected = false; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 981 | for (i = 0; i < data->part_nb; i++) { |
| 982 | part = &data->part_array[i]; |
| 983 | part->alt_id = -1; |
| 984 | |
| 985 | /* skip partition with IP="none" */ |
| 986 | if (part->target == STM32PROG_NONE) { |
| 987 | if (IS_SELECT(part)) { |
Patrick Delaunay | 94e561f | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 988 | stm32prog_err("Layout: selected none phase = 0x%x for part %s", |
| 989 | part->id, part->name); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 990 | return -EINVAL; |
| 991 | } |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | if (part->id == PHASE_FLASHLAYOUT || |
| 996 | part->id > PHASE_LAST_USER) { |
Patrick Delaunay | 94e561f | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 997 | stm32prog_err("Layout: invalid phase = 0x%x for part %s", |
| 998 | part->id, part->name); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 999 | return -EINVAL; |
| 1000 | } |
| 1001 | for (j = i + 1; j < data->part_nb; j++) { |
| 1002 | if (part->id == data->part_array[j].id) { |
Patrick Delaunay | 94e561f | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 1003 | stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s", |
| 1004 | part->id, part->name, data->part_array[j].name); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | } |
| 1008 | for (j = 0; j < STM32PROG_MAX_DEV; j++) { |
| 1009 | if (data->dev[j].target == STM32PROG_NONE) { |
| 1010 | /* new device found */ |
| 1011 | data->dev[j].target = part->target; |
| 1012 | data->dev[j].dev_id = part->dev_id; |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1013 | data->dev[j].full_update = true; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1014 | data->dev_nb++; |
| 1015 | break; |
| 1016 | } else if ((part->target == data->dev[j].target) && |
| 1017 | (part->dev_id == data->dev[j].dev_id)) { |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | if (j == STM32PROG_MAX_DEV) { |
| 1022 | stm32prog_err("Layout: too many device"); |
| 1023 | return -EINVAL; |
| 1024 | } |
Patrick Delaunay | 8f035f7 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1025 | switch (part->target) { |
| 1026 | case STM32PROG_NOR: |
| 1027 | if (!data->fsbl_nor_detected && |
| 1028 | !strncmp(part->name, "fsbl", 4)) |
| 1029 | data->fsbl_nor_detected = true; |
| 1030 | /* fallthrough */ |
| 1031 | case STM32PROG_NAND: |
| 1032 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | f64d32a | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 1033 | #ifdef CONFIG_STM32MP15x_STM32IMAGE |
Patrick Delaunay | 8f035f7 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1034 | if (!data->tee_detected && |
| 1035 | !strncmp(part->name, "tee", 3)) |
| 1036 | data->tee_detected = true; |
| 1037 | break; |
Patrick Delaunay | f64d32a | 2021-07-26 11:21:38 +0200 | [diff] [blame] | 1038 | #endif |
Patrick Delaunay | 8f035f7 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1039 | default: |
| 1040 | break; |
| 1041 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1042 | part->dev = &data->dev[j]; |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1043 | if (!IS_SELECT(part)) |
| 1044 | part->dev->full_update = false; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1045 | list_add_tail(&part->list, &data->dev[j].part_list); |
| 1046 | } |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1051 | static int create_gpt_partitions(struct stm32prog_data *data) |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1052 | { |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1053 | int offset = 0; |
| 1054 | const int buflen = SZ_8K; |
| 1055 | char *buf; |
| 1056 | char uuid[UUID_STR_LEN + 1]; |
| 1057 | unsigned char *uuid_bin; |
| 1058 | unsigned int mmc_id; |
| 1059 | int i; |
| 1060 | bool rootfs_found; |
| 1061 | struct stm32prog_part_t *part; |
| 1062 | |
| 1063 | buf = malloc(buflen); |
| 1064 | if (!buf) |
| 1065 | return -ENOMEM; |
| 1066 | |
| 1067 | puts("partitions : "); |
| 1068 | /* initialize the selected device */ |
| 1069 | for (i = 0; i < data->dev_nb; i++) { |
Patrick Delaunay | ffc405e | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1070 | /* create gpt partition support only for full update on MMC */ |
| 1071 | if (data->dev[i].target != STM32PROG_MMC || |
| 1072 | !data->dev[i].full_update) |
| 1073 | continue; |
| 1074 | |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1075 | offset = 0; |
| 1076 | rootfs_found = false; |
| 1077 | memset(buf, 0, buflen); |
| 1078 | |
| 1079 | list_for_each_entry(part, &data->dev[i].part_list, list) { |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1080 | /* skip eMMC boot partitions */ |
| 1081 | if (part->part_id < 0) |
| 1082 | continue; |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1083 | /* skip Raw Image */ |
| 1084 | if (part->part_type == RAW_IMAGE) |
| 1085 | continue; |
| 1086 | |
| 1087 | if (offset + 100 > buflen) { |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1088 | log_debug("\n%s: buffer too small, %s skippped", |
| 1089 | __func__, part->name); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1090 | continue; |
| 1091 | } |
| 1092 | |
| 1093 | if (!offset) |
| 1094 | offset += sprintf(buf, "gpt write mmc %d \"", |
| 1095 | data->dev[i].dev_id); |
| 1096 | |
| 1097 | offset += snprintf(buf + offset, buflen - offset, |
| 1098 | "name=%s,start=0x%llx,size=0x%llx", |
| 1099 | part->name, |
| 1100 | part->addr, |
| 1101 | part->size); |
| 1102 | |
| 1103 | if (part->part_type == PART_BINARY) |
| 1104 | offset += snprintf(buf + offset, |
| 1105 | buflen - offset, |
| 1106 | ",type=" |
| 1107 | LINUX_RESERVED_UUID); |
| 1108 | else |
| 1109 | offset += snprintf(buf + offset, |
| 1110 | buflen - offset, |
| 1111 | ",type=linux"); |
| 1112 | |
| 1113 | if (part->part_type == PART_SYSTEM) |
| 1114 | offset += snprintf(buf + offset, |
| 1115 | buflen - offset, |
| 1116 | ",bootable"); |
| 1117 | |
| 1118 | if (!rootfs_found && !strcmp(part->name, "rootfs")) { |
| 1119 | mmc_id = part->dev_id; |
| 1120 | rootfs_found = true; |
| 1121 | if (mmc_id < ARRAY_SIZE(uuid_mmc)) { |
| 1122 | uuid_bin = |
| 1123 | (unsigned char *)uuid_mmc[mmc_id].b; |
| 1124 | uuid_bin_to_str(uuid_bin, uuid, |
| 1125 | UUID_STR_FORMAT_GUID); |
| 1126 | offset += snprintf(buf + offset, |
| 1127 | buflen - offset, |
| 1128 | ",uuid=%s", uuid); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | offset += snprintf(buf + offset, buflen - offset, ";"); |
| 1133 | } |
| 1134 | |
| 1135 | if (offset) { |
| 1136 | offset += snprintf(buf + offset, buflen - offset, "\""); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1137 | log_debug("\ncmd: %s\n", buf); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1138 | if (run_command(buf, 0)) { |
| 1139 | stm32prog_err("GPT partitionning fail: %s", |
| 1140 | buf); |
| 1141 | free(buf); |
| 1142 | |
| 1143 | return -1; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (data->dev[i].mmc) |
| 1148 | part_init(mmc_get_blk_desc(data->dev[i].mmc)); |
| 1149 | |
| 1150 | #ifdef DEBUG |
| 1151 | sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1152 | log_debug("\ncmd: %s", buf); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1153 | if (run_command(buf, 0)) |
| 1154 | printf("fail !\n"); |
| 1155 | else |
| 1156 | printf("OK\n"); |
| 1157 | |
| 1158 | sprintf(buf, "part list mmc %d", data->dev[i].dev_id); |
| 1159 | run_command(buf, 0); |
| 1160 | #endif |
| 1161 | } |
| 1162 | puts("done\n"); |
| 1163 | |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1164 | #ifdef DEBUG |
| 1165 | run_command("mtd list", 0); |
| 1166 | #endif |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1167 | free(buf); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1172 | static int stm32prog_alt_add(struct stm32prog_data *data, |
| 1173 | struct dfu_entity *dfu, |
| 1174 | struct stm32prog_part_t *part) |
| 1175 | { |
| 1176 | int ret = 0; |
| 1177 | int offset = 0; |
| 1178 | char devstr[10]; |
| 1179 | char dfustr[10]; |
| 1180 | char buf[ALT_BUF_LEN]; |
| 1181 | u32 size; |
| 1182 | char multiplier, type; |
| 1183 | |
| 1184 | /* max 3 digit for sector size */ |
| 1185 | if (part->size > SZ_1M) { |
| 1186 | size = (u32)(part->size / SZ_1M); |
| 1187 | multiplier = 'M'; |
| 1188 | } else if (part->size > SZ_1K) { |
| 1189 | size = (u32)(part->size / SZ_1K); |
| 1190 | multiplier = 'K'; |
| 1191 | } else { |
| 1192 | size = (u32)part->size; |
| 1193 | multiplier = 'B'; |
| 1194 | } |
| 1195 | if (IS_SELECT(part) && !IS_EMPTY(part)) |
| 1196 | type = 'e'; /*Readable and Writeable*/ |
| 1197 | else |
| 1198 | type = 'a';/*Readable*/ |
| 1199 | |
| 1200 | memset(buf, 0, sizeof(buf)); |
| 1201 | offset = snprintf(buf, ALT_BUF_LEN - offset, |
| 1202 | "@%s/0x%02x/1*%d%c%c ", |
| 1203 | part->name, part->id, |
| 1204 | size, multiplier, type); |
| 1205 | |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1206 | if (part->target == STM32PROG_RAM) { |
| 1207 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1208 | "ram 0x%llx 0x%llx", |
| 1209 | part->addr, part->size); |
| 1210 | } else if (part->part_type == RAW_IMAGE) { |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1211 | u64 dfu_size; |
| 1212 | |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1213 | if (part->dev->target == STM32PROG_MMC) |
| 1214 | dfu_size = part->size / part->dev->mmc->read_bl_len; |
| 1215 | else |
| 1216 | dfu_size = part->size; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1217 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1218 | "raw 0x0 0x%llx", dfu_size); |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1219 | } else if (part->part_id < 0) { |
| 1220 | u64 nb_blk = part->size / part->dev->mmc->read_bl_len; |
| 1221 | |
| 1222 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1223 | "raw 0x%llx 0x%llx", |
| 1224 | part->addr, nb_blk); |
| 1225 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1226 | " mmcpart %d;", -(part->part_id)); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1227 | } else { |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1228 | if (part->part_type == PART_SYSTEM && |
| 1229 | (part->target == STM32PROG_NAND || |
| 1230 | part->target == STM32PROG_NOR || |
| 1231 | part->target == STM32PROG_SPI_NAND)) |
| 1232 | offset += snprintf(buf + offset, |
| 1233 | ALT_BUF_LEN - offset, |
| 1234 | "partubi"); |
| 1235 | else |
| 1236 | offset += snprintf(buf + offset, |
| 1237 | ALT_BUF_LEN - offset, |
| 1238 | "part"); |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1239 | /* dev_id requested by DFU MMC */ |
| 1240 | if (part->target == STM32PROG_MMC) |
| 1241 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1242 | " %d", part->dev_id); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1243 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1244 | " %d;", part->part_id); |
| 1245 | } |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1246 | ret = -ENODEV; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1247 | switch (part->target) { |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1248 | case STM32PROG_MMC: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1249 | if (IS_ENABLED(CONFIG_MMC)) { |
| 1250 | ret = 0; |
| 1251 | sprintf(dfustr, "mmc"); |
| 1252 | sprintf(devstr, "%d", part->dev_id); |
| 1253 | } |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1254 | break; |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1255 | case STM32PROG_NAND: |
| 1256 | case STM32PROG_NOR: |
| 1257 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1258 | if (IS_ENABLED(CONFIG_MTD)) { |
| 1259 | ret = 0; |
| 1260 | sprintf(dfustr, "mtd"); |
| 1261 | get_mtd_by_target(devstr, part->target, part->dev_id); |
| 1262 | } |
Patrick Delaunay | eb845d6 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1263 | break; |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1264 | case STM32PROG_RAM: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1265 | ret = 0; |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1266 | sprintf(dfustr, "ram"); |
| 1267 | sprintf(devstr, "0"); |
| 1268 | break; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1269 | default: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1270 | break; |
| 1271 | } |
| 1272 | if (ret) { |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1273 | stm32prog_err("invalid target: %d", part->target); |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1274 | return ret; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1275 | } |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1276 | log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1277 | ret = dfu_alt_add(dfu, dfustr, devstr, buf); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1278 | log_debug("dfu_alt_add(%s,%s,%s) result %d\n", |
| 1279 | dfustr, devstr, buf, ret); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | static int stm32prog_alt_add_virt(struct dfu_entity *dfu, |
| 1285 | char *name, int phase, int size) |
| 1286 | { |
| 1287 | int ret = 0; |
| 1288 | char devstr[4]; |
| 1289 | char buf[ALT_BUF_LEN]; |
| 1290 | |
| 1291 | sprintf(devstr, "%d", phase); |
| 1292 | sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size); |
| 1293 | ret = dfu_alt_add(dfu, "virt", devstr, buf); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1294 | log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1295 | |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static int dfu_init_entities(struct stm32prog_data *data) |
| 1300 | { |
| 1301 | int ret = 0; |
| 1302 | int phase, i, alt_id; |
| 1303 | struct stm32prog_part_t *part; |
| 1304 | struct dfu_entity *dfu; |
| 1305 | int alt_nb; |
| 1306 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1307 | alt_nb = 1; /* number of virtual = CMD*/ |
| 1308 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) |
| 1309 | alt_nb++; /* OTP*/ |
Patrick Delaunay | b57efb2 | 2021-05-18 15:12:09 +0200 | [diff] [blame] | 1310 | if (CONFIG_IS_ENABLED(DM_PMIC)) |
| 1311 | alt_nb++; /* PMIC NVMEM*/ |
| 1312 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1313 | if (data->part_nb == 0) |
| 1314 | alt_nb++; /* +1 for FlashLayout */ |
| 1315 | else |
| 1316 | for (i = 0; i < data->part_nb; i++) { |
| 1317 | if (data->part_array[i].target != STM32PROG_NONE) |
| 1318 | alt_nb++; |
| 1319 | } |
| 1320 | |
| 1321 | if (dfu_alt_init(alt_nb, &dfu)) |
| 1322 | return -ENODEV; |
| 1323 | |
| 1324 | puts("DFU alt info setting: "); |
| 1325 | if (data->part_nb) { |
| 1326 | alt_id = 0; |
| 1327 | for (phase = 1; |
| 1328 | (phase <= PHASE_LAST_USER) && |
| 1329 | (alt_id < alt_nb) && !ret; |
| 1330 | phase++) { |
| 1331 | /* ordering alt setting by phase id */ |
| 1332 | part = NULL; |
| 1333 | for (i = 0; i < data->part_nb; i++) { |
| 1334 | if (phase == data->part_array[i].id) { |
| 1335 | part = &data->part_array[i]; |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | if (!part) |
| 1340 | continue; |
| 1341 | if (part->target == STM32PROG_NONE) |
| 1342 | continue; |
| 1343 | part->alt_id = alt_id; |
| 1344 | alt_id++; |
| 1345 | |
| 1346 | ret = stm32prog_alt_add(data, dfu, part); |
| 1347 | } |
| 1348 | } else { |
| 1349 | char buf[ALT_BUF_LEN]; |
| 1350 | |
| 1351 | sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000", |
| 1352 | PHASE_FLASHLAYOUT, STM32_DDR_BASE); |
| 1353 | ret = dfu_alt_add(dfu, "ram", NULL, buf); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1354 | log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | if (!ret) |
Patrick Delaunay | b18c3ab | 2021-07-05 09:39:01 +0200 | [diff] [blame] | 1358 | ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1359 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1360 | if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1361 | ret = optee_ta_open(data); |
| 1362 | log_debug("optee_ta result %d\n", ret); |
| 1363 | ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, |
| 1364 | data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC); |
| 1365 | } |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1366 | |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1367 | if (!ret && CONFIG_IS_ENABLED(DM_PMIC)) |
Patrick Delaunay | b18c3ab | 2021-07-05 09:39:01 +0200 | [diff] [blame] | 1368 | ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE); |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1369 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1370 | if (ret) |
| 1371 | stm32prog_err("dfu init failed: %d", ret); |
| 1372 | puts("done\n"); |
| 1373 | |
| 1374 | #ifdef DEBUG |
| 1375 | dfu_show_entities(); |
| 1376 | #endif |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1380 | int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1381 | long *size) |
| 1382 | { |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1383 | u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1384 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1385 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1386 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
| 1387 | stm32prog_err("OTP update not supported"); |
| 1388 | |
| 1389 | return -EOPNOTSUPP; |
| 1390 | } |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1391 | |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1392 | if (!data->otp_part) { |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1393 | data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1394 | if (!data->otp_part) |
| 1395 | return -ENOMEM; |
| 1396 | } |
| 1397 | |
| 1398 | if (!offset) |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1399 | memset(data->otp_part, 0, otp_size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1400 | |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1401 | if (offset + *size > otp_size) |
| 1402 | *size = otp_size - offset; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1403 | |
| 1404 | memcpy((void *)((u32)data->otp_part + offset), buffer, *size); |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1410 | long *size) |
| 1411 | { |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1412 | u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1413 | int result = 0; |
| 1414 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1415 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1416 | stm32prog_err("OTP update not supported"); |
| 1417 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1418 | return -EOPNOTSUPP; |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1421 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1422 | /* alway read for first packet */ |
| 1423 | if (!offset) { |
| 1424 | if (!data->otp_part) |
| 1425 | data->otp_part = |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1426 | memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1427 | |
| 1428 | if (!data->otp_part) { |
| 1429 | result = -ENOMEM; |
| 1430 | goto end_otp_read; |
| 1431 | } |
| 1432 | |
| 1433 | /* init struct with 0 */ |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1434 | memset(data->otp_part, 0, otp_size); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1435 | |
| 1436 | /* call the service */ |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1437 | result = -EOPNOTSUPP; |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1438 | if (data->tee && CONFIG_IS_ENABLED(OPTEE)) |
| 1439 | result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP, |
| 1440 | data->otp_part, OTP_SIZE_TA); |
| 1441 | else if (IS_ENABLED(CONFIG_ARM_SMCCC)) |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1442 | result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL, |
| 1443 | (u32)data->otp_part, 0); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1444 | if (result) |
| 1445 | goto end_otp_read; |
| 1446 | } |
| 1447 | |
| 1448 | if (!data->otp_part) { |
| 1449 | result = -ENOMEM; |
| 1450 | goto end_otp_read; |
| 1451 | } |
| 1452 | |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1453 | if (offset + *size > otp_size) |
| 1454 | *size = otp_size - offset; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1455 | memcpy(buffer, (void *)((u32)data->otp_part + offset), *size); |
| 1456 | |
| 1457 | end_otp_read: |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1458 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1459 | |
| 1460 | return result; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | int stm32prog_otp_start(struct stm32prog_data *data) |
| 1464 | { |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1465 | int result = 0; |
| 1466 | struct arm_smccc_res res; |
| 1467 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1468 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1469 | stm32prog_err("OTP update not supported"); |
| 1470 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1471 | return -EOPNOTSUPP; |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1474 | if (!data->otp_part) { |
| 1475 | stm32prog_err("start OTP without data"); |
| 1476 | return -1; |
| 1477 | } |
| 1478 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1479 | result = -EOPNOTSUPP; |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1480 | if (data->tee && CONFIG_IS_ENABLED(OPTEE)) { |
| 1481 | result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP, |
| 1482 | data->otp_part, OTP_SIZE_TA); |
| 1483 | } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) { |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1484 | arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, |
| 1485 | (u32)data->otp_part, 0, 0, 0, 0, 0, &res); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1486 | |
Patrick Delaunay | 72450b4 | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1487 | if (!res.a0) { |
| 1488 | switch (res.a1) { |
| 1489 | case 0: |
| 1490 | result = 0; |
| 1491 | break; |
| 1492 | case 1: |
| 1493 | stm32prog_err("Provisioning"); |
| 1494 | result = 0; |
| 1495 | break; |
| 1496 | default: |
| 1497 | log_err("%s: OTP incorrect value (err = %ld)\n", |
| 1498 | __func__, res.a1); |
| 1499 | result = -EINVAL; |
| 1500 | break; |
| 1501 | } |
| 1502 | } else { |
| 1503 | log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n", |
| 1504 | __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1505 | result = -EINVAL; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1506 | } |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | free(data->otp_part); |
| 1510 | data->otp_part = NULL; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1511 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1512 | |
| 1513 | return result; |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1516 | int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1517 | long *size) |
| 1518 | { |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1519 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1520 | |
| 1521 | if (!offset) |
| 1522 | memset(data->pmic_part, 0, PMIC_SIZE); |
| 1523 | |
| 1524 | if (offset + *size > PMIC_SIZE) |
| 1525 | *size = PMIC_SIZE - offset; |
| 1526 | |
| 1527 | memcpy(&data->pmic_part[offset], buffer, *size); |
| 1528 | |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1533 | long *size) |
| 1534 | { |
| 1535 | int result = 0, ret; |
| 1536 | struct udevice *dev; |
| 1537 | |
| 1538 | if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) { |
| 1539 | stm32prog_err("PMIC update not supported"); |
| 1540 | |
| 1541 | return -EOPNOTSUPP; |
| 1542 | } |
| 1543 | |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1544 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1545 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1546 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1547 | &dev); |
| 1548 | if (ret) |
| 1549 | return ret; |
| 1550 | |
| 1551 | /* alway request PMIC for first packet */ |
| 1552 | if (!offset) { |
| 1553 | /* init struct with 0 */ |
| 1554 | memset(data->pmic_part, 0, PMIC_SIZE); |
| 1555 | |
| 1556 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1557 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1558 | &dev); |
| 1559 | if (ret) |
| 1560 | return ret; |
| 1561 | |
| 1562 | ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE); |
| 1563 | if (ret < 0) { |
| 1564 | result = ret; |
| 1565 | goto end_pmic_read; |
| 1566 | } |
| 1567 | if (ret != PMIC_SIZE) { |
| 1568 | result = -EACCES; |
| 1569 | goto end_pmic_read; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | if (offset + *size > PMIC_SIZE) |
| 1574 | *size = PMIC_SIZE - offset; |
| 1575 | |
| 1576 | memcpy(buffer, &data->pmic_part[offset], *size); |
| 1577 | |
| 1578 | end_pmic_read: |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1579 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1580 | return result; |
| 1581 | } |
| 1582 | |
| 1583 | int stm32prog_pmic_start(struct stm32prog_data *data) |
| 1584 | { |
| 1585 | int ret; |
| 1586 | struct udevice *dev; |
| 1587 | |
| 1588 | if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) { |
| 1589 | stm32prog_err("PMIC update not supported"); |
| 1590 | |
| 1591 | return -EOPNOTSUPP; |
| 1592 | } |
| 1593 | |
| 1594 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1595 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1596 | &dev); |
| 1597 | if (ret) |
| 1598 | return ret; |
| 1599 | |
| 1600 | return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE); |
| 1601 | } |
| 1602 | |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1603 | /* copy FSBL on NAND to improve reliability on NAND */ |
| 1604 | static int stm32prog_copy_fsbl(struct stm32prog_part_t *part) |
| 1605 | { |
| 1606 | int ret, i; |
| 1607 | void *fsbl; |
| 1608 | struct image_header_s header; |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 1609 | struct stm32_header_v2 raw_header; /* V2 size > v1 size */ |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1610 | struct dfu_entity *dfu; |
| 1611 | long size, offset; |
| 1612 | |
| 1613 | if (part->target != STM32PROG_NAND && |
| 1614 | part->target != STM32PROG_SPI_NAND) |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1615 | return -EINVAL; |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1616 | |
| 1617 | dfu = dfu_get_entity(part->alt_id); |
| 1618 | |
| 1619 | /* read header */ |
| 1620 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 1621 | size = sizeof(raw_header); |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1622 | ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size); |
| 1623 | if (ret) |
| 1624 | return ret; |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1625 | |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 1626 | stm32prog_header_check((ulong)&raw_header, &header); |
| 1627 | if (header.type != HEADER_STM32IMAGE && |
| 1628 | header.type != HEADER_STM32IMAGE_V2) |
Patrick Delaunay | 4fb7b3e | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1629 | return -ENOENT; |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1630 | |
| 1631 | /* read header + payload */ |
Patrick Delaunay | 49d0ecb1 | 2022-03-28 19:25:29 +0200 | [diff] [blame^] | 1632 | size = header.image_length + header.length; |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1633 | size = round_up(size, part->dev->mtd->erasesize); |
| 1634 | fsbl = calloc(1, size); |
| 1635 | if (!fsbl) |
| 1636 | return -ENOMEM; |
| 1637 | ret = dfu->read_medium(dfu, 0, fsbl, &size); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1638 | log_debug("%s read size=%lx ret=%d\n", __func__, size, ret); |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1639 | if (ret) |
| 1640 | goto error; |
| 1641 | |
| 1642 | dfu_transaction_cleanup(dfu); |
| 1643 | offset = 0; |
| 1644 | for (i = part->bin_nb - 1; i > 0; i--) { |
| 1645 | offset += size; |
| 1646 | /* write to the next erase block */ |
| 1647 | ret = dfu->write_medium(dfu, offset, fsbl, &size); |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1648 | log_debug("%s copy at ofset=%lx size=%lx ret=%d", |
| 1649 | __func__, offset, size, ret); |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1650 | if (ret) |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | error: |
| 1655 | free(fsbl); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
Patrick Delaunay | d68e53b | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1659 | static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset) |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1660 | { |
| 1661 | if (data->phase == PHASE_FLASHLAYOUT) { |
| 1662 | if (parse_flash_layout(data, STM32_DDR_BASE, 0)) |
| 1663 | stm32prog_err("Layout: invalid FlashLayout"); |
| 1664 | return; |
| 1665 | } |
| 1666 | |
| 1667 | if (!data->cur_part) |
| 1668 | return; |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1669 | |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1670 | if (data->cur_part->target == STM32PROG_RAM) { |
| 1671 | if (data->cur_part->part_type == PART_SYSTEM) |
| 1672 | data->uimage = data->cur_part->addr; |
| 1673 | if (data->cur_part->part_type == PART_FILESYSTEM) |
| 1674 | data->dtb = data->cur_part->addr; |
Patrick Delaunay | d68e53b | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1675 | if (data->cur_part->part_type == PART_BINARY) { |
| 1676 | data->initrd = data->cur_part->addr; |
| 1677 | data->initrd_size = offset; |
| 1678 | } |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1679 | } |
| 1680 | |
Patrick Delaunay | 878f754 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1681 | if (CONFIG_IS_ENABLED(MMC) && |
| 1682 | data->cur_part->part_id < 0) { |
| 1683 | char cmdbuf[60]; |
| 1684 | |
| 1685 | sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0", |
| 1686 | data->cur_part->dev_id, data->cur_part->dev_id, |
| 1687 | -(data->cur_part->part_id)); |
| 1688 | if (run_command(cmdbuf, 0)) { |
| 1689 | stm32prog_err("commands '%s' failed", cmdbuf); |
| 1690 | return; |
| 1691 | } |
| 1692 | } |
Patrick Delaunay | b83caf9 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1693 | |
| 1694 | if (CONFIG_IS_ENABLED(MTD) && |
| 1695 | data->cur_part->bin_nb > 1) { |
| 1696 | if (stm32prog_copy_fsbl(data->cur_part)) { |
| 1697 | stm32prog_err("%s (0x%x): copy of fsbl failed", |
| 1698 | data->cur_part->name, data->cur_part->id); |
| 1699 | return; |
| 1700 | } |
| 1701 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | void stm32prog_do_reset(struct stm32prog_data *data) |
| 1705 | { |
| 1706 | if (data->phase == PHASE_RESET) { |
| 1707 | data->phase = PHASE_DO_RESET; |
| 1708 | puts("Reset requested\n"); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | void stm32prog_next_phase(struct stm32prog_data *data) |
| 1713 | { |
| 1714 | int phase, i; |
| 1715 | struct stm32prog_part_t *part; |
| 1716 | bool found; |
| 1717 | |
| 1718 | phase = data->phase; |
| 1719 | switch (phase) { |
| 1720 | case PHASE_RESET: |
| 1721 | case PHASE_END: |
| 1722 | case PHASE_DO_RESET: |
| 1723 | return; |
| 1724 | } |
| 1725 | |
| 1726 | /* found next selected partition */ |
Patrick Delaunay | 468f050 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1727 | data->dfu_seq = 0; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1728 | data->cur_part = NULL; |
| 1729 | data->phase = PHASE_END; |
| 1730 | found = false; |
| 1731 | do { |
| 1732 | phase++; |
| 1733 | if (phase > PHASE_LAST_USER) |
| 1734 | break; |
| 1735 | for (i = 0; i < data->part_nb; i++) { |
| 1736 | part = &data->part_array[i]; |
| 1737 | if (part->id == phase) { |
| 1738 | if (IS_SELECT(part) && !IS_EMPTY(part)) { |
| 1739 | data->cur_part = part; |
| 1740 | data->phase = phase; |
| 1741 | found = true; |
| 1742 | } |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | } while (!found); |
| 1747 | |
| 1748 | if (data->phase == PHASE_END) |
| 1749 | puts("Phase=END\n"); |
| 1750 | } |
| 1751 | |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1752 | static int part_delete(struct stm32prog_data *data, |
| 1753 | struct stm32prog_part_t *part) |
| 1754 | { |
| 1755 | int ret = 0; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1756 | unsigned long blks, blks_offset, blks_size; |
| 1757 | struct blk_desc *block_dev = NULL; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1758 | char cmdbuf[40]; |
| 1759 | char devstr[10]; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1760 | |
| 1761 | printf("Erasing %s ", part->name); |
| 1762 | switch (part->target) { |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1763 | case STM32PROG_MMC: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1764 | if (!IS_ENABLED(CONFIG_MMC)) { |
| 1765 | ret = -1; |
| 1766 | stm32prog_err("%s (0x%x): erase invalid", |
| 1767 | part->name, part->id); |
| 1768 | break; |
| 1769 | } |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1770 | printf("on mmc %d: ", part->dev->dev_id); |
| 1771 | block_dev = mmc_get_blk_desc(part->dev->mmc); |
| 1772 | blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len); |
| 1773 | blks_size = lldiv(part->size, part->dev->mmc->read_bl_len); |
| 1774 | /* -1 or -2 : delete boot partition of MMC |
| 1775 | * need to switch to associated hwpart 1 or 2 |
| 1776 | */ |
| 1777 | if (part->part_id < 0) |
| 1778 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, |
| 1779 | part->dev->dev_id, |
| 1780 | -part->part_id)) |
| 1781 | return -1; |
| 1782 | |
| 1783 | blks = blk_derase(block_dev, blks_offset, blks_size); |
| 1784 | |
| 1785 | /* return to user partition */ |
| 1786 | if (part->part_id < 0) |
| 1787 | blk_select_hwpart_devnum(IF_TYPE_MMC, |
| 1788 | part->dev->dev_id, 0); |
| 1789 | if (blks != blks_size) { |
| 1790 | ret = -1; |
| 1791 | stm32prog_err("%s (0x%x): MMC erase failed", |
| 1792 | part->name, part->id); |
| 1793 | } |
| 1794 | break; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1795 | case STM32PROG_NOR: |
| 1796 | case STM32PROG_NAND: |
| 1797 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1798 | if (!IS_ENABLED(CONFIG_MTD)) { |
| 1799 | ret = -1; |
| 1800 | stm32prog_err("%s (0x%x): erase invalid", |
| 1801 | part->name, part->id); |
| 1802 | break; |
| 1803 | } |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1804 | get_mtd_by_target(devstr, part->target, part->dev->dev_id); |
| 1805 | printf("on %s: ", devstr); |
| 1806 | sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx", |
| 1807 | devstr, part->addr, part->size); |
| 1808 | if (run_command(cmdbuf, 0)) { |
| 1809 | ret = -1; |
| 1810 | stm32prog_err("%s (0x%x): MTD erase commands failed (%s)", |
| 1811 | part->name, part->id, cmdbuf); |
| 1812 | } |
| 1813 | break; |
Patrick Delaunay | 306a5cf | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1814 | case STM32PROG_RAM: |
| 1815 | printf("on ram: "); |
| 1816 | memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size); |
| 1817 | break; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1818 | default: |
| 1819 | ret = -1; |
| 1820 | stm32prog_err("%s (0x%x): erase invalid", part->name, part->id); |
| 1821 | break; |
| 1822 | } |
| 1823 | if (!ret) |
| 1824 | printf("done\n"); |
| 1825 | |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1829 | static void stm32prog_devices_init(struct stm32prog_data *data) |
| 1830 | { |
| 1831 | int i; |
| 1832 | int ret; |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1833 | struct stm32prog_part_t *part; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1834 | |
| 1835 | ret = treat_partition_list(data); |
| 1836 | if (ret) |
| 1837 | goto error; |
| 1838 | |
| 1839 | /* initialize the selected device */ |
| 1840 | for (i = 0; i < data->dev_nb; i++) { |
| 1841 | ret = init_device(data, &data->dev[i]); |
| 1842 | if (ret) |
| 1843 | goto error; |
| 1844 | } |
| 1845 | |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1846 | /* delete RAW partition before create partition */ |
| 1847 | for (i = 0; i < data->part_nb; i++) { |
| 1848 | part = &data->part_array[i]; |
| 1849 | |
| 1850 | if (part->part_type != RAW_IMAGE) |
| 1851 | continue; |
| 1852 | |
| 1853 | if (!IS_SELECT(part) || !IS_DELETE(part)) |
| 1854 | continue; |
| 1855 | |
| 1856 | ret = part_delete(data, part); |
| 1857 | if (ret) |
| 1858 | goto error; |
| 1859 | } |
| 1860 | |
Patrick Delaunay | 9a2b054 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1861 | if (IS_ENABLED(CONFIG_MMC)) { |
| 1862 | ret = create_gpt_partitions(data); |
| 1863 | if (ret) |
| 1864 | goto error; |
| 1865 | } |
Patrick Delaunay | aff4c5d | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1866 | |
Patrick Delaunay | 2bad5d0 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1867 | /* delete partition GPT or MTD */ |
| 1868 | for (i = 0; i < data->part_nb; i++) { |
| 1869 | part = &data->part_array[i]; |
| 1870 | |
| 1871 | if (part->part_type == RAW_IMAGE) |
| 1872 | continue; |
| 1873 | |
| 1874 | if (!IS_SELECT(part) || !IS_DELETE(part)) |
| 1875 | continue; |
| 1876 | |
| 1877 | ret = part_delete(data, part); |
| 1878 | if (ret) |
| 1879 | goto error; |
| 1880 | } |
| 1881 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1882 | return; |
| 1883 | |
| 1884 | error: |
| 1885 | data->part_nb = 0; |
| 1886 | } |
| 1887 | |
| 1888 | int stm32prog_dfu_init(struct stm32prog_data *data) |
| 1889 | { |
| 1890 | /* init device if no error */ |
| 1891 | if (data->part_nb) |
| 1892 | stm32prog_devices_init(data); |
| 1893 | |
| 1894 | if (data->part_nb) |
| 1895 | stm32prog_next_phase(data); |
| 1896 | |
| 1897 | /* prepare DFU for device read/write */ |
| 1898 | dfu_free_entities(); |
| 1899 | return dfu_init_entities(data); |
| 1900 | } |
| 1901 | |
| 1902 | int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size) |
| 1903 | { |
| 1904 | memset(data, 0x0, sizeof(*data)); |
Patrick Delaunay | 468f050 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1905 | data->read_phase = PHASE_RESET; |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1906 | data->phase = PHASE_FLASHLAYOUT; |
| 1907 | |
| 1908 | return parse_flash_layout(data, addr, size); |
| 1909 | } |
| 1910 | |
| 1911 | void stm32prog_clean(struct stm32prog_data *data) |
| 1912 | { |
| 1913 | /* clean */ |
| 1914 | dfu_free_entities(); |
| 1915 | free(data->part_array); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1916 | free(data->otp_part); |
Patrick Delaunay | 468f050 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1917 | free(data->buffer); |
Patrick Delaunay | e82ab4c | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1918 | |
| 1919 | if (CONFIG_IS_ENABLED(OPTEE) && data->tee) { |
| 1920 | tee_close_session(data->tee, data->tee_session); |
| 1921 | data->tee = NULL; |
| 1922 | data->tee_session = 0x0; |
| 1923 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* DFU callback: used after serial and direct DFU USB access */ |
| 1927 | void dfu_flush_callback(struct dfu_entity *dfu) |
| 1928 | { |
| 1929 | if (!stm32prog_data) |
| 1930 | return; |
| 1931 | |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1932 | if (dfu->dev_type == DFU_DEV_VIRT) { |
| 1933 | if (dfu->data.virt.dev_num == PHASE_OTP) |
| 1934 | stm32prog_otp_start(stm32prog_data); |
Patrick Delaunay | 6ce1f4a | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1935 | else if (dfu->data.virt.dev_num == PHASE_PMIC) |
| 1936 | stm32prog_pmic_start(stm32prog_data); |
Patrick Delaunay | 936f1ae | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1937 | return; |
| 1938 | } |
| 1939 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1940 | if (dfu->dev_type == DFU_DEV_RAM) { |
| 1941 | if (dfu->alt == 0 && |
| 1942 | stm32prog_data->phase == PHASE_FLASHLAYOUT) { |
Patrick Delaunay | d68e53b | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1943 | stm32prog_end_phase(stm32prog_data, dfu->offset); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1944 | /* waiting DFU DETACH for reenumeration */ |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | if (!stm32prog_data->cur_part) |
| 1949 | return; |
| 1950 | |
| 1951 | if (dfu->alt == stm32prog_data->cur_part->alt_id) { |
Patrick Delaunay | d68e53b | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1952 | stm32prog_end_phase(stm32prog_data, dfu->offset); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1953 | stm32prog_next_phase(stm32prog_data); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | void dfu_initiated_callback(struct dfu_entity *dfu) |
| 1958 | { |
| 1959 | if (!stm32prog_data) |
| 1960 | return; |
| 1961 | |
| 1962 | if (!stm32prog_data->cur_part) |
| 1963 | return; |
| 1964 | |
| 1965 | /* force the saved offset for the current partition */ |
| 1966 | if (dfu->alt == stm32prog_data->cur_part->alt_id) { |
| 1967 | dfu->offset = stm32prog_data->offset; |
Patrick Delaunay | 468f050 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1968 | stm32prog_data->dfu_seq = 0; |
Patrick Delaunay | 711b5bc | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1969 | log_debug("dfu offset = 0x%llx\n", dfu->offset); |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1970 | } |
| 1971 | } |
Patrick Delaunay | 1f2e3d5 | 2021-05-18 15:12:13 +0200 | [diff] [blame] | 1972 | |
| 1973 | void dfu_error_callback(struct dfu_entity *dfu, const char *msg) |
| 1974 | { |
| 1975 | struct stm32prog_data *data = stm32prog_data; |
| 1976 | |
| 1977 | if (!stm32prog_data) |
| 1978 | return; |
| 1979 | |
| 1980 | if (!stm32prog_data->cur_part) |
| 1981 | return; |
| 1982 | |
| 1983 | if (dfu->alt == stm32prog_data->cur_part->alt_id) |
| 1984 | stm32prog_err(msg); |
| 1985 | } |