Search (using Google):  Web Karig

 

6 March 2004

(Revised 8 March 2004)

Overwriting an MBR

First, a few words about this software:

WARNING!

(Big nasty skull and crossbones, wreathed in flames)

This software is dangerous. It will destroy the partitions on your hard disk. You will NOT be able to return to Windows after using this software. If you do not intend for this to happen, do NOT run this software. I will NOT be responsible if you do not heed this warning.

If you do download this software and make a bootable floppy out of it, MARK THE FLOPPY AS DANGEROUS, and make sure you remove the disk from the drive if you are not using it.

DO NOT USE THIS SOFTWARE except on a system you don't mind destroying. I have provided it solely for education purposes, and to show what I've been doing. Its purpose is to install a Karig partition on the hard disk — a partition that uses all of the space on the disk. Eventually, though, I'll get around to writing a more usable installer that you can use if you want to have Karig live on the same hard disk as another partition.

What I've been doing

I was working on the ColorForth commentary, but I decided to pause for a while and return to installing code on my laptop.

The laptop I've been using to test code up to now is not the laptop onto which I want to install Karig. The laptop I've been using is a WinBook J1 — a full-sized laptop with built-in floppy drive and a combination CD burner/DVD reader. I bought it over two years ago, back when I had an extra wad of cash to spend. :-) It has Windows XP installed on it, and I'd still like to be able to use it occasionally to watch DVDs, so I don't want to install Karig on it.

The new laptop, which I bought on eBay about two months ago for about a hundred bucks, is a Soyo PW-9800 — a much smaller laptop, weighing about three or four pounds, with a screen about 61/4 inches wide by 43/4 inches high. It came with a Cyrix 233MHz processor with MMX, 32MB of RAM, 1MB of video RAM (enough for 16-bit color at 800x600 video resolution), a 3GB hard disk inside, and floppy and CD-ROM drives that you have to hook up to the laptop with cables. When I got the laptop, it booted into Windows 2000 when you turned it on, but the performance of Windows on this particular machine was dog slow, so using it would have been a chore. Windows 2000 by itself used up about a quarter of the hard disk. I'd say Windows 2000 is really too big for this machine.

Karig will be small, and this machine is small, so the two just seemed made for each other. :-)

So I moved my big laptop somewhere else, and my Soyo laptop now has the place of honor on my table next to my PC; it has replaced the WinBook as my testing machine. Where before I had been just writing floppy-disk boot sectors to test my code, now I want to install code onto the Soyo's hard disk and run it by rebooting the Soyo.

This requires a change in the way I build the software. All my previous mini-projects required only one boot sector, because the software would be run off the floppy. Now I'll need to write two boot sectors: the first boot sector, when run, will copy the second boot sector from the floppy onto the hard disk.

The two boot sectors have to be in separate files. NASM will not assemble both boot sectors at once; NASM has to be called once to assemble one boot sector and then called again to assemble the other. This is because each boot sector needs its own ORG directive, and NASM will not accept more than one in a given project.

Because of this, I've decided that each entry that discusses a mini-project will provide a link, not to a single ASM file you can view directly in your browser, but to a ZIP file containing all the files in the mini-project. This ZIP file will typically include the following:

  • MAKE.BAT. When I need to assemble the files, I open a command prompt at the folder containing the files and type "make". This assembles the two source-code files, copies the two output files into a single binary file, and writes the binary file to the floppy disk.
  • BS.ASM. This is the source code for the floppy-disk boot sector, which is supposed to install the other boot sector onto the hard disk.
  • SYS.ASM This is the source code for the boot sector to be installed onto the Karig partition on my hard disk.

The MBR

The MBR (master boot record) is essentially the boot sector for the whole hard disk. The hard disk usually contains one or more partitions, each containing its own file system, so that you can have one partition for Windows and one for Linux, for example. The MBR includes not only boot code, but also a table of four 16-byte records, each providing information on a partition — how big it is, where it is, and what file system or operating system the partition contains.

My original thought was simply to devote the entire hard disk to Karig — to write Karig to assume that every sector on the hard disk was OK to read from and write to. I originally had two objections to confining Karig to a partition:

  • Putting a second partition on the hard disk — having Karig share the disk with another OS — was not something I was planning to do. I wanted Karig to have free reign over the full three gigabytes the hard disk offers. Because Karig would be the only thing on the disk, it seemed pointless not to just use the entire disk.

  • Partitions are traditionally installed on track boundaries — that is, they traditionally begin on the first sector of a given track. Because the MBR takes up the first sector of the first track, a Karig partition would need to go into the first sector of the second track. A hard-disk track contains 63 sectors, so this would mean 62 sectors (31KB) of disk space going completely unused, because I wasn't planning on installing a boot manager. (A boot manager is a program that runs when you turn on the computer and less you choose which OS you want to boot. Its code is usually stored in the normally unused sectors in the first track of the hard disk.)

