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