  INTRODUCTION

Kmbsl is Kmh's Master Boot Sector Loader. It is useful when you install more
than one operating system onto the same computer. When you boot the
computer, kmbsl asks you which operating system to use. If you do not
respond within ten seconds, kmbsl boots the default operating system. Kmbsl
also includes some utilities for reading and writing the master boot sector
and partition table.

With other multiple operating system utilities, you select which partition
you want to boot from; and the utility generates an appropriate partition
table. But if the utility does not generate the partition table you want,
then there is not much you can do about it. For example, sometimes you want
to hide operating systems from each other, to prevent the operating systems
from trashing each other. Other times you want to use one operating system
to fix problems in another operating system. When you configure kmbsl, you
give kmbsl several complete partition tables. When kmbsl runs, kmbsl asks
you which partition table to use, and it boots whatever partition is the
boot partition in that partition table. With other multiple operating system
utilities, you select a partition; with kmbsl you select a partition table.
With kmbsl you specify exactly what partition table to use. For example, you
could make one partition table to boot operating system A, with the files of
operating system B hidden; and another partition table to boot operating
system A, with access to the files of operating system B. This makes kmbsl
more flexible than other multiple operating system utilities; but
unfortunately it makes kmbsl more complicated to configure.

Kmbsl has a customizable prompt. The prompt is 0-511 bytes, which should be
enough for a description of what is happening and a list of available
choices.

Before you can use kmbsl, you must configure it. Kmbsl is not a program
which you can run and see what it does. There is no normal configuration
which will work for most people. Kmbsl does not configure itself. You will
have to create your own custom configuration. I have included my
configuration as an example. DO NOT TRY TO USE MY CONFIGURATION BECAUSE IT
WILL NOT WORK ON YOUR COMPUTER! Before you configure kmbsl you must decide
what you want it to do; and before you decide what you want kmbsl to do you
need to know what it is capable of doing. Therefore you should read the
documentation for kmbsl several times before you try to do anything with
kmbsl. You may need to read a computer reference book to learn more about
how a computer boots. You may need to study the source code for kmbsl to
understand what kmbsl does. With some computer programs, you can start using
them and become an expert by using them. You can not do this with kmbsl. You
must become an expert before you configure and install kmbsl. But once you
have kmbsl correctly configured and installed, then any idiot can use kmbsl.

When I say master boot sector or MBS, I mean the 512 bytes of the first
sector of the hard drive; which some people call master boot record or MBR,
partition table or PT, or initial program loader or IPL. When I say
partition table or PT, I mean the 64 bytes of the master boot sector which
describe the partitions on the hard drive. Kmbsl stores its partition tables
elsewhere, but kmbsl copies the current partition table to the standard
location in the master boot sector so that other programs can find it.

You will need to edit some files which include nontext characters such as
nul, otherwise known as ascii 0 or control-@. You will need to edit other
files without adding control-z or newline characters to the end. Most text
editors will not work. For example, MS Edit converts nuls to spaces and adds
a newline to the end. You will probably have to use a binary file editor. If
you do not have a binary file editor, you may have to use debug. I use a
free DOS binary file editor named Hexedit, XE.EXE. XE will overwrite any
character with any other character you want, but it will not insert or
remove characters; thus it will not change the size of a file or create a
new file. This may seem like a defect in XE, but it is actually good for use
with kmbsl, because the kmbsl configuration files must be the correct sizes;
you can edit any of them with XE and you do not have to worry about
accidently changing the size of the file. Thus when I use XE, I first find
another file which is the correct size, then I make a copy of that file,
then I use XE to edit the copy.

Kmbsl does not support booting from the second hard drive.

  HOW TO INSTALL KMBSL

You should copy the kmbsl files to a floppy disk, and you should configure
and install kmbsl from the floppy disk. If you configure kmbsl incorrectly,
you might not be able to access files on your hard drive. But if you have
the kmbsl files on a floppy disk, then you can reconfigure and reinstall or
uninstall kmbsl from the floppy disk. While you are configuring kmbsl, you
may be setting up and trying out several different partition tables and
running several different operating systems. You might not have all files on
your hard drive available all the time. But if you have the kmbsl files on a
floppy disk, then you should have the kmbsl files available all the time.
You might also need a DOS boot floppy.

The first step is to back up your current master boot sector. You should
keep a copy of your master boot sector on the kmbsl floppy, so that you will
be able to restore the master boot sector even if you accidently trash your
hard drive. Make the kmbsl floppy the current drive. Choose a name for the
file which will be the copy of the master boot sector, perhaps ORIGINAL.MBS.
Run the command 'MBSR ORIGINAL.MBS'. Your current master boot sector is now
backed up. Any time you want to uninstall kmbsl and restore your hard drive
to the way it used to be, run the command 'MBSW ORIGINAL.MBS'.

