"Hello world" OS runs on Bochs/Virtual but not on bare metal

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
amiga1200guy
Posts: 2
Joined: Wed Jan 26, 2022 12:36 pm

"Hello world" OS runs on Bochs/Virtual but not on bare metal

Post by amiga1200guy »

Hello , first post :)

I've been following "The little book of OS development", trying to create a simple OS that says "Hello". It runs on Bochs and VirtualBox but when I write it to a USB stick using dd, and try to boot from it, it does not boot.

It's using the "stage2_eltorito" grub (0.97)

My guess is there is probably some random data somewhere that's not present or set to zero on the emulators. But really I've no idea.

Hope somebody could help!

loader.s:

Code: Select all

global loader

MAGIC_NUMBER      equ 0x1BADB002
FLAGS             equ 0x0
CHECKSUM          equ -MAGIC_NUMBER
KERNEL_STACK_SIZE equ 4096
section .bss
align 4
kernel_stack:
	resb KERNEL_STACK_SIZE

section .text
extern kmain
align 4
	dd MAGIC_NUMBER
	dd FLAGS
	dd CHECKSUM

loader:
	mov esp, kernel_stack + KERNEL_STACK_SIZE
        call kmain
.loop:
	jmp .loop
kmain.c:

Code: Select all

unsigned short* framebuf_ptr = (unsigned short*) 0xb8000;

void print(const char* str) {
    char* p = (char*) str;
 
    while (*p != '\0') {
        *(framebuf_ptr++) = (unsigned short)0x0f00 | (unsigned short)*p++; 
    }
}

void kmain()
{
    print("hello");
}
link.ld:

Code: Select all

ENTRY(loader)

SECTIONS {
    . = 0x00100000;

    .text ALIGN (0x1000) :
    {
        *(.text)
    }

    .rodata ALIGN (0x1000) :
    {
        *(.rodata*)
    }

    .data ALIGN (0x1000) :
    {
	*(.data)
    }

    .bss ALIGN (0x1000) :
    {
        *(COMMON)
	*(.bss)
    }
}
Makefile:

Code: Select all

OBJECTS = loader.o kmain.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
         -nostartfiles -nodefaultlibs -O0 -Wall -Wextra -Werror -c

LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf

all: kernel.elf

kernel.elf: $(OBJECTS)
	ld $(LDFLAGS) $(OBJECTS) -o kernel.elf

os.iso: kernel.elf
	cp kernel.elf iso/boot/kernel.elf
	genisoimage -R \
	-b boot/grub/stage2_eltorito \
	-no-emul-boot \
	-boot-load-size 4 \
	-input-charset utf8 \
	-quiet \
	-boot-info-table \
	-o os.iso \
	iso

run: os.iso
	bochs -f bochsrc.txt -q

%.o: %.c
	$(CC) $(CFLAGS) $< -o $@

%.o: %.s
	$(AS) $(ASFLAGS) $< -o $@

clean:
	rm -rf *.o kernel.elf os.iso
To build & run in bochs:

Code: Select all

make run
dd command I used to write USB:

Code: Select all

sudo dd if=os.iso of=/dev/sdc bs=512b status=progress
Thanks in advance for any help :)
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: "Hello world" OS runs on Bochs/Virtual but not on bare m

Post by klange »

Grub 0.97 is fifteen years old, you should use a recent release of Grub 2.

You are manually creating an ISO with only an El Torito boot record. Your computer almost definitely does not support booting such an image from a USB stick. If you upgrade to Grub 2, you can use grub-mkrescue to make a "hybrid boot" image that has an MBR boot sector.
amiga1200guy
Posts: 2
Joined: Wed Jan 26, 2022 12:36 pm

Re: "Hello world" OS runs on Bochs/Virtual but not on bare m

Post by amiga1200guy »

klange wrote:Grub 0.97 is fifteen years old, you should use a recent release of Grub 2.

You are manually creating an ISO with only an El Torito boot record. Your computer almost definitely does not support booting such an image from a USB stick. If you upgrade to Grub 2, you can use grub-mkrescue to make a "hybrid boot" image that has an MBR boot sector.
It worked perfectly! I used grub-mkrescue and made an entry for my ELF kernel and it ran first time.

Virtualbox and BM

TY
Post Reply