blob: 481f012c282db62672bd4f58e94990349a5c6e9a [file] [log] [blame]
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Xilinx, Inc.
4 */
5
6#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <part.h>
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +05309#include <asm/io.h>
10#include <asm/arch/hardware.h>
11#include <asm/arch/sys_proto.h>
12#include <u-boot/md5.h>
13#include <zynq_bootimg.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#define ZYNQ_IMAGE_PHDR_OFFSET 0x09C
18#define ZYNQ_IMAGE_FSBL_LEN_OFFSET 0x040
19#define ZYNQ_PART_HDR_CHKSUM_WORD_COUNT 0x0F
20#define ZYNQ_PART_HDR_WORD_COUNT 0x10
21#define ZYNQ_MAXIMUM_IMAGE_WORD_LEN 0x40000000
22#define MD5_CHECKSUM_SIZE 16
23
24struct headerarray {
25 u32 fields[16];
26};
27
28/*
29 * Check whether the given partition is last partition or not
30 */
31static int zynq_islastpartition(struct headerarray *head)
32{
33 int index;
34
35 debug("%s\n", __func__);
36 if (head->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != 0xFFFFFFFF)
37 return -1;
38
39 for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT - 1; index++) {
40 if (head->fields[index] != 0x0)
41 return -1;
42 }
43
44 return 0;
45}
46
47/*
48 * Get the partition count from the partition header
49 */
50int zynq_get_part_count(struct partition_hdr *part_hdr_info)
51{
52 u32 count;
53 struct headerarray *hap;
54
55 debug("%s\n", __func__);
56
57 for (count = 0; count < ZYNQ_MAX_PARTITION_NUMBER; count++) {
58 hap = (struct headerarray *)&part_hdr_info[count];
59 if (zynq_islastpartition(hap) != -1)
60 break;
61 }
62
63 return count;
64}
65
66/*
67 * Get the partition info of all the partitions available.
68 */
69int zynq_get_partition_info(u32 image_base_addr, u32 *fsbl_len,
70 struct partition_hdr *part_hdr)
71{
72 u32 parthdroffset;
73
74 *fsbl_len = *((u32 *)(image_base_addr + ZYNQ_IMAGE_FSBL_LEN_OFFSET));
75
76 parthdroffset = *((u32 *)(image_base_addr + ZYNQ_IMAGE_PHDR_OFFSET));
77
78 parthdroffset += image_base_addr;
79
80 memcpy(part_hdr, (u32 *)parthdroffset,
81 (sizeof(struct partition_hdr) * ZYNQ_MAX_PARTITION_NUMBER));
82
83 return 0;
84}
85
86/*
87 * Check whether the partition header is valid or not
88 */
89int zynq_validate_hdr(struct partition_hdr *header)
90{
91 struct headerarray *hap;
92 u32 index;
93 u32 checksum;
94
95 debug("%s\n", __func__);
96
97 hap = (struct headerarray *)header;
98
99 for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT; index++) {
100 if (hap->fields[index])
101 break;
102 }
103 if (index == ZYNQ_PART_HDR_WORD_COUNT)
104 return -1;
105
106 checksum = 0;
107 for (index = 0; index < ZYNQ_PART_HDR_CHKSUM_WORD_COUNT; index++)
108 checksum += hap->fields[index];
109
110 checksum ^= 0xFFFFFFFF;
111
112 if (hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != checksum) {
113 printf("Error: Checksum 0x%8.8x != 0x%8.8x\n",
114 checksum, hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT]);
115 return -1;
116 }
117
118 if (header->imagewordlen > ZYNQ_MAXIMUM_IMAGE_WORD_LEN) {
119 printf("INVALID_PARTITION_LENGTH\n");
120 return -1;
121 }
122
123 return 0;
124}
125
126/*
127 * Validate the partition by calculationg the md5 checksum for the
128 * partition and compare with checksum present in checksum offset of
129 * partition
130 */
131int zynq_validate_partition(u32 start_addr, u32 len, u32 chksum_off)
132{
133 u8 checksum[MD5_CHECKSUM_SIZE];
134 u8 calchecksum[MD5_CHECKSUM_SIZE];
135
136 memcpy(&checksum[0], (u32 *)chksum_off, MD5_CHECKSUM_SIZE);
137
138 md5_wd((u8 *)start_addr, len, &calchecksum[0], 0x10000);
139
140 if (!memcmp(checksum, calchecksum, MD5_CHECKSUM_SIZE))
141 return 0;
142
143 printf("Error: Partition DataChecksum\n");
144 return -1;
145}