Decide what partition tables you want, and create a partition table file for
each partition table. Kmbsl allows you to have 1 to 15 partition tables.
Each partition table file must be exactly 64 bytes. You could create the
partition tables by editing the partition table files with a binary editor,
but it is probably easier to set up each partition table with fdisk or
whatever in the usual way. Make sure the computer boots and works the way
you want. Use PTR.BAT to read the partition table from the hard drive and
write it to a file. Choose a name for the file; perhaps something like
WIN95.PT. Then run the command 'PTR WIN95.PT'. You might also want to save a
copy of the full master boot sector for each partition table. If the
operating system you are running does not run DOS programs or if the
operating system prevents programs from accessing the master boot sector,
you will have to boot from a DOS floppy in order to make copies of the
partition table and master boot sector. Then do your next partition
table.

The standard master boot sector loader created by DOS fdisk has the
following rules: All partitions must have the active byte set to either 80h
(active) or 0 (not active). There must be one partition with the active byte
set to 80h. There must not be more than one partition with the active byte
set to 80h. There must not be a partition with the active byte set to 0
before a partition with the active byte set to 80h. In other words, the
first partition in the partition table must be 80h and the last three
partitions in the partition table must be 0. Thus you can only boot from the
first partition in the partition table. The first partition in the partition
table is not always the first partition on the hard drive; you can boot from
any partition on the hard drive as long as that partition is listed first in
the partition table. If you violate these rules, the master boot sector
loader will display an error message and refuse to boot your computer.

Kmbsl does not have so many rules. It will try to boot from the first
partition which is 80h. Kmbsl does not care if this is not the first
partition in the partition table, if other partitions are also 80h, or if
other partitions are something other than 80h or 0. However, some operating
systems or programs may require the standard rules. Therefore, I suggest you
try to obey the standard rules if possible. If you create your partition
table with fdisk, fdisk will probably make sure your partition table
complies with the standard rules, so you probably do not have to worry about
this. For example, if you use fdisk to change the boot partition, fdisk will
probably rearrange the partition table so that the new boot partition is
listed first. Some versions of fdisk display partitions in the order in
which the partitions occur on the hard drive and do not tell you the order
in which the partitions are listed in the partition table; probably when you
tell fdisk to write the partition table, fdisk puts the boot partition
first, then puts the other partitions in the order in which the partitions
occur on the hard drive. Linux fdisk gives you more freedom to break the
rules, so be careful if you are using linux fdisk.

Decide which partition table you want to be the default partition table. The
default partition table which will be used if you do not choose a partition
table within ten seconds when the computer boots.

Decide what key you want to use for each partition table, so that if you
press that key, kmbsl will select that partition table. Create a key file
which contains the character you have chosen. The size of the key file must
be exactly 1 byte. I always use letter keys. 'a' is a different character
than 'A'. You have to press shift to get uppercase letters; thus it is
easier to type lowercase letters than uppercase letters because lowercase
letters are typed with one keypress, while uppercase letters require two
keypresses. Since I am lazy, I use only lowercase letters. Number keys or
punctuation keys should also work. I think the function keys, arrow keys,
etc will not work. You should have a different key for each partition table.
If two different partition tables have the same key, when you press that key
the first partition table will be used, and there will be no way for you to
use the other partition table. Thus you need one key file for each partition
table file. I suggest that you give the key file a name which matches the
name of the partition table file, like 'WIN95.KEY'; or else give the key
file a name which matches the key, like 'W'.

Decide whether the partition table should be written to the hard drive or
not. When you boot your computer, a copy of the partition table is loaded
into memory before the operating system is loaded. Some operating systems
read the partition table from memory; other operating systems read the
partition table from the hard drive. If the operating system reads the
partition table from memory, then you do not have to write the partition
table to the hard drive. But if the operating system reads the partition
table from the hard drive, then you have to write the partition table to the
hard drive. If you do not know, then you could always write the partition
table to the hard drive; writing always works even though it is not always
needed. However, you might wear out the master boot sector if you write to
it too often; and if the master boot sector goes bad, then the whole hard
drive is unusable; therefore you might want to not write the partition table
unless you need to. On the other hand, someone told me that hard drives from
about 1996 or newer can detect and redirect bad sectors, so if the master
boot sector goes bad, the hard drive will store the data in some other
sector, so you do not have to worry about the master boot sector going bad.
You could try not writing the partition table to disk, and change to write
if the computer does not boot. Someone told me writing the partition table
is not needed for DOS, and is needed for everything else: Windows 95+,
Windows NT, OS2, Linux, etc. If you have one partition table you use more
often than any other partition table, you probably should write that
partition table so that it is likely to be in effect if you boot from a
floppy.

