Skip to content

Commit c2c9469

Browse files
committed
Improve alignmentBytes checks, fix minor TODOs
1 parent caa3d1d commit c2c9469

1 file changed

Lines changed: 85 additions & 64 deletions

File tree

common/ffsparser.cpp

Lines changed: 85 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,33 +2032,34 @@ USTATUS FfsParser::parseVolumeBody(const UModelIndex & index, const bool probe)
20322032
// Move to next file
20332033
fileOffset += fileSize;
20342034

2035-
// Check for alignment bytes after the current file
2036-
// Files must be aligned to 8 bytes boundary inside the FFS volume
2037-
// The alignment byte should be equal to 0xFF (erasePolarity = 1) or 0x00 (erasePolarity = 0)
2038-
UINT32 numAlignmentBytes = ALIGN8(fileOffset) - fileOffset;
2039-
2040-
// We might be at the very end of the volume, need a check for it
2041-
if (volumeBodySize < fileOffset + fileSize + numAlignmentBytes) {
2042-
numAlignmentBytes = volumeBodySize - fileOffset - fileSize;
2043-
}
2044-
2045-
if (numAlignmentBytes > 0) {
2046-
UByteArray alignmentBytes = volumeBody.mid(fileOffset + fileSize, numAlignmentBytes);
2047-
// Check alignment bytes to be sane
2048-
if (!isUniformByte(alignmentBytes, emptyByte)) {
2049-
msg(usprintf("%s: unexpected bytes found in alignment area, should all be %s", __FUNCTION__, emptyByte ? "0xFF" : "0x00"), fileIndex);
2050-
2051-
// Add unexpected alignment bytes to tree item info
2052-
UString info;
2053-
for (UINT8 i = 0; i < numAlignmentBytes; i++) {
2054-
info += usprintf("%02X ", alignmentBytes[i]);
2055-
}
2056-
info = UString("Alignment bytes: ") + info + "\n";
2057-
model->addInfo(fileIndex, info);
2035+
if (!probe) {
2036+
// Check for alignment bytes after the current file
2037+
// Files must be aligned to 8 bytes boundary inside the FFS volume
2038+
// The alignment byte should be equal to 0xFF (erasePolarity = 1) or 0x00 (erasePolarity = 0)
2039+
UINT32 numAlignmentBytes = ALIGN8(fileOffset) - fileOffset;
2040+
2041+
// We might be at the very end of the volume, need a check for it
2042+
if (volumeBodySize < fileOffset + numAlignmentBytes) {
2043+
numAlignmentBytes = volumeBodySize - fileOffset;
20582044
}
20592045

2060-
// Store alignment bytes for upholding parsing pre/post-conditions (aka "every byte is accounted for")
2061-
model->setAlignmentBytes(fileIndex, alignmentBytes);
2046+
if (numAlignmentBytes > 0) {
2047+
UByteArray alignmentBytes = volumeBody.mid(fileOffset, numAlignmentBytes);
2048+
// Check alignment bytes to be sane
2049+
if (!isUniformByte(alignmentBytes, emptyByte)) {
2050+
msg(usprintf("%s: unexpected bytes found in file alignment area, should all be %s", __FUNCTION__, emptyByte ? "0xFF" : "0x00"), fileIndex);
2051+
2052+
// Add unexpected alignment bytes to tree item info
2053+
UString info;
2054+
for (UINT8 i = 0; i < numAlignmentBytes; i++) {
2055+
info += usprintf("%02X ", alignmentBytes.at(i));
2056+
}
2057+
model->addInfo(fileIndex, UString("\nAlignment bytes: ") + info);
2058+
}
2059+
2060+
// Store alignment bytes for upholding parsing pre/post-conditions (aka "every byte is accounted for")
2061+
model->setAlignmentBytes(fileIndex, alignmentBytes);
2062+
}
20622063
}
20632064

20642065
// Adjust fileOffset
@@ -2591,33 +2592,34 @@ USTATUS FfsParser::parseSections(const UByteArray & sections, const UModelIndex
25912592
// Move to next section
25922593
sectionOffset += sectionSize;
25932594

2594-
// Check for alignment bytes after the current section
2595-
// Sections must be aligned to 4 bytes boundary inside the FFS file
2596-
// The alignment byte should be equal to 0x00 regardless of erasePolarity
2597-
UINT32 numAlignmentBytes = ALIGN4(sectionOffset) - sectionOffset;
2598-
2599-
// We might be at the very end of the file, need a check for it
2600-
if (fileBodySize < sectionOffset + sectionSize + numAlignmentBytes) {
2601-
numAlignmentBytes = fileBodySize - sectionOffset - sectionSize;
2602-
}
2603-
2604-
if (numAlignmentBytes > 0) {
2605-
UByteArray alignmentBytes = sections.mid(sectionOffset + sectionSize, numAlignmentBytes);
2606-
// Check alignment bytes to be sane
2607-
if (!isUniformByte(alignmentBytes, 0x00)) {
2608-
msg(usprintf("%s: unexpected bytes found in alignment area, should all be 0x00", __FUNCTION__), sectionIndex);
2609-
2610-
// Add unexpected alignment bytes to tree item info
2611-
UString info;
2612-
for (UINT8 i = 0; i < numAlignmentBytes; i++) {
2613-
info += usprintf("%02X ", alignmentBytes[i]);
2614-
}
2615-
info = UString("Alignment bytes: ") + info + "\n";
2616-
model->addInfo(sectionIndex, info);
2595+
if (!probe) {
2596+
// Check for alignment bytes after the current section
2597+
// Sections must be aligned to 4 bytes boundary inside the FFS file
2598+
// The alignment byte should be equal to 0x00 regardless of erasePolarity
2599+
UINT32 numAlignmentBytes = ALIGN4(sectionOffset) - sectionOffset;
2600+
2601+
// We might be at the very end of the file, need a check for it
2602+
if (fileBodySize < sectionOffset + numAlignmentBytes) {
2603+
numAlignmentBytes = fileBodySize - sectionOffset;
26172604
}
26182605

2619-
// Store alignment bytes for upholding parsing pre/post-conditions (aka "every byte is accounted for")
2620-
model->setAlignmentBytes(sectionIndex, alignmentBytes);
2606+
if (numAlignmentBytes > 0) {
2607+
UByteArray alignmentBytes = sections.mid(sectionOffset, numAlignmentBytes);
2608+
// Check alignment bytes to be sane
2609+
if (!isUniformByte(alignmentBytes, 0x00)) {
2610+
msg(usprintf("%s: unexpected bytes found in section alignment area, should all be 0x00", __FUNCTION__), sectionIndex);
2611+
2612+
// Add unexpected alignment bytes to tree item info
2613+
UString info;
2614+
for (UINT8 i = 0; i < numAlignmentBytes; i++) {
2615+
info += usprintf("%02X ", alignmentBytes[i]);
2616+
}
2617+
model->addInfo(sectionIndex, UString("\nAlignment bytes: ") + info);
2618+
}
2619+
2620+
// Store alignment bytes for upholding parsing pre/post-conditions (aka "every byte is accounted for")
2621+
model->setAlignmentBytes(sectionIndex, alignmentBytes);
2622+
}
26212623
}
26222624

26232625
// Adjust sectionOffset
@@ -5486,10 +5488,10 @@ USTATUS FfsParser::parseCpdExtensionsArea(const UModelIndex & index, const UINT3
54865488

54875489
UByteArray body = model->body(index);
54885490
UINT32 offset = 0;
5489-
while (offset < (UINT32)body.size() - sizeof(CPD_EXTENTION_HEADER)) {
5490-
const CPD_EXTENTION_HEADER* extHeader = (const CPD_EXTENTION_HEADER*) (body.constData() + offset);
5491-
if (extHeader->Length > 0
5492-
&& extHeader->Length <= ((UINT32)body.size() - offset)) {
5491+
UINT32 bodySize = (UINT32)body.size();
5492+
while (bodySize - offset >= sizeof(CPD_EXTENTION_HEADER)) {
5493+
const CPD_EXTENTION_HEADER* extHeader = (const CPD_EXTENTION_HEADER*)(body.constData() + offset);
5494+
if (extHeader->Length > 0 && bodySize - offset >= extHeader->Length) {
54935495
UByteArray partition = body.mid(offset, extHeader->Length);
54945496

54955497
UString name = cpdExtensionTypeToUstring(extHeader->Type);
@@ -5604,7 +5606,17 @@ USTATUS FfsParser::parseCpdExtensionsArea(const UModelIndex & index, const UINT3
56045606
offset += extHeader->Length;
56055607
}
56065608
else break;
5607-
// TODO: add padding at the end
5609+
}
5610+
5611+
// Check if we parsed everything, add the rest at the end as padding
5612+
if (offset < bodySize) {
5613+
UByteArray padding = body.mid(offset, bodySize - offset);
5614+
5615+
// Get info
5616+
UString name = UString("Padding");
5617+
5618+
// Add tree item
5619+
model->addItem(offset + localOffset, Types::Padding, getPaddingType(padding), name, UString(), UString(), UByteArray(), padding, UByteArray(), Fixed, index);
56085620
}
56095621

56105622
return U_SUCCESS;
@@ -5618,12 +5630,11 @@ USTATUS FfsParser::parseSignedPackageInfoData(const UModelIndex & index)
56185630

56195631
UByteArray body = model->body(index);
56205632
UINT32 offset = 0;
5621-
while (offset < (UINT32)body.size()) {
5622-
if (body.size() - offset < sizeof(CPD_EXT_SIGNED_PACKAGE_INFO_MODULE))
5623-
break;
5633+
UINT32 bodySize = (UINT32)body.size();
5634+
while (offset < bodySize && bodySize - offset >= sizeof(CPD_EXT_SIGNED_PACKAGE_INFO_MODULE)) {
56245635
const CPD_EXT_SIGNED_PACKAGE_INFO_MODULE* moduleHeader = (const CPD_EXT_SIGNED_PACKAGE_INFO_MODULE*)(body.constData() + offset);
5625-
if ((sizeof(CPD_EXT_SIGNED_PACKAGE_INFO_MODULE) + moduleHeader->HashSize) <= ((UINT32)body.size() - offset)) {
5626-
UByteArray module((const char*)moduleHeader, CpdExtSignedPkgMetadataHashOffset + moduleHeader->HashSize);
5636+
if (bodySize - offset >= sizeof(CPD_EXT_SIGNED_PACKAGE_INFO_MODULE) + moduleHeader->HashSize) {
5637+
UByteArray mdl((const char*)moduleHeader, CpdExtSignedPkgMetadataHashOffset + moduleHeader->HashSize);
56275638
UString name = usprintf("%.12s", moduleHeader->Name);
56285639

56295640
// This hash is stored reversed
@@ -5636,12 +5647,22 @@ USTATUS FfsParser::parseSignedPackageInfoData(const UModelIndex & index)
56365647
moduleHeader->HashAlgorithm,
56375648
moduleHeader->HashSize, moduleHeader->HashSize,
56385649
moduleHeader->MetadataSize, moduleHeader->MetadataSize) + UString(hash.toHex().constData());
5639-
// Add tree otem
5640-
model->addItem(offset, Types::CpdSpiEntry, 0, name, UString(), info, UByteArray(), module, UByteArray(), Fixed, index);
5641-
offset += module.size();
5650+
// Add tree item
5651+
model->addItem(offset, Types::CpdSpiEntry, 0, name, UString(), info, UByteArray(), mdl, UByteArray(), Fixed, index);
5652+
offset += mdl.size();
56425653
}
56435654
else break;
5644-
// TODO: add padding at the end
5655+
}
5656+
5657+
// Check if we parsed everything, add the rest at the end as padding
5658+
if (offset < bodySize) {
5659+
UByteArray padding = body.mid(offset, bodySize - offset);
5660+
5661+
// Get info
5662+
UString name = UString("Padding");
5663+
5664+
// Add tree item
5665+
model->addItem(offset, Types::Padding, getPaddingType(padding), name, UString(), UString(), UByteArray(), padding, UByteArray(), Fixed, index);
56455666
}
56465667

56475668
return U_SUCCESS;

0 commit comments

Comments
 (0)