Page 1 of 1

No bootable device

Posted: Sun Jun 10, 2012 11:52 pm
by rhughes
Hello

I have followed the Bare Bones tutorial and thought it would be nice to create a simple bootloader without grub myself. I have looked around various pages on the wiki here and on the net (living in China though you would be suprised to know how many programming related pages 'don't exist').

I have obvioulsy missed something really simple. Could you please let me know where I went wrong?

boot.asm

Code: Select all

[BITS 16]

global loader

extern kmain

section .text

loader:

    cli

    mov ax, 0x7C00
    mov ds, ax

    sti

    call kmain

    times 510-($-$$) db 0
    db 0xAA
    db 0x55
kernel.c

Code: Select all

extern void kmain(void)
{

}
linker script

Code: Select all

ENTRY (loader)

SECTIONS
{
    . = 0x00100000;

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

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

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

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
Makefile

Code: Select all

CC	=i586-elf-gcc
CFLAGS	=-Werror -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
LD	=i586-elf-ld

Debug: kernel.img

loader.o: boot.asm
	nasm -f elf -o loader.o boot.asm

kernel.o: kernel.c
	$(CC) $(CFLAGS) -o kernel.o -c kernel.c

kernel.bin: kernel.o loader.o
	$(LD) -T linker.ld -o kernel.bin loader.o kernel.o

kernel.img: kernel.bin
	#dd if=/dev/zero of=pad bs=1 count=750
	cat kernel.bin > kernel.img

cleanDebug:
	rm -f loader.o kernel.o kernel.bin kernel.img

install:
	rm -f loader.o kernel.o kernel.bin
Thank you very much,

Richard Hughes

Re: No bootable device

Posted: Sun Jun 10, 2012 11:56 pm
by Nessphoro
Come on, really?

You obviously lack the needed knowledge to even build an OS with GRUB.

First of all, gcc does not generate 16 bit code, and the BIOS doesn't boot ELF either.

Please start over: Introduction

Re: No bootable device

Posted: Mon Jun 11, 2012 12:11 am
by rhughes
I managed to get build and run OK with GRUB, but I guess that is because it can load the ELF format.

I am new, and there is a whole lot to learn!

So if I was to load the kernel like this, it would need to be a "flat kernel" is it called? Basically a kernel with no headers etc...? Or to manually parse the ELF format myself.

Thanks

Re: No bootable device

Posted: Mon Jun 11, 2012 12:20 am
by rhughes
I have just managed to get it running now by changing the way nasm was assembling the files and removing kernel.c

Thank you for your help. Sorry, I didn't realize about the ELF format until you mentioned it in your post.

Thank you very much,

Richard Hughes

Re: No bootable device

Posted: Mon Jun 11, 2012 6:36 am
by Ready4Dis
The bigger issue is, you need to learn how the boot process works if you intend on making a boot loader. The bios only loads the first 512 bytes from disk, which is your ASM file. The kernel.c is not loaded automatically, that is the job of the boot loader. Also, you never tell the .asm file where to start (it's ORG, or origin should be 0x7c00). There is a lot more to starting an operating system from disk than simply calling a function.

Re: No bootable device

Posted: Mon Jun 11, 2012 7:01 am
by rhughes
Ready4Dis wrote:The kernel.c is not loaded automatically, that is the job of the boot loader.
So I guess from this that I would need to jump to a predefined location which is set at compile time. Depending on my kernel file format, I would need to parse it appropriatly and run.
Ready4Dis wrote:Also, you never tell the .asm file where to start (it's ORG, or origin should be 0x7c00).
I did try [ORG 0x7C00] but got a compile time error. So I changed it to:

mov ax 0x7C00
mov ds ax

That is the same thing, right?

Thanks,

Richard

Re: No bootable device

Posted: Mon Jun 11, 2012 7:13 am
by Solar
Ready4Dis wrote:The kernel.c is not loaded automatically, that is the job of the boot loader.
Nitpick: Neither kernel.c (C source) nor kernel.o (object file) are loaded; what the bootloader loads is usually a flat binary generated by the linker from the kernel.o.

@rhughes:

Check out Boot Sequence in the Wiki, or the Babystep tutorial, which go into some detail of the early boot environment.

Re: No bootable device

Posted: Mon Jun 11, 2012 7:16 am
by turdus
rhughes wrote:I did try [ORG 0x7C00] but got a compile time error. So I changed it to:

mov ax 0x7C00
mov ds ax

That is the same thing, right?
No, they're not. One is a compile time directive, the other is a runtime code.
Solar is right, you should read Babysteps.

Re: No bootable device

Posted: Mon Jun 11, 2012 7:30 am
by rhughes
Thanks.

I've read them and they make sense. I understand how to make an asm bootloader which prints to the screen, sets up the stack etc...

What I am trying to figure out, is how to call into a C method. If I create a flat asm bootloader using something like:

nasm boot.asm -f bin -o boot.o

I can't have any external references. For this I have read that I need to assemble into the ELF format. The problem with this, is that the ELF format cannot be loaded directly as a stage 1 boot loader.

So I suppose, my real problem is not knowing how to go from stage 1 to stage 2 - or perhaps I am again missing something.

Thanks,

Richard

Re: No bootable device

Posted: Mon Jun 11, 2012 7:34 am
by Griwes

Re: No bootable device

Posted: Mon Jun 11, 2012 7:35 am
by Solar
rhughes wrote:So I suppose, my real problem is not knowing how to go from stage 1 to stage 2 - or perhaps I am again missing something.
The general idea is to have stage 1 load stage 2 into memory, and then jmp'ing to it...

Re: No bootable device

Posted: Mon Jun 11, 2012 8:33 am
by egos
rhughes wrote:I have obvioulsy missed something really simple. Could you please let me know where I went wrong?
Try this:

Code: Select all

    db 0x55
    db 0xAA

Re: No bootable device

Posted: Mon Jun 11, 2012 11:14 am
by Jezze
Calling a c function and reading a elf binary is not the same thing. You can create a flat binary c program if you wish, have it loaded into memory and then just junp to the first address as you would a function. To call a function in c what you do is push the arguments in reverse order an then issue the call opcode.

Re: No bootable device

Posted: Mon Jun 11, 2012 11:42 am
by iansjack
But bear in mind that if you are working with 64-bit code you don't push the arguments.