Decide what you want for a prompt, and put the prompt in a file. The prompt
file must be exactly 512 bytes. There must be a nul at the end of the
prompt; thus the maximum size of the prompt is 511 bytes. If your prompt is
less than 511 bytes, put your prompt, then a nul, then more nuls or anything
at all to fill in 512 bytes. All characters after the first nul will be
ignored. If the first byte of the prompt is nul, then you will have no
prompt. If you want a newline in the prompt, put carriage return (control-M,
0D hex) then line feed (control-J, 0A hex). If you want two consecutive
newlines, you can put carriage return, line feed, carriage return, line
feed; or you can put carriage return, line feed, line feed.

Now you have all the components of kmbsl in many little files. You need to
combine all these little files together in the correct order into one 2048
byte file, and write that 2048 byte file to the hard drive. Study the file
INSTALL.BAT. INSTALL.BAT uses CAT.BAT to combine my kmbsl components and
installs kmbsl, except that I have sabotaged INSTALL.BAT so that it does
nothing to prevent you from accidently installing my configuration of kmbsl.
The first 512 bytes is the kmbsl loader program, which is contained in the
file KMBSL. The second 512 bytes is the prompt. Then comes the table of
partition tables. The first partition table is the default partition table,
so whichever partition table you have chosen as the default partition table,
put that partition table first. For the rest of your partition tables, kmbsl
will be a few nanoseconds faster if you put the most frequently used
partition tables first, but I doubt you will notice the difference, so you
can put the partition tables in any order. For each partition table, first
put the 1 byte key file, then the 1 byte write/nowrite file, then the 64
byte partition table file. For the write/nowrite file, I have included two
files: WRITE contains the byte which tells kmbsl that the partition table
needs to be written to the hard drive, and NOWRITE contains the byte which
tells kmbsl that the partition table does not need to be written to the hard
drive. After your partition tables, you will need to add another 34 bytes.
Kmbsl ignores these 34 bytes, so you can put anything you want here; but the
bios requires that you put something here. I put 34 nulls, as in the file
null34. Now you should have a 2048 byte file which contains kmbsl and your
configuration; all you have to do now is write it to your hard drive with
MBSW4S.BAT. I suggest you rewrite INSTALL.BAT so that it combines your kmbsl
components and installs kmbsl; but if you prefer to type the commands at the
prompt, go right ahead, I don't care what you do.


  THE MASTER BOOT SECTOR UTILITY PROGRAMS

For configuring and installing kmbsl, I needed a program which could read
and write the master boot sector. Most languages do not include functions
for reading and writing the master boot sector, so I needed to write my
master boot sector utility program in assembly language. It is a lot of work
to write programs in assembly language, so to make it easier, I made a
seperate program for each function, and wrapped each program in a batch
file, and the batch file provides the error messages and alternate file name
functions. Thus I did not have to bother with reading the command line
parameters, selecting a function, or displaying messages in assembly
language. You can ignore the batch files and run the programs directly if
you want, but you will get no error messages and no choice of file names. I
suggest you use the batch files to run the programs.

MBSR means Master Boot Sector Read.
MBSR reads the master boot sector and writes the data to a file. If you give
the batch file a filename, that file is used; otherwise the file MBS.BIN is
used.
MBSW means Master Boot Sector Write.
MBSW is the reverse of MBSR; MBSW reads the file and writes the data to the
master boot sector.

MBSR4S means Master Boot Sector Read 4 Sectors. MBSW4S means Master Boot
Sector Write 4 Sectors.
MBSR4S and MBSW4S are like MBSR and MBSW, except that MBSR4S and MBSW4S
read/write four sectors instead of one sector (4 sectors starting with the
master boot sector, the master boot sector plus the next three sectors) and
the default file name is MBS4S.BIN.

PTR means Partition Table Read. PTR is like MBSR, except that PTR write only
the 64 bytes of the partition table to the file, not the whole 512 bytes of
the master boot sector; and the default filename is PARTITIO.TBL.

You may want to use the utility programs for other things, even if you do
not want to use kmbsl. For example, you can use MBSR and MBSW to backup and
restore your master boot sector. Or you could use MBSR and MBSW to switch
between multiple master boot sectors; you boot into one operating system,
run a menu program to select an operating system, rewrite the master boot
sector for the selected operating system, reboot into that operating system,
then when that operating system shuts down it should rewrite the master boot
sector so that the next boot will run the operating system with the menu
program.

The utility programs will probably not work in Windows 95. My copy of Ralf
Brown's interrupt list, which I downloaded from simtel, says that in Windows
95 we need to create a volume lock before reading or writing the master boot
sector. Since the utility programs do not create volume locks, the utility
programs will probably not work under Windows 95.

  SOURCE CODE

