File:  [NetBSD Developer Wiki] / wikisrc / how_netbsd_boots_on_x86.mdwn
Revision 1.3: download - view: text, annotated - select for diffs
Mon May 27 09:21:34 2013 UTC (10 months, 3 weeks ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by apb: "No active partition" can be fixed with fdisk(8)

**Contents**

[[!toc]]

##  Stage 0: Master Boot Record

  * MBR source: `sys/arch/i386/stand/mbr/mbr.S`
  * MBR binaries: `/usr/mdec/mbr_*`
  * Error messages: 

    * "Error x" 
    * "No active partition" - use fdisk(8) to mark one of the MBR partitions as "active"
    * "Disk read error" 
    * "No operating system" - PBR magic number was wrong 
    * "Invalid CHS read" - CHS vs LBA check selected LBA reads, but BIOS does not support them 

MBR is read by the BIOS and then validated (checked for magic number 0xaa55 at
the end). If validation fails, BIOS may print evil error message and/or try
next boot device; otherwise, BIOS passes control to MBR.

Unless it was compiled with `NO_BANNER`, MBR displays its banner: "NetBSD MBR
Boot". Next, it checks if CHS or LBA addressing must be used in further BIOS
I/O calls. Since partition table entry stores both linear and CHS address,
linear address is computed from CHS address, and if it matches stored linear
address - MBR will use CHS addressing. LBA addressing will be tried otherwise,
even if it is not supported by BIOS.

PBR is then loaded from 1st sector of active partition (or, if boot selector
was compiled into this MBR, it may be loaded from another partition or disk).

##  Stage 1: Partition Boot Record, bootxx

  * PBR source: `sys/arch/i386/stand/bootxx/pbr.S`
  * bootxx source: `sys/arch/i386/stand/bootxx/bootxx.S`
  * combined PBR+bootxx binaries: `/usr/mdec/bootxx_*`
  * Error messages: 

    * '2' or "Disk read" 
    * 'B' or "Not a bootxx image" - phase 1 bootstrap's magic number was wrong 
    * 'P' or "No NetBSD partition" 
    * 'L' or "Invalid CHS read" - see above 

PBR loads phase 1 bootstrap (bootxx) from next 15 sectors on disk, validates
and runs it.

bootxx displays its banner: `NetBSD/(machine) (filesystem) Primary Bootstrap`
and tries to load phase 2 bootstrap (boot2) from several locations:

  * start of MBR partition; or 
  * start of MBR partition + RAIDframe offset; or 
  * start of partition 'a' in disklabel. 

  * bootxx error messages: 

    * "Boot failed (errno <...>): <...>" 
    * All further messages are printed by boot2. For example, a single "0" and no other messages on PC console means that boot2 has autodetected and is using serial console. 

##  Stage 2

  * boot2 source: `sys/arch/i386/stand/boot/`
  * boot2 binary: `/boot`

##  Glossary

BIOS

     Basic Input-Output System 
MBR

     Main Boot Record, stored at LBA 0 
PBR

     Partition Boot Record, stored in first sector of bootable partition 
CHS

     Cylinder, Head, Sector addressing 
LBA

     Linear Block Address[ing] 

##  See also

  * [Making NetBSD Multiboot-Compatible](http://onlamp.com/pub/a/bsd/2007/03/01/inside-multiboot.html)

##  References

  * [boot(8)](http://netbsd.gw.com/cgi-bin/man-cgi?boot+8+NetBSD-current)
  * [fdisk(8)](http://netbsd.gw.com/cgi-bin/man-cgi?fdisk+8+NetBSD-current)
  * [installboot(8)](http://netbsd.gw.com/cgi-bin/man-cgi?installboot+8+NetBSD-current)
  * [mbr(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mbr+8+NetBSD-current)


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb