blob: bddd7a4462c5e8d2ee8f3fb9250de8bff1b011a5 [file] [log] [blame]
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4 */
5
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01007#include <console.h>
8#include <dfu.h>
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02009#include <image.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010010#include <malloc.h>
Patrick Delaunay541c7de2020-03-18 09:24:59 +010011#include <misc.h>
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010012#include <mmc.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010013#include <part.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020014#include <tee.h>
Patrick Delaunay1d96b182020-03-18 09:24:58 +010015#include <asm/arch/stm32mp1_smc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Patrick Delaunay8da5df92022-03-28 19:25:28 +020017#include <dm/device_compat.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010018#include <dm/uclass.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010019#include <jffs2/load_kernel.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010020#include <linux/list.h>
21#include <linux/list_sort.h>
Patrick Delaunay6ab74962020-03-18 09:24:54 +010022#include <linux/mtd/mtd.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010023#include <linux/sizes.h>
24
25#include "stm32prog.h"
26
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010027/* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */
28#define GPT_HEADER_SZ 34
29
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010030#define OPT_SELECT BIT(0)
31#define OPT_EMPTY BIT(1)
Patrick Delaunay291e7222020-03-18 09:24:57 +010032#define OPT_DELETE BIT(2)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010033
34#define IS_SELECT(part) ((part)->option & OPT_SELECT)
35#define IS_EMPTY(part) ((part)->option & OPT_EMPTY)
Patrick Delaunay291e7222020-03-18 09:24:57 +010036#define IS_DELETE(part) ((part)->option & OPT_DELETE)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010037
38#define ALT_BUF_LEN SZ_1K
39
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010040#define ROOTFS_MMC0_UUID \
41 EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \
42 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82)
43
44#define ROOTFS_MMC1_UUID \
45 EFI_GUID(0x491F6117, 0x415D, 0x4F53, \
46 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6)
47
48#define ROOTFS_MMC2_UUID \
49 EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \
50 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18)
51
Patrick Delaunay33029a52022-03-28 19:25:26 +020052/* RAW partition (binary / bootloader) used Linux - reserved UUID */
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010053#define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908"
54
55/*
56 * unique partition guid (uuid) for partition named "rootfs"
57 * on each MMC instance = SD Card or eMMC
58 * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..."
59 */
60static const efi_guid_t uuid_mmc[3] = {
61 ROOTFS_MMC0_UUID,
62 ROOTFS_MMC1_UUID,
63 ROOTFS_MMC2_UUID
64};
65
Patrick Delaunay8dc57682022-03-28 19:25:30 +020066/* FIP type partition UUID used by TF-A*/
67#define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5"
68
69/* unique partition guid (uuid) for FIP partitions A/B */
70#define FIP_A_UUID \
71 EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \
72 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87)
73
74#define FIP_B_UUID \
75 EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \
76 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3)
77
78static const char * const fip_part_name[] = {
79 "fip-a",
80 "fip-b"
81};
82
83static const efi_guid_t fip_part_uuid[] = {
84 FIP_A_UUID,
85 FIP_B_UUID
86};
87
Patrick Delaunay526f66c2020-03-18 09:24:50 +010088/* order of column in flash layout file */
89enum stm32prog_col_t {
90 COL_OPTION,
91 COL_ID,
92 COL_NAME,
93 COL_TYPE,
94 COL_IP,
95 COL_OFFSET,
96 COL_NB_STM32
97};
98
Patrick Delaunay19676ef2021-04-02 14:05:17 +020099#define FIP_TOC_HEADER_NAME 0xAA640001
100
101struct fip_toc_header {
102 u32 name;
103 u32 serial_number;
104 u64 flags;
105};
106
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200107#define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \
108 { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } }
109
110/*
111 * Read NVMEM memory for STM32CubeProgrammer
112 *
113 * [in] value[0].a: Type (0 for OTP access)
114 * [out] memref[1].buffer Output buffer to return all read values
115 * [out] memref[1].size Size of buffer to be read
116 *
117 * Return codes:
118 * TEE_SUCCESS - Invoke command success
119 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
120 */
121#define TA_NVMEM_READ 0x0
122
123/*
124 * Write NVMEM memory for STM32CubeProgrammer
125 *
126 * [in] value[0].a Type (0 for OTP access)
127 * [in] memref[1].buffer Input buffer with the values to write
128 * [in] memref[1].size Size of buffer to be written
129 *
130 * Return codes:
131 * TEE_SUCCESS - Invoke command success
132 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
133 */
134#define TA_NVMEM_WRITE 0x1
135
136/* value of TA_NVMEM type = value[in] a */
137#define NVMEM_OTP 0
138
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200139DECLARE_GLOBAL_DATA_PTR;
140
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200141/* OPTEE TA NVMEM open helper */
142static int optee_ta_open(struct stm32prog_data *data)
143{
144 const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID;
145 struct tee_open_session_arg arg;
146 struct udevice *tee = NULL;
147 int rc;
148
149 if (data->tee)
150 return 0;
151
152 tee = tee_find_device(NULL, NULL, NULL, NULL);
153 if (!tee)
154 return -ENODEV;
155
156 memset(&arg, 0, sizeof(arg));
157 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
158 rc = tee_open_session(tee, &arg, 0, NULL);
159 if (rc < 0)
160 return -ENODEV;
161
162 data->tee = tee;
163 data->tee_session = arg.session;
164
165 return 0;
166}
167
168/* OPTEE TA NVMEM invoke helper */
169static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type,
170 void *buff, ulong size)
171{
172 struct tee_invoke_arg arg;
173 struct tee_param param[2];
174 struct tee_shm *buff_shm;
175 int rc;
176
177 rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm);
178 if (rc)
179 return rc;
180
181 memset(&arg, 0, sizeof(arg));
182 arg.func = cmd;
183 arg.session = data->tee_session;
184
185 memset(param, 0, sizeof(param));
186 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
187 param[0].u.value.a = type;
188
189 if (cmd == TA_NVMEM_WRITE)
190 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
191 else
192 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
193
194 param[1].u.memref.shm = buff_shm;
195 param[1].u.memref.size = size;
196
197 rc = tee_invoke_func(data->tee, &arg, 2, param);
198 if (rc < 0 || arg.ret != 0) {
199 dev_err(data->tee,
200 "TA_NVMEM invoke failed TEE err: %x, err:%x\n",
201 arg.ret, rc);
202 if (!rc)
203 rc = -EIO;
204 }
205
206 tee_shm_free(buff_shm);
207
208 return rc;
209}
210
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100211char *stm32prog_get_error(struct stm32prog_data *data)
212{
213 static const char error_msg[] = "Unspecified";
214
215 if (strlen(data->error) == 0)
216 strcpy(data->error, error_msg);
217
218 return data->error;
219}
220
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200221static bool stm32prog_is_fip_header(struct fip_toc_header *header)
222{
223 return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
224}
225
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200226static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100227{
228 unsigned int i;
229
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200230 if (header->magic_number !=
231 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
232 log_debug("%s:invalid magic number : 0x%x\n",
233 __func__, header->magic_number);
234 return false;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200235 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200236 if (header->header_version != 0x00010000) {
237 log_debug("%s:invalid header version : 0x%x\n",
238 __func__, header->header_version);
239 return false;
240 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100241
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200242 if (header->reserved1 || header->reserved2) {
243 log_debug("%s:invalid reserved field\n", __func__);
244 return false;
245 }
246 for (i = 0; i < sizeof(header->padding); i++) {
247 if (header->padding[i] != 0) {
248 log_debug("%s:invalid padding field\n", __func__);
249 return false;
250 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100251 }
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200252
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200253 return true;
254}
255
256static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
257{
258 unsigned int i;
259
260 if (header->magic_number !=
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100261 (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100262 log_debug("%s:invalid magic number : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200263 __func__, header->magic_number);
264 return false;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100265 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200266 if (header->header_version != 0x00020000) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100267 log_debug("%s:invalid header version : 0x%x\n",
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200268 __func__, header->header_version);
269 return false;
270 }
271 if (header->reserved1 || header->reserved2)
272 return false;
273
274 for (i = 0; i < sizeof(header->padding); i++) {
275 if (header->padding[i] != 0) {
276 log_debug("%s:invalid padding field\n", __func__);
277 return false;
278 }
279 }
280
281 return true;
282}
283
284void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
285{
286 struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
287 struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
288
289 if (!raw_header || !header) {
290 log_debug("%s:no header data\n", __func__);
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200291 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100292 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200293
294 if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
295 header->type = HEADER_FIP;
296 header->length = 0;
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200297 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100298 }
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200299 if (stm32prog_is_stm32_header_v1(v1_header)) {
300 header->type = HEADER_STM32IMAGE;
301 header->image_checksum = le32_to_cpu(v1_header->image_checksum);
302 header->image_length = le32_to_cpu(v1_header->image_length);
303 header->length = sizeof(struct stm32_header_v1);
304 return;
305 }
306 if (stm32prog_is_stm32_header_v2(v2_header)) {
307 header->type = HEADER_STM32IMAGE_V2;
308 header->image_checksum = le32_to_cpu(v2_header->image_checksum);
309 header->image_length = le32_to_cpu(v2_header->image_length);
310 header->length = sizeof(struct stm32_header_v1) +
311 v2_header->extension_headers_length;
312 return;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100313 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100314
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200315 header->type = HEADER_NONE;
316 header->image_checksum = 0x0;
317 header->image_length = 0x0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100318}
319
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200320static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header)
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100321{
322 u32 i, checksum;
323 u8 *payload;
324
325 /* compute checksum on payload */
326 payload = (u8 *)addr;
327 checksum = 0;
328 for (i = header->image_length; i > 0; i--)
329 checksum += *(payload++);
330
331 return checksum;
332}
333
334/* FLASHLAYOUT PARSING *****************************************/
335static int parse_option(struct stm32prog_data *data,
336 int i, char *p, struct stm32prog_part_t *part)
337{
338 int result = 0;
339 char *c = p;
340
341 part->option = 0;
342 if (!strcmp(p, "-"))
343 return 0;
344
345 while (*c) {
346 switch (*c) {
347 case 'P':
348 part->option |= OPT_SELECT;
349 break;
350 case 'E':
351 part->option |= OPT_EMPTY;
352 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +0100353 case 'D':
354 part->option |= OPT_DELETE;
355 break;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100356 default:
357 result = -EINVAL;
358 stm32prog_err("Layout line %d: invalid option '%c' in %s)",
359 i, *c, p);
360 return -EINVAL;
361 }
362 c++;
363 }
364 if (!(part->option & OPT_SELECT)) {
365 stm32prog_err("Layout line %d: missing 'P' in option %s", i, p);
366 return -EINVAL;
367 }
368
369 return result;
370}
371
372static int parse_id(struct stm32prog_data *data,
373 int i, char *p, struct stm32prog_part_t *part)
374{
375 int result = 0;
376 unsigned long value;
377
378 result = strict_strtoul(p, 0, &value);
379 part->id = value;
380 if (result || value > PHASE_LAST_USER) {
381 stm32prog_err("Layout line %d: invalid phase value = %s", i, p);
382 result = -EINVAL;
383 }
384
385 return result;
386}
387
388static int parse_name(struct stm32prog_data *data,
389 int i, char *p, struct stm32prog_part_t *part)
390{
391 int result = 0;
392
393 if (strlen(p) < sizeof(part->name)) {
394 strcpy(part->name, p);
395 } else {
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200396 stm32prog_err("Layout line %d: partition name too long [%zd]: %s",
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100397 i, strlen(p), p);
398 result = -EINVAL;
399 }
400
401 return result;
402}
403
404static int parse_type(struct stm32prog_data *data,
405 int i, char *p, struct stm32prog_part_t *part)
406{
407 int result = 0;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100408 int len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100409
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100410 part->bin_nb = 0;
411 if (!strncmp(p, "Binary", 6)) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100412 part->part_type = PART_BINARY;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100413
414 /* search for Binary(X) case */
415 len = strlen(p);
416 part->bin_nb = 1;
417 if (len > 6) {
418 if (len < 8 ||
419 (p[6] != '(') ||
420 (p[len - 1] != ')'))
421 result = -EINVAL;
422 else
423 part->bin_nb =
Simon Glassff9b9032021-07-24 09:03:30 -0600424 dectoul(&p[7], NULL);
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100425 }
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200426 } else if (!strcmp(p, "FIP")) {
427 part->part_type = PART_FIP;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200428 } else if (!strcmp(p, "ENV")) {
429 part->part_type = PART_ENV;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100430 } else if (!strcmp(p, "System")) {
431 part->part_type = PART_SYSTEM;
432 } else if (!strcmp(p, "FileSystem")) {
433 part->part_type = PART_FILESYSTEM;
434 } else if (!strcmp(p, "RawImage")) {
435 part->part_type = RAW_IMAGE;
436 } else {
437 result = -EINVAL;
438 }
439 if (result)
440 stm32prog_err("Layout line %d: type parsing error : '%s'",
441 i, p);
442
443 return result;
444}
445
446static int parse_ip(struct stm32prog_data *data,
447 int i, char *p, struct stm32prog_part_t *part)
448{
449 int result = 0;
450 unsigned int len = 0;
451
452 part->dev_id = 0;
453 if (!strcmp(p, "none")) {
454 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100455 } else if (!strncmp(p, "mmc", 3)) {
456 part->target = STM32PROG_MMC;
457 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100458 } else if (!strncmp(p, "nor", 3)) {
459 part->target = STM32PROG_NOR;
460 len = 3;
461 } else if (!strncmp(p, "nand", 4)) {
462 part->target = STM32PROG_NAND;
463 len = 4;
464 } else if (!strncmp(p, "spi-nand", 8)) {
465 part->target = STM32PROG_SPI_NAND;
466 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100467 } else if (!strncmp(p, "ram", 3)) {
468 part->target = STM32PROG_RAM;
469 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100470 } else {
471 result = -EINVAL;
472 }
473 if (len) {
474 /* only one digit allowed for device id */
475 if (strlen(p) != len + 1) {
476 result = -EINVAL;
477 } else {
478 part->dev_id = p[len] - '0';
479 if (part->dev_id > 9)
480 result = -EINVAL;
481 }
482 }
483 if (result)
484 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
485
486 return result;
487}
488
489static int parse_offset(struct stm32prog_data *data,
490 int i, char *p, struct stm32prog_part_t *part)
491{
492 int result = 0;
493 char *tail;
494
495 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100496 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100497 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100498 /* eMMC boot parttion */
499 if (!strncmp(p, "boot", 4)) {
500 if (strlen(p) != 5) {
501 result = -EINVAL;
502 } else {
503 if (p[4] == '1')
504 part->part_id = -1;
505 else if (p[4] == '2')
506 part->part_id = -2;
507 else
508 result = -EINVAL;
509 }
510 if (result)
511 stm32prog_err("Layout line %d: invalid part '%s'",
512 i, p);
513 } else {
514 part->addr = simple_strtoull(p, &tail, 0);
515 if (tail == p || *tail != '\0') {
516 stm32prog_err("Layout line %d: invalid offset '%s'",
517 i, p);
518 result = -EINVAL;
519 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100520 }
521
522 return result;
523}
524
525static
526int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
527 struct stm32prog_part_t *part) = {
528 [COL_OPTION] = parse_option,
529 [COL_ID] = parse_id,
530 [COL_NAME] = parse_name,
531 [COL_TYPE] = parse_type,
532 [COL_IP] = parse_ip,
533 [COL_OFFSET] = parse_offset,
534};
535
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100536static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200537 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100538 ulong size)
539{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100540 int column = 0, part_nb = 0, ret;
541 bool end_of_line, eof;
542 char *p, *start, *last, *col;
543 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200544 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100545 int part_list_size;
546 int i;
547
548 data->part_nb = 0;
549
550 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200551 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200552 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100553 u32 checksum;
554
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200555 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200556 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100557
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200558 checksum = stm32prog_header_checksum(addr, &header);
559 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100560 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200561 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100562 return -EIO;
563 }
564 }
565 if (!size)
566 return -EINVAL;
567
568 start = (char *)addr;
569 last = start + size;
570
571 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100572 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100573
574 /* calculate expected number of partitions */
575 part_list_size = 1;
576 p = start;
577 while (*p && (p < last)) {
578 if (*p++ == '\n') {
579 part_list_size++;
580 if (p < last && *p == '#')
581 part_list_size--;
582 }
583 }
584 if (part_list_size > PHASE_LAST_USER) {
585 stm32prog_err("Layout: too many partition (%d)",
586 part_list_size);
587 return -1;
588 }
589 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
590 if (!part) {
591 stm32prog_err("Layout: alloc failed");
592 return -ENOMEM;
593 }
594 data->part_array = part;
595
596 /* main parsing loop */
597 i = 1;
598 eof = false;
599 p = start;
600 col = start; /* 1st column */
601 end_of_line = false;
602 while (!eof) {
603 switch (*p) {
604 /* CR is ignored and replaced by NULL character */
605 case '\r':
606 *p = '\0';
607 p++;
608 continue;
609 case '\0':
610 end_of_line = true;
611 eof = true;
612 break;
613 case '\n':
614 end_of_line = true;
615 break;
616 case '\t':
617 break;
618 case '#':
619 /* comment line is skipped */
620 if (column == 0 && p == col) {
621 while ((p < last) && *p)
622 if (*p++ == '\n')
623 break;
624 col = p;
625 i++;
626 if (p >= last || !*p) {
627 eof = true;
628 end_of_line = true;
629 }
630 continue;
631 }
632 /* fall through */
633 /* by default continue with the next character */
634 default:
635 p++;
636 continue;
637 }
638
639 /* replace by \0: allow string parsing for each column */
640 *p = '\0';
641 p++;
642 if (p >= last) {
643 eof = true;
644 end_of_line = true;
645 }
646
647 /* skip empty line and multiple TAB in tsv file */
648 if (strlen(col) == 0) {
649 col = p;
650 /* skip empty line */
651 if (column == 0 && end_of_line) {
652 end_of_line = false;
653 i++;
654 }
655 continue;
656 }
657
658 if (column < COL_NB_STM32) {
659 ret = parse[column](data, i, col, part);
660 if (ret)
661 return ret;
662 }
663
664 /* save the beginning of the next column */
665 column++;
666 col = p;
667
668 if (!end_of_line)
669 continue;
670
671 /* end of the line detected */
672 end_of_line = false;
673
674 if (column < COL_NB_STM32) {
675 stm32prog_err("Layout line %d: no enought column", i);
676 return -EINVAL;
677 }
678 column = 0;
679 part_nb++;
680 part++;
681 i++;
682 if (part_nb >= part_list_size) {
683 part = NULL;
684 if (!eof) {
685 stm32prog_err("Layout: no enought memory for %d part",
686 part_nb);
687 return -EINVAL;
688 }
689 }
690 }
691 data->part_nb = part_nb;
692 if (data->part_nb == 0) {
693 stm32prog_err("Layout: no partition found");
694 return -ENODEV;
695 }
696
697 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100698}
699
700static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
701{
702 struct stm32prog_part_t *parta, *partb;
703
704 parta = container_of(a, struct stm32prog_part_t, list);
705 partb = container_of(b, struct stm32prog_part_t, list);
706
Patrick Delaunay6915b492020-03-18 09:24:52 +0100707 if (parta->part_id != partb->part_id)
708 return parta->part_id - partb->part_id;
709 else
710 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100711}
712
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100713static void get_mtd_by_target(char *string, enum stm32prog_target target,
714 int dev_id)
715{
716 const char *dev_str;
717
718 switch (target) {
719 case STM32PROG_NOR:
720 dev_str = "nor";
721 break;
722 case STM32PROG_NAND:
723 dev_str = "nand";
724 break;
725 case STM32PROG_SPI_NAND:
726 dev_str = "spi-nand";
727 break;
728 default:
729 dev_str = "invalid";
730 break;
731 }
732 sprintf(string, "%s%d", dev_str, dev_id);
733}
734
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100735static int init_device(struct stm32prog_data *data,
736 struct stm32prog_dev_t *dev)
737{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100738 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100739 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100740 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200741 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100742 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100743 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100744 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100745 u64 first_addr = 0, last_addr = 0;
746 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100747 u64 part_addr, part_size;
748 bool part_found;
749 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200750 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100751
752 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100753 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200754 if (!IS_ENABLED(CONFIG_MMC)) {
755 stm32prog_err("unknown device type = %d", dev->target);
756 return -ENODEV;
757 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100758 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200759 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100760 stm32prog_err("mmc device %d not found", dev->dev_id);
761 return -ENODEV;
762 }
763 block_dev = mmc_get_blk_desc(mmc);
764 if (!block_dev) {
765 stm32prog_err("mmc device %d not probed", dev->dev_id);
766 return -ENODEV;
767 }
768 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
769 dev->mmc = mmc;
770
771 /* reserve a full erase group for each GTP headers */
772 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
773 first_addr = dev->erase_size;
774 last_addr = (u64)(block_dev->lba -
775 mmc->erase_grp_size) *
776 block_dev->blksz;
777 } else {
778 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
779 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
780 block_dev->blksz;
781 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100782 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
783 block_dev->lba, block_dev->blksz);
784 log_debug(" available address = 0x%llx..0x%llx\n",
785 first_addr, last_addr);
786 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100787 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100788 case STM32PROG_NOR:
789 case STM32PROG_NAND:
790 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200791 if (!IS_ENABLED(CONFIG_MTD)) {
792 stm32prog_err("unknown device type = %d", dev->target);
793 return -ENODEV;
794 }
Patrice Chotard49569082023-06-08 17:16:40 +0200795 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
796 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100797 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100798 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100799
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100800 mtd = get_mtd_device_nm(mtd_id);
801 if (IS_ERR(mtd)) {
802 stm32prog_err("MTD device %s not found", mtd_id);
803 return -ENODEV;
804 }
805 first_addr = 0;
806 last_addr = mtd->size;
807 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100808 log_debug("MTD device %s: size=%lld erasesize=%d\n",
809 mtd_id, mtd->size, mtd->erasesize);
810 log_debug(" available address = 0x%llx..0x%llx\n",
811 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100812 dev->mtd = mtd;
813 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100814 case STM32PROG_RAM:
815 first_addr = gd->bd->bi_dram[0].start;
816 last_addr = first_addr + gd->bd->bi_dram[0].size;
817 dev->erase_size = 1;
818 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100819 default:
820 stm32prog_err("unknown device type = %d", dev->target);
821 return -ENODEV;
822 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100823 log_debug(" erase size = 0x%x\n", dev->erase_size);
824 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100825
826 /* order partition list in offset order */
827 list_sort(NULL, &dev->part_list, &part_cmp);
828 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100829 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100830 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100831 if (part->bin_nb > 1) {
832 if ((dev->target != STM32PROG_NAND &&
833 dev->target != STM32PROG_SPI_NAND) ||
834 part->id >= PHASE_FIRST_USER ||
835 strncmp(part->name, "fsbl", 4)) {
836 stm32prog_err("%s (0x%x): multiple binary %d not supported",
837 part->name, part->id,
838 part->bin_nb);
839 return -EINVAL;
840 }
841 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100842 if (part->part_type == RAW_IMAGE) {
843 part->part_id = 0x0;
844 part->addr = 0x0;
845 if (block_dev)
846 part->size = block_dev->lba * block_dev->blksz;
847 else
848 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100849 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
850 part->option, part->id, part->name,
851 part->part_type, part->bin_nb, part->target,
852 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100853 continue;
854 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100855 if (part->part_id < 0) { /* boot hw partition for eMMC */
856 if (mmc) {
857 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100858 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100859 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100860 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100861 part->part_id);
862 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100863 }
864 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100865 part->part_id = part_id++;
866
867 /* last partition : size to the end of the device */
868 if (part->list.next != &dev->part_list) {
869 next_part =
870 container_of(part->list.next,
871 struct stm32prog_part_t,
872 list);
873 if (part->addr < next_part->addr) {
874 part->size = next_part->addr -
875 part->addr;
876 } else {
877 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
878 part->name, part->id,
879 part->addr,
880 next_part->name,
881 next_part->id,
882 next_part->addr);
883 return -EINVAL;
884 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100885 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100886 if (part->addr <= last_addr) {
887 part->size = last_addr - part->addr;
888 } else {
889 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
890 part->name, part->id,
891 part->addr, last_addr);
892 return -EINVAL;
893 }
894 }
895 if (part->addr < first_addr) {
896 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100897 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100898 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100899 return -EINVAL;
900 }
901 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100902 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
903 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
904 part->name, part->id, part->addr,
905 part->dev->erase_size);
906 return -EINVAL;
907 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100908 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
909 part->part_id, part->option, part->id, part->name,
910 part->part_type, part->bin_nb, part->target,
911 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100912
913 part_addr = 0;
914 part_size = 0;
915 part_found = false;
916
917 /* check coherency with existing partition */
918 if (block_dev) {
919 /*
920 * block devices with GPT: check user partition size
921 * only for partial update, the GPT partions are be
922 * created for full update
923 */
924 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100925 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100926 continue;
927 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600928 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100929
930 ret = part_get_info(block_dev, part->part_id,
931 &partinfo);
932
933 if (ret) {
934 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
935 part->name, part->id,
936 part_id, part->dev_id);
937 return -ENODEV;
938 }
939 part_addr = (u64)partinfo.start * partinfo.blksz;
940 part_size = (u64)partinfo.size * partinfo.blksz;
941 part_name = (char *)partinfo.name;
942 part_found = true;
943 }
944
Patrick Delaunay8040da12020-07-31 16:31:52 +0200945 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200946 i = 0;
947 list_for_each_entry(partition, &mtd->partitions, node) {
948 if ((part->part_id - 1) == i) {
949 part_found = true;
950 break;
951 }
952 i++;
953 }
954 if (part_found) {
955 part_addr = partition->offset;
956 part_size = partition->size;
957 part_name = partition->name;
958 } else {
959 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
960 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100961 return -ENODEV;
962 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100963 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200964
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200965 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100966 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100967 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100968 continue;
969 }
970
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100971 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100972
973 if (part->addr != part_addr) {
974 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
975 part->name, part->id, part->part_id,
976 part_name, part->addr, part_addr);
977 return -ENODEV;
978 }
979 if (part->size != part_size) {
980 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
981 part->name, part->id, part->part_id,
982 part_name, part->addr, part->size,
983 part_size);
984 return -ENODEV;
985 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100986 }
987 return 0;
988}
989
990static int treat_partition_list(struct stm32prog_data *data)
991{
992 int i, j;
993 struct stm32prog_part_t *part;
994
995 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
996 data->dev[j].target = STM32PROG_NONE;
997 INIT_LIST_HEAD(&data->dev[j].part_list);
998 }
999
Patrick Delaunayc5112242020-03-18 09:24:55 +01001000 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001001 for (i = 0; i < data->part_nb; i++) {
1002 part = &data->part_array[i];
1003 part->alt_id = -1;
1004
1005 /* skip partition with IP="none" */
1006 if (part->target == STM32PROG_NONE) {
1007 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001008 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1009 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001010 return -EINVAL;
1011 }
1012 continue;
1013 }
1014
1015 if (part->id == PHASE_FLASHLAYOUT ||
1016 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001017 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1018 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001019 return -EINVAL;
1020 }
1021 for (j = i + 1; j < data->part_nb; j++) {
1022 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001023 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1024 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001025 return -EINVAL;
1026 }
1027 }
1028 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1029 if (data->dev[j].target == STM32PROG_NONE) {
1030 /* new device found */
1031 data->dev[j].target = part->target;
1032 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001033 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001034 data->dev_nb++;
1035 break;
1036 } else if ((part->target == data->dev[j].target) &&
1037 (part->dev_id == data->dev[j].dev_id)) {
1038 break;
1039 }
1040 }
1041 if (j == STM32PROG_MAX_DEV) {
1042 stm32prog_err("Layout: too many device");
1043 return -EINVAL;
1044 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001045 switch (part->target) {
1046 case STM32PROG_NOR:
1047 if (!data->fsbl_nor_detected &&
1048 !strncmp(part->name, "fsbl", 4))
1049 data->fsbl_nor_detected = true;
1050 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001051 default:
1052 break;
1053 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001054 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001055 if (!IS_SELECT(part))
1056 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001057 list_add_tail(&part->list, &data->dev[j].part_list);
1058 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001059
1060 return 0;
1061}
1062
Patrick Delaunay8040da12020-07-31 16:31:52 +02001063static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001064{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001065 int offset = 0;
1066 const int buflen = SZ_8K;
1067 char *buf;
1068 char uuid[UUID_STR_LEN + 1];
1069 unsigned char *uuid_bin;
1070 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001071 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001072 bool rootfs_found;
1073 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001074 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001075
1076 buf = malloc(buflen);
1077 if (!buf)
1078 return -ENOMEM;
1079
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001080 /* initialize the selected device */
1081 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001082 /* create gpt partition support only for full update on MMC */
1083 if (data->dev[i].target != STM32PROG_MMC ||
1084 !data->dev[i].full_update)
1085 continue;
1086
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001087 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001088 offset = 0;
1089 rootfs_found = false;
1090 memset(buf, 0, buflen);
1091
1092 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001093 /* skip eMMC boot partitions */
1094 if (part->part_id < 0)
1095 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001096 /* skip Raw Image */
1097 if (part->part_type == RAW_IMAGE)
1098 continue;
1099
1100 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001101 log_debug("\n%s: buffer too small, %s skippped",
1102 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001103 continue;
1104 }
1105
1106 if (!offset)
1107 offset += sprintf(buf, "gpt write mmc %d \"",
1108 data->dev[i].dev_id);
1109
1110 offset += snprintf(buf + offset, buflen - offset,
1111 "name=%s,start=0x%llx,size=0x%llx",
1112 part->name,
1113 part->addr,
1114 part->size);
1115
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001116 switch (part->part_type) {
1117 case PART_BINARY:
1118 type_str = LINUX_RESERVED_UUID;
1119 break;
Patrick Delaunayb386b9c2023-06-08 17:09:54 +02001120 case PART_ENV:
1121 type_str = "u-boot-env";
1122 break;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001123 case PART_FIP:
1124 type_str = FIP_TYPE_UUID;
1125 break;
1126 default:
1127 type_str = "linux";
1128 break;
1129 }
1130 offset += snprintf(buf + offset,
1131 buflen - offset,
1132 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001133
1134 if (part->part_type == PART_SYSTEM)
1135 offset += snprintf(buf + offset,
1136 buflen - offset,
1137 ",bootable");
1138
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001139 /* partition UUID */
1140 uuid_bin = NULL;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001141 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1142 mmc_id = part->dev_id;
1143 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001144 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1145 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1146 }
1147 if (part->part_type == PART_FIP) {
1148 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1149 if (!strcmp(part->name, fip_part_name[j])) {
1150 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1151 break;
1152 }
1153 }
1154 if (uuid_bin) {
1155 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1156 offset += snprintf(buf + offset,
1157 buflen - offset,
1158 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001159 }
1160
1161 offset += snprintf(buf + offset, buflen - offset, ";");
1162 }
1163
1164 if (offset) {
1165 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001166 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001167 if (run_command(buf, 0)) {
1168 stm32prog_err("GPT partitionning fail: %s",
1169 buf);
1170 free(buf);
1171
1172 return -1;
1173 }
1174 }
1175
1176 if (data->dev[i].mmc)
1177 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1178
1179#ifdef DEBUG
1180 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001181 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001182 if (run_command(buf, 0))
1183 printf("fail !\n");
1184 else
1185 printf("OK\n");
1186
1187 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1188 run_command(buf, 0);
1189#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001190 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001191 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001192
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001193#ifdef DEBUG
1194 run_command("mtd list", 0);
1195#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001196 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001197
1198 return 0;
1199}
1200
1201static int stm32prog_alt_add(struct stm32prog_data *data,
1202 struct dfu_entity *dfu,
1203 struct stm32prog_part_t *part)
1204{
1205 int ret = 0;
1206 int offset = 0;
1207 char devstr[10];
1208 char dfustr[10];
1209 char buf[ALT_BUF_LEN];
1210 u32 size;
1211 char multiplier, type;
1212
1213 /* max 3 digit for sector size */
1214 if (part->size > SZ_1M) {
1215 size = (u32)(part->size / SZ_1M);
1216 multiplier = 'M';
1217 } else if (part->size > SZ_1K) {
1218 size = (u32)(part->size / SZ_1K);
1219 multiplier = 'K';
1220 } else {
1221 size = (u32)part->size;
1222 multiplier = 'B';
1223 }
1224 if (IS_SELECT(part) && !IS_EMPTY(part))
1225 type = 'e'; /*Readable and Writeable*/
1226 else
1227 type = 'a';/*Readable*/
1228
1229 memset(buf, 0, sizeof(buf));
1230 offset = snprintf(buf, ALT_BUF_LEN - offset,
1231 "@%s/0x%02x/1*%d%c%c ",
1232 part->name, part->id,
1233 size, multiplier, type);
1234
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001235 if (part->target == STM32PROG_RAM) {
1236 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1237 "ram 0x%llx 0x%llx",
1238 part->addr, part->size);
1239 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001240 u64 dfu_size;
1241
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001242 if (part->dev->target == STM32PROG_MMC)
1243 dfu_size = part->size / part->dev->mmc->read_bl_len;
1244 else
1245 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001246 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1247 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001248 } else if (part->part_id < 0) {
1249 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1250
1251 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1252 "raw 0x%llx 0x%llx",
1253 part->addr, nb_blk);
1254 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001255 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001256 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001257 if (part->part_type == PART_SYSTEM &&
1258 (part->target == STM32PROG_NAND ||
1259 part->target == STM32PROG_NOR ||
1260 part->target == STM32PROG_SPI_NAND))
1261 offset += snprintf(buf + offset,
1262 ALT_BUF_LEN - offset,
1263 "partubi");
1264 else
1265 offset += snprintf(buf + offset,
1266 ALT_BUF_LEN - offset,
1267 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001268 /* dev_id requested by DFU MMC */
1269 if (part->target == STM32PROG_MMC)
1270 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1271 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001272 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001273 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001274 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001275 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001276 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001277 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001278 if (IS_ENABLED(CONFIG_MMC)) {
1279 ret = 0;
1280 sprintf(dfustr, "mmc");
1281 sprintf(devstr, "%d", part->dev_id);
1282 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001283 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001284 case STM32PROG_NAND:
1285 case STM32PROG_NOR:
1286 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001287 if (IS_ENABLED(CONFIG_MTD)) {
1288 ret = 0;
1289 sprintf(dfustr, "mtd");
1290 get_mtd_by_target(devstr, part->target, part->dev_id);
1291 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001292 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001293 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001294 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001295 sprintf(dfustr, "ram");
1296 sprintf(devstr, "0");
1297 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001298 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001299 break;
1300 }
1301 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001302 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001303 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001304 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001305 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001306 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001307 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1308 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001309
1310 return ret;
1311}
1312
1313static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1314 char *name, int phase, int size)
1315{
1316 int ret = 0;
1317 char devstr[4];
1318 char buf[ALT_BUF_LEN];
1319
1320 sprintf(devstr, "%d", phase);
1321 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1322 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001323 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001324
1325 return ret;
1326}
1327
1328static int dfu_init_entities(struct stm32prog_data *data)
1329{
1330 int ret = 0;
1331 int phase, i, alt_id;
1332 struct stm32prog_part_t *part;
1333 struct dfu_entity *dfu;
1334 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001335 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001336
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001337 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001338
1339 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1340 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1341 otp_size = OTP_SIZE_SMC;
1342 /* check if PTA BSEC is supported */
1343 ret = optee_ta_open(data);
1344 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1345 if (!ret && data->tee)
1346 otp_size = OTP_SIZE_TA;
1347 if (otp_size)
1348 alt_nb++; /* OTP*/
1349 }
1350
Patrick Delaunay7d145402021-05-18 15:12:09 +02001351 if (CONFIG_IS_ENABLED(DM_PMIC))
1352 alt_nb++; /* PMIC NVMEM*/
1353
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001354 if (data->part_nb == 0)
1355 alt_nb++; /* +1 for FlashLayout */
1356 else
1357 for (i = 0; i < data->part_nb; i++) {
1358 if (data->part_array[i].target != STM32PROG_NONE)
1359 alt_nb++;
1360 }
1361
1362 if (dfu_alt_init(alt_nb, &dfu))
1363 return -ENODEV;
1364
1365 puts("DFU alt info setting: ");
1366 if (data->part_nb) {
1367 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001368 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001369 for (phase = 1;
1370 (phase <= PHASE_LAST_USER) &&
1371 (alt_id < alt_nb) && !ret;
1372 phase++) {
1373 /* ordering alt setting by phase id */
1374 part = NULL;
1375 for (i = 0; i < data->part_nb; i++) {
1376 if (phase == data->part_array[i].id) {
1377 part = &data->part_array[i];
1378 break;
1379 }
1380 }
1381 if (!part)
1382 continue;
1383 if (part->target == STM32PROG_NONE)
1384 continue;
1385 part->alt_id = alt_id;
1386 alt_id++;
1387
1388 ret = stm32prog_alt_add(data, dfu, part);
1389 }
1390 } else {
1391 char buf[ALT_BUF_LEN];
1392
1393 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001394 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001395 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001396 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001397 }
1398
1399 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001400 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001401
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001402 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1403 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001404
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001405 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001406 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001407
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001408 if (ret)
1409 stm32prog_err("dfu init failed: %d", ret);
1410 puts("done\n");
1411
1412#ifdef DEBUG
1413 dfu_show_entities();
1414#endif
1415 return ret;
1416}
1417
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001418int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1419 long *size)
1420{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001421 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001422 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001423
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001424 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1425 stm32prog_err("OTP update not supported");
1426
1427 return -EOPNOTSUPP;
1428 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001429
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001430 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001431 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001432 if (!data->otp_part)
1433 return -ENOMEM;
1434 }
1435
1436 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001437 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001438
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001439 if (offset + *size > otp_size)
1440 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001441
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001442 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001443
1444 return 0;
1445}
1446
1447int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1448 long *size)
1449{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001450 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001451 int result = 0;
1452
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001453 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001454 stm32prog_err("OTP update not supported");
1455
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001456 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001457 }
1458
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001459 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001460 /* alway read for first packet */
1461 if (!offset) {
1462 if (!data->otp_part)
1463 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001464 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001465
1466 if (!data->otp_part) {
1467 result = -ENOMEM;
1468 goto end_otp_read;
1469 }
1470
1471 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001472 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001473
1474 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001475 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001476 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1477 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1478 data->otp_part, OTP_SIZE_TA);
1479 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001480 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001481 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001482 if (result)
1483 goto end_otp_read;
1484 }
1485
1486 if (!data->otp_part) {
1487 result = -ENOMEM;
1488 goto end_otp_read;
1489 }
1490
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001491 if (offset + *size > otp_size)
1492 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001493 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001494
1495end_otp_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001496 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001497
1498 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001499}
1500
1501int stm32prog_otp_start(struct stm32prog_data *data)
1502{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001503 int result = 0;
1504 struct arm_smccc_res res;
1505
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001506 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001507 stm32prog_err("OTP update not supported");
1508
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001509 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001510 }
1511
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001512 if (!data->otp_part) {
1513 stm32prog_err("start OTP without data");
1514 return -1;
1515 }
1516
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001517 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001518 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1519 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1520 data->otp_part, OTP_SIZE_TA);
1521 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001522 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001523 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001524
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001525 if (!res.a0) {
1526 switch (res.a1) {
1527 case 0:
1528 result = 0;
1529 break;
1530 case 1:
1531 stm32prog_err("Provisioning");
1532 result = 0;
1533 break;
1534 default:
1535 log_err("%s: OTP incorrect value (err = %ld)\n",
1536 __func__, res.a1);
1537 result = -EINVAL;
1538 break;
1539 }
1540 } else {
1541 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1542 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001543 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001544 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001545 }
1546
1547 free(data->otp_part);
1548 data->otp_part = NULL;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001549 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001550
1551 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001552}
1553
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001554int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1555 long *size)
1556{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001557 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001558
1559 if (!offset)
1560 memset(data->pmic_part, 0, PMIC_SIZE);
1561
1562 if (offset + *size > PMIC_SIZE)
1563 *size = PMIC_SIZE - offset;
1564
1565 memcpy(&data->pmic_part[offset], buffer, *size);
1566
1567 return 0;
1568}
1569
1570int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1571 long *size)
1572{
1573 int result = 0, ret;
1574 struct udevice *dev;
1575
Simon Glassb24d5752023-02-05 15:40:35 -07001576 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001577 stm32prog_err("PMIC update not supported");
1578
1579 return -EOPNOTSUPP;
1580 }
1581
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001582 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001583 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001584 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001585 &dev);
1586 if (ret)
1587 return ret;
1588
1589 /* alway request PMIC for first packet */
1590 if (!offset) {
1591 /* init struct with 0 */
1592 memset(data->pmic_part, 0, PMIC_SIZE);
1593
1594 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001595 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001596 &dev);
1597 if (ret)
1598 return ret;
1599
1600 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1601 if (ret < 0) {
1602 result = ret;
1603 goto end_pmic_read;
1604 }
1605 if (ret != PMIC_SIZE) {
1606 result = -EACCES;
1607 goto end_pmic_read;
1608 }
1609 }
1610
1611 if (offset + *size > PMIC_SIZE)
1612 *size = PMIC_SIZE - offset;
1613
1614 memcpy(buffer, &data->pmic_part[offset], *size);
1615
1616end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001617 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001618 return result;
1619}
1620
1621int stm32prog_pmic_start(struct stm32prog_data *data)
1622{
1623 int ret;
1624 struct udevice *dev;
1625
Simon Glassb24d5752023-02-05 15:40:35 -07001626 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001627 stm32prog_err("PMIC update not supported");
1628
1629 return -EOPNOTSUPP;
1630 }
1631
1632 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001633 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001634 &dev);
1635 if (ret)
1636 return ret;
1637
1638 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1639}
1640
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001641/* copy FSBL on NAND to improve reliability on NAND */
1642static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1643{
1644 int ret, i;
1645 void *fsbl;
1646 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001647 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001648 struct dfu_entity *dfu;
1649 long size, offset;
1650
1651 if (part->target != STM32PROG_NAND &&
1652 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001653 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001654
1655 dfu = dfu_get_entity(part->alt_id);
1656
1657 /* read header */
1658 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001659 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001660 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1661 if (ret)
1662 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001663
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001664 stm32prog_header_check((ulong)&raw_header, &header);
1665 if (header.type != HEADER_STM32IMAGE &&
1666 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001667 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001668
1669 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001670 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001671 size = round_up(size, part->dev->mtd->erasesize);
1672 fsbl = calloc(1, size);
1673 if (!fsbl)
1674 return -ENOMEM;
1675 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001676 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001677 if (ret)
1678 goto error;
1679
1680 dfu_transaction_cleanup(dfu);
1681 offset = 0;
1682 for (i = part->bin_nb - 1; i > 0; i--) {
1683 offset += size;
1684 /* write to the next erase block */
1685 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001686 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1687 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001688 if (ret)
1689 goto error;
1690 }
1691
1692error:
1693 free(fsbl);
1694 return ret;
1695}
1696
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001697static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001698{
1699 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001700#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001701 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1702 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001703 data->phase = PHASE_END;
1704 log_notice("U-Boot script received\n");
1705 return;
1706 }
1707#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001708 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001709 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001710 stm32prog_err("Layout: invalid FlashLayout");
1711 return;
1712 }
1713
1714 if (!data->cur_part)
1715 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001716
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001717 if (data->cur_part->target == STM32PROG_RAM) {
1718 if (data->cur_part->part_type == PART_SYSTEM)
1719 data->uimage = data->cur_part->addr;
1720 if (data->cur_part->part_type == PART_FILESYSTEM)
1721 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001722 if (data->cur_part->part_type == PART_BINARY) {
1723 data->initrd = data->cur_part->addr;
1724 data->initrd_size = offset;
1725 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001726 }
1727
Patrick Delaunay6915b492020-03-18 09:24:52 +01001728 if (CONFIG_IS_ENABLED(MMC) &&
1729 data->cur_part->part_id < 0) {
1730 char cmdbuf[60];
1731
1732 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1733 data->cur_part->dev_id, data->cur_part->dev_id,
1734 -(data->cur_part->part_id));
1735 if (run_command(cmdbuf, 0)) {
1736 stm32prog_err("commands '%s' failed", cmdbuf);
1737 return;
1738 }
1739 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001740
Simon Glass8e05bb12023-02-05 15:40:17 -07001741 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001742 data->cur_part->bin_nb > 1) {
1743 if (stm32prog_copy_fsbl(data->cur_part)) {
1744 stm32prog_err("%s (0x%x): copy of fsbl failed",
1745 data->cur_part->name, data->cur_part->id);
1746 return;
1747 }
1748 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001749}
1750
1751void stm32prog_do_reset(struct stm32prog_data *data)
1752{
1753 if (data->phase == PHASE_RESET) {
1754 data->phase = PHASE_DO_RESET;
1755 puts("Reset requested\n");
1756 }
1757}
1758
1759void stm32prog_next_phase(struct stm32prog_data *data)
1760{
1761 int phase, i;
1762 struct stm32prog_part_t *part;
1763 bool found;
1764
1765 phase = data->phase;
1766 switch (phase) {
1767 case PHASE_RESET:
1768 case PHASE_END:
1769 case PHASE_DO_RESET:
1770 return;
1771 }
1772
1773 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001774 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001775 data->cur_part = NULL;
1776 data->phase = PHASE_END;
1777 found = false;
1778 do {
1779 phase++;
1780 if (phase > PHASE_LAST_USER)
1781 break;
1782 for (i = 0; i < data->part_nb; i++) {
1783 part = &data->part_array[i];
1784 if (part->id == phase) {
1785 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1786 data->cur_part = part;
1787 data->phase = phase;
1788 found = true;
1789 }
1790 break;
1791 }
1792 }
1793 } while (!found);
1794
1795 if (data->phase == PHASE_END)
1796 puts("Phase=END\n");
1797}
1798
Patrick Delaunay291e7222020-03-18 09:24:57 +01001799static int part_delete(struct stm32prog_data *data,
1800 struct stm32prog_part_t *part)
1801{
1802 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001803 unsigned long blks, blks_offset, blks_size;
1804 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001805 char cmdbuf[40];
1806 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001807
1808 printf("Erasing %s ", part->name);
1809 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001810 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001811 if (!IS_ENABLED(CONFIG_MMC)) {
1812 ret = -1;
1813 stm32prog_err("%s (0x%x): erase invalid",
1814 part->name, part->id);
1815 break;
1816 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001817 printf("on mmc %d: ", part->dev->dev_id);
1818 block_dev = mmc_get_blk_desc(part->dev->mmc);
1819 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1820 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1821 /* -1 or -2 : delete boot partition of MMC
1822 * need to switch to associated hwpart 1 or 2
1823 */
1824 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001825 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001826 part->dev->dev_id,
1827 -part->part_id))
1828 return -1;
1829
1830 blks = blk_derase(block_dev, blks_offset, blks_size);
1831
1832 /* return to user partition */
1833 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001834 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001835 part->dev->dev_id, 0);
1836 if (blks != blks_size) {
1837 ret = -1;
1838 stm32prog_err("%s (0x%x): MMC erase failed",
1839 part->name, part->id);
1840 }
1841 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001842 case STM32PROG_NOR:
1843 case STM32PROG_NAND:
1844 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001845 if (!IS_ENABLED(CONFIG_MTD)) {
1846 ret = -1;
1847 stm32prog_err("%s (0x%x): erase invalid",
1848 part->name, part->id);
1849 break;
1850 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001851 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1852 printf("on %s: ", devstr);
1853 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1854 devstr, part->addr, part->size);
1855 if (run_command(cmdbuf, 0)) {
1856 ret = -1;
1857 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1858 part->name, part->id, cmdbuf);
1859 }
1860 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001861 case STM32PROG_RAM:
1862 printf("on ram: ");
1863 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1864 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001865 default:
1866 ret = -1;
1867 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1868 break;
1869 }
1870 if (!ret)
1871 printf("done\n");
1872
1873 return ret;
1874}
1875
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001876static void stm32prog_devices_init(struct stm32prog_data *data)
1877{
1878 int i;
1879 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001880 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001881
1882 ret = treat_partition_list(data);
1883 if (ret)
1884 goto error;
1885
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001886 /* empty flashlayout */
1887 if (!data->dev_nb)
1888 return;
1889
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001890 /* initialize the selected device */
1891 for (i = 0; i < data->dev_nb; i++) {
1892 ret = init_device(data, &data->dev[i]);
1893 if (ret)
1894 goto error;
1895 }
1896
Patrick Delaunay291e7222020-03-18 09:24:57 +01001897 /* delete RAW partition before create partition */
1898 for (i = 0; i < data->part_nb; i++) {
1899 part = &data->part_array[i];
1900
1901 if (part->part_type != RAW_IMAGE)
1902 continue;
1903
1904 if (!IS_SELECT(part) || !IS_DELETE(part))
1905 continue;
1906
1907 ret = part_delete(data, part);
1908 if (ret)
1909 goto error;
1910 }
1911
Patrick Delaunay8040da12020-07-31 16:31:52 +02001912 if (IS_ENABLED(CONFIG_MMC)) {
1913 ret = create_gpt_partitions(data);
1914 if (ret)
1915 goto error;
1916 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001917
Patrick Delaunay291e7222020-03-18 09:24:57 +01001918 /* delete partition GPT or MTD */
1919 for (i = 0; i < data->part_nb; i++) {
1920 part = &data->part_array[i];
1921
1922 if (part->part_type == RAW_IMAGE)
1923 continue;
1924
1925 if (!IS_SELECT(part) || !IS_DELETE(part))
1926 continue;
1927
1928 ret = part_delete(data, part);
1929 if (ret)
1930 goto error;
1931 }
1932
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001933 return;
1934
1935error:
1936 data->part_nb = 0;
1937}
1938
1939int stm32prog_dfu_init(struct stm32prog_data *data)
1940{
1941 /* init device if no error */
1942 if (data->part_nb)
1943 stm32prog_devices_init(data);
1944
1945 if (data->part_nb)
1946 stm32prog_next_phase(data);
1947
1948 /* prepare DFU for device read/write */
1949 dfu_free_entities();
1950 return dfu_init_entities(data);
1951}
1952
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001953int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001954{
1955 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001956 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001957 data->phase = PHASE_FLASHLAYOUT;
1958
1959 return parse_flash_layout(data, addr, size);
1960}
1961
1962void stm32prog_clean(struct stm32prog_data *data)
1963{
1964 /* clean */
1965 dfu_free_entities();
1966 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001967 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001968 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001969
1970 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1971 tee_close_session(data->tee, data->tee_session);
1972 data->tee = NULL;
1973 data->tee_session = 0x0;
1974 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001975}
1976
1977/* DFU callback: used after serial and direct DFU USB access */
1978void dfu_flush_callback(struct dfu_entity *dfu)
1979{
1980 if (!stm32prog_data)
1981 return;
1982
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001983 if (dfu->dev_type == DFU_DEV_VIRT) {
1984 if (dfu->data.virt.dev_num == PHASE_OTP)
1985 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001986 else if (dfu->data.virt.dev_num == PHASE_PMIC)
1987 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001988 return;
1989 }
1990
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001991 if (dfu->dev_type == DFU_DEV_RAM) {
1992 if (dfu->alt == 0 &&
1993 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001994 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001995 /* waiting DFU DETACH for reenumeration */
1996 }
1997 }
1998
1999 if (!stm32prog_data->cur_part)
2000 return;
2001
2002 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02002003 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002004 stm32prog_next_phase(stm32prog_data);
2005 }
2006}
2007
2008void dfu_initiated_callback(struct dfu_entity *dfu)
2009{
2010 if (!stm32prog_data)
2011 return;
2012
2013 if (!stm32prog_data->cur_part)
2014 return;
2015
2016 /* force the saved offset for the current partition */
2017 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2018 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002019 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002020 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002021 }
2022}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002023
2024void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2025{
2026 struct stm32prog_data *data = stm32prog_data;
2027
2028 if (!stm32prog_data)
2029 return;
2030
2031 if (!stm32prog_data->cur_part)
2032 return;
2033
2034 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2035 stm32prog_err(msg);
2036}