OSDev.org

The Place to Start for Operating System Developers
It is currently Sun May 19, 2019 10:41 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 2:58 pm 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
I am new to things like OS dev, but I would like to ask you if there is any simple bootloader or tutorial how to load bin file (which contains custom OS).

I read lot of tutorials about bootloaders, but mostly they end with Hello World and doesn't say how to load some bin file.

I have kernel.bin which contains:

kernel.asm:

Code:
    bits    32
    section         .text
            align   4
            dd      0x1BADB002
            dd      0x00
            dd      - (0x1BADB002+0x00)
           
    global start
    extern kernel
    start:
            cli
            call kernel
            hlt


and kernel.c:

Code:
    kernel()
    {
            char* VideoMemory = (char*)0xb8000;
            VideoMemory[0] = 'M';
            VideoMemory[2] = 'y';
            VideoMemory[4] = ' ';
            VideoMemory[6] = 'O';
            VideoMemory[8] = 'S';
    }


It uses grub as bootloader and kernel.bin is saved in /boot/ directory, but I would like to create custom bootloader which loads that kernel.bin

I know that if I create custom bootloader I can use only first 510 bytes, because 511th and 512th byte contains magic number for booting.

I know that I have to use:

Code:
    times 510 - ($ - $$) db 0
    db 55h
    db 0AAh


But I don't know how to tell to that bootloader to load that kernel.bin


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 3:21 pm 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
I would strongly advice against writing your own boot loader when you have that little knowledge of the internal workings of operating systems and boot loaders.

If you insist, however, there are quite a few things a boot loader has to do. I'll be assuming you are using BIOS and not UEFI.
  • BIOS will load your boot sector into memory and execute it, which is the 512 bytes you mentioned.
  • These 512 bytes are hardly enough to do anything, so all your boot sector needs to do is load the second stage boot loader. This is your boot loader's main code. Normally, the boot sector will use INT 13h to read sectors from the disk. The format of the data on the disk is, of course, dependent on the file system you are using.
  • The second stage boot loader does further tasks that require BIOS (e.g. detecting memory, setting display modes, etc.) The second stage loader then loads the kernel into memory, using INT 13h just like the boot sector did. In short, the second stage boot loader contains a small read-only file system "driver."
  • The boot loader then jumps into the kernel's entry point, which it determines by parsing the kernel binary's executable format. It also passes some data to the kernel, which is in the format defined by the multiboot specification in your case, as you mentioned you were using GRUB.

Writing a functional boot loader from scratch is not as small as it may seem, especially since a lot of it is usually done in assembly language. Instead, get familiar with the basics and use GRUB for a while, and when you know enough about the internals of OSes and the PC, then you can write your own boot loader if you don't want to use GRUB.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.
[My OS]


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 3:28 pm 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
Thanks and is possible to create bootloader which uses only that first stage (first 510 bytes) to load that kernel.bin or it have to be two-stage (larger) bootloader?


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 3:40 pm 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
tomsk wrote:
Thanks and is possible to create bootloader which uses only that first stage (first 510 bytes) to load that kernel.bin or it have to be two-stage (larger) bootloader?

Try it yourself. You'll easily see 510 bytes isn't enough to store file system-specific data in the beginning (BPB for FAT, other similar structures for other file systems), and leave space for loading file from disk, detecting memory, setting a display mode, parsing the kernel's executable format, etc. Although it's technically possible to load a file and set up protected mode and run it, there is hardly enough space to do anything a boot loader actually needs to do.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.
[My OS]


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 3:50 pm 
Offline
Member
Member
User avatar

Joined: Tue Feb 11, 2014 4:59 pm
Posts: 53
@tomsk: Please use my bootloader: https://gitlab.com/akasei/Zero it's only 512 bytes long :)
Note that it runs the kernel in 64-bit mode, but it's well documented so you'll know how to run in a 32 bit environment.

_________________
wataha.net - system programming, my own 64 bit kernel and software.


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Sat Sep 22, 2018 4:28 pm 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
omarrx024 wrote:
tomsk wrote:
Thanks and is possible to create bootloader which uses only that first stage (first 510 bytes) to load that kernel.bin or it have to be two-stage (larger) bootloader?

Try it yourself. You'll easily see 510 bytes isn't enough to store file system-specific data in the beginning (BPB for FAT, other similar structures for other file systems), and leave space for loading file from disk, detecting memory, setting a display mode, parsing the kernel's executable format, etc. Although it's technically possible to load a file and set up protected mode and run it, there is hardly enough space to do anything a boot loader actually needs to do.


Thanks for info :)

akasei wrote:
@tomsk: Please use my bootloader: https://gitlab.com/akasei/Zero it's only 512 bytes long :)
Note that it runs the kernel in 64-bit mode, but it's well documented so you'll know how to run in a 32 bit environment.


Thanks I will try to learn from it :)


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Mon Sep 24, 2018 2:14 pm 
Offline
Member
Member

Joined: Wed Jun 17, 2015 9:40 am
Posts: 498
Location: Athens, Greece
Hi,


Since questions of similar nature as yours are somewhat common throughout the general osdev community, I made Bootloader FAQ. I'm all ears for suggestions. :-)


Regards,
glauxosdever


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Tue Sep 25, 2018 12:57 am 
Offline
Member
Member

Joined: Thu Jul 05, 2012 5:12 am
Posts: 923
Location: Finland
glauxosdever wrote:
I'm all ears for suggestions


