blob: 353aecc09dee5de38c4fac1161189a6e7433fdcb [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010024#include <linux/sizes.h>
25
26#include "stm32prog.h"
27
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010028/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
29#define GPT_HEADER_SZ 34
30
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010031#define OPT_SELECT BIT(0)
32#define OPT_EMPTY BIT(1)
Patrick Delaunay291e7222020-03-18 09:24:57 +010033#define OPT_DELETE BIT(2)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010034
35#define IS_SELECT(part) ((part)->option & OPT_SELECT)
36#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay291e7222020-03-18 09:24:57 +010037#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010038
39#define ALT_BUF_LEN SZ_1K
40
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010041#define ROOTFS_MMC0_UUID \
42 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
43 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
44
45#define ROOTFS_MMC1_UUID \
46 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
47 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
48
49#define ROOTFS_MMC2_UUID \
50 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
51 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
52
Patrick Delaunay33029a52022-03-28 19:25:26 +020053/* RAW partition (binary / bootloader) used Linux - reserved UUID */
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010054#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
55
56/*
57 * unique partition guid (uuid) for partition named "rootfs"
58 * on each MMC instance = SD Card or eMMC
59 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
60 */
61static const efi_guid_t uuid_mmc[3] = {
62 ROOTFS_MMC0_UUID,
63 ROOTFS_MMC1_UUID,
64 ROOTFS_MMC2_UUID
65};
66
Patrick Delaunayc203c212023-06-08 17:09:56 +020067/*
68 * GUID value defined in the FWU specification for identification
69 * of the FWU metadata partition.
70 */
71#define FWU_MDATA_UUID "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23"
72
Patrick Delaunay8dc57682022-03-28 19:25:30 +020073/* FIP type partition UUID used by TF-A*/
74#define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5"
75
76/* unique partition guid (uuid) for FIP partitions A/B */
77#define FIP_A_UUID \
78 EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \
79 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87)
80
81#define FIP_B_UUID \
82 EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \
83 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3)
84
85static const char * const fip_part_name[] = {
86 "fip-a",
87 "fip-b"
88};
89
90static const efi_guid_t fip_part_uuid[] = {
91 FIP_A_UUID,
92 FIP_B_UUID
93};
94
Patrick Delaunay526f66c2020-03-18 09:24:50 +010095/* order of column in flash layout file */
96enum stm32prog_col_t {
97 COL_OPTION,
98 COL_ID,
99 COL_NAME,
100 COL_TYPE,
101 COL_IP,
102 COL_OFFSET,
103 COL_NB_STM32
104};
105
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200106#define FIP_TOC_HEADER_NAME 0xAA640001
107
108struct fip_toc_header {
109 u32 name;
110 u32 serial_number;
111 u64 flags;
112};
113
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200114#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
115 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
116
117/*
118 * Read NVMEM memory for STM32CubeProgrammer
119 *
120 * [in] value[0].a: Type (0 for OTP access)
121 * [out] memref[1].buffer Output buffer to return all read values
122 * [out] memref[1].size Size of buffer to be read
123 *
124 * Return codes:
125 * TEE_SUCCESS - Invoke command success
126 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
127 */
128#define TA_NVMEM_READ 0x0
129
130/*
131 * Write NVMEM memory for STM32CubeProgrammer
132 *
133 * [in] value[0].a Type (0 for OTP access)
134 * [in] memref[1].buffer Input buffer with the values to write
135 * [in] memref[1].size Size of buffer to be written
136 *
137 * Return codes:
138 * TEE_SUCCESS - Invoke command success
139 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
140 */
141#define TA_NVMEM_WRITE 0x1
142
143/* value of TA_NVMEM type = value[in] a */
144#define NVMEM_OTP 0
145
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200146DECLARE_GLOBAL_DATA_PTR;
147
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200148/* OPTEE TA NVMEM open helper */
149static int optee_ta_open(struct stm32prog_data *data)
150{
151 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
152 struct tee_open_session_arg arg;
153 struct udevice *tee = NULL;
154 int rc;
155
156 if (data->tee)
157 return 0;
158
159 tee = tee_find_device(NULL, NULL, NULL, NULL);
160 if (!tee)
161 return -ENODEV;
162
163 memset(&arg, 0, sizeof(arg));
164 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
165 rc = tee_open_session(tee, &arg, 0, NULL);
166 if (rc < 0)
167 return -ENODEV;
168
169 data->tee = tee;
170 data->tee_session = arg.session;
171
172 return 0;
173}
174
175/* OPTEE TA NVMEM invoke helper */
176static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
177 void *buff, ulong size)
178{
179 struct tee_invoke_arg arg;
180 struct tee_param param[2];
181 struct tee_shm *buff_shm;
182 int rc;
183
184 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
185 if (rc)
186 return rc;
187
188 memset(&arg, 0, sizeof(arg));
189 arg.func = cmd;
190 arg.session = data->tee_session;
191
192 memset(param, 0, sizeof(param));
193 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
194 param[0].u.value.a = type;
195
196 if (cmd == TA_NVMEM_WRITE)
197 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
198 else
199 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
200
201 param[1].u.memref.shm = buff_shm;
202 param[1].u.memref.size = size;
203
204 rc = tee_invoke_func(data->tee, &arg, 2, param);
205 if (rc < 0 || arg.ret != 0) {
206 dev_err(data->tee,
207 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
208 arg.ret, rc);
209 if (!rc)
210 rc = -EIO;
211 }
212
213 tee_shm_free(buff_shm);
214
215 return rc;
216}
217
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100218char *stm32prog_get_error(struct stm32prog_data *data)
219{
220 static const char error_msg[] = "Unspecified";
221
222 if (strlen(data->error) == 0)
223 strcpy(data->error, error_msg);
224
225 return data->error;
226}
227
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200228static bool stm32prog_is_fip_header(struct fip_toc_header *header)
229{
230 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
231}
232
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200233static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100234{
235 unsigned int i;
236
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200237 if (header->magic_number !=
238 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
239 log_debug("%s:invalid magic number : 0x%x\n",
240 __func__, header->magic_number);
241 return false;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200242 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200243 if (header->header_version != 0x00010000) {
244 log_debug("%s:invalid header version : 0x%x\n",
245 __func__, header->header_version);
246 return false;
247 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100248
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200249 if (header->reserved1 || header->reserved2) {
250 log_debug("%s:invalid reserved field\n", __func__);
251 return false;
252 }
253 for (i = 0; i < sizeof(header->padding); i++) {
254 if (header->padding[i] != 0) {
255 log_debug("%s:invalid padding field\n", __func__);
256 return false;
257 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100258 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200259
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200260 return true;
261}
262
263static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
264{
265 unsigned int i;
266
267 if (header->magic_number !=
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100268 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100269 log_debug("%s:invalid magic number : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200270 __func__, header->magic_number);
271 return false;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100272 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200273 if (header->header_version != 0x00020000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100274 log_debug("%s:invalid header version : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200275 __func__, header->header_version);
276 return false;
277 }
278 if (header->reserved1 || header->reserved2)
279 return false;
280
281 for (i = 0; i < sizeof(header->padding); i++) {
282 if (header->padding[i] != 0) {
283 log_debug("%s:invalid padding field\n", __func__);
284 return false;
285 }
286 }
287
288 return true;
289}
290
291void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
292{
293 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
294 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
295
296 if (!raw_header || !header) {
297 log_debug("%s:no header data\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200298 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100299 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200300
301 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
302 header->type = HEADER_FIP;
303 header->length = 0;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200304 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100305 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200306 if (stm32prog_is_stm32_header_v1(v1_header)) {
307 header->type = HEADER_STM32IMAGE;
308 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
309 header->image_length = le32_to_cpu(v1_header->image_length);
310 header->length = sizeof(struct stm32_header_v1);
311 return;
312 }
313 if (stm32prog_is_stm32_header_v2(v2_header)) {
314 header->type = HEADER_STM32IMAGE_V2;
315 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
316 header->image_length = le32_to_cpu(v2_header->image_length);
317 header->length = sizeof(struct stm32_header_v1) +
318 v2_header->extension_headers_length;
319 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100320 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100321
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200322 header->type = HEADER_NONE;
323 header->image_checksum = 0x0;
324 header->image_length = 0x0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100325}
326
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200327static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100328{
329 u32 i, checksum;
330 u8 *payload;
331
332 /* compute checksum on payload */
333 payload = (u8 *)addr;
334 checksum = 0;
335 for (i = header->image_length; i > 0; i--)
336 checksum += *(payload++);
337
338 return checksum;
339}
340
341/* FLASHLAYOUT PARSING *****************************************/
342static int parse_option(struct stm32prog_data *data,
343 int i, char *p, struct stm32prog_part_t *part)
344{
345 int result = 0;
346 char *c = p;
347
348 part->option = 0;
349 if (!strcmp(p, "-"))
350 return 0;
351
352 while (*c) {
353 switch (*c) {
354 case 'P':
355 part->option |= OPT_SELECT;
356 break;
357 case 'E':
358 part->option |= OPT_EMPTY;
359 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100360 case 'D':
361 part->option |= OPT_DELETE;
362 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100363 default:
364 result = -EINVAL;
365 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
366 i, *c, p);
367 return -EINVAL;
368 }
369 c++;
370 }
371 if (!(part->option & OPT_SELECT)) {
372 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
373 return -EINVAL;
374 }
375
376 return result;
377}
378
379static int parse_id(struct stm32prog_data *data,
380 int i, char *p, struct stm32prog_part_t *part)
381{
382 int result = 0;
383 unsigned long value;
384
385 result = strict_strtoul(p, 0, &value);
386 part->id = value;
387 if (result || value > PHASE_LAST_USER) {
388 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
389 result = -EINVAL;
390 }
391
392 return result;
393}
394
395static int parse_name(struct stm32prog_data *data,
396 int i, char *p, struct stm32prog_part_t *part)
397{
398 int result = 0;
399
400 if (strlen(p) < sizeof(part->name)) {
401 strcpy(part->name, p);
402 } else {
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200403 stm32prog_err("Layout line %d: partition name too long [%zd]: %s",
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100404 i, strlen(p), p);
405 result = -EINVAL;
406 }
407
408 return result;
409}
410
411static int parse_type(struct stm32prog_data *data,
412 int i, char *p, struct stm32prog_part_t *part)
413{
414 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100415 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100416
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100417 part->bin_nb = 0;
418 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100419 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100420
421 /* search for Binary(X) case */
422 len = strlen(p);
423 part->bin_nb = 1;
424 if (len > 6) {
425 if (len < 8 ||
426 (p[6] != '(') ||
427 (p[len - 1] != ')'))
428 result = -EINVAL;
429 else
430 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600431 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100432 }
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200433 } else if (!strcmp(p, "FIP")) {
434 part->part_type = PART_FIP;
Patrick Delaunayc203c212023-06-08 17:09:56 +0200435 } else if (!strcmp(p, "FWU_MDATA")) {
436 part->part_type = PART_FWU_MDATA;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200437 } else if (!strcmp(p, "ENV")) {
438 part->part_type = PART_ENV;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100439 } else if (!strcmp(p, "System")) {
440 part->part_type = PART_SYSTEM;
Patrick Delaunay0e582be2023-06-08 17:09:55 +0200441 } else if (!strcmp(p, "ESP")) {
442 part->part_type = PART_ESP;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100443 } else if (!strcmp(p, "FileSystem")) {
444 part->part_type = PART_FILESYSTEM;
445 } else if (!strcmp(p, "RawImage")) {
446 part->part_type = RAW_IMAGE;
447 } else {
448 result = -EINVAL;
449 }
450 if (result)
451 stm32prog_err("Layout line %d: type parsing error : '%s'",
452 i, p);
453
454 return result;
455}
456
457static int parse_ip(struct stm32prog_data *data,
458 int i, char *p, struct stm32prog_part_t *part)
459{
460 int result = 0;
461 unsigned int len = 0;
462
463 part->dev_id = 0;
464 if (!strcmp(p, "none")) {
465 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100466 } else if (!strncmp(p, "mmc", 3)) {
467 part->target = STM32PROG_MMC;
468 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100469 } else if (!strncmp(p, "nor", 3)) {
470 part->target = STM32PROG_NOR;
471 len = 3;
472 } else if (!strncmp(p, "nand", 4)) {
473 part->target = STM32PROG_NAND;
474 len = 4;
475 } else if (!strncmp(p, "spi-nand", 8)) {
476 part->target = STM32PROG_SPI_NAND;
477 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100478 } else if (!strncmp(p, "ram", 3)) {
479 part->target = STM32PROG_RAM;
480 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100481 } else {
482 result = -EINVAL;
483 }
484 if (len) {
485 /* only one digit allowed for device id */
486 if (strlen(p) != len + 1) {
487 result = -EINVAL;
488 } else {
489 part->dev_id = p[len] - '0';
490 if (part->dev_id > 9)
491 result = -EINVAL;
492 }
493 }
494 if (result)
495 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
496
497 return result;
498}
499
500static int parse_offset(struct stm32prog_data *data,
501 int i, char *p, struct stm32prog_part_t *part)
502{
503 int result = 0;
504 char *tail;
505
506 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100507 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100508 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100509 /* eMMC boot parttion */
510 if (!strncmp(p, "boot", 4)) {
511 if (strlen(p) != 5) {
512 result = -EINVAL;
513 } else {
514 if (p[4] == '1')
515 part->part_id = -1;
516 else if (p[4] == '2')
517 part->part_id = -2;
518 else
519 result = -EINVAL;
520 }
521 if (result)
522 stm32prog_err("Layout line %d: invalid part '%s'",
523 i, p);
524 } else {
Patrick Delaunay53fe8e72023-04-27 15:36:37 +0200525 part->addr = simple_strtoull(p, &tail, 10);
Patrick Delaunay6915b492020-03-18 09:24:52 +0100526 if (tail == p || *tail != '\0') {
527 stm32prog_err("Layout line %d: invalid offset '%s'",
528 i, p);
529 result = -EINVAL;
530 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100531 }
532
533 return result;
534}
535
536static
537int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
538 struct stm32prog_part_t *part) = {
539 [COL_OPTION] = parse_option,
540 [COL_ID] = parse_id,
541 [COL_NAME] = parse_name,
542 [COL_TYPE] = parse_type,
543 [COL_IP] = parse_ip,
544 [COL_OFFSET] = parse_offset,
545};
546
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100547static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200548 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100549 ulong size)
550{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100551 int column = 0, part_nb = 0, ret;
552 bool end_of_line, eof;
553 char *p, *start, *last, *col;
554 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200555 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100556 int part_list_size;
557 int i;
558
559 data->part_nb = 0;
560
561 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200562 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200563 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100564 u32 checksum;
565
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200566 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200567 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100568
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200569 checksum = stm32prog_header_checksum(addr, &header);
570 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100571 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200572 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100573 return -EIO;
574 }
575 }
576 if (!size)
577 return -EINVAL;
578
579 start = (char *)addr;
580 last = start + size;
581
582 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100583 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100584
585 /* calculate expected number of partitions */
586 part_list_size = 1;
587 p = start;
588 while (*p && (p < last)) {
589 if (*p++ == '\n') {
590 part_list_size++;
591 if (p < last && *p == '#')
592 part_list_size--;
593 }
594 }
595 if (part_list_size > PHASE_LAST_USER) {
596 stm32prog_err("Layout: too many partition (%d)",
597 part_list_size);
598 return -1;
599 }
600 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
601 if (!part) {
602 stm32prog_err("Layout: alloc failed");
603 return -ENOMEM;
604 }
605 data->part_array = part;
606
607 /* main parsing loop */
608 i = 1;
609 eof = false;
610 p = start;
611 col = start; /* 1st column */
612 end_of_line = false;
613 while (!eof) {
614 switch (*p) {
615 /* CR is ignored and replaced by NULL character */
616 case '\r':
617 *p = '\0';
618 p++;
619 continue;
620 case '\0':
621 end_of_line = true;
622 eof = true;
623 break;
624 case '\n':
625 end_of_line = true;
626 break;
627 case '\t':
628 break;
629 case '#':
630 /* comment line is skipped */
631 if (column == 0 && p == col) {
632 while ((p < last) && *p)
633 if (*p++ == '\n')
634 break;
635 col = p;
636 i++;
637 if (p >= last || !*p) {
638 eof = true;
639 end_of_line = true;
640 }
641 continue;
642 }
643 /* fall through */
644 /* by default continue with the next character */
645 default:
646 p++;
647 continue;
648 }
649
650 /* replace by \0: allow string parsing for each column */
651 *p = '\0';
652 p++;
653 if (p >= last) {
654 eof = true;
655 end_of_line = true;
656 }
657
658 /* skip empty line and multiple TAB in tsv file */
659 if (strlen(col) == 0) {
660 col = p;
661 /* skip empty line */
662 if (column == 0 && end_of_line) {
663 end_of_line = false;
664 i++;
665 }
666 continue;
667 }
668
669 if (column < COL_NB_STM32) {
670 ret = parse[column](data, i, col, part);
671 if (ret)
672 return ret;
673 }
674
675 /* save the beginning of the next column */
676 column++;
677 col = p;
678
679 if (!end_of_line)
680 continue;
681
682 /* end of the line detected */
683 end_of_line = false;
684
685 if (column < COL_NB_STM32) {
686 stm32prog_err("Layout line %d: no enought column", i);
687 return -EINVAL;
688 }
689 column = 0;
690 part_nb++;
691 part++;
692 i++;
693 if (part_nb >= part_list_size) {
694 part = NULL;
695 if (!eof) {
696 stm32prog_err("Layout: no enought memory for %d part",
697 part_nb);
698 return -EINVAL;
699 }
700 }
701 }
702 data->part_nb = part_nb;
703 if (data->part_nb == 0) {
704 stm32prog_err("Layout: no partition found");
705 return -ENODEV;
706 }
707
708 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100709}
710
711static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
712{
713 struct stm32prog_part_t *parta, *partb;
714
715 parta = container_of(a, struct stm32prog_part_t, list);
716 partb = container_of(b, struct stm32prog_part_t, list);
717
Patrick Delaunay6915b492020-03-18 09:24:52 +0100718 if (parta->part_id != partb->part_id)
719 return parta->part_id - partb->part_id;
720 else
721 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100722}
723
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100724static void get_mtd_by_target(char *string, enum stm32prog_target target,
725 int dev_id)
726{
727 const char *dev_str;
728
729 switch (target) {
730 case STM32PROG_NOR:
731 dev_str = "nor";
732 break;
733 case STM32PROG_NAND:
734 dev_str = "nand";
735 break;
736 case STM32PROG_SPI_NAND:
737 dev_str = "spi-nand";
738 break;
739 default:
740 dev_str = "invalid";
741 break;
742 }
743 sprintf(string, "%s%d", dev_str, dev_id);
744}
745
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100746static int init_device(struct stm32prog_data *data,
747 struct stm32prog_dev_t *dev)
748{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100749 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100750 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100751 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200752 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100753 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100754 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100755 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100756 u64 first_addr = 0, last_addr = 0;
757 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100758 u64 part_addr, part_size;
759 bool part_found;
760 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200761 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100762
763 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100764 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200765 if (!IS_ENABLED(CONFIG_MMC)) {
766 stm32prog_err("unknown device type = %d", dev->target);
767 return -ENODEV;
768 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100769 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200770 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100771 stm32prog_err("mmc device %d not found", dev->dev_id);
772 return -ENODEV;
773 }
774 block_dev = mmc_get_blk_desc(mmc);
775 if (!block_dev) {
776 stm32prog_err("mmc device %d not probed", dev->dev_id);
777 return -ENODEV;
778 }
779 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
780 dev->mmc = mmc;
781
782 /* reserve a full erase group for each GTP headers */
783 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
784 first_addr = dev->erase_size;
785 last_addr = (u64)(block_dev->lba -
786 mmc->erase_grp_size) *
787 block_dev->blksz;
788 } else {
789 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
790 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
791 block_dev->blksz;
792 }
Patrice Chotardfd9a2fa2024-11-29 13:27:07 +0100793 log_debug("MMC %d: lba=%lld blksz=%ld\n", dev->dev_id,
794 (u64)block_dev->lba, block_dev->blksz);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100795 log_debug(" available address = 0x%llx..0x%llx\n",
796 first_addr, last_addr);
797 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100798 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100799 case STM32PROG_NOR:
800 case STM32PROG_NAND:
801 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200802 if (!IS_ENABLED(CONFIG_MTD)) {
803 stm32prog_err("unknown device type = %d", dev->target);
804 return -ENODEV;
805 }
Patrice Chotard49569082023-06-08 17:16:40 +0200806 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
807 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100808 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100809 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100810
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100811 mtd = get_mtd_device_nm(mtd_id);
812 if (IS_ERR(mtd)) {
813 stm32prog_err("MTD device %s not found", mtd_id);
814 return -ENODEV;
815 }
816 first_addr = 0;
817 last_addr = mtd->size;
818 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100819 log_debug("MTD device %s: size=%lld erasesize=%d\n",
820 mtd_id, mtd->size, mtd->erasesize);
821 log_debug(" available address = 0x%llx..0x%llx\n",
822 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100823 dev->mtd = mtd;
824 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100825 case STM32PROG_RAM:
826 first_addr = gd->bd->bi_dram[0].start;
827 last_addr = first_addr + gd->bd->bi_dram[0].size;
828 dev->erase_size = 1;
829 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100830 default:
831 stm32prog_err("unknown device type = %d", dev->target);
832 return -ENODEV;
833 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100834 log_debug(" erase size = 0x%x\n", dev->erase_size);
835 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100836
837 /* order partition list in offset order */
838 list_sort(NULL, &dev->part_list, &part_cmp);
839 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100840 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100841 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100842 if (part->bin_nb > 1) {
843 if ((dev->target != STM32PROG_NAND &&
844 dev->target != STM32PROG_SPI_NAND) ||
845 part->id >= PHASE_FIRST_USER ||
846 strncmp(part->name, "fsbl", 4)) {
847 stm32prog_err("%s (0x%x): multiple binary %d not supported",
848 part->name, part->id,
849 part->bin_nb);
850 return -EINVAL;
851 }
852 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100853 if (part->part_type == RAW_IMAGE) {
854 part->part_id = 0x0;
855 part->addr = 0x0;
856 if (block_dev)
857 part->size = block_dev->lba * block_dev->blksz;
858 else
859 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100860 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
861 part->option, part->id, part->name,
862 part->part_type, part->bin_nb, part->target,
863 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100864 continue;
865 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100866 if (part->part_id < 0) { /* boot hw partition for eMMC */
867 if (mmc) {
868 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100869 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100870 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100871 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100872 part->part_id);
873 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100874 }
875 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100876 part->part_id = part_id++;
877
878 /* last partition : size to the end of the device */
879 if (part->list.next != &dev->part_list) {
880 next_part =
881 container_of(part->list.next,
882 struct stm32prog_part_t,
883 list);
884 if (part->addr < next_part->addr) {
885 part->size = next_part->addr -
886 part->addr;
887 } else {
888 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
889 part->name, part->id,
890 part->addr,
891 next_part->name,
892 next_part->id,
893 next_part->addr);
894 return -EINVAL;
895 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100896 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100897 if (part->addr <= last_addr) {
898 part->size = last_addr - part->addr;
899 } else {
900 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
901 part->name, part->id,
902 part->addr, last_addr);
903 return -EINVAL;
904 }
905 }
906 if (part->addr < first_addr) {
907 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100908 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100909 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100910 return -EINVAL;
911 }
912 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100913 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
914 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
915 part->name, part->id, part->addr,
916 part->dev->erase_size);
917 return -EINVAL;
918 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100919 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
920 part->part_id, part->option, part->id, part->name,
921 part->part_type, part->bin_nb, part->target,
922 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100923
924 part_addr = 0;
925 part_size = 0;
926 part_found = false;
927
928 /* check coherency with existing partition */
929 if (block_dev) {
930 /*
931 * block devices with GPT: check user partition size
932 * only for partial update, the GPT partions are be
933 * created for full update
934 */
935 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100936 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100937 continue;
938 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600939 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100940
941 ret = part_get_info(block_dev, part->part_id,
942 &partinfo);
943
944 if (ret) {
945 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
946 part->name, part->id,
947 part_id, part->dev_id);
948 return -ENODEV;
949 }
950 part_addr = (u64)partinfo.start * partinfo.blksz;
951 part_size = (u64)partinfo.size * partinfo.blksz;
952 part_name = (char *)partinfo.name;
953 part_found = true;
954 }
955
Patrick Delaunay8040da12020-07-31 16:31:52 +0200956 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200957 i = 0;
958 list_for_each_entry(partition, &mtd->partitions, node) {
959 if ((part->part_id - 1) == i) {
960 part_found = true;
961 break;
962 }
963 i++;
964 }
965 if (part_found) {
966 part_addr = partition->offset;
967 part_size = partition->size;
968 part_name = partition->name;
969 } else {
970 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
971 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100972 return -ENODEV;
973 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100974 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200975
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200976 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100977 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100978 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100979 continue;
980 }
981
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100982 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100983
984 if (part->addr != part_addr) {
985 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
986 part->name, part->id, part->part_id,
987 part_name, part->addr, part_addr);
988 return -ENODEV;
989 }
990 if (part->size != part_size) {
991 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
992 part->name, part->id, part->part_id,
993 part_name, part->addr, part->size,
994 part_size);
995 return -ENODEV;
996 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100997 }
998 return 0;
999}
1000
1001static int treat_partition_list(struct stm32prog_data *data)
1002{
1003 int i, j;
1004 struct stm32prog_part_t *part;
1005
1006 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1007 data->dev[j].target = STM32PROG_NONE;
1008 INIT_LIST_HEAD(&data->dev[j].part_list);
1009 }
1010
Patrick Delaunayc5112242020-03-18 09:24:55 +01001011 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001012 for (i = 0; i < data->part_nb; i++) {
1013 part = &data->part_array[i];
1014 part->alt_id = -1;
1015
1016 /* skip partition with IP="none" */
1017 if (part->target == STM32PROG_NONE) {
1018 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001019 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1020 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001021 return -EINVAL;
1022 }
1023 continue;
1024 }
1025
1026 if (part->id == PHASE_FLASHLAYOUT ||
1027 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001028 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1029 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001030 return -EINVAL;
1031 }
1032 for (j = i + 1; j < data->part_nb; j++) {
1033 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001034 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1035 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001036 return -EINVAL;
1037 }
1038 }
1039 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1040 if (data->dev[j].target == STM32PROG_NONE) {
1041 /* new device found */
1042 data->dev[j].target = part->target;
1043 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001044 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001045 data->dev_nb++;
1046 break;
1047 } else if ((part->target == data->dev[j].target) &&
1048 (part->dev_id == data->dev[j].dev_id)) {
1049 break;
1050 }
1051 }
1052 if (j == STM32PROG_MAX_DEV) {
1053 stm32prog_err("Layout: too many device");
1054 return -EINVAL;
1055 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001056 switch (part->target) {
1057 case STM32PROG_NOR:
1058 if (!data->fsbl_nor_detected &&
1059 !strncmp(part->name, "fsbl", 4))
1060 data->fsbl_nor_detected = true;
1061 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001062 default:
1063 break;
1064 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001065 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001066 if (!IS_SELECT(part))
1067 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001068 list_add_tail(&part->list, &data->dev[j].part_list);
1069 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001070
1071 return 0;
1072}
1073
Patrick Delaunay8040da12020-07-31 16:31:52 +02001074static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001075{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001076 int offset = 0;
1077 const int buflen = SZ_8K;
1078 char *buf;
1079 char uuid[UUID_STR_LEN + 1];
1080 unsigned char *uuid_bin;
1081 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001082 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001083 bool rootfs_found;
1084 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001085 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001086
1087 buf = malloc(buflen);
1088 if (!buf)
1089 return -ENOMEM;
1090
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001091 /* initialize the selected device */
1092 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001093 /* create gpt partition support only for full update on MMC */
1094 if (data->dev[i].target != STM32PROG_MMC ||
1095 !data->dev[i].full_update)
1096 continue;
1097
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001098 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001099 offset = 0;
1100 rootfs_found = false;
1101 memset(buf, 0, buflen);
1102
1103 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001104 /* skip eMMC boot partitions */
1105 if (part->part_id < 0)
1106 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001107 /* skip Raw Image */
1108 if (part->part_type == RAW_IMAGE)
1109 continue;
1110
1111 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001112 log_debug("\n%s: buffer too small, %s skippped",
1113 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001114 continue;
1115 }
1116
1117 if (!offset)
1118 offset += sprintf(buf, "gpt write mmc %d \"",
1119 data->dev[i].dev_id);
1120
1121 offset += snprintf(buf + offset, buflen - offset,
1122 "name=%s,start=0x%llx,size=0x%llx",
1123 part->name,
1124 part->addr,
1125 part->size);
1126
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001127 switch (part->part_type) {
1128 case PART_BINARY:
1129 type_str = LINUX_RESERVED_UUID;
1130 break;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +02001131 case PART_ENV:
1132 type_str = "u-boot-env";
1133 break;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001134 case PART_FIP:
1135 type_str = FIP_TYPE_UUID;
1136 break;
Patrick Delaunayc203c212023-06-08 17:09:56 +02001137 case PART_FWU_MDATA:
1138 type_str = FWU_MDATA_UUID;
1139 break;
Patrick Delaunay0e582be2023-06-08 17:09:55 +02001140 case PART_ESP:
1141 /* EFI System Partition */
1142 type_str = "system";
1143 break;
1144 default: /* PART_FILESYSTEM or PART_SYSTEM for distro */
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001145 type_str = "linux";
1146 break;
1147 }
1148 offset += snprintf(buf + offset,
1149 buflen - offset,
1150 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001151
1152 if (part->part_type == PART_SYSTEM)
1153 offset += snprintf(buf + offset,
1154 buflen - offset,
1155 ",bootable");
1156
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001157 /* partition UUID */
1158 uuid_bin = NULL;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001159 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1160 mmc_id = part->dev_id;
1161 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001162 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1163 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1164 }
1165 if (part->part_type == PART_FIP) {
1166 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1167 if (!strcmp(part->name, fip_part_name[j])) {
1168 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1169 break;
1170 }
1171 }
1172 if (uuid_bin) {
1173 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1174 offset += snprintf(buf + offset,
1175 buflen - offset,
1176 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001177 }
1178
1179 offset += snprintf(buf + offset, buflen - offset, ";");
1180 }
1181
1182 if (offset) {
1183 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001184 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001185 if (run_command(buf, 0)) {
1186 stm32prog_err("GPT partitionning fail: %s",
1187 buf);
1188 free(buf);
1189
1190 return -1;
1191 }
1192 }
1193
1194 if (data->dev[i].mmc)
1195 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1196
1197#ifdef DEBUG
1198 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001199 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001200 if (run_command(buf, 0))
1201 printf("fail !\n");
1202 else
1203 printf("OK\n");
1204
1205 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1206 run_command(buf, 0);
1207#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001208 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001209 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001210
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001211#ifdef DEBUG
1212 run_command("mtd list", 0);
1213#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001214 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001215
1216 return 0;
1217}
1218
1219static int stm32prog_alt_add(struct stm32prog_data *data,
1220 struct dfu_entity *dfu,
1221 struct stm32prog_part_t *part)
1222{
1223 int ret = 0;
1224 int offset = 0;
1225 char devstr[10];
1226 char dfustr[10];
1227 char buf[ALT_BUF_LEN];
1228 u32 size;
1229 char multiplier, type;
1230
1231 /* max 3 digit for sector size */
Patrice Chotard01e58942024-11-29 13:27:08 +01001232 if (part->size > SZ_1G) {
1233 size = (u32)(part->size / SZ_1G);
1234 multiplier = 'G';
1235 } else if (part->size > SZ_1M) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001236 size = (u32)(part->size / SZ_1M);
1237 multiplier = 'M';
1238 } else if (part->size > SZ_1K) {
1239 size = (u32)(part->size / SZ_1K);
1240 multiplier = 'K';
1241 } else {
1242 size = (u32)part->size;
1243 multiplier = 'B';
1244 }
1245 if (IS_SELECT(part) && !IS_EMPTY(part))
1246 type = 'e'; /*Readable and Writeable*/
1247 else
1248 type = 'a';/*Readable*/
1249
1250 memset(buf, 0, sizeof(buf));
1251 offset = snprintf(buf, ALT_BUF_LEN - offset,
1252 "@%s/0x%02x/1*%d%c%c ",
1253 part->name, part->id,
1254 size, multiplier, type);
1255
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001256 if (part->target == STM32PROG_RAM) {
1257 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1258 "ram 0x%llx 0x%llx",
1259 part->addr, part->size);
1260 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001261 u64 dfu_size;
1262
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001263 if (part->dev->target == STM32PROG_MMC)
1264 dfu_size = part->size / part->dev->mmc->read_bl_len;
1265 else
1266 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001267 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1268 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001269 } else if (part->part_id < 0) {
1270 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1271
1272 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1273 "raw 0x%llx 0x%llx",
1274 part->addr, nb_blk);
1275 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001276 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001277 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001278 if (part->part_type == PART_SYSTEM &&
1279 (part->target == STM32PROG_NAND ||
1280 part->target == STM32PROG_NOR ||
1281 part->target == STM32PROG_SPI_NAND))
1282 offset += snprintf(buf + offset,
1283 ALT_BUF_LEN - offset,
1284 "partubi");
1285 else
1286 offset += snprintf(buf + offset,
1287 ALT_BUF_LEN - offset,
1288 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001289 /* dev_id requested by DFU MMC */
1290 if (part->target == STM32PROG_MMC)
1291 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1292 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001293 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001294 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001295 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001296 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001297 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001298 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001299 if (IS_ENABLED(CONFIG_MMC)) {
1300 ret = 0;
1301 sprintf(dfustr, "mmc");
1302 sprintf(devstr, "%d", part->dev_id);
1303 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001304 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001305 case STM32PROG_NAND:
1306 case STM32PROG_NOR:
1307 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001308 if (IS_ENABLED(CONFIG_MTD)) {
1309 ret = 0;
1310 sprintf(dfustr, "mtd");
1311 get_mtd_by_target(devstr, part->target, part->dev_id);
1312 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001313 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001314 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001315 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001316 sprintf(dfustr, "ram");
1317 sprintf(devstr, "0");
1318 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001319 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001320 break;
1321 }
1322 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001323 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001324 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001325 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001326 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001327 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001328 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1329 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001330
1331 return ret;
1332}
1333
1334static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1335 char *name, int phase, int size)
1336{
1337 int ret = 0;
1338 char devstr[4];
1339 char buf[ALT_BUF_LEN];
1340
1341 sprintf(devstr, "%d", phase);
1342 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1343 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001344 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001345
1346 return ret;
1347}
1348
1349static int dfu_init_entities(struct stm32prog_data *data)
1350{
1351 int ret = 0;
1352 int phase, i, alt_id;
1353 struct stm32prog_part_t *part;
1354 struct dfu_entity *dfu;
1355 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001356 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001357
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001358 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001359
1360 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1361 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1362 otp_size = OTP_SIZE_SMC;
1363 /* check if PTA BSEC is supported */
1364 ret = optee_ta_open(data);
1365 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1366 if (!ret && data->tee)
1367 otp_size = OTP_SIZE_TA;
1368 if (otp_size)
1369 alt_nb++; /* OTP*/
1370 }
1371
Patrick Delaunay7d145402021-05-18 15:12:09 +02001372 if (CONFIG_IS_ENABLED(DM_PMIC))
1373 alt_nb++; /* PMIC NVMEM*/
1374
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001375 if (data->part_nb == 0)
1376 alt_nb++; /* +1 for FlashLayout */
1377 else
1378 for (i = 0; i < data->part_nb; i++) {
1379 if (data->part_array[i].target != STM32PROG_NONE)
1380 alt_nb++;
1381 }
1382
1383 if (dfu_alt_init(alt_nb, &dfu))
1384 return -ENODEV;
1385
1386 puts("DFU alt info setting: ");
1387 if (data->part_nb) {
1388 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001389 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001390 for (phase = 1;
1391 (phase <= PHASE_LAST_USER) &&
1392 (alt_id < alt_nb) && !ret;
1393 phase++) {
1394 /* ordering alt setting by phase id */
1395 part = NULL;
1396 for (i = 0; i < data->part_nb; i++) {
1397 if (phase == data->part_array[i].id) {
1398 part = &data->part_array[i];
1399 break;
1400 }
1401 }
1402 if (!part)
1403 continue;
1404 if (part->target == STM32PROG_NONE)
1405 continue;
1406 part->alt_id = alt_id;
1407 alt_id++;
1408
1409 ret = stm32prog_alt_add(data, dfu, part);
1410 }
1411 } else {
1412 char buf[ALT_BUF_LEN];
1413
1414 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001415 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001416 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001417 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001418 }
1419
1420 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001421 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001422
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001423 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1424 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001425
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001426 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001427 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001428
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001429 if (ret)
1430 stm32prog_err("dfu init failed: %d", ret);
1431 puts("done\n");
1432
1433#ifdef DEBUG
1434 dfu_show_entities();
1435#endif
1436 return ret;
1437}
1438
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001439int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1440 long *size)
1441{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001442 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001443 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001444
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001445 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1446 stm32prog_err("OTP update not supported");
1447
1448 return -EOPNOTSUPP;
1449 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001450
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001451 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001452 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001453 if (!data->otp_part) {
1454 stm32prog_err("OTP write issue %d", -ENOMEM);
1455
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001456 return -ENOMEM;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001457 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001458 }
1459
1460 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001461 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001462
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001463 if (offset + *size > otp_size)
1464 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001465
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001466 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001467
1468 return 0;
1469}
1470
1471int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1472 long *size)
1473{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001474 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001475 int result = 0;
1476
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001477 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001478 stm32prog_err("OTP update not supported");
1479
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001480 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001481 }
1482
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001483 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001484 /* alway read for first packet */
1485 if (!offset) {
1486 if (!data->otp_part)
1487 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001488 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001489
1490 if (!data->otp_part) {
1491 result = -ENOMEM;
1492 goto end_otp_read;
1493 }
1494
1495 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001496 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001497
1498 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001499 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001500 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1501 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1502 data->otp_part, OTP_SIZE_TA);
1503 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001504 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001505 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001506 if (result)
1507 goto end_otp_read;
1508 }
1509
1510 if (!data->otp_part) {
1511 result = -ENOMEM;
1512 goto end_otp_read;
1513 }
1514
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001515 if (offset + *size > otp_size)
1516 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001517 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001518
1519end_otp_read:
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001520 if (result)
1521 stm32prog_err("OTP read issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001522 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001523
1524 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001525}
1526
1527int stm32prog_otp_start(struct stm32prog_data *data)
1528{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001529 int result = 0;
1530 struct arm_smccc_res res;
1531
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001532 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001533 stm32prog_err("OTP update not supported");
1534
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001535 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001536 }
1537
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001538 if (!data->otp_part) {
1539 stm32prog_err("start OTP without data");
1540 return -1;
1541 }
1542
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001543 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001544 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1545 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1546 data->otp_part, OTP_SIZE_TA);
1547 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001548 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001549 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001550
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001551 if (!res.a0) {
1552 switch (res.a1) {
1553 case 0:
1554 result = 0;
1555 break;
1556 case 1:
1557 stm32prog_err("Provisioning");
1558 result = 0;
1559 break;
1560 default:
1561 log_err("%s: OTP incorrect value (err = %ld)\n",
1562 __func__, res.a1);
1563 result = -EINVAL;
1564 break;
1565 }
1566 } else {
1567 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1568 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001569 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001570 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001571 }
1572
1573 free(data->otp_part);
1574 data->otp_part = NULL;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001575 if (result)
1576 stm32prog_err("OTP write issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001577 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001578
1579 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001580}
1581
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001582int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1583 long *size)
1584{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001585 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001586
1587 if (!offset)
1588 memset(data->pmic_part, 0, PMIC_SIZE);
1589
1590 if (offset + *size > PMIC_SIZE)
1591 *size = PMIC_SIZE - offset;
1592
1593 memcpy(&data->pmic_part[offset], buffer, *size);
1594
1595 return 0;
1596}
1597
1598int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1599 long *size)
1600{
1601 int result = 0, ret;
1602 struct udevice *dev;
1603
Simon Glassb24d5752023-02-05 15:40:35 -07001604 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001605 stm32prog_err("PMIC update not supported");
1606
1607 return -EOPNOTSUPP;
1608 }
1609
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001610 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001611 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001612 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001613 &dev);
1614 if (ret)
1615 return ret;
1616
1617 /* alway request PMIC for first packet */
1618 if (!offset) {
1619 /* init struct with 0 */
1620 memset(data->pmic_part, 0, PMIC_SIZE);
1621
1622 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001623 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001624 &dev);
1625 if (ret)
1626 return ret;
1627
1628 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1629 if (ret < 0) {
1630 result = ret;
1631 goto end_pmic_read;
1632 }
1633 if (ret != PMIC_SIZE) {
1634 result = -EACCES;
1635 goto end_pmic_read;
1636 }
1637 }
1638
1639 if (offset + *size > PMIC_SIZE)
1640 *size = PMIC_SIZE - offset;
1641
1642 memcpy(buffer, &data->pmic_part[offset], *size);
1643
1644end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001645 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001646 return result;
1647}
1648
1649int stm32prog_pmic_start(struct stm32prog_data *data)
1650{
1651 int ret;
1652 struct udevice *dev;
1653
Simon Glassb24d5752023-02-05 15:40:35 -07001654 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001655 stm32prog_err("PMIC update not supported");
1656
1657 return -EOPNOTSUPP;
1658 }
1659
1660 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001661 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001662 &dev);
1663 if (ret)
1664 return ret;
1665
1666 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1667}
1668
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001669/* copy FSBL on NAND to improve reliability on NAND */
1670static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1671{
1672 int ret, i;
1673 void *fsbl;
1674 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001675 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001676 struct dfu_entity *dfu;
1677 long size, offset;
1678
1679 if (part->target != STM32PROG_NAND &&
1680 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001681 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001682
1683 dfu = dfu_get_entity(part->alt_id);
1684
1685 /* read header */
1686 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001687 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001688 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1689 if (ret)
1690 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001691
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001692 stm32prog_header_check((ulong)&raw_header, &header);
1693 if (header.type != HEADER_STM32IMAGE &&
1694 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001695 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001696
1697 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001698 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001699 size = round_up(size, part->dev->mtd->erasesize);
1700 fsbl = calloc(1, size);
1701 if (!fsbl)
1702 return -ENOMEM;
1703 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001704 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001705 if (ret)
1706 goto error;
1707
1708 dfu_transaction_cleanup(dfu);
1709 offset = 0;
1710 for (i = part->bin_nb - 1; i > 0; i--) {
1711 offset += size;
1712 /* write to the next erase block */
1713 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001714 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1715 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001716 if (ret)
1717 goto error;
1718 }
1719
1720error:
1721 free(fsbl);
1722 return ret;
1723}
1724
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001725static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001726{
1727 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001728#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001729 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1730 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001731 data->phase = PHASE_END;
1732 log_notice("U-Boot script received\n");
1733 return;
1734 }
1735#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001736 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001737 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001738 stm32prog_err("Layout: invalid FlashLayout");
1739 return;
1740 }
1741
1742 if (!data->cur_part)
1743 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001744
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001745 if (data->cur_part->target == STM32PROG_RAM) {
1746 if (data->cur_part->part_type == PART_SYSTEM)
1747 data->uimage = data->cur_part->addr;
1748 if (data->cur_part->part_type == PART_FILESYSTEM)
1749 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001750 if (data->cur_part->part_type == PART_BINARY) {
1751 data->initrd = data->cur_part->addr;
1752 data->initrd_size = offset;
1753 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001754 }
1755
Patrick Delaunay6915b492020-03-18 09:24:52 +01001756 if (CONFIG_IS_ENABLED(MMC) &&
1757 data->cur_part->part_id < 0) {
1758 char cmdbuf[60];
1759
1760 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1761 data->cur_part->dev_id, data->cur_part->dev_id,
1762 -(data->cur_part->part_id));
1763 if (run_command(cmdbuf, 0)) {
1764 stm32prog_err("commands '%s' failed", cmdbuf);
1765 return;
1766 }
1767 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001768
Simon Glass8e05bb12023-02-05 15:40:17 -07001769 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001770 data->cur_part->bin_nb > 1) {
1771 if (stm32prog_copy_fsbl(data->cur_part)) {
1772 stm32prog_err("%s (0x%x): copy of fsbl failed",
1773 data->cur_part->name, data->cur_part->id);
1774 return;
1775 }
1776 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001777}
1778
1779void stm32prog_do_reset(struct stm32prog_data *data)
1780{
1781 if (data->phase == PHASE_RESET) {
1782 data->phase = PHASE_DO_RESET;
1783 puts("Reset requested\n");
1784 }
1785}
1786
1787void stm32prog_next_phase(struct stm32prog_data *data)
1788{
1789 int phase, i;
1790 struct stm32prog_part_t *part;
1791 bool found;
1792
1793 phase = data->phase;
1794 switch (phase) {
1795 case PHASE_RESET:
1796 case PHASE_END:
1797 case PHASE_DO_RESET:
1798 return;
1799 }
1800
1801 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001802 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001803 data->cur_part = NULL;
1804 data->phase = PHASE_END;
1805 found = false;
1806 do {
1807 phase++;
1808 if (phase > PHASE_LAST_USER)
1809 break;
1810 for (i = 0; i < data->part_nb; i++) {
1811 part = &data->part_array[i];
1812 if (part->id == phase) {
1813 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1814 data->cur_part = part;
1815 data->phase = phase;
1816 found = true;
1817 }
1818 break;
1819 }
1820 }
1821 } while (!found);
1822
1823 if (data->phase == PHASE_END)
1824 puts("Phase=END\n");
1825}
1826
Patrick Delaunay291e7222020-03-18 09:24:57 +01001827static int part_delete(struct stm32prog_data *data,
1828 struct stm32prog_part_t *part)
1829{
1830 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001831 unsigned long blks, blks_offset, blks_size;
1832 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001833 char cmdbuf[40];
1834 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001835
1836 printf("Erasing %s ", part->name);
1837 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001838 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001839 if (!IS_ENABLED(CONFIG_MMC)) {
1840 ret = -1;
1841 stm32prog_err("%s (0x%x): erase invalid",
1842 part->name, part->id);
1843 break;
1844 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001845 printf("on mmc %d: ", part->dev->dev_id);
1846 block_dev = mmc_get_blk_desc(part->dev->mmc);
1847 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1848 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1849 /* -1 or -2 : delete boot partition of MMC
1850 * need to switch to associated hwpart 1 or 2
1851 */
1852 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001853 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001854 part->dev->dev_id,
1855 -part->part_id))
1856 return -1;
1857
1858 blks = blk_derase(block_dev, blks_offset, blks_size);
1859
1860 /* return to user partition */
1861 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001862 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001863 part->dev->dev_id, 0);
1864 if (blks != blks_size) {
1865 ret = -1;
1866 stm32prog_err("%s (0x%x): MMC erase failed",
1867 part->name, part->id);
1868 }
1869 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001870 case STM32PROG_NOR:
1871 case STM32PROG_NAND:
1872 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001873 if (!IS_ENABLED(CONFIG_MTD)) {
1874 ret = -1;
1875 stm32prog_err("%s (0x%x): erase invalid",
1876 part->name, part->id);
1877 break;
1878 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001879 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1880 printf("on %s: ", devstr);
1881 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1882 devstr, part->addr, part->size);
1883 if (run_command(cmdbuf, 0)) {
1884 ret = -1;
1885 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1886 part->name, part->id, cmdbuf);
1887 }
1888 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001889 case STM32PROG_RAM:
1890 printf("on ram: ");
1891 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1892 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001893 default:
1894 ret = -1;
1895 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1896 break;
1897 }
1898 if (!ret)
1899 printf("done\n");
1900
1901 return ret;
1902}
1903
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001904static void stm32prog_devices_init(struct stm32prog_data *data)
1905{
1906 int i;
1907 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001908 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001909
1910 ret = treat_partition_list(data);
1911 if (ret)
1912 goto error;
1913
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001914 /* empty flashlayout */
1915 if (!data->dev_nb)
1916 return;
1917
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001918 /* initialize the selected device */
1919 for (i = 0; i < data->dev_nb; i++) {
1920 ret = init_device(data, &data->dev[i]);
1921 if (ret)
1922 goto error;
1923 }
1924
Patrick Delaunay291e7222020-03-18 09:24:57 +01001925 /* delete RAW partition before create partition */
1926 for (i = 0; i < data->part_nb; i++) {
1927 part = &data->part_array[i];
1928
1929 if (part->part_type != RAW_IMAGE)
1930 continue;
1931
1932 if (!IS_SELECT(part) || !IS_DELETE(part))
1933 continue;
1934
1935 ret = part_delete(data, part);
1936 if (ret)
1937 goto error;
1938 }
1939
Patrick Delaunay8040da12020-07-31 16:31:52 +02001940 if (IS_ENABLED(CONFIG_MMC)) {
1941 ret = create_gpt_partitions(data);
1942 if (ret)
1943 goto error;
1944 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001945
Patrick Delaunay291e7222020-03-18 09:24:57 +01001946 /* delete partition GPT or MTD */
1947 for (i = 0; i < data->part_nb; i++) {
1948 part = &data->part_array[i];
1949
1950 if (part->part_type == RAW_IMAGE)
1951 continue;
1952
1953 if (!IS_SELECT(part) || !IS_DELETE(part))
1954 continue;
1955
1956 ret = part_delete(data, part);
1957 if (ret)
1958 goto error;
1959 }
1960
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001961 return;
1962
1963error:
1964 data->part_nb = 0;
1965}
1966
1967int stm32prog_dfu_init(struct stm32prog_data *data)
1968{
1969 /* init device if no error */
1970 if (data->part_nb)
1971 stm32prog_devices_init(data);
1972
1973 if (data->part_nb)
1974 stm32prog_next_phase(data);
1975
1976 /* prepare DFU for device read/write */
1977 dfu_free_entities();
1978 return dfu_init_entities(data);
1979}
1980
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001981int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001982{
1983 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001984 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001985 data->phase = PHASE_FLASHLAYOUT;
1986
1987 return parse_flash_layout(data, addr, size);
1988}
1989
1990void stm32prog_clean(struct stm32prog_data *data)
1991{
1992 /* clean */
1993 dfu_free_entities();
1994 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001995 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001996 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001997
1998 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1999 tee_close_session(data->tee, data->tee_session);
2000 data->tee = NULL;
2001 data->tee_session = 0x0;
2002 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002003}
2004
2005/* DFU callback: used after serial and direct DFU USB access */
2006void dfu_flush_callback(struct dfu_entity *dfu)
2007{
2008 if (!stm32prog_data)
2009 return;
2010
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002011 if (dfu->dev_type == DFU_DEV_VIRT) {
2012 if (dfu->data.virt.dev_num == PHASE_OTP)
2013 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01002014 else if (dfu->data.virt.dev_num == PHASE_PMIC)
2015 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002016 return;
2017 }
2018
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002019 if (dfu->dev_type == DFU_DEV_RAM) {
2020 if (dfu->alt == 0 &&
2021 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002022 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002023 /* waiting DFU DETACH for reenumeration */
2024 }
2025 }
2026
2027 if (!stm32prog_data->cur_part)
2028 return;
2029
2030 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002031 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002032 stm32prog_next_phase(stm32prog_data);
2033 }
2034}
2035
2036void dfu_initiated_callback(struct dfu_entity *dfu)
2037{
2038 if (!stm32prog_data)
2039 return;
2040
2041 if (!stm32prog_data->cur_part)
2042 return;
2043
2044 /* force the saved offset for the current partition */
2045 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2046 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002047 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002048 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002049 }
2050}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002051
2052void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2053{
2054 struct stm32prog_data *data = stm32prog_data;
2055
2056 if (!stm32prog_data)
2057 return;
2058
2059 if (!stm32prog_data->cur_part)
2060 return;
2061
2062 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2063 stm32prog_err(msg);
2064}