blob: 3e1fdee5b385e55c2211fbadc81b2848aee140ad [file] [log] [blame]
Patrick Delaunay954bd1a2020-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 Glass09140112020-05-10 11:40:03 -06006#include <command.h>
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01007#include <console.h>
8#include <dfu.h>
9#include <malloc.h>
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +010010#include <misc.h>
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +010011#include <mmc.h>
Patrick Delaunayeb845d62020-03-18 09:24:54 +010012#include <part.h>
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +020013#include <tee.h>
Patrick Delaunay936f1ae2020-03-18 09:24:58 +010014#include <asm/arch/stm32mp1_smc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +020016#include <dm/device_compat.h>
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010017#include <dm/uclass.h>
Patrick Delaunayeb845d62020-03-18 09:24:54 +010018#include <jffs2/load_kernel.h>
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010019#include <linux/list.h>
20#include <linux/list_sort.h>
Patrick Delaunayeb845d62020-03-18 09:24:54 +010021#include <linux/mtd/mtd.h>
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010022#include <linux/sizes.h>
23
24#include "stm32prog.h"
25
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +010026/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
27#define GPT_HEADER_SZ 34
28
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010029#define OPT_SELECT BIT(0)
30#define OPT_EMPTY BIT(1)
Patrick Delaunay2bad5d02020-03-18 09:24:57 +010031#define OPT_DELETE BIT(2)
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010032
33#define IS_SELECT(part) ((part)->option & OPT_SELECT)
34#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay2bad5d02020-03-18 09:24:57 +010035#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay954bd1a2020-03-18 09:24:49 +010036
37#define ALT_BUF_LEN SZ_1K
38
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +010039#define ROOTFS_MMC0_UUID \
40 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
41 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
42
43#define ROOTFS_MMC1_UUID \
44 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
45 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
46
47#define ROOTFS_MMC2_UUID \
48 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
49 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
50
Patrick Delaunayd983e502022-03-28 19:25:26 +020051/* RAW partition (binary / bootloader) used Linux - reserved UUID */
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +010052#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
53
54/*
55 * unique partition guid (uuid) for partition named "rootfs"
56 * on each MMC instance = SD Card or eMMC
57 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
58 */
59static const efi_guid_t uuid_mmc[3] = {
60 ROOTFS_MMC0_UUID,
61 ROOTFS_MMC1_UUID,
62 ROOTFS_MMC2_UUID
63};
64
Patrick Delaunay6ee68392020-03-18 09:24:50 +010065/* order of column in flash layout file */
66enum stm32prog_col_t {
67 COL_OPTION,
68 COL_ID,
69 COL_NAME,
70 COL_TYPE,
71 COL_IP,
72 COL_OFFSET,
73 COL_NB_STM32
74};
75
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +020076#define FIP_TOC_HEADER_NAME 0xAA640001
77
78struct fip_toc_header {
79 u32 name;
80 u32 serial_number;
81 u64 flags;
82};
83
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +020084#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
85 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
86
87/*
88 * Read NVMEM memory for STM32CubeProgrammer
89 *
90 * [in] value[0].a: Type (0 for OTP access)
91 * [out] memref[1].buffer Output buffer to return all read values
92 * [out] memref[1].size Size of buffer to be read
93 *
94 * Return codes:
95 * TEE_SUCCESS - Invoke command success
96 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
97 */
98#define TA_NVMEM_READ 0x0
99
100/*
101 * Write NVMEM memory for STM32CubeProgrammer
102 *
103 * [in] value[0].a Type (0 for OTP access)
104 * [in] memref[1].buffer Input buffer with the values to write
105 * [in] memref[1].size Size of buffer to be written
106 *
107 * Return codes:
108 * TEE_SUCCESS - Invoke command success
109 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
110 */
111#define TA_NVMEM_WRITE 0x1
112
113/* value of TA_NVMEM type = value[in] a */
114#define NVMEM_OTP 0
115
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +0200116DECLARE_GLOBAL_DATA_PTR;
117
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +0200118/* OPTEE TA NVMEM open helper */
119static int optee_ta_open(struct stm32prog_data *data)
120{
121 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
122 struct tee_open_session_arg arg;
123 struct udevice *tee = NULL;
124 int rc;
125
126 if (data->tee)
127 return 0;
128
129 tee = tee_find_device(NULL, NULL, NULL, NULL);
130 if (!tee)
131 return -ENODEV;
132
133 memset(&arg, 0, sizeof(arg));
134 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
135 rc = tee_open_session(tee, &arg, 0, NULL);
136 if (rc < 0)
137 return -ENODEV;
138
139 data->tee = tee;
140 data->tee_session = arg.session;
141
142 return 0;
143}
144
145/* OPTEE TA NVMEM invoke helper */
146static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
147 void *buff, ulong size)
148{
149 struct tee_invoke_arg arg;
150 struct tee_param param[2];
151 struct tee_shm *buff_shm;
152 int rc;
153
154 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
155 if (rc)
156 return rc;
157
158 memset(&arg, 0, sizeof(arg));
159 arg.func = cmd;
160 arg.session = data->tee_session;
161
162 memset(param, 0, sizeof(param));
163 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
164 param[0].u.value.a = type;
165
166 if (cmd == TA_NVMEM_WRITE)
167 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
168 else
169 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
170
171 param[1].u.memref.shm = buff_shm;
172 param[1].u.memref.size = size;
173
174 rc = tee_invoke_func(data->tee, &arg, 2, param);
175 if (rc < 0 || arg.ret != 0) {
176 dev_err(data->tee,
177 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
178 arg.ret, rc);
179 if (!rc)
180 rc = -EIO;
181 }
182
183 tee_shm_free(buff_shm);
184
185 return rc;
186}
187
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100188/* partition handling routines : CONFIG_CMD_MTDPARTS */
189int mtdparts_init(void);
190int find_dev_and_part(const char *id, struct mtd_device **dev,
191 u8 *part_num, struct part_info **part);
192
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100193char *stm32prog_get_error(struct stm32prog_data *data)
194{
195 static const char error_msg[] = "Unspecified";
196
197 if (strlen(data->error) == 0)
198 strcpy(data->error, error_msg);
199
200 return data->error;
201}
202
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +0200203static bool stm32prog_is_fip_header(struct fip_toc_header *header)
204{
205 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
206}
207
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +0200208static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100209{
210 unsigned int i;
211
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +0200212 if (header->magic_number !=
213 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
214 log_debug("%s:invalid magic number : 0x%x\n",
215 __func__, header->magic_number);
216 return false;
217 }
218 if (header->header_version != 0x00010000) {
219 log_debug("%s:invalid header version : 0x%x\n",
220 __func__, header->header_version);
221 return false;
222 }
223
224 if (header->reserved1 || header->reserved2) {
225 log_debug("%s:invalid reserved field\n", __func__);
226 return false;
227 }
228 for (i = 0; i < sizeof(header->padding); i++) {
229 if (header->padding[i] != 0) {
230 log_debug("%s:invalid padding field\n", __func__);
231 return false;
232 }
233 }
234
235 return true;
236}
237
238static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
239{
240 unsigned int i;
241
242 if (header->magic_number !=
243 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
244 log_debug("%s:invalid magic number : 0x%x\n",
245 __func__, header->magic_number);
246 return false;
247 }
248 if (header->header_version != 0x00020000) {
249 log_debug("%s:invalid header version : 0x%x\n",
250 __func__, header->header_version);
251 return false;
252 }
253 if (header->reserved1 || header->reserved2)
254 return false;
255
256 for (i = 0; i < sizeof(header->padding); i++) {
257 if (header->padding[i] != 0) {
258 log_debug("%s:invalid padding field\n", __func__);
259 return false;
260 }
261 }
262
263 return true;
264}
265
266void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
267{
268 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
269 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
270
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +0200271 if (!raw_header || !header) {
272 log_debug("%s:no header data\n", __func__);
273 return;
274 }
275
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +0200276 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
277 header->type = HEADER_FIP;
278 header->length = 0;
279 return;
280 }
281 if (stm32prog_is_stm32_header_v1(v1_header)) {
282 header->type = HEADER_STM32IMAGE;
283 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
284 header->image_length = le32_to_cpu(v1_header->image_length);
285 header->length = sizeof(struct stm32_header_v1);
286 return;
287 }
288 if (stm32prog_is_stm32_header_v2(v2_header)) {
289 header->type = HEADER_STM32IMAGE_V2;
290 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
291 header->image_length = le32_to_cpu(v2_header->image_length);
292 header->length = sizeof(struct stm32_header_v1) +
293 v2_header->extension_headers_length;
294 return;
295 }
296
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +0200297 header->type = HEADER_NONE;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100298 header->image_checksum = 0x0;
299 header->image_length = 0x0;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100300}
301
302static u32 stm32prog_header_checksum(u32 addr, struct image_header_s *header)
303{
304 u32 i, checksum;
305 u8 *payload;
306
307 /* compute checksum on payload */
308 payload = (u8 *)addr;
309 checksum = 0;
310 for (i = header->image_length; i > 0; i--)
311 checksum += *(payload++);
312
313 return checksum;
314}
315
316/* FLASHLAYOUT PARSING *****************************************/
317static int parse_option(struct stm32prog_data *data,
318 int i, char *p, struct stm32prog_part_t *part)
319{
320 int result = 0;
321 char *c = p;
322
323 part->option = 0;
324 if (!strcmp(p, "-"))
325 return 0;
326
327 while (*c) {
328 switch (*c) {
329 case 'P':
330 part->option |= OPT_SELECT;
331 break;
332 case 'E':
333 part->option |= OPT_EMPTY;
334 break;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +0100335 case 'D':
336 part->option |= OPT_DELETE;
337 break;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100338 default:
339 result = -EINVAL;
340 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
341 i, *c, p);
342 return -EINVAL;
343 }
344 c++;
345 }
346 if (!(part->option & OPT_SELECT)) {
347 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
348 return -EINVAL;
349 }
350
351 return result;
352}
353
354static int parse_id(struct stm32prog_data *data,
355 int i, char *p, struct stm32prog_part_t *part)
356{
357 int result = 0;
358 unsigned long value;
359
360 result = strict_strtoul(p, 0, &value);
361 part->id = value;
362 if (result || value > PHASE_LAST_USER) {
363 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
364 result = -EINVAL;
365 }
366
367 return result;
368}
369
370static int parse_name(struct stm32prog_data *data,
371 int i, char *p, struct stm32prog_part_t *part)
372{
373 int result = 0;
374
375 if (strlen(p) < sizeof(part->name)) {
376 strcpy(part->name, p);
377 } else {
378 stm32prog_err("Layout line %d: partition name too long [%d]: %s",
379 i, strlen(p), p);
380 result = -EINVAL;
381 }
382
383 return result;
384}
385
386static int parse_type(struct stm32prog_data *data,
387 int i, char *p, struct stm32prog_part_t *part)
388{
389 int result = 0;
Patrick Delaunayb83caf92020-03-18 09:24:56 +0100390 int len = 0;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100391
Patrick Delaunayb83caf92020-03-18 09:24:56 +0100392 part->bin_nb = 0;
393 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100394 part->part_type = PART_BINARY;
Patrick Delaunayb83caf92020-03-18 09:24:56 +0100395
396 /* search for Binary(X) case */
397 len = strlen(p);
398 part->bin_nb = 1;
399 if (len > 6) {
400 if (len < 8 ||
401 (p[6] != '(') ||
402 (p[len - 1] != ')'))
403 result = -EINVAL;
404 else
405 part->bin_nb =
Simon Glass0b1284e2021-07-24 09:03:30 -0600406 dectoul(&p[7], NULL);
Patrick Delaunayb83caf92020-03-18 09:24:56 +0100407 }
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100408 } else if (!strcmp(p, "System")) {
409 part->part_type = PART_SYSTEM;
410 } else if (!strcmp(p, "FileSystem")) {
411 part->part_type = PART_FILESYSTEM;
412 } else if (!strcmp(p, "RawImage")) {
413 part->part_type = RAW_IMAGE;
414 } else {
415 result = -EINVAL;
416 }
417 if (result)
418 stm32prog_err("Layout line %d: type parsing error : '%s'",
419 i, p);
420
421 return result;
422}
423
424static int parse_ip(struct stm32prog_data *data,
425 int i, char *p, struct stm32prog_part_t *part)
426{
427 int result = 0;
428 unsigned int len = 0;
429
430 part->dev_id = 0;
431 if (!strcmp(p, "none")) {
432 part->target = STM32PROG_NONE;
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100433 } else if (!strncmp(p, "mmc", 3)) {
434 part->target = STM32PROG_MMC;
435 len = 3;
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100436 } else if (!strncmp(p, "nor", 3)) {
437 part->target = STM32PROG_NOR;
438 len = 3;
439 } else if (!strncmp(p, "nand", 4)) {
440 part->target = STM32PROG_NAND;
441 len = 4;
442 } else if (!strncmp(p, "spi-nand", 8)) {
443 part->target = STM32PROG_SPI_NAND;
444 len = 8;
Patrick Delaunay306a5cf2020-03-18 09:25:03 +0100445 } else if (!strncmp(p, "ram", 3)) {
446 part->target = STM32PROG_RAM;
447 len = 0;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100448 } else {
449 result = -EINVAL;
450 }
451 if (len) {
452 /* only one digit allowed for device id */
453 if (strlen(p) != len + 1) {
454 result = -EINVAL;
455 } else {
456 part->dev_id = p[len] - '0';
457 if (part->dev_id > 9)
458 result = -EINVAL;
459 }
460 }
461 if (result)
462 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
463
464 return result;
465}
466
467static int parse_offset(struct stm32prog_data *data,
468 int i, char *p, struct stm32prog_part_t *part)
469{
470 int result = 0;
471 char *tail;
472
473 part->part_id = 0;
Patrick Delaunay878f7542020-03-18 09:24:52 +0100474 part->addr = 0;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100475 part->size = 0;
Patrick Delaunay878f7542020-03-18 09:24:52 +0100476 /* eMMC boot parttion */
477 if (!strncmp(p, "boot", 4)) {
478 if (strlen(p) != 5) {
479 result = -EINVAL;
480 } else {
481 if (p[4] == '1')
482 part->part_id = -1;
483 else if (p[4] == '2')
484 part->part_id = -2;
485 else
486 result = -EINVAL;
487 }
488 if (result)
489 stm32prog_err("Layout line %d: invalid part '%s'",
490 i, p);
491 } else {
492 part->addr = simple_strtoull(p, &tail, 0);
493 if (tail == p || *tail != '\0') {
494 stm32prog_err("Layout line %d: invalid offset '%s'",
495 i, p);
496 result = -EINVAL;
497 }
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100498 }
499
500 return result;
501}
502
503static
504int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
505 struct stm32prog_part_t *part) = {
506 [COL_OPTION] = parse_option,
507 [COL_ID] = parse_id,
508 [COL_NAME] = parse_name,
509 [COL_TYPE] = parse_type,
510 [COL_IP] = parse_ip,
511 [COL_OFFSET] = parse_offset,
512};
513
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100514static int parse_flash_layout(struct stm32prog_data *data,
515 ulong addr,
516 ulong size)
517{
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100518 int column = 0, part_nb = 0, ret;
519 bool end_of_line, eof;
520 char *p, *start, *last, *col;
521 struct stm32prog_part_t *part;
Patrick Delaunay5f14e2f2021-05-18 15:12:04 +0200522 struct image_header_s header;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100523 int part_list_size;
524 int i;
525
526 data->part_nb = 0;
527
528 /* check if STM32image is detected */
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +0200529 stm32prog_header_check(addr, &header);
Patrick Delaunay5f14e2f2021-05-18 15:12:04 +0200530 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100531 u32 checksum;
532
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +0200533 addr = addr + header.length;
Patrick Delaunay5f14e2f2021-05-18 15:12:04 +0200534 size = header.image_length;
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100535
Patrick Delaunay5f14e2f2021-05-18 15:12:04 +0200536 checksum = stm32prog_header_checksum(addr, &header);
537 if (checksum != header.image_checksum) {
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100538 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunay5f14e2f2021-05-18 15:12:04 +0200539 checksum, header.image_checksum);
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100540 return -EIO;
541 }
542 }
543 if (!size)
544 return -EINVAL;
545
546 start = (char *)addr;
547 last = start + size;
548
549 *last = 0x0; /* force null terminated string */
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100550 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay6ee68392020-03-18 09:24:50 +0100551
552 /* calculate expected number of partitions */
553 part_list_size = 1;
554 p = start;
555 while (*p && (p < last)) {
556 if (*p++ == '\n') {
557 part_list_size++;
558 if (p < last && *p == '#')
559 part_list_size--;
560 }
561 }
562 if (part_list_size > PHASE_LAST_USER) {
563 stm32prog_err("Layout: too many partition (%d)",
564 part_list_size);
565 return -1;
566 }
567 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
568 if (!part) {
569 stm32prog_err("Layout: alloc failed");
570 return -ENOMEM;
571 }
572 data->part_array = part;
573
574 /* main parsing loop */
575 i = 1;
576 eof = false;
577 p = start;
578 col = start; /* 1st column */
579 end_of_line = false;
580 while (!eof) {
581 switch (*p) {
582 /* CR is ignored and replaced by NULL character */
583 case '\r':
584 *p = '\0';
585 p++;
586 continue;
587 case '\0':
588 end_of_line = true;
589 eof = true;
590 break;
591 case '\n':
592 end_of_line = true;
593 break;
594 case '\t':
595 break;
596 case '#':
597 /* comment line is skipped */
598 if (column == 0 && p == col) {
599 while ((p < last) && *p)
600 if (*p++ == '\n')
601 break;
602 col = p;
603 i++;
604 if (p >= last || !*p) {
605 eof = true;
606 end_of_line = true;
607 }
608 continue;
609 }
610 /* fall through */
611 /* by default continue with the next character */
612 default:
613 p++;
614 continue;
615 }
616
617 /* replace by \0: allow string parsing for each column */
618 *p = '\0';
619 p++;
620 if (p >= last) {
621 eof = true;
622 end_of_line = true;
623 }
624
625 /* skip empty line and multiple TAB in tsv file */
626 if (strlen(col) == 0) {
627 col = p;
628 /* skip empty line */
629 if (column == 0 && end_of_line) {
630 end_of_line = false;
631 i++;
632 }
633 continue;
634 }
635
636 if (column < COL_NB_STM32) {
637 ret = parse[column](data, i, col, part);
638 if (ret)
639 return ret;
640 }
641
642 /* save the beginning of the next column */
643 column++;
644 col = p;
645
646 if (!end_of_line)
647 continue;
648
649 /* end of the line detected */
650 end_of_line = false;
651
652 if (column < COL_NB_STM32) {
653 stm32prog_err("Layout line %d: no enought column", i);
654 return -EINVAL;
655 }
656 column = 0;
657 part_nb++;
658 part++;
659 i++;
660 if (part_nb >= part_list_size) {
661 part = NULL;
662 if (!eof) {
663 stm32prog_err("Layout: no enought memory for %d part",
664 part_nb);
665 return -EINVAL;
666 }
667 }
668 }
669 data->part_nb = part_nb;
670 if (data->part_nb == 0) {
671 stm32prog_err("Layout: no partition found");
672 return -ENODEV;
673 }
674
675 return 0;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100676}
677
678static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
679{
680 struct stm32prog_part_t *parta, *partb;
681
682 parta = container_of(a, struct stm32prog_part_t, list);
683 partb = container_of(b, struct stm32prog_part_t, list);
684
Patrick Delaunay878f7542020-03-18 09:24:52 +0100685 if (parta->part_id != partb->part_id)
686 return parta->part_id - partb->part_id;
687 else
688 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100689}
690
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100691static void get_mtd_by_target(char *string, enum stm32prog_target target,
692 int dev_id)
693{
694 const char *dev_str;
695
696 switch (target) {
697 case STM32PROG_NOR:
698 dev_str = "nor";
699 break;
700 case STM32PROG_NAND:
701 dev_str = "nand";
702 break;
703 case STM32PROG_SPI_NAND:
704 dev_str = "spi-nand";
705 break;
706 default:
707 dev_str = "invalid";
708 break;
709 }
710 sprintf(string, "%s%d", dev_str, dev_id);
711}
712
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100713static int init_device(struct stm32prog_data *data,
714 struct stm32prog_dev_t *dev)
715{
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100716 struct mmc *mmc = NULL;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100717 struct blk_desc *block_dev = NULL;
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100718 struct mtd_info *mtd = NULL;
719 char mtd_id[16];
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100720 int part_id;
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100721 int ret;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100722 u64 first_addr = 0, last_addr = 0;
723 struct stm32prog_part_t *part, *next_part;
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100724 u64 part_addr, part_size;
725 bool part_found;
726 const char *part_name;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100727
728 switch (dev->target) {
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100729 case STM32PROG_MMC:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +0200730 if (!IS_ENABLED(CONFIG_MMC)) {
731 stm32prog_err("unknown device type = %d", dev->target);
732 return -ENODEV;
733 }
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100734 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayded48be2020-07-06 13:20:58 +0200735 if (!mmc || mmc_init(mmc)) {
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100736 stm32prog_err("mmc device %d not found", dev->dev_id);
737 return -ENODEV;
738 }
739 block_dev = mmc_get_blk_desc(mmc);
740 if (!block_dev) {
741 stm32prog_err("mmc device %d not probed", dev->dev_id);
742 return -ENODEV;
743 }
744 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
745 dev->mmc = mmc;
746
747 /* reserve a full erase group for each GTP headers */
748 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
749 first_addr = dev->erase_size;
750 last_addr = (u64)(block_dev->lba -
751 mmc->erase_grp_size) *
752 block_dev->blksz;
753 } else {
754 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
755 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
756 block_dev->blksz;
757 }
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100758 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
759 block_dev->lba, block_dev->blksz);
760 log_debug(" available address = 0x%llx..0x%llx\n",
761 first_addr, last_addr);
762 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100763 break;
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100764 case STM32PROG_NOR:
765 case STM32PROG_NAND:
766 case STM32PROG_SPI_NAND:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +0200767 if (!IS_ENABLED(CONFIG_MTD)) {
768 stm32prog_err("unknown device type = %d", dev->target);
769 return -ENODEV;
770 }
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100771 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100772 log_debug("%s\n", mtd_id);
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100773
774 mtdparts_init();
775 mtd = get_mtd_device_nm(mtd_id);
776 if (IS_ERR(mtd)) {
777 stm32prog_err("MTD device %s not found", mtd_id);
778 return -ENODEV;
779 }
780 first_addr = 0;
781 last_addr = mtd->size;
782 dev->erase_size = mtd->erasesize;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100783 log_debug("MTD device %s: size=%lld erasesize=%d\n",
784 mtd_id, mtd->size, mtd->erasesize);
785 log_debug(" available address = 0x%llx..0x%llx\n",
786 first_addr, last_addr);
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100787 dev->mtd = mtd;
788 break;
Patrick Delaunay306a5cf2020-03-18 09:25:03 +0100789 case STM32PROG_RAM:
790 first_addr = gd->bd->bi_dram[0].start;
791 last_addr = first_addr + gd->bd->bi_dram[0].size;
792 dev->erase_size = 1;
793 break;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100794 default:
795 stm32prog_err("unknown device type = %d", dev->target);
796 return -ENODEV;
797 }
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100798 log_debug(" erase size = 0x%x\n", dev->erase_size);
799 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100800
801 /* order partition list in offset order */
802 list_sort(NULL, &dev->part_list, &part_cmp);
803 part_id = 1;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100804 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100805 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunayb83caf92020-03-18 09:24:56 +0100806 if (part->bin_nb > 1) {
807 if ((dev->target != STM32PROG_NAND &&
808 dev->target != STM32PROG_SPI_NAND) ||
809 part->id >= PHASE_FIRST_USER ||
810 strncmp(part->name, "fsbl", 4)) {
811 stm32prog_err("%s (0x%x): multiple binary %d not supported",
812 part->name, part->id,
813 part->bin_nb);
814 return -EINVAL;
815 }
816 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100817 if (part->part_type == RAW_IMAGE) {
818 part->part_id = 0x0;
819 part->addr = 0x0;
820 if (block_dev)
821 part->size = block_dev->lba * block_dev->blksz;
822 else
823 part->size = last_addr;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100824 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
825 part->option, part->id, part->name,
826 part->part_type, part->bin_nb, part->target,
827 part->dev_id, part->addr, part->size);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100828 continue;
829 }
Patrick Delaunay878f7542020-03-18 09:24:52 +0100830 if (part->part_id < 0) { /* boot hw partition for eMMC */
831 if (mmc) {
832 part->size = mmc->capacity_boot;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100833 } else {
Patrick Delaunay878f7542020-03-18 09:24:52 +0100834 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100835 part->name, part->id,
Patrick Delaunay878f7542020-03-18 09:24:52 +0100836 part->part_id);
837 return -ENODEV;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100838 }
839 } else {
Patrick Delaunay878f7542020-03-18 09:24:52 +0100840 part->part_id = part_id++;
841
842 /* last partition : size to the end of the device */
843 if (part->list.next != &dev->part_list) {
844 next_part =
845 container_of(part->list.next,
846 struct stm32prog_part_t,
847 list);
848 if (part->addr < next_part->addr) {
849 part->size = next_part->addr -
850 part->addr;
851 } else {
852 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
853 part->name, part->id,
854 part->addr,
855 next_part->name,
856 next_part->id,
857 next_part->addr);
858 return -EINVAL;
859 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100860 } else {
Patrick Delaunay878f7542020-03-18 09:24:52 +0100861 if (part->addr <= last_addr) {
862 part->size = last_addr - part->addr;
863 } else {
864 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
865 part->name, part->id,
866 part->addr, last_addr);
867 return -EINVAL;
868 }
869 }
870 if (part->addr < first_addr) {
871 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100872 part->name, part->id,
Patrick Delaunay878f7542020-03-18 09:24:52 +0100873 part->addr, first_addr);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100874 return -EINVAL;
875 }
876 }
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +0100877 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
878 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
879 part->name, part->id, part->addr,
880 part->dev->erase_size);
881 return -EINVAL;
882 }
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100883 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
884 part->part_id, part->option, part->id, part->name,
885 part->part_type, part->bin_nb, part->target,
886 part->dev_id, part->addr, part->size);
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100887
888 part_addr = 0;
889 part_size = 0;
890 part_found = false;
891
892 /* check coherency with existing partition */
893 if (block_dev) {
894 /*
895 * block devices with GPT: check user partition size
896 * only for partial update, the GPT partions are be
897 * created for full update
898 */
899 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100900 log_debug("\n");
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100901 continue;
902 }
Simon Glass05289792020-05-10 11:39:57 -0600903 struct disk_partition partinfo;
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100904
905 ret = part_get_info(block_dev, part->part_id,
906 &partinfo);
907
908 if (ret) {
909 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
910 part->name, part->id,
911 part_id, part->dev_id);
912 return -ENODEV;
913 }
914 part_addr = (u64)partinfo.start * partinfo.blksz;
915 part_size = (u64)partinfo.size * partinfo.blksz;
916 part_name = (char *)partinfo.name;
917 part_found = true;
918 }
919
Patrick Delaunay9a2b0542020-07-31 16:31:52 +0200920 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrick Delaunayeb845d62020-03-18 09:24:54 +0100921 char mtd_part_id[32];
922 struct part_info *mtd_part;
923 struct mtd_device *mtd_dev;
924 u8 part_num;
925
926 sprintf(mtd_part_id, "%s,%d", mtd_id,
927 part->part_id - 1);
928 ret = find_dev_and_part(mtd_part_id, &mtd_dev,
929 &part_num, &mtd_part);
930 if (ret != 0) {
931 stm32prog_err("%s (0x%x): Invalid MTD partition %s",
932 part->name, part->id,
933 mtd_part_id);
934 return -ENODEV;
935 }
936 part_addr = mtd_part->offset;
937 part_size = mtd_part->size;
938 part_name = mtd_part->name;
939 part_found = true;
940 }
Patrick Delaunay9a2b0542020-07-31 16:31:52 +0200941
Patrick Delaunay53de79f2020-10-15 14:28:17 +0200942 /* no partition for this device */
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100943 if (!part_found) {
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100944 log_debug("\n");
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100945 continue;
946 }
947
Patrick Delaunay711b5bc2020-11-06 19:01:30 +0100948 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunayffc405e2020-03-18 09:24:53 +0100949
950 if (part->addr != part_addr) {
951 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
952 part->name, part->id, part->part_id,
953 part_name, part->addr, part_addr);
954 return -ENODEV;
955 }
956 if (part->size != part_size) {
957 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
958 part->name, part->id, part->part_id,
959 part_name, part->addr, part->size,
960 part_size);
961 return -ENODEV;
962 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100963 }
964 return 0;
965}
966
967static int treat_partition_list(struct stm32prog_data *data)
968{
969 int i, j;
970 struct stm32prog_part_t *part;
971
972 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
973 data->dev[j].target = STM32PROG_NONE;
974 INIT_LIST_HEAD(&data->dev[j].part_list);
975 }
976
Patrick Delaunayf64d32a2021-07-26 11:21:38 +0200977#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunay8f035f72020-03-18 09:24:55 +0100978 data->tee_detected = false;
Patrick Delaunayf64d32a2021-07-26 11:21:38 +0200979#endif
Patrick Delaunay8f035f72020-03-18 09:24:55 +0100980 data->fsbl_nor_detected = false;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100981 for (i = 0; i < data->part_nb; i++) {
982 part = &data->part_array[i];
983 part->alt_id = -1;
984
985 /* skip partition with IP="none" */
986 if (part->target == STM32PROG_NONE) {
987 if (IS_SELECT(part)) {
Patrick Delaunay94e561f2022-01-18 10:33:14 +0100988 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
989 part->id, part->name);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100990 return -EINVAL;
991 }
992 continue;
993 }
994
995 if (part->id == PHASE_FLASHLAYOUT ||
996 part->id > PHASE_LAST_USER) {
Patrick Delaunay94e561f2022-01-18 10:33:14 +0100997 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
998 part->id, part->name);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +0100999 return -EINVAL;
1000 }
1001 for (j = i + 1; j < data->part_nb; j++) {
1002 if (part->id == data->part_array[j].id) {
Patrick Delaunay94e561f2022-01-18 10:33:14 +01001003 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1004 part->id, part->name, data->part_array[j].name);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001005 return -EINVAL;
1006 }
1007 }
1008 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1009 if (data->dev[j].target == STM32PROG_NONE) {
1010 /* new device found */
1011 data->dev[j].target = part->target;
1012 data->dev[j].dev_id = part->dev_id;
Patrick Delaunayffc405e2020-03-18 09:24:53 +01001013 data->dev[j].full_update = true;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001014 data->dev_nb++;
1015 break;
1016 } else if ((part->target == data->dev[j].target) &&
1017 (part->dev_id == data->dev[j].dev_id)) {
1018 break;
1019 }
1020 }
1021 if (j == STM32PROG_MAX_DEV) {
1022 stm32prog_err("Layout: too many device");
1023 return -EINVAL;
1024 }
Patrick Delaunay8f035f72020-03-18 09:24:55 +01001025 switch (part->target) {
1026 case STM32PROG_NOR:
1027 if (!data->fsbl_nor_detected &&
1028 !strncmp(part->name, "fsbl", 4))
1029 data->fsbl_nor_detected = true;
1030 /* fallthrough */
1031 case STM32PROG_NAND:
1032 case STM32PROG_SPI_NAND:
Patrick Delaunayf64d32a2021-07-26 11:21:38 +02001033#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunay8f035f72020-03-18 09:24:55 +01001034 if (!data->tee_detected &&
1035 !strncmp(part->name, "tee", 3))
1036 data->tee_detected = true;
1037 break;
Patrick Delaunayf64d32a2021-07-26 11:21:38 +02001038#endif
Patrick Delaunay8f035f72020-03-18 09:24:55 +01001039 default:
1040 break;
1041 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001042 part->dev = &data->dev[j];
Patrick Delaunayffc405e2020-03-18 09:24:53 +01001043 if (!IS_SELECT(part))
1044 part->dev->full_update = false;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001045 list_add_tail(&part->list, &data->dev[j].part_list);
1046 }
1047
1048 return 0;
1049}
1050
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001051static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001052{
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001053 int offset = 0;
1054 const int buflen = SZ_8K;
1055 char *buf;
1056 char uuid[UUID_STR_LEN + 1];
1057 unsigned char *uuid_bin;
1058 unsigned int mmc_id;
1059 int i;
1060 bool rootfs_found;
1061 struct stm32prog_part_t *part;
1062
1063 buf = malloc(buflen);
1064 if (!buf)
1065 return -ENOMEM;
1066
1067 puts("partitions : ");
1068 /* initialize the selected device */
1069 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunayffc405e2020-03-18 09:24:53 +01001070 /* create gpt partition support only for full update on MMC */
1071 if (data->dev[i].target != STM32PROG_MMC ||
1072 !data->dev[i].full_update)
1073 continue;
1074
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001075 offset = 0;
1076 rootfs_found = false;
1077 memset(buf, 0, buflen);
1078
1079 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay878f7542020-03-18 09:24:52 +01001080 /* skip eMMC boot partitions */
1081 if (part->part_id < 0)
1082 continue;
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001083 /* skip Raw Image */
1084 if (part->part_type == RAW_IMAGE)
1085 continue;
1086
1087 if (offset + 100 > buflen) {
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001088 log_debug("\n%s: buffer too small, %s skippped",
1089 __func__, part->name);
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001090 continue;
1091 }
1092
1093 if (!offset)
1094 offset += sprintf(buf, "gpt write mmc %d \"",
1095 data->dev[i].dev_id);
1096
1097 offset += snprintf(buf + offset, buflen - offset,
1098 "name=%s,start=0x%llx,size=0x%llx",
1099 part->name,
1100 part->addr,
1101 part->size);
1102
1103 if (part->part_type == PART_BINARY)
1104 offset += snprintf(buf + offset,
1105 buflen - offset,
1106 ",type="
1107 LINUX_RESERVED_UUID);
1108 else
1109 offset += snprintf(buf + offset,
1110 buflen - offset,
1111 ",type=linux");
1112
1113 if (part->part_type == PART_SYSTEM)
1114 offset += snprintf(buf + offset,
1115 buflen - offset,
1116 ",bootable");
1117
1118 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1119 mmc_id = part->dev_id;
1120 rootfs_found = true;
1121 if (mmc_id < ARRAY_SIZE(uuid_mmc)) {
1122 uuid_bin =
1123 (unsigned char *)uuid_mmc[mmc_id].b;
1124 uuid_bin_to_str(uuid_bin, uuid,
1125 UUID_STR_FORMAT_GUID);
1126 offset += snprintf(buf + offset,
1127 buflen - offset,
1128 ",uuid=%s", uuid);
1129 }
1130 }
1131
1132 offset += snprintf(buf + offset, buflen - offset, ";");
1133 }
1134
1135 if (offset) {
1136 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001137 log_debug("\ncmd: %s\n", buf);
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001138 if (run_command(buf, 0)) {
1139 stm32prog_err("GPT partitionning fail: %s",
1140 buf);
1141 free(buf);
1142
1143 return -1;
1144 }
1145 }
1146
1147 if (data->dev[i].mmc)
1148 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1149
1150#ifdef DEBUG
1151 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001152 log_debug("\ncmd: %s", buf);
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001153 if (run_command(buf, 0))
1154 printf("fail !\n");
1155 else
1156 printf("OK\n");
1157
1158 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1159 run_command(buf, 0);
1160#endif
1161 }
1162 puts("done\n");
1163
Patrick Delaunayeb845d62020-03-18 09:24:54 +01001164#ifdef DEBUG
1165 run_command("mtd list", 0);
1166#endif
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001167 free(buf);
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001168
1169 return 0;
1170}
1171
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001172static int stm32prog_alt_add(struct stm32prog_data *data,
1173 struct dfu_entity *dfu,
1174 struct stm32prog_part_t *part)
1175{
1176 int ret = 0;
1177 int offset = 0;
1178 char devstr[10];
1179 char dfustr[10];
1180 char buf[ALT_BUF_LEN];
1181 u32 size;
1182 char multiplier, type;
1183
1184 /* max 3 digit for sector size */
1185 if (part->size > SZ_1M) {
1186 size = (u32)(part->size / SZ_1M);
1187 multiplier = 'M';
1188 } else if (part->size > SZ_1K) {
1189 size = (u32)(part->size / SZ_1K);
1190 multiplier = 'K';
1191 } else {
1192 size = (u32)part->size;
1193 multiplier = 'B';
1194 }
1195 if (IS_SELECT(part) && !IS_EMPTY(part))
1196 type = 'e'; /*Readable and Writeable*/
1197 else
1198 type = 'a';/*Readable*/
1199
1200 memset(buf, 0, sizeof(buf));
1201 offset = snprintf(buf, ALT_BUF_LEN - offset,
1202 "@%s/0x%02x/1*%d%c%c ",
1203 part->name, part->id,
1204 size, multiplier, type);
1205
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001206 if (part->target == STM32PROG_RAM) {
1207 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1208 "ram 0x%llx 0x%llx",
1209 part->addr, part->size);
1210 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001211 u64 dfu_size;
1212
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001213 if (part->dev->target == STM32PROG_MMC)
1214 dfu_size = part->size / part->dev->mmc->read_bl_len;
1215 else
1216 dfu_size = part->size;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001217 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1218 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay878f7542020-03-18 09:24:52 +01001219 } else if (part->part_id < 0) {
1220 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1221
1222 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1223 "raw 0x%llx 0x%llx",
1224 part->addr, nb_blk);
1225 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1226 " mmcpart %d;", -(part->part_id));
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001227 } else {
Patrick Delaunayeb845d62020-03-18 09:24:54 +01001228 if (part->part_type == PART_SYSTEM &&
1229 (part->target == STM32PROG_NAND ||
1230 part->target == STM32PROG_NOR ||
1231 part->target == STM32PROG_SPI_NAND))
1232 offset += snprintf(buf + offset,
1233 ALT_BUF_LEN - offset,
1234 "partubi");
1235 else
1236 offset += snprintf(buf + offset,
1237 ALT_BUF_LEN - offset,
1238 "part");
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001239 /* dev_id requested by DFU MMC */
1240 if (part->target == STM32PROG_MMC)
1241 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1242 " %d", part->dev_id);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001243 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1244 " %d;", part->part_id);
1245 }
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001246 ret = -ENODEV;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001247 switch (part->target) {
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001248 case STM32PROG_MMC:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001249 if (IS_ENABLED(CONFIG_MMC)) {
1250 ret = 0;
1251 sprintf(dfustr, "mmc");
1252 sprintf(devstr, "%d", part->dev_id);
1253 }
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001254 break;
Patrick Delaunayeb845d62020-03-18 09:24:54 +01001255 case STM32PROG_NAND:
1256 case STM32PROG_NOR:
1257 case STM32PROG_SPI_NAND:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001258 if (IS_ENABLED(CONFIG_MTD)) {
1259 ret = 0;
1260 sprintf(dfustr, "mtd");
1261 get_mtd_by_target(devstr, part->target, part->dev_id);
1262 }
Patrick Delaunayeb845d62020-03-18 09:24:54 +01001263 break;
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001264 case STM32PROG_RAM:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001265 ret = 0;
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001266 sprintf(dfustr, "ram");
1267 sprintf(devstr, "0");
1268 break;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001269 default:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001270 break;
1271 }
1272 if (ret) {
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001273 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001274 return ret;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001275 }
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001276 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001277 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001278 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1279 dfustr, devstr, buf, ret);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001280
1281 return ret;
1282}
1283
1284static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1285 char *name, int phase, int size)
1286{
1287 int ret = 0;
1288 char devstr[4];
1289 char buf[ALT_BUF_LEN];
1290
1291 sprintf(devstr, "%d", phase);
1292 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1293 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001294 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001295
1296 return ret;
1297}
1298
1299static int dfu_init_entities(struct stm32prog_data *data)
1300{
1301 int ret = 0;
1302 int phase, i, alt_id;
1303 struct stm32prog_part_t *part;
1304 struct dfu_entity *dfu;
1305 int alt_nb;
1306
Patrick Delaunay72450b42022-03-28 19:25:27 +02001307 alt_nb = 1; /* number of virtual = CMD*/
1308 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP))
1309 alt_nb++; /* OTP*/
Patrick Delaunayb57efb22021-05-18 15:12:09 +02001310 if (CONFIG_IS_ENABLED(DM_PMIC))
1311 alt_nb++; /* PMIC NVMEM*/
1312
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001313 if (data->part_nb == 0)
1314 alt_nb++; /* +1 for FlashLayout */
1315 else
1316 for (i = 0; i < data->part_nb; i++) {
1317 if (data->part_array[i].target != STM32PROG_NONE)
1318 alt_nb++;
1319 }
1320
1321 if (dfu_alt_init(alt_nb, &dfu))
1322 return -ENODEV;
1323
1324 puts("DFU alt info setting: ");
1325 if (data->part_nb) {
1326 alt_id = 0;
1327 for (phase = 1;
1328 (phase <= PHASE_LAST_USER) &&
1329 (alt_id < alt_nb) && !ret;
1330 phase++) {
1331 /* ordering alt setting by phase id */
1332 part = NULL;
1333 for (i = 0; i < data->part_nb; i++) {
1334 if (phase == data->part_array[i].id) {
1335 part = &data->part_array[i];
1336 break;
1337 }
1338 }
1339 if (!part)
1340 continue;
1341 if (part->target == STM32PROG_NONE)
1342 continue;
1343 part->alt_id = alt_id;
1344 alt_id++;
1345
1346 ret = stm32prog_alt_add(data, dfu, part);
1347 }
1348 } else {
1349 char buf[ALT_BUF_LEN];
1350
1351 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
1352 PHASE_FLASHLAYOUT, STM32_DDR_BASE);
1353 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001354 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001355 }
1356
1357 if (!ret)
Patrick Delaunayb18c3ab2021-07-05 09:39:01 +02001358 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001359
Patrick Delaunay72450b42022-03-28 19:25:27 +02001360 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001361 ret = optee_ta_open(data);
1362 log_debug("optee_ta result %d\n", ret);
1363 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP,
1364 data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC);
1365 }
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001366
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001367 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunayb18c3ab2021-07-05 09:39:01 +02001368 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001369
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001370 if (ret)
1371 stm32prog_err("dfu init failed: %d", ret);
1372 puts("done\n");
1373
1374#ifdef DEBUG
1375 dfu_show_entities();
1376#endif
1377 return ret;
1378}
1379
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001380int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1381 long *size)
1382{
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001383 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001384 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001385
Patrick Delaunay72450b42022-03-28 19:25:27 +02001386 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1387 stm32prog_err("OTP update not supported");
1388
1389 return -EOPNOTSUPP;
1390 }
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001391
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001392 if (!data->otp_part) {
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001393 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001394 if (!data->otp_part)
1395 return -ENOMEM;
1396 }
1397
1398 if (!offset)
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001399 memset(data->otp_part, 0, otp_size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001400
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001401 if (offset + *size > otp_size)
1402 *size = otp_size - offset;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001403
1404 memcpy((void *)((u32)data->otp_part + offset), buffer, *size);
1405
1406 return 0;
1407}
1408
1409int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1410 long *size)
1411{
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001412 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001413 int result = 0;
1414
Patrick Delaunay72450b42022-03-28 19:25:27 +02001415 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001416 stm32prog_err("OTP update not supported");
1417
Patrick Delaunay72450b42022-03-28 19:25:27 +02001418 return -EOPNOTSUPP;
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001419 }
1420
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001421 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001422 /* alway read for first packet */
1423 if (!offset) {
1424 if (!data->otp_part)
1425 data->otp_part =
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001426 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001427
1428 if (!data->otp_part) {
1429 result = -ENOMEM;
1430 goto end_otp_read;
1431 }
1432
1433 /* init struct with 0 */
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001434 memset(data->otp_part, 0, otp_size);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001435
1436 /* call the service */
Patrick Delaunay72450b42022-03-28 19:25:27 +02001437 result = -EOPNOTSUPP;
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001438 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1439 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1440 data->otp_part, OTP_SIZE_TA);
1441 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay72450b42022-03-28 19:25:27 +02001442 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
1443 (u32)data->otp_part, 0);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001444 if (result)
1445 goto end_otp_read;
1446 }
1447
1448 if (!data->otp_part) {
1449 result = -ENOMEM;
1450 goto end_otp_read;
1451 }
1452
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001453 if (offset + *size > otp_size)
1454 *size = otp_size - offset;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001455 memcpy(buffer, (void *)((u32)data->otp_part + offset), *size);
1456
1457end_otp_read:
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001458 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001459
1460 return result;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001461}
1462
1463int stm32prog_otp_start(struct stm32prog_data *data)
1464{
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001465 int result = 0;
1466 struct arm_smccc_res res;
1467
Patrick Delaunay72450b42022-03-28 19:25:27 +02001468 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001469 stm32prog_err("OTP update not supported");
1470
Patrick Delaunay72450b42022-03-28 19:25:27 +02001471 return -EOPNOTSUPP;
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001472 }
1473
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001474 if (!data->otp_part) {
1475 stm32prog_err("start OTP without data");
1476 return -1;
1477 }
1478
Patrick Delaunay72450b42022-03-28 19:25:27 +02001479 result = -EOPNOTSUPP;
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001480 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1481 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1482 data->otp_part, OTP_SIZE_TA);
1483 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay72450b42022-03-28 19:25:27 +02001484 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
1485 (u32)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001486
Patrick Delaunay72450b42022-03-28 19:25:27 +02001487 if (!res.a0) {
1488 switch (res.a1) {
1489 case 0:
1490 result = 0;
1491 break;
1492 case 1:
1493 stm32prog_err("Provisioning");
1494 result = 0;
1495 break;
1496 default:
1497 log_err("%s: OTP incorrect value (err = %ld)\n",
1498 __func__, res.a1);
1499 result = -EINVAL;
1500 break;
1501 }
1502 } else {
1503 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1504 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001505 result = -EINVAL;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001506 }
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001507 }
1508
1509 free(data->otp_part);
1510 data->otp_part = NULL;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001511 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001512
1513 return result;
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001514}
1515
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001516int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1517 long *size)
1518{
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001519 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001520
1521 if (!offset)
1522 memset(data->pmic_part, 0, PMIC_SIZE);
1523
1524 if (offset + *size > PMIC_SIZE)
1525 *size = PMIC_SIZE - offset;
1526
1527 memcpy(&data->pmic_part[offset], buffer, *size);
1528
1529 return 0;
1530}
1531
1532int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1533 long *size)
1534{
1535 int result = 0, ret;
1536 struct udevice *dev;
1537
1538 if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) {
1539 stm32prog_err("PMIC update not supported");
1540
1541 return -EOPNOTSUPP;
1542 }
1543
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001544 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001545 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -07001546 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001547 &dev);
1548 if (ret)
1549 return ret;
1550
1551 /* alway request PMIC for first packet */
1552 if (!offset) {
1553 /* init struct with 0 */
1554 memset(data->pmic_part, 0, PMIC_SIZE);
1555
1556 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -07001557 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001558 &dev);
1559 if (ret)
1560 return ret;
1561
1562 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1563 if (ret < 0) {
1564 result = ret;
1565 goto end_pmic_read;
1566 }
1567 if (ret != PMIC_SIZE) {
1568 result = -EACCES;
1569 goto end_pmic_read;
1570 }
1571 }
1572
1573 if (offset + *size > PMIC_SIZE)
1574 *size = PMIC_SIZE - offset;
1575
1576 memcpy(buffer, &data->pmic_part[offset], *size);
1577
1578end_pmic_read:
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001579 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001580 return result;
1581}
1582
1583int stm32prog_pmic_start(struct stm32prog_data *data)
1584{
1585 int ret;
1586 struct udevice *dev;
1587
1588 if (!CONFIG_IS_ENABLED(PMIC_STPMIC1)) {
1589 stm32prog_err("PMIC update not supported");
1590
1591 return -EOPNOTSUPP;
1592 }
1593
1594 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -07001595 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001596 &dev);
1597 if (ret)
1598 return ret;
1599
1600 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1601}
1602
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001603/* copy FSBL on NAND to improve reliability on NAND */
1604static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1605{
1606 int ret, i;
1607 void *fsbl;
1608 struct image_header_s header;
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +02001609 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001610 struct dfu_entity *dfu;
1611 long size, offset;
1612
1613 if (part->target != STM32PROG_NAND &&
1614 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +02001615 return -EINVAL;
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001616
1617 dfu = dfu_get_entity(part->alt_id);
1618
1619 /* read header */
1620 dfu_transaction_cleanup(dfu);
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +02001621 size = sizeof(raw_header);
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001622 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1623 if (ret)
1624 return ret;
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +02001625
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +02001626 stm32prog_header_check((ulong)&raw_header, &header);
1627 if (header.type != HEADER_STM32IMAGE &&
1628 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay4fb7b3e2021-04-02 14:05:17 +02001629 return -ENOENT;
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001630
1631 /* read header + payload */
Patrick Delaunay49d0ecb12022-03-28 19:25:29 +02001632 size = header.image_length + header.length;
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001633 size = round_up(size, part->dev->mtd->erasesize);
1634 fsbl = calloc(1, size);
1635 if (!fsbl)
1636 return -ENOMEM;
1637 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001638 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001639 if (ret)
1640 goto error;
1641
1642 dfu_transaction_cleanup(dfu);
1643 offset = 0;
1644 for (i = part->bin_nb - 1; i > 0; i--) {
1645 offset += size;
1646 /* write to the next erase block */
1647 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001648 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1649 __func__, offset, size, ret);
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001650 if (ret)
1651 goto error;
1652 }
1653
1654error:
1655 free(fsbl);
1656 return ret;
1657}
1658
Patrick Delaunayd68e53b2021-05-18 15:12:06 +02001659static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001660{
1661 if (data->phase == PHASE_FLASHLAYOUT) {
1662 if (parse_flash_layout(data, STM32_DDR_BASE, 0))
1663 stm32prog_err("Layout: invalid FlashLayout");
1664 return;
1665 }
1666
1667 if (!data->cur_part)
1668 return;
Patrick Delaunay878f7542020-03-18 09:24:52 +01001669
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001670 if (data->cur_part->target == STM32PROG_RAM) {
1671 if (data->cur_part->part_type == PART_SYSTEM)
1672 data->uimage = data->cur_part->addr;
1673 if (data->cur_part->part_type == PART_FILESYSTEM)
1674 data->dtb = data->cur_part->addr;
Patrick Delaunayd68e53b2021-05-18 15:12:06 +02001675 if (data->cur_part->part_type == PART_BINARY) {
1676 data->initrd = data->cur_part->addr;
1677 data->initrd_size = offset;
1678 }
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001679 }
1680
Patrick Delaunay878f7542020-03-18 09:24:52 +01001681 if (CONFIG_IS_ENABLED(MMC) &&
1682 data->cur_part->part_id < 0) {
1683 char cmdbuf[60];
1684
1685 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1686 data->cur_part->dev_id, data->cur_part->dev_id,
1687 -(data->cur_part->part_id));
1688 if (run_command(cmdbuf, 0)) {
1689 stm32prog_err("commands '%s' failed", cmdbuf);
1690 return;
1691 }
1692 }
Patrick Delaunayb83caf92020-03-18 09:24:56 +01001693
1694 if (CONFIG_IS_ENABLED(MTD) &&
1695 data->cur_part->bin_nb > 1) {
1696 if (stm32prog_copy_fsbl(data->cur_part)) {
1697 stm32prog_err("%s (0x%x): copy of fsbl failed",
1698 data->cur_part->name, data->cur_part->id);
1699 return;
1700 }
1701 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001702}
1703
1704void stm32prog_do_reset(struct stm32prog_data *data)
1705{
1706 if (data->phase == PHASE_RESET) {
1707 data->phase = PHASE_DO_RESET;
1708 puts("Reset requested\n");
1709 }
1710}
1711
1712void stm32prog_next_phase(struct stm32prog_data *data)
1713{
1714 int phase, i;
1715 struct stm32prog_part_t *part;
1716 bool found;
1717
1718 phase = data->phase;
1719 switch (phase) {
1720 case PHASE_RESET:
1721 case PHASE_END:
1722 case PHASE_DO_RESET:
1723 return;
1724 }
1725
1726 /* found next selected partition */
Patrick Delaunay468f0502020-03-18 09:25:00 +01001727 data->dfu_seq = 0;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001728 data->cur_part = NULL;
1729 data->phase = PHASE_END;
1730 found = false;
1731 do {
1732 phase++;
1733 if (phase > PHASE_LAST_USER)
1734 break;
1735 for (i = 0; i < data->part_nb; i++) {
1736 part = &data->part_array[i];
1737 if (part->id == phase) {
1738 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1739 data->cur_part = part;
1740 data->phase = phase;
1741 found = true;
1742 }
1743 break;
1744 }
1745 }
1746 } while (!found);
1747
1748 if (data->phase == PHASE_END)
1749 puts("Phase=END\n");
1750}
1751
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001752static int part_delete(struct stm32prog_data *data,
1753 struct stm32prog_part_t *part)
1754{
1755 int ret = 0;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001756 unsigned long blks, blks_offset, blks_size;
1757 struct blk_desc *block_dev = NULL;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001758 char cmdbuf[40];
1759 char devstr[10];
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001760
1761 printf("Erasing %s ", part->name);
1762 switch (part->target) {
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001763 case STM32PROG_MMC:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001764 if (!IS_ENABLED(CONFIG_MMC)) {
1765 ret = -1;
1766 stm32prog_err("%s (0x%x): erase invalid",
1767 part->name, part->id);
1768 break;
1769 }
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001770 printf("on mmc %d: ", part->dev->dev_id);
1771 block_dev = mmc_get_blk_desc(part->dev->mmc);
1772 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1773 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1774 /* -1 or -2 : delete boot partition of MMC
1775 * need to switch to associated hwpart 1 or 2
1776 */
1777 if (part->part_id < 0)
1778 if (blk_select_hwpart_devnum(IF_TYPE_MMC,
1779 part->dev->dev_id,
1780 -part->part_id))
1781 return -1;
1782
1783 blks = blk_derase(block_dev, blks_offset, blks_size);
1784
1785 /* return to user partition */
1786 if (part->part_id < 0)
1787 blk_select_hwpart_devnum(IF_TYPE_MMC,
1788 part->dev->dev_id, 0);
1789 if (blks != blks_size) {
1790 ret = -1;
1791 stm32prog_err("%s (0x%x): MMC erase failed",
1792 part->name, part->id);
1793 }
1794 break;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001795 case STM32PROG_NOR:
1796 case STM32PROG_NAND:
1797 case STM32PROG_SPI_NAND:
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001798 if (!IS_ENABLED(CONFIG_MTD)) {
1799 ret = -1;
1800 stm32prog_err("%s (0x%x): erase invalid",
1801 part->name, part->id);
1802 break;
1803 }
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001804 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1805 printf("on %s: ", devstr);
1806 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1807 devstr, part->addr, part->size);
1808 if (run_command(cmdbuf, 0)) {
1809 ret = -1;
1810 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1811 part->name, part->id, cmdbuf);
1812 }
1813 break;
Patrick Delaunay306a5cf2020-03-18 09:25:03 +01001814 case STM32PROG_RAM:
1815 printf("on ram: ");
1816 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1817 break;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001818 default:
1819 ret = -1;
1820 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1821 break;
1822 }
1823 if (!ret)
1824 printf("done\n");
1825
1826 return ret;
1827}
1828
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001829static void stm32prog_devices_init(struct stm32prog_data *data)
1830{
1831 int i;
1832 int ret;
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001833 struct stm32prog_part_t *part;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001834
1835 ret = treat_partition_list(data);
1836 if (ret)
1837 goto error;
1838
1839 /* initialize the selected device */
1840 for (i = 0; i < data->dev_nb; i++) {
1841 ret = init_device(data, &data->dev[i]);
1842 if (ret)
1843 goto error;
1844 }
1845
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001846 /* delete RAW partition before create partition */
1847 for (i = 0; i < data->part_nb; i++) {
1848 part = &data->part_array[i];
1849
1850 if (part->part_type != RAW_IMAGE)
1851 continue;
1852
1853 if (!IS_SELECT(part) || !IS_DELETE(part))
1854 continue;
1855
1856 ret = part_delete(data, part);
1857 if (ret)
1858 goto error;
1859 }
1860
Patrick Delaunay9a2b0542020-07-31 16:31:52 +02001861 if (IS_ENABLED(CONFIG_MMC)) {
1862 ret = create_gpt_partitions(data);
1863 if (ret)
1864 goto error;
1865 }
Patrick Delaunayaff4c5d2020-03-18 09:24:51 +01001866
Patrick Delaunay2bad5d02020-03-18 09:24:57 +01001867 /* delete partition GPT or MTD */
1868 for (i = 0; i < data->part_nb; i++) {
1869 part = &data->part_array[i];
1870
1871 if (part->part_type == RAW_IMAGE)
1872 continue;
1873
1874 if (!IS_SELECT(part) || !IS_DELETE(part))
1875 continue;
1876
1877 ret = part_delete(data, part);
1878 if (ret)
1879 goto error;
1880 }
1881
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001882 return;
1883
1884error:
1885 data->part_nb = 0;
1886}
1887
1888int stm32prog_dfu_init(struct stm32prog_data *data)
1889{
1890 /* init device if no error */
1891 if (data->part_nb)
1892 stm32prog_devices_init(data);
1893
1894 if (data->part_nb)
1895 stm32prog_next_phase(data);
1896
1897 /* prepare DFU for device read/write */
1898 dfu_free_entities();
1899 return dfu_init_entities(data);
1900}
1901
1902int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size)
1903{
1904 memset(data, 0x0, sizeof(*data));
Patrick Delaunay468f0502020-03-18 09:25:00 +01001905 data->read_phase = PHASE_RESET;
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001906 data->phase = PHASE_FLASHLAYOUT;
1907
1908 return parse_flash_layout(data, addr, size);
1909}
1910
1911void stm32prog_clean(struct stm32prog_data *data)
1912{
1913 /* clean */
1914 dfu_free_entities();
1915 free(data->part_array);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001916 free(data->otp_part);
Patrick Delaunay468f0502020-03-18 09:25:00 +01001917 free(data->buffer);
Patrick Delaunaye82ab4c2022-03-28 19:25:28 +02001918
1919 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1920 tee_close_session(data->tee, data->tee_session);
1921 data->tee = NULL;
1922 data->tee_session = 0x0;
1923 }
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001924}
1925
1926/* DFU callback: used after serial and direct DFU USB access */
1927void dfu_flush_callback(struct dfu_entity *dfu)
1928{
1929 if (!stm32prog_data)
1930 return;
1931
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001932 if (dfu->dev_type == DFU_DEV_VIRT) {
1933 if (dfu->data.virt.dev_num == PHASE_OTP)
1934 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay6ce1f4a2020-03-18 09:24:59 +01001935 else if (dfu->data.virt.dev_num == PHASE_PMIC)
1936 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay936f1ae2020-03-18 09:24:58 +01001937 return;
1938 }
1939
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001940 if (dfu->dev_type == DFU_DEV_RAM) {
1941 if (dfu->alt == 0 &&
1942 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayd68e53b2021-05-18 15:12:06 +02001943 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001944 /* waiting DFU DETACH for reenumeration */
1945 }
1946 }
1947
1948 if (!stm32prog_data->cur_part)
1949 return;
1950
1951 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayd68e53b2021-05-18 15:12:06 +02001952 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001953 stm32prog_next_phase(stm32prog_data);
1954 }
1955}
1956
1957void dfu_initiated_callback(struct dfu_entity *dfu)
1958{
1959 if (!stm32prog_data)
1960 return;
1961
1962 if (!stm32prog_data->cur_part)
1963 return;
1964
1965 /* force the saved offset for the current partition */
1966 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
1967 dfu->offset = stm32prog_data->offset;
Patrick Delaunay468f0502020-03-18 09:25:00 +01001968 stm32prog_data->dfu_seq = 0;
Patrick Delaunay711b5bc2020-11-06 19:01:30 +01001969 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay954bd1a2020-03-18 09:24:49 +01001970 }
1971}
Patrick Delaunay1f2e3d52021-05-18 15:12:13 +02001972
1973void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
1974{
1975 struct stm32prog_data *data = stm32prog_data;
1976
1977 if (!stm32prog_data)
1978 return;
1979
1980 if (!stm32prog_data->cur_part)
1981 return;
1982
1983 if (dfu->alt == stm32prog_data->cur_part->alt_id)
1984 stm32prog_err(msg);
1985}