-
Notifications
You must be signed in to change notification settings - Fork 537
Expand file tree
/
Copy pathSectionAIEResourcesBin.cxx
More file actions
536 lines (428 loc) · 20.1 KB
/
SectionAIEResourcesBin.cxx
File metadata and controls
536 lines (428 loc) · 20.1 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
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
/**
* Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located 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.
*/
#include "SectionAIEResourcesBin.h"
#include "XclBinUtilities.h"
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/functional/factory.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <cstring>
namespace XUtil = XclBinUtilities;
// Disable windows compiler warnings
#ifdef _WIN32
#pragma warning( disable : 4100 4267 4244)
#endif
// Static Variables / Classes
SectionAIEResourcesBin::init SectionAIEResourcesBin::initializer;
SectionAIEResourcesBin::init::init()
{
auto sectionInfo = std::make_unique<SectionInfo>(AIE_RESOURCES_BIN, "AIE_RESOURCES_BIN", boost::factory<SectionAIEResourcesBin*>());
sectionInfo->supportsSubSections = true;
sectionInfo->subSections.push_back(std::string(getSubSectionName(SubSection::obj)));
sectionInfo->subSections.push_back(std::string(getSubSectionName(SubSection::metadata)));
sectionInfo->supportsIndexing = true;
// Add format support empty (no support)
// The top-level section doesn't support any add syntax.
// Must use sub-sections
sectionInfo->supportedAddFormats.push_back(FormatType::raw);
addSectionType(std::move(sectionInfo));
}
// -------------------------------------------------------------------------
using SubSectionTableCollection = std::vector<std::pair<std::string, SectionAIEResourcesBin::SubSection>>;
static const SubSectionTableCollection&
getSubSectionTable()
{
static const SubSectionTableCollection subSectionTable = {
{ "UNKNOWN", SectionAIEResourcesBin::SubSection::unknown },
{ "OBJ", SectionAIEResourcesBin::SubSection::obj },
{ "METADATA", SectionAIEResourcesBin::SubSection::metadata }
};
return subSectionTable;
}
SectionAIEResourcesBin::SubSection
SectionAIEResourcesBin::getSubSectionEnum(const std::string& sSubSectionName)
{
auto subSectionTable = getSubSectionTable();
auto iter = std::find_if(subSectionTable.begin(), subSectionTable.end(), [&](const auto& entry) {return boost::iequals(entry.first, sSubSectionName);});
if (iter == subSectionTable.end())
return SubSection::unknown;
return iter->second;
}
// -------------------------------------------------------------------------
const std::string&
SectionAIEResourcesBin::getSubSectionName(SectionAIEResourcesBin::SubSection eSubSection)
{
auto subSectionTable = getSubSectionTable();
auto iter = std::find_if(subSectionTable.begin(), subSectionTable.end(), [&](const auto& entry) {return entry.second == eSubSection;});
if (iter == subSectionTable.end())
return getSubSectionName(SubSection::unknown);
return iter->first;
}
// -------------------------------------------------------------------------
bool
SectionAIEResourcesBin::subSectionExists(const std::string& _sSubSectionName) const
{
// No buffer no subsections
if (m_pBuffer == nullptr) {
return false;
}
// There is a sub-system
// Determine if the metadata section has been initialized by the user.
// If not then it doesn't really exist
// Extract the sub-section entry type
SubSection eSS = getSubSectionEnum(_sSubSectionName);
if (eSS == SubSection::metadata) {
// Extract the binary data as a JSON string
std::ostringstream buffer;
writeMetadata(buffer);
std::stringstream ss;
const std::string& sBuffer = buffer.str();
XUtil::TRACE_BUF("String Image", sBuffer.c_str(), sBuffer.size());
ss.write((char*)sBuffer.c_str(), sBuffer.size());
boost::property_tree::ptree pt;
// Create a property tree and determine if the variables are all default values
try{
boost::property_tree::read_json(ss, pt);
}
catch (const boost::property_tree::json_parser_error& e) {
(void)e;
auto errMsg = boost::format("ERROR: Unable to parse metadata file of section '%s'") % getSectionIndexName();
throw std::runtime_error(errMsg.str());
}
boost::property_tree::ptree& ptAieResourcesBin = pt.get_child("aie_resources_bin_metadata");
XUtil::TRACE_PrintTree("Current AIE_RESOURCES_BIN contents", pt);
if ((ptAieResourcesBin.get<std::string>("version") == "") &&
(ptAieResourcesBin.get<std::string>("start_column") == "") &&
(ptAieResourcesBin.get<std::string>("num_columns") == "")) {
// All default values, metadata sub-section has yet to be added
return false;
}
}
return true;
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::copyBufferUpdateMetadata(const char* _pOrigDataSection,
unsigned int _origSectionSize,
std::istream& _istream,
std::ostringstream& _buffer) const
{
XUtil::TRACE("SectionAIEResourcesBin::CopyBufferUpdateMetadata");
// Do we have enough room to overlay the header structure
if (_origSectionSize < sizeof(aie_resources_bin)) {
auto errMsg = boost::format("ERROR: Segment size (%d) is smaller than the size of the aie_resources_bin structure (%d)") % _origSectionSize % sizeof(aie_resources_bin);
throw std::runtime_error(errMsg.str());
}
// Prepare our destination header buffer
aie_resources_bin aieResourcesBinHdr = {}; // Header buffer
std::ostringstream stringBlock; // String block (stored immediately after the header)
auto pHdr = reinterpret_cast<const aie_resources_bin*>(_pOrigDataSection);
if (pHdr->mpo_name >= _origSectionSize ||
pHdr->mpo_version >= _origSectionSize ||
pHdr->m_image_offset >= _origSectionSize ||
(pHdr->m_image_offset + pHdr->m_image_size) > _origSectionSize) {
throw std::runtime_error("ERROR: Invalid offsets in aie_resources_bin structure");
}
XUtil::TRACE_BUF("aie_resources_bin-original", reinterpret_cast<const char*>(pHdr), sizeof(aie_resources_bin));
XUtil::TRACE(boost::format("Original: \n"
" mpo_name (0x%lx): '%s'\n"
" m_image_offset: 0x%lx, m_image_size: 0x%lx\n"
" mpo_version (0x%lx): '%s'\n"
" m_start_column (0x%lx): '%s'\n"
" m_num_columns (0x%lx): '%s'")
% pHdr->mpo_name % (reinterpret_cast<const char*>(pHdr) + pHdr->mpo_name)
% pHdr->m_image_offset % pHdr->m_image_size
% pHdr->mpo_version % (reinterpret_cast<const char*>(pHdr) + pHdr->mpo_version)
% pHdr->m_start_column % (reinterpret_cast<const char*>(pHdr) + pHdr->m_start_column)
% pHdr->m_num_columns % (reinterpret_cast<const char*>(pHdr) + pHdr->m_num_columns));
// Get the JSON metadata
_istream.seekg(0, _istream.end); // Go to the beginning
std::streampos fileSize = _istream.tellg(); // Go to the end
// Copy the buffer into memory
std::unique_ptr<unsigned char[]> memBuffer(new unsigned char[fileSize]);
_istream.clear(); // Clear any previous errors
_istream.seekg(0); // Go to the beginning
_istream.read((char*)memBuffer.get(), fileSize); // Read in the buffer into memory
XUtil::TRACE_BUF("Buffer", (char*)memBuffer.get(), fileSize);
// Convert JSON memory image into a boost property tree
std::stringstream ss;
ss.write((char*)memBuffer.get(), fileSize);
boost::property_tree::ptree pt;
try{
boost::property_tree::read_json(ss, pt);
}
catch (const boost::property_tree::json_parser_error& e) {
(void)e;
auto errMsg = boost::format("ERROR: Unable to parse metadata file of section '%s'") % getSectionIndexName();
throw std::runtime_error(errMsg.str());
}
// ----------------------
// Extract and update the data
boost::property_tree::ptree& ptSK = pt.get_child("aie_resources_bin_metadata");
// Update and record the variables
// mpo_name
{
auto sDefault = reinterpret_cast<const char*>(pHdr) + sizeof(aie_resources_bin) + pHdr->mpo_name;
auto sValue = ptSK.get<std::string>("name", sDefault);
if (sValue.compare(getSectionIndexName()) != 0) {
auto errMsg = boost::format("ERROR: Metadata data name '%s' does not match expected section name '%s'") % sValue % getSectionIndexName();
throw std::runtime_error(errMsg.str());
}
aieResourcesBinHdr.mpo_name = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << sValue << '\0';
XUtil::TRACE(boost::format(" name (0x%lx): '%s'") % aieResourcesBinHdr.mpo_name % sValue);
}
// mpo_version
{
auto sDefault = reinterpret_cast<const char*>(pHdr) + sizeof(aie_resources_bin) + pHdr->mpo_version;
auto sValue = ptSK.get<std::string>("version", sDefault);
aieResourcesBinHdr.mpo_version = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << sValue << '\0';
XUtil::TRACE(boost::format(" version (0x%lx): '%s'") % aieResourcesBinHdr.mpo_version % sValue);
}
// m_start_column
{
auto sDefault = reinterpret_cast<const char*>(pHdr) + sizeof(aie_resources_bin) + pHdr->m_start_column;
auto sValue = ptSK.get<std::string>("start_column", sDefault);
aieResourcesBinHdr.m_start_column = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << sValue << '\0';
XUtil::TRACE(boost::format(" start_column (0x%lx): '%s'") % aieResourcesBinHdr.m_start_column % sValue);
}
// m_num_columns
{
auto sDefault = reinterpret_cast<const char*>(pHdr) + sizeof(aie_resources_bin) + pHdr->m_num_columns;
auto sValue = ptSK.get<std::string>("num_columns", sDefault);
aieResourcesBinHdr.m_num_columns = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << sValue << '\0';
XUtil::TRACE(boost::format(" num_columns (0x%lx): '%s'") % aieResourcesBinHdr.m_num_columns % sValue);
}
// Last item to be initialized
{
aieResourcesBinHdr.m_image_offset = sizeof(aie_resources_bin) + stringBlock.tellp();
aieResourcesBinHdr.m_image_size = pHdr->m_image_size;
XUtil::TRACE(boost::format(" m_image_offset: 0x%lx") % aieResourcesBinHdr.m_image_offset);
XUtil::TRACE(boost::format(" m_image_size: 0x%lx") % aieResourcesBinHdr.m_image_size);
}
// Copy the output to the output buffer.
// Header
_buffer.write(reinterpret_cast<const char*>(&aieResourcesBinHdr), sizeof(aie_resources_bin));
// String block
std::string sStringBlock = stringBlock.str();
_buffer.write(sStringBlock.c_str(), sStringBlock.size());
// Image
_buffer.write(reinterpret_cast<const char*>(pHdr) + pHdr->m_image_offset, pHdr->m_image_size);
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::createDefaultImage(std::istream& _istream, std::ostringstream& _buffer) const
{
XUtil::TRACE("AIE_RESOURCES_BIN-OBJ");
aie_resources_bin aieResourcesBinHdr = aie_resources_bin{};
std::ostringstream stringBlock; // String block (stored immediately after the header)
// Initialize default values
{
// Have all of the mpo (member, point, offset) values point to the zero length terminate string
aieResourcesBinHdr.mpo_name = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << getSectionIndexName() << '\0';
uint32_t mpo_emptyChar = sizeof(aie_resources_bin) + stringBlock.tellp();
stringBlock << '\0';
aieResourcesBinHdr.mpo_version = mpo_emptyChar;
aieResourcesBinHdr.m_start_column = mpo_emptyChar;
aieResourcesBinHdr.m_num_columns = mpo_emptyChar;
}
// Initialize the object image values (last)
{
_istream.seekg(0, _istream.end);
aieResourcesBinHdr.m_image_size = _istream.tellg();
aieResourcesBinHdr.m_image_offset = sizeof(aie_resources_bin) + stringBlock.tellp();
}
XUtil::TRACE_BUF("aie_resources_bin", reinterpret_cast<const char*>(&aieResourcesBinHdr), sizeof(aie_resources_bin));
// Write the header information
_buffer.write(reinterpret_cast<const char*>(&aieResourcesBinHdr), sizeof(aie_resources_bin));
// String block
std::string sStringBlock = stringBlock.str();
_buffer.write(sStringBlock.c_str(), sStringBlock.size());
// Write Data
{
std::unique_ptr<unsigned char[]> memBuffer(new unsigned char[aieResourcesBinHdr.m_image_size]);
_istream.seekg(0);
_istream.clear();
_istream.read(reinterpret_cast<char*>(memBuffer.get()), aieResourcesBinHdr.m_image_size);
_buffer.write(reinterpret_cast<const char*>(memBuffer.get()), aieResourcesBinHdr.m_image_size);
}
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::readSubPayload(const char* _pOrigDataSection,
unsigned int _origSectionSize,
std::istream& _istream,
const std::string& _sSubSectionName,
Section::FormatType _eFormatType,
std::ostringstream& _buffer) const
{
// Determine the sub-section of interest
SubSection eSubSection = getSubSectionEnum(_sSubSectionName);
switch (eSubSection) {
case SubSection::obj:
// Some basic DRC checks
if (_pOrigDataSection != nullptr) {
std::string errMsg = "ERROR: aie_resources_bin object image already exists.";
throw std::runtime_error(errMsg);
}
if (_eFormatType != Section::FormatType::raw) {
std::string errMsg = "ERROR: aie_resources_bin object only supports the RAW format.";
throw std::runtime_error(errMsg);
}
createDefaultImage(_istream, _buffer);
break;
case SubSection::metadata: {
// Some basic DRC checks
if (_pOrigDataSection == nullptr) {
std::string errMsg = "ERROR: Missing aie_resources_bin object image. Add the AIE_RESOURCES_BIN-OBJ image prior to changing its metadata.";
throw std::runtime_error(errMsg);
}
if (_eFormatType != Section::FormatType::json) {
std::string errMsg = "ERROR: AIE_RESOURCES_BIN-METADATA only supports the JSON format.";
throw std::runtime_error(errMsg);
}
copyBufferUpdateMetadata(_pOrigDataSection, _origSectionSize, _istream, _buffer);
}
break;
case SubSection::unknown:
default: {
auto errMsg = boost::format("ERROR: Subsection '%s' not supported by section '%s") % _sSubSectionName % getSectionKindAsString();
throw std::runtime_error(errMsg.str());
}
break;
}
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::writeObjImage(std::ostream& _oStream) const
{
XUtil::TRACE("SectionAIEResourcesBin::writeObjImage");
if (m_bufferSize < sizeof(aie_resources_bin)) {
auto errMsg = boost::format("ERROR: Segment size (%d) is smaller than the size of the bmc structure (%d)") % m_bufferSize % sizeof(aie_resources_bin);
throw std::runtime_error(errMsg.str());
}
auto pHdr = reinterpret_cast<aie_resources_bin*>(m_pBuffer);
if (pHdr->m_image_offset > m_bufferSize ||
pHdr->m_image_size > m_bufferSize ||
pHdr->m_image_offset > m_bufferSize - pHdr->m_image_size) {
throw std::runtime_error("ERROR: Invalid m_image_offset/m_image_size in aie_resources_bin");
}
auto pFWBuffer = reinterpret_cast<const char*>(pHdr) + pHdr->m_image_offset;
_oStream.write(pFWBuffer, pHdr->m_image_size);
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::writeMetadata(std::ostream& _oStream) const
{
XUtil::TRACE("AIE_RESOURCES_BIN-METADATA");
if (m_bufferSize < sizeof(aie_resources_bin)) {
auto errMsg = boost::format("ERROR: Segment size (%d) is smaller than the size of the aie_resources_bin structure (%d)") % m_bufferSize % sizeof(aie_resources_bin);
throw std::runtime_error(errMsg.str());
}
auto pHdr = reinterpret_cast<aie_resources_bin*>(m_pBuffer);
auto safe_str = [this, pHdr](uint32_t off) -> std::string {
if (off >= m_bufferSize)
return "";
return std::string(reinterpret_cast<const char*>(pHdr) + off,
strnlen(reinterpret_cast<const char*>(pHdr) + off, m_bufferSize - off));
};
XUtil::TRACE(boost::format("Original: \n"
" mpo_name (0x%lx): '%s'\n"
" m_image_offset: 0x%lx, m_image_size: 0x%lx\n"
" mpo_version (0x%lx): '%s'\n"
" m_start_column (0x%lx): '%s'\n"
" m_num_columns (0x%lx): '%s'")
% pHdr->mpo_name % safe_str(pHdr->mpo_name)
% pHdr->m_image_offset % pHdr->m_image_size
% pHdr->mpo_version % safe_str(pHdr->mpo_version)
% pHdr->m_start_column % safe_str(pHdr->m_start_column)
% pHdr->m_num_columns % safe_str(pHdr->m_num_columns));
boost::property_tree::ptree ptAieResourcesBin;
ptAieResourcesBin.put("name", safe_str(pHdr->mpo_name));
ptAieResourcesBin.put("version", safe_str(pHdr->mpo_version));
ptAieResourcesBin.put("start_column", safe_str(pHdr->m_start_column));
ptAieResourcesBin.put("num_columns", safe_str(pHdr->m_num_columns));
boost::property_tree::ptree root;
root.put_child("aie_resources_bin_metadata", ptAieResourcesBin);
boost::property_tree::write_json(_oStream, root);
}
// -------------------------------------------------------------------------
void
SectionAIEResourcesBin::writeSubPayload(const std::string& _sSubSectionName,
FormatType _eFormatType,
std::fstream& _oStream) const
{
// Some basic DRC checks
if (m_pBuffer == nullptr) {
std::string errMsg = "ERROR: aie_resources_bin section does not exist.";
throw std::runtime_error(errMsg);
}
SubSection eSubSection = getSubSectionEnum(_sSubSectionName);
switch (eSubSection) {
case SubSection::obj:
// Some basic DRC checks
if (_eFormatType != Section::FormatType::raw) {
std::string errMsg = "ERROR: AIE_RESOURCES_BIN-OBJ only supports the RAW format.";
throw std::runtime_error(errMsg);
}
writeObjImage(_oStream);
break;
case SubSection::metadata: {
if (_eFormatType != Section::FormatType::json) {
std::string errMsg = "ERROR: AIE_RESOURCES_BIN-METADATA only supports the JSON format.";
throw std::runtime_error(errMsg);
}
writeMetadata(_oStream);
}
break;
case SubSection::unknown:
default: {
auto errMsg = boost::format("ERROR: Subsection '%s' not support by section '%s") % _sSubSectionName % getSectionKindAsString();
throw std::runtime_error(errMsg.str());
}
break;
}
}
void
SectionAIEResourcesBin::readXclBinBinary(std::istream& _istream, const axlf_section_header& _sectionHeader)
{
Section::readXclBinBinary(_istream, _sectionHeader);
// Extract the binary data as a JSON string
std::ostringstream buffer;
writeMetadata(buffer);
std::stringstream ss;
const std::string& sBuffer = buffer.str();
XUtil::TRACE_BUF("String Image", sBuffer.c_str(), sBuffer.size());
ss.write((char*)sBuffer.c_str(), sBuffer.size());
// Create a property tree and determine if the variables are all default values
boost::property_tree::ptree pt;
try {
boost::property_tree::read_json(ss, pt);
}
catch (const boost::property_tree::json_parser_error& e) {
(void)e;
auto errMsg = boost::format("ERROR: Unable to parse metadata file of section '%s'") % getSectionIndexName();
throw std::runtime_error(errMsg.str());
}
boost::property_tree::ptree& ptAieResourcesBin = pt.get_child("aie_resources_bin_metadata");
XUtil::TRACE_PrintTree("Current AIE_RESOURCES_BIN contents", pt);
std::string sName = ptAieResourcesBin.get<std::string>("name");
Section::m_sIndexName = sName;
}