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