forked from Xilinx/bootgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootimage.cpp
executable file
·632 lines (560 loc) · 18.4 KB
/
bootimage.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/******************************************************************************
* Copyright 2015-2020 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 <fstream>
#include <algorithm>
#include <string>
#include "bootimage.h"
#include "bootimage-zynq.h"
#include "bootimage-zynqmp.h"
#include "bootimage-versal.h"
#include "bifscanner.h"
#include "bootheader.h"
#include "options.h"
#include "bootgenexception.h"
#include "outputfile.h"
#include "binary.h"
#include "stringutils.h"
#include "checksum.h"
#include "logger.h"
#include "bitutils.h"
/*
------------------------------------------------------------------------------
**************************************************** F U N C T I O N S ***
------------------------------------------------------------------------------
*/
/******************************************************************************/
void BIF_File::Process(Options& options)
{
std::string basefile = StringUtils::BaseName(biffilename);
LOG_TRACE("BIF file parsing started");
BIF::FlexScanner scanner;
BIF::BisonParser parser(scanner, options);
scanner.filename = biffilename;
std::ifstream s(biffilename.c_str());
if (!s)
{
LOG_ERROR("Cannot read BIF file - %s ", basefile.c_str());
}
if(s.peek() == EOF)
{
LOG_ERROR("BIF file is empty - %s ", basefile.c_str());
}
scanner.switch_streams(&s);
int res = parser.parse();
if (res)
{
LOG_ERROR("BIF file parsing failed with code %d", res);
}
LOG_INFO("BIF file parsing completed successfully");
//bifOptionList.push_back(options.bifOptions);
bifOptionList = options.bifOptionsList;
BootImage* currentbi = NULL;
uint8_t index = 0;
for (std::vector<BifOptions*>::iterator bifoptions = bifOptionList.begin(); bifoptions != bifOptionList.end();bifoptions++)
{
if (options.archType == Arch::ZYNQMP)
{
currentbi = new ZynqMpBootImage(options, index);
}
else if (options.archType == Arch::VERSAL)
{
currentbi = new VersalBootImage(options, index);
}
else
{
currentbi = new ZynqBootImage(options, index);
}
if (currentbi != NULL)
{
currentbi->Add(*bifoptions);
bootImages.push_back(currentbi);
}
Output(options, index);
index++;
}
}
/******************************************************************************/
void BIF_File::Output(Options& options, uint8_t index)
{
File::Type splitFileType = options.GetSplitType();
std::list<std::string> outFilename = options.GetOutputFileNames();
BootImage* bi = bootImages.at(index);
{
if (options.GetAuthKeyGeneration() != GenAuthKeys::None)
{
bi->GenerateAuthenticationKeys();
return;
}
if (options.GetGreyKeyGeneration())
{
bi->GenerateGreyKey();
return;
}
if (options.GetMetalKeyGeneration())
{
LOG_ERROR("The support to generate Family Key is deprecated.");
}
LOG_INFO("Building image - %s", bi->Name.c_str());
bi->BuildAndLink(bi->cache);
bi->SetOutputSplitModeFormat(options.bifOptions->GetSplitMode(), options.bifOptions->GetSplitFormat());
bi->SetOutputBitstreamModeFormat(options.GetProcessBitstreamType());
OutputMode::Type outputMode = options.GetOutputMode();
bi->ValidateOutputModes(splitFileType, outputMode);
if (outputMode == OutputMode::OUT_SPLIT_NORMAL || outputMode == OutputMode::OUT_SPLIT_SLAVEBOOT || splitFileType != File::Unknown)
{
outFilename.clear();
std::string fName = StringUtils::RemoveExtension(biffilename);
fName = fName.substr(fName.find_last_of("\\")+1, std::string::npos);
if(splitFileType == File::Unknown)
{
splitFileType = options.GetOutputFormat();
}
fName += (splitFileType == File::MCS) ? ".mcs" : ".bin";
outFilename.push_back(fName);
}
/* Process Bitstream Mode
Create a file with the bitstream filename, add proper extension */
if (outputMode == OutputMode::OUT_BITSTREAM)
{
outFilename.clear();
std::string fName = bi->bitFilename;
if(bi->bitFilename.empty())
{
LOG_ERROR("No bitstream in the BIF file - %s ",options.GetBifFilename().c_str());
}
fName += (options.GetProcessBitstreamType() == File::MCS) ? ".mcs" : ".bin";
outFilename.push_back(fName);
}
bi->OutputPartitionFiles(options, *bi->cache);
/* Create a BIN/MCS file format from the factory
Write to the file outputs based on the format */
for(std::list<std::string>::iterator filename = outFilename.begin();filename != outFilename.end(); filename++)
{
std::string out_filename;
if (index == (options.bifOptionsList.size() - 1))
{
out_filename = *filename;
}
else
{
out_filename = StringUtils::RemoveExtension(*filename) + "_" + bi->Name + StringUtils::GetExtension(*filename);
}
OutputFile* file = OutputFile::Factory(out_filename);
file->Output(options, *bi->cache);
delete file;
}
/* Efuse PPK hash for burning the efuse */
bi->OutputOptionalEfuseHash();
/* Secure Debug Image to enable the JTAG during a secure boot scenario */
bi->OutputOptionalSecureDebugImage();
}
}
/******************************************************************************/
BootImage::BootImage(Options& options, uint8_t index)
: options(options)
, headerSignature_Loaded(false)
, headerAC(0)
, XipMode(false)
, nullPartHeaderSection(NULL)
, core(Core::A53Singlex64)
, legacyEncryptionEnabled(true)
, hash(NULL)
, totalHeadersSize(0)
, currentChecksumCtx(NULL)
, pmuFwSize(0)
, fsblFwSize(0)
, totalPmuFwSize(0)
, totalFsblFwSize(0)
, sourceAddr(0)
, bootloaderFound(false)
, bootloaderAuthenticate(false)
, bootloaderEncrypt(false)
, bootloaderKeySource(KeySource::None)
, keygen(NULL)
, partCount(0)
, authHash(AuthHash::Sha3)
, assumeEncryption(false)
, convertAieElfToCdo(false)
, createSubSystemPdis(false)
, encryptedHeaders(NULL)
, bootHeader(NULL)
, imageHeaderTable(NULL)
, partitionHeaderTable(NULL)
, currentAuthCtx(NULL)
, currentEncryptCtx(NULL)
, partitionOutput(NULL)
, deviceKey(NULL)
, firstIv(NULL)
, firstOptKey(NULL)
{
bifOptions = options.bifOptionsList.at(index);
Name = bifOptions->GetGroupName();
cache = new Binary();
checksumTable = new ChecksumTable();
}
/******************************************************************************/
BootImage::~BootImage()
{
if(currentAuthCtx)
{
delete currentAuthCtx;
}
if(currentEncryptCtx)
{
delete currentEncryptCtx;
}
if(currentChecksumCtx)
{
delete currentChecksumCtx;
}
if (checksumTable)
{
delete checksumTable;
}
if (cache)
{
delete cache;
}
if (bootHeader)
{
delete bootHeader;
}
if (imageHeaderTable)
{
delete imageHeaderTable;
}
if (partitionHeaderTable)
{
delete partitionHeaderTable;
}
if (partitionOutput)
{
delete partitionOutput;
}
if (deviceKey)
{
delete deviceKey;
}
if (firstIv)
{
delete firstIv;
}
if (firstOptKey)
{
delete firstOptKey;
}
if (keygen)
{
delete keygen;
}
if (hash)
{
delete hash;
}
if (nullPartHeaderSection)
{
delete nullPartHeaderSection;
}
}
/******************************************************************************/
void BootImage::OutputOptionalEfuseHash()
{
std::string hashFile = options.GetEfuseHashFileName();
if (hashFile != "")
{
if (bifOptions->GetPPKFileName() != "" || bifOptions->GetPSKFileName() != "")
{
if (currentAuthCtx)
{
currentAuthCtx->GeneratePPKHash(hashFile);
}
}
else
{
LOG_ERROR("Cannot read PPK/PSK. PPK/PSK is mandatory to generate PPK hash bits, using '-efuseppkbits'.");
}
}
}
/******************************************************************************/
void BootImage::BuildAndLink(Binary* cache)
{
if( imageList.size() == 0 )
{
LOG_WARNING("No partition images given");
}
currentAuthCtx->SetPpkSelect(bifOptions->GetPpkSelection());
currentAuthCtx->SetSpkSelect(bifOptions->GetSpkSelection());
currentAuthCtx->SetSpkIdentification(bifOptions->GetSpkId());
DetermineEncryptionDefaults();
partitionHeaderList.clear();
/* Build stage */
/* all static fields within the header tables are populated here */
bootHeader->Build(*this,*cache);
imageHeaderTable->Build(*this,*cache);
partitionHeaderTable->Build(*this,*cache);
partitionHeaderTable->BuildPartitions(*this,*cache);
checksumTable->Build(*this,*cache);
LOG_INFO("After build ");
LOG_DUMP_IMAGE(*cache);
/* Stack and alignment stage */
/* Once the header tables are created, stack all the tables and do the necessary alignment */
cache->StackAndAlign(options);
LOG_INFO("After align ");
LOG_DUMP_IMAGE(*cache);
PrintPartitionInformation();
/* Link stage - fields which depend on partitions are populated here */
bootHeader->Link(*this);
imageHeaderTable->Link(*this);
partitionHeaderTable->Link(*this);
partitionHeaderTable->LinkPartitions(*this);
checksumTable->Link(*this);
LOG_INFO("After Link ");
LOG_DUMP_IMAGE(*cache);
}
/******************************************************************************/
void BootImage::PrintPartitionInformation(void)
{
LOG_INFO("Partition Information: ");
std::string lastImageName;
for (std::list<PartitionHeader*>::iterator pH = partitionHeaderList.begin(); pH != partitionHeaderList.end(); pH++)
{
static uint8_t index = 0;
if ((lastImageName.compare((*pH)->imageHeader->GetName())) != 0)
{
LOG_INFO("Image: %s", (*pH)->imageHeader->GetName().c_str());
lastImageName = (*pH)->imageHeader->GetName();
}
LOG_INFO(" Partition %d: %s, Size: %d", index++, (*pH)->partition->section->Name.c_str(), (*pH)->GetPartitionSize());
}
}
/******************************************************************************/
void BootImage::ValidateOutputModes(File::Type split, OutputMode::Type outMode)
{
if(split != File::Unknown && outMode == OutputMode::OUT_SPLIT_SLAVEBOOT)
{
LOG_ERROR("Split & Split-Slave modes are mutually exclusive, cannot enable both modes");
}
if(outMode == OutputMode::OUT_BITSTREAM && (outMode == OutputMode::OUT_SPLIT_SLAVEBOOT || outMode == OutputMode::OUT_SPLIT_NORMAL))
{
LOG_ERROR("Process Bitstream & Split modes are mutually exclusive, cannot enable both modes");
}
if(split != File::Unknown && outMode == OutputMode::OUT_BITSTREAM)
{
LOG_ERROR("Process Bitstream & Split mode are mutually exclusive, cannot enable both modes");
}
}
/******************************************************************************/
void BootImage::OutputPartitionFiles(Options& options, Binary& cache)
{
partitionOutput->GeneratePartitionFiles(*this, cache);
}
/******************************************************************************/
void BootImage::SetLegacyEncryptionFlag(bool flag)
{
legacyEncryptionEnabled = flag;
}
/******************************************************************************/
AuthHash::Type BootImage::GetAuthHashAlgo(void)
{
return authHash;
}
/******************************************************************************/
Core::Type BootImage::GetCore(void)
{
return core;
}
/******************************************************************************/
uint32_t BootImage::GetPmuFwSize(void)
{
return pmuFwSize;
}
/******************************************************************************/
uint32_t BootImage::GetFsblFwSize(void)
{
return fsblFwSize;
}
/******************************************************************************/
uint32_t BootImage::GetTotalPmuFwSize(void)
{
return totalPmuFwSize;
}
/******************************************************************************/
uint32_t BootImage::GetTotalFsblFwSize(void)
{
return totalFsblFwSize;
}
/******************************************************************************/
Binary::Address_t BootImage::GetFsblSourceAddr(void)
{
return sourceAddr;
}
/******************************************************************************/
bool BootImage::IsBootloaderFound()
{
return bootloaderFound;
}
/******************************************************************************/
void BootImage::SetCore(Core::Type type)
{
core = type;
}
/******************************************************************************/
void BootImage::SetPmuFwSize(uint32_t size)
{
pmuFwSize = size;
}
/******************************************************************************/
void BootImage::SetFsblFwSize(uint32_t size)
{
fsblFwSize = size;
}
/******************************************************************************/
void BootImage::SetTotalPmuFwSize(uint32_t size)
{
totalPmuFwSize = size;
}
/******************************************************************************/
void BootImage::SetTotalFsblFwSize(uint32_t size)
{
totalFsblFwSize = size;
}
/******************************************************************************/
void BootImage::SetFsblSourceAddr(Binary::Address_t addr)
{
sourceAddr = addr;
}
/******************************************************************************/
void BootImage::GenerateAuthenticationKeys(void)
{
if(options.GetAuthKeyGeneration() != GenAuthKeys::None)
{
keygen = new KeyGenerationStruct;
keygen->format = options.GetAuthKeyGeneration();
keygen->ppk_file = bifOptions->GetPPKFileName();
keygen->psk_file = bifOptions->GetPSKFileName();
keygen->spk_file = bifOptions->GetSPKFileName();
keygen->ssk_file = bifOptions->GetSSKFileName();
keygen->keyLength = currentAuthCtx->GetRsaKeyLength();
Key::GenerateRsaKeys(keygen);
}
}
/******************************************************************************/
void BootImage::GenerateGreyKey(void)
{
if (options.GetGreyKeyGeneration())
{
currentEncryptCtx->GenerateGreyKey();
}
}
/******************************************************************************/
void BootImage::SetOutputSplitModeFormat(SplitMode::Type splitMode, File::Type fmt)
{
if(splitMode == SplitMode::Normal)
{
options.SetOutputMode(OutputMode::OUT_SPLIT_NORMAL, fmt);
}
else if (splitMode == SplitMode::SlaveMode)
{
options.SetOutputMode(OutputMode::OUT_SPLIT_SLAVEBOOT, fmt);
}
}
/******************************************************************************/
void BootImage::SetOutputBitstreamModeFormat(File::Type type)
{
if(type != File::Unknown)
{
options.SetOutputMode(OutputMode::OUT_BITSTREAM, type);
}
}
/******************************************************************************/
void BootImage::SetCoreFromDestCpu(DestinationCPU::Type coreType, A53ExecState::Type procType)
{
switch(coreType)
{
case DestinationCPU::A53_0:
if (procType == A53ExecState::AARCH64)
{
SetCore(Core::A53Singlex64);
}
else
{
SetCore(Core::A53Singlex32);
}
break;
case DestinationCPU::R5_0:
SetCore(Core::R5Single);
break;
case DestinationCPU::R5_lockstep:
SetCore(Core::R5Dual);
break;
case DestinationCPU::A53_1:
case DestinationCPU::A53_2:
case DestinationCPU::A53_3:
case DestinationCPU::R5_1:
case DestinationCPU::PMU:
LOG_ERROR("FSBL can run from CPU A53-0 / R5-0 only. Please update destination_cpu accordingly.");
break;
default:
case DestinationCPU::NONE:
break;
}
}
/******************************************************************************/
void BootImage::SetDestCpuFromCore(Core::Type coreType, DestinationCPU::Type cpuType)
{
if (cpuType == DestinationCPU::NONE)
{
switch (coreType)
{
case Core::A53Singlex64:
case Core::A53Singlex32:
{
cpuType = DestinationCPU::A53_0;
}
break;
case Core::R5Single:
{
cpuType = DestinationCPU::R5_0;
}
break;
case Core::R5Dual:
{
cpuType = DestinationCPU::R5_lockstep;
}
break;
}
}
}
/******************************************************************************/
void BootImage::SetAssumeEncryptionFlag(bool flag)
{
assumeEncryption = flag;
}
/******************************************************************************/
std::vector<std::string>& BootImage::GetEncryptionKeyFileVec()
{
return encryptionKeyFileVec;
}
/******************************************************************************/
void BootImage::InsertEncryptionKeyFile(std::string filename)
{
encryptionKeyFileVec.push_back(filename);
}