One of our engineers was adding a third party driver to system today and had trouble getting it to work. When he installed from the CAB file that was provided, it worked correctly. When he built it into the OS, it wouldn’t work correctly (unfortunately I didn’t discuss it in enough detail to tell you what it was doing.) Just before the end of the day he called out to me and said he left the K flag out.
For those of us who read the Platform Builder newsgroup regularly, we know that this is a common mistake that started with Windows CE 6.0 (yes, the K flag was available prior to CE 6.0 but didn’t affect driver loading.) As Software Engineers, many of first look at the code, and don’t think about the REG and BIB files that also affect the loading of drivers.
What is this K flag that I am writing about? Before I get to that, maybe I should give a quick refresher on MODULES and FILES sections in BIB files. In Platform Builder: Using Bib Files to add files to a Windows CE OS Image I wrote:
FILES Section – the files in the FILES section are put into the OS image without modification. That certainly is a good thing for text files, bitmaps and data.
MODULES Section – file files in the MODULES section are “fixed up” when put into the OS image. That means that the file is modified by romimage so that the executable can be run eXecute In Place (XIP) from the ROM image.   There may be times when you don’t want it to be run XIP, like if you want to reduce the image size, but want the same DLL to be run from both Kernel space and User space.
My description of “fixed up” might have been a little vague. Since the K flag directly affects how the files are fixed up, let’s dig deeper. From your first computer or programming course, you may know that executable files targeting most modern operating systems are relocateable. That is the addresses are variables that are relocated when the OS loads the executable. But to save time and possibly space, when creating an OS image, romimage.exe fixes up the addresses of files included in the MODULES section of the BIB files. By default, the addresses are fixed up to user mode addresses.
Starting with Windows CE 6.0, drivers moved to being loaded into kernel mode address space by default. By default, means that unless you tell the system to load the driver into user mode space by setting the DEVFLAGS_LOAD_AS_USERPROC flag in the registry the driver will be loaded by the kernel device manager. But, the addresses are fixed up to user mode addresses by default.
Enter the K flag. The K flag tells romimage to fix up the addresses to kernel mode addresses. So since drivers are loaded into kernel space by default, the K flag must be used for drivers listed under the MODULES section of your BIB files. The following table may be used as a guide for drivers.

 
FILES Section
MODULES Section
K Flag
K Flag not needed because the files are not fixed up
K Flag is needed for Kernel Mode drivers
No K Flag
K Flag not needed because the files are not fixed up
K Flag is not needed for User Mode drivers
 
Note: the table does not apply to applications and DLLs that are to be loaded by applications.
Copyright © 2010 – Bruce Eitman
All Rights Reserved