blob: 3b6ca4e7732d3a0f2ccdc7582c495140d219e75b [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>
9#include <malloc.h>
Patrick Delaunay541c7de2020-03-18 09:24:59 +010010#include <misc.h>
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010011#include <mmc.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010012#include <part.h>
Patrick Delaunay1d96b182020-03-18 09:24:58 +010013#include <asm/arch/stm32mp1_smc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010015#include <dm/uclass.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010016#include <jffs2/load_kernel.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010017#include <linux/list.h>
18#include <linux/list_sort.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010019#include <linux/mtd/mtd.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010020#include <linux/sizes.h>
21
22#include "stm32prog.h"
23
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010024/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
25#define GPT_HEADER_SZ 34
26
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010027#define OPT_SELECT BIT(0)
28#define OPT_EMPTY BIT(1)
Patrick Delaunay291e7222020-03-18 09:24:57 +010029#define OPT_DELETE BIT(2)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010030
31#define IS_SELECT(part) ((part)->option & OPT_SELECT)
32#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay291e7222020-03-18 09:24:57 +010033#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010034
35#define ALT_BUF_LEN SZ_1K
36
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010037#define ROOTFS_MMC0_UUID \
38 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
39 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
40
41#define ROOTFS_MMC1_UUID \
42 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
43 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
44
45#define ROOTFS_MMC2_UUID \
46 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
47 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
48
49/* RAW parttion (binary / bootloader) used Linux - reserved UUID */
50#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
51
52/*
53 * unique partition guid (uuid) for partition named "rootfs"
54 * on each MMC instance = SD Card or eMMC
55 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
56 */
57static const efi_guid_t uuid_mmc[3] = {
58 ROOTFS_MMC0_UUID,
59 ROOTFS_MMC1_UUID,
60 ROOTFS_MMC2_UUID
61};
62
Patrick Delaunay526f66c2020-03-18 09:24:50 +010063/* order of column in flash layout file */
64enum stm32prog_col_t {
65 COL_OPTION,
66 COL_ID,
67 COL_NAME,
68 COL_TYPE,
69 COL_IP,
70 COL_OFFSET,
71 COL_NB_STM32
72};
73
Patrick Delaunay19676ef2021-04-02 14:05:17 +020074#define FIP_TOC_HEADER_NAME 0xAA640001
75
76struct fip_toc_header {
77 u32 name;
78 u32 serial_number;
79 u64 flags;
80};
81
82DECLARE_GLOBAL_DATA_PTR;
83
Patrick Delaunay6ab74962020-03-18 09:24:54 +010084/* partition handling routines : CONFIG_CMD_MTDPARTS */
85int mtdparts_init(void);
86int find_dev_and_part(const char *id, struct mtd_device **dev,
87 u8 *part_num, struct part_info **part);
88
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010089char *stm32prog_get_error(struct stm32prog_data *data)
90{
91 static const char error_msg[] = "Unspecified";
92
93 if (strlen(data->error) == 0)
94 strcpy(data->error, error_msg);
95
96 return data->error;
97}
98
Patrick Delaunay19676ef2021-04-02 14:05:17 +020099static bool stm32prog_is_fip_header(struct fip_toc_header *header)
100{
101 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
102}
103
104void stm32prog_header_check(struct raw_header_s *raw_header,
105 struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100106{
107 unsigned int i;
108
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200109 if (!raw_header || !header) {
110 log_debug("%s:no header data\n", __func__);
111 return;
112 }
113
114 header->type = HEADER_NONE;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100115 header->image_checksum = 0x0;
116 header->image_length = 0x0;
117
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200118 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
119 header->type = HEADER_FIP;
120 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100121 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200122
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100123 if (raw_header->magic_number !=
124 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100125 log_debug("%s:invalid magic number : 0x%x\n",
126 __func__, raw_header->magic_number);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200127 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100128 }
129 /* only header v1.0 supported */
130 if (raw_header->header_version != 0x00010000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100131 log_debug("%s:invalid header version : 0x%x\n",
132 __func__, raw_header->header_version);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200133 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100134 }
135 if (raw_header->reserved1 != 0x0 || raw_header->reserved2) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100136 log_debug("%s:invalid reserved field\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200137 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100138 }
139 for (i = 0; i < (sizeof(raw_header->padding) / 4); i++) {
140 if (raw_header->padding[i] != 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100141 log_debug("%s:invalid padding field\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200142 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100143 }
144 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200145 header->type = HEADER_STM32IMAGE;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100146 header->image_checksum = le32_to_cpu(raw_header->image_checksum);
147 header->image_length = le32_to_cpu(raw_header->image_length);
148
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200149 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100150}
151
152static u32 stm32prog_header_checksum(u32 addr, struct image_header_s *header)
153{
154 u32 i, checksum;
155 u8 *payload;
156
157 /* compute checksum on payload */
158 payload = (u8 *)addr;
159 checksum = 0;
160 for (i = header->image_length; i > 0; i--)
161 checksum += *(payload++);
162
163 return checksum;
164}
165
166/* FLASHLAYOUT PARSING *****************************************/
167static int parse_option(struct stm32prog_data *data,
168 int i, char *p, struct stm32prog_part_t *part)
169{
170 int result = 0;
171 char *c = p;
172
173 part->option = 0;
174 if (!strcmp(p, "-"))
175 return 0;
176
177 while (*c) {
178 switch (*c) {
179 case 'P':
180 part->option |= OPT_SELECT;
181 break;
182 case 'E':
183 part->option |= OPT_EMPTY;
184 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100185 case 'D':
186 part->option |= OPT_DELETE;
187 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100188 default:
189 result = -EINVAL;
190 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
191 i, *c, p);
192 return -EINVAL;
193 }
194 c++;
195 }
196 if (!(part->option & OPT_SELECT)) {
197 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
198 return -EINVAL;
199 }
200
201 return result;
202}
203
204static int parse_id(struct stm32prog_data *data,
205 int i, char *p, struct stm32prog_part_t *part)
206{
207 int result = 0;
208 unsigned long value;
209
210 result = strict_strtoul(p, 0, &value);
211 part->id = value;
212 if (result || value > PHASE_LAST_USER) {
213 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
214 result = -EINVAL;
215 }
216
217 return result;
218}
219
220static int parse_name(struct stm32prog_data *data,
221 int i, char *p, struct stm32prog_part_t *part)
222{
223 int result = 0;
224
225 if (strlen(p) < sizeof(part->name)) {
226 strcpy(part->name, p);
227 } else {
228 stm32prog_err("Layout line %d: partition name too long [%d]: %s",
229 i, strlen(p), p);
230 result = -EINVAL;
231 }
232
233 return result;
234}
235
236static int parse_type(struct stm32prog_data *data,
237 int i, char *p, struct stm32prog_part_t *part)
238{
239 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100240 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100241
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100242 part->bin_nb = 0;
243 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100244 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100245
246 /* search for Binary(X) case */
247 len = strlen(p);
248 part->bin_nb = 1;
249 if (len > 6) {
250 if (len < 8 ||
251 (p[6] != '(') ||
252 (p[len - 1] != ')'))
253 result = -EINVAL;
254 else
255 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600256 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100257 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100258 } else if (!strcmp(p, "System")) {
259 part->part_type = PART_SYSTEM;
260 } else if (!strcmp(p, "FileSystem")) {
261 part->part_type = PART_FILESYSTEM;
262 } else if (!strcmp(p, "RawImage")) {
263 part->part_type = RAW_IMAGE;
264 } else {
265 result = -EINVAL;
266 }
267 if (result)
268 stm32prog_err("Layout line %d: type parsing error : '%s'",
269 i, p);
270
271 return result;
272}
273
274static int parse_ip(struct stm32prog_data *data,
275 int i, char *p, struct stm32prog_part_t *part)
276{
277 int result = 0;
278 unsigned int len = 0;
279
280 part->dev_id = 0;
281 if (!strcmp(p, "none")) {
282 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100283 } else if (!strncmp(p, "mmc", 3)) {
284 part->target = STM32PROG_MMC;
285 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100286 } else if (!strncmp(p, "nor", 3)) {
287 part->target = STM32PROG_NOR;
288 len = 3;
289 } else if (!strncmp(p, "nand", 4)) {
290 part->target = STM32PROG_NAND;
291 len = 4;
292 } else if (!strncmp(p, "spi-nand", 8)) {
293 part->target = STM32PROG_SPI_NAND;
294 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100295 } else if (!strncmp(p, "ram", 3)) {
296 part->target = STM32PROG_RAM;
297 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100298 } else {
299 result = -EINVAL;
300 }
301 if (len) {
302 /* only one digit allowed for device id */
303 if (strlen(p) != len + 1) {
304 result = -EINVAL;
305 } else {
306 part->dev_id = p[len] - '0';
307 if (part->dev_id > 9)
308 result = -EINVAL;
309 }
310 }
311 if (result)
312 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
313
314 return result;
315}
316
317static int parse_offset(struct stm32prog_data *data,
318 int i, char *p, struct stm32prog_part_t *part)
319{
320 int result = 0;
321 char *tail;
322
323 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100324 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100325 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100326 /* eMMC boot parttion */
327 if (!strncmp(p, "boot", 4)) {
328 if (strlen(p) != 5) {
329 result = -EINVAL;
330 } else {
331 if (p[4] == '1')
332 part->part_id = -1;
333 else if (p[4] == '2')
334 part->part_id = -2;
335 else
336 result = -EINVAL;
337 }
338 if (result)
339 stm32prog_err("Layout line %d: invalid part '%s'",
340 i, p);
341 } else {
342 part->addr = simple_strtoull(p, &tail, 0);
343 if (tail == p || *tail != '\0') {
344 stm32prog_err("Layout line %d: invalid offset '%s'",
345 i, p);
346 result = -EINVAL;
347 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100348 }
349
350 return result;
351}
352
353static
354int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
355 struct stm32prog_part_t *part) = {
356 [COL_OPTION] = parse_option,
357 [COL_ID] = parse_id,
358 [COL_NAME] = parse_name,
359 [COL_TYPE] = parse_type,
360 [COL_IP] = parse_ip,
361 [COL_OFFSET] = parse_offset,
362};
363
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100364static int parse_flash_layout(struct stm32prog_data *data,
365 ulong addr,
366 ulong size)
367{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100368 int column = 0, part_nb = 0, ret;
369 bool end_of_line, eof;
370 char *p, *start, *last, *col;
371 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200372 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100373 int part_list_size;
374 int i;
375
376 data->part_nb = 0;
377
378 /* check if STM32image is detected */
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200379 stm32prog_header_check((struct raw_header_s *)addr, &header);
380 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100381 u32 checksum;
382
383 addr = addr + BL_HEADER_SIZE;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200384 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100385
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200386 checksum = stm32prog_header_checksum(addr, &header);
387 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100388 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200389 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100390 return -EIO;
391 }
392 }
393 if (!size)
394 return -EINVAL;
395
396 start = (char *)addr;
397 last = start + size;
398
399 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100400 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100401
402 /* calculate expected number of partitions */
403 part_list_size = 1;
404 p = start;
405 while (*p && (p < last)) {
406 if (*p++ == '\n') {
407 part_list_size++;
408 if (p < last && *p == '#')
409 part_list_size--;
410 }
411 }
412 if (part_list_size > PHASE_LAST_USER) {
413 stm32prog_err("Layout: too many partition (%d)",
414 part_list_size);
415 return -1;
416 }
417 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
418 if (!part) {
419 stm32prog_err("Layout: alloc failed");
420 return -ENOMEM;
421 }
422 data->part_array = part;
423
424 /* main parsing loop */
425 i = 1;
426 eof = false;
427 p = start;
428 col = start; /* 1st column */
429 end_of_line = false;
430 while (!eof) {
431 switch (*p) {
432 /* CR is ignored and replaced by NULL character */
433 case '\r':
434 *p = '\0';
435 p++;
436 continue;
437 case '\0':
438 end_of_line = true;
439 eof = true;
440 break;
441 case '\n':
442 end_of_line = true;
443 break;
444 case '\t':
445 break;
446 case '#':
447 /* comment line is skipped */
448 if (column == 0 && p == col) {
449 while ((p < last) && *p)
450 if (*p++ == '\n')
451 break;
452 col = p;
453 i++;
454 if (p >= last || !*p) {
455 eof = true;
456 end_of_line = true;
457 }
458 continue;
459 }
460 /* fall through */
461 /* by default continue with the next character */
462 default:
463 p++;
464 continue;
465 }
466
467 /* replace by \0: allow string parsing for each column */
468 *p = '\0';
469 p++;
470 if (p >= last) {
471 eof = true;
472 end_of_line = true;
473 }
474
475 /* skip empty line and multiple TAB in tsv file */
476 if (strlen(col) == 0) {
477 col = p;
478 /* skip empty line */
479 if (column == 0 && end_of_line) {
480 end_of_line = false;
481 i++;
482 }
483 continue;
484 }
485
486 if (column < COL_NB_STM32) {
487 ret = parse[column](data, i, col, part);
488 if (ret)
489 return ret;
490 }
491
492 /* save the beginning of the next column */
493 column++;
494 col = p;
495
496 if (!end_of_line)
497 continue;
498
499 /* end of the line detected */
500 end_of_line = false;
501
502 if (column < COL_NB_STM32) {
503 stm32prog_err("Layout line %d: no enought column", i);
504 return -EINVAL;
505 }
506 column = 0;
507 part_nb++;
508 part++;
509 i++;
510 if (part_nb >= part_list_size) {
511 part = NULL;
512 if (!eof) {
513 stm32prog_err("Layout: no enought memory for %d part",
514 part_nb);
515 return -EINVAL;
516 }
517 }
518 }
519 data->part_nb = part_nb;
520 if (data->part_nb == 0) {
521 stm32prog_err("Layout: no partition found");
522 return -ENODEV;
523 }
524
525 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100526}
527
528static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
529{
530 struct stm32prog_part_t *parta, *partb;
531
532 parta = container_of(a, struct stm32prog_part_t, list);
533 partb = container_of(b, struct stm32prog_part_t, list);
534
Patrick Delaunay6915b492020-03-18 09:24:52 +0100535 if (parta->part_id != partb->part_id)
536 return parta->part_id - partb->part_id;
537 else
538 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100539}
540
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100541static void get_mtd_by_target(char *string, enum stm32prog_target target,
542 int dev_id)
543{
544 const char *dev_str;
545
546 switch (target) {
547 case STM32PROG_NOR:
548 dev_str = "nor";
549 break;
550 case STM32PROG_NAND:
551 dev_str = "nand";
552 break;
553 case STM32PROG_SPI_NAND:
554 dev_str = "spi-nand";
555 break;
556 default:
557 dev_str = "invalid";
558 break;
559 }
560 sprintf(string, "%s%d", dev_str, dev_id);
561}
562
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100563static int init_device(struct stm32prog_data *data,
564 struct stm32prog_dev_t *dev)
565{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100566 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100567 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100568 struct mtd_info *mtd = NULL;
569 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100570 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100571 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100572 u64 first_addr = 0, last_addr = 0;
573 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100574 u64 part_addr, part_size;
575 bool part_found;
576 const char *part_name;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100577
578 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100579 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200580 if (!IS_ENABLED(CONFIG_MMC)) {
581 stm32prog_err("unknown device type = %d", dev->target);
582 return -ENODEV;
583 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100584 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200585 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100586 stm32prog_err("mmc device %d not found", dev->dev_id);
587 return -ENODEV;
588 }
589 block_dev = mmc_get_blk_desc(mmc);
590 if (!block_dev) {
591 stm32prog_err("mmc device %d not probed", dev->dev_id);
592 return -ENODEV;
593 }
594 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
595 dev->mmc = mmc;
596
597 /* reserve a full erase group for each GTP headers */
598 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
599 first_addr = dev->erase_size;
600 last_addr = (u64)(block_dev->lba -
601 mmc->erase_grp_size) *
602 block_dev->blksz;
603 } else {
604 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
605 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
606 block_dev->blksz;
607 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100608 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
609 block_dev->lba, block_dev->blksz);
610 log_debug(" available address = 0x%llx..0x%llx\n",
611 first_addr, last_addr);
612 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100613 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100614 case STM32PROG_NOR:
615 case STM32PROG_NAND:
616 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200617 if (!IS_ENABLED(CONFIG_MTD)) {
618 stm32prog_err("unknown device type = %d", dev->target);
619 return -ENODEV;
620 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100621 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100622 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100623
624 mtdparts_init();
625 mtd = get_mtd_device_nm(mtd_id);
626 if (IS_ERR(mtd)) {
627 stm32prog_err("MTD device %s not found", mtd_id);
628 return -ENODEV;
629 }
630 first_addr = 0;
631 last_addr = mtd->size;
632 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100633 log_debug("MTD device %s: size=%lld erasesize=%d\n",
634 mtd_id, mtd->size, mtd->erasesize);
635 log_debug(" available address = 0x%llx..0x%llx\n",
636 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100637 dev->mtd = mtd;
638 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100639 case STM32PROG_RAM:
640 first_addr = gd->bd->bi_dram[0].start;
641 last_addr = first_addr + gd->bd->bi_dram[0].size;
642 dev->erase_size = 1;
643 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100644 default:
645 stm32prog_err("unknown device type = %d", dev->target);
646 return -ENODEV;
647 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100648 log_debug(" erase size = 0x%x\n", dev->erase_size);
649 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100650
651 /* order partition list in offset order */
652 list_sort(NULL, &dev->part_list, &part_cmp);
653 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100654 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100655 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100656 if (part->bin_nb > 1) {
657 if ((dev->target != STM32PROG_NAND &&
658 dev->target != STM32PROG_SPI_NAND) ||
659 part->id >= PHASE_FIRST_USER ||
660 strncmp(part->name, "fsbl", 4)) {
661 stm32prog_err("%s (0x%x): multiple binary %d not supported",
662 part->name, part->id,
663 part->bin_nb);
664 return -EINVAL;
665 }
666 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100667 if (part->part_type == RAW_IMAGE) {
668 part->part_id = 0x0;
669 part->addr = 0x0;
670 if (block_dev)
671 part->size = block_dev->lba * block_dev->blksz;
672 else
673 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100674 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
675 part->option, part->id, part->name,
676 part->part_type, part->bin_nb, part->target,
677 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100678 continue;
679 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100680 if (part->part_id < 0) { /* boot hw partition for eMMC */
681 if (mmc) {
682 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100683 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100684 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100685 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100686 part->part_id);
687 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100688 }
689 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100690 part->part_id = part_id++;
691
692 /* last partition : size to the end of the device */
693 if (part->list.next != &dev->part_list) {
694 next_part =
695 container_of(part->list.next,
696 struct stm32prog_part_t,
697 list);
698 if (part->addr < next_part->addr) {
699 part->size = next_part->addr -
700 part->addr;
701 } else {
702 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
703 part->name, part->id,
704 part->addr,
705 next_part->name,
706 next_part->id,
707 next_part->addr);
708 return -EINVAL;
709 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100710 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100711 if (part->addr <= last_addr) {
712 part->size = last_addr - part->addr;
713 } else {
714 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
715 part->name, part->id,
716 part->addr, last_addr);
717 return -EINVAL;
718 }
719 }
720 if (part->addr < first_addr) {
721 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100722 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100723 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100724 return -EINVAL;
725 }
726 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100727 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
728 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
729 part->name, part->id, part->addr,
730 part->dev->erase_size);
731 return -EINVAL;
732 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100733 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
734 part->part_id, part->option, part->id, part->name,
735 part->part_type, part->bin_nb, part->target,
736 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100737
738 part_addr = 0;
739 part_size = 0;
740 part_found = false;
741
742 /* check coherency with existing partition */
743 if (block_dev) {
744 /*
745 * block devices with GPT: check user partition size
746 * only for partial update, the GPT partions are be
747 * created for full update
748 */
749 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100750 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100751 continue;
752 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600753 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100754
755 ret = part_get_info(block_dev, part->part_id,
756 &partinfo);
757
758 if (ret) {
759 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
760 part->name, part->id,
761 part_id, part->dev_id);
762 return -ENODEV;
763 }
764 part_addr = (u64)partinfo.start * partinfo.blksz;
765 part_size = (u64)partinfo.size * partinfo.blksz;
766 part_name = (char *)partinfo.name;
767 part_found = true;
768 }
769
Patrick Delaunay8040da12020-07-31 16:31:52 +0200770 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100771 char mtd_part_id[32];
772 struct part_info *mtd_part;
773 struct mtd_device *mtd_dev;
774 u8 part_num;
775
776 sprintf(mtd_part_id, "%s,%d", mtd_id,
777 part->part_id - 1);
778 ret = find_dev_and_part(mtd_part_id, &mtd_dev,
779 &part_num, &mtd_part);
780 if (ret != 0) {
781 stm32prog_err("%s (0x%x): Invalid MTD partition %s",
782 part->name, part->id,
783 mtd_part_id);
784 return -ENODEV;
785 }
786 part_addr = mtd_part->offset;
787 part_size = mtd_part->size;
788 part_name = mtd_part->name;
789 part_found = true;
790 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200791
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200792 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100793 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100794 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100795 continue;
796 }
797
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100798 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100799
800 if (part->addr != part_addr) {
801 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
802 part->name, part->id, part->part_id,
803 part_name, part->addr, part_addr);
804 return -ENODEV;
805 }
806 if (part->size != part_size) {
807 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
808 part->name, part->id, part->part_id,
809 part_name, part->addr, part->size,
810 part_size);
811 return -ENODEV;
812 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100813 }
814 return 0;
815}
816
817static int treat_partition_list(struct stm32prog_data *data)
818{
819 int i, j;
820 struct stm32prog_part_t *part;
821
822 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
823 data->dev[j].target = STM32PROG_NONE;
824 INIT_LIST_HEAD(&data->dev[j].part_list);
825 }
826
Patrick Delaunay1008a502021-07-26 11:21:38 +0200827#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunayc5112242020-03-18 09:24:55 +0100828 data->tee_detected = false;
Patrick Delaunay1008a502021-07-26 11:21:38 +0200829#endif
Patrick Delaunayc5112242020-03-18 09:24:55 +0100830 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100831 for (i = 0; i < data->part_nb; i++) {
832 part = &data->part_array[i];
833 part->alt_id = -1;
834
835 /* skip partition with IP="none" */
836 if (part->target == STM32PROG_NONE) {
837 if (IS_SELECT(part)) {
838 stm32prog_err("Layout: selected none phase = 0x%x",
839 part->id);
840 return -EINVAL;
841 }
842 continue;
843 }
844
845 if (part->id == PHASE_FLASHLAYOUT ||
846 part->id > PHASE_LAST_USER) {
847 stm32prog_err("Layout: invalid phase = 0x%x",
848 part->id);
849 return -EINVAL;
850 }
851 for (j = i + 1; j < data->part_nb; j++) {
852 if (part->id == data->part_array[j].id) {
853 stm32prog_err("Layout: duplicated phase 0x%x at line %d and %d",
854 part->id, i, j);
855 return -EINVAL;
856 }
857 }
858 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
859 if (data->dev[j].target == STM32PROG_NONE) {
860 /* new device found */
861 data->dev[j].target = part->target;
862 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100863 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100864 data->dev_nb++;
865 break;
866 } else if ((part->target == data->dev[j].target) &&
867 (part->dev_id == data->dev[j].dev_id)) {
868 break;
869 }
870 }
871 if (j == STM32PROG_MAX_DEV) {
872 stm32prog_err("Layout: too many device");
873 return -EINVAL;
874 }
Patrick Delaunayc5112242020-03-18 09:24:55 +0100875 switch (part->target) {
876 case STM32PROG_NOR:
877 if (!data->fsbl_nor_detected &&
878 !strncmp(part->name, "fsbl", 4))
879 data->fsbl_nor_detected = true;
880 /* fallthrough */
881 case STM32PROG_NAND:
882 case STM32PROG_SPI_NAND:
Patrick Delaunay1008a502021-07-26 11:21:38 +0200883#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunayc5112242020-03-18 09:24:55 +0100884 if (!data->tee_detected &&
885 !strncmp(part->name, "tee", 3))
886 data->tee_detected = true;
887 break;
Patrick Delaunay1008a502021-07-26 11:21:38 +0200888#endif
Patrick Delaunayc5112242020-03-18 09:24:55 +0100889 default:
890 break;
891 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100892 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100893 if (!IS_SELECT(part))
894 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100895 list_add_tail(&part->list, &data->dev[j].part_list);
896 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100897
898 return 0;
899}
900
Patrick Delaunay8040da12020-07-31 16:31:52 +0200901static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100902{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100903 int offset = 0;
904 const int buflen = SZ_8K;
905 char *buf;
906 char uuid[UUID_STR_LEN + 1];
907 unsigned char *uuid_bin;
908 unsigned int mmc_id;
909 int i;
910 bool rootfs_found;
911 struct stm32prog_part_t *part;
912
913 buf = malloc(buflen);
914 if (!buf)
915 return -ENOMEM;
916
917 puts("partitions : ");
918 /* initialize the selected device */
919 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100920 /* create gpt partition support only for full update on MMC */
921 if (data->dev[i].target != STM32PROG_MMC ||
922 !data->dev[i].full_update)
923 continue;
924
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100925 offset = 0;
926 rootfs_found = false;
927 memset(buf, 0, buflen);
928
929 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100930 /* skip eMMC boot partitions */
931 if (part->part_id < 0)
932 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100933 /* skip Raw Image */
934 if (part->part_type == RAW_IMAGE)
935 continue;
936
937 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100938 log_debug("\n%s: buffer too small, %s skippped",
939 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100940 continue;
941 }
942
943 if (!offset)
944 offset += sprintf(buf, "gpt write mmc %d \"",
945 data->dev[i].dev_id);
946
947 offset += snprintf(buf + offset, buflen - offset,
948 "name=%s,start=0x%llx,size=0x%llx",
949 part->name,
950 part->addr,
951 part->size);
952
953 if (part->part_type == PART_BINARY)
954 offset += snprintf(buf + offset,
955 buflen - offset,
956 ",type="
957 LINUX_RESERVED_UUID);
958 else
959 offset += snprintf(buf + offset,
960 buflen - offset,
961 ",type=linux");
962
963 if (part->part_type == PART_SYSTEM)
964 offset += snprintf(buf + offset,
965 buflen - offset,
966 ",bootable");
967
968 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
969 mmc_id = part->dev_id;
970 rootfs_found = true;
971 if (mmc_id < ARRAY_SIZE(uuid_mmc)) {
972 uuid_bin =
973 (unsigned char *)uuid_mmc[mmc_id].b;
974 uuid_bin_to_str(uuid_bin, uuid,
975 UUID_STR_FORMAT_GUID);
976 offset += snprintf(buf + offset,
977 buflen - offset,
978 ",uuid=%s", uuid);
979 }
980 }
981
982 offset += snprintf(buf + offset, buflen - offset, ";");
983 }
984
985 if (offset) {
986 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100987 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100988 if (run_command(buf, 0)) {
989 stm32prog_err("GPT partitionning fail: %s",
990 buf);
991 free(buf);
992
993 return -1;
994 }
995 }
996
997 if (data->dev[i].mmc)
998 part_init(mmc_get_blk_desc(data->dev[i].mmc));
999
1000#ifdef DEBUG
1001 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001002 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001003 if (run_command(buf, 0))
1004 printf("fail !\n");
1005 else
1006 printf("OK\n");
1007
1008 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1009 run_command(buf, 0);
1010#endif
1011 }
1012 puts("done\n");
1013
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001014#ifdef DEBUG
1015 run_command("mtd list", 0);
1016#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001017 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001018
1019 return 0;
1020}
1021
1022static int stm32prog_alt_add(struct stm32prog_data *data,
1023 struct dfu_entity *dfu,
1024 struct stm32prog_part_t *part)
1025{
1026 int ret = 0;
1027 int offset = 0;
1028 char devstr[10];
1029 char dfustr[10];
1030 char buf[ALT_BUF_LEN];
1031 u32 size;
1032 char multiplier, type;
1033
1034 /* max 3 digit for sector size */
1035 if (part->size > SZ_1M) {
1036 size = (u32)(part->size / SZ_1M);
1037 multiplier = 'M';
1038 } else if (part->size > SZ_1K) {
1039 size = (u32)(part->size / SZ_1K);
1040 multiplier = 'K';
1041 } else {
1042 size = (u32)part->size;
1043 multiplier = 'B';
1044 }
1045 if (IS_SELECT(part) && !IS_EMPTY(part))
1046 type = 'e'; /*Readable and Writeable*/
1047 else
1048 type = 'a';/*Readable*/
1049
1050 memset(buf, 0, sizeof(buf));
1051 offset = snprintf(buf, ALT_BUF_LEN - offset,
1052 "@%s/0x%02x/1*%d%c%c ",
1053 part->name, part->id,
1054 size, multiplier, type);
1055
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001056 if (part->target == STM32PROG_RAM) {
1057 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1058 "ram 0x%llx 0x%llx",
1059 part->addr, part->size);
1060 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001061 u64 dfu_size;
1062
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001063 if (part->dev->target == STM32PROG_MMC)
1064 dfu_size = part->size / part->dev->mmc->read_bl_len;
1065 else
1066 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001067 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1068 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001069 } else if (part->part_id < 0) {
1070 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1071
1072 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1073 "raw 0x%llx 0x%llx",
1074 part->addr, nb_blk);
1075 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1076 " mmcpart %d;", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001077 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001078 if (part->part_type == PART_SYSTEM &&
1079 (part->target == STM32PROG_NAND ||
1080 part->target == STM32PROG_NOR ||
1081 part->target == STM32PROG_SPI_NAND))
1082 offset += snprintf(buf + offset,
1083 ALT_BUF_LEN - offset,
1084 "partubi");
1085 else
1086 offset += snprintf(buf + offset,
1087 ALT_BUF_LEN - offset,
1088 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001089 /* dev_id requested by DFU MMC */
1090 if (part->target == STM32PROG_MMC)
1091 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1092 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001093 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1094 " %d;", part->part_id);
1095 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001096 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001097 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001098 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001099 if (IS_ENABLED(CONFIG_MMC)) {
1100 ret = 0;
1101 sprintf(dfustr, "mmc");
1102 sprintf(devstr, "%d", part->dev_id);
1103 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001104 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001105 case STM32PROG_NAND:
1106 case STM32PROG_NOR:
1107 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001108 if (IS_ENABLED(CONFIG_MTD)) {
1109 ret = 0;
1110 sprintf(dfustr, "mtd");
1111 get_mtd_by_target(devstr, part->target, part->dev_id);
1112 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001113 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001114 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001115 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001116 sprintf(dfustr, "ram");
1117 sprintf(devstr, "0");
1118 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001119 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001120 break;
1121 }
1122 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001123 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001124 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001125 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001126 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001127 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001128 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1129 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001130
1131 return ret;
1132}
1133
1134static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1135 char *name, int phase, int size)
1136{
1137 int ret = 0;
1138 char devstr[4];
1139 char buf[ALT_BUF_LEN];
1140
1141 sprintf(devstr, "%d", phase);
1142 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1143 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001144 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001145
1146 return ret;
1147}
1148
1149static int dfu_init_entities(struct stm32prog_data *data)
1150{
1151 int ret = 0;
1152 int phase, i, alt_id;
1153 struct stm32prog_part_t *part;
1154 struct dfu_entity *dfu;
1155 int alt_nb;
1156
Patrick Delaunay7d145402021-05-18 15:12:09 +02001157 alt_nb = 2; /* number of virtual = CMD, OTP*/
1158 if (CONFIG_IS_ENABLED(DM_PMIC))
1159 alt_nb++; /* PMIC NVMEM*/
1160
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001161 if (data->part_nb == 0)
1162 alt_nb++; /* +1 for FlashLayout */
1163 else
1164 for (i = 0; i < data->part_nb; i++) {
1165 if (data->part_array[i].target != STM32PROG_NONE)
1166 alt_nb++;
1167 }
1168
1169 if (dfu_alt_init(alt_nb, &dfu))
1170 return -ENODEV;
1171
1172 puts("DFU alt info setting: ");
1173 if (data->part_nb) {
1174 alt_id = 0;
1175 for (phase = 1;
1176 (phase <= PHASE_LAST_USER) &&
1177 (alt_id < alt_nb) && !ret;
1178 phase++) {
1179 /* ordering alt setting by phase id */
1180 part = NULL;
1181 for (i = 0; i < data->part_nb; i++) {
1182 if (phase == data->part_array[i].id) {
1183 part = &data->part_array[i];
1184 break;
1185 }
1186 }
1187 if (!part)
1188 continue;
1189 if (part->target == STM32PROG_NONE)
1190 continue;
1191 part->alt_id = alt_id;
1192 alt_id++;
1193
1194 ret = stm32prog_alt_add(data, dfu, part);
1195 }
1196 } else {
1197 char buf[ALT_BUF_LEN];
1198
1199 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
1200 PHASE_FLASHLAYOUT, STM32_DDR_BASE);
1201 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001202 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001203 }
1204
1205 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001206 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001207
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001208 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001209 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, OTP_SIZE);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001210
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001211 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001212 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001213
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001214 if (ret)
1215 stm32prog_err("dfu init failed: %d", ret);
1216 puts("done\n");
1217
1218#ifdef DEBUG
1219 dfu_show_entities();
1220#endif
1221 return ret;
1222}
1223
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001224int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1225 long *size)
1226{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001227 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001228
1229 if (!data->otp_part) {
1230 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, OTP_SIZE);
1231 if (!data->otp_part)
1232 return -ENOMEM;
1233 }
1234
1235 if (!offset)
1236 memset(data->otp_part, 0, OTP_SIZE);
1237
1238 if (offset + *size > OTP_SIZE)
1239 *size = OTP_SIZE - offset;
1240
1241 memcpy((void *)((u32)data->otp_part + offset), buffer, *size);
1242
1243 return 0;
1244}
1245
1246int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1247 long *size)
1248{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001249 int result = 0;
1250
Patrick Delaunay8040da12020-07-31 16:31:52 +02001251 if (!IS_ENABLED(CONFIG_ARM_SMCCC)) {
1252 stm32prog_err("OTP update not supported");
1253
1254 return -1;
1255 }
1256
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001257 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001258 /* alway read for first packet */
1259 if (!offset) {
1260 if (!data->otp_part)
1261 data->otp_part =
1262 memalign(CONFIG_SYS_CACHELINE_SIZE, OTP_SIZE);
1263
1264 if (!data->otp_part) {
1265 result = -ENOMEM;
1266 goto end_otp_read;
1267 }
1268
1269 /* init struct with 0 */
1270 memset(data->otp_part, 0, OTP_SIZE);
1271
1272 /* call the service */
1273 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
1274 (u32)data->otp_part, 0);
1275 if (result)
1276 goto end_otp_read;
1277 }
1278
1279 if (!data->otp_part) {
1280 result = -ENOMEM;
1281 goto end_otp_read;
1282 }
1283
1284 if (offset + *size > OTP_SIZE)
1285 *size = OTP_SIZE - offset;
1286 memcpy(buffer, (void *)((u32)data->otp_part + offset), *size);
1287
1288end_otp_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001289 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001290
1291 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001292}
1293
1294int stm32prog_otp_start(struct stm32prog_data *data)
1295{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001296 int result = 0;
1297 struct arm_smccc_res res;
1298
Patrick Delaunay8040da12020-07-31 16:31:52 +02001299 if (!IS_ENABLED(CONFIG_ARM_SMCCC)) {
1300 stm32prog_err("OTP update not supported");
1301
1302 return -1;
1303 }
1304
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001305 if (!data->otp_part) {
1306 stm32prog_err("start OTP without data");
1307 return -1;
1308 }
1309
1310 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
1311 (u32)data->otp_part, 0, 0, 0, 0, 0, &res);
1312
1313 if (!res.a0) {
1314 switch (res.a1) {
1315 case 0:
1316 result = 0;
1317 break;
1318 case 1:
1319 stm32prog_err("Provisioning");
1320 result = 0;
1321 break;
1322 default:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001323 log_err("%s: OTP incorrect value (err = %ld)\n",
1324 __func__, res.a1);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001325 result = -EINVAL;
1326 break;
1327 }
1328 } else {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001329 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1330 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001331 result = -EINVAL;
1332 }
1333
1334 free(data->otp_part);
1335 data->otp_part = NULL;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001336 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001337
1338 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001339}
1340
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001341int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1342 long *size)
1343{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001344 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001345
1346 if (!offset)
1347 memset(data->pmic_part, 0, PMIC_SIZE);
1348
1349 if (offset + *size > PMIC_SIZE)
1350 *size = PMIC_SIZE - offset;
1351
1352 memcpy(&data->pmic_part[offset], buffer, *size);
1353
1354 return 0;
1355}
1356
1357int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1358 long *size)
1359{
1360 int result = 0, ret;
1361 struct udevice *dev;
1362
1363 if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) {
1364 stm32prog_err("PMIC update not supported");
1365
1366 return -EOPNOTSUPP;
1367 }
1368
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001369 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001370 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001371 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001372 &dev);
1373 if (ret)
1374 return ret;
1375
1376 /* alway request PMIC for first packet */
1377 if (!offset) {
1378 /* init struct with 0 */
1379 memset(data->pmic_part, 0, PMIC_SIZE);
1380
1381 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001382 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001383 &dev);
1384 if (ret)
1385 return ret;
1386
1387 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1388 if (ret < 0) {
1389 result = ret;
1390 goto end_pmic_read;
1391 }
1392 if (ret != PMIC_SIZE) {
1393 result = -EACCES;
1394 goto end_pmic_read;
1395 }
1396 }
1397
1398 if (offset + *size > PMIC_SIZE)
1399 *size = PMIC_SIZE - offset;
1400
1401 memcpy(buffer, &data->pmic_part[offset], *size);
1402
1403end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001404 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001405 return result;
1406}
1407
1408int stm32prog_pmic_start(struct stm32prog_data *data)
1409{
1410 int ret;
1411 struct udevice *dev;
1412
1413 if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) {
1414 stm32prog_err("PMIC update not supported");
1415
1416 return -EOPNOTSUPP;
1417 }
1418
1419 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001420 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001421 &dev);
1422 if (ret)
1423 return ret;
1424
1425 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1426}
1427
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001428/* copy FSBL on NAND to improve reliability on NAND */
1429static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1430{
1431 int ret, i;
1432 void *fsbl;
1433 struct image_header_s header;
1434 struct raw_header_s raw_header;
1435 struct dfu_entity *dfu;
1436 long size, offset;
1437
1438 if (part->target != STM32PROG_NAND &&
1439 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001440 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001441
1442 dfu = dfu_get_entity(part->alt_id);
1443
1444 /* read header */
1445 dfu_transaction_cleanup(dfu);
1446 size = BL_HEADER_SIZE;
1447 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1448 if (ret)
1449 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001450
1451 stm32prog_header_check(&raw_header, &header);
1452 if (header.type != HEADER_STM32IMAGE)
1453 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001454
1455 /* read header + payload */
1456 size = header.image_length + BL_HEADER_SIZE;
1457 size = round_up(size, part->dev->mtd->erasesize);
1458 fsbl = calloc(1, size);
1459 if (!fsbl)
1460 return -ENOMEM;
1461 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001462 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001463 if (ret)
1464 goto error;
1465
1466 dfu_transaction_cleanup(dfu);
1467 offset = 0;
1468 for (i = part->bin_nb - 1; i > 0; i--) {
1469 offset += size;
1470 /* write to the next erase block */
1471 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001472 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1473 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001474 if (ret)
1475 goto error;
1476 }
1477
1478error:
1479 free(fsbl);
1480 return ret;
1481}
1482
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001483static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001484{
1485 if (data->phase == PHASE_FLASHLAYOUT) {
1486 if (parse_flash_layout(data, STM32_DDR_BASE, 0))
1487 stm32prog_err("Layout: invalid FlashLayout");
1488 return;
1489 }
1490
1491 if (!data->cur_part)
1492 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001493
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001494 if (data->cur_part->target == STM32PROG_RAM) {
1495 if (data->cur_part->part_type == PART_SYSTEM)
1496 data->uimage = data->cur_part->addr;
1497 if (data->cur_part->part_type == PART_FILESYSTEM)
1498 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001499 if (data->cur_part->part_type == PART_BINARY) {
1500 data->initrd = data->cur_part->addr;
1501 data->initrd_size = offset;
1502 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001503 }
1504
Patrick Delaunay6915b492020-03-18 09:24:52 +01001505 if (CONFIG_IS_ENABLED(MMC) &&
1506 data->cur_part->part_id < 0) {
1507 char cmdbuf[60];
1508
1509 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1510 data->cur_part->dev_id, data->cur_part->dev_id,
1511 -(data->cur_part->part_id));
1512 if (run_command(cmdbuf, 0)) {
1513 stm32prog_err("commands '%s' failed", cmdbuf);
1514 return;
1515 }
1516 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001517
1518 if (CONFIG_IS_ENABLED(MTD) &&
1519 data->cur_part->bin_nb > 1) {
1520 if (stm32prog_copy_fsbl(data->cur_part)) {
1521 stm32prog_err("%s (0x%x): copy of fsbl failed",
1522 data->cur_part->name, data->cur_part->id);
1523 return;
1524 }
1525 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001526}
1527
1528void stm32prog_do_reset(struct stm32prog_data *data)
1529{
1530 if (data->phase == PHASE_RESET) {
1531 data->phase = PHASE_DO_RESET;
1532 puts("Reset requested\n");
1533 }
1534}
1535
1536void stm32prog_next_phase(struct stm32prog_data *data)
1537{
1538 int phase, i;
1539 struct stm32prog_part_t *part;
1540 bool found;
1541
1542 phase = data->phase;
1543 switch (phase) {
1544 case PHASE_RESET:
1545 case PHASE_END:
1546 case PHASE_DO_RESET:
1547 return;
1548 }
1549
1550 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001551 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001552 data->cur_part = NULL;
1553 data->phase = PHASE_END;
1554 found = false;
1555 do {
1556 phase++;
1557 if (phase > PHASE_LAST_USER)
1558 break;
1559 for (i = 0; i < data->part_nb; i++) {
1560 part = &data->part_array[i];
1561 if (part->id == phase) {
1562 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1563 data->cur_part = part;
1564 data->phase = phase;
1565 found = true;
1566 }
1567 break;
1568 }
1569 }
1570 } while (!found);
1571
1572 if (data->phase == PHASE_END)
1573 puts("Phase=END\n");
1574}
1575
Patrick Delaunay291e7222020-03-18 09:24:57 +01001576static int part_delete(struct stm32prog_data *data,
1577 struct stm32prog_part_t *part)
1578{
1579 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001580 unsigned long blks, blks_offset, blks_size;
1581 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001582 char cmdbuf[40];
1583 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001584
1585 printf("Erasing %s ", part->name);
1586 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001587 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001588 if (!IS_ENABLED(CONFIG_MMC)) {
1589 ret = -1;
1590 stm32prog_err("%s (0x%x): erase invalid",
1591 part->name, part->id);
1592 break;
1593 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001594 printf("on mmc %d: ", part->dev->dev_id);
1595 block_dev = mmc_get_blk_desc(part->dev->mmc);
1596 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1597 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1598 /* -1 or -2 : delete boot partition of MMC
1599 * need to switch to associated hwpart 1 or 2
1600 */
1601 if (part->part_id < 0)
1602 if (blk_select_hwpart_devnum(IF_TYPE_MMC,
1603 part->dev->dev_id,
1604 -part->part_id))
1605 return -1;
1606
1607 blks = blk_derase(block_dev, blks_offset, blks_size);
1608
1609 /* return to user partition */
1610 if (part->part_id < 0)
1611 blk_select_hwpart_devnum(IF_TYPE_MMC,
1612 part->dev->dev_id, 0);
1613 if (blks != blks_size) {
1614 ret = -1;
1615 stm32prog_err("%s (0x%x): MMC erase failed",
1616 part->name, part->id);
1617 }
1618 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001619 case STM32PROG_NOR:
1620 case STM32PROG_NAND:
1621 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001622 if (!IS_ENABLED(CONFIG_MTD)) {
1623 ret = -1;
1624 stm32prog_err("%s (0x%x): erase invalid",
1625 part->name, part->id);
1626 break;
1627 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001628 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1629 printf("on %s: ", devstr);
1630 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1631 devstr, part->addr, part->size);
1632 if (run_command(cmdbuf, 0)) {
1633 ret = -1;
1634 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1635 part->name, part->id, cmdbuf);
1636 }
1637 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001638 case STM32PROG_RAM:
1639 printf("on ram: ");
1640 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1641 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001642 default:
1643 ret = -1;
1644 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1645 break;
1646 }
1647 if (!ret)
1648 printf("done\n");
1649
1650 return ret;
1651}
1652
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001653static void stm32prog_devices_init(struct stm32prog_data *data)
1654{
1655 int i;
1656 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001657 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001658
1659 ret = treat_partition_list(data);
1660 if (ret)
1661 goto error;
1662
1663 /* initialize the selected device */
1664 for (i = 0; i < data->dev_nb; i++) {
1665 ret = init_device(data, &data->dev[i]);
1666 if (ret)
1667 goto error;
1668 }
1669
Patrick Delaunay291e7222020-03-18 09:24:57 +01001670 /* delete RAW partition before create partition */
1671 for (i = 0; i < data->part_nb; i++) {
1672 part = &data->part_array[i];
1673
1674 if (part->part_type != RAW_IMAGE)
1675 continue;
1676
1677 if (!IS_SELECT(part) || !IS_DELETE(part))
1678 continue;
1679
1680 ret = part_delete(data, part);
1681 if (ret)
1682 goto error;
1683 }
1684
Patrick Delaunay8040da12020-07-31 16:31:52 +02001685 if (IS_ENABLED(CONFIG_MMC)) {
1686 ret = create_gpt_partitions(data);
1687 if (ret)
1688 goto error;
1689 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001690
Patrick Delaunay291e7222020-03-18 09:24:57 +01001691 /* delete partition GPT or MTD */
1692 for (i = 0; i < data->part_nb; i++) {
1693 part = &data->part_array[i];
1694
1695 if (part->part_type == RAW_IMAGE)
1696 continue;
1697
1698 if (!IS_SELECT(part) || !IS_DELETE(part))
1699 continue;
1700
1701 ret = part_delete(data, part);
1702 if (ret)
1703 goto error;
1704 }
1705
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001706 return;
1707
1708error:
1709 data->part_nb = 0;
1710}
1711
1712int stm32prog_dfu_init(struct stm32prog_data *data)
1713{
1714 /* init device if no error */
1715 if (data->part_nb)
1716 stm32prog_devices_init(data);
1717
1718 if (data->part_nb)
1719 stm32prog_next_phase(data);
1720
1721 /* prepare DFU for device read/write */
1722 dfu_free_entities();
1723 return dfu_init_entities(data);
1724}
1725
1726int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size)
1727{
1728 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001729 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001730 data->phase = PHASE_FLASHLAYOUT;
1731
1732 return parse_flash_layout(data, addr, size);
1733}
1734
1735void stm32prog_clean(struct stm32prog_data *data)
1736{
1737 /* clean */
1738 dfu_free_entities();
1739 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001740 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001741 free(data->buffer);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001742}
1743
1744/* DFU callback: used after serial and direct DFU USB access */
1745void dfu_flush_callback(struct dfu_entity *dfu)
1746{
1747 if (!stm32prog_data)
1748 return;
1749
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001750 if (dfu->dev_type == DFU_DEV_VIRT) {
1751 if (dfu->data.virt.dev_num == PHASE_OTP)
1752 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001753 else if (dfu->data.virt.dev_num == PHASE_PMIC)
1754 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001755 return;
1756 }
1757
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001758 if (dfu->dev_type == DFU_DEV_RAM) {
1759 if (dfu->alt == 0 &&
1760 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001761 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001762 /* waiting DFU DETACH for reenumeration */
1763 }
1764 }
1765
1766 if (!stm32prog_data->cur_part)
1767 return;
1768
1769 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001770 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001771 stm32prog_next_phase(stm32prog_data);
1772 }
1773}
1774
1775void dfu_initiated_callback(struct dfu_entity *dfu)
1776{
1777 if (!stm32prog_data)
1778 return;
1779
1780 if (!stm32prog_data->cur_part)
1781 return;
1782
1783 /* force the saved offset for the current partition */
1784 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
1785 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01001786 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001787 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001788 }
1789}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02001790
1791void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
1792{
1793 struct stm32prog_data *data = stm32prog_data;
1794
1795 if (!stm32prog_data)
1796 return;
1797
1798 if (!stm32prog_data->cur_part)
1799 return;
1800
1801 if (dfu->alt == stm32prog_data->cur_part->alt_id)
1802 stm32prog_err(msg);
1803}