No bootable device

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
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

No bootable device

Post 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
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: No bootable device

Post 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
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: No bootable device

Post 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
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: No bootable device

Post 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
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Re: No bootable device

Post 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.
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: No bootable device

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: No bootable device

Post 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.
Every good solution is obvious once you've found it.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: No bootable device

Post 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.
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: No bootable device

Post 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
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: No bootable device

Post by Griwes »

Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: No bootable device

Post 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...
Every good solution is obvious once you've found it.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: No bootable device

Post 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
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: No bootable device

Post 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.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: No bootable device

Post by iansjack »

But bear in mind that if you are working with 64-bit code you don't push the arguments.
Post Reply