Wednesday, October 14, 2009

A Shortcut to Build your image!!!! [WinCE]

Hi,

Many times we see a problem of this dll is missing. What we do. We just find out the module compile it again and get dll and then build rel then make image. But think a scenario, when you have to include a dll which you can get only after sysgen the project. Looks cumbersome but there is a solution for it. The solution will give just a temporary solution so don't blindly rely on it.
Simply open ce.bib, then comment the particular dll by using semicolon(;).
Then run the command by opening the command window from menu bar(I guess name is like release to with msdos option. )
romimage                                                                   /location of ce.bib/ce.bib

This will give output file. Also you always need to modify ce,bib whenever you wan to build an image.

Why we should not use platform builder to include or exclude a module!!! [WinCE]

Many time we wish to remove or add a module in project. We usually follow two technique.
1. Edit the dirs file
2. Right click the module in platform builder and select exclude from build.

Both works fine.
But in few scenario, second option can cause problem.
Suppose you have two folder lets say A and B.
After building A, we get A.lib. And similarly after building B, we get B.dll but this B.dll uses A.lib to build successfully. Now if you want to remove B, so you would like to remove A and B, both. So you might use the sequence, First A then B or B first then A.
So in the same way, actually module get deleted from dirs file also. So once you plan to include, the module will be added at the end of dirs file. Hence once you choosed B first and then A. In that case, either the b.dll will not be build successfully or it will be build with old A.lib, hence your changes in A.Lib will not be reflected(if you are not going to build A.lib again).

Hence  it is wisely suggested that always edit dirs directly.

Tuesday, October 13, 2009

How VirtualAlloc works!!! [WinCE]

VirturlAlloc internally call SC_VirtualAlloc.

As you know that this api take parameters like the desired address of area assigned and size of file and flags related to action on the region given and access type on the region.

Now I will explain only the use case in which it will give the desired result not the else part.

;)

It start with checking the desired address whether it is inside kernel address and also check for access type and action on region whether it's supported or not.

Check if the request region is under secure VA. If not then calculate the base address of the region assigned just by left shifting the address asked for. Now check the address that it's come under first mapper add or not if yes then check for the process to check the remotely memory is accessed by the current process.

Now check for the allocate type.

Now check whether the request is for slot 0. If yes then take the 64k booundary. and check for the free region.

To check for NULL block.


If the request is not for Slot 0 then use filesystem to reserve the region.

Check the condition like requested region is more than 32MB then find the continuous memory.

* Still Not Completed

Saturday, October 10, 2009

How mounting works with FAT Filesystem [WinCE]

Mounting means attaching a disk to a filesystem.
When mount option get selected in the storage manager, these are following operations performed:

  1. It asked for device information. It will check against read only and set the variable as if after set, it will be read-only disk.

  1. Now it will get device info and will set FAT FS for the device flag variable.

  1. Find out the total number of sectors.

  1. There is read and write operation performed on the disk for which we allocate a buffer. It’s an interesting operation. There is two term FAT Cache and Data Cache comes into picture. FAT Cache is cache of actual sector which comprises the FAT table. Necessity of FAT Cache is that it allow to access very large chunk of files hence we have to traverse in the table of 2^17 for 32k clusters. So with increasing the FAT cache size will resolve the traversing so long. Data Cache is to keeping the cache of file names with a directory. Like to create a new file in a directory will lead to check all file name and then only we can use the new file name.
Using the registry you can increase or decrease

[HKEY_LOCAL_MACHINE\System\StorageManager\FATFS]
            "FatCacheSize"=0xXXXXX  - Size of the FAT Table Cache
            "DataCacheSize"=0xXXXX - Size of the Data Cache

It also checks for cache ID. If it does not exist it also means that the area if for boot and it’s cache disable so there will be normal read and write operation will happen but if it is under cache ID the read or write operation will only perform on the cache. No direct access will be on the sector.

It also finished the reading and writing of disk.

  1. Now it will find the volume. It also means that it will check for the same volume name and will add the new disk name to the disk link list structure.
  2. It will now mount the volume with the filesystem. In turn will perform the following operation:
    1. Open the volume – It first tries to find the volume. If it does not exist then will create one. Then initialize the volume. This operation makes sure that all registry entry parameter is added with volume. Also the volume structure contains all information from size of volume to cluster related parameter. The cluster related value will contain take care of root directory to nested one.
    2. Refresh the volume – This operation required as it can have many open handle. We do even if we have invalid volume.
    3. Format volume if it unformatted or invalid or read only or other invalid conditions – This will format the volume again if required.
    4. Register the volume with filesystem – It will give a name to the volume.
    5. Close the volume – Close the volume handle. This operation comes in picture only if we can’t allocate enough buffers or there is any problem in registering with the file system.

Note: In all these operation, we are using HeapAlloc not VirtualAlloc. The reason is that it will allocate the size of memory asked for not in 4k chuck or what ever like VirtualAlloc.

Friday, October 9, 2009

How KernelRelocate works? [WinCE]

KernelRelocate is one of the most interesting function I found in WinCE. Frankly, I am feeling happy to share the details and how exactly it works.

The pTOC variable is in nk.exe which is feed by ROMIMAGE

This function copies all the copy entries described by the pTOC to RAM. The process of changing an EXE or DLL program file after it has been loaded to reflect the actual load address is called “fixing up”. The variable pTOC actually have information about all dll and exe and where it has to be relocated. It know where RAM starts as ROMIMAGE use config.bib to feed the information. The information is feed by ROMIMAGE tool.

There is basic requirement of the relocation that when nk.exe call any of the dll or exe. It actually calls at RAM location to execution function instead of where it was copied by steploader. Also a steploader just copied data bit by bit although that is RAM. But NK.exe always access different address.

This is actually what is happening. pTOC variable have every information about RAM and where the dll and exe is lie in.


  1. //
  2. // KernelRelocate: move global variables to RAM
  3. //
  4. static BOOL KernelRelocate (ROMHDR *const pTOC)
  5. {
  6. ULONG loop;
  7. COPYentry *cptr;
  8. if (pTOC == (ROMHDR *const) -1) {
  9. return FALSE; // spin forever!
  10. }
  11. // This is where the data sections become valid... don't read globals until after this
  12. for (loop = 0; loop <>ulCopyEntries; loop++) {
  13. cptr = (COPYentry *)(pTOC->ulCopyOffset + loop*sizeof(COPYentry));
  14. if (cptr->ulCopyLen)
  15. memcpy((LPVOID)cptr->ulDest,(LPVOID)cptr->ulSource,cptr->ulCopyLen);
  16. if (cptr->ulCopyLen != cptr->ulDestLen)
  17. memset((LPVOID)(cptr->ulDest+cptr->ulCopyLen),0,cptr->ulDestLen-cptr->ulCopyLen);
  18. }
  19. return TRUE;
  20. }