How to use QEMU properly with multi boot headers
Asked Answered
T

3

5

I am learning the basic of OS making. I have made a multi boot header compliant .asm file and a .c file. The code in the .asm file calls the main function of .c file.

The problem is that QEMU is unable to boot from the file produced after the compilation and linking of the .asm and the .c file .

It simply says that it can't find a bootable device.

Although, I am able to boot from a simple .asm file like :-

  mov ax, 0x0e
  mov al, 'H' 
  int 10h 
  times 510 - ($ - $$) db 0 
  jmp $ 
  dw 0xaa55 

Is there something more which I have to do?

Thereinafter answered 24/8, 2014 at 7:15 Comment(4)
How did you create the boot image? Also, post the .asm file.Brahmin
Also, how are you running the emulator (i.e. command line options).Wampum
Well you aren't using multiboot headers in this code. But one glaring problem is that jmp $ is in the wrong place. It should be before the times statement. As it is the JMP pushed the boot signature of 0xaa55 outside the first 512 bytes which will cause QEMU to not identify it as a boot sector.Treasurer
mov ax, 0x0e is wrong, it should mov to ah.Downhearted
W
0

QEMU doesn't have native support for multiboot. Instead, you'll need to create a virtual hard drive image and install some sort of multiboot boot loader (such as grub), then put your multiboot image somewhere on the drive (i.e. in a file on a partition).

As far as actually installing grub onto a virtual HDD, there's multiple ways to do it, but here's the process I always use:

  1. Use qemu-img or dd if=/dev/zero to create your HDD image.
  2. Download a Linux installer ISO (I typically use Arch Linux).
  3. Boot qemu with the blank HDD image and ISO using -hda <HDD-image-filename> -cdrom <ISO-file-name> -boot once=d. The last bit ensures qemu will try to boot from CD first.
  4. Use fdisk/parted/etc to format the disk.
  5. Mount your boot partition (the one you want to install grub to) and use grub-install.
  6. Unmount and shut down the emulator.

Then, you'll be able to boot off the HDD image and use grub or whatever loader you choose to boot your multiboot image.


The reason your simple ASM example works is because you're effectively emulating the MBR, the first sector of a typical hard drive, so QEMU's BIOS will boot from it (specifically, it sees that 0xaa55 signature).

Wampum answered 24/8, 2014 at 14:48 Comment(2)
QEMU does have support for multiboot. The -kernel option allows you to specify an ELF executable that is multiboot compliant and it will boot from it. It doesn't support multiboot2Treasurer
qemu also only supports elf32 binaries, not elf64. Many versions of binutils' ld will crash while attempting to create hybrid elf32 executables containing x86_64 code. Also, I've not had much luck using objcopy (maybe I'm holding it wrong) to transplant an elf64 to elf32 as the resulting executable is corrupt. Modern grub, elf64 and multiboot2 work fine, albeit slower development cycle than using qemu -kernel with multiboot1 and elf32.Mesoderm
N
11

QEMU 2.0.0 does support multiboot

man qemu says:

-kernel bzImage

Use bzImage as kernel image. The kernel can be either a Linux kernel or in multiboot format.

I have uploaded a minimal hello world example at: https://github.com/cirosantilli/x86-bare-metal-examples/tree/dbbed23e4753320aff59bed7d252fb98ef57832f/multiboot

It generates a GAS + C multiboot file, and uses QEMU to run it.

grub-mkrescue can also convert a multiboot binary to a bootable .iso image which is another good approach.

Barry mentions that multiboot2 is not supported. How to generate a multiboot2 image in case you want to test it: How to compile the simple kernel in multiboot2 Spec corrently?

Nelia answered 13/9, 2015 at 13:44 Comment(3)
QEMU currently can boot only to a multiboot1 header. multiboot2 is not yet supported.Mesoderm
@Barry thanks for the info. If you have a link to a feature request or source comment, also post it in.Nelia
There's no link. READ the QEMU mailing-lists, source and try it for yourself. Multiboot2 and elf64 support have been consistently rejected for many years because the QEMU maintainers are recalcitrant to get with this millennium and actually LISTEN to users because "reasons." If you absolutely need mutliboot2 or elf64 support, use grub on an iso under QEMU. Or if you really want those features, create patches that add them.Mesoderm
W
0

QEMU doesn't have native support for multiboot. Instead, you'll need to create a virtual hard drive image and install some sort of multiboot boot loader (such as grub), then put your multiboot image somewhere on the drive (i.e. in a file on a partition).

As far as actually installing grub onto a virtual HDD, there's multiple ways to do it, but here's the process I always use:

  1. Use qemu-img or dd if=/dev/zero to create your HDD image.
  2. Download a Linux installer ISO (I typically use Arch Linux).
  3. Boot qemu with the blank HDD image and ISO using -hda <HDD-image-filename> -cdrom <ISO-file-name> -boot once=d. The last bit ensures qemu will try to boot from CD first.
  4. Use fdisk/parted/etc to format the disk.
  5. Mount your boot partition (the one you want to install grub to) and use grub-install.
  6. Unmount and shut down the emulator.

Then, you'll be able to boot off the HDD image and use grub or whatever loader you choose to boot your multiboot image.


The reason your simple ASM example works is because you're effectively emulating the MBR, the first sector of a typical hard drive, so QEMU's BIOS will boot from it (specifically, it sees that 0xaa55 signature).

Wampum answered 24/8, 2014 at 14:48 Comment(2)
QEMU does have support for multiboot. The -kernel option allows you to specify an ELF executable that is multiboot compliant and it will boot from it. It doesn't support multiboot2Treasurer
qemu also only supports elf32 binaries, not elf64. Many versions of binutils' ld will crash while attempting to create hybrid elf32 executables containing x86_64 code. Also, I've not had much luck using objcopy (maybe I'm holding it wrong) to transplant an elf64 to elf32 as the resulting executable is corrupt. Modern grub, elf64 and multiboot2 work fine, albeit slower development cycle than using qemu -kernel with multiboot1 and elf32.Mesoderm
W
-1

No, QEMU does have native support for the old multiboot spec, although it does not support VBE, ex.. Just compile from a freestanding compiler with the correct legacy multiboot header into an ELF executable and run with the -kernel option

Whencesoever answered 22/2, 2015 at 7:41 Comment(1)
Needs a minimal working example. GNUmakefile, start.s and kernel.cMesoderm

© 2022 - 2024 — McMap. All rights reserved.