This repository was archived by the owner on Apr 14, 2023. It is now read-only.
forked from Xilinx/bootgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition.cpp
More file actions
executable file
·245 lines (221 loc) · 9.58 KB
/
partition.cpp
File metadata and controls
executable file
·245 lines (221 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/******************************************************************************
* Copyright 2015-2019 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
/*
-------------------------------------------------------------------------------
*********************************************** H E A D E R F I L E S ***
-------------------------------------------------------------------------------
*/
#include <string.h>
#include "bootimage.h"
#include "binary.h"
#include "stringutils.h"
#include "elftools.h"
#include "authentication-zynq.h"
#include "authentication-zynqmp.h"
/*
-------------------------------------------------------------------------------
***************************************************** F U N C T I O N S ***
-------------------------------------------------------------------------------
*/
/******************************************************************************/
Partition::Partition(PartitionHeader* header0, Section* section0)
: header(header0)
{
section = section0;
}
/******************************************************************************/
Partition::Partition(PartitionHeader* header0, const uint8_t* data, Binary::Length_t length)
: header(header0)
{
std::string partName = header->imageHeader->GetName() + StringUtils::Format(".%d",header->index);
/* Pad binary data with 0s, to get 32-bit word size.
This is because bootgen stores partition sizes in terms of words, so partition data *MUST* be a multiple of 32-bits */
int padding = (4 - (length & 3)) & 3;
#if 1
padding = 0;
#endif
Binary::Length_t totallength = length + padding;
section = new Section(partName, totallength);
section->index = header->index;
section->isPartitionData = true;
section->isBitStream = (header->imageHeader->GetDomain() == Domain::PL) ? true : false;
section->isFirstElfSection = (header->firstValidIndex);
section->isBootloader = header->imageHeader->IsBootloader();
memcpy(section->Data, data, length);
memset(section->Data+length, 0, padding);
}
/******************************************************************************/
void Partition::ResizeIfNecessary(Section* section)
{
int padding = (16 - (section->Length & 15)) & 15;
section->IncreaseLengthAndPadTo(section->Length + padding, 0x00);
}
/******************************************************************************/
void Partition::Build( BootImage& bi, Binary& cache)
{
/* Push the section alloted into the Main section */
if(section != NULL)
{
cache.Sections.push_back(section);
}
/* Get the image header from this partition header */
ImageHeader& imageHeader(*this->header->imageHeader);
/* Get the contexts for Authentication & Encryption */
AuthenticationContext* currentAuthCtx = imageHeader.GetAuthContext();
EncryptionContext* encryptCtx = imageHeader.GetEncryptContext();
/* Set default values
Set the address for relative placement for subsequence partitions in image
Don't reserve anything for partition sections */
if(!bi.XipMode)
{
section->Address = 0;
section->Reserve = 0;
}
/* If the partition is a bootloader & XIP mode is enabled, then address is directly
populated based on the QSPI execution address. */
if(!(imageHeader.IsBootloader() && bi.XipMode))
{
/* The offset and reserve attributes only apply to first partition in image
subsequent partitions in the same image are packed right after another */
if (header->IsFirstPartitionInImage())
{
section->Address = imageHeader.GetOffset().ValueOrDefault(0);
section->Reserve = imageHeader.GetReserve().ValueOrDefault(0);
}
else
{
section->continuation = true;
}
}
section->Alignment = imageHeader.GetAlignment().ValueOrDefault(0);
/* If the head and tail of the partition destination are not on a word boundary
we will prepad the data so that DMA can more easily copy the bulk of the data
a whole word at a time. The head and tail bytes are treated seperately */
if (!header->prealigned)
{
/* Use the starting load address to get the number of mis-align word boundary unused bytes at the beginning
of a Partition data.
Byte Addr Word Bytes headAlignment
0 00 00 00 00 0
1 XX 00 00 00 1
2 XX XX 00 00 2
3 XX XX XX 00 3 */
if(header->imageHeader->GetDomain() != Domain::PL)
{
header->headAlignment = header->loadAddress % sizeof(uint32_t);
/* Use the ending load address to get the number of mis-align word boundary unused bytes at the end
of a Partition data.
Byte Addr Word Bytes tailAlignment
0 (1) 00 XX XX XX 3
1 (2) 00 00 XX XX 2
2 (3) 00 00 00 XX 1
3 (0) 00 00 00 00 0 */
uint32_t remainder = (header->loadAddress + section->Length) % sizeof(uint32_t);
header->tailAlignment = (remainder == 0)?0:(4-remainder);
}
section->PadToWordAlignment(header->headAlignment,header->tailAlignment);
}
/* Encryption process on the partition */
if(header->preencrypted && encryptCtx->Type() != Encryption::None)
{
LOG_ERROR("Can't reencrypt a partition that is already encrypted for %s", section->Name.c_str());
}
encryptCtx->Process(bi, header);
uint32_t padLength = 0;
/* Authentication process on the partition */
if(header->presigned)
{
// do nothing
for (std::list<AuthenticationCertificate*>::iterator acs = header->ac.begin(); acs != header->ac.end(); acs++)
{
*acs = 0;
}
}
else
{
header->transferSize = section->Length;
/* Integrity is replaced by Checksum from 2016.3, to maintain backward compatibility,
the bootheader lengths should be populated in case of either checksum or integrity.
Whenever checksum is enabled for FSBL, then add the checksum size to FSBL length */
if((imageHeader.IsBootloader() && imageHeader.GetChecksumContext()->Type() != Checksum::None))
{
padLength = imageHeader.GetChecksumContext()->Size();
}
else
{
if(currentAuthCtx->authAlgorithm->Type() != Authentication::None)
{
padLength = header->GetPartitionPadSize64bBoundary(section);
}
// create AC section and add it to the end of the list.
currentAuthCtx->ResizeIfNecessary(section);
size_t hashPartLen = header->partition->section->Length;
size_t authblock = imageHeader.GetAuthBlock();
if (authblock != 0)
{
hashPartLen = (authblock *1024 *1024);
}
size_t len = 0;
while (len < (header->partition->section->Length))
{
len += hashPartLen;
AuthenticationCertificate* tempacs = NULL;
if (bi.options.archType == Arch::ZYNQ)
{
tempacs = new RSA2048AuthenticationCertificate(currentAuthCtx);
}
else if (bi.options.archType == Arch::ZYNQMP)
{
tempacs = new RSA4096AuthenticationCertificate(currentAuthCtx);
}
if(tempacs != NULL)
{
tempacs->Build(bi, cache, header->partition->section, imageHeader.IsBootloader(), false);
header->ac.push_back(tempacs);
}
}
currentAuthCtx->AddAuthCertSizeToTotalFSBLSize(header);
currentAuthCtx->authBlocks = authblock;
}
}
if(imageHeader.IsBootloader() == true)
{
bi.SetTotalFsblFwSize(imageHeader.GetTotalFsblFwSizeIh() + padLength);
bi.SetTotalPmuFwSize(imageHeader.GetTotalPmuFwSizeIh());
bi.SetPmuFwSize(imageHeader.GetPmuFwSizeIh());
bi.SetFsblFwSize(imageHeader.GetFsblFwSizeIh());
if(header->imageHeader->GetXipMode())
{
// Just to make sure, not to get a negative no. as address
if(imageHeader.GetFsblSourceAddrIh() > imageHeader.GetTotalPmuFwSizeIh())
{
section->Address = imageHeader.GetFsblSourceAddrIh() - imageHeader.GetTotalPmuFwSizeIh();
bi.SetFsblSourceAddr(section->Address);
}
}
}
}
/******************************************************************************/
void Partition::Link(BootImage &bi)
{
for (std::list<AuthenticationCertificate*>::iterator acs = header->ac.begin(); acs != header->ac.end(); acs++)
{
if ((*acs))
{
(*acs)->Link(bi, header->partition->section);
}
}
}