blob: 030614c14dd6ed6c87c5c8d64c7754fe92731020 [file] [log] [blame]
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4 */
5
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01007#include <console.h>
8#include <dfu.h>
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02009#include <image.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010010#include <malloc.h>
Patrick Delaunay541c7de2020-03-18 09:24:59 +010011#include <misc.h>
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010012#include <mmc.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010013#include <part.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020014#include <tee.h>
Patrick Delaunay1d96b182020-03-18 09:24:58 +010015#include <asm/arch/stm32mp1_smc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020017#include <dm/device_compat.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010018#include <dm/uclass.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010019#include <jffs2/load_kernel.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010020#include <linux/list.h>
21#include <linux/list_sort.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010022#include <linux/mtd/mtd.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010023#include <linux/sizes.h>
24
25#include "stm32prog.h"
26
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010027/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
28#define GPT_HEADER_SZ 34
29
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010030#define OPT_SELECT BIT(0)
31#define OPT_EMPTY BIT(1)
Patrick Delaunay291e7222020-03-18 09:24:57 +010032#define OPT_DELETE BIT(2)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010033
34#define IS_SELECT(part) ((part)->option & OPT_SELECT)
35#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay291e7222020-03-18 09:24:57 +010036#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010037
38#define ALT_BUF_LEN SZ_1K
39
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010040#define ROOTFS_MMC0_UUID \
41 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
42 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
43
44#define ROOTFS_MMC1_UUID \
45 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
46 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
47
48#define ROOTFS_MMC2_UUID \
49 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
50 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
51
Patrick Delaunay33029a52022-03-28 19:25:26 +020052/* RAW partition (binary / bootloader) used Linux - reserved UUID */
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010053#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
54
55/*
56 * unique partition guid (uuid) for partition named "rootfs"
57 * on each MMC instance = SD Card or eMMC
58 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
59 */
60static const efi_guid_t uuid_mmc[3] = {
61 ROOTFS_MMC0_UUID,
62 ROOTFS_MMC1_UUID,
63 ROOTFS_MMC2_UUID
64};
65
Patrick Delaunay8dc57682022-03-28 19:25:30 +020066/* FIP type partition UUID used by TF-A*/
67#define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5"
68
69/* unique partition guid (uuid) for FIP partitions A/B */
70#define FIP_A_UUID \
71 EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \
72 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87)
73
74#define FIP_B_UUID \
75 EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \
76 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3)
77
78static const char * const fip_part_name[] = {
79 "fip-a",
80 "fip-b"
81};
82
83static const efi_guid_t fip_part_uuid[] = {
84 FIP_A_UUID,
85 FIP_B_UUID
86};
87
Patrick Delaunay526f66c2020-03-18 09:24:50 +010088/* order of column in flash layout file */
89enum stm32prog_col_t {
90 COL_OPTION,
91 COL_ID,
92 COL_NAME,
93 COL_TYPE,
94 COL_IP,
95 COL_OFFSET,
96 COL_NB_STM32
97};
98
Patrick Delaunay19676ef2021-04-02 14:05:17 +020099#define FIP_TOC_HEADER_NAME 0xAA640001
100
101struct fip_toc_header {
102 u32 name;
103 u32 serial_number;
104 u64 flags;
105};
106
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200107#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
108 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
109
110/*
111 * Read NVMEM memory for STM32CubeProgrammer
112 *
113 * [in] value[0].a: Type (0 for OTP access)
114 * [out] memref[1].buffer Output buffer to return all read values
115 * [out] memref[1].size Size of buffer to be read
116 *
117 * Return codes:
118 * TEE_SUCCESS - Invoke command success
119 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
120 */
121#define TA_NVMEM_READ 0x0
122
123/*
124 * Write NVMEM memory for STM32CubeProgrammer
125 *
126 * [in] value[0].a Type (0 for OTP access)
127 * [in] memref[1].buffer Input buffer with the values to write
128 * [in] memref[1].size Size of buffer to be written
129 *
130 * Return codes:
131 * TEE_SUCCESS - Invoke command success
132 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
133 */
134#define TA_NVMEM_WRITE 0x1
135
136/* value of TA_NVMEM type = value[in] a */
137#define NVMEM_OTP 0
138
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200139DECLARE_GLOBAL_DATA_PTR;
140
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200141/* OPTEE TA NVMEM open helper */
142static int optee_ta_open(struct stm32prog_data *data)
143{
144 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
145 struct tee_open_session_arg arg;
146 struct udevice *tee = NULL;
147 int rc;
148
149 if (data->tee)
150 return 0;
151
152 tee = tee_find_device(NULL, NULL, NULL, NULL);
153 if (!tee)
154 return -ENODEV;
155
156 memset(&arg, 0, sizeof(arg));
157 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
158 rc = tee_open_session(tee, &arg, 0, NULL);
159 if (rc < 0)
160 return -ENODEV;
161
162 data->tee = tee;
163 data->tee_session = arg.session;
164
165 return 0;
166}
167
168/* OPTEE TA NVMEM invoke helper */
169static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
170 void *buff, ulong size)
171{
172 struct tee_invoke_arg arg;
173 struct tee_param param[2];
174 struct tee_shm *buff_shm;
175 int rc;
176
177 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
178 if (rc)
179 return rc;
180
181 memset(&arg, 0, sizeof(arg));
182 arg.func = cmd;
183 arg.session = data->tee_session;
184
185 memset(param, 0, sizeof(param));
186 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
187 param[0].u.value.a = type;
188
189 if (cmd == TA_NVMEM_WRITE)
190 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
191 else
192 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
193
194 param[1].u.memref.shm = buff_shm;
195 param[1].u.memref.size = size;
196
197 rc = tee_invoke_func(data->tee, &arg, 2, param);
198 if (rc < 0 || arg.ret != 0) {
199 dev_err(data->tee,
200 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
201 arg.ret, rc);
202 if (!rc)
203 rc = -EIO;
204 }
205
206 tee_shm_free(buff_shm);
207
208 return rc;
209}
210
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100211char *stm32prog_get_error(struct stm32prog_data *data)
212{
213 static const char error_msg[] = "Unspecified";
214
215 if (strlen(data->error) == 0)
216 strcpy(data->error, error_msg);
217
218 return data->error;
219}
220
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200221static bool stm32prog_is_fip_header(struct fip_toc_header *header)
222{
223 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
224}
225
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200226static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100227{
228 unsigned int i;
229
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200230 if (header->magic_number !=
231 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
232 log_debug("%s:invalid magic number : 0x%x\n",
233 __func__, header->magic_number);
234 return false;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200235 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200236 if (header->header_version != 0x00010000) {
237 log_debug("%s:invalid header version : 0x%x\n",
238 __func__, header->header_version);
239 return false;
240 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100241
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200242 if (header->reserved1 || header->reserved2) {
243 log_debug("%s:invalid reserved field\n", __func__);
244 return false;
245 }
246 for (i = 0; i < sizeof(header->padding); i++) {
247 if (header->padding[i] != 0) {
248 log_debug("%s:invalid padding field\n", __func__);
249 return false;
250 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100251 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200252
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200253 return true;
254}
255
256static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
257{
258 unsigned int i;
259
260 if (header->magic_number !=
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100261 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100262 log_debug("%s:invalid magic number : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200263 __func__, header->magic_number);
264 return false;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100265 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200266 if (header->header_version != 0x00020000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100267 log_debug("%s:invalid header version : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200268 __func__, header->header_version);
269 return false;
270 }
271 if (header->reserved1 || header->reserved2)
272 return false;
273
274 for (i = 0; i < sizeof(header->padding); i++) {
275 if (header->padding[i] != 0) {
276 log_debug("%s:invalid padding field\n", __func__);
277 return false;
278 }
279 }
280
281 return true;
282}
283
284void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
285{
286 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
287 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
288
289 if (!raw_header || !header) {
290 log_debug("%s:no header data\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200291 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100292 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200293
294 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
295 header->type = HEADER_FIP;
296 header->length = 0;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200297 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100298 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200299 if (stm32prog_is_stm32_header_v1(v1_header)) {
300 header->type = HEADER_STM32IMAGE;
301 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
302 header->image_length = le32_to_cpu(v1_header->image_length);
303 header->length = sizeof(struct stm32_header_v1);
304 return;
305 }
306 if (stm32prog_is_stm32_header_v2(v2_header)) {
307 header->type = HEADER_STM32IMAGE_V2;
308 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
309 header->image_length = le32_to_cpu(v2_header->image_length);
310 header->length = sizeof(struct stm32_header_v1) +
311 v2_header->extension_headers_length;
312 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100313 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100314
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200315 header->type = HEADER_NONE;
316 header->image_checksum = 0x0;
317 header->image_length = 0x0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100318}
319
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200320static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100321{
322 u32 i, checksum;
323 u8 *payload;
324
325 /* compute checksum on payload */
326 payload = (u8 *)addr;
327 checksum = 0;
328 for (i = header->image_length; i > 0; i--)
329 checksum += *(payload++);
330
331 return checksum;
332}
333
334/* FLASHLAYOUT PARSING *****************************************/
335static int parse_option(struct stm32prog_data *data,
336 int i, char *p, struct stm32prog_part_t *part)
337{
338 int result = 0;
339 char *c = p;
340
341 part->option = 0;
342 if (!strcmp(p, "-"))
343 return 0;
344
345 while (*c) {
346 switch (*c) {
347 case 'P':
348 part->option |= OPT_SELECT;
349 break;
350 case 'E':
351 part->option |= OPT_EMPTY;
352 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100353 case 'D':
354 part->option |= OPT_DELETE;
355 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100356 default:
357 result = -EINVAL;
358 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
359 i, *c, p);
360 return -EINVAL;
361 }
362 c++;
363 }
364 if (!(part->option & OPT_SELECT)) {
365 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
366 return -EINVAL;
367 }
368
369 return result;
370}
371
372static int parse_id(struct stm32prog_data *data,
373 int i, char *p, struct stm32prog_part_t *part)
374{
375 int result = 0;
376 unsigned long value;
377
378 result = strict_strtoul(p, 0, &value);
379 part->id = value;
380 if (result || value > PHASE_LAST_USER) {
381 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
382 result = -EINVAL;
383 }
384
385 return result;
386}
387
388static int parse_name(struct stm32prog_data *data,
389 int i, char *p, struct stm32prog_part_t *part)
390{
391 int result = 0;
392
393 if (strlen(p) < sizeof(part->name)) {
394 strcpy(part->name, p);
395 } else {
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200396 stm32prog_err("Layout line %d: partition name too long [%zd]: %s",
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100397 i, strlen(p), p);
398 result = -EINVAL;
399 }
400
401 return result;
402}
403
404static int parse_type(struct stm32prog_data *data,
405 int i, char *p, struct stm32prog_part_t *part)
406{
407 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100408 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100409
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100410 part->bin_nb = 0;
411 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100412 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100413
414 /* search for Binary(X) case */
415 len = strlen(p);
416 part->bin_nb = 1;
417 if (len > 6) {
418 if (len < 8 ||
419 (p[6] != '(') ||
420 (p[len - 1] != ')'))
421 result = -EINVAL;
422 else
423 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600424 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100425 }
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200426 } else if (!strcmp(p, "FIP")) {
427 part->part_type = PART_FIP;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200428 } else if (!strcmp(p, "ENV")) {
429 part->part_type = PART_ENV;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100430 } else if (!strcmp(p, "System")) {
431 part->part_type = PART_SYSTEM;
Patrick Delaunay0e582be2023-06-08 17:09:55 +0200432 } else if (!strcmp(p, "ESP")) {
433 part->part_type = PART_ESP;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100434 } else if (!strcmp(p, "FileSystem")) {
435 part->part_type = PART_FILESYSTEM;
436 } else if (!strcmp(p, "RawImage")) {
437 part->part_type = RAW_IMAGE;
438 } else {
439 result = -EINVAL;
440 }
441 if (result)
442 stm32prog_err("Layout line %d: type parsing error : '%s'",
443 i, p);
444
445 return result;
446}
447
448static int parse_ip(struct stm32prog_data *data,
449 int i, char *p, struct stm32prog_part_t *part)
450{
451 int result = 0;
452 unsigned int len = 0;
453
454 part->dev_id = 0;
455 if (!strcmp(p, "none")) {
456 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100457 } else if (!strncmp(p, "mmc", 3)) {
458 part->target = STM32PROG_MMC;
459 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100460 } else if (!strncmp(p, "nor", 3)) {
461 part->target = STM32PROG_NOR;
462 len = 3;
463 } else if (!strncmp(p, "nand", 4)) {
464 part->target = STM32PROG_NAND;
465 len = 4;
466 } else if (!strncmp(p, "spi-nand", 8)) {
467 part->target = STM32PROG_SPI_NAND;
468 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100469 } else if (!strncmp(p, "ram", 3)) {
470 part->target = STM32PROG_RAM;
471 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100472 } else {
473 result = -EINVAL;
474 }
475 if (len) {
476 /* only one digit allowed for device id */
477 if (strlen(p) != len + 1) {
478 result = -EINVAL;
479 } else {
480 part->dev_id = p[len] - '0';
481 if (part->dev_id > 9)
482 result = -EINVAL;
483 }
484 }
485 if (result)
486 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
487
488 return result;
489}
490
491static int parse_offset(struct stm32prog_data *data,
492 int i, char *p, struct stm32prog_part_t *part)
493{
494 int result = 0;
495 char *tail;
496
497 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100498 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100499 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100500 /* eMMC boot parttion */
501 if (!strncmp(p, "boot", 4)) {
502 if (strlen(p) != 5) {
503 result = -EINVAL;
504 } else {
505 if (p[4] == '1')
506 part->part_id = -1;
507 else if (p[4] == '2')
508 part->part_id = -2;
509 else
510 result = -EINVAL;
511 }
512 if (result)
513 stm32prog_err("Layout line %d: invalid part '%s'",
514 i, p);
515 } else {
516 part->addr = simple_strtoull(p, &tail, 0);
517 if (tail == p || *tail != '\0') {
518 stm32prog_err("Layout line %d: invalid offset '%s'",
519 i, p);
520 result = -EINVAL;
521 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100522 }
523
524 return result;
525}
526
527static
528int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
529 struct stm32prog_part_t *part) = {
530 [COL_OPTION] = parse_option,
531 [COL_ID] = parse_id,
532 [COL_NAME] = parse_name,
533 [COL_TYPE] = parse_type,
534 [COL_IP] = parse_ip,
535 [COL_OFFSET] = parse_offset,
536};
537
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100538static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200539 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100540 ulong size)
541{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100542 int column = 0, part_nb = 0, ret;
543 bool end_of_line, eof;
544 char *p, *start, *last, *col;
545 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200546 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100547 int part_list_size;
548 int i;
549
550 data->part_nb = 0;
551
552 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200553 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200554 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100555 u32 checksum;
556
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200557 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200558 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100559
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200560 checksum = stm32prog_header_checksum(addr, &header);
561 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100562 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200563 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100564 return -EIO;
565 }
566 }
567 if (!size)
568 return -EINVAL;
569
570 start = (char *)addr;
571 last = start + size;
572
573 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100574 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100575
576 /* calculate expected number of partitions */
577 part_list_size = 1;
578 p = start;
579 while (*p && (p < last)) {
580 if (*p++ == '\n') {
581 part_list_size++;
582 if (p < last && *p == '#')
583 part_list_size--;
584 }
585 }
586 if (part_list_size > PHASE_LAST_USER) {
587 stm32prog_err("Layout: too many partition (%d)",
588 part_list_size);
589 return -1;
590 }
591 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
592 if (!part) {
593 stm32prog_err("Layout: alloc failed");
594 return -ENOMEM;
595 }
596 data->part_array = part;
597
598 /* main parsing loop */
599 i = 1;
600 eof = false;
601 p = start;
602 col = start; /* 1st column */
603 end_of_line = false;
604 while (!eof) {
605 switch (*p) {
606 /* CR is ignored and replaced by NULL character */
607 case '\r':
608 *p = '\0';
609 p++;
610 continue;
611 case '\0':
612 end_of_line = true;
613 eof = true;
614 break;
615 case '\n':
616 end_of_line = true;
617 break;
618 case '\t':
619 break;
620 case '#':
621 /* comment line is skipped */
622 if (column == 0 && p == col) {
623 while ((p < last) && *p)
624 if (*p++ == '\n')
625 break;
626 col = p;
627 i++;
628 if (p >= last || !*p) {
629 eof = true;
630 end_of_line = true;
631 }
632 continue;
633 }
634 /* fall through */
635 /* by default continue with the next character */
636 default:
637 p++;
638 continue;
639 }
640
641 /* replace by \0: allow string parsing for each column */
642 *p = '\0';
643 p++;
644 if (p >= last) {
645 eof = true;
646 end_of_line = true;
647 }
648
649 /* skip empty line and multiple TAB in tsv file */
650 if (strlen(col) == 0) {
651 col = p;
652 /* skip empty line */
653 if (column == 0 && end_of_line) {
654 end_of_line = false;
655 i++;
656 }
657 continue;
658 }
659
660 if (column < COL_NB_STM32) {
661 ret = parse[column](data, i, col, part);
662 if (ret)
663 return ret;
664 }
665
666 /* save the beginning of the next column */
667 column++;
668 col = p;
669
670 if (!end_of_line)
671 continue;
672
673 /* end of the line detected */
674 end_of_line = false;
675
676 if (column < COL_NB_STM32) {
677 stm32prog_err("Layout line %d: no enought column", i);
678 return -EINVAL;
679 }
680 column = 0;
681 part_nb++;
682 part++;
683 i++;
684 if (part_nb >= part_list_size) {
685 part = NULL;
686 if (!eof) {
687 stm32prog_err("Layout: no enought memory for %d part",
688 part_nb);
689 return -EINVAL;
690 }
691 }
692 }
693 data->part_nb = part_nb;
694 if (data->part_nb == 0) {
695 stm32prog_err("Layout: no partition found");
696 return -ENODEV;
697 }
698
699 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100700}
701
702static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
703{
704 struct stm32prog_part_t *parta, *partb;
705
706 parta = container_of(a, struct stm32prog_part_t, list);
707 partb = container_of(b, struct stm32prog_part_t, list);
708
Patrick Delaunay6915b492020-03-18 09:24:52 +0100709 if (parta->part_id != partb->part_id)
710 return parta->part_id - partb->part_id;
711 else
712 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100713}
714
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100715static void get_mtd_by_target(char *string, enum stm32prog_target target,
716 int dev_id)
717{
718 const char *dev_str;
719
720 switch (target) {
721 case STM32PROG_NOR:
722 dev_str = "nor";
723 break;
724 case STM32PROG_NAND:
725 dev_str = "nand";
726 break;
727 case STM32PROG_SPI_NAND:
728 dev_str = "spi-nand";
729 break;
730 default:
731 dev_str = "invalid";
732 break;
733 }
734 sprintf(string, "%s%d", dev_str, dev_id);
735}
736
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100737static int init_device(struct stm32prog_data *data,
738 struct stm32prog_dev_t *dev)
739{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100740 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100741 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100742 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200743 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100744 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100745 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100746 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100747 u64 first_addr = 0, last_addr = 0;
748 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100749 u64 part_addr, part_size;
750 bool part_found;
751 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200752 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100753
754 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100755 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200756 if (!IS_ENABLED(CONFIG_MMC)) {
757 stm32prog_err("unknown device type = %d", dev->target);
758 return -ENODEV;
759 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100760 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200761 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100762 stm32prog_err("mmc device %d not found", dev->dev_id);
763 return -ENODEV;
764 }
765 block_dev = mmc_get_blk_desc(mmc);
766 if (!block_dev) {
767 stm32prog_err("mmc device %d not probed", dev->dev_id);
768 return -ENODEV;
769 }
770 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
771 dev->mmc = mmc;
772
773 /* reserve a full erase group for each GTP headers */
774 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
775 first_addr = dev->erase_size;
776 last_addr = (u64)(block_dev->lba -
777 mmc->erase_grp_size) *
778 block_dev->blksz;
779 } else {
780 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
781 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
782 block_dev->blksz;
783 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100784 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
785 block_dev->lba, block_dev->blksz);
786 log_debug(" available address = 0x%llx..0x%llx\n",
787 first_addr, last_addr);
788 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100789 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100790 case STM32PROG_NOR:
791 case STM32PROG_NAND:
792 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200793 if (!IS_ENABLED(CONFIG_MTD)) {
794 stm32prog_err("unknown device type = %d", dev->target);
795 return -ENODEV;
796 }
Patrice Chotard49569082023-06-08 17:16:40 +0200797 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
798 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100799 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100800 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100801
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100802 mtd = get_mtd_device_nm(mtd_id);
803 if (IS_ERR(mtd)) {
804 stm32prog_err("MTD device %s not found", mtd_id);
805 return -ENODEV;
806 }
807 first_addr = 0;
808 last_addr = mtd->size;
809 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100810 log_debug("MTD device %s: size=%lld erasesize=%d\n",
811 mtd_id, mtd->size, mtd->erasesize);
812 log_debug(" available address = 0x%llx..0x%llx\n",
813 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100814 dev->mtd = mtd;
815 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100816 case STM32PROG_RAM:
817 first_addr = gd->bd->bi_dram[0].start;
818 last_addr = first_addr + gd->bd->bi_dram[0].size;
819 dev->erase_size = 1;
820 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100821 default:
822 stm32prog_err("unknown device type = %d", dev->target);
823 return -ENODEV;
824 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100825 log_debug(" erase size = 0x%x\n", dev->erase_size);
826 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100827
828 /* order partition list in offset order */
829 list_sort(NULL, &dev->part_list, &part_cmp);
830 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100831 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100832 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100833 if (part->bin_nb > 1) {
834 if ((dev->target != STM32PROG_NAND &&
835 dev->target != STM32PROG_SPI_NAND) ||
836 part->id >= PHASE_FIRST_USER ||
837 strncmp(part->name, "fsbl", 4)) {
838 stm32prog_err("%s (0x%x): multiple binary %d not supported",
839 part->name, part->id,
840 part->bin_nb);
841 return -EINVAL;
842 }
843 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100844 if (part->part_type == RAW_IMAGE) {
845 part->part_id = 0x0;
846 part->addr = 0x0;
847 if (block_dev)
848 part->size = block_dev->lba * block_dev->blksz;
849 else
850 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100851 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
852 part->option, part->id, part->name,
853 part->part_type, part->bin_nb, part->target,
854 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100855 continue;
856 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100857 if (part->part_id < 0) { /* boot hw partition for eMMC */
858 if (mmc) {
859 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100860 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100861 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100862 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100863 part->part_id);
864 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100865 }
866 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100867 part->part_id = part_id++;
868
869 /* last partition : size to the end of the device */
870 if (part->list.next != &dev->part_list) {
871 next_part =
872 container_of(part->list.next,
873 struct stm32prog_part_t,
874 list);
875 if (part->addr < next_part->addr) {
876 part->size = next_part->addr -
877 part->addr;
878 } else {
879 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
880 part->name, part->id,
881 part->addr,
882 next_part->name,
883 next_part->id,
884 next_part->addr);
885 return -EINVAL;
886 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100887 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100888 if (part->addr <= last_addr) {
889 part->size = last_addr - part->addr;
890 } else {
891 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
892 part->name, part->id,
893 part->addr, last_addr);
894 return -EINVAL;
895 }
896 }
897 if (part->addr < first_addr) {
898 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100899 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100900 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100901 return -EINVAL;
902 }
903 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100904 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
905 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
906 part->name, part->id, part->addr,
907 part->dev->erase_size);
908 return -EINVAL;
909 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100910 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
911 part->part_id, part->option, part->id, part->name,
912 part->part_type, part->bin_nb, part->target,
913 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100914
915 part_addr = 0;
916 part_size = 0;
917 part_found = false;
918
919 /* check coherency with existing partition */
920 if (block_dev) {
921 /*
922 * block devices with GPT: check user partition size
923 * only for partial update, the GPT partions are be
924 * created for full update
925 */
926 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100927 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100928 continue;
929 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600930 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100931
932 ret = part_get_info(block_dev, part->part_id,
933 &partinfo);
934
935 if (ret) {
936 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
937 part->name, part->id,
938 part_id, part->dev_id);
939 return -ENODEV;
940 }
941 part_addr = (u64)partinfo.start * partinfo.blksz;
942 part_size = (u64)partinfo.size * partinfo.blksz;
943 part_name = (char *)partinfo.name;
944 part_found = true;
945 }
946
Patrick Delaunay8040da12020-07-31 16:31:52 +0200947 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200948 i = 0;
949 list_for_each_entry(partition, &mtd->partitions, node) {
950 if ((part->part_id - 1) == i) {
951 part_found = true;
952 break;
953 }
954 i++;
955 }
956 if (part_found) {
957 part_addr = partition->offset;
958 part_size = partition->size;
959 part_name = partition->name;
960 } else {
961 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
962 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100963 return -ENODEV;
964 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100965 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200966
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200967 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100968 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100969 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100970 continue;
971 }
972
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100973 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100974
975 if (part->addr != part_addr) {
976 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
977 part->name, part->id, part->part_id,
978 part_name, part->addr, part_addr);
979 return -ENODEV;
980 }
981 if (part->size != part_size) {
982 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
983 part->name, part->id, part->part_id,
984 part_name, part->addr, part->size,
985 part_size);
986 return -ENODEV;
987 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100988 }
989 return 0;
990}
991
992static int treat_partition_list(struct stm32prog_data *data)
993{
994 int i, j;
995 struct stm32prog_part_t *part;
996
997 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
998 data->dev[j].target = STM32PROG_NONE;
999 INIT_LIST_HEAD(&data->dev[j].part_list);
1000 }
1001
Patrick Delaunayc5112242020-03-18 09:24:55 +01001002 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001003 for (i = 0; i < data->part_nb; i++) {
1004 part = &data->part_array[i];
1005 part->alt_id = -1;
1006
1007 /* skip partition with IP="none" */
1008 if (part->target == STM32PROG_NONE) {
1009 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001010 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1011 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001012 return -EINVAL;
1013 }
1014 continue;
1015 }
1016
1017 if (part->id == PHASE_FLASHLAYOUT ||
1018 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001019 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1020 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001021 return -EINVAL;
1022 }
1023 for (j = i + 1; j < data->part_nb; j++) {
1024 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001025 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1026 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001027 return -EINVAL;
1028 }
1029 }
1030 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1031 if (data->dev[j].target == STM32PROG_NONE) {
1032 /* new device found */
1033 data->dev[j].target = part->target;
1034 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001035 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001036 data->dev_nb++;
1037 break;
1038 } else if ((part->target == data->dev[j].target) &&
1039 (part->dev_id == data->dev[j].dev_id)) {
1040 break;
1041 }
1042 }
1043 if (j == STM32PROG_MAX_DEV) {
1044 stm32prog_err("Layout: too many device");
1045 return -EINVAL;
1046 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001047 switch (part->target) {
1048 case STM32PROG_NOR:
1049 if (!data->fsbl_nor_detected &&
1050 !strncmp(part->name, "fsbl", 4))
1051 data->fsbl_nor_detected = true;
1052 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001053 default:
1054 break;
1055 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001056 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001057 if (!IS_SELECT(part))
1058 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001059 list_add_tail(&part->list, &data->dev[j].part_list);
1060 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001061
1062 return 0;
1063}
1064
Patrick Delaunay8040da12020-07-31 16:31:52 +02001065static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001066{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001067 int offset = 0;
1068 const int buflen = SZ_8K;
1069 char *buf;
1070 char uuid[UUID_STR_LEN + 1];
1071 unsigned char *uuid_bin;
1072 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001073 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001074 bool rootfs_found;
1075 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001076 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001077
1078 buf = malloc(buflen);
1079 if (!buf)
1080 return -ENOMEM;
1081
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001082 /* initialize the selected device */
1083 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001084 /* create gpt partition support only for full update on MMC */
1085 if (data->dev[i].target != STM32PROG_MMC ||
1086 !data->dev[i].full_update)
1087 continue;
1088
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001089 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001090 offset = 0;
1091 rootfs_found = false;
1092 memset(buf, 0, buflen);
1093
1094 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001095 /* skip eMMC boot partitions */
1096 if (part->part_id < 0)
1097 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001098 /* skip Raw Image */
1099 if (part->part_type == RAW_IMAGE)
1100 continue;
1101
1102 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001103 log_debug("\n%s: buffer too small, %s skippped",
1104 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001105 continue;
1106 }
1107
1108 if (!offset)
1109 offset += sprintf(buf, "gpt write mmc %d \"",
1110 data->dev[i].dev_id);
1111
1112 offset += snprintf(buf + offset, buflen - offset,
1113 "name=%s,start=0x%llx,size=0x%llx",
1114 part->name,
1115 part->addr,
1116 part->size);
1117
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001118 switch (part->part_type) {
1119 case PART_BINARY:
1120 type_str = LINUX_RESERVED_UUID;
1121 break;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +02001122 case PART_ENV:
1123 type_str = "u-boot-env";
1124 break;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001125 case PART_FIP:
1126 type_str = FIP_TYPE_UUID;
1127 break;
Patrick Delaunay0e582be2023-06-08 17:09:55 +02001128 case PART_ESP:
1129 /* EFI System Partition */
1130 type_str = "system";
1131 break;
1132 default: /* PART_FILESYSTEM or PART_SYSTEM for distro */
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001133 type_str = "linux";
1134 break;
1135 }
1136 offset += snprintf(buf + offset,
1137 buflen - offset,
1138 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001139
1140 if (part->part_type == PART_SYSTEM)
1141 offset += snprintf(buf + offset,
1142 buflen - offset,
1143 ",bootable");
1144
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001145 /* partition UUID */
1146 uuid_bin = NULL;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001147 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1148 mmc_id = part->dev_id;
1149 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001150 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1151 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1152 }
1153 if (part->part_type == PART_FIP) {
1154 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1155 if (!strcmp(part->name, fip_part_name[j])) {
1156 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1157 break;
1158 }
1159 }
1160 if (uuid_bin) {
1161 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1162 offset += snprintf(buf + offset,
1163 buflen - offset,
1164 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001165 }
1166
1167 offset += snprintf(buf + offset, buflen - offset, ";");
1168 }
1169
1170 if (offset) {
1171 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001172 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001173 if (run_command(buf, 0)) {
1174 stm32prog_err("GPT partitionning fail: %s",
1175 buf);
1176 free(buf);
1177
1178 return -1;
1179 }
1180 }
1181
1182 if (data->dev[i].mmc)
1183 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1184
1185#ifdef DEBUG
1186 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001187 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001188 if (run_command(buf, 0))
1189 printf("fail !\n");
1190 else
1191 printf("OK\n");
1192
1193 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1194 run_command(buf, 0);
1195#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001196 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001197 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001198
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001199#ifdef DEBUG
1200 run_command("mtd list", 0);
1201#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001202 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001203
1204 return 0;
1205}
1206
1207static int stm32prog_alt_add(struct stm32prog_data *data,
1208 struct dfu_entity *dfu,
1209 struct stm32prog_part_t *part)
1210{
1211 int ret = 0;
1212 int offset = 0;
1213 char devstr[10];
1214 char dfustr[10];
1215 char buf[ALT_BUF_LEN];
1216 u32 size;
1217 char multiplier, type;
1218
1219 /* max 3 digit for sector size */
1220 if (part->size > SZ_1M) {
1221 size = (u32)(part->size / SZ_1M);
1222 multiplier = 'M';
1223 } else if (part->size > SZ_1K) {
1224 size = (u32)(part->size / SZ_1K);
1225 multiplier = 'K';
1226 } else {
1227 size = (u32)part->size;
1228 multiplier = 'B';
1229 }
1230 if (IS_SELECT(part) && !IS_EMPTY(part))
1231 type = 'e'; /*Readable and Writeable*/
1232 else
1233 type = 'a';/*Readable*/
1234
1235 memset(buf, 0, sizeof(buf));
1236 offset = snprintf(buf, ALT_BUF_LEN - offset,
1237 "@%s/0x%02x/1*%d%c%c ",
1238 part->name, part->id,
1239 size, multiplier, type);
1240
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001241 if (part->target == STM32PROG_RAM) {
1242 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1243 "ram 0x%llx 0x%llx",
1244 part->addr, part->size);
1245 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001246 u64 dfu_size;
1247
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001248 if (part->dev->target == STM32PROG_MMC)
1249 dfu_size = part->size / part->dev->mmc->read_bl_len;
1250 else
1251 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001252 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1253 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001254 } else if (part->part_id < 0) {
1255 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1256
1257 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1258 "raw 0x%llx 0x%llx",
1259 part->addr, nb_blk);
1260 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001261 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001262 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001263 if (part->part_type == PART_SYSTEM &&
1264 (part->target == STM32PROG_NAND ||
1265 part->target == STM32PROG_NOR ||
1266 part->target == STM32PROG_SPI_NAND))
1267 offset += snprintf(buf + offset,
1268 ALT_BUF_LEN - offset,
1269 "partubi");
1270 else
1271 offset += snprintf(buf + offset,
1272 ALT_BUF_LEN - offset,
1273 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001274 /* dev_id requested by DFU MMC */
1275 if (part->target == STM32PROG_MMC)
1276 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1277 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001278 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001279 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001280 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001281 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001282 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001283 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001284 if (IS_ENABLED(CONFIG_MMC)) {
1285 ret = 0;
1286 sprintf(dfustr, "mmc");
1287 sprintf(devstr, "%d", part->dev_id);
1288 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001289 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001290 case STM32PROG_NAND:
1291 case STM32PROG_NOR:
1292 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001293 if (IS_ENABLED(CONFIG_MTD)) {
1294 ret = 0;
1295 sprintf(dfustr, "mtd");
1296 get_mtd_by_target(devstr, part->target, part->dev_id);
1297 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001298 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001299 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001300 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001301 sprintf(dfustr, "ram");
1302 sprintf(devstr, "0");
1303 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001304 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001305 break;
1306 }
1307 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001308 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001309 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001310 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001311 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001312 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001313 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1314 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001315
1316 return ret;
1317}
1318
1319static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1320 char *name, int phase, int size)
1321{
1322 int ret = 0;
1323 char devstr[4];
1324 char buf[ALT_BUF_LEN];
1325
1326 sprintf(devstr, "%d", phase);
1327 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1328 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001329 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001330
1331 return ret;
1332}
1333
1334static int dfu_init_entities(struct stm32prog_data *data)
1335{
1336 int ret = 0;
1337 int phase, i, alt_id;
1338 struct stm32prog_part_t *part;
1339 struct dfu_entity *dfu;
1340 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001341 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001342
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001343 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001344
1345 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1346 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1347 otp_size = OTP_SIZE_SMC;
1348 /* check if PTA BSEC is supported */
1349 ret = optee_ta_open(data);
1350 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1351 if (!ret && data->tee)
1352 otp_size = OTP_SIZE_TA;
1353 if (otp_size)
1354 alt_nb++; /* OTP*/
1355 }
1356
Patrick Delaunay7d145402021-05-18 15:12:09 +02001357 if (CONFIG_IS_ENABLED(DM_PMIC))
1358 alt_nb++; /* PMIC NVMEM*/
1359
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001360 if (data->part_nb == 0)
1361 alt_nb++; /* +1 for FlashLayout */
1362 else
1363 for (i = 0; i < data->part_nb; i++) {
1364 if (data->part_array[i].target != STM32PROG_NONE)
1365 alt_nb++;
1366 }
1367
1368 if (dfu_alt_init(alt_nb, &dfu))
1369 return -ENODEV;
1370
1371 puts("DFU alt info setting: ");
1372 if (data->part_nb) {
1373 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001374 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001375 for (phase = 1;
1376 (phase <= PHASE_LAST_USER) &&
1377 (alt_id < alt_nb) && !ret;
1378 phase++) {
1379 /* ordering alt setting by phase id */
1380 part = NULL;
1381 for (i = 0; i < data->part_nb; i++) {
1382 if (phase == data->part_array[i].id) {
1383 part = &data->part_array[i];
1384 break;
1385 }
1386 }
1387 if (!part)
1388 continue;
1389 if (part->target == STM32PROG_NONE)
1390 continue;
1391 part->alt_id = alt_id;
1392 alt_id++;
1393
1394 ret = stm32prog_alt_add(data, dfu, part);
1395 }
1396 } else {
1397 char buf[ALT_BUF_LEN];
1398
1399 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001400 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001401 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001402 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001403 }
1404
1405 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001406 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001407
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001408 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1409 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001410
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001411 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001412 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001413
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001414 if (ret)
1415 stm32prog_err("dfu init failed: %d", ret);
1416 puts("done\n");
1417
1418#ifdef DEBUG
1419 dfu_show_entities();
1420#endif
1421 return ret;
1422}
1423
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001424int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1425 long *size)
1426{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001427 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001428 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001429
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001430 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1431 stm32prog_err("OTP update not supported");
1432
1433 return -EOPNOTSUPP;
1434 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001435
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001436 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001437 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001438 if (!data->otp_part)
1439 return -ENOMEM;
1440 }
1441
1442 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001443 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001444
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001445 if (offset + *size > otp_size)
1446 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001447
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001448 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001449
1450 return 0;
1451}
1452
1453int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1454 long *size)
1455{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001456 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001457 int result = 0;
1458
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001459 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001460 stm32prog_err("OTP update not supported");
1461
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001462 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001463 }
1464
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001465 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001466 /* alway read for first packet */
1467 if (!offset) {
1468 if (!data->otp_part)
1469 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001470 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001471
1472 if (!data->otp_part) {
1473 result = -ENOMEM;
1474 goto end_otp_read;
1475 }
1476
1477 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001478 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001479
1480 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001481 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001482 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1483 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1484 data->otp_part, OTP_SIZE_TA);
1485 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001486 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001487 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001488 if (result)
1489 goto end_otp_read;
1490 }
1491
1492 if (!data->otp_part) {
1493 result = -ENOMEM;
1494 goto end_otp_read;
1495 }
1496
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001497 if (offset + *size > otp_size)
1498 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001499 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001500
1501end_otp_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001502 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001503
1504 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001505}
1506
1507int stm32prog_otp_start(struct stm32prog_data *data)
1508{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001509 int result = 0;
1510 struct arm_smccc_res res;
1511
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001512 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001513 stm32prog_err("OTP update not supported");
1514
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001515 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001516 }
1517
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001518 if (!data->otp_part) {
1519 stm32prog_err("start OTP without data");
1520 return -1;
1521 }
1522
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001523 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001524 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1525 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1526 data->otp_part, OTP_SIZE_TA);
1527 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001528 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001529 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001530
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001531 if (!res.a0) {
1532 switch (res.a1) {
1533 case 0:
1534 result = 0;
1535 break;
1536 case 1:
1537 stm32prog_err("Provisioning");
1538 result = 0;
1539 break;
1540 default:
1541 log_err("%s: OTP incorrect value (err = %ld)\n",
1542 __func__, res.a1);
1543 result = -EINVAL;
1544 break;
1545 }
1546 } else {
1547 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1548 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001549 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001550 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001551 }
1552
1553 free(data->otp_part);
1554 data->otp_part = NULL;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001555 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001556
1557 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001558}
1559
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001560int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1561 long *size)
1562{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001563 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001564
1565 if (!offset)
1566 memset(data->pmic_part, 0, PMIC_SIZE);
1567
1568 if (offset + *size > PMIC_SIZE)
1569 *size = PMIC_SIZE - offset;
1570
1571 memcpy(&data->pmic_part[offset], buffer, *size);
1572
1573 return 0;
1574}
1575
1576int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1577 long *size)
1578{
1579 int result = 0, ret;
1580 struct udevice *dev;
1581
Simon Glassb24d5752023-02-05 15:40:35 -07001582 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001583 stm32prog_err("PMIC update not supported");
1584
1585 return -EOPNOTSUPP;
1586 }
1587
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001588 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001589 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001590 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001591 &dev);
1592 if (ret)
1593 return ret;
1594
1595 /* alway request PMIC for first packet */
1596 if (!offset) {
1597 /* init struct with 0 */
1598 memset(data->pmic_part, 0, PMIC_SIZE);
1599
1600 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001601 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001602 &dev);
1603 if (ret)
1604 return ret;
1605
1606 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1607 if (ret < 0) {
1608 result = ret;
1609 goto end_pmic_read;
1610 }
1611 if (ret != PMIC_SIZE) {
1612 result = -EACCES;
1613 goto end_pmic_read;
1614 }
1615 }
1616
1617 if (offset + *size > PMIC_SIZE)
1618 *size = PMIC_SIZE - offset;
1619
1620 memcpy(buffer, &data->pmic_part[offset], *size);
1621
1622end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001623 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001624 return result;
1625}
1626
1627int stm32prog_pmic_start(struct stm32prog_data *data)
1628{
1629 int ret;
1630 struct udevice *dev;
1631
Simon Glassb24d5752023-02-05 15:40:35 -07001632 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001633 stm32prog_err("PMIC update not supported");
1634
1635 return -EOPNOTSUPP;
1636 }
1637
1638 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001639 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001640 &dev);
1641 if (ret)
1642 return ret;
1643
1644 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1645}
1646
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001647/* copy FSBL on NAND to improve reliability on NAND */
1648static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1649{
1650 int ret, i;
1651 void *fsbl;
1652 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001653 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001654 struct dfu_entity *dfu;
1655 long size, offset;
1656
1657 if (part->target != STM32PROG_NAND &&
1658 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001659 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001660
1661 dfu = dfu_get_entity(part->alt_id);
1662
1663 /* read header */
1664 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001665 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001666 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1667 if (ret)
1668 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001669
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001670 stm32prog_header_check((ulong)&raw_header, &header);
1671 if (header.type != HEADER_STM32IMAGE &&
1672 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001673 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001674
1675 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001676 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001677 size = round_up(size, part->dev->mtd->erasesize);
1678 fsbl = calloc(1, size);
1679 if (!fsbl)
1680 return -ENOMEM;
1681 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001682 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001683 if (ret)
1684 goto error;
1685
1686 dfu_transaction_cleanup(dfu);
1687 offset = 0;
1688 for (i = part->bin_nb - 1; i > 0; i--) {
1689 offset += size;
1690 /* write to the next erase block */
1691 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001692 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1693 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001694 if (ret)
1695 goto error;
1696 }
1697
1698error:
1699 free(fsbl);
1700 return ret;
1701}
1702
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001703static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001704{
1705 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001706#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001707 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1708 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001709 data->phase = PHASE_END;
1710 log_notice("U-Boot script received\n");
1711 return;
1712 }
1713#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001714 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001715 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001716 stm32prog_err("Layout: invalid FlashLayout");
1717 return;
1718 }
1719
1720 if (!data->cur_part)
1721 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001722
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001723 if (data->cur_part->target == STM32PROG_RAM) {
1724 if (data->cur_part->part_type == PART_SYSTEM)
1725 data->uimage = data->cur_part->addr;
1726 if (data->cur_part->part_type == PART_FILESYSTEM)
1727 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001728 if (data->cur_part->part_type == PART_BINARY) {
1729 data->initrd = data->cur_part->addr;
1730 data->initrd_size = offset;
1731 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001732 }
1733
Patrick Delaunay6915b492020-03-18 09:24:52 +01001734 if (CONFIG_IS_ENABLED(MMC) &&
1735 data->cur_part->part_id < 0) {
1736 char cmdbuf[60];
1737
1738 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1739 data->cur_part->dev_id, data->cur_part->dev_id,
1740 -(data->cur_part->part_id));
1741 if (run_command(cmdbuf, 0)) {
1742 stm32prog_err("commands '%s' failed", cmdbuf);
1743 return;
1744 }
1745 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001746
Simon Glass8e05bb12023-02-05 15:40:17 -07001747 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001748 data->cur_part->bin_nb > 1) {
1749 if (stm32prog_copy_fsbl(data->cur_part)) {
1750 stm32prog_err("%s (0x%x): copy of fsbl failed",
1751 data->cur_part->name, data->cur_part->id);
1752 return;
1753 }
1754 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001755}
1756
1757void stm32prog_do_reset(struct stm32prog_data *data)
1758{
1759 if (data->phase == PHASE_RESET) {
1760 data->phase = PHASE_DO_RESET;
1761 puts("Reset requested\n");
1762 }
1763}
1764
1765void stm32prog_next_phase(struct stm32prog_data *data)
1766{
1767 int phase, i;
1768 struct stm32prog_part_t *part;
1769 bool found;
1770
1771 phase = data->phase;
1772 switch (phase) {
1773 case PHASE_RESET:
1774 case PHASE_END:
1775 case PHASE_DO_RESET:
1776 return;
1777 }
1778
1779 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001780 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001781 data->cur_part = NULL;
1782 data->phase = PHASE_END;
1783 found = false;
1784 do {
1785 phase++;
1786 if (phase > PHASE_LAST_USER)
1787 break;
1788 for (i = 0; i < data->part_nb; i++) {
1789 part = &data->part_array[i];
1790 if (part->id == phase) {
1791 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1792 data->cur_part = part;
1793 data->phase = phase;
1794 found = true;
1795 }
1796 break;
1797 }
1798 }
1799 } while (!found);
1800
1801 if (data->phase == PHASE_END)
1802 puts("Phase=END\n");
1803}
1804
Patrick Delaunay291e7222020-03-18 09:24:57 +01001805static int part_delete(struct stm32prog_data *data,
1806 struct stm32prog_part_t *part)
1807{
1808 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001809 unsigned long blks, blks_offset, blks_size;
1810 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001811 char cmdbuf[40];
1812 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001813
1814 printf("Erasing %s ", part->name);
1815 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001816 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001817 if (!IS_ENABLED(CONFIG_MMC)) {
1818 ret = -1;
1819 stm32prog_err("%s (0x%x): erase invalid",
1820 part->name, part->id);
1821 break;
1822 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001823 printf("on mmc %d: ", part->dev->dev_id);
1824 block_dev = mmc_get_blk_desc(part->dev->mmc);
1825 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1826 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1827 /* -1 or -2 : delete boot partition of MMC
1828 * need to switch to associated hwpart 1 or 2
1829 */
1830 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001831 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001832 part->dev->dev_id,
1833 -part->part_id))
1834 return -1;
1835
1836 blks = blk_derase(block_dev, blks_offset, blks_size);
1837
1838 /* return to user partition */
1839 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001840 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001841 part->dev->dev_id, 0);
1842 if (blks != blks_size) {
1843 ret = -1;
1844 stm32prog_err("%s (0x%x): MMC erase failed",
1845 part->name, part->id);
1846 }
1847 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001848 case STM32PROG_NOR:
1849 case STM32PROG_NAND:
1850 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001851 if (!IS_ENABLED(CONFIG_MTD)) {
1852 ret = -1;
1853 stm32prog_err("%s (0x%x): erase invalid",
1854 part->name, part->id);
1855 break;
1856 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001857 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1858 printf("on %s: ", devstr);
1859 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1860 devstr, part->addr, part->size);
1861 if (run_command(cmdbuf, 0)) {
1862 ret = -1;
1863 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1864 part->name, part->id, cmdbuf);
1865 }
1866 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001867 case STM32PROG_RAM:
1868 printf("on ram: ");
1869 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1870 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001871 default:
1872 ret = -1;
1873 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1874 break;
1875 }
1876 if (!ret)
1877 printf("done\n");
1878
1879 return ret;
1880}
1881
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001882static void stm32prog_devices_init(struct stm32prog_data *data)
1883{
1884 int i;
1885 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001886 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001887
1888 ret = treat_partition_list(data);
1889 if (ret)
1890 goto error;
1891
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001892 /* empty flashlayout */
1893 if (!data->dev_nb)
1894 return;
1895
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001896 /* initialize the selected device */
1897 for (i = 0; i < data->dev_nb; i++) {
1898 ret = init_device(data, &data->dev[i]);
1899 if (ret)
1900 goto error;
1901 }
1902
Patrick Delaunay291e7222020-03-18 09:24:57 +01001903 /* delete RAW partition before create partition */
1904 for (i = 0; i < data->part_nb; i++) {
1905 part = &data->part_array[i];
1906
1907 if (part->part_type != RAW_IMAGE)
1908 continue;
1909
1910 if (!IS_SELECT(part) || !IS_DELETE(part))
1911 continue;
1912
1913 ret = part_delete(data, part);
1914 if (ret)
1915 goto error;
1916 }
1917
Patrick Delaunay8040da12020-07-31 16:31:52 +02001918 if (IS_ENABLED(CONFIG_MMC)) {
1919 ret = create_gpt_partitions(data);
1920 if (ret)
1921 goto error;
1922 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001923
Patrick Delaunay291e7222020-03-18 09:24:57 +01001924 /* delete partition GPT or MTD */
1925 for (i = 0; i < data->part_nb; i++) {
1926 part = &data->part_array[i];
1927
1928 if (part->part_type == RAW_IMAGE)
1929 continue;
1930
1931 if (!IS_SELECT(part) || !IS_DELETE(part))
1932 continue;
1933
1934 ret = part_delete(data, part);
1935 if (ret)
1936 goto error;
1937 }
1938
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001939 return;
1940
1941error:
1942 data->part_nb = 0;
1943}
1944
1945int stm32prog_dfu_init(struct stm32prog_data *data)
1946{
1947 /* init device if no error */
1948 if (data->part_nb)
1949 stm32prog_devices_init(data);
1950
1951 if (data->part_nb)
1952 stm32prog_next_phase(data);
1953
1954 /* prepare DFU for device read/write */
1955 dfu_free_entities();
1956 return dfu_init_entities(data);
1957}
1958
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001959int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001960{
1961 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001962 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001963 data->phase = PHASE_FLASHLAYOUT;
1964
1965 return parse_flash_layout(data, addr, size);
1966}
1967
1968void stm32prog_clean(struct stm32prog_data *data)
1969{
1970 /* clean */
1971 dfu_free_entities();
1972 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001973 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001974 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001975
1976 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1977 tee_close_session(data->tee, data->tee_session);
1978 data->tee = NULL;
1979 data->tee_session = 0x0;
1980 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001981}
1982
1983/* DFU callback: used after serial and direct DFU USB access */
1984void dfu_flush_callback(struct dfu_entity *dfu)
1985{
1986 if (!stm32prog_data)
1987 return;
1988
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001989 if (dfu->dev_type == DFU_DEV_VIRT) {
1990 if (dfu->data.virt.dev_num == PHASE_OTP)
1991 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001992 else if (dfu->data.virt.dev_num == PHASE_PMIC)
1993 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001994 return;
1995 }
1996
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001997 if (dfu->dev_type == DFU_DEV_RAM) {
1998 if (dfu->alt == 0 &&
1999 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002000 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002001 /* waiting DFU DETACH for reenumeration */
2002 }
2003 }
2004
2005 if (!stm32prog_data->cur_part)
2006 return;
2007
2008 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002009 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002010 stm32prog_next_phase(stm32prog_data);
2011 }
2012}
2013
2014void dfu_initiated_callback(struct dfu_entity *dfu)
2015{
2016 if (!stm32prog_data)
2017 return;
2018
2019 if (!stm32prog_data->cur_part)
2020 return;
2021
2022 /* force the saved offset for the current partition */
2023 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2024 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002025 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002026 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002027 }
2028}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002029
2030void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2031{
2032 struct stm32prog_data *data = stm32prog_data;
2033
2034 if (!stm32prog_data)
2035 return;
2036
2037 if (!stm32prog_data->cur_part)
2038 return;
2039
2040 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2041 stm32prog_err(msg);
2042}