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