cdboot.S revision 1.3 1 /* $NetBSD: cdboot.S,v 1.3 2005/07/06 18:12:31 junyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bang Jun-Young.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * This is a primary boot loader that loads a secondary boot loader
41 * directly from CD without performing floppy/hard disk emulation as
42 * described by the El Torito specification.
43 *
44 * TODO:
45 * - Support for loading secondary boot loader > 64kB
46 */
47
48 #include <machine/asm.h>
49
50 #define BOOT_ADDR 0x7c00
51 #define BLOCK_SIZE 2048 /* Default for ISO 9660 */
52 #define VD_LBA 16 /* LBA of Volume Descriptor (VD) */
53 #define PVD_ADDR 0x1000 /* Where Primary VD is loaded */
54 #define ROOTDIR_ADDR 0x1800 /* Where Root Directory is loaded */
55 #define LOADER_ADDR SECONDARY_LOAD_ADDRESS
56
57 /*
58 * See src/sys/sys/bootblock.h for details.
59 */
60 #define MBR_PART_COUNT 4
61 #define MBR_PART_OFFSET 446
62 #define MBR_PART_SIZE 16 /* sizeof(struct mbr_partition) */
63
64 /*
65 * Disk error codes
66 */
67 #define ERROR_TIMEOUT 0x80
68
69 /*
70 * Volume Descriptor types.
71 */
72 #define VD_PRIMARY 1
73 #define VD_SUPPLEMENTARY 2
74 #define VD_TERMINATOR 255
75
76 /* Only actually used entries are listed below */
77
78 /*
79 * Format of Primary Volume Descriptor (8.4)
80 */
81 #define PVD_ROOT_DR 156 /* Offset of Root Directory Record */
82
83 /*
84 * Format of Directory Record (9.1)
85 */
86 #define DR_LEN 0
87 #define DR_EXTENT 2
88 #define DR_DATA_LEN 10
89 #define DR_NAME_LEN 32
90 #define DR_NAME 33
91
92 .text
93 .code16
94 ENTRY(start)
95 xorw %ax, %ax
96 movw %ax, %ds
97 movw %ax, %es
98 movw %ax, %ss
99 movw $BOOT_ADDR, %sp
100 movw %sp, %si
101 movw $start, %di
102 movw $BLOCK_SIZE/2, %cx
103 rep
104 movsw
105 ljmp $0, $real_start
106
107 real_start:
108 movb %dl, boot_drive /* Save boot drive number */
109
110 /*
111 * We can skip boot wait when:
112 * - there's no hard disk present.
113 * - there's no active partition in the MBR of the 1st hard disk.
114 */
115
116 /*
117 * Check presence of hard disks.
118 */
119 movw $0x475, %si
120 movb (%si), %al
121 testb %al, %al
122 jz boot_cdrom
123
124 /*
125 * Find the active partition from the MBR.
126 */
127 movw $0x0201, %ax /* %al = number of sectors to read */
128 movw $BOOT_ADDR, %bx /* %es:%bx = data buffer */
129 movw $0x0001, %cx /* %ch = low 8 bits of cylinder no */
130 /* %cl = high 2 bits of cyl no & */
131 /* sector number */
132 movw $0x0080, %dx /* %dh = head number */
133 /* %dl = disk number */
134 int $0x13 /* Read MBR into memory */
135 jc boot_cdrom /* CF set on error */
136
137 movb $1, mbr_loaded
138 movb $MBR_PART_COUNT, %cl
139 movw $BOOT_ADDR+MBR_PART_OFFSET, %si
140 1:
141 movb (%si), %al
142 testb $0x80, %al
143 jnz found_active
144 addw $MBR_PART_SIZE, %si
145 decb %cl
146 testb %cl, %cl
147 jnz 1b /* If 0, no active partition found */
148 jmp boot_cdrom
149
150 found_active:
151 movw $str_press_key, %si
152 call message
153 next_second:
154 movw $str_dot, %si
155 call message
156 decb wait_count
157 jz boot_hard_disk
158 xorb %ah, %ah /* Get system time */
159 int $0x1a
160 movw %dx, %di /* %cx:%dx = number of clock ticks */
161 addw $19, %di /* 19 ~= 18.2 Hz */
162 wait_key:
163 movb $1, %ah /* Check for keystroke */
164 int $0x16
165 jz not_avail /* ZF clear if keystroke available */
166 xorb %ah, %ah /* Read key to flush keyboard buf */
167 int $0x16
168 jmp boot_cdrom
169 not_avail:
170 xorb %ah, %ah /* Get system time */
171 int $0x1a
172 cmpw %dx, %di /* Compare with saved time */
173 jnz wait_key
174 jmp next_second
175
176 boot_hard_disk:
177 movw $str_crlf, %si
178 call message
179 cmpb $1, mbr_loaded
180 jz 1f
181 movw $0x0201, %ax /* %al = number of sectors to read */
182 movw $BOOT_ADDR, %bx /* %es:%bx = data buffer */
183 movw $0x0001, %cx /* %ch = low 8 bits of cylinder no */
184 /* %cl = high 2 bits of cyl no & */
185 /* sector number */
186 movw $0x0080, %dx /* %dh = head number */
187 /* %dl = disk number */
188 int $0x13 /* Read MBR into memory */
189 jc panic /* CF set on error */
190 1:
191 movw %cs, %ax /* Restore initial state */
192 movw %ax, %ds
193 movw %ax, %es
194 movw $0x0080, %dx /* %dl = boot drive number */
195 jmp $0, $BOOT_ADDR /* Jump to MBR! */
196
197 panic:
198 hlt
199 jmp panic
200
201 boot_cdrom:
202 movw $str_banner, %si
203 call message
204 movl $VD_LBA, %eax
205 next_block:
206 movb $1, %dh /* Number of sectors to read */
207 movl $PVD_ADDR, %ebx
208 call read_sectors
209 cmpb $VD_PRIMARY, (%bx) /* Is it Primary Volume Descriptor? */
210 jz pvd_found
211 incl %eax
212 cmpb $VD_TERMINATOR, (%bx)
213 jnz next_block
214 movw $str_no_pvd, %si
215 call message
216 jmp panic
217
218 pvd_found:
219 movw $PVD_ADDR+PVD_ROOT_DR, %bx
220 movl DR_EXTENT(%bx), %eax /* LBA of the root directory */
221 movl DR_DATA_LEN(%bx), %edx
222 shrl $11, %edx /* Convert to number of sectors */
223 movb %dl, %dh /* ... and load it to %dh */
224 movl $ROOTDIR_ADDR, %ebx
225 call read_sectors
226 next_entry:
227 cmpb $0, DR_LEN(%bx)
228 jz last_entry
229 movw %bx, %si
230 addw $DR_NAME, %si
231 movb DR_NAME_LEN(%bx), %cl
232 movw $str_loader, %di
233 1:
234 movb (%si), %al
235 cmpb %al, (%di)
236 jnz fail
237 incw %si
238 incw %di
239 decb %cl
240 jnz 1b
241 jmp load_loader
242 fail:
243 addw DR_LEN(%bx), %bx
244 jmp next_entry
245 last_entry:
246 movw $str_no_loader, %si
247 call message
248 jmp panic
249
250 load_loader:
251 movl DR_EXTENT(%bx), %eax
252 movl DR_DATA_LEN(%bx), %edx
253 addl $(BLOCK_SIZE-1), %edx /* Convert file length to */
254 shrl $11, %edx /* ... number of sectors */
255 movb %dl, %dh
256 movl $LOADER_ADDR, %ebx
257 call read_sectors
258 xorl %esi, %esi /* Don't provide boot_params */
259 xorl %edx, %edx
260 movb boot_drive, %dl
261 xorl %ebx, %ebx /* Zero sector number */
262 lcall $LOADER_ADDR/16, $0
263 jmp panic
264
265 /*
266 * Read disk sector(s) into memory
267 *
268 * %eax = LBA of starting sector
269 * %ebx = buffer to store sectors
270 * %dh = number of sectors to read
271 */
272 read_sectors:
273 pusha
274 movl %eax, edd_lba /* Convert LBA to segment */
275 shrl $4, %ebx
276 movw %bx, edd_segment
277 movb %dh, edd_nsecs
278 movb boot_drive, %dl
279 movw $edd_packet, %si
280 read_again:
281 movb $0x42, %ah
282 int $0x13
283 jc read_fail
284 popa
285 ret
286 read_fail:
287 cmpb $ERROR_TIMEOUT, %ah
288 jz read_again
289 movw $str_read_error, %si
290 call message
291 jmp panic
292
293 /*
294 * For debugging purpose
295 */
296 put_char:
297 pusha
298 movb $0x0e, %ah
299 movw $0x0001, %bx
300 int $0x10
301 popa
302 ret
303
304 #include <message.S>
305
306 edd_packet:
307 edd_len: .word 16
308 edd_nsecs: .word 0 /* Number of sectors to transfer */
309 edd_offset: .word 0
310 edd_segment: .word 0
311 edd_lba: .quad 0
312
313 wait_count: .byte 6
314 boot_drive: .byte 0
315 mbr_loaded: .byte 0
316
317 str_banner: .ascii "\r\nNetBSD/i386 cd9660 Primary Bootstrap"
318 str_crlf: .asciz "\r\n"
319 str_press_key: .asciz "\r\nPress any key to boot from CD"
320 str_dot: .asciz "."
321 str_read_error: .asciz "Can't read CD"
322 str_no_pvd: .asciz "Can't find Primary Volume Descriptor"
323 str_no_loader: .asciz "Can't find /boot"
324 str_loader: .asciz "BOOT.;1"
325
326 /* Used to calculate free bytes */
327 free_space = end - .
328
329 . = start + BLOCK_SIZE
330 end:
331