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