In wince, the method for applications to directly read, write, and erase flash devices (from: MVP Author: arm-wince)

Source: Internet
Author: User
Tags autoload builtin

Author: arm-WinCE

 

In many forums on the internet, I have seen a question: how can applications directly read and write flash sectors, or a similar question. In short, it is hoped that the application can directly access the flash device, directly read and write data in the sector, or perform other operations. I tried to introduce my methods to you.

 

First, let's give a brief introduction. Wince supports flash devices, generally nandflash or norflash. The FAL + FMD architecture is used. We implement FMD-related interface functions, and the Flash Driver is complete. After wince is started, we can see the disk of the flash device. We can operate on files on the disk, but cannot directly operate on flash devices. The operations on flash devices are nothing more than read, write, erase, and read ID.

 

Now we will introduce the implementation methods. It is unrealistic to directly call fmd_readsector (...), fmd_writesector (...), and fmd_eraseblock (...) in FMD in an application. Here, we will add that these three functions are the read sector, write sector, and erase block functions of Flash respectively. It seems awkward. However, we can call the fmd_oemiocontrol (...) function in the application. This can be done. So we need to change the flash device driver, that is, change the fmd_oemiocontrol (...) function in the flash device driver. My changes are as follows:

Bool fmd_oemiocontrol (DWORD dwiocontrolcode, pbyte pinbuf, DWORD ninbufsize, pbyte poutbuf, DWORD noutbufsize, pdword pbytesreturned)
{
Pfmdinterface pinterface = (pfmdinterface) poutbuf;

Retailmsg (1, (text ("fmd_oemiocontrol: control code is 0x % x/R/N"), dwiocontrolcode ));
Switch (dwiocontrolcode)
{
Case ioctl_fmd_get_interface:
If (! Poutbuf | noutbufsize <sizeof (fmdinterface ))
{
Debugmsg (1, (text ("fmd_oemiocontrol: ioctl_fmd_get_interface bad parameter (s)./R/N ")));
Return (false );
}
Pinterface-> cbsize = sizeof (fmdinterface );
Pinterface-> pinit = fmd_init;
Pinterface-> pdeinit = fmd_deinit;
Pinterface-> pgetinfo = fmd_getinfo;
Pinterface-> pgetinfoex = NULL; // fmd_getinfoex;
Pinterface-> pgetblockstatus = fmd_getblockstatus;
Pinterface-> psetblockstatus = fmd_setblockstatus;
Pinterface-> preadsector = fmd_readsector;
Pinterface-> pwritesector = fmd_writesector;
Pinterface-> peraseblock = fmd_eraseblock;
Pinterface-> ppowerup = fmd_powerup;
Pinterface-> ppowerdown = fmd_powerdown;
Pinterface-> pgetphyssectoraddr = NULL;
Pinterface-> poemiocontrol = fmd_oemiocontrol;
Break;
Case 0xff123456:
Fmd_readsector (...); // call the read sector function
Break;
Case 0xff654321:
Fmd_writesector (...); // call the write sector function
Break;
Case 0xff123457:
Fmd_eraseblock (...); // call the erased block function.
Break;
Default:
Debugmsg (1, (text ("fmd_oemiocontrol: Unrecognized IOCTL (0x % x)./R/N"), dwiocontrolcode ));
Return (false );
}

Return (true );
}

Three cases are added to the fmd_oemiocontrol (...) function, which calls the read/write/erase function. As for the value of case, I define it casually. In this way, the drive part of the flash device is changed.

 

After changing the Flash Driver, I will provide two methods below. Each method is related to the Registry configuration of the flash device:

 

1. Take nandflash as an example. Of course, it is similar to norflash. The Registry configuration is as follows:

[HKEY_LOCAL_MACHINE/Drivers/builtin/nandflash]
"DLL" = "ep94xxnandflash. dll"
"Prefix" = "DSK"
"Order" = DWORD: 4
; "IOCTL" = DWORD: 4
"Profile" = "nsflash"
"Iclass" = "{A4E7EDDA-E575-4252-9D6B-4195D48BB865 }"

; Override names in default profile
[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/nsflash]
"Name" = "ep94xx NAND Flash"
"Folder" = "nandflash"
"Partitiondriver" = "mspart. dll"
"Automount" = DWORD: 1
"Autopart" = DWORD: 1
"AutoFormat" = DWORD: 1

[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/nsflash/fatfs]
"Enablecache" = DWORD: 1
"Cachesize" = DWORD: 1000
"Mountbootable" = DWORD: 1
"Flags" = DWORD: 00000024
"Checkforformat" = DWORD: 1

Then write an application, which is to open dsk1 through createfile: device, and then use deviceiocontrol (..) function to call fmd_oemiocontrol (..) function to directly read, write, and erase flash devices. The application code is as follows:

Handle hfrem;

Hfrem = createfile (text ("dsk1:"), generic_read | generic_write, 0, null, open_existing, 0, null );
If (hfrem = invalid_handle_value)
{
Printf ("Open flash device failed ");
Return 0;
}

