OSDev.org

The Place to Start for Operating System Developers
It is currently Mon May 23, 2022 3:26 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: "Hello world" OS runs on Bochs/Virtual but not on bare metal
PostPosted: Wed Jan 26, 2022 12:55 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: "Hello world" OS runs on Bochs/Virtual but not on bare m
PostPosted: Wed Jan 26, 2022 7:33 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 578
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.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: "Hello world" OS runs on Bochs/Virtual but not on bare m
PostPosted: Thu Jan 27, 2022 12:50 pm 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group