From 83f11589334940308e45bb1e49321e0d58b0587d Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Mon, 18 Nov 2013 20:11:22 +0100 Subject: [PATCH] Version 0.9.2 - fixed a bug in calculateChecksum16, previous versions should not be used! --- ffs.cpp | 20 +++++++++----- ffs.h | 2 +- ffsengine.cpp | 74 +++++++-------------------------------------------- uefitool.ui | 2 +- 4 files changed, 25 insertions(+), 73 deletions(-) diff --git a/ffs.cpp b/ffs.cpp index 2458fc7..1560150 100644 --- a/ffs.cpp +++ b/ffs.cpp @@ -22,20 +22,28 @@ UINT8 calculateChecksum8(UINT8* buffer, UINT32 bufferSize) return 0; UINT8 counter = 0; + while(bufferSize--) counter += buffer[bufferSize]; - return ~counter + 1; + + return (UINT8) 0x100 - counter; } -UINT16 calculateChecksum16(UINT8* buffer, UINT32 bufferSize) +UINT16 calculateChecksum16(UINT16* buffer, UINT32 bufferSize) { if(!buffer) return 0; - + UINT16 counter = 0; - while(bufferSize--) - counter += buffer[bufferSize]; - return ~counter + 1; + UINT32 index = 0; + + bufferSize /= sizeof(UINT16); + + for (; index < bufferSize; index++) { + counter = (UINT16) (counter + buffer[index]); + } + + return (UINT16) 0x10000 - counter; } VOID uint32ToUint24(UINT32 size, UINT8* ffsSize) diff --git a/ffs.h b/ffs.h index 9c2a08f..663112c 100644 --- a/ffs.h +++ b/ffs.h @@ -213,7 +213,7 @@ typedef struct { // Volume header 16bit checksum calculation routine -extern UINT16 calculateChecksum16(UINT8* buffer, UINT32 bufferSize); +extern UINT16 calculateChecksum16(UINT16* buffer, UINT32 bufferSize); //***************************************************************************** // EFI FFS File diff --git a/ffsengine.cpp b/ffsengine.cpp index d4bdec4..2c15c70 100644 --- a/ffsengine.cpp +++ b/ffsengine.cpp @@ -662,7 +662,7 @@ UINT8 FfsEngine::parseVolume(const QByteArray & volume, const QModelIndex & par char empty = volumeHeader->Attributes & EFI_FVB_ERASE_POLARITY ? '\xFF' : '\x00'; // Check header checksum by recalculating it - if (!calculateChecksum16((UINT8*) volumeHeader, volumeHeader->HeaderLength)) { + if (calculateChecksum16((UINT16*) volumeHeader, volumeHeader->HeaderLength)) { msg(tr("parseBios: Volume header checksum is invalid"), parent); } @@ -1742,7 +1742,7 @@ UINT8 FfsEngine::reconstruct(const QModelIndex & index, QQueue & que // Recalculate volume header checksum volumeHeader->Checksum = 0; - volumeHeader->Checksum = calculateChecksum16((UINT8*) volumeHeader, volumeHeader->HeaderLength); + volumeHeader->Checksum = calculateChecksum16((UINT16*) volumeHeader, volumeHeader->HeaderLength); // Reconstruct volume body if (item->childCount()) { @@ -1868,7 +1868,8 @@ UINT8 FfsEngine::reconstruct(const QModelIndex & index, QQueue & que break; } } - // Append last file and fill the rest with empty char + + // Append last file and fill the rest with empty char else { reconstructed.append(file); UINT32 volumeBodySize = volumeSize - header.size(); @@ -1896,7 +1897,8 @@ UINT8 FfsEngine::reconstruct(const QModelIndex & index, QQueue & que // Append current file to new volume body reconstructed.append(file); - // Change current file offset + + // Change current file offset offset += file.size(); } @@ -2139,66 +2141,7 @@ UINT8 FfsEngine::reconstruct(const QModelIndex & index, QQueue & que return ERR_SUCCESS; } - /*// Compress object with new compression algorithm - else if (item->action() == TreeItem::EfiCompress || item->action() == TreeItem::TianoCompress || item->action() == TreeItem::LzmaCompress) { - // Select algorithm - UINT8 algorithm; - if (item->action() == TreeItem::EfiCompress) - algorithm = COMPRESSION_ALGORITHM_EFI11; - else if (item->action() == TreeItem::TianoCompress) - algorithm = COMPRESSION_ALGORITHM_TIANO; - else if (item->action() == TreeItem::LzmaCompress) - algorithm = COMPRESSION_ALGORITHM_LZMA; - else - return ERR_UNKNOWN_COMPRESSION_ALGORITHM; - - // Possible only for compressed sections with EFI1.1, Tiano or LZMA algorithm - if (item->type() == TreeItem::Section && item->subtype() == EFI_SECTION_COMPRESSION && - (item->compression() == COMPRESSION_ALGORITHM_EFI11 || item->compression() == COMPRESSION_ALGORITHM_TIANO || item->compression() == COMPRESSION_ALGORITHM_LZMA)) { - QByteArray header = item->header(); - EFI_COMPRESSION_SECTION* sectionHeader = (EFI_COMPRESSION_SECTION*) header.data(); - if (!item->childCount()) - return ERR_INVALID_SECTION; - - QQueue childrenQueue; - for (int i = 0; i < item->childCount(); i++) { - // Reconstruct subsections - result = reconstruct(index.child(i, index.column()), childrenQueue); - if (result) - return result; - } - - // Construct new section body - UINT32 offset = 0; - while (!childrenQueue.isEmpty()) - { - // Align to 4 byte boundary - UINT8 alignment = offset % 4; - if (alignment) { - alignment = 4 - alignment; - offset += alignment; - reconstructed.append(QByteArray(alignment, '\x00')); - } - - // Get section from queue - QByteArray section = childrenQueue.dequeue(); - - // Append current subsection to new section body - reconstructed.append(section); - - // Change current file offset - offset += section.size(); - } - - // Compress new section body using determined compression algorithm - - - } - else - return ERR_NOT_IMPLEMENTED; - - }*/ - + return ERR_NOT_IMPLEMENTED; } @@ -2235,7 +2178,8 @@ UINT8 FfsEngine::growVolume(QByteArray & header, const UINT32 size, UINT32 & new // Recalculate volume header checksum volumeHeader->Checksum = 0; - volumeHeader->Checksum = calculateChecksum16((UINT8*) volumeHeader, volumeHeader->HeaderLength); + volumeHeader->Checksum = calculateChecksum16((UINT16*) volumeHeader, volumeHeader->HeaderLength); + return ERR_SUCCESS; } diff --git a/uefitool.ui b/uefitool.ui index a932c1c..a65270f 100644 --- a/uefitool.ui +++ b/uefitool.ui @@ -20,7 +20,7 @@ true - UEFITool 0.9.1 + UEFITool 0.9.2