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