I've been following this:
( http://www.codeproject.com/KB/tips/boot-loader.aspx )
But not sure what and how to do next.
How to load self written kernel in it? Or how to make more place than in single segment?
And what to do with binaries? I have to copy bootloader to first sector, ok, but what with kernel, etc, just put on floppy/disc?

- 1,650
- 6
- 24
- 38
-
1+1 I don't agree with that downvote! – BlackBear Jan 24 '11 at 19:35
2 Answers
"How to load a kernel" comes down to knowing where the kernel is on disk and where you want it in memory, and then using BIOS disk services to read it. If you want the kernel to be loaded above 0x00100000 then you may need to load each part into a temporary buffer (that the BIOS can access in real mode), then use either protected mode or "unreal mode" to copy it from the buffer to where you actually want it. If you want to support compression, then you may need to load files then decompress them. If you want the kernel to use a complex file format (e.g. ELF or PE, rather than a simple flat binary) then you may need to parse headers, relocate sections, etc too.
My boot loaders are typically much larger than 1 sector. Code in the first sector loads the second sector, and code in the first and second sectors load the remainder of the boot loader. In this way the boot loader can be 20 KiB (for e.g.), as long as you're careful and don't try to use any code or data that hasn't been loaded yet. You could also have a second stage (and third, fourth, etc stages if you felt like it) where the boot loader loads the second stage, and the second stage loads the next piece, etc.
For where to store the binaries, this depends on what file system/s you're planning to use. If you don't want any file system (or if the file system you want to use has enough "reserved" space at the beginning), then you could just concatenate the binary files together and store them immediately after the boot loader. Otherwise, the boot loader (and/or additional stages) will need to find files in whichever file system you're using.
Note: Different boot loaders work differently. For something like booting from network, the boot loader can be 512 KiB and needs to download data from the network using the PXE API. For CD-ROM you'll probably end up using the ISO9660 file system (and 2 KiB sectors). For hard disks you'll need to handle partitions (and maybe have one boot loader for "MBR partitions" and another boot loader for "GPT partitions"). What you'll end up with is several completely different boot loaders, that all load the kernel (or maybe some sort of RAM disk image if it's a micro-kernel) and leave the computer in a certain state when starting the kernel (e.g. a specific CPU mode, the kernel at a specific address, other file/s at specific places, etc), such that the kernel itself needn't care which boot loader loaded it. For extra complexity, it's possible to include a lot more in this "predefined state" (e.g. address of ACPI tables, description of preconfigured video mode, etc) so that it's possible to write boot loaders for other types of systems and the kernel won't need to care if it booted from "PC BIOS" or UEFI or OpenFirmware or whatever else.

- 35,656
- 2
- 39
- 66
-
For now, I'd like just to make simpliest bootloader which load simpliest kernel. I find lot of tutorials, but they write only about "how to write bootloader/kernel", not how to load kernel. I was using hex editor, to paste my bootloader to floppy, so I have to make same with kernel, just select any segment I like, but it won't be able to be bigger than 512, will be? Also what's the difference between 16bit and 32bit mode? Is there any point at beggining to switch to 32 in kernel? And what changes when I switch to protected mode, and when I should do it? I'll appreciate any help. – Neomex Jan 25 '11 at 13:09
-
That's a lot of different questions. You probably should follow AJ's advice (including reading everything in the "basic information" section at http://wiki.osdev.org/Main_Page ); and also start reading Intel's or AMD's System Programming Guide (e.g. Volume 3A from here http://www.intel.com/products/processor/manuals/ ). – Brendan Jan 25 '11 at 17:16
-
Shouldn't need to use a hex editor - on most unix-like OSs you can use the "dd" utility (and even just copy files directly to "/dev/fd0" in some cases), and for Windows you can get tools to do it (e.g. rawwrite). If you can load one sector from disk then you should be able to figure out how to load more than one sector. There's lots of differences between real mode and protected mode (see Intel or AMD's manuals). – Brendan Jan 25 '11 at 17:17
-
I like to enable protected mode and paging before the kernel starts, but other people enable protected mode in the kernel - depends on your OS design. – Brendan Jan 25 '11 at 17:17
-
1Stackoverflow is great for quick questions and answers, but for discussions (like this) it sucks dog balls. For this reason I'd also recommend using the forums at http://forum.osdev.org/ instead, so people like me don't need to split their replies into 5 separate comments to get around stupid limitations. – Brendan Jan 25 '11 at 17:20
-
For now I havenent got activation mail from them ( 15 hours ), I'm beginner, I've just 'loaded' bootsector, but can't figure out myself how to load something bigger, where to jmp if i have to, and how to connect files (just put on floppy? how i'll know which sector is it? ), i'll move to osdev, but for now this is my only place where I can ask about it. – Neomex Jan 25 '11 at 19:17
Typically, the main thing your boot sector will need to do is load either a second-stage loader or the kernel in to memory. Assuming you are using a PC, you will use the BIOS disk read functions to load sectors. Doing this from a FAT formatted floppy is perfectly possible in the 512b you get for your boot sector. Alternatively, boot from a no-emulation El-Torito CD which gives you more space for your boot loader.
For further information, take a look at OSDev.org or Bona Fide OS Dev.

- 1,621
- 1
- 10
- 23