blob: 393f9a1b411cadea6c271e40af14c3c005d4f57d [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 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100793 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
794 block_dev->lba, block_dev->blksz);
795 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 */
1232 if (part->size > SZ_1M) {
1233 size = (u32)(part->size / SZ_1M);
1234 multiplier = 'M';
1235 } else if (part->size > SZ_1K) {
1236 size = (u32)(part->size / SZ_1K);
1237 multiplier = 'K';
1238 } else {
1239 size = (u32)part->size;
1240 multiplier = 'B';
1241 }
1242 if (IS_SELECT(part) && !IS_EMPTY(part))
1243 type = 'e'; /*Readable and Writeable*/
1244 else
1245 type = 'a';/*Readable*/
1246
1247 memset(buf, 0, sizeof(buf));
1248 offset = snprintf(buf, ALT_BUF_LEN - offset,
1249 "@%s/0x%02x/1*%d%c%c ",
1250 part->name, part->id,
1251 size, multiplier, type);
1252
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001253 if (part->target == STM32PROG_RAM) {
1254 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1255 "ram 0x%llx 0x%llx",
1256 part->addr, part->size);
1257 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001258 u64 dfu_size;
1259
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001260 if (part->dev->target == STM32PROG_MMC)
1261 dfu_size = part->size / part->dev->mmc->read_bl_len;
1262 else
1263 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001264 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1265 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001266 } else if (part->part_id < 0) {
1267 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1268
1269 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1270 "raw 0x%llx 0x%llx",
1271 part->addr, nb_blk);
1272 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001273 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001274 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001275 if (part->part_type == PART_SYSTEM &&
1276 (part->target == STM32PROG_NAND ||
1277 part->target == STM32PROG_NOR ||
1278 part->target == STM32PROG_SPI_NAND))
1279 offset += snprintf(buf + offset,
1280 ALT_BUF_LEN - offset,
1281 "partubi");
1282 else
1283 offset += snprintf(buf + offset,
1284 ALT_BUF_LEN - offset,
1285 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001286 /* dev_id requested by DFU MMC */
1287 if (part->target == STM32PROG_MMC)
1288 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1289 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001290 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001291 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001292 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001293 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001294 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001295 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001296 if (IS_ENABLED(CONFIG_MMC)) {
1297 ret = 0;
1298 sprintf(dfustr, "mmc");
1299 sprintf(devstr, "%d", part->dev_id);
1300 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001301 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001302 case STM32PROG_NAND:
1303 case STM32PROG_NOR:
1304 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001305 if (IS_ENABLED(CONFIG_MTD)) {
1306 ret = 0;
1307 sprintf(dfustr, "mtd");
1308 get_mtd_by_target(devstr, part->target, part->dev_id);
1309 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001310 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001311 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001312 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001313 sprintf(dfustr, "ram");
1314 sprintf(devstr, "0");
1315 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001316 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001317 break;
1318 }
1319 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001320 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001321 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001322 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001323 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001324 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001325 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1326 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001327
1328 return ret;
1329}
1330
1331static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1332 char *name, int phase, int size)
1333{
1334 int ret = 0;
1335 char devstr[4];
1336 char buf[ALT_BUF_LEN];
1337
1338 sprintf(devstr, "%d", phase);
1339 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1340 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001341 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001342
1343 return ret;
1344}
1345
1346static int dfu_init_entities(struct stm32prog_data *data)
1347{
1348 int ret = 0;
1349 int phase, i, alt_id;
1350 struct stm32prog_part_t *part;
1351 struct dfu_entity *dfu;
1352 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001353 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001354
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001355 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001356
1357 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1358 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1359 otp_size = OTP_SIZE_SMC;
1360 /* check if PTA BSEC is supported */
1361 ret = optee_ta_open(data);
1362 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1363 if (!ret && data->tee)
1364 otp_size = OTP_SIZE_TA;
1365 if (otp_size)
1366 alt_nb++; /* OTP*/
1367 }
1368
Patrick Delaunay7d145402021-05-18 15:12:09 +02001369 if (CONFIG_IS_ENABLED(DM_PMIC))
1370 alt_nb++; /* PMIC NVMEM*/
1371
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001372 if (data->part_nb == 0)
1373 alt_nb++; /* +1 for FlashLayout */
1374 else
1375 for (i = 0; i < data->part_nb; i++) {
1376 if (data->part_array[i].target != STM32PROG_NONE)
1377 alt_nb++;
1378 }
1379
1380 if (dfu_alt_init(alt_nb, &dfu))
1381 return -ENODEV;
1382
1383 puts("DFU alt info setting: ");
1384 if (data->part_nb) {
1385 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001386 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001387 for (phase = 1;
1388 (phase <= PHASE_LAST_USER) &&
1389 (alt_id < alt_nb) && !ret;
1390 phase++) {
1391 /* ordering alt setting by phase id */
1392 part = NULL;
1393 for (i = 0; i < data->part_nb; i++) {
1394 if (phase == data->part_array[i].id) {
1395 part = &data->part_array[i];
1396 break;
1397 }
1398 }
1399 if (!part)
1400 continue;
1401 if (part->target == STM32PROG_NONE)
1402 continue;
1403 part->alt_id = alt_id;
1404 alt_id++;
1405
1406 ret = stm32prog_alt_add(data, dfu, part);
1407 }
1408 } else {
1409 char buf[ALT_BUF_LEN];
1410
1411 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001412 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001413 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001414 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001415 }
1416
1417 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001418 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001419
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001420 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1421 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001422
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001423 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001424 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001425
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001426 if (ret)
1427 stm32prog_err("dfu init failed: %d", ret);
1428 puts("done\n");
1429
1430#ifdef DEBUG
1431 dfu_show_entities();
1432#endif
1433 return ret;
1434}
1435
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001436int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1437 long *size)
1438{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001439 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001440 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001441
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001442 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1443 stm32prog_err("OTP update not supported");
1444
1445 return -EOPNOTSUPP;
1446 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001447
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001448 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001449 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001450 if (!data->otp_part) {
1451 stm32prog_err("OTP write issue %d", -ENOMEM);
1452
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001453 return -ENOMEM;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001454 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001455 }
1456
1457 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001458 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001459
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001460 if (offset + *size > otp_size)
1461 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001462
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001463 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001464
1465 return 0;
1466}
1467
1468int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1469 long *size)
1470{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001471 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001472 int result = 0;
1473
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001474 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001475 stm32prog_err("OTP update not supported");
1476
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001477 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001478 }
1479
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001480 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001481 /* alway read for first packet */
1482 if (!offset) {
1483 if (!data->otp_part)
1484 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001485 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001486
1487 if (!data->otp_part) {
1488 result = -ENOMEM;
1489 goto end_otp_read;
1490 }
1491
1492 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001493 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001494
1495 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001496 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001497 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1498 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1499 data->otp_part, OTP_SIZE_TA);
1500 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001501 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001502 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001503 if (result)
1504 goto end_otp_read;
1505 }
1506
1507 if (!data->otp_part) {
1508 result = -ENOMEM;
1509 goto end_otp_read;
1510 }
1511
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001512 if (offset + *size > otp_size)
1513 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001514 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001515
1516end_otp_read:
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001517 if (result)
1518 stm32prog_err("OTP read issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001519 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001520
1521 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001522}
1523
1524int stm32prog_otp_start(struct stm32prog_data *data)
1525{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001526 int result = 0;
1527 struct arm_smccc_res res;
1528
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001529 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001530 stm32prog_err("OTP update not supported");
1531
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001532 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001533 }
1534
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001535 if (!data->otp_part) {
1536 stm32prog_err("start OTP without data");
1537 return -1;
1538 }
1539
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001540 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001541 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1542 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1543 data->otp_part, OTP_SIZE_TA);
1544 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001545 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001546 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001547
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001548 if (!res.a0) {
1549 switch (res.a1) {
1550 case 0:
1551 result = 0;
1552 break;
1553 case 1:
1554 stm32prog_err("Provisioning");
1555 result = 0;
1556 break;
1557 default:
1558 log_err("%s: OTP incorrect value (err = %ld)\n",
1559 __func__, res.a1);
1560 result = -EINVAL;
1561 break;
1562 }
1563 } else {
1564 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1565 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001566 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001567 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001568 }
1569
1570 free(data->otp_part);
1571 data->otp_part = NULL;
Patrick Delaunay9e1dba32023-04-27 15:36:36 +02001572 if (result)
1573 stm32prog_err("OTP write issue %d", result);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001574 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001575
1576 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001577}
1578
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001579int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1580 long *size)
1581{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001582 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001583
1584 if (!offset)
1585 memset(data->pmic_part, 0, PMIC_SIZE);
1586
1587 if (offset + *size > PMIC_SIZE)
1588 *size = PMIC_SIZE - offset;
1589
1590 memcpy(&data->pmic_part[offset], buffer, *size);
1591
1592 return 0;
1593}
1594
1595int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1596 long *size)
1597{
1598 int result = 0, ret;
1599 struct udevice *dev;
1600
Simon Glassb24d5752023-02-05 15:40:35 -07001601 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001602 stm32prog_err("PMIC update not supported");
1603
1604 return -EOPNOTSUPP;
1605 }
1606
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001607 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001608 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001609 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001610 &dev);
1611 if (ret)
1612 return ret;
1613
1614 /* alway request PMIC for first packet */
1615 if (!offset) {
1616 /* init struct with 0 */
1617 memset(data->pmic_part, 0, PMIC_SIZE);
1618
1619 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001620 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001621 &dev);
1622 if (ret)
1623 return ret;
1624
1625 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1626 if (ret < 0) {
1627 result = ret;
1628 goto end_pmic_read;
1629 }
1630 if (ret != PMIC_SIZE) {
1631 result = -EACCES;
1632 goto end_pmic_read;
1633 }
1634 }
1635
1636 if (offset + *size > PMIC_SIZE)
1637 *size = PMIC_SIZE - offset;
1638
1639 memcpy(buffer, &data->pmic_part[offset], *size);
1640
1641end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001642 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001643 return result;
1644}
1645
1646int stm32prog_pmic_start(struct stm32prog_data *data)
1647{
1648 int ret;
1649 struct udevice *dev;
1650
Simon Glassb24d5752023-02-05 15:40:35 -07001651 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001652 stm32prog_err("PMIC update not supported");
1653
1654 return -EOPNOTSUPP;
1655 }
1656
1657 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001658 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001659 &dev);
1660 if (ret)
1661 return ret;
1662
1663 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1664}
1665
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001666/* copy FSBL on NAND to improve reliability on NAND */
1667static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1668{
1669 int ret, i;
1670 void *fsbl;
1671 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001672 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001673 struct dfu_entity *dfu;
1674 long size, offset;
1675
1676 if (part->target != STM32PROG_NAND &&
1677 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001678 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001679
1680 dfu = dfu_get_entity(part->alt_id);
1681
1682 /* read header */
1683 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001684 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001685 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1686 if (ret)
1687 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001688
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001689 stm32prog_header_check((ulong)&raw_header, &header);
1690 if (header.type != HEADER_STM32IMAGE &&
1691 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001692 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001693
1694 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001695 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001696 size = round_up(size, part->dev->mtd->erasesize);
1697 fsbl = calloc(1, size);
1698 if (!fsbl)
1699 return -ENOMEM;
1700 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001701 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001702 if (ret)
1703 goto error;
1704
1705 dfu_transaction_cleanup(dfu);
1706 offset = 0;
1707 for (i = part->bin_nb - 1; i > 0; i--) {
1708 offset += size;
1709 /* write to the next erase block */
1710 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001711 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1712 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001713 if (ret)
1714 goto error;
1715 }
1716
1717error:
1718 free(fsbl);
1719 return ret;
1720}
1721
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001722static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001723{
1724 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001725#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001726 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1727 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001728 data->phase = PHASE_END;
1729 log_notice("U-Boot script received\n");
1730 return;
1731 }
1732#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001733 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001734 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001735 stm32prog_err("Layout: invalid FlashLayout");
1736 return;
1737 }
1738
1739 if (!data->cur_part)
1740 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001741
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001742 if (data->cur_part->target == STM32PROG_RAM) {
1743 if (data->cur_part->part_type == PART_SYSTEM)
1744 data->uimage = data->cur_part->addr;
1745 if (data->cur_part->part_type == PART_FILESYSTEM)
1746 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001747 if (data->cur_part->part_type == PART_BINARY) {
1748 data->initrd = data->cur_part->addr;
1749 data->initrd_size = offset;
1750 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001751 }
1752
Patrick Delaunay6915b492020-03-18 09:24:52 +01001753 if (CONFIG_IS_ENABLED(MMC) &&
1754 data->cur_part->part_id < 0) {
1755 char cmdbuf[60];
1756
1757 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1758 data->cur_part->dev_id, data->cur_part->dev_id,
1759 -(data->cur_part->part_id));
1760 if (run_command(cmdbuf, 0)) {
1761 stm32prog_err("commands '%s' failed", cmdbuf);
1762 return;
1763 }
1764 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001765
Simon Glass8e05bb12023-02-05 15:40:17 -07001766 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001767 data->cur_part->bin_nb > 1) {
1768 if (stm32prog_copy_fsbl(data->cur_part)) {
1769 stm32prog_err("%s (0x%x): copy of fsbl failed",
1770 data->cur_part->name, data->cur_part->id);
1771 return;
1772 }
1773 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001774}
1775
1776void stm32prog_do_reset(struct stm32prog_data *data)
1777{
1778 if (data->phase == PHASE_RESET) {
1779 data->phase = PHASE_DO_RESET;
1780 puts("Reset requested\n");
1781 }
1782}
1783
1784void stm32prog_next_phase(struct stm32prog_data *data)
1785{
1786 int phase, i;
1787 struct stm32prog_part_t *part;
1788 bool found;
1789
1790 phase = data->phase;
1791 switch (phase) {
1792 case PHASE_RESET:
1793 case PHASE_END:
1794 case PHASE_DO_RESET:
1795 return;
1796 }
1797
1798 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001799 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001800 data->cur_part = NULL;
1801 data->phase = PHASE_END;
1802 found = false;
1803 do {
1804 phase++;
1805 if (phase > PHASE_LAST_USER)
1806 break;
1807 for (i = 0; i < data->part_nb; i++) {
1808 part = &data->part_array[i];
1809 if (part->id == phase) {
1810 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1811 data->cur_part = part;
1812 data->phase = phase;
1813 found = true;
1814 }
1815 break;
1816 }
1817 }
1818 } while (!found);
1819
1820 if (data->phase == PHASE_END)
1821 puts("Phase=END\n");
1822}
1823
Patrick Delaunay291e7222020-03-18 09:24:57 +01001824static int part_delete(struct stm32prog_data *data,
1825 struct stm32prog_part_t *part)
1826{
1827 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001828 unsigned long blks, blks_offset, blks_size;
1829 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001830 char cmdbuf[40];
1831 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001832
1833 printf("Erasing %s ", part->name);
1834 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001835 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001836 if (!IS_ENABLED(CONFIG_MMC)) {
1837 ret = -1;
1838 stm32prog_err("%s (0x%x): erase invalid",
1839 part->name, part->id);
1840 break;
1841 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001842 printf("on mmc %d: ", part->dev->dev_id);
1843 block_dev = mmc_get_blk_desc(part->dev->mmc);
1844 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1845 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1846 /* -1 or -2 : delete boot partition of MMC
1847 * need to switch to associated hwpart 1 or 2
1848 */
1849 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001850 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001851 part->dev->dev_id,
1852 -part->part_id))
1853 return -1;
1854
1855 blks = blk_derase(block_dev, blks_offset, blks_size);
1856
1857 /* return to user partition */
1858 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001859 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001860 part->dev->dev_id, 0);
1861 if (blks != blks_size) {
1862 ret = -1;
1863 stm32prog_err("%s (0x%x): MMC erase failed",
1864 part->name, part->id);
1865 }
1866 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001867 case STM32PROG_NOR:
1868 case STM32PROG_NAND:
1869 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001870 if (!IS_ENABLED(CONFIG_MTD)) {
1871 ret = -1;
1872 stm32prog_err("%s (0x%x): erase invalid",
1873 part->name, part->id);
1874 break;
1875 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001876 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1877 printf("on %s: ", devstr);
1878 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1879 devstr, part->addr, part->size);
1880 if (run_command(cmdbuf, 0)) {
1881 ret = -1;
1882 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1883 part->name, part->id, cmdbuf);
1884 }
1885 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001886 case STM32PROG_RAM:
1887 printf("on ram: ");
1888 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1889 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001890 default:
1891 ret = -1;
1892 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1893 break;
1894 }
1895 if (!ret)
1896 printf("done\n");
1897
1898 return ret;
1899}
1900
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001901static void stm32prog_devices_init(struct stm32prog_data *data)
1902{
1903 int i;
1904 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001905 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001906
1907 ret = treat_partition_list(data);
1908 if (ret)
1909 goto error;
1910
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001911 /* empty flashlayout */
1912 if (!data->dev_nb)
1913 return;
1914
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001915 /* initialize the selected device */
1916 for (i = 0; i < data->dev_nb; i++) {
1917 ret = init_device(data, &data->dev[i]);
1918 if (ret)
1919 goto error;
1920 }
1921
Patrick Delaunay291e7222020-03-18 09:24:57 +01001922 /* delete RAW partition before create partition */
1923 for (i = 0; i < data->part_nb; i++) {
1924 part = &data->part_array[i];
1925
1926 if (part->part_type != RAW_IMAGE)
1927 continue;
1928
1929 if (!IS_SELECT(part) || !IS_DELETE(part))
1930 continue;
1931
1932 ret = part_delete(data, part);
1933 if (ret)
1934 goto error;
1935 }
1936
Patrick Delaunay8040da12020-07-31 16:31:52 +02001937 if (IS_ENABLED(CONFIG_MMC)) {
1938 ret = create_gpt_partitions(data);
1939 if (ret)
1940 goto error;
1941 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001942
Patrick Delaunay291e7222020-03-18 09:24:57 +01001943 /* delete partition GPT or MTD */
1944 for (i = 0; i < data->part_nb; i++) {
1945 part = &data->part_array[i];
1946
1947 if (part->part_type == RAW_IMAGE)
1948 continue;
1949
1950 if (!IS_SELECT(part) || !IS_DELETE(part))
1951 continue;
1952
1953 ret = part_delete(data, part);
1954 if (ret)
1955 goto error;
1956 }
1957
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001958 return;
1959
1960error:
1961 data->part_nb = 0;
1962}
1963
1964int stm32prog_dfu_init(struct stm32prog_data *data)
1965{
1966 /* init device if no error */
1967 if (data->part_nb)
1968 stm32prog_devices_init(data);
1969
1970 if (data->part_nb)
1971 stm32prog_next_phase(data);
1972
1973 /* prepare DFU for device read/write */
1974 dfu_free_entities();
1975 return dfu_init_entities(data);
1976}
1977
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001978int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001979{
1980 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001981 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001982 data->phase = PHASE_FLASHLAYOUT;
1983
1984 return parse_flash_layout(data, addr, size);
1985}
1986
1987void stm32prog_clean(struct stm32prog_data *data)
1988{
1989 /* clean */
1990 dfu_free_entities();
1991 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001992 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001993 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001994
1995 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1996 tee_close_session(data->tee, data->tee_session);
1997 data->tee = NULL;
1998 data->tee_session = 0x0;
1999 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002000}
2001
2002/* DFU callback: used after serial and direct DFU USB access */
2003void dfu_flush_callback(struct dfu_entity *dfu)
2004{
2005 if (!stm32prog_data)
2006 return;
2007
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002008 if (dfu->dev_type == DFU_DEV_VIRT) {
2009 if (dfu->data.virt.dev_num == PHASE_OTP)
2010 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01002011 else if (dfu->data.virt.dev_num == PHASE_PMIC)
2012 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01002013 return;
2014 }
2015
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002016 if (dfu->dev_type == DFU_DEV_RAM) {
2017 if (dfu->alt == 0 &&
2018 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002019 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002020 /* waiting DFU DETACH for reenumeration */
2021 }
2022 }
2023
2024 if (!stm32prog_data->cur_part)
2025 return;
2026
2027 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002028 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002029 stm32prog_next_phase(stm32prog_data);
2030 }
2031}
2032
2033void dfu_initiated_callback(struct dfu_entity *dfu)
2034{
2035 if (!stm32prog_data)
2036 return;
2037
2038 if (!stm32prog_data->cur_part)
2039 return;
2040
2041 /* force the saved offset for the current partition */
2042 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2043 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002044 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002045 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002046 }
2047}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002048
2049void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2050{
2051 struct stm32prog_data *data = stm32prog_data;
2052
2053 if (!stm32prog_data)
2054 return;
2055
2056 if (!stm32prog_data->cur_part)
2057 return;
2058
2059 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2060 stm32prog_err(msg);
2061}