fatboot.S revision 1.1 1 1.1 dsl /* $NetBSD: fatboot.S,v 1.1 2007/01/01 22:11:09 dsl Exp $ */
2 1.1 dsl
3 1.1 dsl /*-
4 1.1 dsl * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 dsl * All rights reserved.
6 1.1 dsl *
7 1.1 dsl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 dsl * by David Laight.
9 1.1 dsl *
10 1.1 dsl * Redistribution and use in source and binary forms, with or without
11 1.1 dsl * modification, are permitted provided that the following conditions
12 1.1 dsl * are met:
13 1.1 dsl * 1. Redistributions of source code must retain the above copyright
14 1.1 dsl * notice, this list of conditions and the following disclaimer.
15 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dsl * notice, this list of conditions and the following disclaimer in the
17 1.1 dsl * documentation and/or other materials provided with the distribution.
18 1.1 dsl * 3. Neither the name of The NetBSD Foundation nor the names of its
19 1.1 dsl * contributors may be used to endorse or promote products derived
20 1.1 dsl * from this software without specific prior written permission.
21 1.1 dsl *
22 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1 dsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1 dsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 dsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1 dsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 dsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 dsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 dsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 dsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 dsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 dsl * POSSIBILITY OF SUCH DAMAGE.
33 1.1 dsl */
34 1.1 dsl
35 1.1 dsl /*
36 1.1 dsl * i386 partition boot code
37 1.1 dsl * This version reads boot directly from a FAT16 filesystem on LBA
38 1.1 dsl * Addressable media - eg USB media.
39 1.1 dsl *
40 1.1 dsl * The code is read to address 0:7c00 by the mbr code (in sector zero).
41 1.1 dsl *
42 1.1 dsl * We assume that the partition contains a 'boot parameter block' that
43 1.1 dsl * correctly identifies the filesystem.
44 1.1 dsl *
45 1.1 dsl * On entry and exit the BIOS drive number is in %dl.
46 1.1 dsl */
47 1.1 dsl
48 1.1 dsl #include <machine/asm.h>
49 1.1 dsl #include <sys/bootblock.h>
50 1.1 dsl
51 1.1 dsl #define PBR_AFTERBPB 62 /* BPB size in floppy master BR */
52 1.1 dsl
53 1.1 dsl #ifdef TERSE_ERROR
54 1.1 dsl /*
55 1.1 dsl * Error codes. Done this way to save space.
56 1.1 dsl */
57 1.1 dsl #define ERR_READ 'R' /* Read error */
58 1.1 dsl #define ERR_NO_BOOT 'B' /* No /boot */
59 1.1 dsl #define ERR_NOT_FAT16 'F' /* Not a FAT16 filesystem */
60 1.1 dsl #define ERR_NO_BOOT_MAGIC_2 'M' /* No magic in loaded /boot */
61 1.1 dsl
62 1.1 dsl #define set_err(err) movb $err, %al
63 1.1 dsl
64 1.1 dsl #else
65 1.1 dsl #define set_err(err) mov $err, %ax
66 1.1 dsl #endif
67 1.1 dsl
68 1.1 dsl .text
69 1.1 dsl .code16
70 1.1 dsl ENTRY(start)
71 1.1 dsl jmp start0
72 1.1 dsl nop
73 1.1 dsl oem_name: .ascii "NetBSD40" /* 8 bytes */
74 1.1 dsl /* FAT16 BIOS/BOOT Parameter Block - see struct mbr_bpbFAT16 in bootblock.h */
75 1.1 dsl #define bpb_bytes_per_sec /* .word 0 */ 0x0b /* bytes per sector */
76 1.1 dsl #define bpb_sec_per_clust /* .byte 0 */ 0x0d /* sectors per cluster */
77 1.1 dsl #define bpb_res_sectors /* .word 0 */ 0x0e /* number of reserved sectors */
78 1.1 dsl #define bpb_FATs /* .byte 0 */ 0x10 /* number of FATs */
79 1.1 dsl #define bpb_root_dir_ents /* .word 0 */ 0x11 /* number of root dir entries */
80 1.1 dsl #define bpb_sectors /* .word 0 */ 0x13 /* total number of sectors */
81 1.1 dsl #define bpb_media /* .byte 0 */ 0x15 /* media descriptor */
82 1.1 dsl #define bpb_FAT_secs /* .word 0 */ 0x16 /* number of sectors per FAT */
83 1.1 dsl #define bpb_sec_per_track /* .word 0 */ 0x18 /* sectors per track */
84 1.1 dsl #define bpb_heads /* .word 0 */ 0x1a /* number of heads */
85 1.1 dsl #define bpb_hidden_secs /* .long 0 */ 0x1c /* # of hidden sectors */
86 1.1 dsl #define bpb_huge_sectors /* .long 0 */ 0x20 /* # of sectors if !bpbSectors*/
87 1.1 dsl /* Extended boot area */
88 1.1 dsl #define bs_drive_number /* .byte 0 */ 0x24 /* but we believe the BIOS ! */
89 1.1 dsl #define bs_reserved_1 /* .byte 0 */ 0x25 /* */
90 1.1 dsl #define bs_boot_sig /* .byte 0 */ 0x26 /* */
91 1.1 dsl #define bs_volume_id /* .long 0 */ 0x27 /* Volume ID number */
92 1.1 dsl #define bs_volume_label /* .space 11*/ 0x2b /* Volume label */
93 1.1 dsl #define bs_file_sys_type /* .space 8 */ 0x36 /* "FAT16 " */
94 1.1 dsl
95 1.1 dsl /* Some locals overlaying the end of the above */
96 1.1 dsl #define sec_p_cl_w 0x2c /* 16bit bpb_sec_per_clust */
97 1.1 dsl #define fat_sector 0x30 /* start of FAT in sectors */
98 1.1 dsl
99 1.1 dsl . = start + PBR_AFTERBPB /* skip BPB */
100 1.1 dsl start0:
101 1.1 dsl xor %eax, %eax /* don't trust values of ds, es or ss */
102 1.1 dsl mov %ax, %ds
103 1.1 dsl mov %ax, %es
104 1.1 dsl mov %ax, %ss
105 1.1 dsl mov $start, %sp
106 1.1 dsl mov %sp, %bp /* to access the pbp */
107 1.1 dsl push %dx /* save drive at -2(%bp) */
108 1.1 dsl
109 1.1 dsl xchg %ax, %bx /* %bx = 0 */
110 1.1 dsl
111 1.1 dsl set_err(ERR_NOT_FAT16)
112 1.1 dsl cmpl $'A'|'T'<<8|'1'<<16|'6'<<24, bs_file_sys_type+1(%bp)
113 1.1 dsl jne error
114 1.1 dsl
115 1.1 dsl /* Add 'reserved' (inside ptn) to 'hidden' (ptn offset) */
116 1.1 dsl mov bpb_res_sectors(%bp), %ax
117 1.1 dsl addl bpb_hidden_secs(%bp), %eax
118 1.1 dsl mov %eax, fat_sector(%bp) /* To get first sector of FAT */
119 1.1 dsl
120 1.1 dsl /* Determine base of root directory */
121 1.1 dsl movzbw bpb_FATs(%bp), %ax /* Count of FATs */
122 1.1 dsl mulw bpb_FAT_secs(%bp) /* FAT size in %dx:%ax */
123 1.1 dsl shl $16,%edx
124 1.1 dsl xchg %ax,%dx /* FAT size now in %edx */
125 1.1 dsl addl fat_sector(%bp), %edx /* Directory is after FATs */
126 1.1 dsl push %bx /* %bx is zero */
127 1.1 dsl push %bx /* 64-bit for LBA read */
128 1.1 dsl pushl %edx /* Sector number of root dir */
129 1.1 dsl
130 1.1 dsl push $0x1000 /* Read to 0x10000:0 */
131 1.1 dsl pop %es /* Which we need in %es later */
132 1.1 dsl push %es
133 1.1 dsl push %bx /* Offset zero */
134 1.1 dsl
135 1.1 dsl /* Convert the root directory size to sectors */
136 1.1 dsl push %dx
137 1.1 dsl mov bpb_root_dir_ents(%bp), %ax
138 1.1 dsl mov $0x20, %dx
139 1.1 dsl mul %dx
140 1.1 dsl divw bpb_bytes_per_sec(%bp)
141 1.1 dsl add $0xffff, %dx /* Set carry if remainder non-zero */
142 1.1 dsl adc %bx, %ax /* %bx is still zero */
143 1.1 dsl pop %dx
144 1.1 dsl
145 1.1 dsl /* Read in the entire root directory */
146 1.1 dsl push %ax /* Sectors in root directory */
147 1.1 dsl cwtl
148 1.1 dsl addl %eax, %edx /* %edx now sector of first file */
149 1.1 dsl call read_lba /* Read entire directory */
150 1.1 dsl
151 1.1 dsl /* Scan directory for our file */
152 1.1 dsl xor %di, %di
153 1.1 dsl scan_dir:
154 1.1 dsl mov $boot_filename, %si
155 1.1 dsl mov $11, %cx
156 1.1 dsl repz cmpsb
157 1.1 dsl je found_boot
158 1.1 dsl or $31,%di
159 1.1 dsl inc %di
160 1.1 dsl cmp %ch, %es:(%di) /* %ch is zero - test end of dir */
161 1.1 dsl jz 1f
162 1.1 dsl decw bpb_root_dir_ents(%bp)
163 1.1 dsl jnz scan_dir
164 1.1 dsl 1: set_err(ERR_NO_BOOT)
165 1.1 dsl
166 1.1 dsl error:
167 1.1 dsl #ifdef TERSE_ERROR
168 1.1 dsl movb %al, errcod
169 1.1 dsl movw $errtxt, %si
170 1.1 dsl call message
171 1.1 dsl #else
172 1.1 dsl push %ax
173 1.1 dsl movw $errtxt, %si
174 1.1 dsl call message
175 1.1 dsl pop %si
176 1.1 dsl call message
177 1.1 dsl movw $newline, %si
178 1.1 dsl call message
179 1.1 dsl #endif
180 1.1 dsl 1: sti
181 1.1 dsl hlt
182 1.1 dsl jmp 1b
183 1.1 dsl
184 1.1 dsl found_boot:
185 1.1 dsl movzbl bpb_sec_per_clust(%bp), %eax
186 1.1 dsl movw %ax, sec_p_cl_w(%bp)
187 1.1 dsl add %ax, %ax
188 1.1 dsl subl %eax, %edx /* 1st file sector is cluster 2 */
189 1.1 dsl 1: inc %cl /* Convert power of 2 ... */
190 1.1 dsl shr $1, %ax /* ... to shift */
191 1.1 dsl jnz 1b
192 1.1 dsl dec %cx
193 1.1 dsl dec %cx
194 1.1 dsl movw %es:15(%di), %ax /* Cluster number for file start */
195 1.1 dsl push %es /* We increment the 'segment' ... */
196 1.1 dsl pop %di /* ... after each read, offset is 0 */
197 1.1 dsl
198 1.1 dsl read_data_block:
199 1.1 dsl mov %ax, %bx /* Save cluster number */
200 1.1 dsl shl %cl, %eax /* Convert to sector number */
201 1.1 dsl add %eax, %edx
202 1.1 dsl pushl %edx /* Sector to read */
203 1.1 dsl sub %eax, %edx /* Recover base segment */
204 1.1 dsl push %di /* Target address segment! */
205 1.1 dsl push $0
206 1.1 dsl push sec_p_cl_w(%bp)
207 1.1 dsl call read_lba /* Read a cluster */
208 1.1 dsl
209 1.1 dsl /* Update read ptr for next cluster */
210 1.1 dsl mov bpb_bytes_per_sec(%bp), %ax
211 1.1 dsl shr $4, %ax /* x86 segment count */
212 1.1 dsl shl %cl, %ax /* for a cluster */
213 1.1 dsl add %ax, %di
214 1.1 dsl
215 1.1 dsl /* Lookup FAT slot number in FAT table */
216 1.1 dsl mov %bx, %ax /* Recover cluster number */
217 1.1 dsl push %dx
218 1.1 dsl xor %dx, %dx
219 1.1 dsl divw bpb_bytes_per_sec(%bp)
220 1.1 dsl mov %dx, %bx /* Entry in FAT block */
221 1.1 dsl pop %dx
222 1.1 dsl cmp %ax, fat_cache
223 1.1 dsl je lookup_fat
224 1.1 dsl
225 1.1 dsl /* We must read a different chuck of the FAT */
226 1.1 dsl mov %ax, fat_cache
227 1.1 dsl cwtl
228 1.1 dsl shl $1, %ax
229 1.1 dsl addl fat_sector(%bp), %eax
230 1.1 dsl push %eax
231 1.1 dsl push %ds
232 1.1 dsl push $fat_buffer
233 1.1 dsl push $2 /* Always read 2 sectors of FAT */
234 1.1 dsl call read_lba
235 1.1 dsl
236 1.1 dsl /* Now use low part of cluster number to index FAT sector */
237 1.1 dsl lookup_fat:
238 1.1 dsl add %bx, %bx /* 2 bytes per entry... */
239 1.1 dsl movzwl fat_buffer(%bx), %eax /* Next FAT slot */
240 1.1 dsl cmp $0xfff0, %ax
241 1.1 dsl jb read_data_block
242 1.1 dsl
243 1.1 dsl /* Found end of FAT chain - must be EOF - leap into loaded code */
244 1.1 dsl mov $0x1000, %ax
245 1.1 dsl mov %ax, %es
246 1.1 dsl cmpl $X86_BOOT_MAGIC_2, %es:4
247 1.1 dsl je magic_ok
248 1.1 dsl set_err(ERR_NO_BOOT_MAGIC_2)
249 1.1 dsl err1: jmp error
250 1.1 dsl
251 1.1 dsl /* Set parameters expected by /boot */
252 1.1 dsl magic_ok:
253 1.1 dsl mov bpb_hidden_secs(%bp), %ebx /* ptn base sector */
254 1.1 dsl movb -2(%bp), %dl /* disk number (could pop %dx) */
255 1.1 dsl mov $boot_params + 4, %si
256 1.1 dsl push %es
257 1.1 dsl push $0
258 1.1 dsl lret
259 1.1 dsl
260 1.1 dsl /* Read disk using on-stack int13-extension parameter block */
261 1.1 dsl read_lba:
262 1.1 dsl pop %ax /* Save rtn addr */
263 1.1 dsl pushw $16 /* Stack ctl block length */
264 1.1 dsl mov %sp, %si /* Address ctl block */
265 1.1 dsl push %ax /* restack rtn addr */
266 1.1 dsl pushal
267 1.1 dsl mov -2(%bp), %dl /* Disk # saved on entry */
268 1.1 dsl movb $0x42, %ah
269 1.1 dsl int $0x13
270 1.1 dsl popal
271 1.1 dsl
272 1.1 dsl set_err(ERR_READ)
273 1.1 dsl jc err1
274 1.1 dsl ret $12 /* Discard all except high LBA zeros */
275 1.1 dsl
276 1.1 dsl /*
277 1.1 dsl * I hate #including source files, but pbr_magic below has to be at
278 1.1 dsl * the correct absolute address.
279 1.1 dsl * Clearly this could be done with a linker script.
280 1.1 dsl */
281 1.1 dsl
282 1.1 dsl #include <message.S>
283 1.1 dsl #if 0
284 1.1 dsl #include <dump_eax.S>
285 1.1 dsl #endif
286 1.1 dsl
287 1.1 dsl errtxt: .ascii "Error " /* runs into newline... */
288 1.1 dsl errcod: .byte 0 /* ... if errcod set */
289 1.1 dsl newline:
290 1.1 dsl .asciz "\r\n"
291 1.1 dsl
292 1.1 dsl #ifndef TERSE_ERROR
293 1.1 dsl ERR_READ: .asciz "Disk read"
294 1.1 dsl ERR_NO_BOOT: .asciz "No /boot"
295 1.1 dsl ERR_NOT_FAT16: .asciz "Not FAT16 ptn"
296 1.1 dsl ERR_NO_BOOT_MAGIC_2: .asciz "No magic in /boot"
297 1.1 dsl #endif
298 1.1 dsl
299 1.1 dsl boot_filename: .ascii "BOOT "
300 1.1 dsl
301 1.1 dsl space:
302 1.1 dsl pbr_space = boot_params - .
303 1.1 dsl
304 1.1 dsl /*
305 1.1 dsl * Add magic number, with a zero sized patchable area - just in case something
306 1.1 dsl * finds it and tries to update the area.
307 1.1 dsl * Boot options can be set using 'installboot -e boot' so we don't need to
308 1.1 dsl * use any of our valuable bytes.
309 1.1 dsl */
310 1.1 dsl
311 1.1 dsl . = _C_LABEL(start) + 0x1fe - 2 - 4 - 4
312 1.1 dsl boot_params:
313 1.1 dsl .long X86_BOOT_MAGIC_FAT
314 1.1 dsl .long 1f - .
315 1.1 dsl 1:
316 1.1 dsl fat_cache: .word 0xffff /* Sector number in buffer */
317 1.1 dsl . = _C_LABEL(start) + 0x1fe
318 1.1 dsl .word 0xaa55
319 1.1 dsl fat_buffer: /* 2 sectors worth of FAT table */
320