1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Xilinx, Inc.
8 #include <asm/arch/hardware.h>
9 #include <asm/arch/sys_proto.h>
10 #include <u-boot/md5.h>
11 #include <zynq_bootimg.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 #define ZYNQ_IMAGE_PHDR_OFFSET 0x09C
16 #define ZYNQ_IMAGE_FSBL_LEN_OFFSET 0x040
17 #define ZYNQ_PART_HDR_CHKSUM_WORD_COUNT 0x0F
18 #define ZYNQ_PART_HDR_WORD_COUNT 0x10
19 #define ZYNQ_MAXIMUM_IMAGE_WORD_LEN 0x40000000
20 #define MD5_CHECKSUM_SIZE 16
27 * Check whether the given partition is last partition or not
29 static int zynq_islastpartition(struct headerarray *head)
33 debug("%s\n", __func__);
34 if (head->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != 0xFFFFFFFF)
37 for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT - 1; index++) {
38 if (head->fields[index] != 0x0)
46 * Get the partition count from the partition header
48 int zynq_get_part_count(struct partition_hdr *part_hdr_info)
51 struct headerarray *hap;
53 debug("%s\n", __func__);
55 for (count = 0; count < ZYNQ_MAX_PARTITION_NUMBER; count++) {
56 hap = (struct headerarray *)&part_hdr_info[count];
57 if (zynq_islastpartition(hap) != -1)
65 * Get the partition info of all the partitions available.
67 int zynq_get_partition_info(u32 image_base_addr, u32 *fsbl_len,
68 struct partition_hdr *part_hdr)
72 *fsbl_len = *((u32 *)(image_base_addr + ZYNQ_IMAGE_FSBL_LEN_OFFSET));
74 parthdroffset = *((u32 *)(image_base_addr + ZYNQ_IMAGE_PHDR_OFFSET));
76 parthdroffset += image_base_addr;
78 memcpy(part_hdr, (u32 *)parthdroffset,
79 (sizeof(struct partition_hdr) * ZYNQ_MAX_PARTITION_NUMBER));
85 * Check whether the partition header is valid or not
87 int zynq_validate_hdr(struct partition_hdr *header)
89 struct headerarray *hap;
93 debug("%s\n", __func__);
95 hap = (struct headerarray *)header;
97 for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT; index++) {
98 if (hap->fields[index])
101 if (index == ZYNQ_PART_HDR_WORD_COUNT)
105 for (index = 0; index < ZYNQ_PART_HDR_CHKSUM_WORD_COUNT; index++)
106 checksum += hap->fields[index];
108 checksum ^= 0xFFFFFFFF;
110 if (hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != checksum) {
111 printf("Error: Checksum 0x%8.8x != 0x%8.8x\n",
112 checksum, hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT]);
116 if (header->imagewordlen > ZYNQ_MAXIMUM_IMAGE_WORD_LEN) {
117 printf("INVALID_PARTITION_LENGTH\n");
125 * Validate the partition by calculationg the md5 checksum for the
126 * partition and compare with checksum present in checksum offset of
129 int zynq_validate_partition(u32 start_addr, u32 len, u32 chksum_off)
131 u8 checksum[MD5_CHECKSUM_SIZE];
132 u8 calchecksum[MD5_CHECKSUM_SIZE];
134 memcpy(&checksum[0], (u32 *)chksum_off, MD5_CHECKSUM_SIZE);
136 md5_wd((u8 *)start_addr, len, &calchecksum[0], 0x10000);
138 if (!memcmp(checksum, calchecksum, MD5_CHECKSUM_SIZE))
141 printf("Error: Partition DataChecksum\n");