views:

72

answers:

2

How can I boot my small console from a disk/cd/usb, with the following configuration:

  1. The media that I want to use will be completely raw i.e no filesystem on it.
  2. When I insert the media in my system or assume that its already inserted, I want to make it boot my own small OS.

The procedure that I want to use is that when my system starts, it boots from the disk/cd/usb, and starts my OS. For now imagine that the OS is going to be a hello world program. I actually want to see how the real world OS implement themselves.

+3  A: 

A bootloader must be 512 bytes. No less no more. And it must end with the standard PC boot signature: 0xAA55.

Also note a PC boots in 16 bits mode. You need to load your kernel or secondstage bootloader from that code into memory, and then jump to that code (and maybe switch the CPU to 32 bits protected mode).

For instance (nasm):

BITS 16

; Your assembly code here (510 bytes max)...
jmp $

; Fills the remaining space with 0
times 510 - ( $ - $$ ) db  0

; Boot signature
dw 0xAA55
Macmade
+1 good answer.
Tronic
i have studied and successfully cleared my course of computer organization and assembly language but i dont understand the above code.. its partial assembly language and partially something else .. can u please tell me what sort of assembly language is that ? also please recommend some books .
sp3tsnaz
plus i have another question .. i got the boot loader code and quite a good explanation from some other site .. but i dont understand how will the above instructions be executed directly without compilation ? also once i have successfully booted my small os , how can i add a compiler to it or some executables so that they can be run like an OS ? for example when windows boots it has some pre installed working programs which are definitely not just asm instructions .. they must be compiled or something ? how does that happen? please be descriptive .
sp3tsnaz
The above code can't be executed. It need to be «assembled» (kind of compilation, if you prefer). Assembly is very close to machine code. It just has words instead of op-codes. An assembler will replace those words with op-codes, creating an executable file.You can compile that code with NASM: nasm -f bin -o myOS myOS.asmNote the "-f bin", meaning nasm will generate a flat-form, raw binary, meaning it can be executed directly by the CPU (ELF, Mach-o or PE formats needs some processing).
Macmade
Now about having some other software. Those are just binary files, placed on a medium (floppy, CD, ...). To execute that kind of soft, you first need to know how to handle the filesystem (FAT, HFS, EXT, ...), so you can access the blocks representing the program you want to load. Then, you need to read those blocks into a memory location. If it's raw binary, you can then jump to that memory location, giving control to the program, that will execute. If it's not, you need to process the code in memory to find the program's entry point.
Macmade
Note that such a soft can be written in assembly, C, C++, etc... It doesn't matter. At the end, after compilation, it's just machine code, wrapped in a specific format or not...Note that a kernel is typically a software of that kind, which is loaded and executed by the bootloader. The kernel can then load and execute other software...Welcome to the world of machine code... : P
Macmade
well thankyou for this awesome description :D i studied some bootloader tutos, now i just dont know how can my bootloader give control to a linux kernel? can u give an example ? and for that i read that there should be proper file handling . what i mean to ask is that which filesystem should my usb be formatted into? and any other details that you think are important. Thankyou again =)
sp3tsnaz
+2  A: 

Hi, Thas the job of a boot loader. The bootloader should be present in the first 512 bytes of a HardDisk. This location is called MBR(Master boot record)

  1. When bios loads it checks if the media contains the MBR. it verifies the MBR signature 0xAA55 which should be present as the last 2 bytes of MBR.

  2. Then the Bios loads the BootLoader into RAM at address 0x7C00

  3. Then the boot loader is the one who actually loads the kernal into memory, by reading the filesystem.

  4. usually you cannot fit in all the code in 512 bytes, so there will be a secondory boot loader.

  5. secondary bootloader will be loaded by your primary bootloader.

  6. secondary boot loader loads IDT and GDT (Interupt vector table and Global descriptor table). Enables A20 gate to move into protected mode.

  7. secondary boot loader loads the 32 bit kernel from disk into memory, then jumps to the kernel code

For more information you can download Linux kernel v0.01 (First version). Look how it is done. to my surprise the code to read the File system + the code to move into protected mode is fit into 512 bytes of code.

SysAdmin