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