I assembled these programs with NASM for Linux, so the source is in NASM
format, which means you will have to adjust the source code if you want to
use MASM or TASM.

The timeout is set to ten seconds. If you want a different timeout, you will
need to reassemble. Or maybe you could use use the listing file KMBSL.LST to
figure out which bytes are the timeout, and change those bytes with a binary
editor.

If you want a bigger prompt or more partition tables, you will need to
modify kmbsl to use more than 4 sectors. Hard drives are usually partitioned
so that the first partition begins with the second head (I do not know why);
thus all the sectors of the first head are unused except for the master boot
sector. Thus the maximum number of sectors which you can use with kmbsl is
the number of sectors per head for your hard drive, which is probably 63.
Check your cmos, or the manual for your hard drive, or the label on your
hard drive. If you use too many sectors with kmbsl, you will overwrite and
trash your first partition.

You could modify kmbsl to read the boot sector from a partition from the
second hard drive instead of from the first hard drive, and thus boot from
the second hard drive. However, I suspect that most operating systems will
not work if booted from the second hard drive, so you are probably wasting
your time if you try to boot from the second hard drive.

When I started coding kmbsl, I thought maybe I should have one sector for
the code, one sector for the prompt message, and two sectors for the table
of partition tables. So I coded kmbsl that way, and it worked. So I have the
prompt message and the table of partition tables in seperate sectors. It
does not have to be this way. I started coding kmbsl this way because it was
a way of dividing kmbsl up into parts and thinking about each part
seperately, and because it was a way of estimating how many disk sectors I
would need. And once I had started coding kmbsl this way, I was too lazy to
change it. However, the prompt message is not likely to use exactly one
sector, so there will probably be some unused space after the prompt message
and before the table of partition tables. The table of partition tables does
not use exactly two sectors, so there is some more unused space at the end
of the table of partition tables. So if we move the unused bytes at the end
of the table of partition tables to the beginning of the table of partition
tables, we could use these bytes to have a slightly larger prompt message;
or we could have a slightly smaller prompt message and have one more
partition table in the table of partition tables. There is no reason why the
boundary between the prompt message and the table of partition tables has to
be on a sector boundary.

The master boot sector utility programs should be combined into one program.
I would call it KMBS, kmh's master boot sector utility program. I would have
command line options -r and -w to tell KMBS whether to read or write the
master boot sector; -f=filename to tell KMBS what file to transfer the data
to/from; and -s=number to tell KMBS how many sectors to read/write, with
-s=0 meaning to read/write just the partition table. The program should
create volume locks under Windows 95. Also perhaps there should be an option
to tell it to go ahead even if the file is the wrong size; if the file is
too large, just read the first data and ignore the rest; if the file is too
small, read all the data from the file and then add nulls to get the right
amount of data, and then write the data to the disk.

Maybe there should be a few bytes reserved for special reboot instructions.
Then a program could write some data to those bytes and reboot; then kmbsl
would read those bytes and do whatever the the data said to do instead of
kmbsl's usual behavior, maybe skip the prompt and the timeout and boot a
nondefault operating system. This would be useful if you had a menu program,
and you wanted the menu program to be able to control kmbsl. Or kmbsl could
write some data to some reserved bytes, boot an operating system, then the
operating system could run a program which read the bytes and do something
like change the configuration of the operating system or run some other
program. This would be useful if you wanted the kmbsl prompt to do more than
just select an operating system, like select a configuration for the
operating system or select a program to run after loading the operating
system.

  MISC

Kmbsl is intended for use on IDE hard drives. I do not know what will happen
if you try to use kmbsl on an SCSI hard drive. I think kmbsl will work on
SCSI hard drives.

I do not know what would happen if you try to use kmbsl on a hard drive
which has a partitioning scheme other than the normal partitioning scheme,
like if you are using Ontrack Disk Manager.

Kmbsl is not a DOS program; it does not require DOS. However, my
installation method requires DOS. To install kmbsl from Windows 95, I think
we would have to modify the utility programs so that the utility programs
create volume locks. To install kmbsl from linux, read/write the master boot
sector by reading/writing /dev/hda; maybe something like
'dd bs=512 count=1 < /dev/hda > master_boot_sector'. I do not know how to
install kmbsl from OS2 or Windows NT.

Why does the partition table require a loader program? Why not build the
loader program into the bios? Better yet, build a standard loader program
into the bios, put it at an absolute address or interrupt; then a partition
table could simply jump to the standard bios loader, or run a nonstandard
loader. Better yet, define several different kinds of partition tables, each
with a different signature. The bios should then check the signature, and do
something different for each different kind of partition table.


Kenneth M Howlett   av556@detroit.freenet.org
