Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


O'Reilly Book Excerpts: Car PC Hacks

Hacking Your Car: Install Windows on a CompactFlash Card

by Damien Stolarz

Editor's note: This hack was written by Silvio Fiorito, who contributed it to O'Reilly's Car PC Hacks book.

Microsoft's XP Embedded OS can help you do what XP normally can't—boot off a CompactFlash card.

The car PC community is constantly searching for hardware and software solutions to improve the system's boot speed and reliability and reduce the physical size of the computer. One of these solutions is to build a system that boots off a CompactFlash (CF) drive. Flash storage is used in many commercial devices because it's small, has no moving parts, uses less power than a hard drive, and holds up much better than a hard drive when exposed to vibration and extreme temperatures.

Using only a CF-to-IDE adapter (Google "CF to IDE") and any off-the-shelf CF card (Figure 4-16), flash-based systems can be built relatively easily and affordably. One of the disadvantages of using flash, however, is that due to its construction it can withstand only a limited number of writes. This is fine for digital cameras, but since operating systems like to constantly thrash around on the hard drive with temporary files and virtual memory, flash requires special support by the operating system to filter out or completely disable these writes. Many users have turned to custom versions of Linux, while Windows users have had no viable option other than Windows CE or XP Embedded.

Figure 16
Figure -16. A CompactFlash-to-IDE adapter

For those willing to try, Microsoft provides a free trial of XP Embedded (XPe) from its web site that allows developers to build a fully functional boot image that will work for 90–120 days. It takes a few tries to get it working right, but it's a very powerful tool. After spending a good amount of time playing with it and reading as much as I could about it on Microsoft's MSDN site, I decided to investigate how XPe was able to work on flash disks. Since XPe is just a componentized version of XP, I figured there was no reason why a regular XP install couldn't be made to use some of the XPe components in order to boot from a CF disk.

Related Reading

Car PC Hacks
Tips & Tools for Geeking Your Ride
By Damien Stolarz

Microsoft provides a component called the Enhanced Write Filter (EWF) for developers planning to deploy their systems to a CF disk. EWF is simply a storage filter driver that can be configured to protect one or more volumes from any unwanted writes. All changes to a protected volume are filtered by EWF, which then stores them in RAM rather than writing them to the disk. Upon system shutdown or reboot, changes can be either committed to the volume or discarded. Since the volume is not written to during normal operation, the system is also more resistant to data corruption in case of a sudden power loss.

MinLogon is a component Microsoft added to XPe for devices that need quick boot times and as little overhead as possible. Normally, when an XP system is booted, an executable called WinLogon is started that performs the user login, sets the security policy settings, and runs the logon scripts. This can be a lengthy process and seriously hurt boot times. Car PC developers don't typically need this level of functionality in our systems—we just want the system to boot up as quickly as possible and start playing music like a normal radio would. MinLogon was created for just these types of devices, and coupled with EWF, you can use it to turn a regular XP install into an OS capable of quickly booting off a CF disk.

The first step in preparing a CF install is downloading the XPe trial from http://msdn.microsoft.com/embedded/windowsxpembedded/default.aspx. The install will create a network share on your system called Repositories. This is where all the XPe components are stored, and all the files I use in this hack come from this directory. The best way to experiment with this hack without corrupting your desktop system is to use machine virtualization software, such as VirtualPC (http://www.microsoft.com/windows/virtualpc/default.mspx) or VM-Ware (http://www.vmware.com). Set up your XP install and make sure you've got a way to transfer files between the virtual machine (VM) and the desktop. After I set up my system in VirtualPC, I created a second VM using a differencing drive of the first VM. This was so that if I screwed up I wouldn't have to go through the lengthy reinstall process all over again. (See the VirtualPC documentation for directions on how to set up a differencing drive.)

If you have any disk-cloning software, such as Symantec's Ghost (http://www.symantec.com/sabu/ghost/), you can use that too. Clone the drive you want to experiment with onto some old 1-GB drive you have lying around, and try changing that version so that you can re-clone it if you mess it up.

Setting Up MinLogon

First we'll set up MinLogon. This is an optional component—it is not necessary for running XP from a CF card, but it does improve boot time. Search the Repositories directory on your main hard disk for the latest version of minlogon.exe and transfer it to the test virtual machine or hard drive:

  1. First go to the Windows\System32 directory of your virtual machine and rename the file winlogon.exe to winlogon.exe.bak.

  2. Copy the minlogon.exe file to the Windows\System32 directory and rename it winlogon.exe.

  3. If a Windows File Protection warning comes up warning you about changing the filename, just cancel the dialog to ensure that the new MinLogon file isn't replaced by Windows File Protection.

  4. Next, create a text file called minlogon.reg. Edit the file and enter the following text:

    
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
    "Config"=dword:00000017
    
  5. Save the file and then merge it into the Registry by double-clicking on it. Double-check the Registry to make sure the entries were entered properly.

  6. Reboot the system.

As long as you entered everything properly, the VM will boot into XP using the System account. The first time you boot up it will prepare the user settings for the System account, so it'll take a bit longer than usual. Once that is done, go ahead and reboot again to make sure everything is working properly. MinLogon may cause problems for some applications, so if you find that it doesn't fit your needs, just restore the original winlogon.exe.

