Home | History | Annotate | Line # | Download | only in fatboot
fatboot.S revision 1.2
      1  1.2  dsl /*	$NetBSD: fatboot.S,v 1.2 2007/01/06 20:47:15 dsl Exp $	*/
      2  1.1  dsl 
      3  1.1  dsl /*-
      4  1.1  dsl  * Copyright (c) 2007 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 David Laight.
      9  1.1  dsl  *
     10  1.1  dsl  * Redistribution and use in source and binary forms, with or without
     11  1.1  dsl  * modification, are permitted provided that the following conditions
     12  1.1  dsl  * are met:
     13  1.1  dsl  * 1. Redistributions of source code must retain the above copyright
     14  1.1  dsl  *    notice, this list of conditions and the following disclaimer.
     15  1.1  dsl  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  dsl  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  dsl  *    documentation and/or other materials provided with the distribution.
     18  1.1  dsl  * 3. Neither the name of The NetBSD Foundation nor the names of its
     19  1.1  dsl  *    contributors may be used to endorse or promote products derived
     20  1.1  dsl  *    from this software without specific prior written permission.
     21  1.1  dsl  *
     22  1.1  dsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.1  dsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.1  dsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.1  dsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.1  dsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1  dsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1  dsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  dsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  dsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1  dsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.1  dsl  * POSSIBILITY OF SUCH DAMAGE.
     33  1.1  dsl  */
     34  1.1  dsl 
     35  1.1  dsl /*
     36  1.1  dsl  * i386 partition boot code
     37  1.1  dsl  * This version reads boot directly from a FAT16 filesystem on LBA
     38  1.1  dsl  * Addressable media - eg USB media.
     39  1.1  dsl  *
     40  1.1  dsl  * The code is read to address 0:7c00 by the mbr code (in sector zero).
     41  1.1  dsl  *
     42  1.1  dsl  * We assume that the partition contains a 'boot parameter block' that
     43  1.1  dsl  * correctly identifies the filesystem.
     44  1.1  dsl  *
     45  1.1  dsl  * On entry and exit the BIOS drive number is in %dl.
     46  1.1  dsl  */
     47  1.1  dsl 
     48  1.1  dsl #include <machine/asm.h>
     49  1.1  dsl #include <sys/bootblock.h>
     50  1.1  dsl 
     51  1.2  dsl #ifndef FAT_ENTRY_SIZE
     52  1.2  dsl #error FAT_ENTRY_SIZE not defined
     53  1.2  dsl #endif
     54  1.2  dsl 
     55  1.2  dsl /* Support for FAT32 could be added - but hasn't been yet. */
     56  1.2  dsl #if FAT_ENTRY_SIZE != 16
     57  1.2  dsl #error Unsupported FAT_ENTRY_SIZE value
     58  1.2  dsl #endif
     59  1.2  dsl 
     60  1.1  dsl #define PBR_AFTERBPB	62		/* BPB size in floppy master BR */
     61  1.1  dsl 
     62  1.1  dsl #ifdef TERSE_ERROR
     63  1.1  dsl /*
     64  1.1  dsl  * Error codes. Done this way to save space.
     65  1.1  dsl  */
     66  1.1  dsl #define ERR_READ		'R'	/* Read error */
     67  1.1  dsl #define ERR_NO_BOOT		'B'	/* No /boot */
     68  1.1  dsl #define ERR_NOT_FAT16		'F'	/* Not a FAT16 filesystem */
     69  1.1  dsl #define	ERR_NO_BOOT_MAGIC_2	'M'	/* No magic in loaded /boot */
     70  1.1  dsl 
     71  1.1  dsl #define	set_err(err)	movb	$err, %al
     72  1.1  dsl 
     73  1.1  dsl #else
     74  1.1  dsl #define	set_err(err)	mov	$err, %ax
     75  1.1  dsl #endif
     76  1.1  dsl 
     77  1.1  dsl 	.text
     78  1.1  dsl 	.code16
     79  1.1  dsl ENTRY(start)
     80  1.1  dsl 	jmp	start0
     81  1.1  dsl 	nop
     82  1.1  dsl oem_name:               .ascii	"NetBSD40"	/* 8 bytes */
     83  1.1  dsl /* FAT16 BIOS/BOOT Parameter Block - see struct mbr_bpbFAT16 in bootblock.h */
     84  1.1  dsl #define	bpb_bytes_per_sec   /* .word 0	*/ 0x0b /* bytes per sector */
     85  1.1  dsl #define	bpb_sec_per_clust   /* .byte 0	*/ 0x0d /* sectors per cluster */
     86  1.1  dsl #define	bpb_res_sectors     /* .word 0	*/ 0x0e /* number of reserved sectors */
     87  1.1  dsl #define	bpb_FATs            /* .byte 0	*/ 0x10 /* number of FATs */
     88  1.1  dsl #define	bpb_root_dir_ents   /* .word 0	*/ 0x11 /* number of root dir entries */
     89  1.1  dsl #define	bpb_sectors         /* .word 0	*/ 0x13 /* total number of sectors */
     90  1.1  dsl #define	bpb_media           /* .byte 0	*/ 0x15 /* media descriptor */
     91  1.1  dsl #define	bpb_FAT_secs        /* .word 0	*/ 0x16 /* number of sectors per FAT */
     92  1.1  dsl #define	bpb_sec_per_track   /* .word 0	*/ 0x18 /* sectors per track */
     93  1.1  dsl #define	bpb_heads           /* .word 0	*/ 0x1a /* number of heads */
     94  1.1  dsl #define	bpb_hidden_secs     /* .long 0	*/ 0x1c /* # of hidden sectors */
     95  1.1  dsl #define	bpb_huge_sectors    /* .long 0	*/ 0x20 /* # of sectors if !bpbSectors*/
     96  1.1  dsl /* Extended boot area */
     97  1.1  dsl #define	bs_drive_number     /* .byte 0	*/ 0x24 /* but we believe the BIOS ! */
     98  1.1  dsl #define	bs_reserved_1       /* .byte 0	*/ 0x25 /* */
     99  1.1  dsl #define	bs_boot_sig         /* .byte 0	*/ 0x26 /* */
    100  1.1  dsl #define	bs_volume_id        /* .long 0	*/ 0x27 /* Volume ID number */
    101  1.1  dsl #define	bs_volume_label     /* .space 11*/ 0x2b /* Volume label */
    102  1.1  dsl #define	bs_file_sys_type    /* .space 8	*/ 0x36 /* "FAT16   " */
    103  1.1  dsl 
    104  1.1  dsl /* Some locals overlaying the end of the above */
    105  1.1  dsl #define sec_p_cl_w	0x2c			/* 16bit bpb_sec_per_clust */
    106  1.1  dsl #define	fat_sector	0x30			/* start of FAT in sectors */
    107  1.1  dsl 
    108  1.1  dsl 	. = start + PBR_AFTERBPB	/* skip BPB */
    109  1.1  dsl start0:
    110  1.1  dsl 	xor	%eax, %eax		/* don't trust values of ds, es or ss */
    111  1.1  dsl 	mov	%ax, %ds
    112  1.1  dsl 	mov	%ax, %es
    113  1.1  dsl 	mov	%ax, %ss
    114  1.1  dsl 	mov	$start, %sp
    115  1.1  dsl 	mov	%sp, %bp		/* to access the pbp */
    116  1.1  dsl 	push	%dx			/* save drive at -2(%bp) */
    117  1.1  dsl 
    118  1.1  dsl 	set_err(ERR_NOT_FAT16)
    119  1.1  dsl 	cmpl	$'A'|'T'<<8|'1'<<16|'6'<<24, bs_file_sys_type+1(%bp)
    120  1.1  dsl 	jne	error
    121  1.1  dsl 
    122  1.1  dsl /* Add 'reserved' (inside ptn) to 'hidden' (ptn offset) */
    123  1.1  dsl 	mov	bpb_res_sectors(%bp), %ax
    124  1.1  dsl 	addl	bpb_hidden_secs(%bp), %eax
    125  1.1  dsl 	mov	%eax, fat_sector(%bp)	/* To get first sector of FAT */
    126  1.1  dsl 
    127  1.1  dsl /* Determine base of root directory */
    128  1.1  dsl 	movzbw	bpb_FATs(%bp), %ax	/* Count of FATs */
    129  1.1  dsl 	mulw	bpb_FAT_secs(%bp)	/* FAT size in %dx:%ax */
    130  1.1  dsl 	shl	$16,%edx
    131  1.1  dsl 	xchg	%ax,%dx			/* FAT size now in %edx */
    132  1.1  dsl 	addl	fat_sector(%bp), %edx	/* Directory is after FATs */
    133  1.2  dsl 	push	%cs			/* %cs is zero */
    134  1.2  dsl 	push	%cs			/* 64-bit for LBA read */
    135  1.1  dsl 	pushl	%edx			/* Sector number of root dir */
    136  1.1  dsl 
    137  1.1  dsl 	push	$0x1000			/* Read to 0x10000:0 */
    138  1.1  dsl 	pop	%es			/* Which we need in %es later */
    139  1.1  dsl 	push	%es
    140  1.2  dsl 	push	%cs			/* Offset zero */
    141  1.1  dsl 
    142  1.1  dsl /* Convert the root directory size to sectors */
    143  1.1  dsl 	push	%dx
    144  1.1  dsl 	mov	bpb_root_dir_ents(%bp), %ax
    145  1.1  dsl 	mov	$0x20, %dx
    146  1.1  dsl 	mul	%dx
    147  1.1  dsl 	divw	bpb_bytes_per_sec(%bp)
    148  1.1  dsl 	add	$0xffff, %dx		/* Set carry if remainder non-zero */
    149  1.2  dsl 	adc	$0, %ax			/* and round up the division */
    150  1.1  dsl 	pop	%dx
    151  1.1  dsl 
    152  1.1  dsl /* Read in the entire root directory */
    153  1.1  dsl 	push	%ax			/* Sectors in root directory */
    154  1.1  dsl 	cwtl
    155  1.1  dsl 	addl	%eax, %edx		/* %edx now sector of first file */
    156  1.1  dsl 	call	read_lba		/* Read entire directory */
    157  1.1  dsl 
    158  1.1  dsl /* Scan directory for our file */
    159  1.1  dsl 	xor	%di, %di
    160  1.1  dsl scan_dir:
    161  1.1  dsl 	mov	$boot_filename, %si
    162  1.1  dsl 	mov	$11, %cx
    163  1.1  dsl 	repz cmpsb
    164  1.1  dsl 	je	found_boot
    165  1.1  dsl 	or	$31,%di
    166  1.1  dsl 	inc	%di
    167  1.1  dsl 	cmp	%ch, %es:(%di)		/* %ch is zero - test end of dir */
    168  1.1  dsl 	jz	1f
    169  1.1  dsl 	decw	bpb_root_dir_ents(%bp)
    170  1.1  dsl 	jnz	scan_dir
    171  1.1  dsl 1:	set_err(ERR_NO_BOOT)
    172  1.1  dsl 
    173  1.1  dsl error:
    174  1.1  dsl #ifdef TERSE_ERROR
    175  1.1  dsl 	movb	%al, errcod
    176  1.1  dsl 	movw	$errtxt, %si
    177  1.1  dsl 	call	message
    178  1.1  dsl #else
    179  1.1  dsl 	push	%ax
    180  1.1  dsl 	movw	$errtxt, %si
    181  1.1  dsl 	call	message
    182  1.1  dsl 	pop	%si
    183  1.1  dsl 	call	message
    184  1.1  dsl 	movw	$newline, %si
    185  1.1  dsl 	call	message
    186  1.1  dsl #endif
    187  1.1  dsl 1:	sti
    188  1.1  dsl 	hlt
    189  1.1  dsl 	jmp	1b
    190  1.1  dsl 
    191  1.1  dsl found_boot:
    192  1.1  dsl 	movzbl	bpb_sec_per_clust(%bp), %eax
    193  1.1  dsl 	movw	%ax, sec_p_cl_w(%bp)
    194  1.1  dsl 	add	%ax, %ax
    195  1.1  dsl 	subl	%eax, %edx		/* 1st file sector is cluster 2 */
    196  1.1  dsl 1:	inc	%cl			/* Convert power of 2 ... */
    197  1.1  dsl 	shr	$1, %ax			/* ... to shift */
    198  1.1  dsl 	jnz	1b
    199  1.1  dsl 	dec %cx
    200  1.1  dsl 	dec %cx
    201  1.1  dsl 	movw	%es:15(%di), %ax	/* Cluster number for file start */
    202  1.1  dsl 	push	%es			/* We increment the 'segment' ... */
    203  1.1  dsl 	pop	%di			/* ... after each read, offset is 0 */
    204  1.1  dsl 
    205  1.1  dsl read_data_block:
    206  1.1  dsl 	mov	%ax, %bx		/* Save cluster number */
    207  1.1  dsl 	shl	%cl, %eax		/* Convert to sector number */
    208  1.1  dsl 	add	%eax, %edx
    209  1.1  dsl 	pushl	%edx			/* Sector to read */
    210  1.1  dsl 	sub	%eax, %edx		/* Recover base segment */
    211  1.1  dsl 	push	%di			/* Target address segment! */
    212  1.1  dsl 	push	$0
    213  1.1  dsl 	push	sec_p_cl_w(%bp)
    214  1.1  dsl 	call	read_lba		/* Read a cluster */
    215  1.1  dsl 
    216  1.1  dsl /* Update read ptr for next cluster */
    217  1.1  dsl 	mov	bpb_bytes_per_sec(%bp), %ax
    218  1.1  dsl 	shr	$4, %ax			/* x86 segment count */
    219  1.1  dsl 	shl	%cl, %ax		/* for a cluster */
    220  1.1  dsl 	add	%ax, %di
    221  1.1  dsl 
    222  1.1  dsl /* Lookup FAT slot number in FAT table */
    223  1.1  dsl 	mov	%bx, %ax		/* Recover cluster number */
    224  1.1  dsl 	push	%dx
    225  1.1  dsl 	xor	%dx, %dx
    226  1.1  dsl 	divw	bpb_bytes_per_sec(%bp)
    227  1.1  dsl 	mov	%dx, %bx		/* Entry in FAT block */
    228  1.1  dsl 	pop	%dx
    229  1.1  dsl 	cmp	%ax, fat_cache
    230  1.1  dsl 	je	lookup_fat
    231  1.1  dsl 
    232  1.1  dsl /* We must read a different chuck of the FAT */
    233  1.1  dsl 	mov	%ax, fat_cache
    234  1.1  dsl 	cwtl
    235  1.1  dsl 	shl	$1, %ax
    236  1.1  dsl 	addl	fat_sector(%bp), %eax
    237  1.1  dsl 	push	%eax
    238  1.1  dsl 	push	%ds
    239  1.1  dsl 	push	$fat_buffer
    240  1.1  dsl 	push	$2			/* Always read 2 sectors of FAT */
    241  1.1  dsl 	call	read_lba
    242  1.1  dsl 
    243  1.1  dsl /* Now use low part of cluster number to index FAT sector */
    244  1.1  dsl lookup_fat:
    245  1.1  dsl 	add	%bx, %bx		/* 2 bytes per entry... */
    246  1.1  dsl 	movzwl	fat_buffer(%bx), %eax	/* Next FAT slot */
    247  1.1  dsl 	cmp	$0xfff0, %ax
    248  1.1  dsl 	jb	read_data_block
    249  1.1  dsl 
    250  1.1  dsl /* Found end of FAT chain - must be EOF  - leap into loaded code */
    251  1.1  dsl 	mov	$0x1000, %ax
    252  1.1  dsl 	mov	%ax, %es
    253  1.1  dsl 	cmpl	$X86_BOOT_MAGIC_2, %es:4
    254  1.1  dsl 	je	magic_ok
    255  1.1  dsl 	set_err(ERR_NO_BOOT_MAGIC_2)
    256  1.1  dsl err1:	jmp	error
    257  1.1  dsl 
    258  1.1  dsl /* Set parameters expected by /boot */
    259  1.1  dsl magic_ok:
    260  1.1  dsl 	mov	bpb_hidden_secs(%bp), %ebx	/* ptn base sector */
    261  1.1  dsl 	movb	-2(%bp), %dl		/* disk number (could pop %dx) */
    262  1.1  dsl 	mov	$boot_params + 4, %si
    263  1.1  dsl 	push	%es
    264  1.1  dsl 	push	$0
    265  1.1  dsl 	lret
    266  1.1  dsl 
    267  1.1  dsl /* Read disk using on-stack int13-extension parameter block */
    268  1.1  dsl read_lba:
    269  1.1  dsl 	pop	%ax			/* Save rtn addr */
    270  1.1  dsl 	pushw	$16			/* Stack ctl block length */
    271  1.1  dsl 	mov	%sp, %si		/* Address ctl block */
    272  1.1  dsl 	push	%ax			/* restack rtn addr */
    273  1.2  dsl 	pushal				/* Save everything except %si and %ax*/
    274  1.1  dsl 	mov	-2(%bp), %dl		/* Disk # saved on entry */
    275  1.1  dsl 	movb	$0x42, %ah
    276  1.1  dsl 	int	$0x13
    277  1.1  dsl 	popal
    278  1.1  dsl 
    279  1.1  dsl 	set_err(ERR_READ)
    280  1.1  dsl 	jc	err1
    281  1.1  dsl 	ret	$12			/* Discard all except high LBA zeros */
    282  1.1  dsl 
    283  1.1  dsl /*
    284  1.1  dsl  * I hate #including source files, but pbr_magic below has to be at
    285  1.1  dsl  * the correct absolute address.
    286  1.1  dsl  * Clearly this could be done with a linker script.
    287  1.1  dsl  */
    288  1.1  dsl 
    289  1.1  dsl #include <message.S>
    290  1.1  dsl #if 0
    291  1.1  dsl #include <dump_eax.S>
    292  1.1  dsl #endif
    293  1.1  dsl 
    294  1.1  dsl errtxt: .ascii	"Error "		/* runs into newline... */
    295  1.1  dsl errcod: .byte	0			/* ... if errcod set */
    296  1.1  dsl newline:
    297  1.1  dsl 	.asciz	"\r\n"
    298  1.1  dsl 
    299  1.1  dsl #ifndef TERSE_ERROR
    300  1.1  dsl ERR_READ:		.asciz	"Disk read"
    301  1.1  dsl ERR_NO_BOOT:		.asciz	"No /boot"
    302  1.1  dsl ERR_NOT_FAT16:		.asciz	"Not FAT16 ptn"
    303  1.1  dsl ERR_NO_BOOT_MAGIC_2:	.asciz	"No magic in /boot"
    304  1.1  dsl #endif
    305  1.1  dsl 
    306  1.1  dsl boot_filename: .ascii	"BOOT       "
    307  1.1  dsl 
    308  1.1  dsl space:
    309  1.1  dsl pbr_space = boot_params - .
    310  1.1  dsl 
    311  1.1  dsl /*
    312  1.1  dsl  * Add magic number, with a zero sized patchable area - just in case something
    313  1.1  dsl  * finds it and tries to update the area.
    314  1.1  dsl  * Boot options can be set using 'installboot -e boot' so we don't need to
    315  1.1  dsl  * use any of our valuable bytes.
    316  1.1  dsl  */
    317  1.1  dsl 
    318  1.1  dsl 	. = _C_LABEL(start) + 0x1fe - 2 - 4 - 4
    319  1.1  dsl boot_params:
    320  1.1  dsl 	.long	X86_BOOT_MAGIC_FAT
    321  1.1  dsl 	.long	1f - .
    322  1.1  dsl 1:
    323  1.1  dsl fat_cache:	.word	0xffff		/* Sector number in buffer */
    324  1.1  dsl 	. = _C_LABEL(start) + 0x1fe
    325  1.1  dsl 	.word	0xaa55
    326  1.1  dsl fat_buffer:				/* 2 sectors worth of FAT table */
    327