再次学习flash驱动——挖掘不能发现nand flash盘符的原因
2008-12-05 13:41
330 查看
作者:wogoyixikexie@gliet 2008-12-05
昨晚看以前优龙的老4.2BSP的flash驱动,发现和我现在不能发现这个盘符的驱动有些差别。下面这个函数是flash驱动加载的时候要执行的,作用是检测flash有没有坏块。我发现我在新的flash驱动中犯了严重的错误。等下会在代码中说明。
DWORD FMD_GetBlockStatus(BLOCK_ID blockID)
{
SECTOR_ADDR Sector = (blockID * NAND_PAGE_CNT);
SectorInfo SI;
DWORD dwResult = 0;
//我在新的flash驱动根本没有这句关键代码,这句的作用是把NK和bootloader
//占用的flash 的block标记为坏块,这样起到保护的作用
//其实这个并不完整,因为这样会检测超出最大block数目正确的做法是
//if(blockID<0x280||blockID>flash总block数目-1)//这样就不会越界了。
if(blockID<0x280)//
return BLOCK_STATUS_BAD;
if (IsBlockBad(blockID))
return BLOCK_STATUS_BAD;
if (!FMD_ReadSector(Sector, NULL, &SI, 1))
return BLOCK_STATUS_UNKNOWN;
if (!(SI.bOEMReserved & OEM_BLOCK_READONLY))
dwResult |= BLOCK_STATUS_READONLY;
if (!(SI.bOEMReserved & OEM_BLOCK_RESERVED))
dwResult |= BLOCK_STATUS_RESERVED;
return(dwResult);
}
我还发现一些注册表的问题。
这是4.2可发现盘符的BSP的flash注册表部分
[HKEY_LOCAL_MACHINE/System/StorageManager/Profiles/FlashDrv]
"DriverPath"="Drivers//BuiltIn//FlashDrv"
"DefaultFileSystem"="FATFS"//这在失败哪里是没有的,居然是binfs不可思议。
"PartitionDriver"="mspart.dll"
"AutoMount"=dword:1
"AutoPart"=dword:1
"AutoFormat"=dword:1//这个也没有,这个会对没有格式化过的flash自动格式化。
"Folder"="nand flash"
"Name"="Microsoft Flash Disk"
"BootPhase"=dword:0
"Flags"=dword:1000//这个有什么用?
"Ioctl"=dword:4//这个有什么用?
少了那几个东西,对于使用优龙的BIOS来说,是不能发现盘符的。
在这里重新回到优龙ADS bootloader的话题,我仔细看过,里面的确没有binfs以及FAT分区功能。
那经过上面注册表之后为什么就实现分区了呢?
——————查看CSDN老帖,有:
有几个问题求教,望高手们赐教:
1 用CreatePartition函数会自动生成MBR和分区表吗?
2 我用DeviceIoControl函数时,好象只能对某个分区进行读写。我想用CreateFile,第一个参数应该指定一个物理设备名,对于Nand Flash应该指定为什么字符串啊?
================================
是可以生成mbr,以及mbr里的partition table的。
参见C:/WINCE420/PRIVATE/WINCEOS/COREOS/STORAGE/DOSPART/part.cpp代码
============================================================================================
C:/WINCE500/PRIVATE/WINCEOS/COREOS/STORAGE/DOSPART/helper.cpp(747):BOOL WriteMBR(DriverState *state, SECTORNUM snSectorNum, SECTORNUM snNumSectors, BYTE fileSysType, BOOL bCreateMBR)
//重新创建MBR,这个不知道在哪里被调用了,但是可以解释优龙的问题。
/*****************************************************************************
* WriteMBR - adds a partition entry to the master boot record. The partition
* table is traversed to find the placement for this partition. Entries within
* the MBR are sorted by the start sector.
*
* Input: state - structure for this store
* snSectorNum - start sector number for this partition
* snNumSectors - number of sectors in this partition
* fileSysType - partition type
* bCreateMBR - TRUE to generate a new MBR, FALSE to add to the
* existing MBR
*
* Output: none
*
* Return: TRUE if successfully written, FALSE for failures, ERROR_DISK_FULL
* will be set if no room in the MBR
*
*****************************************************************************/
BOOL WriteMBR(DriverState *state, SECTORNUM snSectorNum, SECTORNUM snNumSectors, BYTE fileSysType, BOOL bCreateMBR)
{
PBYTE buffer = NULL;
PPARTENTRY tmpbuffer, tmpbuffer1;
BOOL bResult;
int i, partIndex, partNum = -1;
// don't create the MBR, it already exists so use it 这个要特别注意,检测是否已经分区,没有就重新写入MBR
if (!bCreateMBR)
{
bResult = ReadSectors (state, 0, 1, &buffer);
if (!bResult)
return FALSE;
for (i = 0, partIndex = 0, tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET); i < 4; i++, tmpbuffer++)
{
if (tmpbuffer->Part_TotalSectors == 0)
{
partNum = i;
break;
}
// find the index of the partition located just before this one
if (snSectorNum > tmpbuffer->Part_StartSector)
partIndex = i + 1;
}
if (partNum == -1)
{
// we return this error code so the caller can tell that there's no room in the MBR
LocalFree(buffer);
SetLastError(ERROR_DISK_FULL);
return FALSE;
}
// these indexes would be equal if we are adding to the end of the table
if (partIndex != partNum)
{
// this partition needs to be added in the order that it appears on the disk - so this may involve
// shifting some of the existing partition entries to open up the partition entry where this belongs
tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + (partNum * sizeof(PARTENTRY)));
tmpbuffer1 = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + ((partNum -1) * sizeof(PARTENTRY)));
for (; partNum > partIndex; partNum--)
{
memcpy(tmpbuffer, tmpbuffer1, sizeof(PARTENTRY));
tmpbuffer--;
tmpbuffer1--;
}
}
}
else
{
buffer = (PBYTE)LocalAlloc(LMEM_ZEROINIT, state->diskInfo.di_bytes_per_sect);
if (!buffer)
return FALSE;
// add header and trailer info to the MBR
*(WORD *)(buffer + BOOT_SIGNATURE) = BOOTSECTRAILSIGH;
buffer[0] = 0xE9;
buffer[1] = 0xfd;
buffer[2] = 0xff;
partNum = 0;
}
tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + partNum * (int)sizeof(PARTENTRY));
// create the partition entry for the extended partition
bResult = ConvertLBAtoCHS (state, snSectorNum, &tmpbuffer->Part_FirstTrack, &tmpbuffer->Part_FirstHead, &tmpbuffer->Part_FirstSector);
tmpbuffer->Part_FileSystem = fileSysType;
tmpbuffer->Part_StartSector = (DWORD)snSectorNum;
tmpbuffer->Part_TotalSectors = (DWORD)snNumSectors;
bResult = ConvertLBAtoCHS (state, tmpbuffer->Part_StartSector + tmpbuffer->Part_TotalSectors -1, &tmpbuffer->Part_LastTrack, &tmpbuffer->Part_LastHead, &tmpbuffer->Part_LastSector);
bResult = WriteSectors(state, 0, 1, buffer);
LocalFree(buffer);
return bResult;
}
如果eboot不格式化FAT分区,就要在flash驱动中把NK所占block标记为坏块。才行。
但是如何让系统自动格式化位binfs我就不知道怎么搞了。这个不行的。因为当时已经存在NK了,不能再格式化了。
哈哈,终于弄明白优龙为何不支持binfs了。
哈哈,现在如果在ADS增加binfs格式化那么就好办了。
转载请标明:作者wogoyixikexie@gliet.桂林电子科技大学一系科协。如有错误,希望能够留言指出。
昨晚看以前优龙的老4.2BSP的flash驱动,发现和我现在不能发现这个盘符的驱动有些差别。下面这个函数是flash驱动加载的时候要执行的,作用是检测flash有没有坏块。我发现我在新的flash驱动中犯了严重的错误。等下会在代码中说明。
DWORD FMD_GetBlockStatus(BLOCK_ID blockID)
{
SECTOR_ADDR Sector = (blockID * NAND_PAGE_CNT);
SectorInfo SI;
DWORD dwResult = 0;
//我在新的flash驱动根本没有这句关键代码,这句的作用是把NK和bootloader
//占用的flash 的block标记为坏块,这样起到保护的作用
//其实这个并不完整,因为这样会检测超出最大block数目正确的做法是
//if(blockID<0x280||blockID>flash总block数目-1)//这样就不会越界了。
if(blockID<0x280)//
return BLOCK_STATUS_BAD;
if (IsBlockBad(blockID))
return BLOCK_STATUS_BAD;
if (!FMD_ReadSector(Sector, NULL, &SI, 1))
return BLOCK_STATUS_UNKNOWN;
if (!(SI.bOEMReserved & OEM_BLOCK_READONLY))
dwResult |= BLOCK_STATUS_READONLY;
if (!(SI.bOEMReserved & OEM_BLOCK_RESERVED))
dwResult |= BLOCK_STATUS_RESERVED;
return(dwResult);
}
我还发现一些注册表的问题。
这是4.2可发现盘符的BSP的flash注册表部分
[HKEY_LOCAL_MACHINE/System/StorageManager/Profiles/FlashDrv]
"DriverPath"="Drivers//BuiltIn//FlashDrv"
"DefaultFileSystem"="FATFS"//这在失败哪里是没有的,居然是binfs不可思议。
"PartitionDriver"="mspart.dll"
"AutoMount"=dword:1
"AutoPart"=dword:1
"AutoFormat"=dword:1//这个也没有,这个会对没有格式化过的flash自动格式化。
"Folder"="nand flash"
"Name"="Microsoft Flash Disk"
"BootPhase"=dword:0
"Flags"=dword:1000//这个有什么用?
"Ioctl"=dword:4//这个有什么用?
少了那几个东西,对于使用优龙的BIOS来说,是不能发现盘符的。
在这里重新回到优龙ADS bootloader的话题,我仔细看过,里面的确没有binfs以及FAT分区功能。
那经过上面注册表之后为什么就实现分区了呢?
——————查看CSDN老帖,有:
有几个问题求教,望高手们赐教:
1 用CreatePartition函数会自动生成MBR和分区表吗?
2 我用DeviceIoControl函数时,好象只能对某个分区进行读写。我想用CreateFile,第一个参数应该指定一个物理设备名,对于Nand Flash应该指定为什么字符串啊?
================================
是可以生成mbr,以及mbr里的partition table的。
参见C:/WINCE420/PRIVATE/WINCEOS/COREOS/STORAGE/DOSPART/part.cpp代码
============================================================================================
C:/WINCE500/PRIVATE/WINCEOS/COREOS/STORAGE/DOSPART/helper.cpp(747):BOOL WriteMBR(DriverState *state, SECTORNUM snSectorNum, SECTORNUM snNumSectors, BYTE fileSysType, BOOL bCreateMBR)
//重新创建MBR,这个不知道在哪里被调用了,但是可以解释优龙的问题。
/*****************************************************************************
* WriteMBR - adds a partition entry to the master boot record. The partition
* table is traversed to find the placement for this partition. Entries within
* the MBR are sorted by the start sector.
*
* Input: state - structure for this store
* snSectorNum - start sector number for this partition
* snNumSectors - number of sectors in this partition
* fileSysType - partition type
* bCreateMBR - TRUE to generate a new MBR, FALSE to add to the
* existing MBR
*
* Output: none
*
* Return: TRUE if successfully written, FALSE for failures, ERROR_DISK_FULL
* will be set if no room in the MBR
*
*****************************************************************************/
BOOL WriteMBR(DriverState *state, SECTORNUM snSectorNum, SECTORNUM snNumSectors, BYTE fileSysType, BOOL bCreateMBR)
{
PBYTE buffer = NULL;
PPARTENTRY tmpbuffer, tmpbuffer1;
BOOL bResult;
int i, partIndex, partNum = -1;
// don't create the MBR, it already exists so use it 这个要特别注意,检测是否已经分区,没有就重新写入MBR
if (!bCreateMBR)
{
bResult = ReadSectors (state, 0, 1, &buffer);
if (!bResult)
return FALSE;
for (i = 0, partIndex = 0, tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET); i < 4; i++, tmpbuffer++)
{
if (tmpbuffer->Part_TotalSectors == 0)
{
partNum = i;
break;
}
// find the index of the partition located just before this one
if (snSectorNum > tmpbuffer->Part_StartSector)
partIndex = i + 1;
}
if (partNum == -1)
{
// we return this error code so the caller can tell that there's no room in the MBR
LocalFree(buffer);
SetLastError(ERROR_DISK_FULL);
return FALSE;
}
// these indexes would be equal if we are adding to the end of the table
if (partIndex != partNum)
{
// this partition needs to be added in the order that it appears on the disk - so this may involve
// shifting some of the existing partition entries to open up the partition entry where this belongs
tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + (partNum * sizeof(PARTENTRY)));
tmpbuffer1 = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + ((partNum -1) * sizeof(PARTENTRY)));
for (; partNum > partIndex; partNum--)
{
memcpy(tmpbuffer, tmpbuffer1, sizeof(PARTENTRY));
tmpbuffer--;
tmpbuffer1--;
}
}
}
else
{
buffer = (PBYTE)LocalAlloc(LMEM_ZEROINIT, state->diskInfo.di_bytes_per_sect);
if (!buffer)
return FALSE;
// add header and trailer info to the MBR
*(WORD *)(buffer + BOOT_SIGNATURE) = BOOTSECTRAILSIGH;
buffer[0] = 0xE9;
buffer[1] = 0xfd;
buffer[2] = 0xff;
partNum = 0;
}
tmpbuffer = (PPARTENTRY)(buffer + PARTTABLE_OFFSET + partNum * (int)sizeof(PARTENTRY));
// create the partition entry for the extended partition
bResult = ConvertLBAtoCHS (state, snSectorNum, &tmpbuffer->Part_FirstTrack, &tmpbuffer->Part_FirstHead, &tmpbuffer->Part_FirstSector);
tmpbuffer->Part_FileSystem = fileSysType;
tmpbuffer->Part_StartSector = (DWORD)snSectorNum;
tmpbuffer->Part_TotalSectors = (DWORD)snNumSectors;
bResult = ConvertLBAtoCHS (state, tmpbuffer->Part_StartSector + tmpbuffer->Part_TotalSectors -1, &tmpbuffer->Part_LastTrack, &tmpbuffer->Part_LastHead, &tmpbuffer->Part_LastSector);
bResult = WriteSectors(state, 0, 1, buffer);
LocalFree(buffer);
return bResult;
}
如果eboot不格式化FAT分区,就要在flash驱动中把NK所占block标记为坏块。才行。
但是如何让系统自动格式化位binfs我就不知道怎么搞了。这个不行的。因为当时已经存在NK了,不能再格式化了。
哈哈,终于弄明白优龙为何不支持binfs了。
哈哈,现在如果在ADS增加binfs格式化那么就好办了。
转载请标明:作者wogoyixikexie@gliet.桂林电子科技大学一系科协。如有错误,希望能够留言指出。
相关文章推荐
- 再次学习flash驱动——挖掘不能发现nand flash盘符的原因
- 经过几天的挖掘,我发现,学习阶段,一定一定要低调,谦虚,不能发生矛盾。
- NAND FLASH学习笔记之MTD下nand flash驱动(二)————几个重要文件和几个重要的结构体
- 利用asio实现了一个服务器,多个客户端连接,并异常断开连接,发现后面再也连接不上服务器了,不能建立新连接了。原因分析
- 通过大学学习后发现毕业后不能去的几类单位
- 用eclipse做的一个短信发送器,编译通过但是部署到模拟器上面出错.后来经过多方学习发现是新的sdk原因
- NAND FLASH学习笔记之MTD下nand flash驱动(七)---我的调试
- mfc对话框程序在ide编译可运行,但编译完毕的debug和release版本不能运行。。后来发现原因是:用到的sqlite数据库文件没在对应目录下,而代码中没有try..catch..
- 最近在搞SPI Flash的驱动,有一个问题迟迟不能解决
- DBDesigner 4 与 MySql 5 不能连接主要是驱动的原因
- 发现内存不能read和written的原因以及解决方法
- asp.net mvc页面为何不能显示Flash(.swf)动画的原因
- WPF-MVVM模式学习笔记3——MVVM概念再次挖掘
- Android新编译的内核驱动模块不能被init加载原因解决
- 关于LF2407捕获不能再次中断的原因及解决方法
- 当发现不能读取页面上输入的数据时可能是什么原因造成的,怎么解决?.
- Maven学习 (四) 使用Nexus搭建Maven私服 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找
- NAND FLASH学习笔记之MTD下nand flash驱动(二)
- NAND FLASH学习笔记之MTD下nand flash驱动(四)