1 1.2 nonaka /* $NetBSD: mbr.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 ENTRY(start) 32 1.1 uwe mova mbr_end, r0 33 1.1 uwe mov r0, r11 /* r11: relocate address */ 34 1.1 uwe 35 1.1 uwe mov.w mbr_size, r2 36 1.1 uwe sub r2, r0 37 1.1 uwe mov r0, r10 /* r10: loaded address */ 38 1.1 uwe 39 1.1 uwe mov.w stack_offset, r1 40 1.1 uwe add r1, r0 41 1.1 uwe mov r0, r15 /* r15: stack pointer */ 42 1.1 uwe 43 1.1 uwe /* relocate code */ 44 1.1 uwe mova jmp_start, r0 45 1.1 uwe mov r0, r13 46 1.1 uwe add r2, r13 /* calc jump address */ 47 1.1 uwe 48 1.1 uwe mov r10, r0 49 1.1 uwe mov r11, r1 50 1.1 uwe 1: mov.b @r0+, r3 51 1.1 uwe dt r2 52 1.1 uwe mov.b r3, @r1 53 1.1 uwe bf/s 1b 54 1.1 uwe add #1, r1 55 1.1 uwe 56 1.1 uwe jmp @r13 57 1.1 uwe nop 58 1.1 uwe 59 1.1 uwe .align 2 60 1.1 uwe jmp_start: 61 1.1 uwe /* enable cache */ 62 1.1 uwe mov #0, r4 63 1.1 uwe mov #6, r0 64 1.1 uwe trapa #0x3f 65 1.1 uwe 66 1.1 uwe #ifndef NO_BANNER 67 1.1 uwe /* print banner */ 68 1.1 uwe mova banner, r0 69 1.1 uwe bsr message_crlf 70 1.1 uwe mov r0, r4 71 1.1 uwe #endif 72 1.1 uwe 73 1.1 uwe /* search bootable partition */ 74 1.1 uwe mov.w part_offset, r12 75 1.1 uwe add r11, r12 /* r12: pointer to partition entry */ 76 1.1 uwe mov #MBR_PART_COUNT, r8 /* r8: partition loop counter */ 77 1.1 uwe loop_part: 78 1.1 uwe mov.b @(4, r12), r0 79 1.1 uwe cmp/eq #MBR_PTYPE_UNUSED, r0 80 1.1 uwe bt next_part 81 1.1 uwe 82 1.1 uwe /* check active partition */ 83 1.1 uwe mov.b @(0, r12), r0 84 1.1 uwe cmp/eq #0x80, r0 85 1.1 uwe bf next_part 86 1.1 uwe 87 1.1 uwe /* found bootable partition */ 88 1.1 uwe mov.w @(8, r12), r0 /* load unaligned 32bit data */ 89 1.1 uwe mov r0, r1 90 1.1 uwe mov.w @(10, r12), r0 91 1.1 uwe extu.w r1, r1 92 1.1 uwe shll16 r0 93 1.1 uwe or r1, r0 94 1.1 uwe 95 1.1 uwe mov r0, r3 96 1.1 uwe mova found_sector, r0 97 1.1 uwe bra boot_lba 98 1.1 uwe mov.l r3, @r0 99 1.1 uwe 100 1.1 uwe next_part: 101 1.1 uwe dt r8 102 1.1 uwe bf/s loop_part 103 1.1 uwe add #16, r12 104 1.1 uwe 105 1.1 uwe noos_error: 106 1.1 uwe /* Not found bootable partition */ 107 1.1 uwe mova ERR_NOOS, r0 108 1.1 uwe error: 109 1.1 uwe bsr message_crlf 110 1.1 uwe mov r0, r4 111 1.1 uwe 99: bra 99b 112 1.1 uwe nop 113 1.1 uwe 114 1.1 uwe read_error: 115 1.1 uwe bra error 116 1.1 uwe mova ERR_READ, r0 117 1.1 uwe 118 1.1 uwe message_crlf: 119 1.1 uwe mov #32, r0 120 1.1 uwe trapa #0x3f 121 1.1 uwe mova crlf, r0 122 1.1 uwe mov r0, r4 123 1.1 uwe mov #32, r0 124 1.1 uwe trapa #0x3f 125 1.1 uwe rts 126 1.1 uwe nop 127 1.1 uwe 128 1.1 uwe read_sector_lba: 129 1.1 uwe mov #1, r7 130 1.1 uwe mov #2, r0 131 1.1 uwe trapa #0x3f 132 1.1 uwe tst r0, r0 133 1.1 uwe bf read_error 134 1.1 uwe rts 135 1.1 uwe nop 136 1.1 uwe 137 1.1 uwe boot_lba: 138 1.1 uwe /* read PBR sector */ 139 1.1 uwe mova found_sector, r0 140 1.1 uwe mov #0x40, r4 141 1.1 uwe mov.l @r0, r5 142 1.1 uwe bsr read_sector_lba 143 1.1 uwe mov r10, r6 144 1.1 uwe 145 1.1 uwe /* flush cache */ 146 1.1 uwe mov #0, r4 147 1.1 uwe mov #6, r0 148 1.1 uwe trapa #0x3f 149 1.1 uwe 150 1.1 uwe /* check signature */ 151 1.1 uwe mov.b @(0, r10), r0 152 1.1 uwe tst r0, r0 153 1.1 uwe bt noos_error /* first byte non-zero */ 154 1.1 uwe mov.w magic_offset, r0 155 1.1 uwe mov.w @(r0, r10), r1 156 1.1 uwe mov.w magic, r2 157 1.1 uwe cmp/eq r1, r2 158 1.1 uwe bf noos_error /* magic */ 159 1.1 uwe 160 1.1 uwe /* now jump to PBR */ 161 1.1 uwe mov r10, r0 162 1.1 uwe jmp @r10 163 1.1 uwe nop 164 1.1 uwe 165 1.1 uwe 166 1.1 uwe .align 1 167 1.1 uwe mbr_size: .word mbr_end - _C_LABEL(start) 168 1.1 uwe .align 1 169 1.1 uwe stack_offset: .word 0x1000 170 1.1 uwe .align 1 171 1.1 uwe part_offset: .word MBR_PART_OFFSET 172 1.1 uwe .align 1 173 1.1 uwe magic_offset: .word MBR_MAGIC_OFFSET 174 1.1 uwe 175 1.1 uwe .align 2 176 1.1 uwe found_sector: .long 0 177 1.1 uwe 178 1.1 uwe #ifndef NO_BANNER 179 1.1 uwe .align 2 180 1.1 uwe banner: .asciz "\r\nNetBSD MBR boot" 181 1.1 uwe #endif 182 1.1 uwe .align 2 183 1.1 uwe crlf: .asciz "\r\n" 184 1.1 uwe 185 1.1 uwe .align 2 186 1.1 uwe ERR_INVPART: .asciz "No active partition" 187 1.1 uwe .align 2 188 1.1 uwe ERR_READ: .asciz "Disk read error" 189 1.1 uwe .align 2 190 1.1 uwe ERR_NOOS: .asciz "No operating system" 191 1.1 uwe 192 1.1 uwe 193 1.1 uwe /* space for mbr_dsn */ 194 1.1 uwe . = _C_LABEL(start) + MBR_DSN_OFFSET 195 1.1 uwe .long 0 196 1.1 uwe 197 1.1 uwe /* mbr_bootsel_magic */ 198 1.1 uwe . = _C_LABEL(start) + MBR_BS_MAGIC_OFFSET 199 1.1 uwe .word MBR_BS_MAGIC 200 1.1 uwe 201 1.1 uwe /* 202 1.1 uwe * MBR partition table 203 1.1 uwe */ 204 1.1 uwe . = _C_LABEL(start) + MBR_PART_OFFSET 205 1.1 uwe _pbr_part0: 206 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 207 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 208 1.1 uwe _pbr_part1: 209 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 210 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 211 1.1 uwe _pbr_part2: 212 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 213 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 214 1.1 uwe _pbr_part3: 215 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 216 1.1 uwe .byte 0, 0, 0, 0, 0, 0, 0, 0 217 1.1 uwe 218 1.1 uwe . = _C_LABEL(start) + MBR_MAGIC_OFFSET 219 1.1 uwe magic: 220 1.1 uwe .word MBR_MAGIC 221 1.1 uwe mbr_end: 222