1 /* $NetBSD: cdboot.S,v 1.12 2011/01/04 16:53:05 jakllsch Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * This is a primary boot loader that loads a secondary boot loader
34 * directly from CD without performing floppy/hard disk emulation as
35 * described by the El Torito specification.
38 #include <machine/asm.h>
39 #include <sys/bootblock.h>
41 #define BOOT_ADDR 0x7c00
42 #define BLOCK_SIZE 2048 /* Default for ISO 9660 */
43 #define VD_LBA 16 /* LBA of Volume Descriptor (VD) */
44 #define PVD_ADDR end /* Where Primary VD is loaded */
45 #define ROOTDIR_ADDR end+BLOCK_SIZE /* Where Root Directory is loaded */
46 #define LOADER_ADDR SECONDARY_LOAD_ADDRESS
49 #define MBR_AFTERBPB 90 /* BPB size in FAT32 partition BR */
51 #define MBR_AFTERBPB 62 /* BPB size in floppy master BR */
55 * See src/sys/sys/bootblock.h for details.
57 #define MBR_PART_COUNT 4
58 #define MBR_PART_OFFSET 446
59 #define MBR_PART_SIZE 16 /* sizeof(struct mbr_partition) */
64 #define ERROR_TIMEOUT 0x80
67 * Volume Descriptor types.
70 #define VD_SUPPLEMENTARY 2
71 #define VD_TERMINATOR 255
73 /* Only actually used entries are listed below */
76 * Format of Primary Volume Descriptor (8.4)
78 #define PVD_ROOT_DR 156 /* Offset of Root Directory Record */
81 * Format of Directory Record (9.1)
85 #define DR_DATA_LEN 10
86 #define DR_NAME_LEN 32
94 . = start + MBR_AFTERBPB /* skip BPB */
95 . = start + MBR_DSN_OFFSET
98 /* mbr_bootsel_magic (not used here) */
99 . = start + MBR_BS_MAGIC_OFFSET
102 . = start + MBR_PART_OFFSET
103 . = start + MBR_MAGIC_OFFSET
106 .fill 512 /* reserve space for disklabel */
110 .long X86_BOOT_MAGIC_1 /* checked by installboot & pbr code */
111 boot_params: /* space for patchable variables */
112 .long 1f - boot_params /* length of this data area */
113 #include <boot_params.S>
114 . = start1 + 0x80 /* Space for patching unknown params */
123 movw $BLOCK_SIZE/2, %cx
129 movb %dl, boot_drive /* Save boot drive number */
131 #ifndef DISABLE_KEYPRESS
133 * We can skip boot wait when:
134 * - there's no hard disk present.
135 * - there's no active partition in the MBR of the 1st hard disk.
139 * Check presence of hard disks.
147 * Find the active partition from the MBR.
149 movw $0x0201, %ax /* %al = number of sectors to read */
150 movw $BOOT_ADDR, %bx /* %es:%bx = data buffer */
151 movw $0x0001, %cx /* %ch = low 8 bits of cylinder no */
152 /* %cl = high 2 bits of cyl no & */
154 movw $0x0080, %dx /* %dh = head number */
155 /* %dl = disk number */
156 int $0x13 /* Read MBR into memory */
157 jc boot_cdrom /* CF set on error */
160 movb $MBR_PART_COUNT, %cl
161 movw $BOOT_ADDR+MBR_PART_OFFSET, %si
166 addw $MBR_PART_SIZE, %si
169 jnz 1b /* If 0, no active partition found */
173 movw $str_press_key, %si
180 xorb %ah, %ah /* Get system time */
182 movw %dx, %di /* %cx:%dx = number of clock ticks */
183 addw $19, %di /* 19 ~= 18.2 Hz */
185 movb $1, %ah /* Check for keystroke */
187 jz not_avail /* ZF clear if keystroke available */
188 xorb %ah, %ah /* Read key to flush keyboard buf */
192 xorb %ah, %ah /* Get system time */
194 cmpw %dx, %di /* Compare with saved time */
203 movw $0x0201, %ax /* %al = number of sectors to read */
204 movw $BOOT_ADDR, %bx /* %es:%bx = data buffer */
205 movw $0x0001, %cx /* %ch = low 8 bits of cylinder no */
206 /* %cl = high 2 bits of cyl no & */
208 movw $0x0080, %dx /* %dh = head number */
209 /* %dl = disk number */
210 int $0x13 /* Read MBR into memory */
211 jc panic /* CF set on error */
213 movw %cs, %ax /* Restore initial state */
216 movw $0x0080, %dx /* %dl = boot drive number */
217 jmp $0, $BOOT_ADDR /* Jump to MBR! */
218 jmp panic /* This should be never executed */
219 #endif /* !DISABLE_KEYPRESS */
222 movw $str_banner, %si
225 /* Read volume descriptor sectors until Primary decriptor found */
228 movb $1, %dh /* Number of sectors to read */
231 cmpb $VD_PRIMARY, (%bx) /* Is it Primary Volume Descriptor? */
234 cmpb $VD_TERMINATOR, (%bx)
236 movw $str_no_pvd, %si
240 /* Read all of root directory */
242 movw $PVD_ADDR+PVD_ROOT_DR, %bx
243 movl DR_EXTENT(%bx), %eax /* LBA of the root directory */
244 movl DR_DATA_LEN(%bx), %edx
245 shrl $11, %edx /* Convert to number of sectors */
246 movb %dl, %dh /* ... and load it to %dh */
247 movl $ROOTDIR_ADDR, %ebx
250 /* Scan directory entries searching for /boot */
256 movb DR_NAME_LEN(%bx), %cl
257 movw $str_loader, %di
268 addw DR_LEN(%bx), %bx
271 movw $str_no_loader, %si
275 /* Found /boot, read contents to 0x1000:0 */
277 movl DR_EXTENT(%bx), %eax
278 movl DR_DATA_LEN(%bx), %edx
279 addl $(BLOCK_SIZE-1), %edx /* Convert file length to */
280 shrl $11, %edx /* ... number of sectors */
282 movl $LOADER_ADDR, %ebx
285 /* Finally call into code of /boot */
286 movl $boot_params, %esi /* Provide boot_params */
289 xorl %ebx, %ebx /* Zero sector number */
290 lcall $LOADER_ADDR/16, $0
291 /* fall through on load failure */
297 * Read disk sector(s) into memory
299 * %eax = LBA of starting sector
300 * %ebx = buffer to store sectors
301 * %dh = number of sectors to read
303 * Long transfers are split onto multiple 64k reads
305 #define MAX_SECTORS (0x10000/BLOCK_SIZE)
309 shrl $4, %ebx /* Convert buffer addr to seg:0 */
310 movw %bx, edd_segment
311 1: movb %dh, edd_nsecs
312 cmpb $MAX_SECTORS, %dh
313 jle 2f /* j if less than 64k */
314 movb $MAX_SECTORS, edd_nsecs /* Read 32 sectors - 64k bytes */
315 2: movb boot_drive, %dl
316 movw $edd_packet, %si
319 push %dx /* bios shouldn't kill %dh, but ... */
321 pop %dx /* ... better safe than sorry! */
323 addw $0x1000, edd_segment /* Advance segment addr by 64k bytes */
324 addl $MAX_SECTORS, edd_lba /* And sector number to match */
325 sub edd_nsecs, %dh /* Number of sectors remaining */
331 cmpb $ERROR_TIMEOUT, %ah
333 movw $str_read_error, %si
341 edd_nsecs: .word 0 /* Number of sectors to transfer */
350 str_banner: .ascii "\r\nNetBSD/x86 cd9660 Primary Bootstrap"
351 str_crlf: .asciz "\r\n"
352 str_press_key: .asciz "\r\nPress any key to boot from CD"
354 str_read_error: .asciz "Can't read CD"
355 str_no_pvd: .asciz "Can't find Primary Volume Descriptor"
356 str_no_loader: .asciz "Can't find /minixboot"
357 str_loader: .asciz "MINIXBOOT.;1"
359 /* Used to calculate free bytes */
362 . = start + BLOCK_SIZE