blob: 9ba94be804ee56463c8848a3c5219e632f50e6b4 [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 Delaunayc203c212023-06-08 17:09:56 +020066/*
67 * GUID value defined in the FWU specification for identification
68 * of the FWU metadata partition.
69 */
70#define FWU_MDATA_UUID "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23"
71
Patrick Delaunay8dc57682022-03-28 19:25:30 +020072/* FIP type partition UUID used by TF-A*/
73#define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5"
74
75/* unique partition guid (uuid) for FIP partitions A/B */
76#define FIP_A_UUID \
77 EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \
78 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87)
79
80#define FIP_B_UUID \
81 EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \
82 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3)
83
84static const char * const fip_part_name[] = {
85 "fip-a",
86 "fip-b"
87};
88
89static const efi_guid_t fip_part_uuid[] = {
90 FIP_A_UUID,
91 FIP_B_UUID
92};
93
Patrick Delaunay526f66c2020-03-18 09:24:50 +010094/* order of column in flash layout file */
95enum stm32prog_col_t {
96 COL_OPTION,
97 COL_ID,
98 COL_NAME,
99 COL_TYPE,
100 COL_IP,
101 COL_OFFSET,
102 COL_NB_STM32
103};
104
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200105#define FIP_TOC_HEADER_NAME 0xAA640001
106
107struct fip_toc_header {
108 u32 name;
109 u32 serial_number;
110 u64 flags;
111};
112
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200113#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
114 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
115
116/*
117 * Read NVMEM memory for STM32CubeProgrammer
118 *
119 * [in] value[0].a: Type (0 for OTP access)
120 * [out] memref[1].buffer Output buffer to return all read values
121 * [out] memref[1].size Size of buffer to be read
122 *
123 * Return codes:
124 * TEE_SUCCESS - Invoke command success
125 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
126 */
127#define TA_NVMEM_READ 0x0
128
129/*
130 * Write NVMEM memory for STM32CubeProgrammer
131 *
132 * [in] value[0].a Type (0 for OTP access)
133 * [in] memref[1].buffer Input buffer with the values to write
134 * [in] memref[1].size Size of buffer to be written
135 *
136 * Return codes:
137 * TEE_SUCCESS - Invoke command success
138 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
139 */
140#define TA_NVMEM_WRITE 0x1
141
142/* value of TA_NVMEM type = value[in] a */
143#define NVMEM_OTP 0
144
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200145DECLARE_GLOBAL_DATA_PTR;
146
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200147/* OPTEE TA NVMEM open helper */
148static int optee_ta_open(struct stm32prog_data *data)
149{
150 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
151 struct tee_open_session_arg arg;
152 struct udevice *tee = NULL;
153 int rc;
154
155 if (data->tee)
156 return 0;
157
158 tee = tee_find_device(NULL, NULL, NULL, NULL);
159 if (!tee)
160 return -ENODEV;
161
162 memset(&arg, 0, sizeof(arg));
163 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
164 rc = tee_open_session(tee, &arg, 0, NULL);
165 if (rc < 0)
166 return -ENODEV;
167
168 data->tee = tee;
169 data->tee_session = arg.session;
170
171 return 0;
172}
173
174/* OPTEE TA NVMEM invoke helper */
175static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
176 void *buff, ulong size)
177{
178 struct tee_invoke_arg arg;
179 struct tee_param param[2];
180 struct tee_shm *buff_shm;
181 int rc;
182
183 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
184 if (rc)
185 return rc;
186
187 memset(&arg, 0, sizeof(arg));
188 arg.func = cmd;
189 arg.session = data->tee_session;
190
191 memset(param, 0, sizeof(param));
192 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
193 param[0].u.value.a = type;
194
195 if (cmd == TA_NVMEM_WRITE)
196 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
197 else
198 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
199
200 param[1].u.memref.shm = buff_shm;
201 param[1].u.memref.size = size;
202
203 rc = tee_invoke_func(data->tee, &arg, 2, param);
204 if (rc < 0 || arg.ret != 0) {
205 dev_err(data->tee,
206 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
207 arg.ret, rc);
208 if (!rc)
209 rc = -EIO;
210 }
211
212 tee_shm_free(buff_shm);
213
214 return rc;
215}
216
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100217char *stm32prog_get_error(struct stm32prog_data *data)
218{
219 static const char error_msg[] = "Unspecified";
220
221 if (strlen(data->error) == 0)
222 strcpy(data->error, error_msg);
223
224 return data->error;
225}
226
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200227static bool stm32prog_is_fip_header(struct fip_toc_header *header)
228{
229 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
230}
231
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200232static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100233{
234 unsigned int i;
235
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200236 if (header->magic_number !=
237 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
238 log_debug("%s:invalid magic number : 0x%x\n",
239 __func__, header->magic_number);
240 return false;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200241 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200242 if (header->header_version != 0x00010000) {
243 log_debug("%s:invalid header version : 0x%x\n",
244 __func__, header->header_version);
245 return false;
246 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100247
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200248 if (header->reserved1 || header->reserved2) {
249 log_debug("%s:invalid reserved field\n", __func__);
250 return false;
251 }
252 for (i = 0; i < sizeof(header->padding); i++) {
253 if (header->padding[i] != 0) {
254 log_debug("%s:invalid padding field\n", __func__);
255 return false;
256 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100257 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200258
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200259 return true;
260}
261
262static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
263{
264 unsigned int i;
265
266 if (header->magic_number !=
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100267 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100268 log_debug("%s:invalid magic number : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200269 __func__, header->magic_number);
270 return false;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100271 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200272 if (header->header_version != 0x00020000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100273 log_debug("%s:invalid header version : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200274 __func__, header->header_version);
275 return false;
276 }
277 if (header->reserved1 || header->reserved2)
278 return false;
279
280 for (i = 0; i < sizeof(header->padding); i++) {
281 if (header->padding[i] != 0) {
282 log_debug("%s:invalid padding field\n", __func__);
283 return false;
284 }
285 }
286
287 return true;
288}
289
290void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
291{
292 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
293 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
294
295 if (!raw_header || !header) {
296 log_debug("%s:no header data\n", __func__);
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
300 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
301 header->type = HEADER_FIP;
302 header->length = 0;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200303 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100304 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200305 if (stm32prog_is_stm32_header_v1(v1_header)) {
306 header->type = HEADER_STM32IMAGE;
307 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
308 header->image_length = le32_to_cpu(v1_header->image_length);
309 header->length = sizeof(struct stm32_header_v1);
310 return;
311 }
312 if (stm32prog_is_stm32_header_v2(v2_header)) {
313 header->type = HEADER_STM32IMAGE_V2;
314 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
315 header->image_length = le32_to_cpu(v2_header->image_length);
316 header->length = sizeof(struct stm32_header_v1) +
317 v2_header->extension_headers_length;
318 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100319 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100320
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200321 header->type = HEADER_NONE;
322 header->image_checksum = 0x0;
323 header->image_length = 0x0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100324}
325
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200326static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100327{
328 u32 i, checksum;
329 u8 *payload;
330
331 /* compute checksum on payload */
332 payload = (u8 *)addr;
333 checksum = 0;
334 for (i = header->image_length; i > 0; i--)
335 checksum += *(payload++);
336
337 return checksum;
338}
339
340/* FLASHLAYOUT PARSING *****************************************/
341static int parse_option(struct stm32prog_data *data,
342 int i, char *p, struct stm32prog_part_t *part)
343{
344 int result = 0;
345 char *c = p;
346
347 part->option = 0;
348 if (!strcmp(p, "-"))
349 return 0;
350
351 while (*c) {
352 switch (*c) {
353 case 'P':
354 part->option |= OPT_SELECT;
355 break;
356 case 'E':
357 part->option |= OPT_EMPTY;
358 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100359 case 'D':
360 part->option |= OPT_DELETE;
361 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100362 default:
363 result = -EINVAL;
364 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
365 i, *c, p);
366 return -EINVAL;
367 }
368 c++;
369 }
370 if (!(part->option & OPT_SELECT)) {
371 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
372 return -EINVAL;
373 }
374
375 return result;
376}
377
378static int parse_id(struct stm32prog_data *data,
379 int i, char *p, struct stm32prog_part_t *part)
380{
381 int result = 0;
382 unsigned long value;
383
384 result = strict_strtoul(p, 0, &value);
385 part->id = value;
386 if (result || value > PHASE_LAST_USER) {
387 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
388 result = -EINVAL;
389 }
390
391 return result;
392}
393
394static int parse_name(struct stm32prog_data *data,
395 int i, char *p, struct stm32prog_part_t *part)
396{
397 int result = 0;
398
399 if (strlen(p) < sizeof(part->name)) {
400 strcpy(part->name, p);
401 } else {
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200402 stm32prog_err("Layout line %d: partition name too long [%zd]: %s",
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100403 i, strlen(p), p);
404 result = -EINVAL;
405 }
406
407 return result;
408}
409
410static int parse_type(struct stm32prog_data *data,
411 int i, char *p, struct stm32prog_part_t *part)
412{
413 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100414 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100415
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100416 part->bin_nb = 0;
417 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100418 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100419
420 /* search for Binary(X) case */
421 len = strlen(p);
422 part->bin_nb = 1;
423 if (len > 6) {
424 if (len < 8 ||
425 (p[6] != '(') ||
426 (p[len - 1] != ')'))
427 result = -EINVAL;
428 else
429 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600430 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100431 }
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200432 } else if (!strcmp(p, "FIP")) {
433 part->part_type = PART_FIP;
Patrick Delaunayc203c212023-06-08 17:09:56 +0200434 } else if (!strcmp(p, "FWU_MDATA")) {
435 part->part_type = PART_FWU_MDATA;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200436 } else if (!strcmp(p, "ENV")) {
437 part->part_type = PART_ENV;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100438 } else if (!strcmp(p, "System")) {
439 part->part_type = PART_SYSTEM;
Patrick Delaunay0e582be2023-06-08 17:09:55 +0200440 } else if (!strcmp(p, "ESP")) {
441 part->part_type = PART_ESP;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100442 } else if (!strcmp(p, "FileSystem")) {
443 part->part_type = PART_FILESYSTEM;
444 } else if (!strcmp(p, "RawImage")) {
445 part->part_type = RAW_IMAGE;
446 } else {
447 result = -EINVAL;
448 }
449 if (result)
450 stm32prog_err("Layout line %d: type parsing error : '%s'",
451 i, p);
452
453 return result;
454}
455
456static int parse_ip(struct stm32prog_data *data,
457 int i, char *p, struct stm32prog_part_t *part)
458{
459 int result = 0;
460 unsigned int len = 0;
461
462 part->dev_id = 0;
463 if (!strcmp(p, "none")) {
464 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100465 } else if (!strncmp(p, "mmc", 3)) {
466 part->target = STM32PROG_MMC;
467 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100468 } else if (!strncmp(p, "nor", 3)) {
469 part->target = STM32PROG_NOR;
470 len = 3;
471 } else if (!strncmp(p, "nand", 4)) {
472 part->target = STM32PROG_NAND;
473 len = 4;
474 } else if (!strncmp(p, "spi-nand", 8)) {
475 part->target = STM32PROG_SPI_NAND;
476 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100477 } else if (!strncmp(p, "ram", 3)) {
478 part->target = STM32PROG_RAM;
479 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100480 } else {
481 result = -EINVAL;
482 }
483 if (len) {
484 /* only one digit allowed for device id */
485 if (strlen(p) != len + 1) {
486 result = -EINVAL;
487 } else {
488 part->dev_id = p[len] - '0';
489 if (part->dev_id > 9)
490 result = -EINVAL;
491 }
492 }
493 if (result)
494 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
495
496 return result;
497}
498
499static int parse_offset(struct stm32prog_data *data,
500 int i, char *p, struct stm32prog_part_t *part)
501{
502 int result = 0;
503 char *tail;
504
505 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100506 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100507 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100508 /* eMMC boot parttion */
509 if (!strncmp(p, "boot", 4)) {
510 if (strlen(p) != 5) {
511 result = -EINVAL;
512 } else {
513 if (p[4] == '1')
514 part->part_id = -1;
515 else if (p[4] == '2')
516 part->part_id = -2;
517 else
518 result = -EINVAL;
519 }
520 if (result)
521 stm32prog_err("Layout line %d: invalid part '%s'",
522 i, p);
523 } else {
Patrick Delaunay53fe8e72023-04-27 15:36:37 +0200524 part->addr = simple_strtoull(p, &tail, 10);
Patrick Delaunay6915b492020-03-18 09:24:52 +0100525 if (tail == p || *tail != '\0') {
526 stm32prog_err("Layout line %d: invalid offset '%s'",
527 i, p);
528 result = -EINVAL;
529 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100530 }
531
532 return result;
533}
534
535static
536int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
537 struct stm32prog_part_t *part) = {
538 [COL_OPTION] = parse_option,
539 [COL_ID] = parse_id,
540 [COL_NAME] = parse_name,
541 [COL_TYPE] = parse_type,
542 [COL_IP] = parse_ip,
543 [COL_OFFSET] = parse_offset,
544};
545
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100546static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200547 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100548 ulong size)
549{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100550 int column = 0, part_nb = 0, ret;
551 bool end_of_line, eof;
552 char *p, *start, *last, *col;
553 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200554 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100555 int part_list_size;
556 int i;
557
558 data->part_nb = 0;
559
560 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200561 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200562 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100563 u32 checksum;
564
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200565 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200566 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100567
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200568 checksum = stm32prog_header_checksum(addr, &header);
569 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100570 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200571 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100572 return -EIO;
573 }
574 }
575 if (!size)
576 return -EINVAL;
577
578 start = (char *)addr;
579 last = start + size;
580
581 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100582 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100583
584 /* calculate expected number of partitions */
585 part_list_size = 1;
586 p = start;
587 while (*p && (p < last)) {
588 if (*p++ == '\n') {
589 part_list_size++;
590 if (p < last && *p == '#')
591 part_list_size--;
592 }
593 }
594 if (part_list_size > PHASE_LAST_USER) {
595 stm32prog_err("Layout: too many partition (%d)",
596 part_list_size);
597 return -1;
598 }
599 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
600 if (!part) {
601 stm32prog_err("Layout: alloc failed");
602 return -ENOMEM;
603 }
604 data->part_array = part;
605
606 /* main parsing loop */
607 i = 1;
608 eof = false;
609 p = start;
610 col = start; /* 1st column */
611 end_of_line = false;
612 while (!eof) {
613 switch (*p) {
614 /* CR is ignored and replaced by NULL character */
615 case '\r':
616 *p = '\0';
617 p++;
618 continue;
619 case '\0':
620 end_of_line = true;
621 eof = true;
622 break;
623 case '\n':
624 end_of_line = true;
625 break;
626 case '\t':
627 break;
628 case '#':
629 /* comment line is skipped */
630 if (column == 0 && p == col) {
631 while ((p < last) && *p)
632 if (*p++ == '\n')
633 break;
634 col = p;
635 i++;
636 if (p >= last || !*p) {
637 eof = true;
638 end_of_line = true;
639 }
640 continue;
641 }
642 /* fall through */
643 /* by default continue with the next character */
644 default:
645 p++;
646 continue;
647 }
648
649 /* replace by \0: allow string parsing for each column */
650 *p = '\0';
651 p++;
652 if (p >= last) {
653 eof = true;
654 end_of_line = true;
655 }
656
657 /* skip empty line and multiple TAB in tsv file */
658 if (strlen(col) == 0) {
659 col = p;
660 /* skip empty line */
661 if (column == 0 && end_of_line) {
662 end_of_line = false;
663 i++;
664 }
665 continue;
666 }
667
668 if (column < COL_NB_STM32) {
669 ret = parse[column](data, i, col, part);
670 if (ret)
671 return ret;
672 }
673
674 /* save the beginning of the next column */
675 column++;
676 col = p;
677
678 if (!end_of_line)
679 continue;
680
681 /* end of the line detected */
682 end_of_line = false;
683
684 if (column < COL_NB_STM32) {
685 stm32prog_err("Layout line %d: no enought column", i);
686 return -EINVAL;
687 }
688 column = 0;
689 part_nb++;
690 part++;
691 i++;
692 if (part_nb >= part_list_size) {
693 part = NULL;
694 if (!eof) {
695 stm32prog_err("Layout: no enought memory for %d part",
696 part_nb);
697 return -EINVAL;
698 }
699 }
700 }
701 data->part_nb = part_nb;
702 if (data->part_nb == 0) {
703 stm32prog_err("Layout: no partition found");
704 return -ENODEV;
705 }
706
707 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100708}
709
710static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
711{
712 struct stm32prog_part_t *parta, *partb;
713
714 parta = container_of(a, struct stm32prog_part_t, list);
715 partb = container_of(b, struct stm32prog_part_t, list);
716
Patrick Delaunay6915b492020-03-18 09:24:52 +0100717 if (parta->part_id != partb->part_id)
718 return parta->part_id - partb->part_id;
719 else
720 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100721}
722
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100723static void get_mtd_by_target(char *string, enum stm32prog_target target,
724 int dev_id)
725{
726 const char *dev_str;
727
728 switch (target) {
729 case STM32PROG_NOR:
730 dev_str = "nor";
731 break;
732 case STM32PROG_NAND:
733 dev_str = "nand";
734 break;
735 case STM32PROG_SPI_NAND:
736 dev_str = "spi-nand";
737 break;
738 default:
739 dev_str = "invalid";
740 break;
741 }
742 sprintf(string, "%s%d", dev_str, dev_id);
743}
744
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100745static int init_device(struct stm32prog_data *data,
746 struct stm32prog_dev_t *dev)
747{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100748 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100749 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100750 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200751 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100752 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100753 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100754 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100755 u64 first_addr = 0, last_addr = 0;
756 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100757 u64 part_addr, part_size;
758 bool part_found;
759 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200760 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100761
762 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100763 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200764 if (!IS_ENABLED(CONFIG_MMC)) {
765 stm32prog_err("unknown device type = %d", dev->target);
766 return -ENODEV;
767 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100768 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200769 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100770 stm32prog_err("mmc device %d not found", dev->dev_id);
771 return -ENODEV;
772 }
773 block_dev = mmc_get_blk_desc(mmc);
774 if (!block_dev) {
775 stm32prog_err("mmc device %d not probed", dev->dev_id);
776 return -ENODEV;
777 }
778 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
779 dev->mmc = mmc;
780
781 /* reserve a full erase group for each GTP headers */
782 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
783 first_addr = dev->erase_size;
784 last_addr = (u64)(block_dev->lba -
785 mmc->erase_grp_size) *
786 block_dev->blksz;
787 } else {
788 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
789 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
790 block_dev->blksz;
791 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100792 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
793 block_dev->lba, block_dev->blksz);
794 log_debug(" available address = 0x%llx..0x%llx\n",
795 first_addr, last_addr);
796 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100797 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100798 case STM32PROG_NOR:
799 case STM32PROG_NAND:
800 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200801 if (!IS_ENABLED(CONFIG_MTD)) {
802 stm32prog_err("unknown device type = %d", dev->target);
803 return -ENODEV;
804 }
Patrice Chotard49569082023-06-08 17:16:40 +0200805 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
806 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100807 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100808 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100809
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100810 mtd = get_mtd_device_nm(mtd_id);
811 if (IS_ERR(mtd)) {
812 stm32prog_err("MTD device %s not found", mtd_id);
813 return -ENODEV;
814 }
815 first_addr = 0;
816 last_addr = mtd->size;
817 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100818 log_debug("MTD device %s: size=%lld erasesize=%d\n",
819 mtd_id, mtd->size, mtd->erasesize);
820 log_debug(" available address = 0x%llx..0x%llx\n",
821 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100822 dev->mtd = mtd;
823 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100824 case STM32PROG_RAM:
825 first_addr = gd->bd->bi_dram[0].start;
826 last_addr = first_addr + gd->bd->bi_dram[0].size;
827 dev->erase_size = 1;
828 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100829 default:
830 stm32prog_err("unknown device type = %d", dev->target);
831 return -ENODEV;
832 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100833 log_debug(" erase size = 0x%x\n", dev->erase_size);
834 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100835
836 /* order partition list in offset order */
837 list_sort(NULL, &dev->part_list, &part_cmp);
838 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100839 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100840 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100841 if (part->bin_nb > 1) {
842 if ((dev->target != STM32PROG_NAND &&
843 dev->target != STM32PROG_SPI_NAND) ||
844 part->id >= PHASE_FIRST_USER ||
845 strncmp(part->name, "fsbl", 4)) {
846 stm32prog_err("%s (0x%x): multiple binary %d not supported",
847 part->name, part->id,
848 part->bin_nb);
849 return -EINVAL;
850 }
851 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100852 if (part->part_type == RAW_IMAGE) {
853 part->part_id = 0x0;
854 part->addr = 0x0;
855 if (block_dev)
856 part->size = block_dev->lba * block_dev->blksz;
857 else
858 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100859 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
860 part->option, part->id, part->name,
861 part->part_type, part->bin_nb, part->target,
862 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100863 continue;
864 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100865 if (part->part_id < 0) { /* boot hw partition for eMMC */
866 if (mmc) {
867 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100868 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100869 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100870 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100871 part->part_id);
872 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100873 }
874 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100875 part->part_id = part_id++;
876
877 /* last partition : size to the end of the device */
878 if (part->list.next != &dev->part_list) {
879 next_part =
880 container_of(part->list.next,
881 struct stm32prog_part_t,
882 list);
883 if (part->addr < next_part->addr) {
884 part->size = next_part->addr -
885 part->addr;
886 } else {
887 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
888 part->name, part->id,
889 part->addr,
890 next_part->name,
891 next_part->id,
892 next_part->addr);
893 return -EINVAL;
894 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100895 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100896 if (part->addr <= last_addr) {
897 part->size = last_addr - part->addr;
898 } else {
899 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
900 part->name, part->id,
901 part->addr, last_addr);
902 return -EINVAL;
903 }
904 }
905 if (part->addr < first_addr) {
906 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100907 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100908 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100909 return -EINVAL;
910 }
911 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100912 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
913 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
914 part->name, part->id, part->addr,
915 part->dev->erase_size);
916 return -EINVAL;
917 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100918 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
919 part->part_id, part->option, part->id, part->name,
920 part->part_type, part->bin_nb, part->target,
921 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100922
923 part_addr = 0;
924 part_size = 0;
925 part_found = false;
926
927 /* check coherency with existing partition */
928 if (block_dev) {
929 /*
930 * block devices with GPT: check user partition size
931 * only for partial update, the GPT partions are be
932 * created for full update
933 */
934 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100935 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100936 continue;
937 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600938 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100939
940 ret = part_get_info(block_dev, part->part_id,
941 &partinfo);
942
943 if (ret) {
944 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
945 part->name, part->id,
946 part_id, part->dev_id);
947 return -ENODEV;
948 }
949 part_addr = (u64)partinfo.start * partinfo.blksz;
950 part_size = (u64)partinfo.size * partinfo.blksz;
951 part_name = (char *)partinfo.name;
952 part_found = true;
953 }
954
Patrick Delaunay8040da12020-07-31 16:31:52 +0200955 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200956 i = 0;
957 list_for_each_entry(partition, &mtd->partitions, node) {
958 if ((part->part_id - 1) == i) {
959 part_found = true;
960 break;
961 }
962 i++;
963 }
964 if (part_found) {
965 part_addr = partition->offset;
966 part_size = partition->size;
967 part_name = partition->name;
968 } else {
969 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
970 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100971 return -ENODEV;
972 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100973 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200974
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200975 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100976 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100977 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100978 continue;
979 }
980
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100981 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100982
983 if (part->addr != part_addr) {
984 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
985 part->name, part->id, part->part_id,
986 part_name, part->addr, part_addr);
987 return -ENODEV;
988 }
989 if (part->size != part_size) {
990 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
991 part->name, part->id, part->part_id,
992 part_name, part->addr, part->size,
993 part_size);
994 return -ENODEV;
995 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100996 }
997 return 0;
998}
999
1000static int treat_partition_list(struct stm32prog_data *data)
1001{
1002 int i, j;
1003 struct stm32prog_part_t *part;
1004
1005 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1006 data->dev[j].target = STM32PROG_NONE;
1007 INIT_LIST_HEAD(&data->dev[j].part_list);
1008 }
1009
Patrick Delaunayc5112242020-03-18 09:24:55 +01001010 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001011 for (i = 0; i < data->part_nb; i++) {
1012 part = &data->part_array[i];
1013 part->alt_id = -1;
1014
1015 /* skip partition with IP="none" */
1016 if (part->target == STM32PROG_NONE) {
1017 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001018 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1019 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001020 return -EINVAL;
1021 }
1022 continue;
1023 }
1024
1025 if (part->id == PHASE_FLASHLAYOUT ||
1026 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001027 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1028 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001029 return -EINVAL;
1030 }
1031 for (j = i + 1; j < data->part_nb; j++) {
1032 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001033 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1034 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001035 return -EINVAL;
1036 }
1037 }
1038 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1039 if (data->dev[j].target == STM32PROG_NONE) {
1040 /* new device found */
1041 data->dev[j].target = part->target;
1042 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001043 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001044 data->dev_nb++;
1045 break;
1046 } else if ((part->target == data->dev[j].target) &&
1047 (part->dev_id == data->dev[j].dev_id)) {
1048 break;
1049 }
1050 }
1051 if (j == STM32PROG_MAX_DEV) {
1052 stm32prog_err("Layout: too many device");
1053 return -EINVAL;
1054 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001055 switch (part->target) {
1056 case STM32PROG_NOR:
1057 if (!data->fsbl_nor_detected &&
1058 !strncmp(part->name, "fsbl", 4))
1059 data->fsbl_nor_detected = true;
1060 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001061 default:
1062 break;
1063 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001064 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001065 if (!IS_SELECT(part))
1066 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001067 list_add_tail(&part->list, &data->dev[j].part_list);
1068 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001069
1070 return 0;
1071}
1072
Patrick Delaunay8040da12020-07-31 16:31:52 +02001073static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001074{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001075 int offset = 0;
1076 const int buflen = SZ_8K;
1077 char *buf;
1078 char uuid[UUID_STR_LEN + 1];
1079 unsigned char *uuid_bin;
1080 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001081 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001082 bool rootfs_found;
1083 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001084 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001085
1086 buf = malloc(buflen);
1087 if (!buf)
1088 return -ENOMEM;
1089
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001090 /* initialize the selected device */
1091 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001092 /* create gpt partition support only for full update on MMC */
1093 if (data->dev[i].target != STM32PROG_MMC ||
1094 !data->dev[i].full_update)
1095 continue;
1096
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001097 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001098 offset = 0;
1099 rootfs_found = false;
1100 memset(buf, 0, buflen);
1101
1102 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001103 /* skip eMMC boot partitions */
1104 if (part->part_id < 0)
1105 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001106 /* skip Raw Image */
1107 if (part->part_type == RAW_IMAGE)
1108 continue;
1109
1110 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001111 log_debug("\n%s: buffer too small, %s skippped",
1112 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001113 continue;
1114 }
1115
1116 if (!offset)
1117 offset += sprintf(buf, "gpt write mmc %d \"",
1118 data->dev[i].dev_id);
1119
1120 offset += snprintf(buf + offset, buflen - offset,
1121 "name=%s,start=0x%llx,size=0x%llx",
1122 part->name,
1123 part->addr,
1124 part->size);
1125
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001126 switch (part->part_type) {
1127 case PART_BINARY:
1128 type_str = LINUX_RESERVED_UUID;
1129 break;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +02001130 case PART_ENV:
1131 type_str = "u-boot-env";
1132 break;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001133 case PART_FIP:
1134 type_str = FIP_TYPE_UUID;
1135 break;
Patrick Delaunayc203c212023-06-08 17:09:56 +02001136 case PART_FWU_MDATA:
1137 type_str = FWU_MDATA_UUID;
1138 break;
Patrick Delaunay0e582be2023-06-08 17:09:55 +02001139 case PART_ESP:
1140 /* EFI System Partition */
1141 type_str = "system";
1142 break;
1143 default: /* PART_FILESYSTEM or PART_SYSTEM for distro */
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001144 type_str = "linux";
1145 break;
1146 }
1147 offset += snprintf(buf + offset,
1148 buflen - offset,
1149 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001150
1151 if (part->part_type == PART_SYSTEM)
1152 offset += snprintf(buf + offset,
1153 buflen - offset,
1154 ",bootable");
1155
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001156 /* partition UUID */
1157 uuid_bin = NULL;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001158 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1159 mmc_id = part->dev_id;
1160 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001161 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1162 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1163 }
1164 if (part->part_type == PART_FIP) {
1165 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1166 if (!strcmp(part->name, fip_part_name[j])) {
1167 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1168 break;
1169 }
1170 }
1171 if (uuid_bin) {
1172 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1173 offset += snprintf(buf + offset,
1174 buflen - offset,
1175 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001176 }
1177
1178 offset += snprintf(buf + offset, buflen - offset, ";");
1179 }
1180
1181 if (offset) {
1182 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001183 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001184 if (run_command(buf, 0)) {
1185 stm32prog_err("GPT partitionning fail: %s",
1186 buf);
1187 free(buf);
1188
1189 return -1;
1190 }
1191 }
1192
1193 if (data->dev[i].mmc)
1194 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1195
1196#ifdef DEBUG
1197 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001198 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001199 if (run_command(buf, 0))
1200 printf("fail !\n");
1201 else
1202 printf("OK\n");
1203
1204 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1205 run_command(buf, 0);
1206#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001207 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001208 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001209
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001210#ifdef DEBUG
1211 run_command("mtd list", 0);
1212#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001213 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001214
1215 return 0;
1216}
1217
1218static int stm32prog_alt_add(struct stm32prog_data *data,
1219 struct dfu_entity *dfu,
1220 struct stm32prog_part_t *part)
1221{
1222 int ret = 0;
1223 int offset = 0;
1224 char devstr[10];
1225 char dfustr[10];
1226 char buf[ALT_BUF_LEN];
1227 u32 size;
1228 char multiplier, type;
1229
1230 /* max 3 digit for sector size */
1231 if (part->size > SZ_1M) {
1232 size = (u32)(part->size / SZ_1M);
1233 multiplier = 'M';
1234 } else if (part->size > SZ_1K) {
1235 size = (u32)(part->size / SZ_1K);
1236 multiplier = 'K';
1237 } else {
1238 size = (u32)part->size;
1239 multiplier = 'B';
1240 }
1241 if (IS_SELECT(part) && !IS_EMPTY(part))
1242 type = 'e'; /*Readable and Writeable*/
1243 else
1244 type = 'a';/*Readable*/
1245
1246 memset(buf, 0, sizeof(buf));
1247 offset = snprintf(buf, ALT_BUF_LEN - offset,
1248 "@%s/0x%02x/1*%d%c%c ",
1249 part->name, part->id,
1250 size, multiplier, type);
1251
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001252 if (part->target == STM32PROG_RAM) {
1253 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1254 "ram 0x%llx 0x%llx",
1255 part->addr, part->size);
1256 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001257 u64 dfu_size;
1258
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001259 if (part->dev->target == STM32PROG_MMC)
1260 dfu_size = part->size / part->dev->mmc->read_bl_len;
1261 else
1262 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001263 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1264 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001265 } else if (part->part_id < 0) {
1266 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1267
1268 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1269 "raw 0x%llx 0x%llx",
1270 part->addr, nb_blk);
1271 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001272 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001273 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001274 if (part->part_type == PART_SYSTEM &&
1275 (part->target == STM32PROG_NAND ||
1276 part->target == STM32PROG_NOR ||
1277 part->target == STM32PROG_SPI_NAND))
1278 offset += snprintf(buf + offset,
1279 ALT_BUF_LEN - offset,
1280 "partubi");
1281 else
1282 offset += snprintf(buf + offset,
1283 ALT_BUF_LEN - offset,
1284 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001285 /* dev_id requested by DFU MMC */
1286 if (part->target == STM32PROG_MMC)
1287 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1288 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001289 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001290 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001291 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001292 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001293 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001294 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001295 if (IS_ENABLED(CONFIG_MMC)) {
1296 ret = 0;
1297 sprintf(dfustr, "mmc");
1298 sprintf(devstr, "%d", part->dev_id);
1299 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001300 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001301 case STM32PROG_NAND:
1302 case STM32PROG_NOR:
1303 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001304 if (IS_ENABLED(CONFIG_MTD)) {
1305 ret = 0;
1306 sprintf(dfustr, "mtd");
1307 get_mtd_by_target(devstr, part->target, part->dev_id);
1308 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001309 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001310 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001311 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001312 sprintf(dfustr, "ram");
1313 sprintf(devstr, "0");
1314 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001315 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001316 break;
1317 }
1318 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001319 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001320 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001321 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001322 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001323 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001324 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1325 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001326
1327 return ret;
1328}
1329
1330static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1331 char *name, int phase, int size)
1332{
1333 int ret = 0;
1334 char devstr[4];
1335 char buf[ALT_BUF_LEN];
1336
1337 sprintf(devstr, "%d", phase);
1338 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1339 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001340 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001341
1342 return ret;
1343}
1344
1345static int dfu_init_entities(struct stm32prog_data *data)
1346{
1347 int ret = 0;
1348 int phase, i, alt_id;
1349 struct stm32prog_part_t *part;
1350 struct dfu_entity *dfu;
1351 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001352 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001353
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001354 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001355
1356 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1357 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1358 otp_size = OTP_SIZE_SMC;
1359 /* check if PTA BSEC is supported */
1360 ret = optee_ta_open(data);
1361 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1362 if (!ret && data->tee)
1363 otp_size = OTP_SIZE_TA;
1364 if (otp_size)
1365 alt_nb++; /* OTP*/
1366 }
1367
Patrick Delaunay7d145402021-05-18 15:12:09 +02001368 if (CONFIG_IS_ENABLED(DM_PMIC))
1369 alt_nb++; /* PMIC NVMEM*/
1370
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001371 if (data->part_nb == 0)
1372 alt_nb++; /* +1 for FlashLayout */
1373 else
1374 for (i = 0; i < data->part_nb; i++) {
1375 if (data->part_array[i].target != STM32PROG_NONE)
1376 alt_nb++;
1377 }
1378
1379 if (dfu_alt_init(alt_nb, &dfu))
1380 return -ENODEV;
1381
1382 puts("DFU alt info setting: ");
1383 if (data->part_nb) {
1384 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001385 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001386 for (phase = 1;
1387 (phase <= PHASE_LAST_USER) &&
1388 (alt_id < alt_nb) && !ret;
1389 phase++) {
1390 /* ordering alt setting by phase id */
1391 part = NULL;
1392 for (i = 0; i < data->part_nb; i++) {
1393 if (phase == data->part_array[i].id) {
1394 part = &data->part_array[i];
1395 break;
1396 }
1397 }
1398 if (!part)
1399 continue;
1400 if (part->target == STM32PROG_NONE)
1401 continue;
1402 part->alt_id = alt_id;
1403 alt_id++;
1404
1405 ret = stm32prog_alt_add(data, dfu, part);
1406 }
1407 } else {
1408 char buf[ALT_BUF_LEN];
1409
1410 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001411 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001412 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001413 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001414 }
1415
1416 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001417 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001418
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001419 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1420 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001421
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001422 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001423 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001424
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001425 if (ret)
1426 stm32prog_err("dfu init failed: %d", ret);
1427 puts("done\n");
1428
1429#ifdef DEBUG
1430 dfu_show_entities();
1431#endif
1432 return ret;
1433}
1434
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001435int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1436 long *size)
1437{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001438 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001439 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001440
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001441 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1442 stm32prog_err("OTP update not supported");
1443
1444 return -EOPNOTSUPP;
1445 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001446
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001447 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001448 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001449 if (!data->otp_part) {
1450 stm32prog_err("OTP write issue %d", -ENOMEM);
1451
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001452 return -ENOMEM;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001453 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001454 }
1455
1456 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001457 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001458
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001459 if (offset + *size > otp_size)
1460 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001461
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001462 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001463
1464 return 0;
1465}
1466
1467int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1468 long *size)
1469{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001470 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001471 int result = 0;
1472
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001473 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001474 stm32prog_err("OTP update not supported");
1475
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001476 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001477 }
1478
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001479 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001480 /* alway read for first packet */
1481 if (!offset) {
1482 if (!data->otp_part)
1483 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001484 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001485
1486 if (!data->otp_part) {
1487 result = -ENOMEM;
1488 goto end_otp_read;
1489 }
1490
1491 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001492 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001493
1494 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001495 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001496 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1497 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1498 data->otp_part, OTP_SIZE_TA);
1499 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001500 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001501 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001502 if (result)
1503 goto end_otp_read;
1504 }
1505
1506 if (!data->otp_part) {
1507 result = -ENOMEM;
1508 goto end_otp_read;
1509 }
1510
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001511 if (offset + *size > otp_size)
1512 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001513 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001514
1515end_otp_read:
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001516 if (result)
1517 stm32prog_err("OTP read issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001518 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001519
1520 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001521}
1522
1523int stm32prog_otp_start(struct stm32prog_data *data)
1524{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001525 int result = 0;
1526 struct arm_smccc_res res;
1527
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001528 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001529 stm32prog_err("OTP update not supported");
1530
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001531 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001532 }
1533
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001534 if (!data->otp_part) {
1535 stm32prog_err("start OTP without data");
1536 return -1;
1537 }
1538
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001539 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001540 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1541 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1542 data->otp_part, OTP_SIZE_TA);
1543 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001544 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001545 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001546
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001547 if (!res.a0) {
1548 switch (res.a1) {
1549 case 0:
1550 result = 0;
1551 break;
1552 case 1:
1553 stm32prog_err("Provisioning");
1554 result = 0;
1555 break;
1556 default:
1557 log_err("%s: OTP incorrect value (err = %ld)\n",
1558 __func__, res.a1);
1559 result = -EINVAL;
1560 break;
1561 }
1562 } else {
1563 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1564 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001565 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001566 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001567 }
1568
1569 free(data->otp_part);
1570 data->otp_part = NULL;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001571 if (result)
1572 stm32prog_err("OTP write issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001573 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001574
1575 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001576}
1577
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001578int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1579 long *size)
1580{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001581 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001582
1583 if (!offset)
1584 memset(data->pmic_part, 0, PMIC_SIZE);
1585
1586 if (offset + *size > PMIC_SIZE)
1587 *size = PMIC_SIZE - offset;
1588
1589 memcpy(&data->pmic_part[offset], buffer, *size);
1590
1591 return 0;
1592}
1593
1594int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1595 long *size)
1596{
1597 int result = 0, ret;
1598 struct udevice *dev;
1599
Simon Glassb24d5752023-02-05 15:40:35 -07001600 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001601 stm32prog_err("PMIC update not supported");
1602
1603 return -EOPNOTSUPP;
1604 }
1605
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001606 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001607 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001608 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001609 &dev);
1610 if (ret)
1611 return ret;
1612
1613 /* alway request PMIC for first packet */
1614 if (!offset) {
1615 /* init struct with 0 */
1616 memset(data->pmic_part, 0, PMIC_SIZE);
1617
1618 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001619 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001620 &dev);
1621 if (ret)
1622 return ret;
1623
1624 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1625 if (ret < 0) {
1626 result = ret;
1627 goto end_pmic_read;
1628 }
1629 if (ret != PMIC_SIZE) {
1630 result = -EACCES;
1631 goto end_pmic_read;
1632 }
1633 }
1634
1635 if (offset + *size > PMIC_SIZE)
1636 *size = PMIC_SIZE - offset;
1637
1638 memcpy(buffer, &data->pmic_part[offset], *size);
1639
1640end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001641 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001642 return result;
1643}
1644
1645int stm32prog_pmic_start(struct stm32prog_data *data)
1646{
1647 int ret;
1648 struct udevice *dev;
1649
Simon Glassb24d5752023-02-05 15:40:35 -07001650 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001651 stm32prog_err("PMIC update not supported");
1652
1653 return -EOPNOTSUPP;
1654 }
1655
1656 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001657 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001658 &dev);
1659 if (ret)
1660 return ret;
1661
1662 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1663}
1664
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001665/* copy FSBL on NAND to improve reliability on NAND */
1666static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1667{
1668 int ret, i;
1669 void *fsbl;
1670 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001671 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001672 struct dfu_entity *dfu;
1673 long size, offset;
1674
1675 if (part->target != STM32PROG_NAND &&
1676 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001677 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001678
1679 dfu = dfu_get_entity(part->alt_id);
1680
1681 /* read header */
1682 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001683 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001684 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1685 if (ret)
1686 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001687
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001688 stm32prog_header_check((ulong)&raw_header, &header);
1689 if (header.type != HEADER_STM32IMAGE &&
1690 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001691 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001692
1693 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001694 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001695 size = round_up(size, part->dev->mtd->erasesize);
1696 fsbl = calloc(1, size);
1697 if (!fsbl)
1698 return -ENOMEM;
1699 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001700 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001701 if (ret)
1702 goto error;
1703
1704 dfu_transaction_cleanup(dfu);
1705 offset = 0;
1706 for (i = part->bin_nb - 1; i > 0; i--) {
1707 offset += size;
1708 /* write to the next erase block */
1709 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001710 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1711 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001712 if (ret)
1713 goto error;
1714 }
1715
1716error:
1717 free(fsbl);
1718 return ret;
1719}
1720
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001721static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001722{
1723 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001724#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001725 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1726 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001727 data->phase = PHASE_END;
1728 log_notice("U-Boot script received\n");
1729 return;
1730 }
1731#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001732 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001733 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001734 stm32prog_err("Layout: invalid FlashLayout");
1735 return;
1736 }
1737
1738 if (!data->cur_part)
1739 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001740
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001741 if (data->cur_part->target == STM32PROG_RAM) {
1742 if (data->cur_part->part_type == PART_SYSTEM)
1743 data->uimage = data->cur_part->addr;
1744 if (data->cur_part->part_type == PART_FILESYSTEM)
1745 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001746 if (data->cur_part->part_type == PART_BINARY) {
1747 data->initrd = data->cur_part->addr;
1748 data->initrd_size = offset;
1749 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001750 }
1751
Patrick Delaunay6915b492020-03-18 09:24:52 +01001752 if (CONFIG_IS_ENABLED(MMC) &&
1753 data->cur_part->part_id < 0) {
1754 char cmdbuf[60];
1755
1756 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1757 data->cur_part->dev_id, data->cur_part->dev_id,
1758 -(data->cur_part->part_id));
1759 if (run_command(cmdbuf, 0)) {
1760 stm32prog_err("commands '%s' failed", cmdbuf);
1761 return;
1762 }
1763 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001764
Simon Glass8e05bb12023-02-05 15:40:17 -07001765 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001766 data->cur_part->bin_nb > 1) {
1767 if (stm32prog_copy_fsbl(data->cur_part)) {
1768 stm32prog_err("%s (0x%x): copy of fsbl failed",
1769 data->cur_part->name, data->cur_part->id);
1770 return;
1771 }
1772 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001773}
1774
1775void stm32prog_do_reset(struct stm32prog_data *data)
1776{
1777 if (data->phase == PHASE_RESET) {
1778 data->phase = PHASE_DO_RESET;
1779 puts("Reset requested\n");
1780 }
1781}
1782
1783void stm32prog_next_phase(struct stm32prog_data *data)
1784{
1785 int phase, i;
1786 struct stm32prog_part_t *part;
1787 bool found;
1788
1789 phase = data->phase;
1790 switch (phase) {
1791 case PHASE_RESET:
1792 case PHASE_END:
1793 case PHASE_DO_RESET:
1794 return;
1795 }
1796
1797 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001798 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001799 data->cur_part = NULL;
1800 data->phase = PHASE_END;
1801 found = false;
1802 do {
1803 phase++;
1804 if (phase > PHASE_LAST_USER)
1805 break;
1806 for (i = 0; i < data->part_nb; i++) {
1807 part = &data->part_array[i];
1808 if (part->id == phase) {
1809 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1810 data->cur_part = part;
1811 data->phase = phase;
1812 found = true;
1813 }
1814 break;
1815 }
1816 }
1817 } while (!found);
1818
1819 if (data->phase == PHASE_END)
1820 puts("Phase=END\n");
1821}
1822
Patrick Delaunay291e7222020-03-18 09:24:57 +01001823static int part_delete(struct stm32prog_data *data,
1824 struct stm32prog_part_t *part)
1825{
1826 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001827 unsigned long blks, blks_offset, blks_size;
1828 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001829 char cmdbuf[40];
1830 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001831
1832 printf("Erasing %s ", part->name);
1833 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001834 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001835 if (!IS_ENABLED(CONFIG_MMC)) {
1836 ret = -1;
1837 stm32prog_err("%s (0x%x): erase invalid",
1838 part->name, part->id);
1839 break;
1840 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001841 printf("on mmc %d: ", part->dev->dev_id);
1842 block_dev = mmc_get_blk_desc(part->dev->mmc);
1843 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1844 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1845 /* -1 or -2 : delete boot partition of MMC
1846 * need to switch to associated hwpart 1 or 2
1847 */
1848 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001849 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001850 part->dev->dev_id,
1851 -part->part_id))
1852 return -1;
1853
1854 blks = blk_derase(block_dev, blks_offset, blks_size);
1855
1856 /* return to user partition */
1857 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001858 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001859 part->dev->dev_id, 0);
1860 if (blks != blks_size) {
1861 ret = -1;
1862 stm32prog_err("%s (0x%x): MMC erase failed",
1863 part->name, part->id);
1864 }
1865 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001866 case STM32PROG_NOR:
1867 case STM32PROG_NAND:
1868 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001869 if (!IS_ENABLED(CONFIG_MTD)) {
1870 ret = -1;
1871 stm32prog_err("%s (0x%x): erase invalid",
1872 part->name, part->id);
1873 break;
1874 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001875 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1876 printf("on %s: ", devstr);
1877 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1878 devstr, part->addr, part->size);
1879 if (run_command(cmdbuf, 0)) {
1880 ret = -1;
1881 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1882 part->name, part->id, cmdbuf);
1883 }
1884 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001885 case STM32PROG_RAM:
1886 printf("on ram: ");
1887 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1888 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001889 default:
1890 ret = -1;
1891 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1892 break;
1893 }
1894 if (!ret)
1895 printf("done\n");
1896
1897 return ret;
1898}
1899
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001900static void stm32prog_devices_init(struct stm32prog_data *data)
1901{
1902 int i;
1903 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001904 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001905
1906 ret = treat_partition_list(data);
1907 if (ret)
1908 goto error;
1909
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001910 /* empty flashlayout */
1911 if (!data->dev_nb)
1912 return;
1913
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001914 /* initialize the selected device */
1915 for (i = 0; i < data->dev_nb; i++) {
1916 ret = init_device(data, &data->dev[i]);
1917 if (ret)
1918 goto error;
1919 }
1920
Patrick Delaunay291e7222020-03-18 09:24:57 +01001921 /* delete RAW partition before create partition */
1922 for (i = 0; i < data->part_nb; i++) {
1923 part = &data->part_array[i];
1924
1925 if (part->part_type != RAW_IMAGE)
1926 continue;
1927
1928 if (!IS_SELECT(part) || !IS_DELETE(part))
1929 continue;
1930
1931 ret = part_delete(data, part);
1932 if (ret)
1933 goto error;
1934 }
1935
Patrick Delaunay8040da12020-07-31 16:31:52 +02001936 if (IS_ENABLED(CONFIG_MMC)) {
1937 ret = create_gpt_partitions(data);
1938 if (ret)
1939 goto error;
1940 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001941
Patrick Delaunay291e7222020-03-18 09:24:57 +01001942 /* delete partition GPT or MTD */
1943 for (i = 0; i < data->part_nb; i++) {
1944 part = &data->part_array[i];
1945
1946 if (part->part_type == RAW_IMAGE)
1947 continue;
1948
1949 if (!IS_SELECT(part) || !IS_DELETE(part))
1950 continue;
1951
1952 ret = part_delete(data, part);
1953 if (ret)
1954 goto error;
1955 }
1956
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001957 return;
1958
1959error:
1960 data->part_nb = 0;
1961}
1962
1963int stm32prog_dfu_init(struct stm32prog_data *data)
1964{
1965 /* init device if no error */
1966 if (data->part_nb)
1967 stm32prog_devices_init(data);
1968
1969 if (data->part_nb)
1970 stm32prog_next_phase(data);
1971
1972 /* prepare DFU for device read/write */
1973 dfu_free_entities();
1974 return dfu_init_entities(data);
1975}
1976
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001977int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001978{
1979 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001980 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001981 data->phase = PHASE_FLASHLAYOUT;
1982
1983 return parse_flash_layout(data, addr, size);
1984}
1985
1986void stm32prog_clean(struct stm32prog_data *data)
1987{
1988 /* clean */
1989 dfu_free_entities();
1990 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001991 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001992 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001993
1994 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1995 tee_close_session(data->tee, data->tee_session);
1996 data->tee = NULL;
1997 data->tee_session = 0x0;
1998 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001999}
2000
2001/* DFU callback: used after serial and direct DFU USB access */
2002void dfu_flush_callback(struct dfu_entity *dfu)
2003{
2004 if (!stm32prog_data)
2005 return;
2006
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002007 if (dfu->dev_type == DFU_DEV_VIRT) {
2008 if (dfu->data.virt.dev_num == PHASE_OTP)
2009 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01002010 else if (dfu->data.virt.dev_num == PHASE_PMIC)
2011 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002012 return;
2013 }
2014
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002015 if (dfu->dev_type == DFU_DEV_RAM) {
2016 if (dfu->alt == 0 &&
2017 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002018 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002019 /* waiting DFU DETACH for reenumeration */
2020 }
2021 }
2022
2023 if (!stm32prog_data->cur_part)
2024 return;
2025
2026 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002027 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002028 stm32prog_next_phase(stm32prog_data);
2029 }
2030}
2031
2032void dfu_initiated_callback(struct dfu_entity *dfu)
2033{
2034 if (!stm32prog_data)
2035 return;
2036
2037 if (!stm32prog_data->cur_part)
2038 return;
2039
2040 /* force the saved offset for the current partition */
2041 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2042 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002043 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002044 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002045 }
2046}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002047
2048void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2049{
2050 struct stm32prog_data *data = stm32prog_data;
2051
2052 if (!stm32prog_data)
2053 return;
2054
2055 if (!stm32prog_data->cur_part)
2056 return;
2057
2058 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2059 stm32prog_err(msg);
2060}