Skip to content

Commit

Permalink
support big file write at option --raw
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Li <[email protected]>
  • Loading branch information
nxpfrankli committed Aug 2, 2018
1 parent 359c4bc commit 934c137
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 59 deletions.
18 changes: 16 additions & 2 deletions src/CStCFBootImager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 41 additions & 11 deletions src/StExtraComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ CStExtraComponent::CStExtraComponent(std::string & extraFileName,

m_extra_filesize = 0;

m_extra_reserved_size = EXTRA_RESERVERD_SIZE;

m_last_error = PrepareData();
}

Expand Down Expand Up @@ -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)
Expand All @@ -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];

Expand All @@ -132,22 +134,50 @@ 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);
old =precent;
}
}

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;

}
6 changes: 4 additions & 2 deletions src/StExtraComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
51 changes: 29 additions & 22 deletions src/cfimager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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;
Expand Down
31 changes: 24 additions & 7 deletions src/cfimager.sln
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 934c137

Please sign in to comment.