Setting Up Enhanced Write Filter (EWF)

Now that MinLogon is working properly, you can go ahead and set up EWF. Before you do so, make sure you disable the paging file by right-clicking on My Computer, clicking the Advanced tab, clicking the Performance button, clicking the Change button in the Virtual Memory section, and selecting "No paging file." You'll also want to disable system restores, by right-clicking on My Computer, selecting the System Restore tab, and checking "Turn off System Restore." (These features interfere with EWF.) One bug I've found is that when booting with EWF, XP always brings up the recovery options at bootup. You can disable this by deleting the file named bootstat.dat in the Windows directory. You'll need to search the Repositories directory again for three files: ewf.sys, ewfntldr, and ewfmgr.exe. Since the directories may change with each release, make sure you search for the latest versions and place them on the VM system.

  1. Rename the ntldr file on your root drive to ntldr.bak.

  2. Move the ewfntldr file to your root drive, and rename it ntldr.

  3. Move ewfmgr.exe to your Windows\System32 folder.

  4. Move ewf.sys to your Windows\System32\drivers folder.

  5. Create a text file called ewf.reg and enter the text from Example 4-1. (You can download these files at http://www.oreilly.com/catalog/carpchks.)

    Example 4-1. Registry entries to set up Enhanced Write Filter

    
      Windows Registry Editor Version 5.00
      [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Dfrg\BootOptimizeFunction]
      "Enable"="N"
      [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\OptimalLayout]
      "EnableAutoLayout"=dword:00000000
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
      "NtfsDisableLastAccessUpdate"=dword:00000001
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory
      Management\PrefetchParameters]
      "EnablePrefetcher"=dword:00000000
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager]
      BootExecute=""
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_EWF]
      "NextInstance"=dword:00000001
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_EWF\0000]
      "Service"="EWF"
      "Legacy"=dword:00000001
      "ConfigFlags"=dword:00000020
      "Class"="LegacyDriver"
      "ClassGUID"="{8ECC055D-047F-11D1-A537-0000F8753ED1}"
      "DeviceDesc"="EWF"
      "Capabilities"=dword:00000000
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_EWF\0000\Control]
      "ActiveService"="EWF"
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ewf]
      "ErrorControl"=dword:00000001
      "Group"="System Bus Extender"
      "Start"=dword:00000000
      "Type"=dword:00000001
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{71A27CDD-812A-11D0-
      BEC7-08002BE2092F}]
      "UpperFilters"="Ewf"
      [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ewf\Parameters\Protected\
      Volume0]
      "Type"=dword:00000001
      "ArcName"="multi(0)disk(0)rdisk(0)partition(1)"
    
  6. Save the file. Before you merge it, you need to alter the permissions on one Registry key. In regedit, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root. Right-click on Root and click Permissions. Set "Everyone" to have Full Control, and then merge the file by double-clicking on it. As you did for MinLogon, ensure that all the values were entered properly, and then reset the Root key permissions to the way they were before.

  7. Reboot the system.

Pay special attention to the last entry, ArcName. That points to the volume you want protected. This script will default to the first partition of the master drive on the primary IDE controller, so as long as you have your CF card set as the master drive on the primary IDE controller you'll be fine.

The first few entries are optimizations for EWF-enabled systems. To minimize disk writes, we've disabled automatic defragmentation and prefetch. I also included a tweak to disable the NTFS last-access file timestamp. If you use NTFS on your system, you don't want the OS constantly updating timestamps for files you access and creating unnecessary disk writes.

Once the system boots, pull up a command line and run ewfmgrn:, where n: is the letter of the protected drive (typically "c"). The output should be similar to this:


Protected Volume Configuration
 Type            RAM (REG)
 State           ENABLED
 Boot Command    NO_CMD
  Param1         0
  Param2         0
 Volume ID       87 0B 88 0B 00 7E 00 00 00 00 00 00 00 00 00 00
 Device Name     "\Device\HarddiskVolume1" [C:]
 Max Levels      1
 Clump Size      512
 Current Level   1
  Memory used for data 1294336 bytes
 Memory used for mapping 4096 bytes

If instead you get an error stating that no EWF volume could be found, pull up the Registry Editor and recheck your settings, make sure that ewf.sys is in the System32\drivers directory, unplug any other hard drives, and restart.

ewfmgr gives you some important information about your protected volume and tells you how much RAM your overlay is taking up. That's an important factor to keep in mind: the more changes you make to your protected volume, the more RAM it'll take up, until you finally run out of memory. So be careful what you do to your system with EWF running.

Here are two important commands to remember:


	C:\ ewfmgr c: -commitanddisable –live

This will immediately disable EWF and commit all changes to the volume.And this command will enable EWF on the next bootup:


	C:\ ewfmgr c: -enable

The typical process for making persistent changes to your volume is to run the commitanddisable command, make your changes, run the enable command, and restart.

