Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 2 | /* |
Michal Simek | 174d7284 | 2023-07-10 14:35:49 +0200 | [diff] [blame] | 3 | * Copyright (C) 2016 Michal Simek <michal.simek@amd.com> |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 4 | * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com> |
| 5 | * |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 6 | * The following Boot Header format/structures and values are defined in the |
| 7 | * following documents: |
Jean-Francois Dagenais | df4950e | 2017-03-23 07:39:14 -0400 | [diff] [blame] | 8 | * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4) |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 9 | * * ug1137 ZynqMP Software Developer Guide v6.0 (Chapter 16) |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 10 | * |
| 11 | * Expected Header Size = 0x9C0 |
| 12 | * Forced as 'little' endian, 32-bit words |
| 13 | * |
| 14 | * 0x 0 - Interrupt table (8 words) |
| 15 | * ... (Default value = 0xeafffffe) |
| 16 | * 0x 1f |
| 17 | * 0x 20 - Width detection |
| 18 | * * DEFAULT_WIDTHDETECTION 0xaa995566 |
| 19 | * 0x 24 - Image identifier |
| 20 | * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58 |
| 21 | * 0x 28 - Encryption |
| 22 | * * 0x00000000 - None |
| 23 | * * 0xa5c3c5a3 - eFuse |
| 24 | * * 0xa5c3c5a7 - obfuscated key in eFUSE |
| 25 | * * 0x3a5c3c5a - bbRam |
| 26 | * * 0xa35c7ca5 - obfuscated key in boot header |
| 27 | * 0x 2C - Image load |
| 28 | * 0x 30 - Image offset |
| 29 | * 0x 34 - PFW image length |
| 30 | * 0x 38 - Total PFW image length |
| 31 | * 0x 3C - Image length |
| 32 | * 0x 40 - Total image length |
| 33 | * 0x 44 - Image attributes |
| 34 | * 0x 48 - Header checksum |
| 35 | * 0x 4c - Obfuscated key |
| 36 | * ... |
| 37 | * 0x 68 |
| 38 | * 0x 6c - Reserved |
| 39 | * 0x 70 - User defined |
| 40 | * ... |
| 41 | * 0x 9c |
| 42 | * 0x a0 - Secure header initialization vector |
| 43 | * ... |
| 44 | * 0x a8 |
| 45 | * 0x ac - Obfuscated key initialization vector |
| 46 | * ... |
| 47 | * 0x b4 |
| 48 | * 0x b8 - Register Initialization, 511 Address and Data word pairs |
| 49 | * * List is terminated with an address of 0xffffffff or |
| 50 | * ... * at the max number of entries |
| 51 | * 0x8b4 |
| 52 | * 0x8b8 - Reserved |
| 53 | * ... |
| 54 | * 0x9bf |
| 55 | * 0x9c0 - Data/Image starts here or above |
| 56 | */ |
| 57 | |
| 58 | #include "imagetool.h" |
| 59 | #include "mkimage.h" |
Alexander Graf | e384cdf | 2018-04-13 14:18:50 +0200 | [diff] [blame] | 60 | #include "zynqmpimage.h" |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 61 | #include <image.h> |
| 62 | |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 63 | static struct zynqmp_header zynqmpimage_header; |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 64 | static void *dynamic_header; |
| 65 | static FILE *fpmu; |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 66 | |
| 67 | static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr) |
| 68 | { |
| 69 | uint32_t checksum = 0; |
| 70 | |
| 71 | if (ptr == NULL) |
| 72 | return 0; |
| 73 | |
| 74 | checksum += le32_to_cpu(ptr->width_detection); |
| 75 | checksum += le32_to_cpu(ptr->image_identifier); |
| 76 | checksum += le32_to_cpu(ptr->encryption); |
| 77 | checksum += le32_to_cpu(ptr->image_load); |
| 78 | checksum += le32_to_cpu(ptr->image_offset); |
| 79 | checksum += le32_to_cpu(ptr->pfw_image_length); |
| 80 | checksum += le32_to_cpu(ptr->total_pfw_image_length); |
| 81 | checksum += le32_to_cpu(ptr->image_size); |
| 82 | checksum += le32_to_cpu(ptr->image_stored_size); |
| 83 | checksum += le32_to_cpu(ptr->image_attributes); |
| 84 | checksum = ~checksum; |
| 85 | |
| 86 | return cpu_to_le32(checksum); |
| 87 | } |
| 88 | |
Alexander Graf | 6915dcf | 2018-04-13 14:18:52 +0200 | [diff] [blame] | 89 | void zynqmpimage_default_header(struct zynqmp_header *ptr) |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 90 | { |
| 91 | int i; |
| 92 | |
| 93 | if (ptr == NULL) |
| 94 | return; |
| 95 | |
| 96 | ptr->width_detection = HEADER_WIDTHDETECTION; |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 97 | ptr->image_attributes = HEADER_CPU_SELECT_A53_64BIT; |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 98 | ptr->image_identifier = HEADER_IMAGEIDENTIFIER; |
| 99 | ptr->encryption = cpu_to_le32(ENCRYPTION_NONE); |
| 100 | |
| 101 | /* Setup not-supported/constant/reserved fields */ |
| 102 | for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) |
| 103 | ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT; |
| 104 | |
| 105 | for (i = 0; i < HEADER_REGINITS; i++) { |
| 106 | ptr->register_init[i].address = HEADER_REGINIT_NULL; |
| 107 | ptr->register_init[i].data = 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Certain reserved fields are required to be set to 0, ensure they are |
| 112 | * set as such. |
| 113 | */ |
| 114 | ptr->pfw_image_length = 0x0; |
| 115 | ptr->total_pfw_image_length = 0x0; |
| 116 | } |
| 117 | |
| 118 | /* mkimage glue functions */ |
| 119 | static int zynqmpimage_verify_header(unsigned char *ptr, int image_size, |
| 120 | struct image_tool_params *params) |
| 121 | { |
| 122 | struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr; |
| 123 | |
| 124 | if (image_size < sizeof(struct zynqmp_header)) |
| 125 | return -1; |
| 126 | |
| 127 | if (zynqhdr->width_detection != HEADER_WIDTHDETECTION) |
| 128 | return -1; |
| 129 | if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER) |
| 130 | return -1; |
| 131 | |
| 132 | if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum) |
| 133 | return -1; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Brandon Maier | c919069 | 2024-01-04 18:50:09 +0000 | [diff] [blame] | 138 | static struct image_header * |
| 139 | find_partition_image(const struct zynqmp_header *zynqhdr, |
| 140 | const struct partition_header *ph) |
| 141 | { |
| 142 | struct partition_header *ph_walk; |
| 143 | struct image_header *ih; |
| 144 | int i; |
| 145 | |
| 146 | for_each_zynqmp_image(zynqhdr, ih) { |
| 147 | for_each_zynqmp_part_in_image(zynqhdr, i, ph_walk, ih) { |
| 148 | if (ph == ph_walk) |
| 149 | return ih; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | static void print_partition_name(const struct zynqmp_header *zynqhdr, |
| 157 | const struct partition_header *ph) |
| 158 | { |
| 159 | const struct image_header *ih; |
| 160 | size_t word_len; |
| 161 | char *name; |
| 162 | int i; |
| 163 | |
| 164 | ih = find_partition_image(zynqhdr, ph); |
| 165 | if (!ih) |
| 166 | return; |
| 167 | |
| 168 | /* Name is stored in big-endian words, find the terminating word and |
| 169 | * byte-swap into a new buffer |
| 170 | */ |
| 171 | word_len = strlen((char *)ih->image_name); |
| 172 | word_len = ALIGN(word_len + 1, 4); |
| 173 | |
| 174 | name = calloc(1, word_len); |
| 175 | if (!name) |
| 176 | return; |
| 177 | |
| 178 | for (i = 0; i < word_len / 4; i++) |
| 179 | ((uint32_t *)name)[i] = uswap_32(ih->image_name[i]); |
| 180 | |
| 181 | printf(" Image name : %s\n", name); |
| 182 | free(name); |
| 183 | } |
| 184 | |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 185 | static void print_partition(const void *ptr, const struct partition_header *ph) |
| 186 | { |
| 187 | uint32_t attr = le32_to_cpu(ph->attributes); |
| 188 | unsigned long len = le32_to_cpu(ph->len) * 4; |
Brandon Maier | 2ddd024 | 2024-01-04 18:50:07 +0000 | [diff] [blame] | 189 | unsigned long len_enc = le32_to_cpu(ph->len_enc) * 4; |
| 190 | unsigned long len_unenc = le32_to_cpu(ph->len_unenc) * 4; |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 191 | const char *part_owner; |
| 192 | const char *dest_devs[0x8] = { |
| 193 | "none", "PS", "PL", "PMU", "unknown", "unknown", "unknown", |
| 194 | "unknown" |
| 195 | }; |
| 196 | |
| 197 | switch (attr & PART_ATTR_PART_OWNER_MASK) { |
| 198 | case PART_ATTR_PART_OWNER_FSBL: |
| 199 | part_owner = "FSBL"; |
| 200 | break; |
| 201 | case PART_ATTR_PART_OWNER_UBOOT: |
| 202 | part_owner = "U-Boot"; |
| 203 | break; |
| 204 | default: |
| 205 | part_owner = "Unknown"; |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | printf("%s payload on CPU %s (%s):\n", part_owner, |
| 210 | dest_cpus[(attr & PART_ATTR_DEST_CPU_MASK) >> 8], |
| 211 | dest_devs[(attr & PART_ATTR_DEST_DEVICE_MASK) >> 4]); |
| 212 | |
Brandon Maier | c919069 | 2024-01-04 18:50:09 +0000 | [diff] [blame] | 213 | print_partition_name(ptr, ph); |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 214 | printf(" Offset : 0x%08x\n", le32_to_cpu(ph->offset) * 4); |
| 215 | printf(" Size : %lu (0x%lx) bytes\n", len, len); |
Brandon Maier | 2ddd024 | 2024-01-04 18:50:07 +0000 | [diff] [blame] | 216 | if (len != len_unenc) |
| 217 | printf(" Size Data : %lu (0x%lx) bytes\n", len_unenc, len_unenc); |
| 218 | if (len_unenc != len_enc) |
| 219 | printf(" Size Enc : %lu (0x%lx) bytes\n", len_unenc, len_unenc); |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 220 | printf(" Load : 0x%08llx", |
| 221 | (unsigned long long)le64_to_cpu(ph->load_address)); |
| 222 | if (ph->load_address != ph->entry_point) |
| 223 | printf(" (entry=0x%08llx)\n", |
| 224 | (unsigned long long)le64_to_cpu(ph->entry_point)); |
| 225 | else |
| 226 | printf("\n"); |
| 227 | printf(" Attributes : "); |
| 228 | |
| 229 | if (attr & PART_ATTR_VEC_LOCATION) |
| 230 | printf("vec "); |
| 231 | |
| 232 | if (attr & PART_ATTR_ENCRYPTED) |
| 233 | printf("encrypted "); |
| 234 | |
| 235 | switch (attr & PART_ATTR_CHECKSUM_MASK) { |
| 236 | case PART_ATTR_CHECKSUM_MD5: |
| 237 | printf("md5 "); |
| 238 | break; |
| 239 | case PART_ATTR_CHECKSUM_SHA2: |
| 240 | printf("sha2 "); |
| 241 | break; |
| 242 | case PART_ATTR_CHECKSUM_SHA3: |
| 243 | printf("sha3 "); |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | if (attr & PART_ATTR_BIG_ENDIAN) |
| 248 | printf("BigEndian "); |
| 249 | |
| 250 | if (attr & PART_ATTR_RSA_SIG) |
| 251 | printf("RSA "); |
| 252 | |
| 253 | if (attr & PART_ATTR_A53_EXEC_AARCH32) |
| 254 | printf("AArch32 "); |
| 255 | |
| 256 | if (attr & PART_ATTR_TARGET_EL_MASK) |
| 257 | printf("EL%d ", (attr & PART_ATTR_TARGET_EL_MASK) >> 1); |
| 258 | |
| 259 | if (attr & PART_ATTR_TZ_SECURE) |
| 260 | printf("secure "); |
| 261 | printf("\n"); |
| 262 | |
| 263 | printf(" Checksum : 0x%08x\n", le32_to_cpu(ph->checksum)); |
| 264 | } |
| 265 | |
Pali Rohár | 2972d7d | 2023-03-29 21:25:54 +0200 | [diff] [blame] | 266 | void zynqmpimage_print_header(const void *ptr, struct image_tool_params *params) |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 267 | { |
| 268 | struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr; |
Brandon Maier | d47935b | 2024-01-04 18:50:08 +0000 | [diff] [blame] | 269 | struct partition_header *ph; |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 270 | int i; |
| 271 | |
Michal Simek | d28baea | 2018-03-14 11:02:24 +0100 | [diff] [blame] | 272 | printf("Image Type : Xilinx ZynqMP Boot Image support\n"); |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 273 | printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset)); |
| 274 | printf("Image Size : %lu bytes (%lu bytes packed)\n", |
| 275 | (unsigned long)le32_to_cpu(zynqhdr->image_size), |
| 276 | (unsigned long)le32_to_cpu(zynqhdr->image_stored_size)); |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 277 | |
| 278 | if (zynqhdr->pfw_image_length) |
| 279 | printf("PMUFW Size : %lu bytes (%lu bytes packed)\n", |
| 280 | (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length), |
| 281 | (unsigned long)le32_to_cpu( |
| 282 | zynqhdr->total_pfw_image_length)); |
| 283 | |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 284 | printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load)); |
| 285 | printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum)); |
| 286 | |
| 287 | for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) { |
| 288 | if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT) |
| 289 | continue; |
| 290 | |
| 291 | printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i, |
| 292 | le32_to_cpu(zynqhdr->interrupt_vectors[i])); |
| 293 | } |
| 294 | |
| 295 | for (i = 0; i < HEADER_REGINITS; i++) { |
| 296 | if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL) |
| 297 | break; |
| 298 | |
| 299 | if (i == 0) |
| 300 | printf("Custom Register Initialization:\n"); |
| 301 | |
| 302 | printf(" @ 0x%08x -> 0x%08x\n", |
| 303 | le32_to_cpu(zynqhdr->register_init[i].address), |
| 304 | le32_to_cpu(zynqhdr->register_init[i].data)); |
| 305 | } |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 306 | |
Brandon Maier | d47935b | 2024-01-04 18:50:08 +0000 | [diff] [blame] | 307 | for_each_zynqmp_part(zynqhdr, i, ph) { |
| 308 | print_partition(ptr, ph); |
Alexander Graf | e9dbfb3 | 2018-04-13 14:18:49 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 311 | free(dynamic_header); |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int zynqmpimage_check_params(struct image_tool_params *params) |
| 315 | { |
| 316 | if (!params) |
| 317 | return 0; |
| 318 | |
| 319 | if (params->addr != 0x0) { |
| 320 | fprintf(stderr, "Error: Load Address cannot be specified.\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * If the entry point is specified ensure it is 64 byte aligned. |
| 326 | */ |
| 327 | if (params->eflag && (params->ep % 64 != 0)) { |
| 328 | fprintf(stderr, |
| 329 | "Error: Entry Point must be aligned to a 64-byte boundary.\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
Brandon Maier | d47935b | 2024-01-04 18:50:08 +0000 | [diff] [blame] | 333 | return !(params->lflag || params->dflag || params->outfile); |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int zynqmpimage_check_image_types(uint8_t type) |
| 337 | { |
| 338 | if (type == IH_TYPE_ZYNQMPIMAGE) |
| 339 | return EXIT_SUCCESS; |
| 340 | return EXIT_FAILURE; |
| 341 | } |
| 342 | |
Michal Simek | 6d0cbbd | 2017-12-05 15:42:25 +0100 | [diff] [blame] | 343 | static uint32_t fsize(FILE *fp) |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 344 | { |
Michal Simek | 6d0cbbd | 2017-12-05 15:42:25 +0100 | [diff] [blame] | 345 | int size, ret, origin; |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 346 | |
Michal Simek | 6d0cbbd | 2017-12-05 15:42:25 +0100 | [diff] [blame] | 347 | origin = ftell(fp); |
| 348 | if (origin < 0) { |
| 349 | fprintf(stderr, "Incorrect file size\n"); |
| 350 | fclose(fp); |
| 351 | exit(2); |
| 352 | } |
| 353 | |
| 354 | ret = fseek(fp, 0L, SEEK_END); |
| 355 | if (ret) { |
| 356 | fprintf(stderr, "Incorrect file SEEK_END\n"); |
| 357 | fclose(fp); |
| 358 | exit(3); |
| 359 | } |
| 360 | |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 361 | size = ftell(fp); |
Michal Simek | 6d0cbbd | 2017-12-05 15:42:25 +0100 | [diff] [blame] | 362 | if (size < 0) { |
| 363 | fprintf(stderr, "Incorrect file size\n"); |
| 364 | fclose(fp); |
| 365 | exit(4); |
| 366 | } |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 367 | |
| 368 | /* going back */ |
Michal Simek | 6d0cbbd | 2017-12-05 15:42:25 +0100 | [diff] [blame] | 369 | ret = fseek(fp, origin, SEEK_SET); |
| 370 | if (ret) { |
| 371 | fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin); |
| 372 | fclose(fp); |
| 373 | exit(3); |
| 374 | } |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 375 | |
| 376 | return size; |
| 377 | } |
| 378 | |
| 379 | static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr, |
| 380 | const char *filename) |
| 381 | { |
| 382 | uint32_t size; |
| 383 | |
| 384 | /* Setup PMU fw size */ |
| 385 | zynqhdr->pfw_image_length = fsize(fpmu); |
| 386 | zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length; |
| 387 | |
| 388 | zynqhdr->image_size -= zynqhdr->pfw_image_length; |
| 389 | zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length; |
| 390 | |
| 391 | /* Read the whole PMUFW to the header */ |
| 392 | size = fread(&zynqhdr->__reserved4[66], 1, |
| 393 | zynqhdr->pfw_image_length, fpmu); |
| 394 | if (size != zynqhdr->pfw_image_length) { |
| 395 | fprintf(stderr, "Cannot read PMUFW file: %s\n", filename); |
| 396 | fclose(fpmu); |
| 397 | exit(1); |
| 398 | } |
| 399 | |
| 400 | fclose(fpmu); |
| 401 | } |
| 402 | |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 403 | static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr, |
| 404 | const char *filename) |
| 405 | { |
Michal Simek | 56c7e80 | 2016-10-21 13:16:13 +0200 | [diff] [blame] | 406 | FILE *fp; |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 407 | struct zynqmp_reginit reginit; |
| 408 | unsigned int reg_count = 0; |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 409 | int r, err; |
Michal Simek | 56c7e80 | 2016-10-21 13:16:13 +0200 | [diff] [blame] | 410 | struct stat path_stat; |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 411 | |
Michal Simek | 56c7e80 | 2016-10-21 13:16:13 +0200 | [diff] [blame] | 412 | /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */ |
| 413 | fp = fopen(filename, "r"); |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 414 | if (!fp) { |
| 415 | fprintf(stderr, "Cannot open initparams file: %s\n", filename); |
| 416 | exit(1); |
| 417 | } |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 418 | |
| 419 | err = fstat(fileno(fp), &path_stat); |
Michal Simek | ac71d41 | 2016-12-20 09:58:31 +0100 | [diff] [blame] | 420 | if (err) { |
| 421 | fclose(fp); |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 422 | return; |
Michal Simek | ac71d41 | 2016-12-20 09:58:31 +0100 | [diff] [blame] | 423 | } |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 424 | |
Michal Simek | ac71d41 | 2016-12-20 09:58:31 +0100 | [diff] [blame] | 425 | if (!S_ISREG(path_stat.st_mode)) { |
| 426 | fclose(fp); |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 427 | return; |
Michal Simek | ac71d41 | 2016-12-20 09:58:31 +0100 | [diff] [blame] | 428 | } |
Michal Simek | ebe0f53 | 2016-12-06 17:17:01 +0100 | [diff] [blame] | 429 | |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 430 | do { |
| 431 | r = fscanf(fp, "%x %x", ®init.address, ®init.data); |
| 432 | if (r == 2) { |
| 433 | zynqhdr->register_init[reg_count] = reginit; |
| 434 | ++reg_count; |
| 435 | } |
| 436 | r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */ |
| 437 | } while ((r != EOF) && (reg_count < HEADER_REGINITS)); |
| 438 | fclose(fp); |
| 439 | } |
| 440 | |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 441 | static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 442 | struct image_tool_params *params) |
| 443 | { |
| 444 | struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr; |
| 445 | zynqmpimage_default_header(zynqhdr); |
| 446 | |
| 447 | /* place image directly after header */ |
| 448 | zynqhdr->image_offset = |
| 449 | cpu_to_le32((uint32_t)sizeof(struct zynqmp_header)); |
| 450 | zynqhdr->image_size = cpu_to_le32(params->file_size - |
| 451 | sizeof(struct zynqmp_header)); |
| 452 | zynqhdr->image_stored_size = zynqhdr->image_size; |
| 453 | zynqhdr->image_load = 0xfffc0000; |
| 454 | if (params->eflag) |
| 455 | zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep); |
| 456 | |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 457 | /* PMUFW */ |
| 458 | if (fpmu) |
| 459 | zynqmpimage_pmufw(zynqhdr, params->imagename); |
| 460 | |
Mike Looijmans | 3b64608 | 2016-09-20 11:37:24 +0200 | [diff] [blame] | 461 | /* User can pass in text file with init list */ |
| 462 | if (strlen(params->imagename2)) |
| 463 | zynqmpimage_parse_initparams(zynqhdr, params->imagename2); |
| 464 | |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 465 | zynqhdr->checksum = zynqmpimage_checksum(zynqhdr); |
| 466 | } |
| 467 | |
Brandon Maier | d47935b | 2024-01-04 18:50:08 +0000 | [diff] [blame] | 468 | static int zynqmpimage_partition_extract(struct zynqmp_header *zynqhdr, |
| 469 | const struct partition_header *ph, |
| 470 | const char *filename) |
| 471 | { |
| 472 | ulong data = (ulong)zynqmp_get_offset(zynqhdr, ph->offset); |
| 473 | unsigned long len = le32_to_cpu(ph->len_enc) * 4; |
| 474 | |
| 475 | return imagetool_save_subimage(filename, data, len); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * zynqmpimage_extract_contents - retrieve a sub-image component from the image |
| 480 | * @ptr: pointer to the image header |
| 481 | * @params: command line parameters |
| 482 | * |
| 483 | * returns: |
| 484 | * zero in case of success or a negative value if fail. |
| 485 | */ |
| 486 | static int zynqmpimage_extract_contents(void *ptr, struct image_tool_params *params) |
| 487 | { |
| 488 | struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr; |
| 489 | struct partition_header *ph; |
| 490 | int i; |
| 491 | |
| 492 | for_each_zynqmp_part(zynqhdr, i, ph) { |
| 493 | if (i == params->pflag) |
| 494 | return zynqmpimage_partition_extract(ptr, ph, params->outfile); |
| 495 | } |
| 496 | |
| 497 | printf("No partition found\n"); |
| 498 | return -1; |
| 499 | } |
| 500 | |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 501 | static int zynqmpimage_vrec_header(struct image_tool_params *params, |
| 502 | struct image_type_params *tparams) |
| 503 | { |
| 504 | struct stat path_stat; |
| 505 | char *filename = params->imagename; |
| 506 | int err; |
| 507 | |
| 508 | /* Handle static case without PMUFW */ |
| 509 | tparams->header_size = sizeof(struct zynqmp_header); |
| 510 | tparams->hdr = (void *)&zynqmpimage_header; |
| 511 | |
| 512 | /* PMUFW name is passed via params->imagename */ |
| 513 | if (strlen(filename) == 0) |
| 514 | return EXIT_SUCCESS; |
| 515 | |
| 516 | fpmu = fopen(filename, "r"); |
| 517 | if (!fpmu) { |
| 518 | fprintf(stderr, "Cannot open PMUFW file: %s\n", filename); |
| 519 | return EXIT_FAILURE; |
| 520 | } |
| 521 | |
| 522 | err = fstat(fileno(fpmu), &path_stat); |
| 523 | if (err) { |
| 524 | fclose(fpmu); |
| 525 | fpmu = NULL; |
| 526 | return EXIT_FAILURE; |
| 527 | } |
| 528 | |
| 529 | if (!S_ISREG(path_stat.st_mode)) { |
| 530 | fclose(fpmu); |
| 531 | fpmu = NULL; |
| 532 | return EXIT_FAILURE; |
| 533 | } |
| 534 | |
| 535 | /* Increase header size by PMUFW file size */ |
| 536 | tparams->header_size += fsize(fpmu); |
| 537 | |
| 538 | /* Allocate buffer with space for PMUFW */ |
| 539 | dynamic_header = calloc(1, tparams->header_size); |
| 540 | tparams->hdr = dynamic_header; |
| 541 | |
| 542 | return EXIT_SUCCESS; |
| 543 | } |
| 544 | |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 545 | U_BOOT_IMAGE_TYPE( |
| 546 | zynqmpimage, |
| 547 | "Xilinx ZynqMP Boot Image support", |
| 548 | sizeof(struct zynqmp_header), |
| 549 | (void *)&zynqmpimage_header, |
| 550 | zynqmpimage_check_params, |
| 551 | zynqmpimage_verify_header, |
| 552 | zynqmpimage_print_header, |
| 553 | zynqmpimage_set_header, |
Brandon Maier | d47935b | 2024-01-04 18:50:08 +0000 | [diff] [blame] | 554 | zynqmpimage_extract_contents, |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 555 | zynqmpimage_check_image_types, |
| 556 | NULL, |
Michal Simek | c85a6b7 | 2016-10-21 12:58:17 +0200 | [diff] [blame] | 557 | zynqmpimage_vrec_header |
Michal Simek | d9b58b3 | 2016-04-27 14:03:29 +0200 | [diff] [blame] | 558 | ); |