Subject: Boot Sequence Guide
To: Jeffrey Russell Horner <jhorner@cs.utk.edu>
From: VaX#n8 <vax@ccwf.cc.utexas.edu>
List: port-i386
Date: 07/20/1995 03:35:32
Guide to partitioning, partition programs, and boot sequences

This guide will attempt to describe partitioning, boot sequences, and the
programs which manage them on the x86 platform.  Perhaps the simplest way
to explain partitions and the bootup sequence is to start with simple cases,
and add complexity as needed.  We will start with a single-OS setup, and
proceed to more copmlicated examples.  We will not attempt to cover those
systems that do geometry translation.  The author takes no responsibility
for any damages caused by following these directions.  This is for
informational purposes only.

SINGLE OS, SINGLE DISK

The simplest setup is a single OS on a single disk.  For example, you might
have NetBSD on your first and only SCSI disk (you do use SCSI, don't you?).
The bootup sequence should be as follows:

1.  The BIOS bootstrap routine loads the first sector of the disk (0:0:1) into
memory at location 0000:7C00H.
The first sector (in this example) is the primary bootstrap loader.
The BIOS checks that the last two bytes of that sector are AA55H.  If they are
not, you will probably get a BIOS-dependent message:

Missing Operating System

2.  This primary or "first stage" bootstrap loader mainly has the job of
loading the secondary bootstrap loader.  The code for the primary bootstrap
is in /usr/mdec/??boot, /usr/mdec/sdboot in this case.
This is generated from a directory like /sys/arch/i386/boot/*.
The primary loader examines the first sector of the disk and looks at
something I call the hard partition table.  It examines this table for a
partition with the system id of 165 (Net/386BSD).  In our case, this partition
should start at the beginning of the disk.
It would load the 2-14th sectors of the NetBSD hard partition it found above.
In this case, it would load the sectors 0:0:2 through 0:0:14.  It loads this
secondary bootstrap loader into the area 0000:1000H.
If the secondary bootstrap loader is absent, you may get the following error
messages from the NetBSD primary bootstrap:

No bootable partition
Read Error

3.  The NetBSD secondary bootstrap loader is recognizable because it provides
the user with a prompt.  It is represented by the file /usr/mdec/bootsd.
The typical prompt looks like:

>> NetBSD BOOT: xxxx/yyyy k []
use hd(1,a)/netbsd to boot sd0 when wd0 is also installed
Boot: [[[sd(0,a)]/netbsd][-adrs] :-

The NetBSD secondary bootstrap is designed to load the kernel.  It has no
device drivers, so that means you'll have to boot off stuff your BIOS
recognizes (i.e. the first two drives).  It looks for the kernel of your
choice.  It would then load it into memory.  Since the kernel might be bigger
than 512K or 640K, the secondary bootstrap has to switch between real mode (so
it can use BIOS calls) and protected mode (so it can access lots of memory).
When you select a drive, or let it default, it goes and reads the partition
table from the disklabel on that drive.  That statement glosses over a lot of
fine details, so let me elaborate.  Suppose it defaults, and goes to boot
this file:

sd(0,a)/netbsd

Using a procedure similar to the primary bootstrap, it finds a hard partition
on sd0 with a NetBSD system ID.  It then searches this hard partition for the
disklabel.  This contains an embedded Unix partition table.
The Unix partition table is just a list of "partitions" and their disk address
ranges.  It finds Unix partition a (the "a" in the sd(0,a) above) and finds
the beginning of that Unix partition.  It then looks for the file netbsd
within this filesystem, which it will load and execute, causing the OS to
finally start.
It can respond with one of these error messages if something goes wrong:

Can't find KERNELFILE - like it says, you specified a filename for the kernel
   which doesn't exist in the drive/partition you specified.
invalid format - the partition you selected does not have the proper "magic
   number" (a special code used to identify formatted partitions) - it could
   be damaged, unformatted, or not even a NetBSD partition.
Wangtek unsupported - ?
kernel too large - your kernel exceeds 1MB or whatever the limit is.
unknown device - you have to pick fd*,sd*,hd* or wd*
bad unit - give something like sd(0,a) - not sd(q,a).

How to set your disk up this way:

Run NetBSD's fdisk, and create a single hard partition that starts at 0:0:1
for NetBSD.  (TODO: This step may or may not be necessary (or possible))
Use "disklabel -w -r sd0 ..." to write a disklabel to the drive.  Note that
this will overwrite the secondary (TODO: also primary?) bootstrap loader
area.  In this situation, this means that it will overwrite the area from
0:0:1 to about 0:0:14 or so.
Use "disklabel -B sd0" to write primary and secondary bootstrap loaders to the 
drive.  Note that this will overwrite the hard partition table, but it has a
"fake" compiled-in hard partition table that has a 386/NetBSD hard partition
starting at the beginning of the disk.  It will not overwrite the Unix
partition table.  (Note: you could simply have used the -B option in the
previous disklabel command, but I seperated them here for clarity).
You can optionally go back and use NetBSD's fdisk to adjust the hard partition
table to accurately reflect the size of the partition, but only the starting
disk address is really necessary.

MULTIPLE OS, SINGLE DISK

Let's say (since it is very common) that you have set up MS-DOS and NetBSD
on your only disk.  The bootup sequence is as follows:

1.  Same as above.

2.  On all the MS-DOS/NetBSD systems I have experience with (excepting boot
managers, see below), the MBR is the MS-DOS primary bootstrap code placed there
when it was initially formatted for MS-DOS.  Should the MS-DOS primary bootstrap
be lost, the (once undocumented?) MS-DOS command "FDISK /MBR" can re-write
it to the (first sector of the) disk.  (TODO: will this blow away the embedded
partition table? I tend to think not.)
It would then proceed to scan it's embedded hard partition table for an active
hard partition (TODO: what if there are more than one, or none?  is that ok?).
The primary loader then relocates itself and loads the first sector from the
active hard partition.  In a sense, this parallels step one above.
If the first sector of the hard partition doesn't have the right magic number,
it might say:

Non-System disk or disk error
Replace and press any key when ready

3.a.  Assume the active hard partition was the MS-DOS partition.  Now we load
the first sector of this partition, which could be a full track into the disk
(that is, address (0:1:1)).  We then load the second and subsequent sectors
until we have bootstrapped ourselves into MS-DOS.  Some versions of MS-DOS
(6.x and up?) will print a message at this point similar to:

Starting MS-DOS

3.b.  Assume the active hard partition was the NetBSD partition.  Now
we proceed by loading the first sector of the NetBSD partition, which we will
assume is at location (100:0:1).  This is the primary bootstrap as described
in SINGLE OS, SINGLE DISK part 2 above.  This time, when it examines the
hard partition table, it will not be the one embedded within itself.  Remember,
the primary NetBSD bootstrap reads the hard partition table from (0:0:1),
even though it resides at some other location (100:0:1).  It searches for
the NetBSD partition, as before, which starts at location (100:0:1).  It
then loads the 2nd-14th sectors from this location.

4.b.  Same as SINGLE OS, SINGLE DISK part 3 above.

How to set your disk up like this:

Use MS-DOS's FDISK.EXE program (usually, run from floppy) to create
a primary MS-DOS partition that does NOT fill the entire drive.  It will
usually start at (0:1:1) and end at some cylinder boundary, let's say (100:0:1).
Then, use NetBSD's fdisk program to create another partition that spans the
rest of the drive.  Make sure to give it a system id of 165, and mark one of
the partitions as active.  Execute "disklabel -B -w -r sd0 ...", which looks
up the NetBSD partition and writes the primary and secondary bootblocks
for NetBSD into the first sectors of the NetBSD hard partition (for example,
they might be put in (100:0:1) through (100:0:14)).  The Unix partition table
is also stored somewhere in here (TODO: find out where).

MULTIPLE OS, SINGLE DISK, BOOT MANAGER

1.  Same as above.

2.  At this point, the boot manager's primary bootstrap will take over.
Some may fix entirely in the first sector, but most will have a two-part
procedure, where the first sector will load it's secondary bootstrap in a
manner reminiscent of OS's.  For example, OS-BS 2.0 Beta 8 loads several
of the following sectors into memory, and presents a menu (which can be
editted with an external utility).  This menu allows the user to pick
any hard partition off the disk.  After the boot manager decided on a
partition (either by user decision or timeout, etc) then it would start an
OS by jumping to the first sector of that hard partition and executing it.  

To set your disk up like this:

Same as above; set up each OS, then install the boot manager last (or as per
its instructions).

MULTIPLE OS, MULTIPLE DISK, BOOT MANAGER

OS-BS V2.0 Beta8 allows you to boot off any hard partition on the 2 first
BIOS-recognized disks.  I have tried many boot managers and find this one
to be the most flexible, the flashiest, and the best-designed.  It is menu
oriented and can time out for unattended reboots.
The documentation that comes with OS-BS discusses some issues with booting
from drives other than the first (two?).

OTHER PROGRAMS

bteasy.zip - another boot manager
pboot.zip - yet another boot manager
PFDISK.EXE - a MS-DOS program which gives you more control over the hard
    partition table in the MBR than the native FDISK.EXE

SEE ALSO

/usr/src/sys/arch/i386/boot/*

GLOSSARY

Active Partition - One of the hard partitions on a disk should be marked as
the active partition, which designates the hard partition to boot.
Boot Blocks - See Primary and Secondary Bootstrap Loaders.
Disk Addressing - A sector of the disk is identified by a triple, consisting
of cylinder, head, sector.  Traditionally, cylinders and head numbering
starts with zero, while sectors start with one.  Most people think of the disk
as being laid out in "row-major order".  This means that the numbering goes
in the following "order" ; 0:0:1, 0:0:2, 0:0:3, and so on.  Although the disk
is laid out in three dimensions, it may help to think of it this way.  Another
way of phrasing the order is that all the sectors are in order, then you
proceed to the next head.  After every head's set of sectors is the next
cylinder.  Physically, heads are aligned vertically, cylinders are concentric
cylinders about the axis of rotation, and sectors are radial units that form
a complete circle about the axis of rotation.
Disklabel - A bunch of data about a drive that tells Unix how to use the
drive.
Filesystem - A single Unix Partition.
First Sector - See master boot record.
Hard Partition Table - A listing of disk address ranges that is always stored
in the first sector of the disk (0:0:1).  Limited in capacity to four
hard partitions.  Sometimes called the DOS partition table.
Master Boot Record - The "first" sector of a disk (0:0:1).  See disk addressing.
MBR - See master boot record.
Physical Partitions - See Hard Partitions.
Physical Partition Table - See Hard Partition Table.
Primary Bootstrap Loader - A piece of code generated by NetBSD, and stored in
/usr/mdec/sdboot.
Secondary Bootstrap Loader - A complement to the Primary Bootstrap Loader,
stored in /usr/mdec/bootsd.
Soft Partitions - See Unix Partition Table.
Unix Partition Table - A listing of "partitions" (letters a-h) and their disk
address ranges, as well as some other information.  Part of the disk label.
NetBSD stores all of it's partitions in such a way that they usually exist
contiguously within a single hard partition, although they can point outside
of it if you so desire (not recommended for novices).

AUTHOR

VaX#n8
vax@ccwf.cc.utexas.edu
20 July 1995
-- 
VaX#n8 (vak-sa-nate) - n, CS senior++ and Unix junkie - vax@ccwf.cc.utexas.edu
Deal with evil through strength, yet encourage good through trust.    - PGP me