Iret = deviceiocontrol (hfsert, 0xff123456, para1, para2, para3, para4, para5, para6); // read flash sector
Iret = deviceiocontrol (hfsert, 0xff654321, para1, para2, para3, para4, para5, para6); // write flash sector
Iret = deviceiocontrol (hfrem, 0xff123457, para1, para2, para3, para4, para5, para6); // erase flash Block

Printf ("deviceiocontrol OK/R/N ");

While (1)
;

Through the above application, you can call the fmd_oemiocontrol (...) function in the flash device driver, so that you can call the read/write/erase function according to different cases.

 

2. Take nandflash as an example. Of course, it is similar to norflash. The Registry configuration is as follows:

[HKEY_LOCAL_MACHINE/Drivers/builtin/nandflash]
"DLL" = "ep94xxnandflash. dll"
"Prefix" = "DSK"
"Order" = DWORD: 4
; "IOCTL" = DWORD: 4
"Profile" = "nsflash"
"Iclass" = "{A4E7EDDA-E575-4252-9D6B-4195D48BB865 }"

; Override names in default profile
[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/nsflash]
"Name" = "ep94xx NAND Flash"
"Folder" = "nandflash"
"Partitiondriver" = "mspart. dll"
"Automount" = DWORD: 1
"Autopart" = DWORD: 1
"AutoFormat" = DWORD: 1

[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/nsflash/fatfs]
"Enablecache" = DWORD: 1
"Cachesize" = DWORD: 1000
"Mountbootable" = DWORD: 1
"Flags" = DWORD: 00000024
"Checkforformat" = DWORD: 1

[HKEY_LOCAL_MACHINE/system/storagemanager/autoload/nsflash]
"Driverpath" = "drivers // builtin // nandflash"
"Loadflags" = DWORD: 0
"Bootphase" = DWORD: 1

Then write the application, mainly through the openstore to open nsflash, and then through deviceiocontrol (..) function to call fmd_oemiocontrol (..) function to directly read, write, and erase flash devices. The application code is as follows:

Handle hfrem;

Hfrem = openstore (L "nsflash ");
If (hfrem = invalid_handle_value)
{
Printf ("Open flash device failed ");
Return 0;
}

Iret = deviceiocontrol (hfsert, 0xff123456, para1, para2, para3, para4, para5, para6 );
Iret = deviceiocontrol (hfsert, 0xff654321, para1, para2, para3, para4, para5, para6 );
Iret = deviceiocontrol (hfsert, 0xff123457, para1, para2, para3, para4, para5, para6 );

Printf ("deviceiocontrol OK/R/N ");

While (1)
;

In this way, you can also call the fmd_oemiocontrol (...) function in the application to directly access the flash device.

 

To sum up, the general principles of the above two methods are the same. They call the fmd_oemiocontrol function through the deviceiocontrol function, and then directly access the flash drive, in this way, you can directly read, write, and erase flash devices in the application.

 

Finally, you must note that the Flash Driver must protect functions that directly operate on flash hardware, such as read, write, and erase, because flash devices should be managed by the wince file system, and now your applications can directly access it, so to be safe, add mutex protection to avoid access conflicts.

All of the above implementations are done on wince6.0, and I believe it should be similar to wince5.0.

========================================== For the Microsoft MVP master Registry Modification Method, I am a bit puzzled. Now I have posted the original Registry

This is my original Registry
If bsp_nonandfs!
[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/smflash]
"Defaultfilesystem" = "binfs"
"Autopart" = DWORD: 1
"Automount" = DWORD: 1
"Partitiondriver" = "mspart. dll"
"Name" = "Microsoft flash disk"
"Folder" = "residentflash"
"Bootphase" = DWORD: 0

; Keep fatfs from trying to shadow/Windows
[HKEY_LOCAL_MACHINE/system/storagemanager/profiles/smflash/fatfs]
"Flags" = DWORD: 14
"Formattfat" = DWORD: 1
"Checkforformat" = DWORD: 1

[HKEY_LOCAL_MACHINE/Drivers/builtin/smflash]
"Prefix" = "DSK"
"DLL" = "smflash. dll"
"Index" = DWORD: 1
"Order" = DWORD: 0
"Profile" = "smflash"
"Iclass" = "{A4E7EDDA-E575-4252-9D6B-4195D48BB865 }"

[HKEY_LOCAL_MACHINE/system/storagemanager/fatfs]
"Flags" = DWORD: 00000014; fatfs_tfat_always | fatfs_force_tfat
"Mountflags" = DWORD: 0

[HKEY_LOCAL_MACHINE/system/storagemanager/autoload/smflash]
"Driverpath" = "drivers // builtin // smflash"
; Loadflags 0x01 = load synchronously
"Loadflags" = DWORD: 1
"Bootphase" = DWORD: 0
Endif; bsp_nonandfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.