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