MINI2440从SD卡更新NK及nboot(一)
买了友善 MINI2440 开发板, SuperVivi 不开源,自己的板子又没有 NOR Flash ,烧入系统带来很大的困扰,无奈之下,只能自己做烧入工具来解决。大致思路,先从 SD 卡启动 WINCE ,再用工具格式化 Flash 及分区,将 Nboot 及 NK 写入相应的块。
应用程序操作 Flash ,无非就是 Read/Write/Earse Flash , CSDN 上有一牛人写 在 WinCE 下,应用程序直接读 / 写 / 擦除 flash 设备的方法 。
下面是我的部分代码:
代码
1 BOOL FMD_OEMIoControl(DWORD dwIoControlCode, PBYTE pInBuf, DWORD nInBufSize,PBYTE pOutBuf, DWORD nOutBufSize, PDWORD pBytesReturned)
2 {
3 ……
4 switch(dwIoControlCode)
5 {
6 ……
7 //参数自己定义
8 case 0xFF11:
9 return FMD_ReadSector(nInBufSize,pInBuf,NULL,nOutBufSize); //读Sector
10 break;
11 case 0xFF22:
12 return FMD_WriteSector(nInBufSize,pInBuf,NULL,nOutBufSize); //写Sector
13 break;
14 case 0xFF33:
15 return FMD_EraseBlock(nInBufSize); /擦除Block
16 break;
17 }
18 }
2 {
3 ……
4 switch(dwIoControlCode)
5 {
6 ……
7 //参数自己定义
8 case 0xFF11:
9 return FMD_ReadSector(nInBufSize,pInBuf,NULL,nOutBufSize); //读Sector
10 break;
11 case 0xFF22:
12 return FMD_WriteSector(nInBufSize,pInBuf,NULL,nOutBufSize); //写Sector
13 break;
14 case 0xFF33:
15 return FMD_EraseBlock(nInBufSize); /擦除Block
16 break;
17 }
18 }
修改注册表,以便应用程序调用驱动
代码
1 [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\NANDFLASH]
2 "Prefix"="DSK"
3 "Dll"="nandflash.dll"
4 "Index"=dword:1
5 "Order"=dword:0
6 "Profile"="NANDFLASH"
7 "IClass"="{A4E7EDDA-E575-4252-9D6B-4195D48BB865}"
8
9 [HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\NANDFLASH]
10 ;"DefaultFileSystem"="BINFS"
11 "AutoPart"=dword:1
12 "AutoMount"=dword:1
13 "AutoFormat"=dword:1
14 "PartitionDriver"="mspart.dll"
15 "Name"="Microsoft Flash Disk"
16 "Folder"="ResidentFlash"
17 [HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\NANDFLASH\FATFS]
18 "Flags"=dword:14
19 "FormatExfat"=dword:1
20 "CheckForFormat"=dword:1
21 "EnableWriteBack"=dword:1
2 "Prefix"="DSK"
3 "Dll"="nandflash.dll"
4 "Index"=dword:1
5 "Order"=dword:0
6 "Profile"="NANDFLASH"
7 "IClass"="{A4E7EDDA-E575-4252-9D6B-4195D48BB865}"
8
9 [HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\NANDFLASH]
10 ;"DefaultFileSystem"="BINFS"
11 "AutoPart"=dword:1
12 "AutoMount"=dword:1
13 "AutoFormat"=dword:1
14 "PartitionDriver"="mspart.dll"
15 "Name"="Microsoft Flash Disk"
16 "Folder"="ResidentFlash"
17 [HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\NANDFLASH\FATFS]
18 "Flags"=dword:14
19 "FormatExfat"=dword:1
20 "CheckForFormat"=dword:1
21 "EnableWriteBack"=dword:1
应用程序调用驱动打开设备
hFirm = CreateFile (TEXT ("DSK1:" ), GENERIC_READ | GENERIC_WRITE , 0, NULL , OPEN_EXISTING , 0, NULL );
再通过DeviceIoControl 操作Flash
更新NBOOT ,NBOOT 写在第0 块,用CreateFile 及ReadFile 将NBOOT.bin 读取至数组中,再写入Flash
关键代码:
代码
1 DWORD ReadSize = 4*1024;//NBOOT最大4K
2 DWORD actlen = 0;
3 PCHAR pBuff=(char *)malloc(ReadSize);
4 memset(pBuff,0xFF,ReadSize);
5 iRet = ReadFile(hFile, pBuff, ReadSize, &actlen, NULL);
6 if (iRet == FALSE)
7 {
8 RETAILMSG(1,(TEXT("Read nboot File Failed.\n\r")));
9 return;
10 }
11 //先Erase,再Write
12 iRet = EraseBlock(hFirm,0);//Erase 第0块
13 if (!iRet)
14 {
15 RETAILMSG(1,(TEXT("Ease Erase 0 Failed.\n\r")));
16 }
17 //从第0块第0页开始写,每次写一页,我用的Flash每页为512bytes
18 for (int i=0; i < ReadSize/512; i++)
19 {
20 iRet = WriteSector(hFirm,i,&pBuff[i*512]);
21 if (iRet == FALSE)
22 {
23 RETAILMSG(1,(TEXT("Write nboot File Failed.\n\r")));
24 return;
25 }
26 }
27
2 DWORD actlen = 0;
3 PCHAR pBuff=(char *)malloc(ReadSize);
4 memset(pBuff,0xFF,ReadSize);
5 iRet = ReadFile(hFile, pBuff, ReadSize, &actlen, NULL);
6 if (iRet == FALSE)
7 {
8 RETAILMSG(1,(TEXT("Read nboot File Failed.\n\r")));
9 return;
10 }
11 //先Erase,再Write
12 iRet = EraseBlock(hFirm,0);//Erase 第0块
13 if (!iRet)
14 {
15 RETAILMSG(1,(TEXT("Ease Erase 0 Failed.\n\r")));
16 }
17 //从第0块第0页开始写,每次写一页,我用的Flash每页为512bytes
18 for (int i=0; i < ReadSize/512; i++)
19 {
20 iRet = WriteSector(hFirm,i,&pBuff[i*512]);
21 if (iRet == FALSE)
22 {
23 RETAILMSG(1,(TEXT("Write nboot File Failed.\n\r")));
24 return;
25 }
26 }
27
----------------------------------------------------------
尊重他人劳动成果,转载请标明出处http://www.cnblogs.com/feishanm