Multibooting real-mode kernel.

Programming, for all ages and all languages.
Post Reply
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Multibooting real-mode kernel.

Post by bilsch01 »

I want to build a real mode OS. I'm using Ubuntu and NASM. I'm using the real-mode example from the tutorials. I add the multiboot numbers at the beginning. I have a problem. Apparently I can't begin my real mode (NASM) program with a multiboot header - or maybe I can't multiboot a 16 bit program. Someone told me Grub switches into protected mode for multiboot compliant kernels. The code uses BIOS interrupts, so protected mode will be a problem.

QEMU (or grub) complains: error invalid arch-dependent ELF magic. I googled it. No help. If I remove the multiboot numbers at the beginning. It complains: no multiboot header.

I tried using NASM directive 'bits 32' for the first bytes which are the multiboot header (such as 0x1BADB002 ) and then use 'bits 16' for the rest of the code. That makes no difference.

File grub.cfg must have the following menu entry:
menuentry "myos" {
multiboot /boot/myos.kernel
}

File grub-mkrescue creates file myos.iso based on that menuentry

qemu-system-i386 -cdrom myos.iso says: invalid arch-dependent ELF magic. The only place magic is involved is in the multiboot header
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Multibooting real-mode kernel.

Post by BrightLight »

GRUB can only boot multiboot-compliant kernels, which are 32-bit or (someone correct me if I'm wrong) 64-bit kernels. There's no point making your kernel multiboot-compliant if it's 16-bit.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Multibooting real-mode kernel.

Post by bilsch01 »

omarrx024 wrote:GRUB can only boot multiboot-compliant kernels, which are 32-bit or (someone correct me if I'm wrong) 64-bit kernels. There's no point making your kernel multiboot-compliant if it's 16-bit.


It doesn't work if I remove the word 'multiboot' from:

menuentry "myos" {
multiboot /boot/myos.kernel
}

I have grub installed on the computer. There's no way to boot except using grub.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Multibooting real-mode kernel.

Post by iansjack »

Grub can chain load another bootloader. So you put your 16-bit bootloader in the bootsector of the partition containing your OS and then chain load it from Grub.

http://www.gnu.org/software/grub/manual ... m-directly
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Multibooting real-mode kernel.

Post by bilsch01 »

I notice that the meaty skeleton tutorial is set up to boot using QEMU and no physical boot device. That method relies on GRUB, which I cant use for 16-bit kernel. Also I notice I can boot my simple 16-bit kernel from a flashdrive with only a MSDOS MBR in sector 1, with my kernel beginning in sector 2. I don't need an image of a DOS bootable floppy or a harddrive image. So apparently the only extra software needed is a MSDOS MBR. I believe I should be able to use that in an ISO file along with my simple 16-bit kernel to boot using the linux -cdrom (software) device that is used by meaty skeleton via QEMU. I concatenated the kernel onto the MBR and made an ISO from that. But QEMU says no bootable device.

There must be a simple method using the following 5 components:

DOS MBR in sector #1 (or el torito equivalent)
16-bit kernel following the MBR
Xorriso ISO creating program
-cdrom (software) device used in meaty skeleton
QEMU emulator

I tried some ideas using the above, but always QEMU seems to be looking for a bootable device (hardwae or bootable image?).

Thanks. Bill S.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Multibooting real-mode kernel.

Post by BASICFreak »

NOTE Before Reading: I have not actually tried this, it is all theory to me.

GRUB does allow you to boot a 16-bit kernel, in fact that is how memtest86+ is loaded:

Code: Select all

menuentry 'Memory test (memtest86+)' {
	insmod part_msdos
	insmod ext2
	set root='hd3,msdos1'
	*CUT - ADVANCED SEARCH FOR UUID - CUT*
	linux16	/memtest86+.bin
}
Now, I have disassembled memtest86+.bin - it has no multiboot header and the first few lines are just segment fixes:

Code: Select all

00000000  B8C007            mov ax,0x7c0
00000003  8ED8              mov ds,ax
00000005  B80090            mov ax,0x9000
00000008  8EC0              mov es,ax
0000000A  B90001            mov cx,0x100
0000000D  29F6              sub si,si
0000000F  29FF              sub di,di
00000011  FC                cld
00000012  F3A5              rep movsw
00000014  EA19000090        jmp word 0x9000:0x19
So, you can boot 16-bit, but without multiboot. And it seems Grub will load your binary to 0x7C00? which memtest86+ relocates to 0x90000.

But my recommendation would be to either use 32-bit multiboot complaint kernel OR roll your own boot sectors / loaders.

One more choice you do have - if you require multiboot - is to make a 32-bit multiboot complaint kernel and drop back down to 16-bit after grub loads you in.



Best Regards,

B!
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: Multibooting real-mode kernel.

Post by Icee »

If all you want from GRUB is to load your file as a boot sector at 0x7C00 and transfer control to it in real mode, use the "chainloader" command.
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Multibooting real-mode kernel.

Post by bilsch01 »

OK. I got it. I made isolinux.cfg file that makes an ISO for my kernel then I use QEMU to boot the ISO - I just need to streamline it with a script. Thanks everybody for your input it is very instructive information. Bill S.
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Multibooting real-mode kernel.

Post by bilsch01 »

iansjack wrote:Grub can chain load another bootloader. So you put your 16-bit bootloader in the bootsector of the partition containing your OS and then chain load it from Grub.

http://www.gnu.org/software/grub/manual ... m-directly
I did a 16-bit bootloader and chainloading with grub. It works great. Thanks.

Bill S.
Post Reply