From 082fb091f1a0cab9d00e82de54fee32b6a1c0c7b Mon Sep 17 00:00:00 2001 From: Benjamin Dobell Date: Sun, 1 Jun 2014 14:09:56 +1000 Subject: Fixed support for large files (up to 2^32 - 1 bytes) The Loke protocol supports 32-bit unsigned for the size of files being flashed. However, POSIX file commands only support 32-bit (signed). As such we now have platform specific support for larger files. --- heimdall/Makefile.am | 2 +- heimdall/source/BridgeManager.cpp | 10 +++++----- heimdall/source/DownloadPitAction.cpp | 6 +++--- heimdall/source/FlashAction.cpp | 36 +++++++++++++++++------------------ heimdall/source/Heimdall.h | 13 +++++++++++++ heimdall/source/PrintPitAction.cpp | 10 +++++----- heimdall/source/SendFilePartPacket.h | 8 ++++---- 7 files changed, 49 insertions(+), 36 deletions(-) diff --git a/heimdall/Makefile.am b/heimdall/Makefile.am index c793757..cf171e0 100644 --- a/heimdall/Makefile.am +++ b/heimdall/Makefile.am @@ -1,8 +1,8 @@ AUTOMAKE_OPTIONS = subdir-objects ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} AM_CPPFLAGS = $(DEPS_CFLAGS) -std=c++0x -I../libpit/Source +AC_SYS_LARGEFILE STATIC_LIBS = ../libpit/libpit-1.4.a - bin_PROGRAMS = heimdall heimdall_SOURCES = source/Arguments.cpp \ source/BridgeManager.cpp \ diff --git a/heimdall/source/BridgeManager.cpp b/heimdall/source/BridgeManager.cpp index 32807ca..dc2926a 100644 --- a/heimdall/source/BridgeManager.cpp +++ b/heimdall/source/BridgeManager.cpp @@ -1002,9 +1002,9 @@ bool BridgeManager::SendFile(FILE *file, unsigned int destination, unsigned int return (false); } - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); - rewind(file); + FileSeek(file, 0, SEEK_END); + unsigned int fileSize = (unsigned int)FileTell(file); + FileRewind(file); ResponsePacket *fileTransferResponse = new ResponsePacket(ResponsePacket::kResponseTypeFileTransfer); success = ReceivePacket(fileTransferResponse); @@ -1031,7 +1031,7 @@ bool BridgeManager::SendFile(FILE *file, unsigned int destination, unsigned int lastSequenceSize++; } - long bytesTransferred = 0; + unsigned int bytesTransferred = 0; unsigned int currentPercent; unsigned int previousPercent = 0; Interface::Print("0%%"); @@ -1144,7 +1144,7 @@ bool BridgeManager::SendFile(FILE *file, unsigned int destination, unsigned int if (bytesTransferred > fileSize) bytesTransferred = fileSize; - currentPercent = (int)(100.0f * ((float)bytesTransferred / (float)fileSize)); + currentPercent = (unsigned int)(100.0 * ((double)bytesTransferred / (double)fileSize)); if (currentPercent != previousPercent) { diff --git a/heimdall/source/DownloadPitAction.cpp b/heimdall/source/DownloadPitAction.cpp index 000844e..841afd2 100644 --- a/heimdall/source/DownloadPitAction.cpp +++ b/heimdall/source/DownloadPitAction.cpp @@ -120,7 +120,7 @@ int DownloadPitAction::Execute(int argc, char **argv) // Open output file const char *outputFilename = outputArgument->GetValue().c_str(); - FILE *outputPitFile = fopen(outputFilename, "wb"); + FILE *outputPitFile = FileOpen(outputFilename, "wb"); if (!outputPitFile) { @@ -135,7 +135,7 @@ int DownloadPitAction::Execute(int argc, char **argv) if (bridgeManager->Initialise(resume) != BridgeManager::kInitialiseSucceeded || !bridgeManager->BeginSession()) { - fclose(outputPitFile); + FileClose(outputPitFile); delete bridgeManager; return (1); @@ -164,7 +164,7 @@ int DownloadPitAction::Execute(int argc, char **argv) delete bridgeManager; - fclose(outputPitFile); + FileClose(outputPitFile); delete [] pitBuffer; return (success ? 0 : 1); diff --git a/heimdall/source/FlashAction.cpp b/heimdall/source/FlashAction.cpp index aa08ed8..590e0b5 100644 --- a/heimdall/source/FlashAction.cpp +++ b/heimdall/source/FlashAction.cpp @@ -87,7 +87,7 @@ static bool openFiles(Arguments& arguments, vector& partitionFile if (pitArgument) { - pitFile = fopen(pitArgument->GetValue().c_str(), "rb"); + pitFile = FileOpen(pitArgument->GetValue().c_str(), "rb"); if (!pitFile) { @@ -109,7 +109,7 @@ static bool openFiles(Arguments& arguments, vector& partitionFile if (arguments.GetArgumentTypes().find(argumentName) == arguments.GetArgumentTypes().end()) { const StringArgument *stringArgument = static_cast(*it); - FILE *file = fopen(stringArgument->GetValue().c_str(), "rb"); + FILE *file = FileOpen(stringArgument->GetValue().c_str(), "rb"); if (!file) { @@ -130,34 +130,34 @@ static void closeFiles(vector& partitionFiles, FILE *& pitFile) if (pitFile) { - fclose(pitFile); + FileClose(pitFile); pitFile = nullptr; } // Close partition files for (vector::const_iterator it = partitionFiles.begin(); it != partitionFiles.end(); it++) - fclose(it->file); + FileClose(it->file); partitionFiles.clear(); } static bool sendTotalTransferSize(BridgeManager *bridgeManager, const vector& partitionFiles, FILE *pitFile, bool repartition) { - int totalBytes = 0; + unsigned int totalBytes = 0; for (vector::const_iterator it = partitionFiles.begin(); it != partitionFiles.end(); it++) { - fseek(it->file, 0, SEEK_END); - totalBytes += ftell(it->file); - rewind(it->file); + FileSeek(it->file, 0, SEEK_END); + totalBytes += (unsigned int)FileTell(it->file); + FileRewind(it->file); } if (repartition) { - fseek(pitFile, 0, SEEK_END); - totalBytes += ftell(pitFile); - rewind(pitFile); + FileSeek(pitFile, 0, SEEK_END); + totalBytes += (unsigned int)FileTell(pitFile); + FileRewind(pitFile); } bool success; @@ -168,7 +168,7 @@ static bool sendTotalTransferSize(BridgeManager *bridgeManager, const vector 0) { - rewind(pitFile); + FileRewind(pitFile); localPitData = new PitData(); localPitData->Unpack(pitFileBuffer); diff --git a/heimdall/source/Heimdall.h b/heimdall/source/Heimdall.h index 5e348c4..3982dd4 100644 --- a/heimdall/source/Heimdall.h +++ b/heimdall/source/Heimdall.h @@ -30,6 +30,12 @@ #define va_copy(d, s) ((d) = (s)) #endif +#define FileOpen(FILE, MODE) fopen(FILE, MODE) +#define FileClose(FILE) fclose(FILE) +#define FileSeek(FILE, OFFSET, ORIGIN) _fseeki64(FILE, OFFSET, ORIGIN) +#define FileTell(FILE) _ftelli64(FILE) +#define FileRewind(FILE) rewind(FILE) + #else #include "../config.h" @@ -37,6 +43,13 @@ #if defined(OS_DARWIN) || defined(OS_LINUX) #include #define Sleep(t) usleep(1000*t) + +#define FileOpen(FILE, MODE) fopen(FILE, MODE) +#define FileClose(FILE) fclose(FILE) +#define FileSeek(FILE, OFFSET, ORIGIN) fseeko(FILE, OFFSET, ORIGIN) +#define FileTell(FILE) ftello(FILE) +#define FileRewind(FILE) rewind(FILE) + #else #error operating system not supported #endif diff --git a/heimdall/source/PrintPitAction.cpp b/heimdall/source/PrintPitAction.cpp index 781973d..12c28d1 100644 --- a/heimdall/source/PrintPitAction.cpp +++ b/heimdall/source/PrintPitAction.cpp @@ -115,7 +115,7 @@ int PrintPitAction::Execute(int argc, char **argv) { const char *filename = fileArgument->GetValue().c_str(); - localPitFile = fopen(filename, "rb"); + localPitFile = FileOpen(filename, "rb"); if (!localPitFile) { @@ -133,14 +133,14 @@ int PrintPitAction::Execute(int argc, char **argv) { // Print PIT from file; there's no need for a BridgeManager. - fseek(localPitFile, 0, SEEK_END); - long localPitFileSize = ftell(localPitFile); - rewind(localPitFile); + FileSeek(localPitFile, 0, SEEK_END); + unsigned int localPitFileSize = (unsigned int)FileTell(localPitFile); + FileRewind(localPitFile); // Load the local pit file into memory. unsigned char *pitFileBuffer = new unsigned char[localPitFileSize]; size_t dataRead = fread(pitFileBuffer, 1, localPitFileSize, localPitFile); // dataRead is discarded, it's here to remove warnings. - fclose(localPitFile); + FileClose(localPitFile); PitData *pitData = new PitData(); pitData->Unpack(pitFileBuffer); diff --git a/heimdall/source/SendFilePartPacket.h b/heimdall/source/SendFilePartPacket.h index 3ecaa89..38e9dbe 100644 --- a/heimdall/source/SendFilePartPacket.h +++ b/heimdall/source/SendFilePartPacket.h @@ -38,11 +38,11 @@ namespace Heimdall { memset(data, 0, size); - long position = ftell(file); + unsigned int position = (unsigned int)FileTell(file); - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); - fseek(file, position, SEEK_SET); + FileSeek(file, 0, SEEK_END); + unsigned int fileSize = (unsigned int)FileTell(file); + FileSeek(file, position, SEEK_SET); // min(fileSize, size) unsigned int bytesToRead = (fileSize < size) ? fileSize - position : size; -- cgit v1.2.3