-
Notifications
You must be signed in to change notification settings - Fork 42
/
bitutils.cpp
executable file
·389 lines (353 loc) · 11.7 KB
/
bitutils.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
/******************************************************************************
* Copyright 2015-2022 Xilinx, Inc.
* Copyright 2022-2023 Advanced Micro Devices, 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 <stdint.h>
#include <string>
#include <sstream>
#include "bootgenexception.h"
#include "bitutils.h"
#include <assert.h>
#include <iostream>
#include <string.h>
#include "bootimage.h"
/*
-------------------------------------------------------------------------------
********************************************* P R E P R O C E S S O R S ***
-------------------------------------------------------------------------------
*/
#define NO_IO_ERROR 0
#define DATA_SYNC_WORD 0xAA995566
#define TIMER_CMD_WORD 0x30022001
/*
-------------------------------------------------------------------------------
***************************************************** F U N C T I O N S ***
-------------------------------------------------------------------------------
*/
/******************************************************************************/
BitFile::BitFile(std::istream& stream0)
: is(stream0),
temp(NULL),
encryptType(Encryption::None)
{
}
/******************************************************************************/
BitFile::~BitFile()
{
if (temp)
{
delete temp;
}
}
/******************************************************************************/
void BitFile::ParseBit(BootImage& bi)
{
switch (is.Peek())
{
case 0x00:
ReadBITHeader_x4k();
break;
case 0x19:
ReadBITHeader_NeoCAD();
break;
case 'X':
case 'x':
ReadRbtHeader();
break;
default:
LOG_DEBUG(DEBUG_STAMP, "First Byte in Bitstream - 0x%X", is.Peek());
LOG_ERROR("Bitstream parsing error !!! Unsupported BIT file");
}
inputPackageName = bi.options.GetDevicePackageName();
if(bi.partitionOutput->Type() != PartitionArch::FPGA)
{
ValidateDeviceName(header.devicePackageName);
}
}
/******************************************************************************/
void BitFile::ReadBITHeader_x4k( )
{
header.unknownString = is.ReadPascalString( );
header.unknownValue = is.ReadShort( );
header.fieldSeparatorA = is.ReadByte( );
header.designName = is.ReadPascalString();
header.fieldSeparatorB = is.ReadByte( );
header.devicePackageName= is.ReadPascalString();
header.fieldSeparatorC = is.ReadByte( );
header.designDate = is.ReadPascalString();
header.fieldSeparatorD = is.ReadByte( );
header.designTime = is.ReadPascalString();
if( ( header.fieldSeparatorA != 'a' ) ||
( header.fieldSeparatorB != 'b' ) ||
( header.fieldSeparatorC != 'c' ) ||
( header.fieldSeparatorD != 'd' ) ||
( header.designName.size() == 0 ) ||
( header.devicePackageName.size() == 0 ) ||
( header.designDate.size() == 0 ) ||
( header.designTime.size() == 0 ) ||
( header.unknownString != "\x0F\xF0\x0F\xF0\x0F\xF0\x0F\xF0" ) )
{
LOG_ERROR("Bitstream parsing error !!! Invalid/Corrupted Bitstream Header");
}
header.designDateAndTime = header.designDate + " " + header.designTime;
header.fieldSeparatorE = is.ReadByte();
header.dataByteLength = is.ReadLong();
}
/******************************************************************************/
void BitFile::ReadBITHeader_NeoCAD()
{
header.headerType = is.ReadByte();
header.designName = is.ReadCString();
header.deviceFamily = is.ReadCString();
header.devicePackageName = is.ReadCString();
header.designDateAndTime = is.ReadCString();
/* Skip unknown data */
is.Skip(sizeof(header.unknownData));
header.dataByteLength = is.ReadLong() / BITS_IN_BYTE;
}
/******************************************************************************/
void BitFile::Copy(OutputStream* os)
{
os->Alloc(header.dataByteLength + 32);
if (header.dataByteLength % 4)
{
LOG_ERROR("Bitstream length %d - is not word aligned", header.dataByteLength);
}
int words = header.dataByteLength / sizeof(uint32_t);
for(int i=0;i<words;i++)
{
uint32_t x = is.ReadLong();
os->WriteLong(x);
}
}
/******************************************************************************/
void BitFile::Strip(OutputStream* os)
{
os->Alloc(header.dataByteLength + 32);
uint32_t cmdWord;
/* In case of Zynq bitstreams, skip synchronization words, until we get the magic AA995566 */
do
{
cmdWord = is.ReadLong();
} while (cmdWord != DATA_SYNC_WORD);
/* Dummy Read is necessary to move to next location where the actual bitstream starts
FPGA encryption bitstream does not include this NOP data
Zynq & ZynqMP encrypted bitstreams contain this NOP data */
DummyRead();
/* Now copy all command words, until we get a DESYNCH or REBOOT command */
BitCommand cmd;
do
{
cmd.ReadWrite(is,os);
} while (! (cmd.reg == BitCommand::CMD_reg && (cmd.value == BitCommand::DESYNCH_cmd || cmd.value == BitCommand::REBOOT_cmd)));
/* Don't copy the DESYNCH or REBOOT command. */
os->Rewind(2*sizeof(uint32_t));
}
/******************************************************************************/
void BitFile::ReadRbtHeader()
{
std::string line = is.ReadLine();
if ((line != "Xilinx ASCII Bitstream") && (line != "Xilinx ASCII CFI Deviceimage") &&
(line != "Xilinx ASCII NPI Deviceimage") && (line != "Xilinx ASCII PSAXIMM Deviceimage"))
{
LOG_DEBUG(DEBUG_STAMP, "Wrong Line - %s", line.c_str());
LOG_ERROR("Bitstream parsing error !!! First line of RBT has incorrect starting line");
}
if (line == "Xilinx ASCII Bitstream")
{
is.SetRbt();
}
while (true)
{
line = is.ReadLine();
if (line == "")
{
break;
}
size_t colon = line.find(':');
if (colon != std::string::npos)
{
std::string field = line.substr(0, colon);
size_t valuePtr = line.find_first_not_of(" \t", colon + 1);
if (valuePtr != std::string::npos)
{
if (field == "Design name")
{
header.designName = line.substr(valuePtr);
}
else if (field == "Part")
{
header.devicePackageName = line.substr(valuePtr);
}
else if (field == "Date")
{
header.designDateAndTime = line.substr(valuePtr);
}
else if (field == "Bits")
{
header.dataByteLengthString = line.substr(valuePtr);
uint32_t y;
std::stringstream x(header.dataByteLengthString);
x >> y;
header.dataByteLength = y / BITS_IN_BYTE;
break;
}
}
}
}
ValidateDeviceName(header.devicePackageName);
}
/******************************************************************************/
void BitFile::ValidateDeviceName(const std::string& packageName)
{
if (inputPackageName != "")
{
if (packageName.compare(0, inputPackageName.length(), inputPackageName) == 0)
{
return;
}
}
else
{
ComparePartsDataBase(packageName);
}
}
/******************************************************************************/
void BitFile::SetEncryptionType(Encryption::Type type)
{
encryptType = type;
}
/******************************************************************************/
OutputStream* ZynqBitFile::GetOutputStreamType(void)
{
return new OutputStream_LE();
}
/******************************************************************************/
bool ZynqBitFile::GetBitStripFlag(void)
{
if (encryptType == Encryption::AES)
{
return true;
}
else
{
return false;
}
}
/******************************************************************************/
bool ZynqBitFile::GetBitPadFlag(bool legacyFlag)
{
if (legacyFlag && (encryptType == Encryption::None))
{
/* Don't pad to 32 bytes */
return false;
}
else
{
return true;
}
}
/******************************************************************************/
void ZynqBitFile::ComparePartsDataBase(const std::string& packageName)
{
const char* parts[] = { "xc7z", "7z", "xa7z", "a7z", "xq7z", "q7z", NULL };
for (int i = 0; parts[i]; i++)
{
if (packageName.compare(0, strlen(parts[i]), parts[i]) == 0)
{
return;
}
}
LOG_DEBUG(DEBUG_STAMP, "BIT file package name - %s", packageName.c_str());
LOG_ERROR("Partname %s in Bit file is incompatible with Zynq (Zynq 7000) parts", packageName.c_str());
}
/******************************************************************************/
OutputStream* ZynqMpBitFile::GetOutputStreamType(void)
{
if(encryptType == Encryption::AES)
{
return new OutputStream_BE();
}
else
{
return new OutputStream_LE();
}
}
/******************************************************************************/
bool ZynqMpBitFile::GetBitStripFlag(void)
{
return false;
}
/******************************************************************************/
bool ZynqMpBitFile::GetBitPadFlag(bool legacyFlag)
{
return false;
}
/******************************************************************************/
void ZynqMpBitFile::ComparePartsDataBase(const std::string& packageName)
{
const char* parts[] = { "xqzu", "xczu", "xazu", "xck", NULL };
for(int i=0; parts[i]; i++)
{
if (packageName.compare(0,strlen(parts[i]),parts[i]) == 0)
{
return;
}
}
LOG_DEBUG(DEBUG_STAMP, "BIT file package name - %s", packageName.c_str());
LOG_ERROR("Partname %s in Bit file is incompatible with Zynq MP parts", packageName.c_str());
}
/******************************************************************************/
void BitCommand::ReadWrite(InputStream_BE& is, OutputStream* os)
{
instruction = is.ReadLong();
os->WriteLong(instruction);
word_type = Config_Word_Get_Type();
reg = Config_Word_Get_Register();
if (Config_Word_Get_Op() == kConfig_NOP_cmd)
{
count = 0;
}
else
{
if (word_type == cmd_word)
{
count = Config_Word_Get_Word_Count();
}
else if (word_type == extension_word)
{
count = Config_Word_Get_Extension_Count();
}
else
{
LOG_DEBUG(DEBUG_STAMP, "Bad command word in BIT stream: 0x%X", instruction);
}
}
for (uint32_t i = 0;i<count;i++)
{
uint32_t x;
x = is.ReadLong();
os->WriteLong(x);
if (i == 0)
{
value = x;
}
}
}