Dual Boot from One SD Card

From OLPC
Revision as of 07:57, 13 October 2010 by Wmb@firmworks.com (talk | contribs) (Added details.)
Jump to navigation Jump to search

XO laptops have been able to dual-boot Windows and Linux for some time, by putting a specially-configured version of Windows XP on a 4GB SD card and inserting it into the XO's external SD slot. If that card is present in the external slot, the XO boots Windows, otherwise it boots Linux from the internal device (raw NAND on XO-1 or microSD on XO-1.5).

There is now a need for dual-booting from a single 8GB microSD card in the XO-1.5's internal slot. This page describes the resulting technical issues and their solutions.

The Partition Problem

The key new problem is that Windows will not accept multiple partitions on removable media. Windows allows there to be an FDISK partition map on an SD card, but that partition map must specify exactly one partition, otherwise Windows will not boot. I am not sure of the rationale for that restriction, but I am quite convinced that the restriction exists, both from third-party reports and also from testing that I have performed myself.

The obvious way to divide the single 8GB microSD card into separate sections for Windows and Linux would be to use a separate partition for each, but the obvious way of doing that fails because of Window's single-partition restriction.

The solution is for OFW to dynamically rewrite the partition map when switching between Linux and Windows. When booting Windows, OFW installs a partition map that shows:

  • Partition 1: start: 64M size: 4G-64M (i.e. the first half of the card, containing Windows)
  • Partitions 2-4: unused

When booting Linux, OFW installs a partition map that shows:

  • Partition 1: start: 4G size: 64M (Linux boot partition)
  • Partition 2: start: 4G+64M size: 4G-64M (Linux system partition)
  • Partition 3: start: 64M size: 4G-64M (Windows)
  • Partition 4: unused

That way, each OS sees what it is expecting. Windows sees only the Windows stuff, and does not see any extra partitions that could cause it to fail. Linux sees the expected boot and system as partitions 1 and 2, and additionally can be given access to the Windows filesystem if we decide to (Linux can handle NTFS).

We can keep backup copies of both partition maps in the 64M area between the MBR and the Windows partition area.

The "4G" size shown above is not really exactly 4G, but will have to be adjusted down to account for the actual SD size, which is a bit less than a full 8G.

OFW will only rewrite the partition map when it has to. In the usual case where someone reboots the same OS that was last used, a rewrite will not be necessary.

Details

Windows uses the 15 sectors immediately after the MBR sector to store code for a simple NTFS reader. The code in the MBR reads in those other 15 sectors. Therefore, it's necessary to ensure that those sectors contain correct data when the MBR is set for Windows.

The way we do that is to rewrite not just the partition map sector alone, but instead the first 16 sectors. That probably works well with the large page sizes prevalent in current NAND FLASH devices.

The layout is:

  • MBR at sector 0 (byte offsets 0-0x1ff)
  • Additional switched sectors from sector 1 - 15 (byte offsets 0x200 - 0x1fff)
  • Saved sectors for OS 0 (Windows) from sectors 0x400 - 0x40f (byte offsets 0x80000-0x81fff)
  • Saved sectors for OS 1 (Linux) from sectors 0x410 - 0x41f (byte offsets 0x82000-0x83fff)
  • Secure Boot Area (see "Security" below) starting at sector 0x800 (byte offset 0x100000)

The reason for spreading things out so much is in hopes of putting the various critical items in different NAND FLASH erase blocks, thus reducing the probability of failure due to erase disturbs.

User Interface

If the XO is booted without user intervention (other than pressing the power button), OFW will boot the OS that was most recently booted. To boot the other OS, the user must press the rotate game key after pressing the power button. That brings up a graphical menu that lets you choose which OS to boot. After you boot the other OS, it becomes the "most recently booted" one and will subsequently boot without intervention.

The icons that depict the OSs are the Linux penguin ("Tux") and the Windows XP logo.

Security

If it is necessary to sign the Windows boot files, it can be done using the scheme described at http://wiki.laptop.org/go/Firmware_security_for_Windows .

The need for backup copies of the partition map causes the starting address of the "secure boot area" to change from 0x200 (sector 1) to 0x100000 (sector 0x800).