From 934c1376975cb15b394b7fe5336d7e265686965b Mon Sep 17 00:00:00 2001 From: Frank Li Date: Thu, 2 Aug 2018 14:14:29 -0500 Subject: [PATCH] support big file write at option --raw Signed-off-by: Frank Li --- src/CStCFBootImager.cpp | 18 ++++++++- src/StExtraComponent.cpp | 52 +++++++++++++++++++----- src/StExtraComponent.h | 6 ++- src/cfimager.cpp | 51 ++++++++++++++---------- src/cfimager.sln | 31 +++++++++++---- src/cfimager.vcxproj | 86 +++++++++++++++++++++++++++++++++++++++- src/stdafx.h | 14 +------ 7 files changed, 199 insertions(+), 59 deletions(-) diff --git a/src/CStCFBootImager.cpp b/src/CStCFBootImager.cpp index e18757f..6452c95 100644 --- a/src/CStCFBootImager.cpp +++ b/src/CStCFBootImager.cpp @@ -109,9 +109,23 @@ void CStCFBootImager::setVolumeLabel(const std::string & label) void CStCFBootImager::writeImage() { assert(m_device); - assert(m_firmware); + + if (m_bRaw) + { + std::string filename; + + m_extra->GetExtraFilename(filename); + + CStExtraComponent extra(filename, filename); + extra.set_extra_reserved_size(0); + + extra.WriteToDisk(this->m_offset / m_blockSize, m_device, this->m_skip); + return; + } + + assert(m_firmware); - if (m_bImage || m_bRaw) + if (m_bImage) { writeFirmware(); return; diff --git a/src/StExtraComponent.cpp b/src/StExtraComponent.cpp index 881b9cb..806af29 100644 --- a/src/StExtraComponent.cpp +++ b/src/StExtraComponent.cpp @@ -26,6 +26,8 @@ CStExtraComponent::CStExtraComponent(std::string & extraFileName, m_extra_filesize = 0; + m_extra_reserved_size = EXTRA_RESERVERD_SIZE; + m_last_error = PrepareData(); } @@ -82,7 +84,7 @@ ST_ERROR CStExtraComponent::PrepareData() uint64_t CStExtraComponent::GetSizeInBytes() { - return m_extra_filesize + EXTRA_RESERVERD_SIZE; + return m_extra_filesize + m_extra_reserved_size; } uint64_t CStExtraComponent::GetSizeInSectors(uint32_t sector_size) @@ -103,10 +105,10 @@ uint64_t CStExtraComponent::GetSizeInSectors(uint32_t sector_size) return sectors; } -void CStExtraComponent::WriteToDisk(int32_t start_block, CStBlockDevice * pDevice) +void CStExtraComponent::WriteToDisk(int32_t start_block, CStBlockDevice * pDevice, uint64_t input_skip_byte) { int buffersize; - buffersize = 0x1000; + buffersize = 0x100000; char *buff =new char[buffersize]; @@ -132,15 +134,33 @@ void CStExtraComponent::WriteToDisk(int32_t start_block, CStBlockDevice * pDevic return ; } - int i=0; - int precent, old; - precent = old =0; - while( fw_file.read(buff,buffersize) >0) - { - pDevice->writeBlocks(start_block+i,buffersize/pDevice->getBlockSize(),buff); - i+=buffersize/pDevice->getBlockSize(); + uint64_t i=0; + uint64_t precent, old; + precent= 0; + old = 1000; + + fw_file.seekg(input_skip_byte, ifstream::beg); + + DWORD start = GetTickCount(); - precent = (i)*100 /(this->m_extra_filesize/pDevice->getBlockSize()); + uint64_t total_blocks = this->m_extra_filesize; + total_blocks += pDevice->getBlockSize() - 1; + total_blocks /= pDevice->getBlockSize(); + + while(1) + { + fw_file.read(buff, buffersize); + if (fw_file.gcount() == 0) + break; + + uint64_t sz = fw_file.gcount(); + sz += pDevice->getBlockSize() - 1; + sz /= pDevice->getBlockSize(); + + pDevice->writeBlocks(start_block+i, sz,buff); + i += sz; + + precent = (i)*100 / total_blocks; if(precent != old) { printf("write %d%%\r", precent); @@ -148,6 +168,16 @@ void CStExtraComponent::WriteToDisk(int32_t start_block, CStBlockDevice * pDevic } } + DWORD end = GetTickCount(); + + uint64_t speed; + if (end - start == 0) + speed = 1000; + else + speed = (total_blocks * pDevice->getBlockSize()) * 1000 / (end - start); + + printf("\n\nTotal: %d.%03ds speed:%lld.%03lldMB/s", (end - start) / 1000, (end - start) % 1000, + speed / 10000000, speed / 1000); delete buff; } \ No newline at end of file diff --git a/src/StExtraComponent.h b/src/StExtraComponent.h index e90d647..8eace45 100644 --- a/src/StExtraComponent.h +++ b/src/StExtraComponent.h @@ -29,12 +29,14 @@ class CStExtraComponent : uint64_t GetSizeInBytes(); uint64_t GetSizeInSectors(uint32_t sector_size); - void WriteToDisk(int32_t start_block, CStBlockDevice * pDevice); + void WriteToDisk(int32_t start_block, CStBlockDevice * pDevice, uint64_t input_skip_byte=0); void CStExtraComponent::GetExtraFilename(std::string& outFilename); + void set_extra_reserved_size(uint64_t x) { m_extra_reserved_size = x; } private: ST_ERROR PrepareData(); std::string m_extra_filename; - uint64_t m_extra_filesize; + uint64_t m_extra_filesize; + uint64_t m_extra_reserved_size; }; diff --git a/src/cfimager.cpp b/src/cfimager.cpp index 94ccf11..c700bb1 100644 --- a/src/cfimager.cpp +++ b/src/cfimager.cpp @@ -148,34 +148,43 @@ class CFBootImagerTool : public CStCFBootImager::Listener printf("device block size = %u bytes\n", device.getBlockSize()); printf("device block count = %#x\n\n", device.getBlockCount()); + CStCFBootImager imager; + // check if media is removable // if (!m_showInfo && !device.isMediaRemovable()) // { // throw std::runtime_error("media is not removable"); // } - - CStFwComponent firmware(m_filename,"CStFwComponent"); - // create firmware instance - unsigned extraBlocks=0; - if( !m_filename.empty() ) - { + if(!m_bRaw) + { + CStFwComponent firmware(m_filename,"CStFwComponent"); + // create firmware instance + unsigned extraBlocks=0; + if( !m_filename.empty()) + { - THROW_IF_ST_ERROR( firmware.GetLastError()); - uint64_t firmwareSize = firmware.GetSizeInBytes(); - if (firmwareSize == 0) - { - throw std::runtime_error("empty firmware file"); - } + THROW_IF_ST_ERROR( firmware.GetLastError()); + uint64_t firmwareSize = firmware.GetSizeInBytes(); + if (firmwareSize == 0) + { + throw std::runtime_error("empty firmware file"); + } - uint64_t firmwareBlocks = firmware.GetSizeInSectors(device.getBlockSize()); - printf("firmware size = %#x bytes (%#x blocks)\n", (uint32_t)firmwareSize, (uint32_t)firmwareBlocks); + uint64_t firmwareBlocks = firmware.GetSizeInSectors(device.getBlockSize()); + printf("firmware size = %#x bytes (%#x blocks)\n", (uint32_t)firmwareSize, (uint32_t)firmwareBlocks); - // compute extra space in blocks - extraBlocks = computeExtraBlocks(device.getBlockSize(), firmwareSize); - printf("extra blocks = %u\n", extraBlocks); + // compute extra space in blocks + extraBlocks = computeExtraBlocks(device.getBlockSize(), firmwareSize); + printf("extra blocks = %u\n", extraBlocks); + } + imager.setFirmware(&firmware); + imager.setExtraBlocks(extraBlocks); } - + + if (m_bRaw && m_extra_filename.empty()) + m_extra_filename = m_filename; + CStExtraComponent extra(m_extra_filename,"CStFwComponent"); if( !m_extra_filename.empty()) { @@ -185,17 +194,15 @@ class CFBootImagerTool : public CStCFBootImager::Listener // now do the real work try { - CStCFBootImager imager; imager.setROMVersion(m_romVersion); imager.setListener(this); imager.setDevice(&device); - imager.setFirmware(&firmware); + imager.setExtra(&extra); imager.setAlwaysFormat(m_alwaysFormat); imager.setWriteFAT32(!m_skipFatFormat); imager.setWinCEVersion(m_WinCEVersion); - imager.setExtraBlocks(extraBlocks); - imager.Image(m_bImage); + imager.Image(m_bImage); imager.RedundantBoot(m_bRedundantBoot); imager.BCBBoot(m_bBCBBoot); imager.m_bRaw = m_bRaw; diff --git a/src/cfimager.sln b/src/cfimager.sln index fdac4d7..8531ccc 100644 --- a/src/cfimager.sln +++ b/src/cfimager.sln @@ -1,19 +1,36 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2027 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cfimager", "cfimager.vcxproj", "{5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Default = Debug|Default - Release|Default = Release|Default + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|Default.ActiveCfg = Debug|Win32 - {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|Default.Build.0 = Debug|Win32 - {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|Default.ActiveCfg = Release|Win32 - {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|Default.Build.0 = Release|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|Win32.ActiveCfg = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|Win32.Build.0 = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|x64.ActiveCfg = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|x64.Build.0 = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|x86.ActiveCfg = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Debug|x86.Build.0 = Debug|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|Win32.ActiveCfg = Release|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|Win32.Build.0 = Release|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|x64.ActiveCfg = Release|x64 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|x64.Build.0 = Release|x64 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|x86.ActiveCfg = Release|Win32 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {48BBB538-48F5-46C3-8416-4FAD704E6609} + EndGlobalSection EndGlobal diff --git a/src/cfimager.vcxproj b/src/cfimager.vcxproj index e4088b9..927d136 100644 --- a/src/cfimager.vcxproj +++ b/src/cfimager.vcxproj @@ -5,25 +5,44 @@ Debug Win32 + + Debug + x64 + Release Win32 + + Release + x64 + {5E51B414-2D1B-4E42-96B7-2FC006CD0BFD} cfimager Win32Proj + 8.1 Application - v110 + v141 + MultiByte + + + Application + v141 MultiByte Application - v110 + v141 + MultiByte + + + Application + v141 MultiByte @@ -33,10 +52,18 @@ + + + + + + + + <_ProjectFileVersion>11.0.50727.1 @@ -46,11 +73,17 @@ Debug\ true + + true + Release\ Release\ false + + false + Disabled @@ -77,6 +110,33 @@ + + + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebug + true + + + Level3 + ProgramDatabase + + + $(OutDir)cfimager.exe + RequireAdministrator + true + $(OutDir)cfimager.pdb + Console + false + + + + + + + + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) @@ -98,6 +158,28 @@ MachineX86 + + + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreaded + true + + + Level3 + ProgramDatabase + + + $(OutDir)cfimager.exe + RequireAdministrator + true + Console + true + true + false + + + + diff --git a/src/stdafx.h b/src/stdafx.h index b3d92cf..34fa69e 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -14,16 +14,4 @@ #include -// define the standard C99 types that are stupidly missing from windows - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned long uint32_t; -typedef unsigned long long uint64_t; - -typedef signed char int8_t; -typedef signed short int16_t; -typedef signed long int32_t; -typedef signed long long int64_t; - - +// define the standard C99 types that are stupidly missing from windows \ No newline at end of file