You picked up on a topic that may end up being controversial. Although the FAQ is under your own username, new wiki pages should strive for neutrality. If you checked out the history of discussions about this topic on this forum, it would always have the same plot. Those who think that writing a bootloader (BIOS) is a bad idea use every opportunity to understate the importance of it, ridicule the museum hardware (e.g. floppies), or consider it to be a waste of time. Those how do think it is a good idea to write a bootloader have most probably done that and try their best to convince themselves that what they chose to do was not a bad decision. I did the latter so I am very biased when it comes to this topic. The best neutral argument for writing a bootloader (BIOS) that I ever can come up with is the genuine interest in the subject. Having fun is a subjective experience that cannot be disputed.

With this in mind, I still think the discussion about the topic might be useful because both parties would learn something new that helps them go forward on the chosen road.

Good that you started writing on the forum more actively! :)

_________________
Undefined behavior since 2012


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Tue Sep 25, 2018 4:30 am 
Offline
Member
Member

Joined: Wed Jun 17, 2015 9:40 am
Posts: 498
Location: Athens, Greece
Hi,


Quote:
You picked up on a topic that may end up being controversial. Although the FAQ is under your own username, new wiki pages should strive for neutrality. If you checked out the history of discussions about this topic on this forum, it would always have the same plot. Those who think that writing a bootloader (BIOS) is a bad idea use every opportunity to understate the importance of it, ridicule the museum hardware (e.g. floppies), or consider it to be a waste of time. Those how do think it is a good idea to write a bootloader have most probably done that and try their best to convince themselves that what they chose to do was not a bad decision. I did the latter so I am very biased when it comes to this topic. The best neutral argument for writing a bootloader (BIOS) that I ever can come up with is the genuine interest in the subject. Having fun is a subjective experience that cannot be disputed.
Indeed, just about everything osdev-related we do may turn out to be controversial. Even paging has been a controversial topic sometimes. The intent of a wiki page, especially a FAQ, is to provide a weighted overview and let the reader decide for themselves. The problem is that, since I'm still the sole author of that page, its views are obviously biased in favour of mine (e.g. don't bother with floppies, don't do multiboot in your bootloader, start minimal and then see what the kernel wants/needs and implement it, etc). That's why I asked for suggestions.

Although the relevant question (GRUB or custom bootloader?) hasn't been filled in yet, I'm in favour for doing a custom bootloader. I'll probably fill that section when I find some genuine arguments in favour of using GRUB (so it's weighted and not biased), unless I fill my part and someone else fills the other part. I think one suggestion I'd do is to use GRUB if the reader doesn't want to do better than GRUB (in the long run) or doesn't want to invest the necessary time to do better than GRUB (in the long run). Of course, that requires the reader to know about the shortcomings of GRUB, so maybe this suggestion could be improved.

As for BIOS vs UEFI, I don't have any experience with UEFI yet, apart from doing a "Hello, world!", so I can't talk about that.

Quote:
With this in mind, I still think the discussion about the topic might be useful because both parties would learn something new that helps them go forward on the chosen road.
While you are right, I don't think any experienced osdever that has made up his mind would read this FAQ, which is almost exclusively directed to osdevers who are just starting out and not sure how to do things or more experienced people who are willing to reconsider their stance, possibly by rewriting their OSes from scratch.

Quote:
Good that you started writing on the forum more actively! :)
Thanks! :-)

I'm trying to be more active with both the forums/wiki and my projects, whenever I have the needed motivation. Currently, I'm doing a compiler for a custom programming language (I repeat, whenever I have motivation :P), then I'll write the full OS in that language (hopefully). But maybe the ambitiousness of the project is putting me off? I don't really know. Some years ago, I was able to persist at writing a (bad quality) EHCI driver for three months; now I'm losing patience after only a couple of days. It's really crazy. I hope I can fix the bugs with myself before causing new bugs in my code. :P


Regards,
glauxosdever


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Tue Sep 25, 2018 2:22 pm 
Offline
Member
Member

Joined: Wed Jun 17, 2015 9:40 am
Posts: 498
Location: Athens, Greece
Hi,


I added an (obviously biased) section about "GRUB vs custom bootloader". Feel free to suggest improvements and, why not, edit the page if you want. I don't mind that, despite having it still in my namespace.


Regards,
glauxosdever


Top
 Profile  
 
 Post subject: Re: Simple Bootloader to load bin
PostPosted: Thu Oct 11, 2018 9:13 am 
Offline

Joined: Wed Oct 10, 2018 12:08 pm
Posts: 10
tomsk wrote:
But I don't know how to tell to that bootloader to load that kernel.bin


Greetings Tomsk.

When I first started to learn how all this works, I started out with this website.

http://www.brokenthorn.com/Resources/OSDevIndex.html

It teaches how boot loaders work and shows example code step by step. It shows the two first stages of loading and even how to load a kernel from the 2nd stage.

Another website I learned from is this one...

http://www.osdever.net/bkerndev/Docs/title.htm

And this shares pretty much the same thing, but from a different point of view.

I love both of those sites. A gold mine of information. But read both with an open mind, because on the more modern computers, neither of those work any longer. But they are still good for learning purposes to understand how things work. ( NOTE : They do work in VirtualBox )


SIDE NOTE : Before you even begin to think about making an OS, you MUST.. and I can't stress this enough, you MUST know your compilers. I use YASM, and sometimes NASM depending on the situation, as my assembler. And I use a Cross-Compiler ( MinGW x64 ) on windows to compile my C code. I followed the directions here on how to make a Cross-Compiler : https://wiki.osdev.org/GCC_Cross-Compiler

If you are on a windows machine, you only need Cygwin to make the cross compiler. After that, you can remove Cygwin. I never needed it after that.

Hope this helps.

Digg

_________________
TUTORIAL - Boot USB Drive using 64-Bit UEFI CLASS 3 - x86_64


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group