diff options
author | Manuel Coenen <manuel@duet3d.com> | 2021-02-15 18:12:16 +0300 |
---|---|---|
committer | Manuel Coenen <manuel@duet3d.com> | 2021-02-15 18:12:16 +0300 |
commit | 3ba6159e1c646fb3ca3d6a35807cef23703d6c4b (patch) | |
tree | 01ed2cb679ea1789d2f6a54c9313513f563cd06a /src/bossa/Flasher.cpp | |
parent | a0ad84462bc8dbe98204ade0e2f65cdb2a0fdd32 (diff) |
Use stack for filename instead of allocting it permanently
Diffstat (limited to 'src/bossa/Flasher.cpp')
-rw-r--r-- | src/bossa/Flasher.cpp | 148 |
1 files changed, 58 insertions, 90 deletions
diff --git a/src/bossa/Flasher.cpp b/src/bossa/Flasher.cpp index 976906a0..774c00d6 100644 --- a/src/bossa/Flasher.cpp +++ b/src/bossa/Flasher.cpp @@ -44,7 +44,7 @@ void Flasher::erase(uint32_t foffset) THROWS(GCodeException) _flash->eraseAuto(false); } -bool Flasher::write(const char* filename, const char* dir, uint32_t& foffset) THROWS(GCodeException) +bool Flasher::write(FileStore *infile, uint32_t& foffset) THROWS(GCodeException) { uint32_t pageSize = _flash->pageSize(); uint32_t numPages; @@ -54,64 +54,48 @@ bool Flasher::write(const char* filename, const char* dir, uint32_t& foffset) TH { pageNum = 0; Platform& platform = reprap.GetPlatform(); - infile = platform.OpenFile(dir, filename, OpenMode::read); - if (infile == nullptr) - { - platform.MessageF(ErrorMessage, "Failed to open file %s\n", filename); - throw GCodeException(nullptr); - } if (infile->Length() == 0) { infile->Close(); - platform.MessageF(ErrorMessage, "Firmware file is empty %s\n", filename); + platform.MessageF(ErrorMessage, "Firmware file is empt\n"); throw GCodeException(nullptr); } } - try - { - - numPages = (infile->Length() + pageSize - 1) / pageSize; - if (foffset == 0) - { - if (numPages > _flash->numPages()) - { - throw FileSizeError("Flasher::write: FileSizeError"); - } + numPages = (infile->Length() + pageSize - 1) / pageSize; + if (foffset == 0) + { + if (numPages > _flash->numPages()) + { + throw FileSizeError("Flasher::write: FileSizeError"); + } - _observer.onStatus("Writing %ld bytes to PanelDue flash memory\n", infile->Length()); - } + _observer.onStatus("Writing %ld bytes to PanelDue flash memory\n", infile->Length()); + } - uint8_t buffer[pageSize]; - uint32_t pageOffset = foffset / pageSize; + uint8_t buffer[pageSize]; + uint32_t pageOffset = foffset / pageSize; - if ((fbytes = infile->Read((char*)buffer, pageSize)) > 0) - { - _observer.onProgress(pageNum, numPages); + if ((fbytes = infile->Read((char*)buffer, pageSize)) > 0) + { + _observer.onProgress(pageNum, numPages); - _flash->loadBuffer(buffer, fbytes); - _flash->writePage(pageOffset /* + pageNum */); - foffset += fbytes; + _flash->loadBuffer(buffer, fbytes); + _flash->writePage(pageOffset /* + pageNum */); + foffset += fbytes; - pageNum++; - if (!(pageNum == numPages || fbytes != (int)pageSize)) - { - return false; // We get back in the next PanelDueFlasher::Spin() iteration - } + pageNum++; + if (!(pageNum == numPages || fbytes != (int)pageSize)) + { + return false; // We get back in the next PanelDueFlasher::Spin() iteration } - } - catch(...) - { - infile->Close(); - throw; - } + } - infile->Close(); _observer.onProgress(numPages, numPages); return true; } -bool Flasher::verify(const char* filename, const char* dir, uint32_t& pageErrors, uint32_t& totalErrors, uint32_t& foffset) THROWS(GCodeException) +bool Flasher::verify(FileStore *infile, uint32_t& pageErrors, uint32_t& totalErrors, uint32_t& foffset) THROWS(GCodeException) { uint32_t pageSize = _flash->pageSize(); uint8_t bufferA[pageSize]; @@ -130,62 +114,46 @@ bool Flasher::verify(const char* filename, const char* dir, uint32_t& pageErrors if (foffset == 0) { pageNum = 0; + } - // Note that we can skip all checks for file validity here since - // we would not get here from write() if any of these failed + numPages = (infile->Length() + pageSize - 1) / pageSize; + if (foffset == 0) + { + if (numPages > _flash->numPages()) + throw FileSizeError("Flasher::verify: FileSizeError"); - infile = reprap.GetPlatform().OpenFile(dir, filename, OpenMode::read); - } + _observer.onStatus("Verifying %ld bytes of PanelDue flash memory\n", infile->Length()); + } - try - { + if ((fbytes = infile->Read((char*)bufferA, pageSize)) > 0) + { + byteErrors = 0; + foffset += fbytes; - numPages = (infile->Length() + pageSize - 1) / pageSize; - if (foffset == 0) - { - if (numPages > _flash->numPages()) - throw FileSizeError("Flasher::verify: FileSizeError"); + _observer.onProgress(pageNum, numPages); - _observer.onStatus("Verifying %ld bytes of PanelDue flash memory\n", infile->Length()); - } + { + _flash->readPage(pageOffset /*+ pageNum */, bufferB); - if ((fbytes = infile->Read((char*)bufferA, pageSize)) > 0) - { - byteErrors = 0; - foffset += fbytes; - - _observer.onProgress(pageNum, numPages); - - { - _flash->readPage(pageOffset /*+ pageNum */, bufferB); - - for (uint32_t i = 0; i < (uint32_t) fbytes; i++) - { - if (bufferA[i] != bufferB[i]) - byteErrors++; - } - } - - if (byteErrors != 0) - { - pageErrors++; - totalErrors += byteErrors; - } - - pageNum++; - if (!(pageNum == numPages || fbytes != (int)pageSize)) - { - return false; // We get back in the next PanelDueFlasher::Spin() iteration - } - } - } - catch(...) - { - infile->Close(); - throw; - } + for (uint32_t i = 0; i < (uint32_t) fbytes; i++) + { + if (bufferA[i] != bufferB[i]) + byteErrors++; + } + } + + if (byteErrors != 0) + { + pageErrors++; + totalErrors += byteErrors; + } - infile->Close(); + pageNum++; + if (!(pageNum == numPages || fbytes != (int)pageSize)) + { + return false; // We get back in the next PanelDueFlasher::Spin() iteration + } + } _observer.onProgress(numPages, numPages); |