Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 7 | #include <console.h> |
| 8 | #include <dfu.h> |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 9 | #include <image.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 10 | #include <malloc.h> |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 11 | #include <misc.h> |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 12 | #include <mmc.h> |
Tom Rini | 94a28e9 | 2025-05-21 16:51:21 -0600 | [diff] [blame] | 13 | #include <mtd.h> |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 14 | #include <part.h> |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 15 | #include <tee.h> |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 16 | #include <asm/arch/stm32mp1_smc.h> |
Patrick Delaunay | 285025f | 2024-03-20 15:56:42 +0100 | [diff] [blame] | 17 | #include <asm/arch/sys_proto.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 20 | #include <dm/uclass.h> |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 21 | #include <jffs2/load_kernel.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/list_sort.h> |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 24 | #include <linux/mtd/mtd.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 25 | #include <linux/printk.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 26 | #include <linux/sizes.h> |
| 27 | |
| 28 | #include "stm32prog.h" |
| 29 | |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 30 | /* Primary GPT header size for 128 entries : 17kB = 34 LBA of 512B */ |
| 31 | #define GPT_HEADER_SZ 34 |
| 32 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 33 | #define OPT_SELECT BIT(0) |
| 34 | #define OPT_EMPTY BIT(1) |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 35 | #define OPT_DELETE BIT(2) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 36 | |
| 37 | #define IS_SELECT(part) ((part)->option & OPT_SELECT) |
| 38 | #define IS_EMPTY(part) ((part)->option & OPT_EMPTY) |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 39 | #define IS_DELETE(part) ((part)->option & OPT_DELETE) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 40 | |
| 41 | #define ALT_BUF_LEN SZ_1K |
| 42 | |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 43 | #define ROOTFS_MMC0_UUID \ |
| 44 | EFI_GUID(0xE91C4E10, 0x16E6, 0x4C0E, \ |
| 45 | 0xBD, 0x0E, 0x77, 0xBE, 0xCF, 0x4A, 0x35, 0x82) |
| 46 | |
| 47 | #define ROOTFS_MMC1_UUID \ |
| 48 | EFI_GUID(0x491F6117, 0x415D, 0x4F53, \ |
| 49 | 0x88, 0xC9, 0x6E, 0x0D, 0xE5, 0x4D, 0xEA, 0xC6) |
| 50 | |
| 51 | #define ROOTFS_MMC2_UUID \ |
| 52 | EFI_GUID(0xFD58F1C7, 0xBE0D, 0x4338, \ |
| 53 | 0x88, 0xE9, 0xAD, 0x8F, 0x05, 0x0A, 0xEB, 0x18) |
| 54 | |
Patrick Delaunay | 33029a5 | 2022-03-28 19:25:26 +0200 | [diff] [blame] | 55 | /* RAW partition (binary / bootloader) used Linux - reserved UUID */ |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 56 | #define LINUX_RESERVED_UUID "8DA63339-0007-60C0-C436-083AC8230908" |
| 57 | |
| 58 | /* |
| 59 | * unique partition guid (uuid) for partition named "rootfs" |
| 60 | * on each MMC instance = SD Card or eMMC |
| 61 | * allow fixed kernel bootcmd: "rootf=PARTUID=e91c4e10-..." |
| 62 | */ |
| 63 | static const efi_guid_t uuid_mmc[3] = { |
| 64 | ROOTFS_MMC0_UUID, |
| 65 | ROOTFS_MMC1_UUID, |
| 66 | ROOTFS_MMC2_UUID |
| 67 | }; |
| 68 | |
Patrick Delaunay | c203c21 | 2023-06-08 17:09:56 +0200 | [diff] [blame] | 69 | /* |
| 70 | * GUID value defined in the FWU specification for identification |
| 71 | * of the FWU metadata partition. |
| 72 | */ |
| 73 | #define FWU_MDATA_UUID "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23" |
| 74 | |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 75 | /* FIP type partition UUID used by TF-A*/ |
| 76 | #define FIP_TYPE_UUID "19D5DF83-11B0-457B-BE2C-7559C13142A5" |
| 77 | |
| 78 | /* unique partition guid (uuid) for FIP partitions A/B */ |
| 79 | #define FIP_A_UUID \ |
| 80 | EFI_GUID(0x4FD84C93, 0x54EF, 0x463F, \ |
| 81 | 0xA7, 0xEF, 0xAE, 0x25, 0xFF, 0x88, 0x70, 0x87) |
| 82 | |
| 83 | #define FIP_B_UUID \ |
| 84 | EFI_GUID(0x09C54952, 0xD5BF, 0x45AF, \ |
| 85 | 0xAC, 0xEE, 0x33, 0x53, 0x03, 0x76, 0x6F, 0xB3) |
| 86 | |
| 87 | static const char * const fip_part_name[] = { |
| 88 | "fip-a", |
| 89 | "fip-b" |
| 90 | }; |
| 91 | |
| 92 | static const efi_guid_t fip_part_uuid[] = { |
| 93 | FIP_A_UUID, |
| 94 | FIP_B_UUID |
| 95 | }; |
| 96 | |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 97 | /* order of column in flash layout file */ |
| 98 | enum stm32prog_col_t { |
| 99 | COL_OPTION, |
| 100 | COL_ID, |
| 101 | COL_NAME, |
| 102 | COL_TYPE, |
| 103 | COL_IP, |
| 104 | COL_OFFSET, |
| 105 | COL_NB_STM32 |
| 106 | }; |
| 107 | |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 108 | #define FIP_TOC_HEADER_NAME 0xAA640001 |
| 109 | |
| 110 | struct fip_toc_header { |
| 111 | u32 name; |
| 112 | u32 serial_number; |
| 113 | u64 flags; |
| 114 | }; |
| 115 | |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 116 | #define TA_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \ |
| 117 | { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } } |
| 118 | |
| 119 | /* |
| 120 | * Read NVMEM memory for STM32CubeProgrammer |
| 121 | * |
| 122 | * [in] value[0].a: Type (0 for OTP access) |
| 123 | * [out] memref[1].buffer Output buffer to return all read values |
| 124 | * [out] memref[1].size Size of buffer to be read |
| 125 | * |
| 126 | * Return codes: |
| 127 | * TEE_SUCCESS - Invoke command success |
| 128 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 129 | */ |
| 130 | #define TA_NVMEM_READ 0x0 |
| 131 | |
| 132 | /* |
| 133 | * Write NVMEM memory for STM32CubeProgrammer |
| 134 | * |
| 135 | * [in] value[0].a Type (0 for OTP access) |
| 136 | * [in] memref[1].buffer Input buffer with the values to write |
| 137 | * [in] memref[1].size Size of buffer to be written |
| 138 | * |
| 139 | * Return codes: |
| 140 | * TEE_SUCCESS - Invoke command success |
| 141 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 142 | */ |
| 143 | #define TA_NVMEM_WRITE 0x1 |
| 144 | |
| 145 | /* value of TA_NVMEM type = value[in] a */ |
| 146 | #define NVMEM_OTP 0 |
| 147 | |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 148 | DECLARE_GLOBAL_DATA_PTR; |
| 149 | |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 150 | /* OPTEE TA NVMEM open helper */ |
| 151 | static int optee_ta_open(struct stm32prog_data *data) |
| 152 | { |
| 153 | const struct tee_optee_ta_uuid uuid = TA_NVMEM_UUID; |
| 154 | struct tee_open_session_arg arg; |
| 155 | struct udevice *tee = NULL; |
| 156 | int rc; |
| 157 | |
| 158 | if (data->tee) |
| 159 | return 0; |
| 160 | |
| 161 | tee = tee_find_device(NULL, NULL, NULL, NULL); |
| 162 | if (!tee) |
| 163 | return -ENODEV; |
| 164 | |
| 165 | memset(&arg, 0, sizeof(arg)); |
| 166 | tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); |
| 167 | rc = tee_open_session(tee, &arg, 0, NULL); |
| 168 | if (rc < 0) |
| 169 | return -ENODEV; |
| 170 | |
| 171 | data->tee = tee; |
| 172 | data->tee_session = arg.session; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* OPTEE TA NVMEM invoke helper */ |
| 178 | static int optee_ta_invoke(struct stm32prog_data *data, int cmd, int type, |
| 179 | void *buff, ulong size) |
| 180 | { |
| 181 | struct tee_invoke_arg arg; |
| 182 | struct tee_param param[2]; |
| 183 | struct tee_shm *buff_shm; |
| 184 | int rc; |
| 185 | |
| 186 | rc = tee_shm_register(data->tee, buff, size, 0, &buff_shm); |
| 187 | if (rc) |
| 188 | return rc; |
| 189 | |
| 190 | memset(&arg, 0, sizeof(arg)); |
| 191 | arg.func = cmd; |
| 192 | arg.session = data->tee_session; |
| 193 | |
| 194 | memset(param, 0, sizeof(param)); |
| 195 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 196 | param[0].u.value.a = type; |
| 197 | |
| 198 | if (cmd == TA_NVMEM_WRITE) |
| 199 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; |
| 200 | else |
| 201 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| 202 | |
| 203 | param[1].u.memref.shm = buff_shm; |
| 204 | param[1].u.memref.size = size; |
| 205 | |
| 206 | rc = tee_invoke_func(data->tee, &arg, 2, param); |
| 207 | if (rc < 0 || arg.ret != 0) { |
| 208 | dev_err(data->tee, |
| 209 | "TA_NVMEM invoke failed TEE err: %x, err:%x\n", |
| 210 | arg.ret, rc); |
| 211 | if (!rc) |
| 212 | rc = -EIO; |
| 213 | } |
| 214 | |
| 215 | tee_shm_free(buff_shm); |
| 216 | |
| 217 | return rc; |
| 218 | } |
| 219 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 220 | char *stm32prog_get_error(struct stm32prog_data *data) |
| 221 | { |
| 222 | static const char error_msg[] = "Unspecified"; |
| 223 | |
| 224 | if (strlen(data->error) == 0) |
| 225 | strcpy(data->error, error_msg); |
| 226 | |
| 227 | return data->error; |
| 228 | } |
| 229 | |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 230 | static bool stm32prog_is_fip_header(struct fip_toc_header *header) |
| 231 | { |
| 232 | return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number; |
| 233 | } |
| 234 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 235 | static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header) |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 236 | { |
| 237 | unsigned int i; |
| 238 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 239 | if (header->magic_number != |
| 240 | (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) { |
| 241 | log_debug("%s:invalid magic number : 0x%x\n", |
| 242 | __func__, header->magic_number); |
| 243 | return false; |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 244 | } |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 245 | if (header->header_version != 0x00010000) { |
| 246 | log_debug("%s:invalid header version : 0x%x\n", |
| 247 | __func__, header->header_version); |
| 248 | return false; |
| 249 | } |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 250 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 251 | if (header->reserved1 || header->reserved2) { |
| 252 | log_debug("%s:invalid reserved field\n", __func__); |
| 253 | return false; |
| 254 | } |
| 255 | for (i = 0; i < sizeof(header->padding); i++) { |
| 256 | if (header->padding[i] != 0) { |
| 257 | log_debug("%s:invalid padding field\n", __func__); |
| 258 | return false; |
| 259 | } |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 260 | } |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 261 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | |
| 265 | static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header) |
| 266 | { |
| 267 | unsigned int i; |
| 268 | |
| 269 | if (header->magic_number != |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 270 | (('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 271 | log_debug("%s:invalid magic number : 0x%x\n", |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 272 | __func__, header->magic_number); |
| 273 | return false; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 274 | } |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 275 | if (header->header_version != 0x00020000) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 276 | log_debug("%s:invalid header version : 0x%x\n", |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 277 | __func__, header->header_version); |
| 278 | return false; |
| 279 | } |
| 280 | if (header->reserved1 || header->reserved2) |
| 281 | return false; |
| 282 | |
| 283 | for (i = 0; i < sizeof(header->padding); i++) { |
| 284 | if (header->padding[i] != 0) { |
| 285 | log_debug("%s:invalid padding field\n", __func__); |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header) |
| 294 | { |
| 295 | struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header; |
| 296 | struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header; |
| 297 | |
| 298 | if (!raw_header || !header) { |
| 299 | log_debug("%s:no header data\n", __func__); |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 300 | return; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 301 | } |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 302 | |
| 303 | if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) { |
| 304 | header->type = HEADER_FIP; |
| 305 | header->length = 0; |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 306 | return; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 307 | } |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 308 | if (stm32prog_is_stm32_header_v1(v1_header)) { |
| 309 | header->type = HEADER_STM32IMAGE; |
| 310 | header->image_checksum = le32_to_cpu(v1_header->image_checksum); |
| 311 | header->image_length = le32_to_cpu(v1_header->image_length); |
| 312 | header->length = sizeof(struct stm32_header_v1); |
| 313 | return; |
| 314 | } |
| 315 | if (stm32prog_is_stm32_header_v2(v2_header)) { |
| 316 | header->type = HEADER_STM32IMAGE_V2; |
| 317 | header->image_checksum = le32_to_cpu(v2_header->image_checksum); |
| 318 | header->image_length = le32_to_cpu(v2_header->image_length); |
| 319 | header->length = sizeof(struct stm32_header_v1) + |
| 320 | v2_header->extension_headers_length; |
| 321 | return; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 322 | } |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 323 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 324 | header->type = HEADER_NONE; |
| 325 | header->image_checksum = 0x0; |
| 326 | header->image_length = 0x0; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 329 | static u32 stm32prog_header_checksum(uintptr_t addr, struct image_header_s *header) |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 330 | { |
| 331 | u32 i, checksum; |
| 332 | u8 *payload; |
| 333 | |
| 334 | /* compute checksum on payload */ |
| 335 | payload = (u8 *)addr; |
| 336 | checksum = 0; |
| 337 | for (i = header->image_length; i > 0; i--) |
| 338 | checksum += *(payload++); |
| 339 | |
| 340 | return checksum; |
| 341 | } |
| 342 | |
| 343 | /* FLASHLAYOUT PARSING *****************************************/ |
| 344 | static int parse_option(struct stm32prog_data *data, |
| 345 | int i, char *p, struct stm32prog_part_t *part) |
| 346 | { |
| 347 | int result = 0; |
| 348 | char *c = p; |
| 349 | |
| 350 | part->option = 0; |
| 351 | if (!strcmp(p, "-")) |
| 352 | return 0; |
| 353 | |
| 354 | while (*c) { |
| 355 | switch (*c) { |
| 356 | case 'P': |
| 357 | part->option |= OPT_SELECT; |
| 358 | break; |
| 359 | case 'E': |
| 360 | part->option |= OPT_EMPTY; |
| 361 | break; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 362 | case 'D': |
| 363 | part->option |= OPT_DELETE; |
| 364 | break; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 365 | default: |
| 366 | result = -EINVAL; |
| 367 | stm32prog_err("Layout line %d: invalid option '%c' in %s)", |
| 368 | i, *c, p); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | c++; |
| 372 | } |
| 373 | if (!(part->option & OPT_SELECT)) { |
| 374 | stm32prog_err("Layout line %d: missing 'P' in option %s", i, p); |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | return result; |
| 379 | } |
| 380 | |
| 381 | static int parse_id(struct stm32prog_data *data, |
| 382 | int i, char *p, struct stm32prog_part_t *part) |
| 383 | { |
| 384 | int result = 0; |
| 385 | unsigned long value; |
| 386 | |
| 387 | result = strict_strtoul(p, 0, &value); |
| 388 | part->id = value; |
| 389 | if (result || value > PHASE_LAST_USER) { |
| 390 | stm32prog_err("Layout line %d: invalid phase value = %s", i, p); |
| 391 | result = -EINVAL; |
| 392 | } |
| 393 | |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | static int parse_name(struct stm32prog_data *data, |
| 398 | int i, char *p, struct stm32prog_part_t *part) |
| 399 | { |
| 400 | int result = 0; |
| 401 | |
| 402 | if (strlen(p) < sizeof(part->name)) { |
| 403 | strcpy(part->name, p); |
| 404 | } else { |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 405 | stm32prog_err("Layout line %d: partition name too long [%zd]: %s", |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 406 | i, strlen(p), p); |
| 407 | result = -EINVAL; |
| 408 | } |
| 409 | |
| 410 | return result; |
| 411 | } |
| 412 | |
| 413 | static int parse_type(struct stm32prog_data *data, |
| 414 | int i, char *p, struct stm32prog_part_t *part) |
| 415 | { |
| 416 | int result = 0; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 417 | int len = 0; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 418 | |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 419 | part->bin_nb = 0; |
| 420 | if (!strncmp(p, "Binary", 6)) { |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 421 | part->part_type = PART_BINARY; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 422 | |
| 423 | /* search for Binary(X) case */ |
| 424 | len = strlen(p); |
| 425 | part->bin_nb = 1; |
| 426 | if (len > 6) { |
| 427 | if (len < 8 || |
| 428 | (p[6] != '(') || |
| 429 | (p[len - 1] != ')')) |
| 430 | result = -EINVAL; |
| 431 | else |
| 432 | part->bin_nb = |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 433 | dectoul(&p[7], NULL); |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 434 | } |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 435 | } else if (!strcmp(p, "FIP")) { |
| 436 | part->part_type = PART_FIP; |
Patrick Delaunay | c203c21 | 2023-06-08 17:09:56 +0200 | [diff] [blame] | 437 | } else if (!strcmp(p, "FWU_MDATA")) { |
| 438 | part->part_type = PART_FWU_MDATA; |
Patrick Delaunay | b386b9c | 2023-06-08 17:09:54 +0200 | [diff] [blame] | 439 | } else if (!strcmp(p, "ENV")) { |
| 440 | part->part_type = PART_ENV; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 441 | } else if (!strcmp(p, "System")) { |
| 442 | part->part_type = PART_SYSTEM; |
Patrick Delaunay | 0e582be | 2023-06-08 17:09:55 +0200 | [diff] [blame] | 443 | } else if (!strcmp(p, "ESP")) { |
| 444 | part->part_type = PART_ESP; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 445 | } else if (!strcmp(p, "FileSystem")) { |
| 446 | part->part_type = PART_FILESYSTEM; |
| 447 | } else if (!strcmp(p, "RawImage")) { |
| 448 | part->part_type = RAW_IMAGE; |
| 449 | } else { |
| 450 | result = -EINVAL; |
| 451 | } |
| 452 | if (result) |
| 453 | stm32prog_err("Layout line %d: type parsing error : '%s'", |
| 454 | i, p); |
| 455 | |
| 456 | return result; |
| 457 | } |
| 458 | |
| 459 | static int parse_ip(struct stm32prog_data *data, |
| 460 | int i, char *p, struct stm32prog_part_t *part) |
| 461 | { |
| 462 | int result = 0; |
| 463 | unsigned int len = 0; |
| 464 | |
| 465 | part->dev_id = 0; |
| 466 | if (!strcmp(p, "none")) { |
| 467 | part->target = STM32PROG_NONE; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 468 | } else if (!strncmp(p, "mmc", 3)) { |
| 469 | part->target = STM32PROG_MMC; |
| 470 | len = 3; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 471 | } else if (!strncmp(p, "nor", 3)) { |
| 472 | part->target = STM32PROG_NOR; |
| 473 | len = 3; |
| 474 | } else if (!strncmp(p, "nand", 4)) { |
| 475 | part->target = STM32PROG_NAND; |
| 476 | len = 4; |
| 477 | } else if (!strncmp(p, "spi-nand", 8)) { |
| 478 | part->target = STM32PROG_SPI_NAND; |
| 479 | len = 8; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 480 | } else if (!strncmp(p, "ram", 3)) { |
| 481 | part->target = STM32PROG_RAM; |
| 482 | len = 0; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 483 | } else { |
| 484 | result = -EINVAL; |
| 485 | } |
| 486 | if (len) { |
| 487 | /* only one digit allowed for device id */ |
| 488 | if (strlen(p) != len + 1) { |
| 489 | result = -EINVAL; |
| 490 | } else { |
| 491 | part->dev_id = p[len] - '0'; |
| 492 | if (part->dev_id > 9) |
| 493 | result = -EINVAL; |
| 494 | } |
| 495 | } |
| 496 | if (result) |
| 497 | stm32prog_err("Layout line %d: ip parsing error: '%s'", i, p); |
| 498 | |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | static int parse_offset(struct stm32prog_data *data, |
| 503 | int i, char *p, struct stm32prog_part_t *part) |
| 504 | { |
| 505 | int result = 0; |
| 506 | char *tail; |
| 507 | |
| 508 | part->part_id = 0; |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 509 | part->addr = 0; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 510 | part->size = 0; |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 511 | /* eMMC boot parttion */ |
| 512 | if (!strncmp(p, "boot", 4)) { |
| 513 | if (strlen(p) != 5) { |
| 514 | result = -EINVAL; |
| 515 | } else { |
| 516 | if (p[4] == '1') |
| 517 | part->part_id = -1; |
| 518 | else if (p[4] == '2') |
| 519 | part->part_id = -2; |
| 520 | else |
| 521 | result = -EINVAL; |
| 522 | } |
| 523 | if (result) |
| 524 | stm32prog_err("Layout line %d: invalid part '%s'", |
| 525 | i, p); |
| 526 | } else { |
Patrick Delaunay | 53fe8e7 | 2023-04-27 15:36:37 +0200 | [diff] [blame] | 527 | part->addr = simple_strtoull(p, &tail, 10); |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 528 | if (tail == p || *tail != '\0') { |
| 529 | stm32prog_err("Layout line %d: invalid offset '%s'", |
| 530 | i, p); |
| 531 | result = -EINVAL; |
| 532 | } |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return result; |
| 536 | } |
| 537 | |
| 538 | static |
| 539 | int (* const parse[COL_NB_STM32])(struct stm32prog_data *data, int i, char *p, |
| 540 | struct stm32prog_part_t *part) = { |
| 541 | [COL_OPTION] = parse_option, |
| 542 | [COL_ID] = parse_id, |
| 543 | [COL_NAME] = parse_name, |
| 544 | [COL_TYPE] = parse_type, |
| 545 | [COL_IP] = parse_ip, |
| 546 | [COL_OFFSET] = parse_offset, |
| 547 | }; |
| 548 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 549 | static int parse_flash_layout(struct stm32prog_data *data, |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 550 | uintptr_t addr, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 551 | ulong size) |
| 552 | { |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 553 | int column = 0, part_nb = 0, ret; |
| 554 | bool end_of_line, eof; |
| 555 | char *p, *start, *last, *col; |
| 556 | struct stm32prog_part_t *part; |
Patrick Delaunay | f67fd84 | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 557 | struct image_header_s header; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 558 | int part_list_size; |
| 559 | int i; |
| 560 | |
| 561 | data->part_nb = 0; |
| 562 | |
| 563 | /* check if STM32image is detected */ |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 564 | stm32prog_header_check(addr, &header); |
Patrick Delaunay | f67fd84 | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 565 | if (header.type == HEADER_STM32IMAGE) { |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 566 | u32 checksum; |
| 567 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 568 | addr = addr + header.length; |
Patrick Delaunay | f67fd84 | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 569 | size = header.image_length; |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 570 | |
Patrick Delaunay | f67fd84 | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 571 | checksum = stm32prog_header_checksum(addr, &header); |
| 572 | if (checksum != header.image_checksum) { |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 573 | stm32prog_err("Layout: invalid checksum : 0x%x expected 0x%x", |
Patrick Delaunay | f67fd84 | 2021-05-18 15:12:04 +0200 | [diff] [blame] | 574 | checksum, header.image_checksum); |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 575 | return -EIO; |
| 576 | } |
| 577 | } |
| 578 | if (!size) |
| 579 | return -EINVAL; |
| 580 | |
| 581 | start = (char *)addr; |
| 582 | last = start + size; |
| 583 | |
| 584 | *last = 0x0; /* force null terminated string */ |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 585 | log_debug("flash layout =\n%s\n", start); |
Patrick Delaunay | 526f66c | 2020-03-18 09:24:50 +0100 | [diff] [blame] | 586 | |
| 587 | /* calculate expected number of partitions */ |
| 588 | part_list_size = 1; |
| 589 | p = start; |
| 590 | while (*p && (p < last)) { |
| 591 | if (*p++ == '\n') { |
| 592 | part_list_size++; |
| 593 | if (p < last && *p == '#') |
| 594 | part_list_size--; |
| 595 | } |
| 596 | } |
| 597 | if (part_list_size > PHASE_LAST_USER) { |
| 598 | stm32prog_err("Layout: too many partition (%d)", |
| 599 | part_list_size); |
| 600 | return -1; |
| 601 | } |
| 602 | part = calloc(sizeof(struct stm32prog_part_t), part_list_size); |
| 603 | if (!part) { |
| 604 | stm32prog_err("Layout: alloc failed"); |
| 605 | return -ENOMEM; |
| 606 | } |
| 607 | data->part_array = part; |
| 608 | |
| 609 | /* main parsing loop */ |
| 610 | i = 1; |
| 611 | eof = false; |
| 612 | p = start; |
| 613 | col = start; /* 1st column */ |
| 614 | end_of_line = false; |
| 615 | while (!eof) { |
| 616 | switch (*p) { |
| 617 | /* CR is ignored and replaced by NULL character */ |
| 618 | case '\r': |
| 619 | *p = '\0'; |
| 620 | p++; |
| 621 | continue; |
| 622 | case '\0': |
| 623 | end_of_line = true; |
| 624 | eof = true; |
| 625 | break; |
| 626 | case '\n': |
| 627 | end_of_line = true; |
| 628 | break; |
| 629 | case '\t': |
| 630 | break; |
| 631 | case '#': |
| 632 | /* comment line is skipped */ |
| 633 | if (column == 0 && p == col) { |
| 634 | while ((p < last) && *p) |
| 635 | if (*p++ == '\n') |
| 636 | break; |
| 637 | col = p; |
| 638 | i++; |
| 639 | if (p >= last || !*p) { |
| 640 | eof = true; |
| 641 | end_of_line = true; |
| 642 | } |
| 643 | continue; |
| 644 | } |
| 645 | /* fall through */ |
| 646 | /* by default continue with the next character */ |
| 647 | default: |
| 648 | p++; |
| 649 | continue; |
| 650 | } |
| 651 | |
| 652 | /* replace by \0: allow string parsing for each column */ |
| 653 | *p = '\0'; |
| 654 | p++; |
| 655 | if (p >= last) { |
| 656 | eof = true; |
| 657 | end_of_line = true; |
| 658 | } |
| 659 | |
| 660 | /* skip empty line and multiple TAB in tsv file */ |
| 661 | if (strlen(col) == 0) { |
| 662 | col = p; |
| 663 | /* skip empty line */ |
| 664 | if (column == 0 && end_of_line) { |
| 665 | end_of_line = false; |
| 666 | i++; |
| 667 | } |
| 668 | continue; |
| 669 | } |
| 670 | |
| 671 | if (column < COL_NB_STM32) { |
| 672 | ret = parse[column](data, i, col, part); |
| 673 | if (ret) |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | /* save the beginning of the next column */ |
| 678 | column++; |
| 679 | col = p; |
| 680 | |
| 681 | if (!end_of_line) |
| 682 | continue; |
| 683 | |
| 684 | /* end of the line detected */ |
| 685 | end_of_line = false; |
| 686 | |
| 687 | if (column < COL_NB_STM32) { |
| 688 | stm32prog_err("Layout line %d: no enought column", i); |
| 689 | return -EINVAL; |
| 690 | } |
| 691 | column = 0; |
| 692 | part_nb++; |
| 693 | part++; |
| 694 | i++; |
| 695 | if (part_nb >= part_list_size) { |
| 696 | part = NULL; |
| 697 | if (!eof) { |
| 698 | stm32prog_err("Layout: no enought memory for %d part", |
| 699 | part_nb); |
| 700 | return -EINVAL; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | data->part_nb = part_nb; |
| 705 | if (data->part_nb == 0) { |
| 706 | stm32prog_err("Layout: no partition found"); |
| 707 | return -ENODEV; |
| 708 | } |
| 709 | |
| 710 | return 0; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static int __init part_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 714 | { |
| 715 | struct stm32prog_part_t *parta, *partb; |
| 716 | |
| 717 | parta = container_of(a, struct stm32prog_part_t, list); |
| 718 | partb = container_of(b, struct stm32prog_part_t, list); |
| 719 | |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 720 | if (parta->part_id != partb->part_id) |
| 721 | return parta->part_id - partb->part_id; |
| 722 | else |
| 723 | return parta->addr > partb->addr ? 1 : -1; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 726 | static void get_mtd_by_target(char *string, enum stm32prog_target target, |
| 727 | int dev_id) |
| 728 | { |
| 729 | const char *dev_str; |
| 730 | |
| 731 | switch (target) { |
| 732 | case STM32PROG_NOR: |
| 733 | dev_str = "nor"; |
| 734 | break; |
| 735 | case STM32PROG_NAND: |
| 736 | dev_str = "nand"; |
| 737 | break; |
| 738 | case STM32PROG_SPI_NAND: |
| 739 | dev_str = "spi-nand"; |
| 740 | break; |
| 741 | default: |
| 742 | dev_str = "invalid"; |
| 743 | break; |
| 744 | } |
| 745 | sprintf(string, "%s%d", dev_str, dev_id); |
| 746 | } |
| 747 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 748 | static int init_device(struct stm32prog_data *data, |
| 749 | struct stm32prog_dev_t *dev) |
| 750 | { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 751 | struct mmc *mmc = NULL; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 752 | struct blk_desc *block_dev = NULL; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 753 | struct mtd_info *mtd = NULL; |
Patrice Chotard | 4956908 | 2023-06-08 17:16:40 +0200 | [diff] [blame] | 754 | struct mtd_info *partition; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 755 | char mtd_id[16]; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 756 | int part_id; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 757 | int ret; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 758 | u64 first_addr = 0, last_addr = 0; |
| 759 | struct stm32prog_part_t *part, *next_part; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 760 | u64 part_addr, part_size; |
| 761 | bool part_found; |
| 762 | const char *part_name; |
Patrice Chotard | 4956908 | 2023-06-08 17:16:40 +0200 | [diff] [blame] | 763 | u8 i; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 764 | |
| 765 | switch (dev->target) { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 766 | case STM32PROG_MMC: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 767 | if (!IS_ENABLED(CONFIG_MMC)) { |
| 768 | stm32prog_err("unknown device type = %d", dev->target); |
| 769 | return -ENODEV; |
| 770 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 771 | mmc = find_mmc_device(dev->dev_id); |
Patrick Delaunay | f4aaeed | 2020-07-06 13:20:58 +0200 | [diff] [blame] | 772 | if (!mmc || mmc_init(mmc)) { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 773 | stm32prog_err("mmc device %d not found", dev->dev_id); |
| 774 | return -ENODEV; |
| 775 | } |
| 776 | block_dev = mmc_get_blk_desc(mmc); |
| 777 | if (!block_dev) { |
| 778 | stm32prog_err("mmc device %d not probed", dev->dev_id); |
| 779 | return -ENODEV; |
| 780 | } |
| 781 | dev->erase_size = mmc->erase_grp_size * block_dev->blksz; |
| 782 | dev->mmc = mmc; |
| 783 | |
| 784 | /* reserve a full erase group for each GTP headers */ |
| 785 | if (mmc->erase_grp_size > GPT_HEADER_SZ) { |
| 786 | first_addr = dev->erase_size; |
| 787 | last_addr = (u64)(block_dev->lba - |
| 788 | mmc->erase_grp_size) * |
| 789 | block_dev->blksz; |
| 790 | } else { |
| 791 | first_addr = (u64)GPT_HEADER_SZ * block_dev->blksz; |
| 792 | last_addr = (u64)(block_dev->lba - GPT_HEADER_SZ - 1) * |
| 793 | block_dev->blksz; |
| 794 | } |
Patrice Chotard | fd9a2fa | 2024-11-29 13:27:07 +0100 | [diff] [blame] | 795 | log_debug("MMC %d: lba=%lld blksz=%ld\n", dev->dev_id, |
| 796 | (u64)block_dev->lba, block_dev->blksz); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 797 | log_debug(" available address = 0x%llx..0x%llx\n", |
| 798 | first_addr, last_addr); |
| 799 | log_debug(" full_update = %d\n", dev->full_update); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 800 | break; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 801 | case STM32PROG_NOR: |
| 802 | case STM32PROG_NAND: |
| 803 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 804 | if (!IS_ENABLED(CONFIG_MTD)) { |
| 805 | stm32prog_err("unknown device type = %d", dev->target); |
| 806 | return -ENODEV; |
| 807 | } |
Patrice Chotard | 4956908 | 2023-06-08 17:16:40 +0200 | [diff] [blame] | 808 | /* register partitions with MTDIDS/MTDPARTS or OF fallback */ |
| 809 | mtd_probe_devices(); |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 810 | get_mtd_by_target(mtd_id, dev->target, dev->dev_id); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 811 | log_debug("%s\n", mtd_id); |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 812 | |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 813 | mtd = get_mtd_device_nm(mtd_id); |
| 814 | if (IS_ERR(mtd)) { |
| 815 | stm32prog_err("MTD device %s not found", mtd_id); |
| 816 | return -ENODEV; |
| 817 | } |
| 818 | first_addr = 0; |
| 819 | last_addr = mtd->size; |
| 820 | dev->erase_size = mtd->erasesize; |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 821 | log_debug("MTD device %s: size=%lld erasesize=%d\n", |
| 822 | mtd_id, mtd->size, mtd->erasesize); |
| 823 | log_debug(" available address = 0x%llx..0x%llx\n", |
| 824 | first_addr, last_addr); |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 825 | dev->mtd = mtd; |
| 826 | break; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 827 | case STM32PROG_RAM: |
| 828 | first_addr = gd->bd->bi_dram[0].start; |
| 829 | last_addr = first_addr + gd->bd->bi_dram[0].size; |
| 830 | dev->erase_size = 1; |
| 831 | break; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 832 | default: |
| 833 | stm32prog_err("unknown device type = %d", dev->target); |
| 834 | return -ENODEV; |
| 835 | } |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 836 | log_debug(" erase size = 0x%x\n", dev->erase_size); |
| 837 | log_debug(" full_update = %d\n", dev->full_update); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 838 | |
| 839 | /* order partition list in offset order */ |
| 840 | list_sort(NULL, &dev->part_list, &part_cmp); |
| 841 | part_id = 1; |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 842 | log_debug("id : Opt Phase Name target.n dev.n addr size part_off part_size\n"); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 843 | list_for_each_entry(part, &dev->part_list, list) { |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 844 | if (part->bin_nb > 1) { |
| 845 | if ((dev->target != STM32PROG_NAND && |
| 846 | dev->target != STM32PROG_SPI_NAND) || |
| 847 | part->id >= PHASE_FIRST_USER || |
| 848 | strncmp(part->name, "fsbl", 4)) { |
| 849 | stm32prog_err("%s (0x%x): multiple binary %d not supported", |
| 850 | part->name, part->id, |
| 851 | part->bin_nb); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 855 | if (part->part_type == RAW_IMAGE) { |
| 856 | part->part_id = 0x0; |
| 857 | part->addr = 0x0; |
| 858 | if (block_dev) |
| 859 | part->size = block_dev->lba * block_dev->blksz; |
| 860 | else |
| 861 | part->size = last_addr; |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 862 | log_debug("-- : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx\n", |
| 863 | part->option, part->id, part->name, |
| 864 | part->part_type, part->bin_nb, part->target, |
| 865 | part->dev_id, part->addr, part->size); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 866 | continue; |
| 867 | } |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 868 | if (part->part_id < 0) { /* boot hw partition for eMMC */ |
| 869 | if (mmc) { |
| 870 | part->size = mmc->capacity_boot; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 871 | } else { |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 872 | stm32prog_err("%s (0x%x): hw partition not expected : %d", |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 873 | part->name, part->id, |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 874 | part->part_id); |
| 875 | return -ENODEV; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 876 | } |
| 877 | } else { |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 878 | part->part_id = part_id++; |
| 879 | |
| 880 | /* last partition : size to the end of the device */ |
| 881 | if (part->list.next != &dev->part_list) { |
| 882 | next_part = |
| 883 | container_of(part->list.next, |
| 884 | struct stm32prog_part_t, |
| 885 | list); |
| 886 | if (part->addr < next_part->addr) { |
| 887 | part->size = next_part->addr - |
| 888 | part->addr; |
| 889 | } else { |
| 890 | stm32prog_err("%s (0x%x): same address : 0x%llx == %s (0x%x): 0x%llx", |
| 891 | part->name, part->id, |
| 892 | part->addr, |
| 893 | next_part->name, |
| 894 | next_part->id, |
| 895 | next_part->addr); |
| 896 | return -EINVAL; |
| 897 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 898 | } else { |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 899 | if (part->addr <= last_addr) { |
| 900 | part->size = last_addr - part->addr; |
| 901 | } else { |
| 902 | stm32prog_err("%s (0x%x): invalid address 0x%llx (max=0x%llx)", |
| 903 | part->name, part->id, |
| 904 | part->addr, last_addr); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | } |
| 908 | if (part->addr < first_addr) { |
| 909 | stm32prog_err("%s (0x%x): invalid address 0x%llx (min=0x%llx)", |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 910 | part->name, part->id, |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 911 | part->addr, first_addr); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 912 | return -EINVAL; |
| 913 | } |
| 914 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 915 | if ((part->addr & ((u64)part->dev->erase_size - 1)) != 0) { |
| 916 | stm32prog_err("%s (0x%x): not aligned address : 0x%llx on erase size 0x%x", |
| 917 | part->name, part->id, part->addr, |
| 918 | part->dev->erase_size); |
| 919 | return -EINVAL; |
| 920 | } |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 921 | log_debug("%02d : %1d %02x %14s %02d.%d %02d.%02d %08llx %08llx", |
| 922 | part->part_id, part->option, part->id, part->name, |
| 923 | part->part_type, part->bin_nb, part->target, |
| 924 | part->dev_id, part->addr, part->size); |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 925 | |
| 926 | part_addr = 0; |
| 927 | part_size = 0; |
| 928 | part_found = false; |
| 929 | |
| 930 | /* check coherency with existing partition */ |
| 931 | if (block_dev) { |
| 932 | /* |
| 933 | * block devices with GPT: check user partition size |
| 934 | * only for partial update, the GPT partions are be |
| 935 | * created for full update |
| 936 | */ |
| 937 | if (dev->full_update || part->part_id < 0) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 938 | log_debug("\n"); |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 939 | continue; |
| 940 | } |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 941 | struct disk_partition partinfo; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 942 | |
| 943 | ret = part_get_info(block_dev, part->part_id, |
| 944 | &partinfo); |
| 945 | |
| 946 | if (ret) { |
| 947 | stm32prog_err("%s (0x%x):Couldn't find part %d on device mmc %d", |
| 948 | part->name, part->id, |
| 949 | part_id, part->dev_id); |
| 950 | return -ENODEV; |
| 951 | } |
| 952 | part_addr = (u64)partinfo.start * partinfo.blksz; |
| 953 | part_size = (u64)partinfo.size * partinfo.blksz; |
| 954 | part_name = (char *)partinfo.name; |
| 955 | part_found = true; |
| 956 | } |
| 957 | |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 958 | if (IS_ENABLED(CONFIG_MTD) && mtd) { |
Patrice Chotard | 4956908 | 2023-06-08 17:16:40 +0200 | [diff] [blame] | 959 | i = 0; |
| 960 | list_for_each_entry(partition, &mtd->partitions, node) { |
| 961 | if ((part->part_id - 1) == i) { |
| 962 | part_found = true; |
| 963 | break; |
| 964 | } |
| 965 | i++; |
| 966 | } |
| 967 | if (part_found) { |
| 968 | part_addr = partition->offset; |
| 969 | part_size = partition->size; |
| 970 | part_name = partition->name; |
| 971 | } else { |
| 972 | stm32prog_err("%s (0x%x):Couldn't find part %d on device mtd %s", |
| 973 | part->name, part->id, part->part_id, mtd_id); |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 974 | return -ENODEV; |
| 975 | } |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 976 | } |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 977 | |
Patrick Delaunay | d5de938 | 2020-10-15 14:28:17 +0200 | [diff] [blame] | 978 | /* no partition for this device */ |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 979 | if (!part_found) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 980 | log_debug("\n"); |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 981 | continue; |
| 982 | } |
| 983 | |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 984 | log_debug(" %08llx %08llx\n", part_addr, part_size); |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 985 | |
| 986 | if (part->addr != part_addr) { |
| 987 | stm32prog_err("%s (0x%x): Bad address for partition %d (%s) = 0x%llx <> 0x%llx expected", |
| 988 | part->name, part->id, part->part_id, |
| 989 | part_name, part->addr, part_addr); |
| 990 | return -ENODEV; |
| 991 | } |
| 992 | if (part->size != part_size) { |
| 993 | stm32prog_err("%s (0x%x): Bad size for partition %d (%s) at 0x%llx = 0x%llx <> 0x%llx expected", |
| 994 | part->name, part->id, part->part_id, |
| 995 | part_name, part->addr, part->size, |
| 996 | part_size); |
| 997 | return -ENODEV; |
| 998 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int treat_partition_list(struct stm32prog_data *data) |
| 1004 | { |
| 1005 | int i, j; |
| 1006 | struct stm32prog_part_t *part; |
| 1007 | |
| 1008 | for (j = 0; j < STM32PROG_MAX_DEV; j++) { |
| 1009 | data->dev[j].target = STM32PROG_NONE; |
| 1010 | INIT_LIST_HEAD(&data->dev[j].part_list); |
| 1011 | } |
| 1012 | |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1013 | data->fsbl_nor_detected = false; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1014 | for (i = 0; i < data->part_nb; i++) { |
| 1015 | part = &data->part_array[i]; |
| 1016 | part->alt_id = -1; |
| 1017 | |
| 1018 | /* skip partition with IP="none" */ |
| 1019 | if (part->target == STM32PROG_NONE) { |
| 1020 | if (IS_SELECT(part)) { |
Patrick Delaunay | f5b8571 | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 1021 | stm32prog_err("Layout: selected none phase = 0x%x for part %s", |
| 1022 | part->id, part->name); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1023 | return -EINVAL; |
| 1024 | } |
| 1025 | continue; |
| 1026 | } |
| 1027 | |
| 1028 | if (part->id == PHASE_FLASHLAYOUT || |
| 1029 | part->id > PHASE_LAST_USER) { |
Patrick Delaunay | f5b8571 | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 1030 | stm32prog_err("Layout: invalid phase = 0x%x for part %s", |
| 1031 | part->id, part->name); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1032 | return -EINVAL; |
| 1033 | } |
| 1034 | for (j = i + 1; j < data->part_nb; j++) { |
| 1035 | if (part->id == data->part_array[j].id) { |
Patrick Delaunay | f5b8571 | 2022-01-18 10:33:14 +0100 | [diff] [blame] | 1036 | stm32prog_err("Layout: duplicated phase 0x%x for part %s and %s", |
| 1037 | part->id, part->name, data->part_array[j].name); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1038 | return -EINVAL; |
| 1039 | } |
| 1040 | } |
| 1041 | for (j = 0; j < STM32PROG_MAX_DEV; j++) { |
| 1042 | if (data->dev[j].target == STM32PROG_NONE) { |
| 1043 | /* new device found */ |
| 1044 | data->dev[j].target = part->target; |
| 1045 | data->dev[j].dev_id = part->dev_id; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1046 | data->dev[j].full_update = true; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1047 | data->dev_nb++; |
| 1048 | break; |
| 1049 | } else if ((part->target == data->dev[j].target) && |
| 1050 | (part->dev_id == data->dev[j].dev_id)) { |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | if (j == STM32PROG_MAX_DEV) { |
| 1055 | stm32prog_err("Layout: too many device"); |
| 1056 | return -EINVAL; |
| 1057 | } |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1058 | switch (part->target) { |
| 1059 | case STM32PROG_NOR: |
| 1060 | if (!data->fsbl_nor_detected && |
| 1061 | !strncmp(part->name, "fsbl", 4)) |
| 1062 | data->fsbl_nor_detected = true; |
| 1063 | /* fallthrough */ |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 1064 | default: |
| 1065 | break; |
| 1066 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1067 | part->dev = &data->dev[j]; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1068 | if (!IS_SELECT(part)) |
| 1069 | part->dev->full_update = false; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1070 | list_add_tail(&part->list, &data->dev[j].part_list); |
| 1071 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1076 | static int create_gpt_partitions(struct stm32prog_data *data) |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1077 | { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1078 | int offset = 0; |
| 1079 | const int buflen = SZ_8K; |
| 1080 | char *buf; |
| 1081 | char uuid[UUID_STR_LEN + 1]; |
| 1082 | unsigned char *uuid_bin; |
| 1083 | unsigned int mmc_id; |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1084 | int i, j; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1085 | bool rootfs_found; |
| 1086 | struct stm32prog_part_t *part; |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1087 | const char *type_str; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1088 | |
| 1089 | buf = malloc(buflen); |
| 1090 | if (!buf) |
| 1091 | return -ENOMEM; |
| 1092 | |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1093 | /* initialize the selected device */ |
| 1094 | for (i = 0; i < data->dev_nb; i++) { |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 1095 | /* create gpt partition support only for full update on MMC */ |
| 1096 | if (data->dev[i].target != STM32PROG_MMC || |
| 1097 | !data->dev[i].full_update) |
| 1098 | continue; |
| 1099 | |
Patrick Delaunay | 95a6bf4 | 2022-09-09 17:22:15 +0200 | [diff] [blame] | 1100 | printf("partitions on mmc%d: ", data->dev[i].dev_id); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1101 | offset = 0; |
| 1102 | rootfs_found = false; |
| 1103 | memset(buf, 0, buflen); |
| 1104 | |
| 1105 | list_for_each_entry(part, &data->dev[i].part_list, list) { |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1106 | /* skip eMMC boot partitions */ |
| 1107 | if (part->part_id < 0) |
| 1108 | continue; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1109 | /* skip Raw Image */ |
| 1110 | if (part->part_type == RAW_IMAGE) |
| 1111 | continue; |
| 1112 | |
| 1113 | if (offset + 100 > buflen) { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1114 | log_debug("\n%s: buffer too small, %s skippped", |
| 1115 | __func__, part->name); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1116 | continue; |
| 1117 | } |
| 1118 | |
| 1119 | if (!offset) |
| 1120 | offset += sprintf(buf, "gpt write mmc %d \"", |
| 1121 | data->dev[i].dev_id); |
| 1122 | |
| 1123 | offset += snprintf(buf + offset, buflen - offset, |
| 1124 | "name=%s,start=0x%llx,size=0x%llx", |
| 1125 | part->name, |
| 1126 | part->addr, |
| 1127 | part->size); |
| 1128 | |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1129 | switch (part->part_type) { |
| 1130 | case PART_BINARY: |
| 1131 | type_str = LINUX_RESERVED_UUID; |
| 1132 | break; |
Patrick Delaunay | b386b9c | 2023-06-08 17:09:54 +0200 | [diff] [blame] | 1133 | case PART_ENV: |
| 1134 | type_str = "u-boot-env"; |
| 1135 | break; |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1136 | case PART_FIP: |
| 1137 | type_str = FIP_TYPE_UUID; |
| 1138 | break; |
Patrick Delaunay | c203c21 | 2023-06-08 17:09:56 +0200 | [diff] [blame] | 1139 | case PART_FWU_MDATA: |
| 1140 | type_str = FWU_MDATA_UUID; |
| 1141 | break; |
Patrick Delaunay | 0e582be | 2023-06-08 17:09:55 +0200 | [diff] [blame] | 1142 | case PART_ESP: |
| 1143 | /* EFI System Partition */ |
| 1144 | type_str = "system"; |
| 1145 | break; |
| 1146 | default: /* PART_FILESYSTEM or PART_SYSTEM for distro */ |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1147 | type_str = "linux"; |
| 1148 | break; |
| 1149 | } |
| 1150 | offset += snprintf(buf + offset, |
| 1151 | buflen - offset, |
| 1152 | ",type=%s", type_str); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1153 | |
| 1154 | if (part->part_type == PART_SYSTEM) |
| 1155 | offset += snprintf(buf + offset, |
| 1156 | buflen - offset, |
| 1157 | ",bootable"); |
| 1158 | |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1159 | /* partition UUID */ |
| 1160 | uuid_bin = NULL; |
Patrick Delaunay | 2b89429 | 2025-01-16 10:54:34 +0100 | [diff] [blame] | 1161 | if (!rootfs_found && (!strcmp(part->name, "rootfs") || |
| 1162 | !strcmp(part->name, "rootfs-a"))) { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1163 | mmc_id = part->dev_id; |
| 1164 | rootfs_found = true; |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 1165 | if (mmc_id < ARRAY_SIZE(uuid_mmc)) |
| 1166 | uuid_bin = (unsigned char *)uuid_mmc[mmc_id].b; |
| 1167 | } |
| 1168 | if (part->part_type == PART_FIP) { |
| 1169 | for (j = 0; j < ARRAY_SIZE(fip_part_name); j++) |
| 1170 | if (!strcmp(part->name, fip_part_name[j])) { |
| 1171 | uuid_bin = (unsigned char *)fip_part_uuid[j].b; |
| 1172 | break; |
| 1173 | } |
| 1174 | } |
| 1175 | if (uuid_bin) { |
| 1176 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
| 1177 | offset += snprintf(buf + offset, |
| 1178 | buflen - offset, |
| 1179 | ",uuid=%s", uuid); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | offset += snprintf(buf + offset, buflen - offset, ";"); |
| 1183 | } |
| 1184 | |
| 1185 | if (offset) { |
| 1186 | offset += snprintf(buf + offset, buflen - offset, "\""); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1187 | log_debug("\ncmd: %s\n", buf); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1188 | if (run_command(buf, 0)) { |
| 1189 | stm32prog_err("GPT partitionning fail: %s", |
| 1190 | buf); |
| 1191 | free(buf); |
| 1192 | |
| 1193 | return -1; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | if (data->dev[i].mmc) |
| 1198 | part_init(mmc_get_blk_desc(data->dev[i].mmc)); |
| 1199 | |
| 1200 | #ifdef DEBUG |
| 1201 | sprintf(buf, "gpt verify mmc %d", data->dev[i].dev_id); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1202 | log_debug("\ncmd: %s", buf); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1203 | if (run_command(buf, 0)) |
| 1204 | printf("fail !\n"); |
| 1205 | else |
| 1206 | printf("OK\n"); |
| 1207 | |
| 1208 | sprintf(buf, "part list mmc %d", data->dev[i].dev_id); |
| 1209 | run_command(buf, 0); |
| 1210 | #endif |
Patrick Delaunay | 95a6bf4 | 2022-09-09 17:22:15 +0200 | [diff] [blame] | 1211 | puts("done\n"); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1212 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1213 | |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1214 | #ifdef DEBUG |
| 1215 | run_command("mtd list", 0); |
| 1216 | #endif |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1217 | free(buf); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static int stm32prog_alt_add(struct stm32prog_data *data, |
| 1223 | struct dfu_entity *dfu, |
| 1224 | struct stm32prog_part_t *part) |
| 1225 | { |
| 1226 | int ret = 0; |
| 1227 | int offset = 0; |
| 1228 | char devstr[10]; |
| 1229 | char dfustr[10]; |
| 1230 | char buf[ALT_BUF_LEN]; |
| 1231 | u32 size; |
| 1232 | char multiplier, type; |
| 1233 | |
| 1234 | /* max 3 digit for sector size */ |
Patrice Chotard | 01e5894 | 2024-11-29 13:27:08 +0100 | [diff] [blame] | 1235 | if (part->size > SZ_1G) { |
| 1236 | size = (u32)(part->size / SZ_1G); |
| 1237 | multiplier = 'G'; |
| 1238 | } else if (part->size > SZ_1M) { |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1239 | size = (u32)(part->size / SZ_1M); |
| 1240 | multiplier = 'M'; |
| 1241 | } else if (part->size > SZ_1K) { |
| 1242 | size = (u32)(part->size / SZ_1K); |
| 1243 | multiplier = 'K'; |
| 1244 | } else { |
| 1245 | size = (u32)part->size; |
| 1246 | multiplier = 'B'; |
| 1247 | } |
| 1248 | if (IS_SELECT(part) && !IS_EMPTY(part)) |
| 1249 | type = 'e'; /*Readable and Writeable*/ |
| 1250 | else |
| 1251 | type = 'a';/*Readable*/ |
| 1252 | |
| 1253 | memset(buf, 0, sizeof(buf)); |
| 1254 | offset = snprintf(buf, ALT_BUF_LEN - offset, |
| 1255 | "@%s/0x%02x/1*%d%c%c ", |
| 1256 | part->name, part->id, |
| 1257 | size, multiplier, type); |
| 1258 | |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1259 | if (part->target == STM32PROG_RAM) { |
| 1260 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1261 | "ram 0x%llx 0x%llx", |
| 1262 | part->addr, part->size); |
| 1263 | } else if (part->part_type == RAW_IMAGE) { |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1264 | u64 dfu_size; |
| 1265 | |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1266 | if (part->dev->target == STM32PROG_MMC) |
| 1267 | dfu_size = part->size / part->dev->mmc->read_bl_len; |
| 1268 | else |
| 1269 | dfu_size = part->size; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1270 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1271 | "raw 0x0 0x%llx", dfu_size); |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1272 | } else if (part->part_id < 0) { |
| 1273 | u64 nb_blk = part->size / part->dev->mmc->read_bl_len; |
| 1274 | |
| 1275 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1276 | "raw 0x%llx 0x%llx", |
| 1277 | part->addr, nb_blk); |
| 1278 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
Patrick Delaunay | b5793a6 | 2022-06-16 18:37:59 +0200 | [diff] [blame] | 1279 | " mmcpart %d", -(part->part_id)); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1280 | } else { |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1281 | if (part->part_type == PART_SYSTEM && |
| 1282 | (part->target == STM32PROG_NAND || |
| 1283 | part->target == STM32PROG_NOR || |
| 1284 | part->target == STM32PROG_SPI_NAND)) |
| 1285 | offset += snprintf(buf + offset, |
| 1286 | ALT_BUF_LEN - offset, |
| 1287 | "partubi"); |
| 1288 | else |
| 1289 | offset += snprintf(buf + offset, |
| 1290 | ALT_BUF_LEN - offset, |
| 1291 | "part"); |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1292 | /* dev_id requested by DFU MMC */ |
| 1293 | if (part->target == STM32PROG_MMC) |
| 1294 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
| 1295 | " %d", part->dev_id); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1296 | offset += snprintf(buf + offset, ALT_BUF_LEN - offset, |
Patrick Delaunay | b5793a6 | 2022-06-16 18:37:59 +0200 | [diff] [blame] | 1297 | " %d", part->part_id); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1298 | } |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1299 | ret = -ENODEV; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1300 | switch (part->target) { |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1301 | case STM32PROG_MMC: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1302 | if (IS_ENABLED(CONFIG_MMC)) { |
| 1303 | ret = 0; |
| 1304 | sprintf(dfustr, "mmc"); |
| 1305 | sprintf(devstr, "%d", part->dev_id); |
| 1306 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1307 | break; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1308 | case STM32PROG_NAND: |
| 1309 | case STM32PROG_NOR: |
| 1310 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1311 | if (IS_ENABLED(CONFIG_MTD)) { |
| 1312 | ret = 0; |
| 1313 | sprintf(dfustr, "mtd"); |
| 1314 | get_mtd_by_target(devstr, part->target, part->dev_id); |
| 1315 | } |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 1316 | break; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1317 | case STM32PROG_RAM: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1318 | ret = 0; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1319 | sprintf(dfustr, "ram"); |
| 1320 | sprintf(devstr, "0"); |
| 1321 | break; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1322 | default: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1323 | break; |
| 1324 | } |
| 1325 | if (ret) { |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1326 | stm32prog_err("invalid target: %d", part->target); |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1327 | return ret; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1328 | } |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1329 | log_debug("dfu_alt_add(%s,%s,%s)\n", dfustr, devstr, buf); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1330 | ret = dfu_alt_add(dfu, dfustr, devstr, buf); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1331 | log_debug("dfu_alt_add(%s,%s,%s) result %d\n", |
| 1332 | dfustr, devstr, buf, ret); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1333 | |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
| 1337 | static int stm32prog_alt_add_virt(struct dfu_entity *dfu, |
| 1338 | char *name, int phase, int size) |
| 1339 | { |
| 1340 | int ret = 0; |
| 1341 | char devstr[4]; |
| 1342 | char buf[ALT_BUF_LEN]; |
| 1343 | |
| 1344 | sprintf(devstr, "%d", phase); |
| 1345 | sprintf(buf, "@%s/0x%02x/1*%dBe", name, phase, size); |
| 1346 | ret = dfu_alt_add(dfu, "virt", devstr, buf); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1347 | log_debug("dfu_alt_add(virt,%s,%s) result %d\n", devstr, buf, ret); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1348 | |
| 1349 | return ret; |
| 1350 | } |
| 1351 | |
| 1352 | static int dfu_init_entities(struct stm32prog_data *data) |
| 1353 | { |
| 1354 | int ret = 0; |
| 1355 | int phase, i, alt_id; |
| 1356 | struct stm32prog_part_t *part; |
| 1357 | struct dfu_entity *dfu; |
| 1358 | int alt_nb; |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 1359 | u32 otp_size = 0; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1360 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1361 | alt_nb = 1; /* number of virtual = CMD*/ |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 1362 | |
Patrick Delaunay | 285025f | 2024-03-20 15:56:42 +0100 | [diff] [blame] | 1363 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && !stm32mp_is_closed()) { |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 1364 | /* OTP_SIZE_SMC = 0 if SMC is not supported */ |
| 1365 | otp_size = OTP_SIZE_SMC; |
| 1366 | /* check if PTA BSEC is supported */ |
| 1367 | ret = optee_ta_open(data); |
| 1368 | log_debug("optee_ta_open(PTA_NVMEM) result %d\n", ret); |
| 1369 | if (!ret && data->tee) |
| 1370 | otp_size = OTP_SIZE_TA; |
| 1371 | if (otp_size) |
| 1372 | alt_nb++; /* OTP*/ |
| 1373 | } |
| 1374 | |
Patrick Delaunay | 7d14540 | 2021-05-18 15:12:09 +0200 | [diff] [blame] | 1375 | if (CONFIG_IS_ENABLED(DM_PMIC)) |
| 1376 | alt_nb++; /* PMIC NVMEM*/ |
| 1377 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1378 | if (data->part_nb == 0) |
| 1379 | alt_nb++; /* +1 for FlashLayout */ |
| 1380 | else |
| 1381 | for (i = 0; i < data->part_nb; i++) { |
| 1382 | if (data->part_array[i].target != STM32PROG_NONE) |
| 1383 | alt_nb++; |
| 1384 | } |
| 1385 | |
| 1386 | if (dfu_alt_init(alt_nb, &dfu)) |
| 1387 | return -ENODEV; |
| 1388 | |
| 1389 | puts("DFU alt info setting: "); |
| 1390 | if (data->part_nb) { |
| 1391 | alt_id = 0; |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 1392 | ret = 0; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1393 | for (phase = 1; |
| 1394 | (phase <= PHASE_LAST_USER) && |
| 1395 | (alt_id < alt_nb) && !ret; |
| 1396 | phase++) { |
| 1397 | /* ordering alt setting by phase id */ |
| 1398 | part = NULL; |
| 1399 | for (i = 0; i < data->part_nb; i++) { |
| 1400 | if (phase == data->part_array[i].id) { |
| 1401 | part = &data->part_array[i]; |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
| 1405 | if (!part) |
| 1406 | continue; |
| 1407 | if (part->target == STM32PROG_NONE) |
| 1408 | continue; |
| 1409 | part->alt_id = alt_id; |
| 1410 | alt_id++; |
| 1411 | |
| 1412 | ret = stm32prog_alt_add(data, dfu, part); |
| 1413 | } |
| 1414 | } else { |
| 1415 | char buf[ALT_BUF_LEN]; |
| 1416 | |
| 1417 | sprintf(buf, "@FlashLayout/0x%02x/1*256Ke ram %x 40000", |
Patrick Delaunay | e334d32 | 2022-09-06 18:53:18 +0200 | [diff] [blame] | 1418 | PHASE_FLASHLAYOUT, CONFIG_SYS_LOAD_ADDR); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1419 | ret = dfu_alt_add(dfu, "ram", NULL, buf); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1420 | log_debug("dfu_alt_add(ram, NULL,%s) result %d\n", buf, ret); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | if (!ret) |
Patrick Delaunay | bd57749 | 2021-07-05 09:39:01 +0200 | [diff] [blame] | 1424 | ret = stm32prog_alt_add_virt(dfu, "virtual", PHASE_CMD, CMD_SIZE); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1425 | |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 1426 | if (!ret && IS_ENABLED(CONFIG_CMD_STM32PROG_OTP) && otp_size) |
| 1427 | ret = stm32prog_alt_add_virt(dfu, "OTP", PHASE_OTP, otp_size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1428 | |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1429 | if (!ret && CONFIG_IS_ENABLED(DM_PMIC)) |
Patrick Delaunay | bd57749 | 2021-07-05 09:39:01 +0200 | [diff] [blame] | 1430 | ret = stm32prog_alt_add_virt(dfu, "PMIC", PHASE_PMIC, PMIC_SIZE); |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1431 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1432 | if (ret) |
| 1433 | stm32prog_err("dfu init failed: %d", ret); |
| 1434 | puts("done\n"); |
| 1435 | |
| 1436 | #ifdef DEBUG |
| 1437 | dfu_show_entities(); |
| 1438 | #endif |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1442 | int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1443 | long *size) |
| 1444 | { |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1445 | u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC; |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1446 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1447 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1448 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
| 1449 | stm32prog_err("OTP update not supported"); |
| 1450 | |
| 1451 | return -EOPNOTSUPP; |
| 1452 | } |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1453 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1454 | if (!data->otp_part) { |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1455 | data->otp_part = memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size); |
Patrick Delaunay | 9e1dba3 | 2023-04-27 15:36:36 +0200 | [diff] [blame] | 1456 | if (!data->otp_part) { |
| 1457 | stm32prog_err("OTP write issue %d", -ENOMEM); |
| 1458 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1459 | return -ENOMEM; |
Patrick Delaunay | 9e1dba3 | 2023-04-27 15:36:36 +0200 | [diff] [blame] | 1460 | } |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | if (!offset) |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1464 | memset(data->otp_part, 0, otp_size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1465 | |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1466 | if (offset + *size > otp_size) |
| 1467 | *size = otp_size - offset; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1468 | |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 1469 | memcpy((void *)((uintptr_t)data->otp_part + offset), buffer, *size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1470 | |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1475 | long *size) |
| 1476 | { |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1477 | u32 otp_size = data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1478 | int result = 0; |
| 1479 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1480 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1481 | stm32prog_err("OTP update not supported"); |
| 1482 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1483 | return -EOPNOTSUPP; |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1486 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1487 | /* alway read for first packet */ |
| 1488 | if (!offset) { |
| 1489 | if (!data->otp_part) |
| 1490 | data->otp_part = |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1491 | memalign(CONFIG_SYS_CACHELINE_SIZE, otp_size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1492 | |
| 1493 | if (!data->otp_part) { |
| 1494 | result = -ENOMEM; |
| 1495 | goto end_otp_read; |
| 1496 | } |
| 1497 | |
| 1498 | /* init struct with 0 */ |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1499 | memset(data->otp_part, 0, otp_size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1500 | |
| 1501 | /* call the service */ |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1502 | result = -EOPNOTSUPP; |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1503 | if (data->tee && CONFIG_IS_ENABLED(OPTEE)) |
| 1504 | result = optee_ta_invoke(data, TA_NVMEM_READ, NVMEM_OTP, |
| 1505 | data->otp_part, OTP_SIZE_TA); |
| 1506 | else if (IS_ENABLED(CONFIG_ARM_SMCCC)) |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1507 | result = stm32_smc_exec(STM32_SMC_BSEC, STM32_SMC_READ_ALL, |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 1508 | (unsigned long)data->otp_part, 0); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1509 | if (result) |
| 1510 | goto end_otp_read; |
| 1511 | } |
| 1512 | |
| 1513 | if (!data->otp_part) { |
| 1514 | result = -ENOMEM; |
| 1515 | goto end_otp_read; |
| 1516 | } |
| 1517 | |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1518 | if (offset + *size > otp_size) |
| 1519 | *size = otp_size - offset; |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 1520 | memcpy(buffer, (void *)((uintptr_t)data->otp_part + offset), *size); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1521 | |
| 1522 | end_otp_read: |
Patrick Delaunay | 9e1dba3 | 2023-04-27 15:36:36 +0200 | [diff] [blame] | 1523 | if (result) |
| 1524 | stm32prog_err("OTP read issue %d", result); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1525 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1526 | |
| 1527 | return result; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | int stm32prog_otp_start(struct stm32prog_data *data) |
| 1531 | { |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1532 | int result = 0; |
| 1533 | struct arm_smccc_res res; |
| 1534 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1535 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG_OTP)) { |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1536 | stm32prog_err("OTP update not supported"); |
| 1537 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1538 | return -EOPNOTSUPP; |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1541 | if (!data->otp_part) { |
| 1542 | stm32prog_err("start OTP without data"); |
| 1543 | return -1; |
| 1544 | } |
| 1545 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1546 | result = -EOPNOTSUPP; |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 1547 | if (data->tee && CONFIG_IS_ENABLED(OPTEE)) { |
| 1548 | result = optee_ta_invoke(data, TA_NVMEM_WRITE, NVMEM_OTP, |
| 1549 | data->otp_part, OTP_SIZE_TA); |
| 1550 | } else if (IS_ENABLED(CONFIG_ARM_SMCCC)) { |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1551 | arm_smccc_smc(STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 1552 | (uintptr_t)data->otp_part, 0, 0, 0, 0, 0, &res); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1553 | |
Patrick Delaunay | 455b06a | 2022-03-28 19:25:27 +0200 | [diff] [blame] | 1554 | if (!res.a0) { |
| 1555 | switch (res.a1) { |
| 1556 | case 0: |
| 1557 | result = 0; |
| 1558 | break; |
| 1559 | case 1: |
| 1560 | stm32prog_err("Provisioning"); |
| 1561 | result = 0; |
| 1562 | break; |
| 1563 | default: |
| 1564 | log_err("%s: OTP incorrect value (err = %ld)\n", |
| 1565 | __func__, res.a1); |
| 1566 | result = -EINVAL; |
| 1567 | break; |
| 1568 | } |
| 1569 | } else { |
| 1570 | log_err("%s: Failed to exec svc=%x op=%x in secure mode (err = %ld)\n", |
| 1571 | __func__, STM32_SMC_BSEC, STM32_SMC_WRITE_ALL, res.a0); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1572 | result = -EINVAL; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1573 | } |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | free(data->otp_part); |
| 1577 | data->otp_part = NULL; |
Patrick Delaunay | 9e1dba3 | 2023-04-27 15:36:36 +0200 | [diff] [blame] | 1578 | if (result) |
| 1579 | stm32prog_err("OTP write issue %d", result); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1580 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1581 | |
| 1582 | return result; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1585 | int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1586 | long *size) |
| 1587 | { |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1588 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1589 | |
| 1590 | if (!offset) |
| 1591 | memset(data->pmic_part, 0, PMIC_SIZE); |
| 1592 | |
| 1593 | if (offset + *size > PMIC_SIZE) |
| 1594 | *size = PMIC_SIZE - offset; |
| 1595 | |
| 1596 | memcpy(&data->pmic_part[offset], buffer, *size); |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, u8 *buffer, |
| 1602 | long *size) |
| 1603 | { |
| 1604 | int result = 0, ret; |
| 1605 | struct udevice *dev; |
| 1606 | |
Simon Glass | b24d575 | 2023-02-05 15:40:35 -0700 | [diff] [blame] | 1607 | if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) { |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1608 | stm32prog_err("PMIC update not supported"); |
| 1609 | |
| 1610 | return -EOPNOTSUPP; |
| 1611 | } |
| 1612 | |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1613 | log_debug("%s: %x %lx\n", __func__, offset, *size); |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1614 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1615 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1616 | &dev); |
| 1617 | if (ret) |
| 1618 | return ret; |
| 1619 | |
| 1620 | /* alway request PMIC for first packet */ |
| 1621 | if (!offset) { |
| 1622 | /* init struct with 0 */ |
| 1623 | memset(data->pmic_part, 0, PMIC_SIZE); |
| 1624 | |
| 1625 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1626 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1627 | &dev); |
| 1628 | if (ret) |
| 1629 | return ret; |
| 1630 | |
| 1631 | ret = misc_read(dev, 0xF8, data->pmic_part, PMIC_SIZE); |
| 1632 | if (ret < 0) { |
| 1633 | result = ret; |
| 1634 | goto end_pmic_read; |
| 1635 | } |
| 1636 | if (ret != PMIC_SIZE) { |
| 1637 | result = -EACCES; |
| 1638 | goto end_pmic_read; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | if (offset + *size > PMIC_SIZE) |
| 1643 | *size = PMIC_SIZE - offset; |
| 1644 | |
| 1645 | memcpy(buffer, &data->pmic_part[offset], *size); |
| 1646 | |
| 1647 | end_pmic_read: |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1648 | log_debug("%s: result %i\n", __func__, result); |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1649 | return result; |
| 1650 | } |
| 1651 | |
| 1652 | int stm32prog_pmic_start(struct stm32prog_data *data) |
| 1653 | { |
| 1654 | int ret; |
| 1655 | struct udevice *dev; |
| 1656 | |
Simon Glass | b24d575 | 2023-02-05 15:40:35 -0700 | [diff] [blame] | 1657 | if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) { |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1658 | stm32prog_err("PMIC update not supported"); |
| 1659 | |
| 1660 | return -EOPNOTSUPP; |
| 1661 | } |
| 1662 | |
| 1663 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1664 | DM_DRIVER_GET(stpmic1_nvm), |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 1665 | &dev); |
| 1666 | if (ret) |
| 1667 | return ret; |
| 1668 | |
| 1669 | return misc_write(dev, 0xF8, data->pmic_part, PMIC_SIZE); |
| 1670 | } |
| 1671 | |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1672 | /* copy FSBL on NAND to improve reliability on NAND */ |
| 1673 | static int stm32prog_copy_fsbl(struct stm32prog_part_t *part) |
| 1674 | { |
| 1675 | int ret, i; |
| 1676 | void *fsbl; |
| 1677 | struct image_header_s header; |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 1678 | struct stm32_header_v2 raw_header; /* V2 size > v1 size */ |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1679 | struct dfu_entity *dfu; |
| 1680 | long size, offset; |
| 1681 | |
| 1682 | if (part->target != STM32PROG_NAND && |
| 1683 | part->target != STM32PROG_SPI_NAND) |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1684 | return -EINVAL; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1685 | |
| 1686 | dfu = dfu_get_entity(part->alt_id); |
| 1687 | |
| 1688 | /* read header */ |
| 1689 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 1690 | size = sizeof(raw_header); |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1691 | ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size); |
| 1692 | if (ret) |
| 1693 | return ret; |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1694 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 1695 | stm32prog_header_check((ulong)&raw_header, &header); |
| 1696 | if (header.type != HEADER_STM32IMAGE && |
| 1697 | header.type != HEADER_STM32IMAGE_V2) |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 1698 | return -ENOENT; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* read header + payload */ |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 1701 | size = header.image_length + header.length; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1702 | size = round_up(size, part->dev->mtd->erasesize); |
| 1703 | fsbl = calloc(1, size); |
| 1704 | if (!fsbl) |
| 1705 | return -ENOMEM; |
| 1706 | ret = dfu->read_medium(dfu, 0, fsbl, &size); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1707 | log_debug("%s read size=%lx ret=%d\n", __func__, size, ret); |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1708 | if (ret) |
| 1709 | goto error; |
| 1710 | |
| 1711 | dfu_transaction_cleanup(dfu); |
| 1712 | offset = 0; |
| 1713 | for (i = part->bin_nb - 1; i > 0; i--) { |
| 1714 | offset += size; |
| 1715 | /* write to the next erase block */ |
| 1716 | ret = dfu->write_medium(dfu, offset, fsbl, &size); |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 1717 | log_debug("%s copy at ofset=%lx size=%lx ret=%d", |
| 1718 | __func__, offset, size, ret); |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1719 | if (ret) |
| 1720 | goto error; |
| 1721 | } |
| 1722 | |
| 1723 | error: |
| 1724 | free(fsbl); |
| 1725 | return ret; |
| 1726 | } |
| 1727 | |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1728 | static void stm32prog_end_phase(struct stm32prog_data *data, u64 offset) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1729 | { |
| 1730 | if (data->phase == PHASE_FLASHLAYOUT) { |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 1731 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Patrick Delaunay | e334d32 | 2022-09-06 18:53:18 +0200 | [diff] [blame] | 1732 | if (genimg_get_format((void *)CONFIG_SYS_LOAD_ADDR) == IMAGE_FORMAT_LEGACY) { |
| 1733 | data->script = CONFIG_SYS_LOAD_ADDR; |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 1734 | data->phase = PHASE_END; |
| 1735 | log_notice("U-Boot script received\n"); |
| 1736 | return; |
| 1737 | } |
| 1738 | #endif |
Patrick Delaunay | c14b0eb | 2022-03-28 19:25:33 +0200 | [diff] [blame] | 1739 | log_notice("\nFlashLayout received, size = %lld\n", offset); |
Patrick Delaunay | e334d32 | 2022-09-06 18:53:18 +0200 | [diff] [blame] | 1740 | if (parse_flash_layout(data, CONFIG_SYS_LOAD_ADDR, offset)) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1741 | stm32prog_err("Layout: invalid FlashLayout"); |
| 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | if (!data->cur_part) |
| 1746 | return; |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1747 | |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1748 | if (data->cur_part->target == STM32PROG_RAM) { |
| 1749 | if (data->cur_part->part_type == PART_SYSTEM) |
| 1750 | data->uimage = data->cur_part->addr; |
| 1751 | if (data->cur_part->part_type == PART_FILESYSTEM) |
| 1752 | data->dtb = data->cur_part->addr; |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 1753 | if (data->cur_part->part_type == PART_BINARY) { |
| 1754 | data->initrd = data->cur_part->addr; |
| 1755 | data->initrd_size = offset; |
| 1756 | } |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 1759 | if (CONFIG_IS_ENABLED(MMC) && |
| 1760 | data->cur_part->part_id < 0) { |
| 1761 | char cmdbuf[60]; |
| 1762 | |
| 1763 | sprintf(cmdbuf, "mmc bootbus %d 0 0 0; mmc partconf %d 1 %d 0", |
| 1764 | data->cur_part->dev_id, data->cur_part->dev_id, |
| 1765 | -(data->cur_part->part_id)); |
| 1766 | if (run_command(cmdbuf, 0)) { |
| 1767 | stm32prog_err("commands '%s' failed", cmdbuf); |
| 1768 | return; |
| 1769 | } |
| 1770 | } |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1771 | |
Simon Glass | 8e05bb1 | 2023-02-05 15:40:17 -0700 | [diff] [blame] | 1772 | if (IS_ENABLED(CONFIG_MTD) && |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 1773 | data->cur_part->bin_nb > 1) { |
| 1774 | if (stm32prog_copy_fsbl(data->cur_part)) { |
| 1775 | stm32prog_err("%s (0x%x): copy of fsbl failed", |
| 1776 | data->cur_part->name, data->cur_part->id); |
| 1777 | return; |
| 1778 | } |
| 1779 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | void stm32prog_do_reset(struct stm32prog_data *data) |
| 1783 | { |
| 1784 | if (data->phase == PHASE_RESET) { |
| 1785 | data->phase = PHASE_DO_RESET; |
| 1786 | puts("Reset requested\n"); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | void stm32prog_next_phase(struct stm32prog_data *data) |
| 1791 | { |
| 1792 | int phase, i; |
| 1793 | struct stm32prog_part_t *part; |
| 1794 | bool found; |
| 1795 | |
| 1796 | phase = data->phase; |
| 1797 | switch (phase) { |
| 1798 | case PHASE_RESET: |
| 1799 | case PHASE_END: |
| 1800 | case PHASE_DO_RESET: |
| 1801 | return; |
| 1802 | } |
| 1803 | |
| 1804 | /* found next selected partition */ |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1805 | data->dfu_seq = 0; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1806 | data->cur_part = NULL; |
| 1807 | data->phase = PHASE_END; |
| 1808 | found = false; |
| 1809 | do { |
| 1810 | phase++; |
| 1811 | if (phase > PHASE_LAST_USER) |
| 1812 | break; |
| 1813 | for (i = 0; i < data->part_nb; i++) { |
| 1814 | part = &data->part_array[i]; |
| 1815 | if (part->id == phase) { |
| 1816 | if (IS_SELECT(part) && !IS_EMPTY(part)) { |
| 1817 | data->cur_part = part; |
| 1818 | data->phase = phase; |
| 1819 | found = true; |
| 1820 | } |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
| 1824 | } while (!found); |
| 1825 | |
| 1826 | if (data->phase == PHASE_END) |
| 1827 | puts("Phase=END\n"); |
| 1828 | } |
| 1829 | |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1830 | static int part_delete(struct stm32prog_data *data, |
| 1831 | struct stm32prog_part_t *part) |
| 1832 | { |
| 1833 | int ret = 0; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1834 | unsigned long blks, blks_offset, blks_size; |
| 1835 | struct blk_desc *block_dev = NULL; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1836 | char cmdbuf[40]; |
| 1837 | char devstr[10]; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1838 | |
| 1839 | printf("Erasing %s ", part->name); |
| 1840 | switch (part->target) { |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1841 | case STM32PROG_MMC: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1842 | if (!IS_ENABLED(CONFIG_MMC)) { |
| 1843 | ret = -1; |
| 1844 | stm32prog_err("%s (0x%x): erase invalid", |
| 1845 | part->name, part->id); |
| 1846 | break; |
| 1847 | } |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1848 | printf("on mmc %d: ", part->dev->dev_id); |
| 1849 | block_dev = mmc_get_blk_desc(part->dev->mmc); |
| 1850 | blks_offset = lldiv(part->addr, part->dev->mmc->read_bl_len); |
| 1851 | blks_size = lldiv(part->size, part->dev->mmc->read_bl_len); |
| 1852 | /* -1 or -2 : delete boot partition of MMC |
| 1853 | * need to switch to associated hwpart 1 or 2 |
| 1854 | */ |
| 1855 | if (part->part_id < 0) |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 1856 | if (blk_select_hwpart_devnum(UCLASS_MMC, |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1857 | part->dev->dev_id, |
| 1858 | -part->part_id)) |
| 1859 | return -1; |
| 1860 | |
| 1861 | blks = blk_derase(block_dev, blks_offset, blks_size); |
| 1862 | |
| 1863 | /* return to user partition */ |
| 1864 | if (part->part_id < 0) |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 1865 | blk_select_hwpart_devnum(UCLASS_MMC, |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1866 | part->dev->dev_id, 0); |
| 1867 | if (blks != blks_size) { |
| 1868 | ret = -1; |
| 1869 | stm32prog_err("%s (0x%x): MMC erase failed", |
| 1870 | part->name, part->id); |
| 1871 | } |
| 1872 | break; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1873 | case STM32PROG_NOR: |
| 1874 | case STM32PROG_NAND: |
| 1875 | case STM32PROG_SPI_NAND: |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1876 | if (!IS_ENABLED(CONFIG_MTD)) { |
| 1877 | ret = -1; |
| 1878 | stm32prog_err("%s (0x%x): erase invalid", |
| 1879 | part->name, part->id); |
| 1880 | break; |
| 1881 | } |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1882 | get_mtd_by_target(devstr, part->target, part->dev->dev_id); |
| 1883 | printf("on %s: ", devstr); |
| 1884 | sprintf(cmdbuf, "mtd erase %s 0x%llx 0x%llx", |
| 1885 | devstr, part->addr, part->size); |
| 1886 | if (run_command(cmdbuf, 0)) { |
| 1887 | ret = -1; |
| 1888 | stm32prog_err("%s (0x%x): MTD erase commands failed (%s)", |
| 1889 | part->name, part->id, cmdbuf); |
| 1890 | } |
| 1891 | break; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 1892 | case STM32PROG_RAM: |
| 1893 | printf("on ram: "); |
| 1894 | memset((void *)(uintptr_t)part->addr, 0, (size_t)part->size); |
| 1895 | break; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1896 | default: |
| 1897 | ret = -1; |
| 1898 | stm32prog_err("%s (0x%x): erase invalid", part->name, part->id); |
| 1899 | break; |
| 1900 | } |
| 1901 | if (!ret) |
| 1902 | printf("done\n"); |
| 1903 | |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1907 | static void stm32prog_devices_init(struct stm32prog_data *data) |
| 1908 | { |
| 1909 | int i; |
| 1910 | int ret; |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1911 | struct stm32prog_part_t *part; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1912 | |
| 1913 | ret = treat_partition_list(data); |
| 1914 | if (ret) |
| 1915 | goto error; |
| 1916 | |
Patrick Delaunay | 8f7eb3e | 2022-09-06 18:53:17 +0200 | [diff] [blame] | 1917 | /* empty flashlayout */ |
| 1918 | if (!data->dev_nb) |
| 1919 | return; |
| 1920 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1921 | /* initialize the selected device */ |
| 1922 | for (i = 0; i < data->dev_nb; i++) { |
| 1923 | ret = init_device(data, &data->dev[i]); |
| 1924 | if (ret) |
| 1925 | goto error; |
| 1926 | } |
| 1927 | |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1928 | /* delete RAW partition before create partition */ |
| 1929 | for (i = 0; i < data->part_nb; i++) { |
| 1930 | part = &data->part_array[i]; |
| 1931 | |
| 1932 | if (part->part_type != RAW_IMAGE) |
| 1933 | continue; |
| 1934 | |
| 1935 | if (!IS_SELECT(part) || !IS_DELETE(part)) |
| 1936 | continue; |
| 1937 | |
| 1938 | ret = part_delete(data, part); |
| 1939 | if (ret) |
| 1940 | goto error; |
| 1941 | } |
| 1942 | |
Patrick Delaunay | 8040da1 | 2020-07-31 16:31:52 +0200 | [diff] [blame] | 1943 | if (IS_ENABLED(CONFIG_MMC)) { |
| 1944 | ret = create_gpt_partitions(data); |
| 1945 | if (ret) |
| 1946 | goto error; |
| 1947 | } |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 1948 | |
Patrick Delaunay | 291e722 | 2020-03-18 09:24:57 +0100 | [diff] [blame] | 1949 | /* delete partition GPT or MTD */ |
| 1950 | for (i = 0; i < data->part_nb; i++) { |
| 1951 | part = &data->part_array[i]; |
| 1952 | |
| 1953 | if (part->part_type == RAW_IMAGE) |
| 1954 | continue; |
| 1955 | |
| 1956 | if (!IS_SELECT(part) || !IS_DELETE(part)) |
| 1957 | continue; |
| 1958 | |
| 1959 | ret = part_delete(data, part); |
| 1960 | if (ret) |
| 1961 | goto error; |
| 1962 | } |
| 1963 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1964 | return; |
| 1965 | |
| 1966 | error: |
| 1967 | data->part_nb = 0; |
| 1968 | } |
| 1969 | |
| 1970 | int stm32prog_dfu_init(struct stm32prog_data *data) |
| 1971 | { |
| 1972 | /* init device if no error */ |
| 1973 | if (data->part_nb) |
| 1974 | stm32prog_devices_init(data); |
| 1975 | |
| 1976 | if (data->part_nb) |
| 1977 | stm32prog_next_phase(data); |
| 1978 | |
| 1979 | /* prepare DFU for device read/write */ |
| 1980 | dfu_free_entities(); |
| 1981 | return dfu_init_entities(data); |
| 1982 | } |
| 1983 | |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 1984 | int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size) |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1985 | { |
| 1986 | memset(data, 0x0, sizeof(*data)); |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1987 | data->read_phase = PHASE_RESET; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1988 | data->phase = PHASE_FLASHLAYOUT; |
| 1989 | |
| 1990 | return parse_flash_layout(data, addr, size); |
| 1991 | } |
| 1992 | |
| 1993 | void stm32prog_clean(struct stm32prog_data *data) |
| 1994 | { |
| 1995 | /* clean */ |
| 1996 | dfu_free_entities(); |
| 1997 | free(data->part_array); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 1998 | free(data->otp_part); |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 1999 | free(data->buffer); |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 2000 | |
| 2001 | if (CONFIG_IS_ENABLED(OPTEE) && data->tee) { |
| 2002 | tee_close_session(data->tee, data->tee_session); |
| 2003 | data->tee = NULL; |
| 2004 | data->tee_session = 0x0; |
| 2005 | } |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* DFU callback: used after serial and direct DFU USB access */ |
| 2009 | void dfu_flush_callback(struct dfu_entity *dfu) |
| 2010 | { |
| 2011 | if (!stm32prog_data) |
| 2012 | return; |
| 2013 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 2014 | if (dfu->dev_type == DFU_DEV_VIRT) { |
| 2015 | if (dfu->data.virt.dev_num == PHASE_OTP) |
| 2016 | stm32prog_otp_start(stm32prog_data); |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 2017 | else if (dfu->data.virt.dev_num == PHASE_PMIC) |
| 2018 | stm32prog_pmic_start(stm32prog_data); |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 2022 | if (dfu->dev_type == DFU_DEV_RAM) { |
| 2023 | if (dfu->alt == 0 && |
| 2024 | stm32prog_data->phase == PHASE_FLASHLAYOUT) { |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 2025 | stm32prog_end_phase(stm32prog_data, dfu->offset); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 2026 | /* waiting DFU DETACH for reenumeration */ |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | if (!stm32prog_data->cur_part) |
| 2031 | return; |
| 2032 | |
| 2033 | if (dfu->alt == stm32prog_data->cur_part->alt_id) { |
Patrick Delaunay | ab198fe | 2021-05-18 15:12:06 +0200 | [diff] [blame] | 2034 | stm32prog_end_phase(stm32prog_data, dfu->offset); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 2035 | stm32prog_next_phase(stm32prog_data); |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | void dfu_initiated_callback(struct dfu_entity *dfu) |
| 2040 | { |
| 2041 | if (!stm32prog_data) |
| 2042 | return; |
| 2043 | |
| 2044 | if (!stm32prog_data->cur_part) |
| 2045 | return; |
| 2046 | |
| 2047 | /* force the saved offset for the current partition */ |
| 2048 | if (dfu->alt == stm32prog_data->cur_part->alt_id) { |
| 2049 | dfu->offset = stm32prog_data->offset; |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 2050 | stm32prog_data->dfu_seq = 0; |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 2051 | log_debug("dfu offset = 0x%llx\n", dfu->offset); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 2052 | } |
| 2053 | } |
Patrick Delaunay | c7e9a11 | 2021-05-18 15:12:13 +0200 | [diff] [blame] | 2054 | |
| 2055 | void dfu_error_callback(struct dfu_entity *dfu, const char *msg) |
| 2056 | { |
| 2057 | struct stm32prog_data *data = stm32prog_data; |
| 2058 | |
| 2059 | if (!stm32prog_data) |
| 2060 | return; |
| 2061 | |
| 2062 | if (!stm32prog_data->cur_part) |
| 2063 | return; |
| 2064 | |
| 2065 | if (dfu->alt == stm32prog_data->cur_part->alt_id) |
| 2066 | stm32prog_err(msg); |
| 2067 | } |