blob: 0cdc14dd5bc8eff68aa0ca9ce97c3ae753a7c887 [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 Delaunay526f66c2020-03-18 09:24:50 +0100428 } else if (!strcmp(p, "System")) {
429 part->part_type = PART_SYSTEM;
430 } else if (!strcmp(p, "FileSystem")) {
431 part->part_type = PART_FILESYSTEM;
432 } else if (!strcmp(p, "RawImage")) {
433 part->part_type = RAW_IMAGE;
434 } else {
435 result = -EINVAL;
436 }
437 if (result)
438 stm32prog_err("Layout line %d: type parsing error : '%s'",
439 i, p);
440
441 return result;
442}
443
444static int parse_ip(struct stm32prog_data *data,
445 int i, char *p, struct stm32prog_part_t *part)
446{
447 int result = 0;
448 unsigned int len = 0;
449
450 part->dev_id = 0;
451 if (!strcmp(p, "none")) {
452 part->target = STM32PROG_NONE;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100453 } else if (!strncmp(p, "mmc", 3)) {
454 part->target = STM32PROG_MMC;
455 len = 3;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100456 } else if (!strncmp(p, "nor", 3)) {
457 part->target = STM32PROG_NOR;
458 len = 3;
459 } else if (!strncmp(p, "nand", 4)) {
460 part->target = STM32PROG_NAND;
461 len = 4;
462 } else if (!strncmp(p, "spi-nand", 8)) {
463 part->target = STM32PROG_SPI_NAND;
464 len = 8;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100465 } else if (!strncmp(p, "ram", 3)) {
466 part->target = STM32PROG_RAM;
467 len = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100468 } else {
469 result = -EINVAL;
470 }
471 if (len) {
472 /* only one digit allowed for device id */
473 if (strlen(p) != len + 1) {
474 result = -EINVAL;
475 } else {
476 part->dev_id = p[len] - '0';
477 if (part->dev_id > 9)
478 result = -EINVAL;
479 }
480 }
481 if (result)
482 stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p);
483
484 return result;
485}
486
487static int parse_offset(struct stm32prog_data *data,
488 int i, char *p, struct stm32prog_part_t *part)
489{
490 int result = 0;
491 char *tail;
492
493 part->part_id = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100494 part->addr = 0;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100495 part->size = 0;
Patrick Delaunay6915b492020-03-18 09:24:52 +0100496 /* eMMC boot parttion */
497 if (!strncmp(p, "boot", 4)) {
498 if (strlen(p) != 5) {
499 result = -EINVAL;
500 } else {
501 if (p[4] == '1')
502 part->part_id = -1;
503 else if (p[4] == '2')
504 part->part_id = -2;
505 else
506 result = -EINVAL;
507 }
508 if (result)
509 stm32prog_err("Layout line %d: invalid part '%s'",
510 i, p);
511 } else {
512 part->addr = simple_strtoull(p, &tail, 0);
513 if (tail == p || *tail != '\0') {
514 stm32prog_err("Layout line %d: invalid offset '%s'",
515 i, p);
516 result = -EINVAL;
517 }
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100518 }
519
520 return result;
521}
522
523static
524int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p,
525 struct stm32prog_part_t *part) = {
526 [COL_OPTION] = parse_option,
527 [COL_ID] = parse_id,
528 [COL_NAME] = parse_name,
529 [COL_TYPE] = parse_type,
530 [COL_IP] = parse_ip,
531 [COL_OFFSET] = parse_offset,
532};
533
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100534static int parse_flash_layout(struct stm32prog_data *data,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200535 uintptr_t addr,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100536 ulong size)
537{
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100538 int column = 0, part_nb = 0, ret;
539 bool end_of_line, eof;
540 char *p, *start, *last, *col;
541 struct stm32prog_part_t *part;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200542 struct image_header_s header;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100543 int part_list_size;
544 int i;
545
546 data->part_nb = 0;
547
548 /* check if STM32image is detected */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200549 stm32prog_header_check(addr, &header);
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200550 if (header.type == HEADER_STM32IMAGE) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100551 u32 checksum;
552
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200553 addr = addr + header.length;
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200554 size = header.image_length;
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100555
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200556 checksum = stm32prog_header_checksum(addr, &header);
557 if (checksum != header.image_checksum) {
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100558 stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x",
Patrick Delaunayf67fd842021-05-18 15:12:04 +0200559 checksum, header.image_checksum);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100560 return -EIO;
561 }
562 }
563 if (!size)
564 return -EINVAL;
565
566 start = (char *)addr;
567 last = start + size;
568
569 *last = 0x0; /* force null terminated string */
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100570 log_debug("flash layout =\n%s\n", start);
Patrick Delaunay526f66c2020-03-18 09:24:50 +0100571
572 /* calculate expected number of partitions */
573 part_list_size = 1;
574 p = start;
575 while (*p && (p < last)) {
576 if (*p++ == '\n') {
577 part_list_size++;
578 if (p < last && *p == '#')
579 part_list_size--;
580 }
581 }
582 if (part_list_size > PHASE_LAST_USER) {
583 stm32prog_err("Layout: too many partition (%d)",
584 part_list_size);
585 return -1;
586 }
587 part = calloc(sizeof(struct stm32prog_part_t), part_list_size);
588 if (!part) {
589 stm32prog_err("Layout: alloc failed");
590 return -ENOMEM;
591 }
592 data->part_array = part;
593
594 /* main parsing loop */
595 i = 1;
596 eof = false;
597 p = start;
598 col = start; /* 1st column */
599 end_of_line = false;
600 while (!eof) {
601 switch (*p) {
602 /* CR is ignored and replaced by NULL character */
603 case '\r':
604 *p = '\0';
605 p++;
606 continue;
607 case '\0':
608 end_of_line = true;
609 eof = true;
610 break;
611 case '\n':
612 end_of_line = true;
613 break;
614 case '\t':
615 break;
616 case '#':
617 /* comment line is skipped */
618 if (column == 0 && p == col) {
619 while ((p < last) && *p)
620 if (*p++ == '\n')
621 break;
622 col = p;
623 i++;
624 if (p >= last || !*p) {
625 eof = true;
626 end_of_line = true;
627 }
628 continue;
629 }
630 /* fall through */
631 /* by default continue with the next character */
632 default:
633 p++;
634 continue;
635 }
636
637 /* replace by \0: allow string parsing for each column */
638 *p = '\0';
639 p++;
640 if (p >= last) {
641 eof = true;
642 end_of_line = true;
643 }
644
645 /* skip empty line and multiple TAB in tsv file */
646 if (strlen(col) == 0) {
647 col = p;
648 /* skip empty line */
649 if (column == 0 && end_of_line) {
650 end_of_line = false;
651 i++;
652 }
653 continue;
654 }
655
656 if (column < COL_NB_STM32) {
657 ret = parse[column](data, i, col, part);
658 if (ret)
659 return ret;
660 }
661
662 /* save the beginning of the next column */
663 column++;
664 col = p;
665
666 if (!end_of_line)
667 continue;
668
669 /* end of the line detected */
670 end_of_line = false;
671
672 if (column < COL_NB_STM32) {
673 stm32prog_err("Layout line %d: no enought column", i);
674 return -EINVAL;
675 }
676 column = 0;
677 part_nb++;
678 part++;
679 i++;
680 if (part_nb >= part_list_size) {
681 part = NULL;
682 if (!eof) {
683 stm32prog_err("Layout: no enought memory for %d part",
684 part_nb);
685 return -EINVAL;
686 }
687 }
688 }
689 data->part_nb = part_nb;
690 if (data->part_nb == 0) {
691 stm32prog_err("Layout: no partition found");
692 return -ENODEV;
693 }
694
695 return 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100696}
697
698static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b)
699{
700 struct stm32prog_part_t *parta, *partb;
701
702 parta = container_of(a, struct stm32prog_part_t, list);
703 partb = container_of(b, struct stm32prog_part_t, list);
704
Patrick Delaunay6915b492020-03-18 09:24:52 +0100705 if (parta->part_id != partb->part_id)
706 return parta->part_id - partb->part_id;
707 else
708 return parta->addr > partb->addr ? 1 : -1;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100709}
710
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100711static void get_mtd_by_target(char *string, enum stm32prog_target target,
712 int dev_id)
713{
714 const char *dev_str;
715
716 switch (target) {
717 case STM32PROG_NOR:
718 dev_str = "nor";
719 break;
720 case STM32PROG_NAND:
721 dev_str = "nand";
722 break;
723 case STM32PROG_SPI_NAND:
724 dev_str = "spi-nand";
725 break;
726 default:
727 dev_str = "invalid";
728 break;
729 }
730 sprintf(string, "%s%d", dev_str, dev_id);
731}
732
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100733static int init_device(struct stm32prog_data *data,
734 struct stm32prog_dev_t *dev)
735{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100736 struct mmc *mmc = NULL;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100737 struct blk_desc *block_dev = NULL;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100738 struct mtd_info *mtd = NULL;
Patrice Chotard49569082023-06-08 17:16:40 +0200739 struct mtd_info *partition;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100740 char mtd_id[16];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100741 int part_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100742 int ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100743 u64 first_addr = 0, last_addr = 0;
744 struct stm32prog_part_t *part, *next_part;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100745 u64 part_addr, part_size;
746 bool part_found;
747 const char *part_name;
Patrice Chotard49569082023-06-08 17:16:40 +0200748 u8 i;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100749
750 switch (dev->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100751 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200752 if (!IS_ENABLED(CONFIG_MMC)) {
753 stm32prog_err("unknown device type = %d", dev->target);
754 return -ENODEV;
755 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100756 mmc = find_mmc_device(dev->dev_id);
Patrick Delaunayf4aaeed2020-07-06 13:20:58 +0200757 if (!mmc || mmc_init(mmc)) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100758 stm32prog_err("mmc device %d not found", dev->dev_id);
759 return -ENODEV;
760 }
761 block_dev = mmc_get_blk_desc(mmc);
762 if (!block_dev) {
763 stm32prog_err("mmc device %d not probed", dev->dev_id);
764 return -ENODEV;
765 }
766 dev->erase_size = mmc->erase_grp_size * block_dev->blksz;
767 dev->mmc = mmc;
768
769 /* reserve a full erase group for each GTP headers */
770 if (mmc->erase_grp_size > GPT_HEADER_SZ) {
771 first_addr = dev->erase_size;
772 last_addr = (u64)(block_dev->lba -
773 mmc->erase_grp_size) *
774 block_dev->blksz;
775 } else {
776 first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz;
777 last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) *
778 block_dev->blksz;
779 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100780 log_debug("MMC %d: lba=%ld blksz=%ld\n", dev->dev_id,
781 block_dev->lba, block_dev->blksz);
782 log_debug(" available address = 0x%llx..0x%llx\n",
783 first_addr, last_addr);
784 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100785 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100786 case STM32PROG_NOR:
787 case STM32PROG_NAND:
788 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +0200789 if (!IS_ENABLED(CONFIG_MTD)) {
790 stm32prog_err("unknown device type = %d", dev->target);
791 return -ENODEV;
792 }
Patrice Chotard49569082023-06-08 17:16:40 +0200793 /* register partitions with MTDIDS/MTDPARTS or OF fallback */
794 mtd_probe_devices();
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100795 get_mtd_by_target(mtd_id, dev->target, dev->dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100796 log_debug("%s\n", mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100797
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100798 mtd = get_mtd_device_nm(mtd_id);
799 if (IS_ERR(mtd)) {
800 stm32prog_err("MTD device %s not found", mtd_id);
801 return -ENODEV;
802 }
803 first_addr = 0;
804 last_addr = mtd->size;
805 dev->erase_size = mtd->erasesize;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100806 log_debug("MTD device %s: size=%lld erasesize=%d\n",
807 mtd_id, mtd->size, mtd->erasesize);
808 log_debug(" available address = 0x%llx..0x%llx\n",
809 first_addr, last_addr);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100810 dev->mtd = mtd;
811 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100812 case STM32PROG_RAM:
813 first_addr = gd->bd->bi_dram[0].start;
814 last_addr = first_addr + gd->bd->bi_dram[0].size;
815 dev->erase_size = 1;
816 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100817 default:
818 stm32prog_err("unknown device type = %d", dev->target);
819 return -ENODEV;
820 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100821 log_debug(" erase size = 0x%x\n", dev->erase_size);
822 log_debug(" full_update = %d\n", dev->full_update);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100823
824 /* order partition list in offset order */
825 list_sort(NULL, &dev->part_list, &part_cmp);
826 part_id = 1;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100827 log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100828 list_for_each_entry(part, &dev->part_list, list) {
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100829 if (part->bin_nb > 1) {
830 if ((dev->target != STM32PROG_NAND &&
831 dev->target != STM32PROG_SPI_NAND) ||
832 part->id >= PHASE_FIRST_USER ||
833 strncmp(part->name, "fsbl", 4)) {
834 stm32prog_err("%s (0x%x): multiple binary %d not supported",
835 part->name, part->id,
836 part->bin_nb);
837 return -EINVAL;
838 }
839 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100840 if (part->part_type == RAW_IMAGE) {
841 part->part_id = 0x0;
842 part->addr = 0x0;
843 if (block_dev)
844 part->size = block_dev->lba * block_dev->blksz;
845 else
846 part->size = last_addr;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100847 log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n",
848 part->option, part->id, part->name,
849 part->part_type, part->bin_nb, part->target,
850 part->dev_id, part->addr, part->size);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100851 continue;
852 }
Patrick Delaunay6915b492020-03-18 09:24:52 +0100853 if (part->part_id < 0) { /* boot hw partition for eMMC */
854 if (mmc) {
855 part->size = mmc->capacity_boot;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100856 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100857 stm32prog_err("%s (0x%x): hw partition not expected : %d",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100858 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100859 part->part_id);
860 return -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100861 }
862 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100863 part->part_id = part_id++;
864
865 /* last partition : size to the end of the device */
866 if (part->list.next != &dev->part_list) {
867 next_part =
868 container_of(part->list.next,
869 struct stm32prog_part_t,
870 list);
871 if (part->addr < next_part->addr) {
872 part->size = next_part->addr -
873 part->addr;
874 } else {
875 stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx",
876 part->name, part->id,
877 part->addr,
878 next_part->name,
879 next_part->id,
880 next_part->addr);
881 return -EINVAL;
882 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100883 } else {
Patrick Delaunay6915b492020-03-18 09:24:52 +0100884 if (part->addr <= last_addr) {
885 part->size = last_addr - part->addr;
886 } else {
887 stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)",
888 part->name, part->id,
889 part->addr, last_addr);
890 return -EINVAL;
891 }
892 }
893 if (part->addr < first_addr) {
894 stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)",
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100895 part->name, part->id,
Patrick Delaunay6915b492020-03-18 09:24:52 +0100896 part->addr, first_addr);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100897 return -EINVAL;
898 }
899 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100900 if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) {
901 stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x",
902 part->name, part->id, part->addr,
903 part->dev->erase_size);
904 return -EINVAL;
905 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100906 log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx",
907 part->part_id, part->option, part->id, part->name,
908 part->part_type, part->bin_nb, part->target,
909 part->dev_id, part->addr, part->size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100910
911 part_addr = 0;
912 part_size = 0;
913 part_found = false;
914
915 /* check coherency with existing partition */
916 if (block_dev) {
917 /*
918 * block devices with GPT: check user partition size
919 * only for partial update, the GPT partions are be
920 * created for full update
921 */
922 if (dev->full_update || part->part_id < 0) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100923 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100924 continue;
925 }
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600926 struct disk_partition partinfo;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100927
928 ret = part_get_info(block_dev, part->part_id,
929 &partinfo);
930
931 if (ret) {
932 stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d",
933 part->name, part->id,
934 part_id, part->dev_id);
935 return -ENODEV;
936 }
937 part_addr = (u64)partinfo.start * partinfo.blksz;
938 part_size = (u64)partinfo.size * partinfo.blksz;
939 part_name = (char *)partinfo.name;
940 part_found = true;
941 }
942
Patrick Delaunay8040da12020-07-31 16:31:52 +0200943 if (IS_ENABLED(CONFIG_MTD) && mtd) {
Patrice Chotard49569082023-06-08 17:16:40 +0200944 i = 0;
945 list_for_each_entry(partition, &mtd->partitions, node) {
946 if ((part->part_id - 1) == i) {
947 part_found = true;
948 break;
949 }
950 i++;
951 }
952 if (part_found) {
953 part_addr = partition->offset;
954 part_size = partition->size;
955 part_name = partition->name;
956 } else {
957 stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s",
958 part->name, part->id, part->part_id, mtd_id);
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100959 return -ENODEV;
960 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100961 }
Patrick Delaunay8040da12020-07-31 16:31:52 +0200962
Patrick Delaunayd5de9382020-10-15 14:28:17 +0200963 /* no partition for this device */
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100964 if (!part_found) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100965 log_debug("\n");
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100966 continue;
967 }
968
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100969 log_debug(" %08llx %08llx\n", part_addr, part_size);
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100970
971 if (part->addr != part_addr) {
972 stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected",
973 part->name, part->id, part->part_id,
974 part_name, part->addr, part_addr);
975 return -ENODEV;
976 }
977 if (part->size != part_size) {
978 stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected",
979 part->name, part->id, part->part_id,
980 part_name, part->addr, part->size,
981 part_size);
982 return -ENODEV;
983 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100984 }
985 return 0;
986}
987
988static int treat_partition_list(struct stm32prog_data *data)
989{
990 int i, j;
991 struct stm32prog_part_t *part;
992
993 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
994 data->dev[j].target = STM32PROG_NONE;
995 INIT_LIST_HEAD(&data->dev[j].part_list);
996 }
997
Patrick Delaunayc5112242020-03-18 09:24:55 +0100998 data->fsbl_nor_detected = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100999 for (i = 0; i < data->part_nb; i++) {
1000 part = &data->part_array[i];
1001 part->alt_id = -1;
1002
1003 /* skip partition with IP="none" */
1004 if (part->target == STM32PROG_NONE) {
1005 if (IS_SELECT(part)) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001006 stm32prog_err("Layout: selected none phase = 0x%x for part %s",
1007 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001008 return -EINVAL;
1009 }
1010 continue;
1011 }
1012
1013 if (part->id == PHASE_FLASHLAYOUT ||
1014 part->id > PHASE_LAST_USER) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001015 stm32prog_err("Layout: invalid phase = 0x%x for part %s",
1016 part->id, part->name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001017 return -EINVAL;
1018 }
1019 for (j = i + 1; j < data->part_nb; j++) {
1020 if (part->id == data->part_array[j].id) {
Patrick Delaunayf5b85712022-01-18 10:33:14 +01001021 stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s",
1022 part->id, part->name, data->part_array[j].name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001023 return -EINVAL;
1024 }
1025 }
1026 for (j = 0; j < STM32PROG_MAX_DEV; j++) {
1027 if (data->dev[j].target == STM32PROG_NONE) {
1028 /* new device found */
1029 data->dev[j].target = part->target;
1030 data->dev[j].dev_id = part->dev_id;
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001031 data->dev[j].full_update = true;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001032 data->dev_nb++;
1033 break;
1034 } else if ((part->target == data->dev[j].target) &&
1035 (part->dev_id == data->dev[j].dev_id)) {
1036 break;
1037 }
1038 }
1039 if (j == STM32PROG_MAX_DEV) {
1040 stm32prog_err("Layout: too many device");
1041 return -EINVAL;
1042 }
Patrick Delaunayc5112242020-03-18 09:24:55 +01001043 switch (part->target) {
1044 case STM32PROG_NOR:
1045 if (!data->fsbl_nor_detected &&
1046 !strncmp(part->name, "fsbl", 4))
1047 data->fsbl_nor_detected = true;
1048 /* fallthrough */
Patrick Delaunayc5112242020-03-18 09:24:55 +01001049 default:
1050 break;
1051 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001052 part->dev = &data->dev[j];
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001053 if (!IS_SELECT(part))
1054 part->dev->full_update = false;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001055 list_add_tail(&part->list, &data->dev[j].part_list);
1056 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001057
1058 return 0;
1059}
1060
Patrick Delaunay8040da12020-07-31 16:31:52 +02001061static int create_gpt_partitions(struct stm32prog_data *data)
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001062{
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001063 int offset = 0;
1064 const int buflen = SZ_8K;
1065 char *buf;
1066 char uuid[UUID_STR_LEN + 1];
1067 unsigned char *uuid_bin;
1068 unsigned int mmc_id;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001069 int i, j;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001070 bool rootfs_found;
1071 struct stm32prog_part_t *part;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001072 const char *type_str;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001073
1074 buf = malloc(buflen);
1075 if (!buf)
1076 return -ENOMEM;
1077
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001078 /* initialize the selected device */
1079 for (i = 0; i < data->dev_nb; i++) {
Patrick Delaunay5ce50062020-03-18 09:24:53 +01001080 /* create gpt partition support only for full update on MMC */
1081 if (data->dev[i].target != STM32PROG_MMC ||
1082 !data->dev[i].full_update)
1083 continue;
1084
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001085 printf("partitions on mmc%d: ", data->dev[i].dev_id);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001086 offset = 0;
1087 rootfs_found = false;
1088 memset(buf, 0, buflen);
1089
1090 list_for_each_entry(part, &data->dev[i].part_list, list) {
Patrick Delaunay6915b492020-03-18 09:24:52 +01001091 /* skip eMMC boot partitions */
1092 if (part->part_id < 0)
1093 continue;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001094 /* skip Raw Image */
1095 if (part->part_type == RAW_IMAGE)
1096 continue;
1097
1098 if (offset + 100 > buflen) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001099 log_debug("\n%s: buffer too small, %s skippped",
1100 __func__, part->name);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001101 continue;
1102 }
1103
1104 if (!offset)
1105 offset += sprintf(buf, "gpt write mmc %d \"",
1106 data->dev[i].dev_id);
1107
1108 offset += snprintf(buf + offset, buflen - offset,
1109 "name=%s,start=0x%llx,size=0x%llx",
1110 part->name,
1111 part->addr,
1112 part->size);
1113
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001114 switch (part->part_type) {
1115 case PART_BINARY:
1116 type_str = LINUX_RESERVED_UUID;
1117 break;
1118 case PART_FIP:
1119 type_str = FIP_TYPE_UUID;
1120 break;
1121 default:
1122 type_str = "linux";
1123 break;
1124 }
1125 offset += snprintf(buf + offset,
1126 buflen - offset,
1127 ",type=%s", type_str);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001128
1129 if (part->part_type == PART_SYSTEM)
1130 offset += snprintf(buf + offset,
1131 buflen - offset,
1132 ",bootable");
1133
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001134 /* partition UUID */
1135 uuid_bin = NULL;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001136 if (!rootfs_found && !strcmp(part->name, "rootfs")) {
1137 mmc_id = part->dev_id;
1138 rootfs_found = true;
Patrick Delaunay8dc57682022-03-28 19:25:30 +02001139 if (mmc_id < ARRAY_SIZE(uuid_mmc))
1140 uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b;
1141 }
1142 if (part->part_type == PART_FIP) {
1143 for (j = 0; j < ARRAY_SIZE(fip_part_name); j++)
1144 if (!strcmp(part->name, fip_part_name[j])) {
1145 uuid_bin = (unsigned char *)fip_part_uuid[j].b;
1146 break;
1147 }
1148 }
1149 if (uuid_bin) {
1150 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
1151 offset += snprintf(buf + offset,
1152 buflen - offset,
1153 ",uuid=%s", uuid);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001154 }
1155
1156 offset += snprintf(buf + offset, buflen - offset, ";");
1157 }
1158
1159 if (offset) {
1160 offset += snprintf(buf + offset, buflen - offset, "\"");
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001161 log_debug("\ncmd: %s\n", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001162 if (run_command(buf, 0)) {
1163 stm32prog_err("GPT partitionning fail: %s",
1164 buf);
1165 free(buf);
1166
1167 return -1;
1168 }
1169 }
1170
1171 if (data->dev[i].mmc)
1172 part_init(mmc_get_blk_desc(data->dev[i].mmc));
1173
1174#ifdef DEBUG
1175 sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001176 log_debug("\ncmd: %s", buf);
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001177 if (run_command(buf, 0))
1178 printf("fail !\n");
1179 else
1180 printf("OK\n");
1181
1182 sprintf(buf, "part list mmc %d", data->dev[i].dev_id);
1183 run_command(buf, 0);
1184#endif
Patrick Delaunay95a6bf42022-09-09 17:22:15 +02001185 puts("done\n");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001186 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001187
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001188#ifdef DEBUG
1189 run_command("mtd list", 0);
1190#endif
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001191 free(buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001192
1193 return 0;
1194}
1195
1196static int stm32prog_alt_add(struct stm32prog_data *data,
1197 struct dfu_entity *dfu,
1198 struct stm32prog_part_t *part)
1199{
1200 int ret = 0;
1201 int offset = 0;
1202 char devstr[10];
1203 char dfustr[10];
1204 char buf[ALT_BUF_LEN];
1205 u32 size;
1206 char multiplier, type;
1207
1208 /* max 3 digit for sector size */
1209 if (part->size > SZ_1M) {
1210 size = (u32)(part->size / SZ_1M);
1211 multiplier = 'M';
1212 } else if (part->size > SZ_1K) {
1213 size = (u32)(part->size / SZ_1K);
1214 multiplier = 'K';
1215 } else {
1216 size = (u32)part->size;
1217 multiplier = 'B';
1218 }
1219 if (IS_SELECT(part) && !IS_EMPTY(part))
1220 type = 'e'; /*Readable and Writeable*/
1221 else
1222 type = 'a';/*Readable*/
1223
1224 memset(buf, 0, sizeof(buf));
1225 offset = snprintf(buf, ALT_BUF_LEN - offset,
1226 "@%s/0x%02x/1*%d%c%c ",
1227 part->name, part->id,
1228 size, multiplier, type);
1229
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001230 if (part->target == STM32PROG_RAM) {
1231 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1232 "ram 0x%llx 0x%llx",
1233 part->addr, part->size);
1234 } else if (part->part_type == RAW_IMAGE) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001235 u64 dfu_size;
1236
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001237 if (part->dev->target == STM32PROG_MMC)
1238 dfu_size = part->size / part->dev->mmc->read_bl_len;
1239 else
1240 dfu_size = part->size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001241 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1242 "raw 0x0 0x%llx", dfu_size);
Patrick Delaunay6915b492020-03-18 09:24:52 +01001243 } else if (part->part_id < 0) {
1244 u64 nb_blk = part->size / part->dev->mmc->read_bl_len;
1245
1246 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1247 "raw 0x%llx 0x%llx",
1248 part->addr, nb_blk);
1249 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001250 " mmcpart %d", -(part->part_id));
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001251 } else {
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001252 if (part->part_type == PART_SYSTEM &&
1253 (part->target == STM32PROG_NAND ||
1254 part->target == STM32PROG_NOR ||
1255 part->target == STM32PROG_SPI_NAND))
1256 offset += snprintf(buf + offset,
1257 ALT_BUF_LEN - offset,
1258 "partubi");
1259 else
1260 offset += snprintf(buf + offset,
1261 ALT_BUF_LEN - offset,
1262 "part");
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001263 /* dev_id requested by DFU MMC */
1264 if (part->target == STM32PROG_MMC)
1265 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
1266 " %d", part->dev_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001267 offset += snprintf(buf + offset, ALT_BUF_LEN - offset,
Patrick Delaunayb5793a62022-06-16 18:37:59 +02001268 " %d", part->part_id);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001269 }
Patrick Delaunay8040da12020-07-31 16:31:52 +02001270 ret = -ENODEV;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001271 switch (part->target) {
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001272 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001273 if (IS_ENABLED(CONFIG_MMC)) {
1274 ret = 0;
1275 sprintf(dfustr, "mmc");
1276 sprintf(devstr, "%d", part->dev_id);
1277 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001278 break;
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001279 case STM32PROG_NAND:
1280 case STM32PROG_NOR:
1281 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001282 if (IS_ENABLED(CONFIG_MTD)) {
1283 ret = 0;
1284 sprintf(dfustr, "mtd");
1285 get_mtd_by_target(devstr, part->target, part->dev_id);
1286 }
Patrick Delaunay6ab74962020-03-18 09:24:54 +01001287 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001288 case STM32PROG_RAM:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001289 ret = 0;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001290 sprintf(dfustr, "ram");
1291 sprintf(devstr, "0");
1292 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001293 default:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001294 break;
1295 }
1296 if (ret) {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001297 stm32prog_err("invalid target: %d", part->target);
Patrick Delaunay8040da12020-07-31 16:31:52 +02001298 return ret;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001299 }
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001300 log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001301 ret = dfu_alt_add(dfu, dfustr, devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001302 log_debug("dfu_alt_add(%s,%s,%s) result %d\n",
1303 dfustr, devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001304
1305 return ret;
1306}
1307
1308static int stm32prog_alt_add_virt(struct dfu_entity *dfu,
1309 char *name, int phase, int size)
1310{
1311 int ret = 0;
1312 char devstr[4];
1313 char buf[ALT_BUF_LEN];
1314
1315 sprintf(devstr, "%d", phase);
1316 sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size);
1317 ret = dfu_alt_add(dfu, "virt", devstr, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001318 log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001319
1320 return ret;
1321}
1322
1323static int dfu_init_entities(struct stm32prog_data *data)
1324{
1325 int ret = 0;
1326 int phase, i, alt_id;
1327 struct stm32prog_part_t *part;
1328 struct dfu_entity *dfu;
1329 int alt_nb;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001330 u32 otp_size = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001331
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001332 alt_nb = 1; /* number of virtual = CMD*/
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001333
1334 if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1335 /* OTP_SIZE_SMC = 0 if SMC is not supported */
1336 otp_size = OTP_SIZE_SMC;
1337 /* check if PTA BSEC is supported */
1338 ret = optee_ta_open(data);
1339 log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret);
1340 if (!ret && data->tee)
1341 otp_size = OTP_SIZE_TA;
1342 if (otp_size)
1343 alt_nb++; /* OTP*/
1344 }
1345
Patrick Delaunay7d145402021-05-18 15:12:09 +02001346 if (CONFIG_IS_ENABLED(DM_PMIC))
1347 alt_nb++; /* PMIC NVMEM*/
1348
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001349 if (data->part_nb == 0)
1350 alt_nb++; /* +1 for FlashLayout */
1351 else
1352 for (i = 0; i < data->part_nb; i++) {
1353 if (data->part_array[i].target != STM32PROG_NONE)
1354 alt_nb++;
1355 }
1356
1357 if (dfu_alt_init(alt_nb, &dfu))
1358 return -ENODEV;
1359
1360 puts("DFU alt info setting: ");
1361 if (data->part_nb) {
1362 alt_id = 0;
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001363 ret = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001364 for (phase = 1;
1365 (phase <= PHASE_LAST_USER) &&
1366 (alt_id < alt_nb) && !ret;
1367 phase++) {
1368 /* ordering alt setting by phase id */
1369 part = NULL;
1370 for (i = 0; i < data->part_nb; i++) {
1371 if (phase == data->part_array[i].id) {
1372 part = &data->part_array[i];
1373 break;
1374 }
1375 }
1376 if (!part)
1377 continue;
1378 if (part->target == STM32PROG_NONE)
1379 continue;
1380 part->alt_id = alt_id;
1381 alt_id++;
1382
1383 ret = stm32prog_alt_add(data, dfu, part);
1384 }
1385 } else {
1386 char buf[ALT_BUF_LEN];
1387
1388 sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000",
Patrick Delaunaye334d322022-09-06 18:53:18 +02001389 PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001390 ret = dfu_alt_add(dfu, "ram", NULL, buf);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001391 log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001392 }
1393
1394 if (!ret)
Patrick Delaunaybd577492021-07-05 09:39:01 +02001395 ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001396
Patrick Delaunay9a699b72022-09-06 18:53:20 +02001397 if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size)
1398 ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001399
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001400 if (!ret && CONFIG_IS_ENABLED(DM_PMIC))
Patrick Delaunaybd577492021-07-05 09:39:01 +02001401 ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001402
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001403 if (ret)
1404 stm32prog_err("dfu init failed: %d", ret);
1405 puts("done\n");
1406
1407#ifdef DEBUG
1408 dfu_show_entities();
1409#endif
1410 return ret;
1411}
1412
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001413int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1414 long *size)
1415{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001416 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001417 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001418
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001419 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
1420 stm32prog_err("OTP update not supported");
1421
1422 return -EOPNOTSUPP;
1423 }
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001424
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001425 if (!data->otp_part) {
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001426 data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001427 if (!data->otp_part)
1428 return -ENOMEM;
1429 }
1430
1431 if (!offset)
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001432 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001433
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001434 if (offset + *size > otp_size)
1435 *size = otp_size - offset;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001436
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001437 memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001438
1439 return 0;
1440}
1441
1442int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1443 long *size)
1444{
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001445 u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001446 int result = 0;
1447
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001448 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001449 stm32prog_err("OTP update not supported");
1450
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001451 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001452 }
1453
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001454 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001455 /* alway read for first packet */
1456 if (!offset) {
1457 if (!data->otp_part)
1458 data->otp_part =
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001459 memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001460
1461 if (!data->otp_part) {
1462 result = -ENOMEM;
1463 goto end_otp_read;
1464 }
1465
1466 /* init struct with 0 */
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001467 memset(data->otp_part, 0, otp_size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001468
1469 /* call the service */
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001470 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001471 if (data->tee && CONFIG_IS_ENABLED(OPTEE))
1472 result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP,
1473 data->otp_part, OTP_SIZE_TA);
1474 else if (IS_ENABLED(CONFIG_ARM_SMCCC))
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001475 result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001476 (unsigned long)data->otp_part, 0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001477 if (result)
1478 goto end_otp_read;
1479 }
1480
1481 if (!data->otp_part) {
1482 result = -ENOMEM;
1483 goto end_otp_read;
1484 }
1485
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001486 if (offset + *size > otp_size)
1487 *size = otp_size - offset;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001488 memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001489
1490end_otp_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001491 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001492
1493 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001494}
1495
1496int stm32prog_otp_start(struct stm32prog_data *data)
1497{
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001498 int result = 0;
1499 struct arm_smccc_res res;
1500
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001501 if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) {
Patrick Delaunay8040da12020-07-31 16:31:52 +02001502 stm32prog_err("OTP update not supported");
1503
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001504 return -EOPNOTSUPP;
Patrick Delaunay8040da12020-07-31 16:31:52 +02001505 }
1506
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001507 if (!data->otp_part) {
1508 stm32prog_err("start OTP without data");
1509 return -1;
1510 }
1511
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001512 result = -EOPNOTSUPP;
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001513 if (data->tee && CONFIG_IS_ENABLED(OPTEE)) {
1514 result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP,
1515 data->otp_part, OTP_SIZE_TA);
1516 } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) {
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001517 arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL,
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001518 (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001519
Patrick Delaunay455b06a2022-03-28 19:25:27 +02001520 if (!res.a0) {
1521 switch (res.a1) {
1522 case 0:
1523 result = 0;
1524 break;
1525 case 1:
1526 stm32prog_err("Provisioning");
1527 result = 0;
1528 break;
1529 default:
1530 log_err("%s: OTP incorrect value (err = %ld)\n",
1531 __func__, res.a1);
1532 result = -EINVAL;
1533 break;
1534 }
1535 } else {
1536 log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n",
1537 __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001538 result = -EINVAL;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001539 }
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001540 }
1541
1542 free(data->otp_part);
1543 data->otp_part = NULL;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001544 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001545
1546 return result;
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001547}
1548
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001549int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer,
1550 long *size)
1551{
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001552 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001553
1554 if (!offset)
1555 memset(data->pmic_part, 0, PMIC_SIZE);
1556
1557 if (offset + *size > PMIC_SIZE)
1558 *size = PMIC_SIZE - offset;
1559
1560 memcpy(&data->pmic_part[offset], buffer, *size);
1561
1562 return 0;
1563}
1564
1565int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer,
1566 long *size)
1567{
1568 int result = 0, ret;
1569 struct udevice *dev;
1570
Simon Glassb24d5752023-02-05 15:40:35 -07001571 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001572 stm32prog_err("PMIC update not supported");
1573
1574 return -EOPNOTSUPP;
1575 }
1576
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001577 log_debug("%s: %x %lx\n", __func__, offset, *size);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001578 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001579 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001580 &dev);
1581 if (ret)
1582 return ret;
1583
1584 /* alway request PMIC for first packet */
1585 if (!offset) {
1586 /* init struct with 0 */
1587 memset(data->pmic_part, 0, PMIC_SIZE);
1588
1589 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001590 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001591 &dev);
1592 if (ret)
1593 return ret;
1594
1595 ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1596 if (ret < 0) {
1597 result = ret;
1598 goto end_pmic_read;
1599 }
1600 if (ret != PMIC_SIZE) {
1601 result = -EACCES;
1602 goto end_pmic_read;
1603 }
1604 }
1605
1606 if (offset + *size > PMIC_SIZE)
1607 *size = PMIC_SIZE - offset;
1608
1609 memcpy(buffer, &data->pmic_part[offset], *size);
1610
1611end_pmic_read:
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001612 log_debug("%s: result %i\n", __func__, result);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001613 return result;
1614}
1615
1616int stm32prog_pmic_start(struct stm32prog_data *data)
1617{
1618 int ret;
1619 struct udevice *dev;
1620
Simon Glassb24d5752023-02-05 15:40:35 -07001621 if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) {
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001622 stm32prog_err("PMIC update not supported");
1623
1624 return -EOPNOTSUPP;
1625 }
1626
1627 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -07001628 DM_DRIVER_GET(stpmic1_nvm),
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001629 &dev);
1630 if (ret)
1631 return ret;
1632
1633 return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE);
1634}
1635
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001636/* copy FSBL on NAND to improve reliability on NAND */
1637static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
1638{
1639 int ret, i;
1640 void *fsbl;
1641 struct image_header_s header;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001642 struct stm32_header_v2 raw_header; /* V2 size > v1 size */
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001643 struct dfu_entity *dfu;
1644 long size, offset;
1645
1646 if (part->target != STM32PROG_NAND &&
1647 part->target != STM32PROG_SPI_NAND)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001648 return -EINVAL;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001649
1650 dfu = dfu_get_entity(part->alt_id);
1651
1652 /* read header */
1653 dfu_transaction_cleanup(dfu);
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001654 size = sizeof(raw_header);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001655 ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
1656 if (ret)
1657 return ret;
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001658
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001659 stm32prog_header_check((ulong)&raw_header, &header);
1660 if (header.type != HEADER_STM32IMAGE &&
1661 header.type != HEADER_STM32IMAGE_V2)
Patrick Delaunay19676ef2021-04-02 14:05:17 +02001662 return -ENOENT;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001663
1664 /* read header + payload */
Patrick Delaunay953d8bf2022-03-28 19:25:29 +02001665 size = header.image_length + header.length;
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001666 size = round_up(size, part->dev->mtd->erasesize);
1667 fsbl = calloc(1, size);
1668 if (!fsbl)
1669 return -ENOMEM;
1670 ret = dfu->read_medium(dfu, 0, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001671 log_debug("%s read size=%lx ret=%d\n", __func__, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001672 if (ret)
1673 goto error;
1674
1675 dfu_transaction_cleanup(dfu);
1676 offset = 0;
1677 for (i = part->bin_nb - 1; i > 0; i--) {
1678 offset += size;
1679 /* write to the next erase block */
1680 ret = dfu->write_medium(dfu, offset, fsbl, &size);
Patrick Delaunay2b15af52020-11-06 19:01:30 +01001681 log_debug("%s copy at ofset=%lx size=%lx ret=%d",
1682 __func__, offset, size, ret);
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001683 if (ret)
1684 goto error;
1685 }
1686
1687error:
1688 free(fsbl);
1689 return ret;
1690}
1691
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001692static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001693{
1694 if (data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001695#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Patrick Delaunaye334d322022-09-06 18:53:18 +02001696 if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) {
1697 data->script = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +02001698 data->phase = PHASE_END;
1699 log_notice("U-Boot script received\n");
1700 return;
1701 }
1702#endif
Patrick Delaunayc14b0eb2022-03-28 19:25:33 +02001703 log_notice("\nFlashLayout received, size = %lld\n", offset);
Patrick Delaunaye334d322022-09-06 18:53:18 +02001704 if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset))
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001705 stm32prog_err("Layout: invalid FlashLayout");
1706 return;
1707 }
1708
1709 if (!data->cur_part)
1710 return;
Patrick Delaunay6915b492020-03-18 09:24:52 +01001711
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001712 if (data->cur_part->target == STM32PROG_RAM) {
1713 if (data->cur_part->part_type == PART_SYSTEM)
1714 data->uimage = data->cur_part->addr;
1715 if (data->cur_part->part_type == PART_FILESYSTEM)
1716 data->dtb = data->cur_part->addr;
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001717 if (data->cur_part->part_type == PART_BINARY) {
1718 data->initrd = data->cur_part->addr;
1719 data->initrd_size = offset;
1720 }
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001721 }
1722
Patrick Delaunay6915b492020-03-18 09:24:52 +01001723 if (CONFIG_IS_ENABLED(MMC) &&
1724 data->cur_part->part_id < 0) {
1725 char cmdbuf[60];
1726
1727 sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0",
1728 data->cur_part->dev_id, data->cur_part->dev_id,
1729 -(data->cur_part->part_id));
1730 if (run_command(cmdbuf, 0)) {
1731 stm32prog_err("commands '%s' failed", cmdbuf);
1732 return;
1733 }
1734 }
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001735
Simon Glass8e05bb12023-02-05 15:40:17 -07001736 if (IS_ENABLED(CONFIG_MTD) &&
Patrick Delaunay851d6f32020-03-18 09:24:56 +01001737 data->cur_part->bin_nb > 1) {
1738 if (stm32prog_copy_fsbl(data->cur_part)) {
1739 stm32prog_err("%s (0x%x): copy of fsbl failed",
1740 data->cur_part->name, data->cur_part->id);
1741 return;
1742 }
1743 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001744}
1745
1746void stm32prog_do_reset(struct stm32prog_data *data)
1747{
1748 if (data->phase == PHASE_RESET) {
1749 data->phase = PHASE_DO_RESET;
1750 puts("Reset requested\n");
1751 }
1752}
1753
1754void stm32prog_next_phase(struct stm32prog_data *data)
1755{
1756 int phase, i;
1757 struct stm32prog_part_t *part;
1758 bool found;
1759
1760 phase = data->phase;
1761 switch (phase) {
1762 case PHASE_RESET:
1763 case PHASE_END:
1764 case PHASE_DO_RESET:
1765 return;
1766 }
1767
1768 /* found next selected partition */
Patrick Delaunayb823d992020-03-18 09:25:00 +01001769 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001770 data->cur_part = NULL;
1771 data->phase = PHASE_END;
1772 found = false;
1773 do {
1774 phase++;
1775 if (phase > PHASE_LAST_USER)
1776 break;
1777 for (i = 0; i < data->part_nb; i++) {
1778 part = &data->part_array[i];
1779 if (part->id == phase) {
1780 if (IS_SELECT(part) && !IS_EMPTY(part)) {
1781 data->cur_part = part;
1782 data->phase = phase;
1783 found = true;
1784 }
1785 break;
1786 }
1787 }
1788 } while (!found);
1789
1790 if (data->phase == PHASE_END)
1791 puts("Phase=END\n");
1792}
1793
Patrick Delaunay291e7222020-03-18 09:24:57 +01001794static int part_delete(struct stm32prog_data *data,
1795 struct stm32prog_part_t *part)
1796{
1797 int ret = 0;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001798 unsigned long blks, blks_offset, blks_size;
1799 struct blk_desc *block_dev = NULL;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001800 char cmdbuf[40];
1801 char devstr[10];
Patrick Delaunay291e7222020-03-18 09:24:57 +01001802
1803 printf("Erasing %s ", part->name);
1804 switch (part->target) {
Patrick Delaunay291e7222020-03-18 09:24:57 +01001805 case STM32PROG_MMC:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001806 if (!IS_ENABLED(CONFIG_MMC)) {
1807 ret = -1;
1808 stm32prog_err("%s (0x%x): erase invalid",
1809 part->name, part->id);
1810 break;
1811 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001812 printf("on mmc %d: ", part->dev->dev_id);
1813 block_dev = mmc_get_blk_desc(part->dev->mmc);
1814 blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len);
1815 blks_size = lldiv(part->size, part->dev->mmc->read_bl_len);
1816 /* -1 or -2 : delete boot partition of MMC
1817 * need to switch to associated hwpart 1 or 2
1818 */
1819 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001820 if (blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001821 part->dev->dev_id,
1822 -part->part_id))
1823 return -1;
1824
1825 blks = blk_derase(block_dev, blks_offset, blks_size);
1826
1827 /* return to user partition */
1828 if (part->part_id < 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -06001829 blk_select_hwpart_devnum(UCLASS_MMC,
Patrick Delaunay291e7222020-03-18 09:24:57 +01001830 part->dev->dev_id, 0);
1831 if (blks != blks_size) {
1832 ret = -1;
1833 stm32prog_err("%s (0x%x): MMC erase failed",
1834 part->name, part->id);
1835 }
1836 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001837 case STM32PROG_NOR:
1838 case STM32PROG_NAND:
1839 case STM32PROG_SPI_NAND:
Patrick Delaunay8040da12020-07-31 16:31:52 +02001840 if (!IS_ENABLED(CONFIG_MTD)) {
1841 ret = -1;
1842 stm32prog_err("%s (0x%x): erase invalid",
1843 part->name, part->id);
1844 break;
1845 }
Patrick Delaunay291e7222020-03-18 09:24:57 +01001846 get_mtd_by_target(devstr, part->target, part->dev->dev_id);
1847 printf("on %s: ", devstr);
1848 sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx",
1849 devstr, part->addr, part->size);
1850 if (run_command(cmdbuf, 0)) {
1851 ret = -1;
1852 stm32prog_err("%s (0x%x): MTD erase commands failed (%s)",
1853 part->name, part->id, cmdbuf);
1854 }
1855 break;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +01001856 case STM32PROG_RAM:
1857 printf("on ram: ");
1858 memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size);
1859 break;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001860 default:
1861 ret = -1;
1862 stm32prog_err("%s (0x%x): erase invalid", part->name, part->id);
1863 break;
1864 }
1865 if (!ret)
1866 printf("done\n");
1867
1868 return ret;
1869}
1870
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001871static void stm32prog_devices_init(struct stm32prog_data *data)
1872{
1873 int i;
1874 int ret;
Patrick Delaunay291e7222020-03-18 09:24:57 +01001875 struct stm32prog_part_t *part;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001876
1877 ret = treat_partition_list(data);
1878 if (ret)
1879 goto error;
1880
Patrick Delaunay8f7eb3e2022-09-06 18:53:17 +02001881 /* empty flashlayout */
1882 if (!data->dev_nb)
1883 return;
1884
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001885 /* initialize the selected device */
1886 for (i = 0; i < data->dev_nb; i++) {
1887 ret = init_device(data, &data->dev[i]);
1888 if (ret)
1889 goto error;
1890 }
1891
Patrick Delaunay291e7222020-03-18 09:24:57 +01001892 /* delete RAW partition before create partition */
1893 for (i = 0; i < data->part_nb; i++) {
1894 part = &data->part_array[i];
1895
1896 if (part->part_type != RAW_IMAGE)
1897 continue;
1898
1899 if (!IS_SELECT(part) || !IS_DELETE(part))
1900 continue;
1901
1902 ret = part_delete(data, part);
1903 if (ret)
1904 goto error;
1905 }
1906
Patrick Delaunay8040da12020-07-31 16:31:52 +02001907 if (IS_ENABLED(CONFIG_MMC)) {
1908 ret = create_gpt_partitions(data);
1909 if (ret)
1910 goto error;
1911 }
Patrick Delaunay7aae1e32020-03-18 09:24:51 +01001912
Patrick Delaunay291e7222020-03-18 09:24:57 +01001913 /* delete partition GPT or MTD */
1914 for (i = 0; i < data->part_nb; i++) {
1915 part = &data->part_array[i];
1916
1917 if (part->part_type == RAW_IMAGE)
1918 continue;
1919
1920 if (!IS_SELECT(part) || !IS_DELETE(part))
1921 continue;
1922
1923 ret = part_delete(data, part);
1924 if (ret)
1925 goto error;
1926 }
1927
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001928 return;
1929
1930error:
1931 data->part_nb = 0;
1932}
1933
1934int stm32prog_dfu_init(struct stm32prog_data *data)
1935{
1936 /* init device if no error */
1937 if (data->part_nb)
1938 stm32prog_devices_init(data);
1939
1940 if (data->part_nb)
1941 stm32prog_next_phase(data);
1942
1943 /* prepare DFU for device read/write */
1944 dfu_free_entities();
1945 return dfu_init_entities(data);
1946}
1947
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +02001948int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size)
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001949{
1950 memset(data, 0x0, sizeof(*data));
Patrick Delaunayb823d992020-03-18 09:25:00 +01001951 data->read_phase = PHASE_RESET;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001952 data->phase = PHASE_FLASHLAYOUT;
1953
1954 return parse_flash_layout(data, addr, size);
1955}
1956
1957void stm32prog_clean(struct stm32prog_data *data)
1958{
1959 /* clean */
1960 dfu_free_entities();
1961 free(data->part_array);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001962 free(data->otp_part);
Patrick Delaunayb823d992020-03-18 09:25:00 +01001963 free(data->buffer);
Patrick Delaunay8da5df92022-03-28 19:25:28 +02001964
1965 if (CONFIG_IS_ENABLED(OPTEE) && data->tee) {
1966 tee_close_session(data->tee, data->tee_session);
1967 data->tee = NULL;
1968 data->tee_session = 0x0;
1969 }
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001970}
1971
1972/* DFU callback: used after serial and direct DFU USB access */
1973void dfu_flush_callback(struct dfu_entity *dfu)
1974{
1975 if (!stm32prog_data)
1976 return;
1977
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001978 if (dfu->dev_type == DFU_DEV_VIRT) {
1979 if (dfu->data.virt.dev_num == PHASE_OTP)
1980 stm32prog_otp_start(stm32prog_data);
Patrick Delaunay541c7de2020-03-18 09:24:59 +01001981 else if (dfu->data.virt.dev_num == PHASE_PMIC)
1982 stm32prog_pmic_start(stm32prog_data);
Patrick Delaunay1d96b182020-03-18 09:24:58 +01001983 return;
1984 }
1985
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001986 if (dfu->dev_type == DFU_DEV_RAM) {
1987 if (dfu->alt == 0 &&
1988 stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001989 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001990 /* waiting DFU DETACH for reenumeration */
1991 }
1992 }
1993
1994 if (!stm32prog_data->cur_part)
1995 return;
1996
1997 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
Patrick Delaunayab198fe2021-05-18 15:12:06 +02001998 stm32prog_end_phase(stm32prog_data, dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001999 stm32prog_next_phase(stm32prog_data);
2000 }
2001}
2002
2003void dfu_initiated_callback(struct dfu_entity *dfu)
2004{
2005 if (!stm32prog_data)
2006 return;
2007
2008 if (!stm32prog_data->cur_part)
2009 return;
2010
2011 /* force the saved offset for the current partition */
2012 if (dfu->alt == stm32prog_data->cur_part->alt_id) {
2013 dfu->offset = stm32prog_data->offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +01002014 stm32prog_data->dfu_seq = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +01002015 log_debug("dfu offset = 0x%llx\n", dfu->offset);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01002016 }
2017}
Patrick Delaunayc7e9a112021-05-18 15:12:13 +02002018
2019void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
2020{
2021 struct stm32prog_data *data = stm32prog_data;
2022
2023 if (!stm32prog_data)
2024 return;
2025
2026 if (!stm32prog_data->cur_part)
2027 return;
2028
2029 if (dfu->alt == stm32prog_data->cur_part->alt_id)
2030 stm32prog_err(msg);
2031}