views:

550

answers:

3

Let's say I'd like to start a small linux distro before my ordinary operating system start.

  1. BIOS load MBR and execute MBR.
  2. MBR locates the active partition which is my linux partition.
  3. Linux start and I perform what I need to do.
  4. Linux shut down and I switch to Real Mode again.
  5. The original partition boot sector is loaded and my ordinary OS start.

AFAIK, step 4 will be the difficult task, restore the state on all devices prior to linux, will INT13h be functional? Do I need to restore the Interrupt Vector Table? To mention a few.

Has this been done in any existing project perhaps?

+1  A: 

Have you looked into kexec?

RandomNickName42
+1  A: 

Linux does not normally support this, particularly since it reinitializes hardware in a way that the BIOS and DOS programs may not expect. However, there is some infrastructure to switch back to real mode in specific cases - particularly, for a reboot (see machine_real_restart in arch/x86/kernel/reboot.c) - and has code to reinitialize hardware for kexec or suspend. I suspect you might be able to do something with a combination of these - but I don't know if the result will truly match what DOS or Windows would expect to see on reboot.

A much easier plan would be to use a chainloading bootloader that can be set to boot in a particular configuration once, like GRUB. You could invoke grub-set-default, then reboot. When GRUB comes up, it would then pass control off to Windows. By then setting the fallback OS to the Linux partition, control would return to Linux on the next boot.

Yet another option may be to use Coreboot, but I'm not sure if this is production-ready for booting windows yet.

bdonlan
+2  A: 

i haven't tried this so I don't know if it would work, but here goes:

There is an option in the header of a bzImage format kernel file that specifies the address of real mode code to execute before the protected mode code starts. You could create a minimal bzImage-compliant file which has no actual kernel, but which has real mode code to load your MBR using INT 0x13 to 0x7c00 and jmp into it like the BIOS does.

If you use kexec to load the bzImage using the "-t bzImage-x86 --real-mode" options, it should reset the PE bit in CR0 to drop to realmode (as bdonlan above mentioned) and execute the code pointed to by the bzImage header option.

The bzImage header option is called realmode_swtch and is documented in /usr/src/linux/Documentation/x86/boot.txt , the header format code is in /usr/src/linux/arch/x86/boot/header.S

matja