blob: 9acbc0689a92eb5cc188b62b0348b3979e4c15f1 [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>
Tom Rini94a28e92025-05-21 16:51:21 -060013#include <mtd.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010014#include <part.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020015#include <tee.h>
Patrick Delaunay1d96b182020-03-18 09:24:58 +010016#include <asm/arch/stm32mp1_smc.h>
Patrick Delaunay285025f2024-03-20 15:56:42 +010017#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020019#include <dm/device_compat.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010020#include <dm/uclass.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010021#include <jffs2/load_kernel.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010022#include <linux/list.h>
23#include <linux/list_sort.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010024#include <linux/mtd/mtd.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060025#include <linux/printk.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010026#include <linux/sizes.h>
27
28#include "stm32prog.h"
29
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010030/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
31#define GPT_HEADER_SZ 34
32
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010033#define OPT_SELECT BIT(0)
34#define OPT_EMPTY BIT(1)
Patrick Delaunay291e7222020-03-18 09:24:57 +010035#define OPT_DELETE BIT(2)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010036
37#define IS_SELECT(part) ((part)->option & OPT_SELECT)
38#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay291e7222020-03-18 09:24:57 +010039#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010040
41#define ALT_BUF_LEN SZ_1K
42
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010043#define ROOTFS_MMC0_UUID \
44 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
45 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
46
47#define ROOTFS_MMC1_UUID \
48 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
49 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
50
51#define ROOTFS_MMC2_UUID \
52 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
53 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
54
Patrick Delaunay33029a52022-03-28 19:25:26 +020055/* RAW partition (binary / bootloader) used Linux - reserved UUID */
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010056#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
57
58/*
59 * unique partition guid (uuid) for partition named "rootfs"
60 * on each MMC instance = SD Card or eMMC
61 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
62 */
63static const efi_guid_t uuid_mmc[3] = {
64 ROOTFS_MMC0_UUID,
65 ROOTFS_MMC1_UUID,
66 ROOTFS_MMC2_UUID
67};
68
Patrick Delaunayc203c212023-06-08 17:09:56 +020069/*
70 * GUID value defined in the FWU specification for identification
71 * of the FWU metadata partition.
72 */
73#define FWU_MDATA_UUID "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23"
74
Patrick Delaunay8dc57682022-03-28 19:25:30 +020075/* FIP type partition UUID used by TF-A*/
76#define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5"
77
78/* unique partition guid (uuid) for FIP partitions A/B */
79#define FIP_A_UUID \
80 EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \
81 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87)
82
83#define FIP_B_UUID \
84 EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \
85 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3)
86
87static const char * const fip_part_name[] = {
88 "fip-a",
89 "fip-b"
90};
91
92static const efi_guid_t fip_part_uuid[] = {
93 FIP_A_UUID,
94 FIP_B_UUID
95};
96
Patrick Delaunay526f66c2020-03-18 09:24:50 +010097/* order of column in flash layout file */
98enum stm32prog_col_t {
99 COL_OPTION,
100 COL_ID,
101 COL_NAME,
102 COL_TYPE,
103 COL_IP,
104 COL_OFFSET,
105 COL_NB_STM32
106};
107
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200108#define FIP_TOC_HEADER_NAME 0xAA640001
109
110struct fip_toc_header {
111 u32 name;
112 u32 serial_number;
113 u64 flags;
114};
115
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200116#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
117 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
118
119/*
120 * Read NVMEM memory for STM32CubeProgrammer
121 *
122 * [in] value[0].a: Type (0 for OTP access)
123 * [out] memref[1].buffer Output buffer to return all read values
124 * [out] memref[1].size Size of buffer to be read
125 *
126 * Return codes:
127 * TEE_SUCCESS - Invoke command success
128 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
129 */
130#define TA_NVMEM_READ 0x0
131
132/*
133 * Write NVMEM memory for STM32CubeProgrammer
134 *
135 * [in] value[0].a Type (0 for OTP access)
136 * [in] memref[1].buffer Input buffer with the values to write
137 * [in] memref[1].size Size of buffer to be written
138 *
139 * Return codes:
140 * TEE_SUCCESS - Invoke command success
141 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
142 */
143#define TA_NVMEM_WRITE 0x1
144
145/* value of TA_NVMEM type = value[in] a */
146#define NVMEM_OTP 0
147
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200148DECLARE_GLOBAL_DATA_PTR;
149
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200150/* OPTEE TA NVMEM open helper */
151static int optee_ta_open(struct stm32prog_data *data)
152{
153 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
154 struct tee_open_session_arg arg;
155 struct udevice *tee = NULL;
156 int rc;
157
158 if (data->tee)
159 return 0;
160
161 tee = tee_find_device(NULL, NULL, NULL, NULL);
162 if (!tee)
163 return -ENODEV;
164
165 memset(&arg, 0, sizeof(arg));
166 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
167 rc = tee_open_session(tee, &arg, 0, NULL);
168 if (rc < 0)
169 return -ENODEV;
170
171 data->tee = tee;
172 data->tee_session = arg.session;
173
174 return 0;
175}
176
177/* OPTEE TA NVMEM invoke helper */
178static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
179 void *buff, ulong size)
180{
181 struct tee_invoke_arg arg;
182 struct tee_param param[2];
183 struct tee_shm *buff_shm;
184 int rc;
185
186 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
187 if (rc)
188 return rc;
189
190 memset(&arg, 0, sizeof(arg));
191 arg.func = cmd;
192 arg.session = data->tee_session;
193
194 memset(param, 0, sizeof(param));
195 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
196 param[0].u.value.a = type;
197
198 if (cmd == TA_NVMEM_WRITE)
199 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
200 else
201 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
202
203 param[1].u.memref.shm = buff_shm;
204 param[1].u.memref.size = size;
205
206 rc = tee_invoke_func(data->tee, &arg, 2, param);
207 if (rc < 0 || arg.ret != 0) {
208 dev_err(data->tee,
209 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
210 arg.ret, rc);
211 if (!rc)
212 rc = -EIO;
213 }
214
215 tee_shm_free(buff_shm);
216
217 return rc;
218}
219
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100220char *stm32prog_get_error(struct stm32prog_data *data)
221{
222 static const char error_msg[] = "Unspecified";
223
224 if (strlen(data->error) == 0)
225 strcpy(data->error, error_msg);
226
227 return data->error;
228}
229
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200230static bool stm32prog_is_fip_header(struct fip_toc_header *header)
231{
232 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
233}
234
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200235static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100236{
237 unsigned int i;
238
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200239 if (header->magic_number !=
240 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
241 log_debug("%s:invalid magic number : 0x%x\n",
242 __func__, header->magic_number);
243 return false;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200244 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200245 if (header->header_version != 0x00010000) {
246 log_debug("%s:invalid header version : 0x%x\n",
247 __func__, header->header_version);
248 return false;
249 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100250
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200251 if (header->reserved1 || header->reserved2) {
252 log_debug("%s:invalid reserved field\n", __func__);
253 return false;
254 }
255 for (i = 0; i < sizeof(header->padding); i++) {
256 if (header->padding[i] != 0) {
257 log_debug("%s:invalid padding field\n", __func__);
258 return false;
259 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100260 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200261
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200262 return true;
263}
264
265static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
266{
267 unsigned int i;
268
269 if (header->magic_number !=
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100270 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100271 log_debug("%s:invalid magic number : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200272 __func__, header->magic_number);
273 return false;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100274 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200275 if (header->header_version != 0x00020000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100276 log_debug("%s:invalid header version : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200277 __func__, header->header_version);
278 return false;
279 }
280 if (header->reserved1 || header->reserved2)
281 return false;
282
283 for (i = 0; i < sizeof(header->padding); i++) {
284 if (header->padding[i] != 0) {
285 log_debug("%s:invalid padding field\n", __func__);
286 return false;
287 }
288 }
289
290 return true;
291}
292
293void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
294{
295 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
296 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
297
298 if (!raw_header || !header) {
299 log_debug("%s:no header data\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200300 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100301 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200302
303 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
304 header->type = HEADER_FIP;
305 header->length = 0;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200306 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100307 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200308 if (stm32prog_is_stm32_header_v1(v1_header)) {
309 header->type = HEADER_STM32IMAGE;
310 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
311 header->image_length = le32_to_cpu(v1_header->image_length);
312 header->length = sizeof(struct stm32_header_v1);
313 return;
314 }
315 if (stm32prog_is_stm32_header_v2(v2_header)) {
316 header->type = HEADER_STM32IMAGE_V2;
317 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
318 header->image_length = le32_to_cpu(v2_header->image_length);
319 header->length = sizeof(struct stm32_header_v1) +
320 v2_header->extension_headers_length;
321 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100322 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100323
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200324 header->type = HEADER_NONE;
325 header->image_checksum = 0x0;
326 header->image_length = 0x0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100327}
328
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200329static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100330{
331 u32 i, checksum;
332 u8 *payload;
333
334 /* compute checksum on payload */
335 payload = (u8 *)addr;
336 checksum = 0;
337 for (i = header->image_length; i > 0; i--)
338 checksum += *(payload++);
339
340 return checksum;
341}
342
343/* FLASHLAYOUT PARSING *****************************************/
344static int parse_option(struct stm32prog_data *data,
345 int i, char *p, struct stm32prog_part_t *part)
346{
347 int result = 0;
348 char *c = p;
349
350 part->option = 0;
351 if (!strcmp(p, "-"))
352 return 0;
353
354 while (*c) {
355 switch (*c) {
356 case 'P':
357 part->option |= OPT_SELECT;
358 break;
359 case 'E':
360 part->option |= OPT_EMPTY;
361 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100362 case 'D':
363 part->option |= OPT_DELETE;
364 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100365 default:
366 result = -EINVAL;
367 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
368 i, *c, p);
369 return -EINVAL;
370 }
371 c++;
372 }
373 if (!(part->option & OPT_SELECT)) {
374 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
375 return -EINVAL;
376 }
377
378 return result;
379}
380
381static int parse_id(struct stm32prog_data *data,
382 int i, char *p, struct stm32prog_part_t *part)
383{
384 int result = 0;
385 unsigned long value;
386
387 result = strict_strtoul(p, 0, &value);
388 part->id = value;
389 if (result || value > PHASE_LAST_USER) {
390 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
391 result = -EINVAL;
392 }
393
394 return result;
395}
396
397static int parse_name(struct stm32prog_data *data,
398 int i, char *p, struct stm32prog_part_t *part)
399{
400 int result = 0;
401
402 if (strlen(p) < sizeof(part->name)) {
403 strcpy(part->name, p);
404 } else {
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200405 stm32prog_err("Layout line %d: partition name too long [%zd]: %s",
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100406 i, strlen(p), p);
407 result = -EINVAL;
408 }
409
410 return result;
411}
412
413static int parse_type(struct stm32prog_data *data,
414 int i, char *p, struct stm32prog_part_t *part)
415{
416 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100417 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100418
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100419 part->bin_nb = 0;
420 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100421 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100422
423 /* search for Binary(X) case */
424 len = strlen(p);
425 part->bin_nb = 1;
426 if (len > 6) {
427 if (len < 8 ||
428 (p[6] != '(') ||
429 (p[len - 1] != ')'))
430 result = -EINVAL;
431 else
432 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600433 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100434 }
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200435 } else if (!strcmp(p, "FIP")) {
436 part->part_type = PART_FIP;
Patrick Delaunayc203c212023-06-08 17:09:56 +0200437 } else if (!strcmp(p, "FWU_MDATA")) {
438 part->part_type = PART_FWU_MDATA;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200439 } else if (!strcmp(p, "ENV")) {
440 part->part_type = PART_ENV;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100441 } else if (!strcmp(p, "System")) {
442 part->part_type = PART_SYSTEM;
Patrick Delaunay0e582be2023-06-08 17:09:55 +0200443 } else if (!strcmp(p, "ESP")) {
444 part->part_type = PART_ESP;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100445 } else if (!strcmp(p, "FileSystem")) {
446 part->part_type = PART_FILESYSTEM;
447 } else if (!strcmp(p, "RawImage")) {
448 part->part_type = RAW_IMAGE;
449 } else {
450 result = -EINVAL;
451 }
452 if (result)
453 stm32prog_err("Layout line %d: type parsing error : '%s'",
454 i, p);
455
456 return result;
457}
458
459static int parse_ip(struct stm32prog_data *data,
460 int i, char *p, struct stm32prog_part_t *part)
461{
462 int result = 0;
463 unsigned int len = 0;
464
465 part->dev_id = 0;
466 if (!strcmp(p, "none")) {
467 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100468 } else if (!strncmp(p, "mmc", 3)) {
469 part->target = STM32PROG_MMC;
470 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100471 } else if (!strncmp(p, "nor", 3)) {
472 part->target = STM32PROG_NOR;
473 len = 3;
474 } else if (!strncmp(p, "nand", 4)) {
475 part->target = STM32PROG_NAND;
476 len = 4;
477 } else if (!strncmp(p, "spi-nand", 8)) {
478 part->target = STM32PROG_SPI_NAND;
479 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100480 } else if (!strncmp(p, "ram", 3)) {
481 part->target = STM32PROG_RAM;
482 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100483 } else {
484 result = -EINVAL;
485 }
486 if (len) {
487 /* only one digit allowed for device id */
488 if (strlen(p) != len + 1) {
489 result = -EINVAL;
490 } else {
491 part->dev_id = p[len] - '0';
492 if (part->dev_id > 9)
493 result = -EINVAL;
494 }
495 }
496 if (result)
497 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
498
499 return result;
500}
501
502static int parse_offset(struct stm32prog_data *data,
503 int i, char *p, struct stm32prog_part_t *part)
504{
505 int result = 0;
506 char *tail;
507
508 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100509 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100510 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100511 /* eMMC boot parttion */
512 if (!strncmp(p, "boot", 4)) {
513 if (strlen(p) != 5) {
514 result = -EINVAL;
515 } else {
516 if (p[4] == '1')
517 part->part_id = -1;
518 else if (p[4] == '2')
519 part->part_id = -2;
520 else
521 result = -EINVAL;
522 }
523 if (result)
524 stm32prog_err("Layout line %d: invalid part '%s'",
525 i, p);
526 } else {
Patrick Delaunay53fe8e72023-04-27 15:36:37 +0200527 part->addr = simple_strtoull(p, &tail, 10);
Patrick Delaunay6915b492020-03-18 09:24:52 +0100528 if (tail == p || *tail != '\0') {
529 stm32prog_err("Layout line %d: invalid offset '%s'",
530 i, p);
531 result = -EINVAL;
532 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100533 }
534
535 return result;
536}
537
538static
539int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
540 struct stm32prog_part_t *part) = {
541 [COL_OPTION] = parse_option,
542 [COL_ID] = parse_id,
543 [COL_NAME] = parse_name,
544 [COL_TYPE] = parse_type,
545 [COL_IP] = parse_ip,
546 [COL_OFFSET] = parse_offset,
547};
548
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100549static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200550 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100551 ulong size)
552{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100553 int column = 0, part_nb = 0, ret;
554 bool end_of_line, eof;
555 char *p, *start, *last, *col;
556 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200557 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100558 int part_list_size;
559 int i;
560
561 data->part_nb = 0;
562
563 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200564 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200565 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100566 u32 checksum;
567
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200568 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200569 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100570
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200571 checksum = stm32prog_header_checksum(addr, &header);
572 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100573 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200574 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100575 return -EIO;
576 }
577 }
578 if (!size)
579 return -EINVAL;
580
581 start = (char *)addr;
582 last = start + size;
583
584 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100585 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100586
587 /* calculate expected number of partitions */
588 part_list_size = 1;
589 p = start;
590 while (*p && (p < last)) {
591 if (*p++ == '\n') {
592 part_list_size++;
593 if (p < last && *p == '#')
594 part_list_size--;
595 }
596 }
597 if (part_list_size > PHASE_LAST_USER) {
598 stm32prog_err("Layout: too many partition (%d)",
599 part_list_size);
600 return -1;
601 }
602 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
603 if (!part) {
604 stm32prog_err("Layout: alloc failed");
605 return -ENOMEM;
606 }
607 data->part_array = part;
608
609 /* main parsing loop */
610 i = 1;
611 eof = false;
612 p = start;
613 col = start; /* 1st column */
614 end_of_line = false;
615 while (!eof) {
616 switch (*p) {
617 /* CR is ignored and replaced by NULL character */
618 case '\r':
619 *p = '\0';
620 p++;
621 continue;
622 case '\0':
623 end_of_line = true;
624 eof = true;
625 break;
626 case '\n':
627 end_of_line = true;
628 break;
629 case '\t':
630 break;
631 case '#':
632 /* comment line is skipped */
633 if (column == 0 && p == col) {
634 while ((p < last) && *p)
635 if (*p++ == '\n')
636 break;
637 col = p;
638 i++;
639 if (p >= last || !*p) {
640 eof = true;
641 end_of_line = true;
642 }
643 continue;
644 }
645 /* fall through */
646 /* by default continue with the next character */
647 default:
648 p++;
649 continue;
650 }
651
652 /* replace by \0: allow string parsing for each column */
653 *p = '\0';
654 p++;
655 if (p >= last) {
656 eof = true;
657 end_of_line = true;
658 }
659
660 /* skip empty line and multiple TAB in tsv file */
661 if (strlen(col) == 0) {
662 col = p;
663 /* skip empty line */
664 if (column == 0 && end_of_line) {
665 end_of_line = false;
666 i++;
667 }
668 continue;
669 }
670
671 if (column < COL_NB_STM32) {
672 ret = parse[column](data, i, col, part);
673 if (ret)
674 return ret;
675 }
676
677 /* save the beginning of the next column */
678 column++;
679 col = p;
680
681 if (!end_of_line)
682 continue;
683
684 /* end of the line detected */
685 end_of_line = false;
686
687 if (column < COL_NB_STM32) {
688 stm32prog_err("Layout line %d: no enought column", i);
689 return -EINVAL;
690 }
691 column = 0;
692 part_nb++;
693 part++;
694 i++;
695 if (part_nb >= part_list_size) {
696 part = NULL;
697 if (!eof) {
698 stm32prog_err("Layout: no enought memory for %d part",
699 part_nb);
700 return -EINVAL;
701 }
702 }
703 }
704 data->part_nb = part_nb;
705 if (data->part_nb == 0) {
706 stm32prog_err("Layout: no partition found");
707 return -ENODEV;
708 }
709
710 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100711}
712
713static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
714{
715 struct stm32prog_part_t *parta, *partb;
716
717 parta = container_of(a, struct stm32prog_part_t, list);
718 partb = container_of(b, struct stm32prog_part_t, list);
719
Patrick Delaunay6915b492020-03-18 09:24:52 +0100720 if (parta->part_id != partb->part_id)
721 return parta->part_id - partb->part_id;
722 else
723 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100724}
725
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100726static void get_mtd_by_target(char *string, enum stm32prog_target target,
727 int dev_id)
728{
729 const char *dev_str;
730
731 switch (target) {
732 case STM32PROG_NOR:
733 dev_str = "nor";
734 break;
735 case STM32PROG_NAND:
736 dev_str = "nand";
737 break;
738 case STM32PROG_SPI_NAND:
739 dev_str = "spi-nand";
740 break;
741 default:
742 dev_str = "invalid";
743 break;
744 }
745 sprintf(string, "%s%d", dev_str, dev_id);
746}
747
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100748static int init_device(struct stm32prog_data *data,
749 struct stm32prog_dev_t *dev)
750{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100751 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100752 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100753 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200754 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100755 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100756 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100757 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100758 u64 first_addr = 0, last_addr = 0;
759 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100760 u64 part_addr, part_size;
761 bool part_found;
762 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200763 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100764
765 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100766 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200767 if (!IS_ENABLED(CONFIG_MMC)) {
768 stm32prog_err("unknown device type = %d", dev->target);
769 return -ENODEV;
770 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100771 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200772 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100773 stm32prog_err("mmc device %d not found", dev->dev_id);
774 return -ENODEV;
775 }
776 block_dev = mmc_get_blk_desc(mmc);
777 if (!block_dev) {
778 stm32prog_err("mmc device %d not probed", dev->dev_id);
779 return -ENODEV;
780 }
781 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
782 dev->mmc = mmc;
783
784 /* reserve a full erase group for each GTP headers */
785 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
786 first_addr = dev->erase_size;
787 last_addr = (u64)(block_dev->lba -
788 mmc->erase_grp_size) *
789 block_dev->blksz;
790 } else {
791 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
792 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
793 block_dev->blksz;
794 }
Patrice Chotardfd9a2fa2024-11-29 13:27:07 +0100795 log_debug("MMC %d: lba=%lld blksz=%ld\n", dev->dev_id,
796 (u64)block_dev->lba, block_dev->blksz);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100797 log_debug(" available address = 0x%llx..0x%llx\n",
798 first_addr, last_addr);
799 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100800 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100801 case STM32PROG_NOR:
802 case STM32PROG_NAND:
803 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200804 if (!IS_ENABLED(CONFIG_MTD)) {
805 stm32prog_err("unknown device type = %d", dev->target);
806 return -ENODEV;
807 }
Patrice Chotard49569082023-06-08 17:16:40 +0200808 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
809 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100810 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100811 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100812
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100813 mtd = get_mtd_device_nm(mtd_id);
814 if (IS_ERR(mtd)) {
815 stm32prog_err("MTD device %s not found", mtd_id);
816 return -ENODEV;
817 }
818 first_addr = 0;
819 last_addr = mtd->size;
820 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100821 log_debug("MTD device %s: size=%lld erasesize=%d\n",
822 mtd_id, mtd->size, mtd->erasesize);
823 log_debug(" available address = 0x%llx..0x%llx\n",
824 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100825 dev->mtd = mtd;
826 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100827 case STM32PROG_RAM:
828 first_addr = gd->bd->bi_dram[0].start;
829 last_addr = first_addr + gd->bd->bi_dram[0].size;
830 dev->erase_size = 1;
831 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100832 default:
833 stm32prog_err("unknown device type = %d", dev->target);
834 return -ENODEV;
835 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100836 log_debug(" erase size = 0x%x\n", dev->erase_size);
837 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100838
839 /* order partition list in offset order */
840 list_sort(NULL, &dev->part_list, &part_cmp);
841 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100842 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100843 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100844 if (part->bin_nb > 1) {
845 if ((dev->target != STM32PROG_NAND &&
846 dev->target != STM32PROG_SPI_NAND) ||
847 part->id >= PHASE_FIRST_USER ||
848 strncmp(part->name, "fsbl", 4)) {
849 stm32prog_err("%s (0x%x): multiple binary %d not supported",
850 part->name, part->id,
851 part->bin_nb);
852 return -EINVAL;
853 }
854 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100855 if (part->part_type == RAW_IMAGE) {
856 part->part_id = 0x0;
857 part->addr = 0x0;
858 if (block_dev)
859 part->size = block_dev->lba * block_dev->blksz;
860 else
861 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100862 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
863 part->option, part->id, part->name,
864 part->part_type, part->bin_nb, part->target,
865 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100866 continue;
867 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100868 if (part->part_id < 0) { /* boot hw partition for eMMC */
869 if (mmc) {
870 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100871 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100872 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100873 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100874 part->part_id);
875 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100876 }
877 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100878 part->part_id = part_id++;
879
880 /* last partition : size to the end of the device */
881 if (part->list.next != &dev->part_list) {
882 next_part =
883 container_of(part->list.next,
884 struct stm32prog_part_t,
885 list);
886 if (part->addr < next_part->addr) {
887 part->size = next_part->addr -
888 part->addr;
889 } else {
890 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
891 part->name, part->id,
892 part->addr,
893 next_part->name,
894 next_part->id,
895 next_part->addr);
896 return -EINVAL;
897 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100898 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100899 if (part->addr <= last_addr) {
900 part->size = last_addr - part->addr;
901 } else {
902 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
903 part->name, part->id,
904 part->addr, last_addr);
905 return -EINVAL;
906 }
907 }
908 if (part->addr < first_addr) {
909 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100910 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100911 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100912 return -EINVAL;
913 }
914 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100915 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
916 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
917 part->name, part->id, part->addr,
918 part->dev->erase_size);
919 return -EINVAL;
920 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100921 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
922 part->part_id, part->option, part->id, part->name,
923 part->part_type, part->bin_nb, part->target,
924 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100925
926 part_addr = 0;
927 part_size = 0;
928 part_found = false;
929
930 /* check coherency with existing partition */
931 if (block_dev) {
932 /*
933 * block devices with GPT: check user partition size
934 * only for partial update, the GPT partions are be
935 * created for full update
936 */
937 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100938 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100939 continue;
940 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600941 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100942
943 ret = part_get_info(block_dev, part->part_id,
944 &partinfo);
945
946 if (ret) {
947 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
948 part->name, part->id,
949 part_id, part->dev_id);
950 return -ENODEV;
951 }
952 part_addr = (u64)partinfo.start * partinfo.blksz;
953 part_size = (u64)partinfo.size * partinfo.blksz;
954 part_name = (char *)partinfo.name;
955 part_found = true;
956 }
957
Patrick Delaunay8040da12020-07-31 16:31:52 +0200958 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200959 i = 0;
960 list_for_each_entry(partition, &mtd->partitions, node) {
961 if ((part->part_id - 1) == i) {
962 part_found = true;
963 break;
964 }
965 i++;
966 }
967 if (part_found) {
968 part_addr = partition->offset;
969 part_size = partition->size;
970 part_name = partition->name;
971 } else {
972 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
973 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100974 return -ENODEV;
975 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100976 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200977
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200978 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100979 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100980 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100981 continue;
982 }
983
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100984 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100985
986 if (part->addr != part_addr) {
987 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
988 part->name, part->id, part->part_id,
989 part_name, part->addr, part_addr);
990 return -ENODEV;
991 }
992 if (part->size != part_size) {
993 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
994 part->name, part->id, part->part_id,
995 part_name, part->addr, part->size,
996 part_size);
997 return -ENODEV;
998 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100999 }
1000 return 0;
1001}
1002
1003static int treat_partition_list(struct stm32prog_data *data)
1004{
1005 int i, j;
1006 struct stm32prog_part_t *part;
1007
1008 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1009 data->dev[j].target = STM32PROG_NONE;
1010 INIT_LIST_HEAD(&data->dev[j].part_list);
1011 }
1012
Patrick Delaunayc5112242020-03-18 09:24:55 +01001013 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001014 for (i = 0; i < data->part_nb; i++) {
1015 part = &data->part_array[i];
1016 part->alt_id = -1;
1017
1018 /* skip partition with IP="none" */
1019 if (part->target == STM32PROG_NONE) {
1020 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001021 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1022 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001023 return -EINVAL;
1024 }
1025 continue;
1026 }
1027
1028 if (part->id == PHASE_FLASHLAYOUT ||
1029 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001030 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1031 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001032 return -EINVAL;
1033 }
1034 for (j = i + 1; j < data->part_nb; j++) {
1035 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001036 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1037 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001038 return -EINVAL;
1039 }
1040 }
1041 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1042 if (data->dev[j].target == STM32PROG_NONE) {
1043 /* new device found */
1044 data->dev[j].target = part->target;
1045 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001046 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001047 data->dev_nb++;
1048 break;
1049 } else if ((part->target == data->dev[j].target) &&
1050 (part->dev_id == data->dev[j].dev_id)) {
1051 break;
1052 }
1053 }
1054 if (j == STM32PROG_MAX_DEV) {
1055 stm32prog_err("Layout: too many device");
1056 return -EINVAL;
1057 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001058 switch (part->target) {
1059 case STM32PROG_NOR:
1060 if (!data->fsbl_nor_detected &&
1061 !strncmp(part->name, "fsbl", 4))
1062 data->fsbl_nor_detected = true;
1063 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001064 default:
1065 break;
1066 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001067 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001068 if (!IS_SELECT(part))
1069 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001070 list_add_tail(&part->list, &data->dev[j].part_list);
1071 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001072
1073 return 0;
1074}
1075
Patrick Delaunay8040da12020-07-31 16:31:52 +02001076static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001077{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001078 int offset = 0;
1079 const int buflen = SZ_8K;
1080 char *buf;
1081 char uuid[UUID_STR_LEN + 1];
1082 unsigned char *uuid_bin;
1083 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001084 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001085 bool rootfs_found;
1086 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001087 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001088
1089 buf = malloc(buflen);
1090 if (!buf)
1091 return -ENOMEM;
1092
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001093 /* initialize the selected device */
1094 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001095 /* create gpt partition support only for full update on MMC */
1096 if (data->dev[i].target != STM32PROG_MMC ||
1097 !data->dev[i].full_update)
1098 continue;
1099
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001100 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001101 offset = 0;
1102 rootfs_found = false;
1103 memset(buf, 0, buflen);
1104
1105 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001106 /* skip eMMC boot partitions */
1107 if (part->part_id < 0)
1108 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001109 /* skip Raw Image */
1110 if (part->part_type == RAW_IMAGE)
1111 continue;
1112
1113 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001114 log_debug("\n%s: buffer too small, %s skippped",
1115 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001116 continue;
1117 }
1118
1119 if (!offset)
1120 offset += sprintf(buf, "gpt write mmc %d \"",
1121 data->dev[i].dev_id);
1122
1123 offset += snprintf(buf + offset, buflen - offset,
1124 "name=%s,start=0x%llx,size=0x%llx",
1125 part->name,
1126 part->addr,
1127 part->size);
1128
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001129 switch (part->part_type) {
1130 case PART_BINARY:
1131 type_str = LINUX_RESERVED_UUID;
1132 break;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +02001133 case PART_ENV:
1134 type_str = "u-boot-env";
1135 break;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001136 case PART_FIP:
1137 type_str = FIP_TYPE_UUID;
1138 break;
Patrick Delaunayc203c212023-06-08 17:09:56 +02001139 case PART_FWU_MDATA:
1140 type_str = FWU_MDATA_UUID;
1141 break;
Patrick Delaunay0e582be2023-06-08 17:09:55 +02001142 case PART_ESP:
1143 /* EFI System Partition */
1144 type_str = "system";
1145 break;
1146 default: /* PART_FILESYSTEM or PART_SYSTEM for distro */
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001147 type_str = "linux";
1148 break;
1149 }
1150 offset += snprintf(buf + offset,
1151 buflen - offset,
1152 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001153
1154 if (part->part_type == PART_SYSTEM)
1155 offset += snprintf(buf + offset,
1156 buflen - offset,
1157 ",bootable");
1158
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001159 /* partition UUID */
1160 uuid_bin = NULL;
Patrick Delaunay2b894292025-01-16 10:54:34 +01001161 if (!rootfs_found && (!strcmp(part->name, "rootfs") ||
1162 !strcmp(part->name, "rootfs-a"))) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001163 mmc_id = part->dev_id;
1164 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001165 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1166 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1167 }
1168 if (part->part_type == PART_FIP) {
1169 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1170 if (!strcmp(part->name, fip_part_name[j])) {
1171 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1172 break;
1173 }
1174 }
1175 if (uuid_bin) {
1176 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1177 offset += snprintf(buf + offset,
1178 buflen - offset,
1179 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001180 }
1181
1182 offset += snprintf(buf + offset, buflen - offset, ";");
1183 }
1184
1185 if (offset) {
1186 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001187 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001188 if (run_command(buf, 0)) {
1189 stm32prog_err("GPT partitionning fail: %s",
1190 buf);
1191 free(buf);
1192
1193 return -1;
1194 }
1195 }
1196
1197 if (data->dev[i].mmc)
1198 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1199
1200#ifdef DEBUG
1201 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001202 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001203 if (run_command(buf, 0))
1204 printf("fail !\n");
1205 else
1206 printf("OK\n");
1207
1208 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1209 run_command(buf, 0);
1210#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001211 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001212 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001213
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001214#ifdef DEBUG
1215 run_command("mtd list", 0);
1216#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001217 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001218
1219 return 0;
1220}
1221
1222static int stm32prog_alt_add(struct stm32prog_data *data,
1223 struct dfu_entity *dfu,
1224 struct stm32prog_part_t *part)
1225{
1226 int ret = 0;
1227 int offset = 0;
1228 char devstr[10];
1229 char dfustr[10];
1230 char buf[ALT_BUF_LEN];
1231 u32 size;
1232 char multiplier, type;
1233
1234 /* max 3 digit for sector size */
Patrice Chotard01e58942024-11-29 13:27:08 +01001235 if (part->size > SZ_1G) {
1236 size = (u32)(part->size / SZ_1G);
1237 multiplier = 'G';
1238 } else if (part->size > SZ_1M) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001239 size = (u32)(part->size / SZ_1M);
1240 multiplier = 'M';
1241 } else if (part->size > SZ_1K) {
1242 size = (u32)(part->size / SZ_1K);
1243 multiplier = 'K';
1244 } else {
1245 size = (u32)part->size;
1246 multiplier = 'B';
1247 }
1248 if (IS_SELECT(part) && !IS_EMPTY(part))
1249 type = 'e'; /*Readable and Writeable*/
1250 else
1251 type = 'a';/*Readable*/
1252
1253 memset(buf, 0, sizeof(buf));
1254 offset = snprintf(buf, ALT_BUF_LEN - offset,
1255 "@%s/0x%02x/1*%d%c%c ",
1256 part->name, part->id,
1257 size, multiplier, type);
1258
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001259 if (part->target == STM32PROG_RAM) {
1260 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1261 "ram 0x%llx 0x%llx",
1262 part->addr, part->size);
1263 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001264 u64 dfu_size;
1265
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001266 if (part->dev->target == STM32PROG_MMC)
1267 dfu_size = part->size / part->dev->mmc->read_bl_len;
1268 else
1269 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001270 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1271 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001272 } else if (part->part_id < 0) {
1273 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1274
1275 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1276 "raw 0x%llx 0x%llx",
1277 part->addr, nb_blk);
1278 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001279 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001280 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001281 if (part->part_type == PART_SYSTEM &&
1282 (part->target == STM32PROG_NAND ||
1283 part->target == STM32PROG_NOR ||
1284 part->target == STM32PROG_SPI_NAND))
1285 offset += snprintf(buf + offset,
1286 ALT_BUF_LEN - offset,
1287 "partubi");
1288 else
1289 offset += snprintf(buf + offset,
1290 ALT_BUF_LEN - offset,
1291 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001292 /* dev_id requested by DFU MMC */
1293 if (part->target == STM32PROG_MMC)
1294 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1295 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001296 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001297 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001298 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001299 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001300 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001301 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001302 if (IS_ENABLED(CONFIG_MMC)) {
1303 ret = 0;
1304 sprintf(dfustr, "mmc");
1305 sprintf(devstr, "%d", part->dev_id);
1306 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001307 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001308 case STM32PROG_NAND:
1309 case STM32PROG_NOR:
1310 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001311 if (IS_ENABLED(CONFIG_MTD)) {
1312 ret = 0;
1313 sprintf(dfustr, "mtd");
1314 get_mtd_by_target(devstr, part->target, part->dev_id);
1315 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001316 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001317 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001318 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001319 sprintf(dfustr, "ram");
1320 sprintf(devstr, "0");
1321 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001322 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001323 break;
1324 }
1325 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001326 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001327 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001328 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001329 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001330 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001331 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1332 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001333
1334 return ret;
1335}
1336
1337static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1338 char *name, int phase, int size)
1339{
1340 int ret = 0;
1341 char devstr[4];
1342 char buf[ALT_BUF_LEN];
1343
1344 sprintf(devstr, "%d", phase);
1345 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1346 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001347 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001348
1349 return ret;
1350}
1351
1352static int dfu_init_entities(struct stm32prog_data *data)
1353{
1354 int ret = 0;
1355 int phase, i, alt_id;
1356 struct stm32prog_part_t *part;
1357 struct dfu_entity *dfu;
1358 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001359 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001360
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001361 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001362
Patrick Delaunay285025f2024-03-20 15:56:42 +01001363 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && !stm32mp_is_closed()) {
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001364 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1365 otp_size = OTP_SIZE_SMC;
1366 /* check if PTA BSEC is supported */
1367 ret = optee_ta_open(data);
1368 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1369 if (!ret && data->tee)
1370 otp_size = OTP_SIZE_TA;
1371 if (otp_size)
1372 alt_nb++; /* OTP*/
1373 }
1374
Patrick Delaunay7d145402021-05-18 15:12:09 +02001375 if (CONFIG_IS_ENABLED(DM_PMIC))
1376 alt_nb++; /* PMIC NVMEM*/
1377
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001378 if (data->part_nb == 0)
1379 alt_nb++; /* +1 for FlashLayout */
1380 else
1381 for (i = 0; i < data->part_nb; i++) {
1382 if (data->part_array[i].target != STM32PROG_NONE)
1383 alt_nb++;
1384 }
1385
1386 if (dfu_alt_init(alt_nb, &dfu))
1387 return -ENODEV;
1388
1389 puts("DFU alt info setting: ");
1390 if (data->part_nb) {
1391 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001392 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001393 for (phase = 1;
1394 (phase <= PHASE_LAST_USER) &&
1395 (alt_id < alt_nb) && !ret;
1396 phase++) {
1397 /* ordering alt setting by phase id */
1398 part = NULL;
1399 for (i = 0; i < data->part_nb; i++) {
1400 if (phase == data->part_array[i].id) {
1401 part = &data->part_array[i];
1402 break;
1403 }
1404 }
1405 if (!part)
1406 continue;
1407 if (part->target == STM32PROG_NONE)
1408 continue;
1409 part->alt_id = alt_id;
1410 alt_id++;
1411
1412 ret = stm32prog_alt_add(data, dfu, part);
1413 }
1414 } else {
1415 char buf[ALT_BUF_LEN];
1416
1417 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001418 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001419 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001420 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001421 }
1422
1423 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001424 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001425
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001426 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1427 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001428
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001429 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001430 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001431
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001432 if (ret)
1433 stm32prog_err("dfu init failed: %d", ret);
1434 puts("done\n");
1435
1436#ifdef DEBUG
1437 dfu_show_entities();
1438#endif
1439 return ret;
1440}
1441
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001442int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1443 long *size)
1444{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001445 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001446 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001447
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001448 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1449 stm32prog_err("OTP update not supported");
1450
1451 return -EOPNOTSUPP;
1452 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001453
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001454 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001455 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001456 if (!data->otp_part) {
1457 stm32prog_err("OTP write issue %d", -ENOMEM);
1458
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001459 return -ENOMEM;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001460 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001461 }
1462
1463 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001464 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001465
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001466 if (offset + *size > otp_size)
1467 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001468
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001469 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001470
1471 return 0;
1472}
1473
1474int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1475 long *size)
1476{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001477 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001478 int result = 0;
1479
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001480 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001481 stm32prog_err("OTP update not supported");
1482
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001483 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001484 }
1485
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001486 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001487 /* alway read for first packet */
1488 if (!offset) {
1489 if (!data->otp_part)
1490 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001491 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001492
1493 if (!data->otp_part) {
1494 result = -ENOMEM;
1495 goto end_otp_read;
1496 }
1497
1498 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001499 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001500
1501 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001502 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001503 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1504 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1505 data->otp_part, OTP_SIZE_TA);
1506 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001507 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001508 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001509 if (result)
1510 goto end_otp_read;
1511 }
1512
1513 if (!data->otp_part) {
1514 result = -ENOMEM;
1515 goto end_otp_read;
1516 }
1517
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001518 if (offset + *size > otp_size)
1519 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001520 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001521
1522end_otp_read:
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001523 if (result)
1524 stm32prog_err("OTP read issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001525 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001526
1527 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001528}
1529
1530int stm32prog_otp_start(struct stm32prog_data *data)
1531{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001532 int result = 0;
1533 struct arm_smccc_res res;
1534
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001535 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001536 stm32prog_err("OTP update not supported");
1537
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001538 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001539 }
1540
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001541 if (!data->otp_part) {
1542 stm32prog_err("start OTP without data");
1543 return -1;
1544 }
1545
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001546 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001547 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1548 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1549 data->otp_part, OTP_SIZE_TA);
1550 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001551 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001552 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001553
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001554 if (!res.a0) {
1555 switch (res.a1) {
1556 case 0:
1557 result = 0;
1558 break;
1559 case 1:
1560 stm32prog_err("Provisioning");
1561 result = 0;
1562 break;
1563 default:
1564 log_err("%s: OTP incorrect value (err = %ld)\n",
1565 __func__, res.a1);
1566 result = -EINVAL;
1567 break;
1568 }
1569 } else {
1570 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1571 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001572 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001573 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001574 }
1575
1576 free(data->otp_part);
1577 data->otp_part = NULL;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001578 if (result)
1579 stm32prog_err("OTP write issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001580 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001581
1582 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001583}
1584
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001585int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1586 long *size)
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
1590 if (!offset)
1591 memset(data->pmic_part, 0, PMIC_SIZE);
1592
1593 if (offset + *size > PMIC_SIZE)
1594 *size = PMIC_SIZE - offset;
1595
1596 memcpy(&data->pmic_part[offset], buffer, *size);
1597
1598 return 0;
1599}
1600
1601int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1602 long *size)
1603{
1604 int result = 0, ret;
1605 struct udevice *dev;
1606
Simon Glassb24d5752023-02-05 15:40:35 -07001607 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001608 stm32prog_err("PMIC update not supported");
1609
1610 return -EOPNOTSUPP;
1611 }
1612
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001613 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001614 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001615 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001616 &dev);
1617 if (ret)
1618 return ret;
1619
1620 /* alway request PMIC for first packet */
1621 if (!offset) {
1622 /* init struct with 0 */
1623 memset(data->pmic_part, 0, PMIC_SIZE);
1624
1625 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001626 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001627 &dev);
1628 if (ret)
1629 return ret;
1630
1631 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1632 if (ret < 0) {
1633 result = ret;
1634 goto end_pmic_read;
1635 }
1636 if (ret != PMIC_SIZE) {
1637 result = -EACCES;
1638 goto end_pmic_read;
1639 }
1640 }
1641
1642 if (offset + *size > PMIC_SIZE)
1643 *size = PMIC_SIZE - offset;
1644
1645 memcpy(buffer, &data->pmic_part[offset], *size);
1646
1647end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001648 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001649 return result;
1650}
1651
1652int stm32prog_pmic_start(struct stm32prog_data *data)
1653{
1654 int ret;
1655 struct udevice *dev;
1656
Simon Glassb24d5752023-02-05 15:40:35 -07001657 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001658 stm32prog_err("PMIC update not supported");
1659
1660 return -EOPNOTSUPP;
1661 }
1662
1663 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001664 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001665 &dev);
1666 if (ret)
1667 return ret;
1668
1669 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1670}
1671
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001672/* copy FSBL on NAND to improve reliability on NAND */
1673static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1674{
1675 int ret, i;
1676 void *fsbl;
1677 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001678 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001679 struct dfu_entity *dfu;
1680 long size, offset;
1681
1682 if (part->target != STM32PROG_NAND &&
1683 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001684 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001685
1686 dfu = dfu_get_entity(part->alt_id);
1687
1688 /* read header */
1689 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001690 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001691 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1692 if (ret)
1693 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001694
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001695 stm32prog_header_check((ulong)&raw_header, &header);
1696 if (header.type != HEADER_STM32IMAGE &&
1697 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001698 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001699
1700 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001701 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001702 size = round_up(size, part->dev->mtd->erasesize);
1703 fsbl = calloc(1, size);
1704 if (!fsbl)
1705 return -ENOMEM;
1706 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001707 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001708 if (ret)
1709 goto error;
1710
1711 dfu_transaction_cleanup(dfu);
1712 offset = 0;
1713 for (i = part->bin_nb - 1; i > 0; i--) {
1714 offset += size;
1715 /* write to the next erase block */
1716 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001717 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1718 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001719 if (ret)
1720 goto error;
1721 }
1722
1723error:
1724 free(fsbl);
1725 return ret;
1726}
1727
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001728static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001729{
1730 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001731#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001732 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1733 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001734 data->phase = PHASE_END;
1735 log_notice("U-Boot script received\n");
1736 return;
1737 }
1738#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001739 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001740 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001741 stm32prog_err("Layout: invalid FlashLayout");
1742 return;
1743 }
1744
1745 if (!data->cur_part)
1746 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001747
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001748 if (data->cur_part->target == STM32PROG_RAM) {
1749 if (data->cur_part->part_type == PART_SYSTEM)
1750 data->uimage = data->cur_part->addr;
1751 if (data->cur_part->part_type == PART_FILESYSTEM)
1752 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001753 if (data->cur_part->part_type == PART_BINARY) {
1754 data->initrd = data->cur_part->addr;
1755 data->initrd_size = offset;
1756 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001757 }
1758
Patrick Delaunay6915b492020-03-18 09:24:52 +01001759 if (CONFIG_IS_ENABLED(MMC) &&
1760 data->cur_part->part_id < 0) {
1761 char cmdbuf[60];
1762
1763 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1764 data->cur_part->dev_id, data->cur_part->dev_id,
1765 -(data->cur_part->part_id));
1766 if (run_command(cmdbuf, 0)) {
1767 stm32prog_err("commands '%s' failed", cmdbuf);
1768 return;
1769 }
1770 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001771
Simon Glass8e05bb12023-02-05 15:40:17 -07001772 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001773 data->cur_part->bin_nb > 1) {
1774 if (stm32prog_copy_fsbl(data->cur_part)) {
1775 stm32prog_err("%s (0x%x): copy of fsbl failed",
1776 data->cur_part->name, data->cur_part->id);
1777 return;
1778 }
1779 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001780}
1781
1782void stm32prog_do_reset(struct stm32prog_data *data)
1783{
1784 if (data->phase == PHASE_RESET) {
1785 data->phase = PHASE_DO_RESET;
1786 puts("Reset requested\n");
1787 }
1788}
1789
1790void stm32prog_next_phase(struct stm32prog_data *data)
1791{
1792 int phase, i;
1793 struct stm32prog_part_t *part;
1794 bool found;
1795
1796 phase = data->phase;
1797 switch (phase) {
1798 case PHASE_RESET:
1799 case PHASE_END:
1800 case PHASE_DO_RESET:
1801 return;
1802 }
1803
1804 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001805 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001806 data->cur_part = NULL;
1807 data->phase = PHASE_END;
1808 found = false;
1809 do {
1810 phase++;
1811 if (phase > PHASE_LAST_USER)
1812 break;
1813 for (i = 0; i < data->part_nb; i++) {
1814 part = &data->part_array[i];
1815 if (part->id == phase) {
1816 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1817 data->cur_part = part;
1818 data->phase = phase;
1819 found = true;
1820 }
1821 break;
1822 }
1823 }
1824 } while (!found);
1825
1826 if (data->phase == PHASE_END)
1827 puts("Phase=END\n");
1828}
1829
Patrick Delaunay291e7222020-03-18 09:24:57 +01001830static int part_delete(struct stm32prog_data *data,
1831 struct stm32prog_part_t *part)
1832{
1833 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001834 unsigned long blks, blks_offset, blks_size;
1835 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001836 char cmdbuf[40];
1837 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001838
1839 printf("Erasing %s ", part->name);
1840 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001841 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001842 if (!IS_ENABLED(CONFIG_MMC)) {
1843 ret = -1;
1844 stm32prog_err("%s (0x%x): erase invalid",
1845 part->name, part->id);
1846 break;
1847 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001848 printf("on mmc %d: ", part->dev->dev_id);
1849 block_dev = mmc_get_blk_desc(part->dev->mmc);
1850 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1851 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1852 /* -1 or -2 : delete boot partition of MMC
1853 * need to switch to associated hwpart 1 or 2
1854 */
1855 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001856 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001857 part->dev->dev_id,
1858 -part->part_id))
1859 return -1;
1860
1861 blks = blk_derase(block_dev, blks_offset, blks_size);
1862
1863 /* return to user partition */
1864 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001865 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001866 part->dev->dev_id, 0);
1867 if (blks != blks_size) {
1868 ret = -1;
1869 stm32prog_err("%s (0x%x): MMC erase failed",
1870 part->name, part->id);
1871 }
1872 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001873 case STM32PROG_NOR:
1874 case STM32PROG_NAND:
1875 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001876 if (!IS_ENABLED(CONFIG_MTD)) {
1877 ret = -1;
1878 stm32prog_err("%s (0x%x): erase invalid",
1879 part->name, part->id);
1880 break;
1881 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001882 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1883 printf("on %s: ", devstr);
1884 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1885 devstr, part->addr, part->size);
1886 if (run_command(cmdbuf, 0)) {
1887 ret = -1;
1888 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1889 part->name, part->id, cmdbuf);
1890 }
1891 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001892 case STM32PROG_RAM:
1893 printf("on ram: ");
1894 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1895 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001896 default:
1897 ret = -1;
1898 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1899 break;
1900 }
1901 if (!ret)
1902 printf("done\n");
1903
1904 return ret;
1905}
1906
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001907static void stm32prog_devices_init(struct stm32prog_data *data)
1908{
1909 int i;
1910 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001911 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001912
1913 ret = treat_partition_list(data);
1914 if (ret)
1915 goto error;
1916
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001917 /* empty flashlayout */
1918 if (!data->dev_nb)
1919 return;
1920
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001921 /* initialize the selected device */
1922 for (i = 0; i < data->dev_nb; i++) {
1923 ret = init_device(data, &data->dev[i]);
1924 if (ret)
1925 goto error;
1926 }
1927
Patrick Delaunay291e7222020-03-18 09:24:57 +01001928 /* delete RAW partition before create partition */
1929 for (i = 0; i < data->part_nb; i++) {
1930 part = &data->part_array[i];
1931
1932 if (part->part_type != RAW_IMAGE)
1933 continue;
1934
1935 if (!IS_SELECT(part) || !IS_DELETE(part))
1936 continue;
1937
1938 ret = part_delete(data, part);
1939 if (ret)
1940 goto error;
1941 }
1942
Patrick Delaunay8040da12020-07-31 16:31:52 +02001943 if (IS_ENABLED(CONFIG_MMC)) {
1944 ret = create_gpt_partitions(data);
1945 if (ret)
1946 goto error;
1947 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001948
Patrick Delaunay291e7222020-03-18 09:24:57 +01001949 /* delete partition GPT or MTD */
1950 for (i = 0; i < data->part_nb; i++) {
1951 part = &data->part_array[i];
1952
1953 if (part->part_type == RAW_IMAGE)
1954 continue;
1955
1956 if (!IS_SELECT(part) || !IS_DELETE(part))
1957 continue;
1958
1959 ret = part_delete(data, part);
1960 if (ret)
1961 goto error;
1962 }
1963
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001964 return;
1965
1966error:
1967 data->part_nb = 0;
1968}
1969
1970int stm32prog_dfu_init(struct stm32prog_data *data)
1971{
1972 /* init device if no error */
1973 if (data->part_nb)
1974 stm32prog_devices_init(data);
1975
1976 if (data->part_nb)
1977 stm32prog_next_phase(data);
1978
1979 /* prepare DFU for device read/write */
1980 dfu_free_entities();
1981 return dfu_init_entities(data);
1982}
1983
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001984int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001985{
1986 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001987 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001988 data->phase = PHASE_FLASHLAYOUT;
1989
1990 return parse_flash_layout(data, addr, size);
1991}
1992
1993void stm32prog_clean(struct stm32prog_data *data)
1994{
1995 /* clean */
1996 dfu_free_entities();
1997 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001998 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001999 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02002000
2001 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
2002 tee_close_session(data->tee, data->tee_session);
2003 data->tee = NULL;
2004 data->tee_session = 0x0;
2005 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002006}
2007
2008/* DFU callback: used after serial and direct DFU USB access */
2009void dfu_flush_callback(struct dfu_entity *dfu)
2010{
2011 if (!stm32prog_data)
2012 return;
2013
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002014 if (dfu->dev_type == DFU_DEV_VIRT) {
2015 if (dfu->data.virt.dev_num == PHASE_OTP)
2016 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01002017 else if (dfu->data.virt.dev_num == PHASE_PMIC)
2018 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002019 return;
2020 }
2021
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002022 if (dfu->dev_type == DFU_DEV_RAM) {
2023 if (dfu->alt == 0 &&
2024 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002025 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002026 /* waiting DFU DETACH for reenumeration */
2027 }
2028 }
2029
2030 if (!stm32prog_data->cur_part)
2031 return;
2032
2033 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002034 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002035 stm32prog_next_phase(stm32prog_data);
2036 }
2037}
2038
2039void dfu_initiated_callback(struct dfu_entity *dfu)
2040{
2041 if (!stm32prog_data)
2042 return;
2043
2044 if (!stm32prog_data->cur_part)
2045 return;
2046
2047 /* force the saved offset for the current partition */
2048 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2049 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002050 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002051 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002052 }
2053}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002054
2055void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2056{
2057 struct stm32prog_data *data = stm32prog_data;
2058
2059 if (!stm32prog_data)
2060 return;
2061
2062 if (!stm32prog_data->cur_part)
2063 return;
2064
2065 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2066 stm32prog_err(msg);
2067}