OSDev.org
https://forum.osdev.org/

"Hello world" OS runs on Bochs/Virtual but not on bare metal
https://forum.osdev.org/viewtopic.php?f=1&t=56104
Page 1 of 1

Author:  amiga1200guy [ Wed Jan 26, 2022 12:55 pm ]
Post subject:  "Hello world" OS runs on Bochs/Virtual but not on bare metal

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:
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:
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:
ENTRY(loader)

SECTIONS {
    . = 0x00100000;

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

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

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

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


Makefile:
Code:
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:
make run


dd command I used to write USB:
Code:
sudo dd if=os.iso of=/dev/sdc bs=512b status=progress


Thanks in advance for any help :)

Author:  klange [ Wed Jan 26, 2022 7:33 pm ]
Post subject:  Re: "Hello world" OS runs on Bochs/Virtual but not on bare m

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.

Author:  amiga1200guy [ Thu Jan 27, 2022 12:50 pm ]
Post subject:  Re: "Hello world" OS runs on Bochs/Virtual but not on bare m

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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/