So I first experimented to see if I could just use the MBR as a straight boot sector which would just load the rest of the system from the sectors in that first track. I didn't go all the way and write code to actually read other sectors into memory, but I'm confident that I could have done so and that it would have worked, because the new MBR that I wrote — one with no valid partition entries, one that did nothing but print a message on the screen — loaded and executed correctly. So I could have just used the hard disk as an array of sectors, with no partition on it.

However, the reality is that, if I'm going to be sharing Karig with anyone, it needs to be able to live in a partition. Somebody downloading Karig would need to be able to set aside a partition to contain Karig and not have to worry that Karig will simply overwrite sectors set aside for his Windows or Linux or BeOS partition. So I can make Karig sharable with other people, at the cost of only 31KB of unused disk space.

So I wrote an MBR, and I wrote a boot sector to install the MBR on the first sector of the hard disk.

Code

The code is in a ZIP file.

The binary image produced by the batch file contains two boot sectors. The first one (produced by BS.ASM) will be the boot sector on the floppy; the second one (produced by SYS.ASM) will be written onto the hard disk.

BS.ASM

The floppy-disk boot sector simply clears the screen,

		mov	ax, 3
		int	0x10

reads the second sector of the first track of the floppy into memory (using INT 13h, AH=2),

		mov	ch, 0      ; cylinder #
		mov	dh, 0      ; head #
		mov	cl, 2      ; sector #
		mov	dl, 0x00   ; drive # (0=floppy, 0x80=HD)
		mov	ah, 2      ; action (2=read, 3=write)
		mov	al, 1      ; # sectors
		mov	bx, 0x7E00 ; buffer address
		int	0x13

writes it to the first sector of the first track of the first hard disk on the system (using INT 13h, AH=3),

		mov	ch, 0      ; cylinder #
		mov	dh, 0      ; head #
		mov	cl, 1      ; sector #
		mov	dl, 0x80   ; drive # (0=floppy, 0x80=HD)
		mov	ah, 3      ; action (2=read, 3=write)
		mov	al, 1      ; # sectors
		mov	bx, 0x7E00 ; buffer address
		int	0x13

and prints a message.

		mov	ax, 0x1301
		mov	bx, 0x000F
		mov	cx, len1
		mov	dx, 0x0000
		mov	bp, msg1
		int	0x10

		jmp	$

msg1:		db	"Sector copied."
len1:		equ	$-msg1

SYS.ASM

When you reboot the computer without the floppy in the drive, the boot sector written to the hard disk will run. This boot sector is a replacement MBR.

The MBR clears the screen,

		mov	ax, 3
		int	0x10

and prints a message to show that the MBR did indeed load.

		mov	ax, 0x1301
		mov	bx, 0x000F
		mov	cx, len1
		mov	dx, 0x0000
		mov	bp, msg1
		int	0x10

		jmp	$

msg1:		db	"Placeholder for OS loaded."
len1:		equ	$-msg1

Also note that my MBR has a partition table. The values in this partition table are particular to my system.

Note that the first partition is marked as bootable (0x80). The software that is usually in an MBR is supposed to be able to load and run the boot sector in a partition marked as bootable.

Also note that the partition-type ID I have chosen for Karig is 0x6C. (I selected a value that does not appear on this list of partition-type IDs.) When I get around to writing a real installer, I'll have it examine the MBR for a partition with an ID of 0x6C, and I'll have it refuse to install if such a partition does not exist.

		times	0x1BE - ($-$$) db 0x90 ; nop

		db	0x80           ; bootable
		db	1,1,0          ; starting head, sector, cylinder
		db	0x6C           ; partition type

		db	0x7F           ; ending head     (0x7F)
		db	0x3F+(0x3<<6)  ; ending sector   (0x3F)
		db	0x13           ; ending cylinder (0x313)

		dd	1              ; starting sector (LBA)
			               ;   (Safe value is 63)
		dd	6354369        ; ending sector (LBA)

Finally, note that the other three partition records are all blank.

		dd	0,0,0,0
		dd	0,0,0,0
		dd	0,0,0,0

		db	0x55, 0xAA    ; marks sector as boot sector

Remember, don't assemble and run this software on your system. I wrote this simply as an experiment.

[UPDATE 2004-03-08: I've changed this code to make it a little safer. The original version would destroy your MBR as soon as the BIOS ran the code, which is one of the reasons this code was especially dangerous. Now I've added code to print a message warning you of what's about to happen and to wait until you press the 'Y' key before proceeding. Although this makes the code less dangerous, and a little more fit to be distributed to other people, the software still destroys the MBR, so it's still dangerous code.]

(Skull design courtesy Extreme Clipart.)

Check the index for other entries.