Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

booting from a disk/cd/usb

Tags:

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.

like image 617
sp3tsnaz Avatar asked Mar 03 '10 08:03

sp3tsnaz


People also ask

What is USB CD boot option?

The Boot Menu is the Windows' menu that lets you select the device you want to boot from: HDD, USB, CD-ROM etc. If the Boot Menu isn't available, you can force your computer to boot from an external and removable media (such as a USB flash drive, CD or DVD) by configuring your BIOS/UEFI settings.

Can you boot a computer from a CD?

On many computers, as the computer is booting, you may need to enter the boot menu (e.g., may see Press F9 for boot menu). If this option is available, press the key to enter the boot menu and then select the option to boot from the CD-ROM or DVD drive.


2 Answers

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
like image 138
Macmade Avatar answered Sep 28 '22 17:09

Macmade


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.

like image 20
SysAdmin Avatar answered Sep 28 '22 17:09

SysAdmin