Booting OS with GRUB

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
vt
Posts: 1
Joined: Mon Nov 04, 2019 9:36 am

Booting OS with GRUB

Post by vt »

We are developing 32-bit OS. I used to run it like this:

Code: Select all

qemu-system-i386 -kernel os.bin
. Today I wanted to make ISO image:

Code: Select all

grub-mkrescue -o bootable.iso iso
When I start

Code: Select all

qemu-system-i386 -cdrom bootable.iso
it shows grub. Next, I press enter to boot OS. The OS started up, but in less than a second it returns to GRUB.

Grub config:

Code: Select all

menuentry "OS" {
multiboot /os.bin
}
linker.ld:

Code: Select all

ENTRY(_start)


SECTIONS
{
	. = 1M;
	kernel_phys_start = .;

	.text BLOCK(4K) : ALIGN(4K)
	{
		*(.multiboot)
		*(.text)
	}

	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
	}
	kernel_phys_end = .;
}
boot.s:

Code: Select all

.set ALIGN,    1<<0             # align loaded modules on page boundaries
.set MEMINFO,  1<<1             # provide memory map
.set FLAGS,    ALIGN | MEMINFO  # this is the Multiboot 'flag' field
.set MAGIC,    0x1BADB002       # 'magic number' lets bootloader find the header
.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot

# Declare a header as in the Multiboot Standard.
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

# Reserve a stack for the initial thread.
.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

# The kernel entry point.
.section .text
.global _start
.type _start, @function
_start:
	movl $stack_top, %esp

	# Call the global constructors.
	#call _init

	# Transfer control to the main kernel.
	call init

	# Hang if kernel_main unexpectedly returns.
	cli
1:	hlt
	jmp 1b
.size _start, . - _start
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Booting OS with GRUB

Post by Ethin »

If your description is accurate, then your OS is probably triple faulting. Once grub has loaded your kernel it is impossible for the system to "return" to it without a processor reset being initiated. Ensure you've set up a double fault handler to catch any errors that are not caught by any other IDT entries -- that should solve your problem.
Post Reply