Home | History | Annotate | Line # | Download | only in bootxx
pbr.S revision 1.1
      1  1.1  uwe /*	$NetBSD: pbr.S,v 1.1 2006/09/01 21:26:19 uwe Exp $	*/
      2  1.1  uwe 
      3  1.1  uwe /*-
      4  1.1  uwe  * Copyright (c) 2005 NONAKA Kimihiro
      5  1.1  uwe  * All rights reserved.
      6  1.1  uwe  *
      7  1.1  uwe  * Redistribution and use in source and binary forms, with or without
      8  1.1  uwe  * modification, are permitted provided that the following conditions
      9  1.1  uwe  * are met:
     10  1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     11  1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     12  1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  uwe  *    documentation and/or other materials provided with the distribution.
     15  1.1  uwe  *
     16  1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  uwe  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  uwe  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  uwe  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  1.1  uwe  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  uwe  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  uwe  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  uwe  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  uwe  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  uwe  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  uwe  * SUCH DAMAGE.
     27  1.1  uwe  */
     28  1.1  uwe 
     29  1.1  uwe #include <machine/asm.h>
     30  1.1  uwe #include <sys/bootblock.h>
     31  1.1  uwe 
     32  1.1  uwe #ifdef BOOT_FROM_FAT
     33  1.1  uwe #define MBR_AFTERBPB	90		/* BPB size in FAT32 partition BR */
     34  1.1  uwe #else
     35  1.1  uwe #define MBR_AFTERBPB	62		/* BPB size in floppy master BR */
     36  1.1  uwe #endif
     37  1.1  uwe 
     38  1.1  uwe ENTRY(start)
     39  1.1  uwe 	bra	start0
     40  1.1  uwe 	 .byte	0x11			/* 0x4e11: cmp/pz r14... */
     41  1.1  uwe 	.ascii	"NetBSD20"
     42  1.1  uwe 
     43  1.1  uwe 	. = _C_LABEL(start) + MBR_BPB_OFFSET	/* move to start of BPB */
     44  1.1  uwe 
     45  1.1  uwe 	. = _C_LABEL(start) + MBR_AFTERBPB	/* skip BPB */
     46  1.1  uwe start0:
     47  1.1  uwe 	mova	mbr_end, r0
     48  1.1  uwe 	mov.w	mbr_size, r2
     49  1.1  uwe 	sub	r2, r0
     50  1.1  uwe 	mov	r0, r11			/* r11: own loaded address */
     51  1.1  uwe 
     52  1.1  uwe 	mov.w	stack_offset, r1
     53  1.1  uwe 	add	r1, r0
     54  1.1  uwe 	mov	r0, r15			/* r15: stack pointer */
     55  1.1  uwe 	mov	r0, r10			/* r10: load address */
     56  1.1  uwe 
     57  1.1  uwe 	/* enable/flush cache */
     58  1.1  uwe 	mov	#0, r4
     59  1.1  uwe 	mov	#6, r0
     60  1.1  uwe 	trapa	#0x3f
     61  1.1  uwe 
     62  1.1  uwe 	/* Read from start of disk */
     63  1.1  uwe 	mov	#0x40, r4		/* LBA */
     64  1.1  uwe 	mov	#0, r5			/* LBA #0 */
     65  1.1  uwe 	mov	r10, r6			/* buffer address */
     66  1.1  uwe 	bsr	read_sectors_lba
     67  1.1  uwe 	 mov	#BOOTXX_SECTORS, r7	/* number of sectors */
     68  1.1  uwe 
     69  1.1  uwe 	mov.l	@r11, r1
     70  1.1  uwe 	mov.l	@r10, r2
     71  1.1  uwe 	cmp/eq	r1, r2
     72  1.1  uwe 	bt/s	pbr_read_ok
     73  1.1  uwe 	 mov	#0, r9			/* r9: sector # */
     74  1.1  uwe 
     75  1.1  uwe 	/* Search bootable partition */
     76  1.1  uwe 	mov.w	part_offset, r12
     77  1.1  uwe 	add	r10, r12		/* r12: pointer to partition entry */
     78  1.1  uwe 	mov	#MBR_PART_COUNT, r8	/* r8: partition loop counter */
     79  1.1  uwe loop_part:
     80  1.1  uwe 	mov.b	@(4, r12), r0
     81  1.1  uwe #ifdef BOOT_FROM_FAT
     82  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT12, r0
     83  1.1  uwe 	bt	found
     84  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT16S, r0
     85  1.1  uwe 	bt	found
     86  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT16B, r0
     87  1.1  uwe 	bt	found
     88  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT32, r0
     89  1.1  uwe 	bt	found
     90  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT32L, r0
     91  1.1  uwe 	bt	found
     92  1.1  uwe 	cmp/eq	#MBR_PTYPE_FAT16L, r0
     93  1.1  uwe 	bt	found
     94  1.1  uwe #else
     95  1.1  uwe 	cmp/eq	#MBR_PTYPE_NETBSD, r0
     96  1.1  uwe #endif
     97  1.1  uwe 	bf	next_part
     98  1.1  uwe 
     99  1.1  uwe found:
    100  1.1  uwe 	/* found boot partition */
    101  1.1  uwe 	mov.w	@(8, r12), r0
    102  1.1  uwe 	mov	r0, r1
    103  1.1  uwe 	mov.w	@(10, r12), r0
    104  1.1  uwe 	extu.w	r1, r1
    105  1.1  uwe 	shll16	r0
    106  1.1  uwe 	or	r1, r0
    107  1.1  uwe 	tst	r0, r0
    108  1.1  uwe 	bt	next_part		/* start LBA == 0 ? */
    109  1.1  uwe 
    110  1.1  uwe 	bra	boot_lba
    111  1.1  uwe 	 mov	r0, r9
    112  1.1  uwe 
    113  1.1  uwe next_part:
    114  1.1  uwe 	dt	r8
    115  1.1  uwe 	bf/s	loop_part
    116  1.1  uwe 	 add	#16, r12
    117  1.1  uwe 
    118  1.1  uwe ptn_error:
    119  1.1  uwe 	/* Not found NetBSD partition */
    120  1.1  uwe 	mova	ERR_PTN, r0
    121  1.1  uwe error:
    122  1.1  uwe 	bsr	message_crlf
    123  1.1  uwe 	 mov	r0, r4
    124  1.1  uwe 99:	bra	99b
    125  1.1  uwe 	 nop
    126  1.1  uwe 
    127  1.1  uwe read_error:
    128  1.1  uwe 	bra	error
    129  1.1  uwe 	 mova	ERR_READ, r0
    130  1.1  uwe 
    131  1.1  uwe magic_error:
    132  1.1  uwe 	bra	error
    133  1.1  uwe 	 mova	ERR_NO_BOOTXX, r0
    134  1.1  uwe 
    135  1.1  uwe message_crlf:
    136  1.1  uwe 	mov	#32, r0
    137  1.1  uwe 	trapa	#0x3f
    138  1.1  uwe 	mova	crlf, r0
    139  1.1  uwe 	mov	r0, r4
    140  1.1  uwe 	mov	#32, r0
    141  1.1  uwe 	trapa	#0x3f
    142  1.1  uwe 	rts
    143  1.1  uwe 	 nop
    144  1.1  uwe 
    145  1.1  uwe read_sectors_lba:
    146  1.1  uwe 	mov	#2, r0
    147  1.1  uwe 	trapa	#0x3f
    148  1.1  uwe 	tst	r0, r0
    149  1.1  uwe 	bf	read_error
    150  1.1  uwe 	rts
    151  1.1  uwe 	 nop
    152  1.1  uwe 
    153  1.1  uwe boot_lba:
    154  1.1  uwe 	mov	#0x40, r4		/* LBA */
    155  1.1  uwe 	mov	r9, r5			/* LBA # */
    156  1.1  uwe 	mov	r10, r6			/* buffer address */
    157  1.1  uwe 	bsr	read_sectors_lba
    158  1.1  uwe 	 mov	#BOOTXX_SECTORS, r7	/* number of sectors */
    159  1.1  uwe 
    160  1.1  uwe pbr_read_ok:
    161  1.1  uwe 	mov.l	.L.bootxx_magic1, r1
    162  1.1  uwe 	mov.l	.L.bootxx_magic, r2
    163  1.1  uwe 	mov.l	@r2, r2
    164  1.1  uwe 	cmp/eq	r1, r2
    165  1.1  uwe 	bf	magic_error
    166  1.1  uwe 
    167  1.1  uwe 	/* flush cache */
    168  1.1  uwe 	mov	#0, r4
    169  1.1  uwe 	mov	#6, r0
    170  1.1  uwe 	trapa	#0x3f
    171  1.1  uwe 
    172  1.1  uwe 	mov.l	.L.bootxx_start, r13
    173  1.1  uwe 	jmp	@r13			/* jump to bootxx */
    174  1.1  uwe 	 mov	r9, r4			/* pass sector address to bootxx */
    175  1.1  uwe 
    176  1.1  uwe 
    177  1.1  uwe 	.align	1
    178  1.1  uwe mbr_size:	.word	mbr_end - _C_LABEL(start)
    179  1.1  uwe 	.align	1
    180  1.1  uwe stack_offset:	.word	0x1000
    181  1.1  uwe 	.align	1
    182  1.1  uwe part_offset:	.word	MBR_PART_OFFSET
    183  1.1  uwe 	.align	1
    184  1.1  uwe magic_offset:	.word	MBR_MAGIC_OFFSET
    185  1.1  uwe 
    186  1.1  uwe 	.align	2
    187  1.1  uwe .L.bootxx_magic1:
    188  1.1  uwe 	.long	LANDISK_BOOT_MAGIC_1
    189  1.1  uwe .L.bootxx_magic:
    190  1.1  uwe 	.long	_C_LABEL(bootxx_magic)
    191  1.1  uwe .L.bootxx_start:
    192  1.1  uwe 	.long	_C_LABEL(bootxx_start)
    193  1.1  uwe 
    194  1.1  uwe 	.align	2
    195  1.1  uwe crlf:		.asciz	"\r\n"
    196  1.1  uwe 
    197  1.1  uwe 	.align	2
    198  1.1  uwe ERR_READ:	.asciz  "Disk read"
    199  1.1  uwe 	.align	2
    200  1.1  uwe ERR_NO_BOOTXX:	.asciz  "Not a bootxx image"
    201  1.1  uwe 	.align	2
    202  1.1  uwe ERR_PTN:	.asciz  "No NetBSD partition"
    203  1.1  uwe 
    204  1.1  uwe 
    205  1.1  uwe /* space for mbr_dsn */
    206  1.1  uwe 	. = _C_LABEL(start) + MBR_DSN_OFFSET
    207  1.1  uwe 	.long	0
    208  1.1  uwe 
    209  1.1  uwe /* mbr_bootsel_magic */
    210  1.1  uwe 	. = _C_LABEL(start) + MBR_BS_MAGIC_OFFSET
    211  1.1  uwe 	.word	0
    212  1.1  uwe 
    213  1.1  uwe /*
    214  1.1  uwe  * MBR partition table
    215  1.1  uwe  */
    216  1.1  uwe 	. = _C_LABEL(start) + MBR_PART_OFFSET
    217  1.1  uwe _pbr_part0:
    218  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    219  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    220  1.1  uwe _pbr_part1:
    221  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    222  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    223  1.1  uwe _pbr_part2:
    224  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    225  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    226  1.1  uwe _pbr_part3:
    227  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    228  1.1  uwe 	.byte	0, 0, 0, 0, 0, 0, 0, 0
    229  1.1  uwe 
    230  1.1  uwe 	. = _C_LABEL(start) + MBR_MAGIC_OFFSET
    231  1.1  uwe magic:
    232  1.1  uwe 	.word	MBR_MAGIC
    233  1.1  uwe mbr_end:
    234