1.support to write ubi image

2.cut off iconv reference

Signed-off-by: liuyi <liuyi@rock-chips.com>
This commit is contained in:
liuyi 2020-08-31 11:46:24 +08:00
parent 6e92ebcf8b
commit 0dcb0a4ca1
3 changed files with 208 additions and 87 deletions

View File

@ -8,7 +8,7 @@
#include <dirent.h> #include <dirent.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <iconv.h> // #include <iconv.h>
#include <wchar.h> #include <wchar.h>
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
@ -88,6 +88,7 @@ typedef struct sparse_header_t {
/* table implementation */ /* table implementation */
} sparse_header; } sparse_header;
#define SPARSE_HEADER_MAGIC 0xed26ff3a #define SPARSE_HEADER_MAGIC 0xed26ff3a
#define UBI_HEADER_MAGIC 0x23494255
#define CHUNK_TYPE_RAW 0xCAC1 #define CHUNK_TYPE_RAW 0xCAC1
#define CHUNK_TYPE_FILL 0xCAC2 #define CHUNK_TYPE_FILL 0xCAC2
#define CHUNK_TYPE_DONT_CARE 0xCAC3 #define CHUNK_TYPE_DONT_CARE 0xCAC3
@ -230,6 +231,6 @@ typedef enum{
typedef void (*ProgressPromptCB)(DWORD deviceLayer, ENUM_PROGRESS_PROMPT promptID, long long totalValue, long long currentValue, ENUM_CALL_STEP emCall); typedef void (*ProgressPromptCB)(DWORD deviceLayer, ENUM_PROGRESS_PROMPT promptID, long long totalValue, long long currentValue, ENUM_CALL_STEP emCall);
bool WideStringToString(wchar_t *pszSrc, char *&pszDest); // bool WideStringToString(wchar_t *pszSrc, char *&pszDest);
bool StringToWideString(char *pszSrc, wchar_t *&pszDest); // bool StringToWideString(char *pszSrc, wchar_t *&pszDest);
#endif #endif

View File

@ -89,7 +89,7 @@ typedef struct {
#define DIRECTION_OUT 0x00 #define DIRECTION_OUT 0x00
#define DIRECTION_IN 0x80 #define DIRECTION_IN 0x80
#define MAX_TEST_BLOCKS 512 #define MAX_TEST_BLOCKS 512
#define MAX_ERASE_BLOCKS 128 #define MAX_ERASE_BLOCKS 16
#define MAX_CLEAR_LEN 16*1024 #define MAX_CLEAR_LEN 16*1024
#ifndef ERR_SUCCESS #ifndef ERR_SUCCESS

286
main.cpp
View File

@ -143,81 +143,81 @@ void PrintData(PBYTE pData, int nSize)
printf(" %s\r\n", szPrint); printf(" %s\r\n", szPrint);
} }
bool StringToWideString(char *pszSrc, wchar_t *&pszDest) // bool StringToWideString(char *pszSrc, wchar_t *&pszDest)
{ // {
if (!pszSrc) // if (!pszSrc)
return false; // return false;
int nSrcLen = strlen(pszSrc); // int nSrcLen = strlen(pszSrc);
int nDestLen = nSrcLen * 2; // int nDestLen = nSrcLen * 2;
//
pszDest = NULL; // pszDest = NULL;
pszDest = new wchar_t[nDestLen]; // pszDest = new wchar_t[nDestLen];
if (!pszDest) // if (!pszDest)
return false; // return false;
nDestLen = nDestLen * sizeof(wchar_t); // nDestLen = nDestLen * sizeof(wchar_t);
memset(pszDest, 0, nDestLen); // memset(pszDest, 0, nDestLen);
int iRet; // int iRet;
iconv_t cd; // iconv_t cd;
cd = iconv_open("UTF-32", "UTF-8"); // cd = iconv_open("UTF-32", "UTF-8");
if((iconv_t)-1 == cd) { // if((iconv_t)-1 == cd) {
delete []pszDest; // delete []pszDest;
pszDest = NULL; // pszDest = NULL;
return false; // return false;
} // }
char *pIn, *pOut; // char *pIn, *pOut;
pIn = (char *)pszSrc; // pIn = (char *)pszSrc;
pOut = (char *)pszDest; // pOut = (char *)pszDest;
//
iRet = iconv(cd, (char **)&pIn, (size_t *)&nSrcLen, (char **)&pOut, (size_t *)&nDestLen); // iRet = iconv(cd, (char **)&pIn, (size_t *)&nSrcLen, (char **)&pOut, (size_t *)&nDestLen);
//
if(iRet == -1) { // if(iRet == -1) {
delete []pszDest; // delete []pszDest;
pszDest = NULL; // pszDest = NULL;
iconv_close(cd); // iconv_close(cd);
return false; // return false;
} // }
//
iconv_close(cd); // iconv_close(cd);
//
return true; // return true;
} // }
bool WideStringToString(wchar_t *pszSrc, char *&pszDest) // bool WideStringToString(wchar_t *pszSrc, char *&pszDest)
{ // {
if (!pszSrc) // if (!pszSrc)
return false; // return false;
int nSrcLen = wcslen(pszSrc); // int nSrcLen = wcslen(pszSrc);
int nDestLen = nSrcLen * 2; // int nDestLen = nSrcLen * 2;
nSrcLen = nSrcLen * sizeof(wchar_t); // nSrcLen = nSrcLen * sizeof(wchar_t);
pszDest = NULL; // pszDest = NULL;
pszDest = new char[nDestLen]; // pszDest = new char[nDestLen];
if (!pszDest) // if (!pszDest)
return false; // return false;
memset(pszDest, 0, nDestLen); // memset(pszDest, 0, nDestLen);
int iRet; // int iRet;
iconv_t cd; // iconv_t cd;
cd = iconv_open("UTF-8", "UTF-32"); // cd = iconv_open("UTF-8", "UTF-32");
//
if((iconv_t)-1 == cd) { // if((iconv_t)-1 == cd) {
delete []pszDest; // delete []pszDest;
pszDest = NULL; // pszDest = NULL;
return false; // return false;
} // }
char *pIn, *pOut; // char *pIn, *pOut;
pIn = (char *)pszSrc; // pIn = (char *)pszSrc;
pOut = (char *)pszDest; // pOut = (char *)pszDest;
iRet = iconv(cd, (char **)&pIn, (size_t *)&nSrcLen, (char **)&pOut, (size_t *)&nDestLen); // iRet = iconv(cd, (char **)&pIn, (size_t *)&nSrcLen, (char **)&pOut, (size_t *)&nDestLen);
//
if(iRet == -1) { // if(iRet == -1) {
delete []pszDest; // delete []pszDest;
pszDest = NULL; // pszDest = NULL;
iconv_close(cd); // iconv_close(cd);
return false; // return false;
} // }
//
iconv_close(cd); // iconv_close(cd);
//
return true; // return true;
} // }
int find_config_item(CONFIG_ITEM_VECTOR &vecItems, const char *pszName) int find_config_item(CONFIG_ITEM_VECTOR &vecItems, const char *pszName)
{ {
unsigned int i; unsigned int i;
@ -547,6 +547,31 @@ bool is_sparse_image(char *szImage)
return true; return true;
} }
bool is_ubifs_image(char *szImage)
{
FILE *file = NULL;
u32 magic;
u32 uiRead;
file = fopen(szImage, "rb");
if( !file ) {
if (g_pLogObject)
g_pLogObject->Record("%s failed, err=%d, can't open file: %s\r\n", __func__, errno, szImage);
return false;
}
uiRead = fread(&magic, 1, sizeof(magic), file);
if (uiRead != sizeof(magic)) {
if (g_pLogObject)
g_pLogObject->Record("%s failed, err=%d, read=%d, total=%d\r\n", __func__, errno, uiRead, sizeof(magic));
fclose(file);
return false;
}
fclose(file);
if (magic!=UBI_HEADER_MAGIC)
{
return false;
}
return true;
}
void gen_rand_uuid(unsigned char *uuid_bin) void gen_rand_uuid(unsigned char *uuid_bin)
{ {
efi_guid_t id; efi_guid_t id;
@ -2296,6 +2321,7 @@ bool read_flash_info(STRUCT_RKDEVICE_DESC &dev)
printf("\tManufacturer: %s, value=%02X\r\n", "Unknown", info.bManufCode); printf("\tManufacturer: %s, value=%02X\r\n", "Unknown", info.bManufCode);
printf("\tFlash Size: %d MB\r\n", info.uiFlashSize / 2 / 1024); printf("\tFlash Size: %d MB\r\n", info.uiFlashSize / 2 / 1024);
printf("\tFlash Size: %d Sectors\r\n", info.uiFlashSize);
printf("\tBlock Size: %d KB\r\n", info.usBlockSize / 2); printf("\tBlock Size: %d KB\r\n", info.usBlockSize / 2);
printf("\tPage Size: %d KB\r\n", info.bPageSize / 2); printf("\tPage Size: %d KB\r\n", info.bPageSize / 2);
printf("\tECC Bits: %d\r\n", info.bECCBits); printf("\tECC Bits: %d\r\n", info.bECCBits);
@ -2525,6 +2551,80 @@ Exit_ReadLBA:
fclose(file); fclose(file);
return bSuccess; return bSuccess;
} }
bool erase_ubi_block(STRUCT_RKDEVICE_DESC &dev, u32 uiOffset, u32 uiPartSize)
{
STRUCT_FLASHINFO_CMD info;
CRKComm *pComm = NULL;
BYTE flashID[5];
bool bRet,bSuccess=false;
UINT uiReadCount,uiStartBlock,uiEraseBlock,uiBlockCount,uiErasePos;
int iRet;
DWORD *pID=NULL;
if (!check_device_type(dev, RKUSB_LOADER | RKUSB_MASKROM))
return false;
pComm = new CRKUsbComm(dev, g_pLogObject, bRet);
if (!bRet)
{
printf("Erase ubi quit, creating comm object failed!\r\n");
goto EXIT_UBI_ERASE;
}
iRet = pComm->RKU_ReadFlashID(flashID);
if(iRet!=ERR_SUCCESS)
{
if (g_pLogObject)
{
g_pLogObject->Record("Error:EraseUBIBlock-->RKU_ReadFlashID failed,RetCode(%d)",iRet);
}
goto EXIT_UBI_ERASE;
}
pID = (DWORD *)flashID;
if (*pID==0x434d4d45)//emmc
{
bSuccess = true;
goto EXIT_UBI_ERASE;
}
iRet = pComm->RKU_ReadFlashInfo((BYTE *)&info,&uiReadCount);
if (iRet!=ERR_SUCCESS)
{
if (g_pLogObject)
g_pLogObject->Record("Error:EraseUBIBlock-->RKU_ReadFlashInfo err=%d", iRet);
goto EXIT_UBI_ERASE;
}
if (uiPartSize==0xFFFFFFFF)
uiPartSize = info.uiFlashSize - uiOffset - (info.usBlockSize * 4);
uiStartBlock = uiOffset / info.usBlockSize;
if ((uiPartSize % info.usBlockSize) == 0)
uiEraseBlock = uiPartSize / info.usBlockSize;
else
uiEraseBlock = uiPartSize / info.usBlockSize + 1;
uiErasePos=uiStartBlock;
while (uiEraseBlock>0)
{
uiBlockCount = (uiEraseBlock<MAX_ERASE_BLOCKS)?uiEraseBlock:MAX_ERASE_BLOCKS;
iRet = pComm->RKU_EraseBlock(0,uiErasePos,uiBlockCount,ERASE_FORCE);
if ((iRet!=ERR_SUCCESS)&&(iRet!=ERR_FOUND_BAD_BLOCK))
{
if (g_pLogObject)
{
g_pLogObject->Record("Error:EraseUBIBlock-->RKU_EraseBlock failed,RetCode(%d)",iRet);
}
goto EXIT_UBI_ERASE;
}
uiErasePos += uiBlockCount;
uiEraseBlock -= uiBlockCount;
}
bSuccess = true;
EXIT_UBI_ERASE:
if (pComm)
delete pComm;
return bSuccess;
}
bool erase_partition(CRKUsbComm *pComm, UINT uiOffset, UINT uiSize) bool erase_partition(CRKUsbComm *pComm, UINT uiOffset, UINT uiSize)
{ {
UINT uiErase=1024*32; UINT uiErase=1024*32;
@ -2764,6 +2864,7 @@ bool write_lba(STRUCT_RKDEVICE_DESC &dev, UINT uiBegin, char *szFile)
UINT uiLen; UINT uiLen;
int nSectorSize = 512; int nSectorSize = 512;
BYTE pBuf[nSectorSize * DEFAULT_RW_LBA]; BYTE pBuf[nSectorSize * DEFAULT_RW_LBA];
pComm = new CRKUsbComm(dev, g_pLogObject, bRet); pComm = new CRKUsbComm(dev, g_pLogObject, bRet);
if (bRet) { if (bRet) {
@ -2948,11 +3049,9 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan)
return true; return true;
} else if (strcmp(strCmd.c_str(), "PACK") == 0) {//pack boot loader } else if (strcmp(strCmd.c_str(), "PACK") == 0) {//pack boot loader
mergeBoot(); mergeBoot();
return true; return true;
} else if (strcmp(strCmd.c_str(), "UNPACK") == 0) {//unpack boot loader } else if (strcmp(strCmd.c_str(), "UNPACK") == 0) {//unpack boot loader
string strLoader = argv[2]; string strLoader = argv[2];
unpackBoot((char*)strLoader.c_str()); unpackBoot((char*)strLoader.c_str());
return true; return true;
} else if (strcmp(strCmd.c_str(), "TAGSPL") == 0) {//tag u-boot spl } else if (strcmp(strCmd.c_str(), "TAGSPL") == 0) {//tag u-boot spl
@ -2969,7 +3068,7 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan)
cnt = pScan->Search(RKUSB_MASKROM | RKUSB_LOADER); cnt = pScan->Search(RKUSB_MASKROM | RKUSB_LOADER);
if(strcmp(strCmd.c_str(), "LD") == 0) { if(strcmp(strCmd.c_str(), "LD") == 0) {
list_device(pScan); list_device(pScan);
return true; return (cnt>0)?true:false;
} }
if (cnt < 1) { if (cnt < 1) {
@ -3074,8 +3173,15 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan)
else { else {
if (is_sparse_image(argv[3])) if (is_sparse_image(argv[3]))
bSuccess = write_sparse_lba(dev, (u32)uiBegin, (u32)-1, argv[3]); bSuccess = write_sparse_lba(dev, (u32)uiBegin, (u32)-1, argv[3]);
else else {
bSuccess = true;
if (is_ubifs_image(argv[3]))
bSuccess = erase_ubi_block(dev, (u32)uiBegin, (u32)-1);
if (bSuccess)
bSuccess = write_lba(dev, (u32)uiBegin, argv[3]); bSuccess = write_lba(dev, (u32)uiBegin, argv[3]);
else
printf("Failure of Erase for writing ubi image!\r\n");
}
} }
} else } else
printf("Parameter of [WL] command is invalid, please check help!\r\n"); printf("Parameter of [WL] command is invalid, please check help!\r\n");
@ -3087,8 +3193,15 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan)
if (bRet) { if (bRet) {
if (is_sparse_image(argv[3])) if (is_sparse_image(argv[3]))
bSuccess = write_sparse_lba(dev, (u32)lba, (u32)(lba_end - lba + 1), argv[3]); bSuccess = write_sparse_lba(dev, (u32)lba, (u32)(lba_end - lba + 1), argv[3]);
else else {
bSuccess = write_lba(dev, (u32)lba, argv[3]); bSuccess = true;
if (is_ubifs_image(argv[3]))
bSuccess = erase_ubi_block(dev, (u32)lba, (u32)(lba_end - lba + 1));
if (bSuccess)
bSuccess = write_lba(dev, (u32)lba, argv[3]);
else
printf("Failure of Erase for writing ubi image!\r\n");
}
} else } else
printf("No found %s partition\r\n", argv[2]); printf("No found %s partition\r\n", argv[2]);
} else { } else {
@ -3098,8 +3211,15 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan)
if (bRet) { if (bRet) {
if (is_sparse_image(argv[3])) if (is_sparse_image(argv[3]))
bSuccess = write_sparse_lba(dev, part_offset, part_size, argv[3]); bSuccess = write_sparse_lba(dev, part_offset, part_size, argv[3]);
else else {
bSuccess = write_lba(dev, part_offset, argv[3]); bSuccess = true;
if (is_ubifs_image(argv[3]))
bSuccess = erase_ubi_block(dev, part_offset, part_size);
if (bSuccess)
bSuccess = write_lba(dev, part_offset, argv[3]);
else
printf("Failure of Erase for writing ubi image!\r\n");
}
} else } else
printf("No found %s partition\r\n", argv[2]); printf("No found %s partition\r\n", argv[2]);
} }