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