Another useful feature for car PC systems is what Microsoft calls "Hibernate Once, Resume Many" (HORM). If you have hibernation support enabled in your system, this basically allows you to hibernate your system once, and then always resume from that same hibernation state every time you boot up. This minimizes writes to the CF card and improves boot and shutdown times. All it takes is a simple file called resmany.dat on the root of your drive. Just create an empty text file and name it resmany.dat. When this file is present on the root drive, the EWF NTLDR knows not to reset the hibernation file as it normally would; thus, you never have to re-hibernate, unless you specifically request it. If you decide you don't want to resume from hibernation, just hit F8 while the system is booting to delete the restoration data and boot up normally.

The hibernation process bypasses EWF, so there's no need to disable EWF before you hibernate. However, make sure to disable EWF when you create the resmany.dat file, or it won't be written to the flash drive. (You'll then need to re-enable EWF and reboot before hibernating.) Most likely, you'll have an external drive containing your MP3s. Microsoft recommends setting the hibernation point without any other hard drives plugged into the system, because if the write cache still has data in it when you hibernate, every time you resume that data will be in the write cache and could potentially corrupt the partition. XP will automatically detect any new drives that are attached to the system, so once you set the hibernation point you can leave your drives plugged in.

Setting Up Your XP Installation

Now that you have successfully created an EWF system in a VM, you can start planning your final system. My recommendation is to first make a customized XP install, using a program such as nLite (http://nuhi.msfn.org/nlite.html) to remove any excess features you don't need. You want to get your XP installation as small as possible so that it can fit onto a CF card. You'll probably have to choose between a 512-MB or 1-GB card. Do some research, and try to find the fastest CF card you can afford. Keep in mind that if you plan on using hibernation, your space requirements will increase by the amount of RAM you install in the system. In other words, if your XP installation takes up 320 MB and you have a 256-MB stick of RAM, you'll use up about 576 MB of space and will need to get a 1-GB card. However, if you don't need hibernation, you can make do with a 512-MB card and save yourself some money. You'll also need to buy a CF-to-IDE adapter. Do a search on Google, and you'll find quite a few different adapters out there. Some can even plug directly into the IDE port rather than using cables, which saves space.

The best way to go about putting your XP installation on a CF card is to first set up your system on a regular hard drive. Load up all your drivers and third-party tools, make the necessary configuration changes, and of course install EWF and MinLogon. Once you're happy with the system, you need to initialize your CF card. Microsoft recommends using a FAT filesystem to improve the performance of EWF and minimize writes to the drive. Also, many retail CF cards come configured as removable drives, and as Windows XP will not allow you to partition and format a removable drive with NTFS, you must use FAT for these cards (although you can sometimes get a utility from the manufacturer to configure the CF card to appear as a fixed, or non-removable, drive).

XPe includes a special tool called Bootprep.exe that is used to enable FAT-formatted disks to boot into Windows XP. To configure a FAT-formatted CF disk to boot XP, you'll need a DOS boot disk with fdisk.exe, format.com, and bootprep.exe. Here's what to do:

  1. Start by installing your CF card as the master drive on the primary controller and your hard drive on the secondary controller.

  2. Boot into DOS and partition your disk, and then run the command format c: /s. This command formats the disk, and the /s switch copies over the DOS system files to make the CF disk bootable. Reboot, and if all goes well the system will boot into DOS from the CF disk. If not, you need to check your BIOS settings.

  3. Once you've confirmed that your CF card boots successfully, boot with your DOS boot disk (not the CF card) and reformat the card by using the command format c: (without the /s switch). Then use the bootprep /dc command (the /d switch specifies which drive to use) to run Bootprep.

If you are using NTFS, simply use Windows Disk Management to partition the drive and format it.

Now you are ready to copy over your XP install. Since Windows won't allow copying of files from the currently running OS instance, you need to boot into another OS, or attach both the hard drive and the CF card to another machine. Use whatever method you prefer, whether it's booting into Knoppix, DOS, or another XP installation. Just make sure that whichever method you use, it copies all hidden and system files and keeps the file attributes (hidden, read-only, archived, and so on) intact.

Once the transfer is done, connect your CF card to your system, remove all other hard drives, and boot up. As long as all the files were copied over properly, the system will start booting into your XP install, just as it did from the hard drive. Once the system boots up, take a look around and make sure everything is working right. Bring up a console by running cmd from the Start menu, and check that EWF is running.

With your system now running on a CF card, you have a small, fast, and reliable installation of XP to use for your car PC or any other small, embedded device you plan to use. I suggest you experiment a little by using the standby power function, as well as taking a look through Microsoft's MSDN site for embedded technologies (http://msdn.microsoft.com/embedded/) to see what tips they may have.

If you're going to be making significant changes to the system, you may want to consider doing it on the hard drive first and then redeploying to the CF disk. That way, you can clean out log files, temp directories, and any other leftover junk that'll take up precious space on your CF disk.

--Silvio Fiorito

Damien Stolarz is an inventor who's made different kinds of computers talk to each other for a decade. He co-founded Blue Falcon Networks to architect and develop networking software. In 2002, Damien created Robotarmy, a high-technology consulting firm.


View catalog information for Car PC Hacks

Return to the Windows DevCenter.

Copyright © 2009 O'Reilly Media, Inc.