Home | History | Annotate | Line # | Download | only in mbr
mbr.S revision 1.4
      1 /*	$NetBSD: mbr.S,v 1.4 2003/10/08 04:25:45 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999-2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden, based on an earlier work by Wolfgang Solfrank.
      9  * Major surgery performed by David Laight.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * i386 master boot code
     42  */
     43 
     44 /* Compile options:
     45  * BOOTSEL	- bootselector code
     46  * BOOT_EXTENDED - scan extended partition list (LBA reads)
     47  * TERSE_ERROR	- terse error messages
     48  * NO_CHS	- all reads are LBA
     49  * NO_LBA_CHECK	- no check if bios supports LBA reads
     50  */
     51 
     52 #include <machine/asm.h>
     53 #include <sys/bootblock.h>
     54 
     55 #define BOOTADDR	0x7c00
     56 #define LOADADDR	0x0600		/* address were are linked to */
     57 
     58 #define TABENTRYSIZE	(MBR_BS_PARTNAMESIZE + 1)
     59 #define NAMETABSIZE	(MBR_PART_COUNT * TABENTRYSIZE)
     60 
     61 /* Scan values for the various keys we use, as returned by the BIOS */
     62 #define	SCAN_ENTER	0x1c
     63 #define	SCAN_F1		0x3b
     64 #define	SCAN_1		0x2
     65 
     66 /*
     67  * Minimum and maximum drive number that is considered to be valid.
     68  */
     69 #define MINDRV		0x80
     70 #define MAXDRV		0x87
     71 
     72 #ifdef TERSE_ERROR
     73 /*
     74  * Error codes. Done this way to save space.
     75  */
     76 #define ERR_INVPART	'1'		/* Invalid partition table */
     77 #define ERR_READ	'2'		/* Read error */
     78 #define ERR_NOOS	'3'		/* Magic no. check failed for part. */
     79 #define	ERR_KEY		'?'		/* unknown key press */
     80 #define	ERR_NO_LBA	'L'		/* sector above chs limit */
     81 
     82 #define	set_err(err)	movb	$err, %al
     83 
     84 #else
     85 #define	set_err(err)	mov	$err, %ax
     86 #endif
     87 
     88 	.text
     89 	.code16
     90 /*
     91  * Move ourselves out of the way first.
     92  * (to the address we are linked at - 0x600)
     93  * and zero our bss
     94  */
     95 ENTRY(start)
     96 	xor	%ax, %ax
     97 	mov	%ax, %ss
     98 	movw	$BOOTADDR, %sp
     99 	mov	%ax, %es
    100 	mov	%ax, %ds
    101 	mov	%sp, %si
    102 	movw	$start, %di
    103 	movw	$(bss_start - start)/2, %cx
    104 	rep
    105 	movsw
    106 	mov	$(bss_end - bss_start + 1)/2, %cx
    107 	rep
    108 	stosw
    109 	ljmp	$0, $mbr		/* leap into copy of code */
    110 
    111 /*
    112  * Sanity check the drive number passed by the BIOS. Some BIOSs may not
    113  * do this and pass garbage.
    114  */
    115 mbr:
    116 	cmpb	$MAXDRV, %dl		/* relies on MINDRV being 0x80 */
    117 	jle	1f
    118 	movb	$MINDRV, %dl		/* garbage in, boot disk 0 */
    119 1:
    120 	push	%dx			/* save drive number */
    121 	push	%dx			/* twice - for err_msg loop */
    122 
    123 #ifdef BOOTSEL
    124 /*
    125  * Walk through the selector (name) table printing used entries.
    126  */
    127 bootsel_menu:
    128 	movw	$nametab, %bx
    129 #ifdef BOOT_EXTENDED
    130 	xorl	%ecx, %ecx		/* base of extended partition */
    131 next_extended:
    132 	xorl	%edx, %edx		/* for next extended partition */
    133 #endif
    134 	lea	parttab - nametab(%bx), %bp
    135 next_ptn:
    136 	movb	4(%bp), %al		/* partition type */
    137 #ifdef BOOT_EXTENDED
    138 	movl	8(%bp), %edi		/* partition sector number */
    139 	cmpb	$MBR_PTYPE_EXT, %al	/* Extended partition */
    140 	je	1f
    141 	cmpb	$MBR_PTYPE_EXT_LBA, %al	/* Extended LBA partition */
    142 	je	1f
    143 	cmpb	$MBR_PTYPE_EXT_LNX, %al	/* Linux extended partition */
    144 	jne	2f
    145 1:	movl	%edi, %edx		/* save next extended ptn */
    146 	jmp	3f
    147 2:
    148 #endif
    149 	test	%al, %al		/* undefined partition */
    150 	je	3f
    151 	cmpb	$0, (%bx)		/* check for prompt */
    152 	jz	3f
    153 
    154 	/* output menu item */
    155 	movw	$prefix, %si
    156 	incb	(%si)
    157 	call	message			/* menu number */
    158 	mov	(%si), %si		/* ':' << 8 | '1' + count */
    159 	shl	$2, %si			/* const + count * 4 */
    160 #define	CONST	(4 * ((':' << 8) + '1' - ((SCAN_1 - SCAN_F1) & 0xff)))
    161 #ifdef NO_CHS
    162 	addl	lba_sector, %edi
    163 	movl	%edi, ptn_list - CONST(%si)	/* sector to read */
    164 #else
    165 	mov	%bp, ptn_list - CONST(%si)	/* partition info */
    166 #endif
    167 #undef CONST
    168 	mov	%bx, %si
    169 	call	message			/* prompt */
    170 	movw	$crlf, %si
    171 	call	message
    172 3:
    173 	add	$0x10, %bp
    174 	add	$TABENTRYSIZE, %bx
    175 	cmpb	$(nametab - start - 0x100) + 4 * TABENTRYSIZE, %bl
    176 	jne	next_ptn
    177 
    178 #ifdef BOOT_EXTENDED
    179 /*
    180  * Now check extended partition chain
    181  */
    182 	testl	%edx, %edx
    183 	je	wait_key
    184 	testl	%ecx, %ecx
    185 	jne	1f
    186 	xchg	%ecx, %edx		/* save base of ext ptn chain */
    187 1:	addl	%ecx, %edx		/* sector to read */
    188 	movl	%edx, lba_sector
    189 	movw	$lba_info, %si
    190 	movb	$0x42, %ah
    191 	pop	%dx			/* recover drive # */
    192 	push	%dx			/* save drive */
    193 	int	$0x13
    194 	jc	wait_key		/* abort menu on read fail */
    195 	cmpw	$MBR_MAGIC, LOADADDR + MBR_MAGIC_OFFSET
    196 	movw	$nametab - LOADADDR + BOOTADDR, %bx
    197 	je	next_extended
    198 #endif
    199 
    200 /*
    201  * Get the initial time value for the timeout comparison. It is returned
    202  * by int 1a in cx:dx. We do sums modulo 2^16 so it doesn't matter if
    203  * the counter wraps (which it does every hour) - so we can safely
    204  * ignore 'cx'.
    205  *
    206  * Loop around checking for a keypress until we have one, or timeout is
    207  * reached.
    208  */
    209 wait_key:
    210 	xorb	%ah, %ah
    211 	int	$0x1a
    212 	mov	%dx, %di		/* start time to di */
    213 3:
    214 	movb	$1, %ah			/* looks to see if a */
    215 	int	$0x16			/* key has been pressed */
    216 	jnz	get_key
    217 	xorb	%ah, %ah
    218 	int	$0x1a			/* current time to cx:dx */
    219 	sub	%di, %dx
    220 	movw	timeout, %ax
    221 	cmp	%ax, %dx		/* always wait for 1 tick... */
    222 	jbe	3b			/* 0xffff means never timeout */
    223 def_key:
    224 	movb	defkey, %al		/* timedout - pick default key */
    225 	jmp	check_key
    226 get_key:
    227 	xorb	%ah, %ah
    228 	int	$0x16			/* 'read key', code ah, ascii al */
    229 	shr	$8, %ax			/* code in %al, %ah zero */
    230 
    231 /*
    232  * We have a keycode, see what it means.
    233  * If we don't know we generate error '?' and go ask again
    234  */
    235 check_key:
    236 /*
    237  * <enter> -> boot active partition.
    238  */
    239 	cmpb	$SCAN_ENTER, %al
    240 	jne	boot_disk
    241 #endif	/* BOOTSEL */
    242 
    243 /*
    244  * Scan MBR for first active partition, and boot it.
    245  */
    246 	mov	$MBR_PART_COUNT, %cx
    247 	mov	$parttab, %si
    248 1:
    249 	cmpb	$0x80, (%si)
    250 #ifdef NO_CHS
    251 	jne	10f			/* not active */
    252 	movl	8(%si), %ebp		/* sector number of ptn */
    253 	jmp	boot_lba
    254 10:
    255 #else
    256 	je	boot_si
    257 #endif
    258 	add	$0x10, %si
    259 	loop	1b
    260 	set_err(ERR_INVPART)
    261 	jmp	err_msg
    262 
    263 #ifdef BOOTSEL
    264 /*
    265  * F1-F10 -> boot disk 0-9. Check if the requested disk isn't above
    266  * the number of disks actually in the system as stored in 0:0475 by
    267  * the BIOS.
    268  * If we trust loc 475, we needn't check the upper bound on the keystroke
    269  * This is always sector 0, so always read using chs.
    270  */
    271 boot_disk:
    272 	subb	$SCAN_F1, %al
    273 	cmpb	0x0475, %al
    274 	jae	boot_ptn
    275 	addb	$0x80, %al
    276 	pop	%dx			/* dump saved drive # */
    277 	push	%ax			/* replace with new */
    278 #ifdef NO_CHS
    279 	xorl	%ebp, %ebp		/* read sector number 0 */
    280 	jmp	boot_lba
    281 #else
    282 	movw	$chs_zero, %si		/* chs read sector zero info */
    283 	jmp	read_chs
    284 #endif
    285 
    286 /*
    287  * Boot requested partition.
    288  * Use keycode to index the table we generated when we scanned the mbr
    289  * while generating the menu.
    290  *
    291  * We very carfully saved the values in the correct part of the table.
    292  */
    293 
    294 boot_ptn:
    295 	shl	$2, %ax
    296 	movw	%ax, %si
    297 #ifdef NO_CHS
    298 	movl	ptn_list(%si), %ebp
    299 	testl	%ebp, %ebp
    300 	jnz	boot_lba
    301 #else
    302 	mov	ptn_list(%si), %si
    303 	test	%si, %si
    304 	jnz	boot_si
    305 #endif
    306 	set_err(ERR_KEY)
    307   /*	jmp	err_msg */
    308 #endif	/* BOOTSEL */
    309 
    310 /* Something went wrong...
    311  * Output error code,
    312  * reset disk subsystem - needed after read failure,
    313  * and wait for user key
    314  */
    315 err_msg:
    316 #ifdef TERSE_ERROR
    317 	movb	%al, errcod
    318 	movw	$errtxt, %si
    319 	call	message
    320 #else
    321 	push	%ax
    322 	movw	$errtxt, %si
    323 	call	message
    324 	pop	%si
    325 	call	message
    326 	movw	$crlf, %si
    327 	call	message
    328 #endif
    329 	pop	%dx			/* drive we errored on */
    330 	xor	%ax,%ax			/* only need %ah = 0 */
    331 	int	$0x13			/* reset disk subsystem */
    332 #ifdef BOOTSEL
    333 	pop	%dx			/* original drive number */
    334 	push	%dx
    335 	push	%dx
    336 	jmp	get_key
    337 #else
    338 	int	$0x18			/* BIOS might ask for a key */
    339 					/* press and retry boot seq. */
    340 1:	sti
    341 	hlt
    342 	jmp	1b
    343 #endif
    344 
    345 #ifndef NO_CHS
    346 /*
    347  * Active partition pointed to by si.
    348  * Read the first sector.
    349  *
    350  * We can either do a CHS (Cylinder Head Sector) or an LBA (Logical
    351  * Block Address) read.  Always doing the LBA one
    352  * would be nice - unfortunately not all systems support it.
    353  * Also some may contain a separate (eg SCSI) bios that doesn't
    354  * support it even when the main bios does.
    355  *
    356  * There is also the additional problem that the CHS values may be wrong
    357  * (eg if fdisk was run on a different system that used different BIOS
    358  * geometry).  We convert the CHS value to a LBA sector number using
    359  * the geometry from the BIOS, if the number matches we do a CHS read.
    360  */
    361 boot_si:
    362 	movl	8(%si), %ebp		/* get sector # */
    363 
    364 	testb	$MBR_BS_READ_LBA, flags
    365 	jnz	boot_lba		/* fdisk forced LBA read */
    366 
    367 	pop	%dx			/* collect saved drive... */
    368 	push	%dx			/* ...number to dl */
    369 	movb	$8, %ah
    370 	int	$0x13			/* chs info */
    371 
    372 /*
    373  * Validate geometry, if the CHS sector number doesn't match the LBA one
    374  * we'll do an LBA read.
    375  * calc: (cylinder * number_of_heads + head) * number_of_sectors + sector
    376  * and compare against LBA sector number.
    377  * Take a slight 'flier' and assume we can just check 16bits (very likely
    378  * to be true because the number of sectors per track is 63).
    379  */
    380 	movw	2(%si), %ax		/* cylinder + sector */
    381 	push	%ax			/* save for sector */
    382 	shr	$6, %al
    383 	xchgb	%al, %ah		/* 10 bit cylinder number */
    384 	shr	$8, %dx			/* last head */
    385 	inc	%dx			/* number of heads */
    386 	mul	%dx
    387 	mov	1(%si), %dl		/* head we want */
    388 	add	%dx, %ax
    389 	and	$0x3f, %cx		/* number of sectors */
    390 	mul	%cx
    391 	pop	%dx			/* recover sector we want */
    392 	and	$0x3f, %dx
    393 	add	%dx, %ax
    394 	dec	%ax
    395 
    396 	cmp	%bp, %ax
    397 	je	read_chs
    398 
    399 #ifndef NO_LBA_CHECK
    400 /*
    401  * Determine whether we have int13-extensions, by calling int 13, function 41.
    402  * Check for the magic number returned, and the disk packet capability.
    403  */
    404 	movw	$0x55aa, %bx
    405 	movb	$0x41, %ah
    406 	pop	%dx
    407 	push	%dx
    408 	int	$0x13
    409 	jc	1f			/* no int13 extensions */
    410 	cmpw	$0xaa55, %bx
    411 	jnz	1f
    412 	testb	$1, %cl
    413 	jnz	boot_lba
    414 1:	set_err(ERR_NO_LBA)
    415 	jmp	err_msg
    416 #endif	/* NO_LBA_CHECK */
    417 #endif	/* NO_CHS */
    418 
    419 /*
    420  * Save sector number (passed in %ebp) into lba parameter block,
    421  * read the sector and leap into it.
    422  */
    423 boot_lba:
    424 	movl	%ebp, lba_sector	/* save sector number */
    425 	movw	$lba_info, %si
    426 	movb	$0x42, %ah
    427 	pop	%dx			/* recover drive # */
    428 do_read:
    429 	push	%dx			/* save drive */
    430 	int	$0x13
    431 
    432 	set_err(ERR_READ)
    433 	jc	err_msg
    434 
    435 /*
    436  * Check signature for valid bootcode
    437  */
    438 	movb	BOOTADDR, %al		/* first byte non-zero */
    439 	test	%al, %al
    440 	jz	1f
    441 	movw	BOOTADDR + MBR_MAGIC_OFFSET, %ax
    442 1:	cmp	$MBR_MAGIC, %ax
    443 	set_err(ERR_NOOS)
    444 	jnz	err_msg
    445 
    446 /* We pass the sector number through to the next stage boot.
    447  * It doesn't have to use it (indeed no other mbr code will generate) it,
    448  * but it does let us have a NetBSD pbr that can identify where it was
    449  * read from!  This lets us use this code to select between two
    450  * NetBSD system on the same physical driver.
    451  * (If we've read the mbr of a different disk, it gets a random number
    452  * - but it wasn't expecting anything...)
    453 */
    454 	movl	%ebp, %esi
    455 	pop	%dx			/* recover drive # */
    456 	jmp	start - LOADADDR + BOOTADDR
    457 
    458 
    459 #ifndef NO_CHS
    460 /*
    461  * Sector below CHS limit
    462  * Do a cylinder-head-sector read instead.
    463  */
    464 read_chs:
    465 	pop	%dx			/* recover drive # */
    466 	movb	1(%si), %dh		/* head */
    467 	movw	2(%si), %cx		/* ch=cyl, cl=sect */
    468 	movw	$BOOTADDR, %bx		/* es:bx is buffer */
    469 	movw	$0x201, %ax		/* command 2, 1 sector */
    470 	jmp	do_read
    471 #endif
    472 
    473 /*
    474  * Control block for int-13 LBA read.
    475  * We need a xx, 00, 01, 00 somewhere to load chs for sector zero,
    476  * by a complete fluke there is one here!
    477  */
    478 chs_zero:
    479 lba_info:
    480 	.word	0x10			/* control block length */
    481 	.word	1			/* sector count */
    482 	.word	BOOTADDR		/* offset in segment */
    483 	.word	0			/* segment */
    484 lba_sector:
    485 	.long	0x0000			/* sector # goes here... */
    486 	.long	0x0000
    487 
    488 errtxt: .ascii	"Error "		/* runs into crlf if errcod set */
    489 errcod: .byte	0
    490 crlf:	.asciz	"\r\n"
    491 
    492 #ifdef BOOTSEL
    493 prefix:	.asciz	"0: "
    494 #endif
    495 
    496 #ifndef TERSE_ERROR
    497 ERR_INVPART:	.asciz	"No active partition"
    498 ERR_READ:	.asciz	"Disk read error"
    499 ERR_NOOS:	.asciz	"No operating system"
    500 #ifndef NO_LBA_CHECK
    501 ERR_NO_LBA:	.asciz	"Invalid CHS read"
    502 #endif
    503 #ifdef BOOTSEL
    504 ERR_KEY:	.asciz	"bad key"
    505 #endif
    506 #endif
    507 
    508 /*
    509  * I hate #including source files, but the stuff below has to be at
    510  * the correct absolute address.
    511  * Clearly this could be done with a linker script.
    512  */
    513 
    514 #include <message.S>
    515 #if 0
    516 #include <dump_eax.S>
    517 #endif
    518 
    519 /*
    520  * Stuff from here on is overwritten by fdisk - the offset must not change...
    521  *
    522  * Get amount of space to makefile can report it.
    523  * (Unfortunately I can't seem to get the value reported when it is -ve)
    524  */
    525 mbr_space	= defkey - .
    526 	. = start + MBR_BOOTSEL_OFFSET
    527 /*
    528  * Default action, as a keyvalue we'd normally read from the BIOS.
    529  */
    530 defkey:
    531 	.byte	SCAN_ENTER		/* ps/2 code */
    532 #ifndef BOOTSEL_FLAGS
    533 #define	BOOTSEL_FLAGS	0
    534 #endif
    535 flags:	.byte	MBR_BS_NEWMBR | BOOTSEL_FLAGS
    536 /*
    537  * Timeout value. ~65536 ticks per hour, which is ~18.2 times per second.
    538  * 0xffff means never timeout.
    539  */
    540 timeout:
    541 	.word	182			/* default to 10 seconds */
    542 /*
    543  * Space for name/select table and partition table.
    544  */
    545 nametab:
    546 	.fill	MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1), 0x01, 0x00
    547 
    548 	. = start + MBR_PART_OFFSET - 2
    549 	.word	MBR_MAGIC
    550 
    551 	. = start + MBR_PART_OFFSET
    552 parttab:
    553 	.fill	0x40, 0x01, 0x00
    554 
    555 	. = start + MBR_MAGIC_OFFSET
    556 	.word	MBR_MAGIC
    557 
    558 /* zeroed data space */
    559 bss_off = 0
    560 bss_start = .
    561 #define BSS(name, size) name = bss_start + bss_off; bss_off = bss_off + size
    562 	BSS(ptn_list, 256 * 4)		/* long[]: boot sector numbers */
    563 	BSS(bss_end, 0)
    564