mbr.S revision 1.8 1 /* $NetBSD: mbr.S,v 1.8 2004/07/04 00:38:51 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1999-2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden, based on an earlier work by Wolfgang Solfrank.
9 * Major surgery performed by David Laight.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * i386 master boot code
42 */
43
44 /* Compile options:
45 * BOOTSEL - bootselector code
46 * BOOT_EXTENDED - scan extended partition list (LBA reads)
47 * TERSE_ERROR - terse error messages
48 * NO_CHS - all reads are LBA
49 * NO_LBA_CHECK - no check if bios supports LBA reads
50 */
51
52 #ifdef BOOT_EXTENDED
53 #define NO_CHS 1
54 #define BOOTSEL 1
55 #endif
56
57 #include <machine/asm.h>
58 #include <sys/bootblock.h>
59
60 #define BOOTADDR 0x7c00
61 #define LOADADDR 0x0600 /* address were are linked to */
62
63 #define TABENTRYSIZE (MBR_BS_PARTNAMESIZE + 1)
64 #define NAMETABSIZE (MBR_PART_COUNT * TABENTRYSIZE)
65
66 /* Scan values for the various keys we use, as returned by the BIOS */
67 #define SCAN_ENTER 0x1c
68 #define SCAN_F1 0x3b
69 #define SCAN_1 0x2
70
71 /*
72 * Minimum and maximum drive number that is considered to be valid.
73 */
74 #define MINDRV 0x80
75 #define MAXDRV 0x8f
76
77 #ifdef TERSE_ERROR
78 /*
79 * Error codes. Done this way to save space.
80 */
81 #define ERR_INVPART '1' /* Invalid partition table */
82 #define ERR_READ '2' /* Read error */
83 #define ERR_NOOS '3' /* Magic no. check failed for part. */
84 #define ERR_KEY '?' /* unknown key press */
85 #define ERR_NO_LBA 'L' /* sector above chs limit */
86
87 #define set_err(err) movb $err, %al
88
89 #else
90 #define set_err(err) mov $err, %ax
91 #endif
92
93 .text
94 .code16
95 /*
96 * Move ourselves out of the way first.
97 * (to the address we are linked at - 0x600)
98 * and zero our bss
99 */
100 ENTRY(start)
101 xor %ax, %ax
102 mov %ax, %ss
103 movw $BOOTADDR, %sp
104 mov %ax, %es
105 mov %ax, %ds
106 mov %sp, %si
107 movw $start, %di
108 movw $(bss_start - start)/2, %cx
109 rep
110 movsw
111 mov $(bss_end - bss_start + 1)/2, %cx
112 rep
113 stosw
114 ljmp $0, $mbr /* leap into copy of code */
115
116 /*
117 * Sanity check the drive number passed by the BIOS. Some BIOSs may not
118 * do this and pass garbage.
119 */
120 mbr:
121 cmpb $MAXDRV, %dl /* relies on MINDRV being 0x80 */
122 jle 1f
123 movb $MINDRV, %dl /* garbage in, boot disk 0 */
124 1:
125 push %dx /* save drive number */
126 push %dx /* twice - for err_msg loop */
127
128 /*
129 * Walk through the selector (name) table printing used entries.
130 */
131 bootsel_menu:
132 movw $nametab, %bx
133 #ifdef BOOT_EXTENDED
134 xorl %ecx, %ecx /* base of extended partition */
135 next_extended:
136 xorl %edx, %edx /* for next extended partition */
137 #endif
138 lea parttab - nametab(%bx), %bp
139 next_ptn:
140 movb 4(%bp), %al /* partition type */
141 #ifdef NO_CHS
142 movl 8(%bp), %edi /* partition sector number */
143 #ifdef BOOT_EXTENDED
144 cmpb $MBR_PTYPE_EXT, %al /* Extended partition */
145 je 1f
146 cmpb $MBR_PTYPE_EXT_LBA, %al /* Extended LBA partition */
147 je 1f
148 cmpb $MBR_PTYPE_EXT_LNX, %al /* Linux extended partition */
149 jne 2f
150 1: movl %edi, %edx /* save next extended ptn */
151 jmp 4f
152 2:
153 #endif
154 addl lba_sector, %edi /* add in extended ptn base */
155 #endif
156 test %al, %al /* undefined partition */
157 je 4f
158 cmpb $0x80, (%bp) /* check for active partition */
159 jne 3f /* jump if not... */
160 #define ENTER (4 * ((SCAN_ENTER - SCAN_F1) & 0xff))
161 #ifdef NO_CHS
162 movl %edi, ptn_list + ENTER /* save location of active ptn */
163 #else
164 mov %bp, ptn_list + ENTER
165 #endif
166 #undef ENTER
167 3:
168 #ifdef BOOTSEL
169 cmpb $0, (%bx) /* check for prompt */
170 jz 4f
171 /* output menu item */
172 movw $prefix, %si
173 incb (%si)
174 call message /* menu number */
175 mov (%si), %si /* ':' << 8 | '1' + count */
176 shl $2, %si /* const + count * 4 */
177 #define CONST (4 * ((':' << 8) + '1' - ((SCAN_1 - SCAN_F1) & 0xff)))
178 #ifdef NO_CHS
179 movl %edi, ptn_list - CONST(%si) /* sector to read */
180 #else
181 mov %bp, ptn_list - CONST(%si) /* partition info */
182 #endif
183 #undef CONST
184 mov %bx, %si
185 call message /* prompt */
186 movw $crlf, %si
187 call message
188 #endif
189 4:
190 add $0x10, %bp
191 add $TABENTRYSIZE, %bx
192 cmpb $(nametab - start - 0x100) + 4 * TABENTRYSIZE, %bl
193 jne next_ptn
194
195 #ifdef BOOT_EXTENDED
196 /*
197 * Now check extended partition chain
198 */
199 testl %edx, %edx
200 je wait_key
201 testl %ecx, %ecx
202 jne 1f
203 xchg %ecx, %edx /* save base of ext ptn chain */
204 1: addl %ecx, %edx /* sector to read */
205 movl %edx, lba_sector
206 movw $lba_info, %si
207 movb $0x42, %ah
208 pop %dx /* recover drive # */
209 push %dx /* save drive */
210 int $0x13
211 jc wait_key /* abort menu on read fail */
212 cmpw $MBR_MAGIC, LOADADDR + MBR_MAGIC_OFFSET
213 movw $nametab - LOADADDR + BOOTADDR, %bx
214 je next_extended
215 #endif
216
217 /*
218 * If we haven't output any menu entries, then the system sits waiting
219 * for a keypress with the user looking at a blank screen wondering
220 * why nothing is happening.
221 * To stop this we generate <ENTER> - to boot the active partition
222 * if we haven't output any menu texts.
223 * The non-bootsel code also traverses this code path.
224 */
225
226 #ifndef BOOTSEL
227 movb $SCAN_ENTER - SCAN_F1, %al
228 #else
229 movb $SCAN_ENTER, %al /* default to active partition */
230 cmpb $'0', prefix /* did we output a menu ? */
231 je check_key /* Nope - process SCAN_ENTER */
232 /*
233 * Get the initial time value for the timeout comparison. It is returned
234 * by int 1a in cx:dx. We do sums modulo 2^16 so it doesn't matter if
235 * the counter wraps (which it does every hour) - so we can safely
236 * ignore 'cx'.
237 *
238 * Loop around checking for a keypress until we have one, or timeout is
239 * reached.
240 */
241 wait_key:
242 xorb %ah, %ah
243 int $0x1a
244 mov %dx, %di /* start time to di */
245 3:
246 movb $1, %ah /* looks to see if a */
247 int $0x16 /* key has been pressed */
248 jnz get_key
249 xorb %ah, %ah
250 int $0x1a /* current time to cx:dx */
251 sub %di, %dx
252 movw timeout, %ax
253 cmp %ax, %dx /* always wait for 1 tick... */
254 jbe 3b /* 0xffff means never timeout */
255 def_key:
256 movb defkey, %al /* timedout - pick default key */
257 jmp check_key
258 get_key:
259 xorb %ah, %ah
260 int $0x16 /* 'read key', code ah, ascii al */
261 shr $8, %ax /* code in %al, %ah zero */
262
263 /*
264 * We have a keycode, see what it means.
265 * If we don't know we generate error '?' and go ask again
266 */
267 check_key:
268 /*
269 * F1-F10 -> boot disk 0-9. Check if the requested disk isn't above
270 * the number of disks actually in the system as stored in 0:0475 by
271 * the BIOS.
272 * If we trust loc 475, we needn't check the upper bound on the keystroke
273 * This is always sector 0, so always read using chs.
274 */
275 subb $SCAN_F1, %al
276 cmpb 0x0475, %al
277 jae boot_ptn
278 addb $0x80, %al
279 pop %dx /* dump saved drive # */
280 push %ax /* replace with new */
281 #ifdef NO_CHS
282 xorl %ebp, %ebp /* read sector number 0 */
283 jmp boot_lba
284 #else
285 movw $chs_zero, %si /* chs read sector zero info */
286 jmp read_chs
287 #endif
288 #endif /* BOOTSEL */
289
290 /*
291 * Boot requested partition.
292 * Use keycode to index the table we generated when we scanned the mbr
293 * while generating the menu.
294 *
295 * We very carfully saved the values in the correct part of the table.
296 */
297
298 boot_ptn:
299 shl $2, %ax
300 movw %ax, %si
301 #ifdef NO_CHS
302 movl ptn_list(%si), %ebp
303 testl %ebp, %ebp
304 jnz boot_lba
305 #else
306 mov ptn_list(%si), %si
307 test %si, %si
308 jnz boot_si
309 #endif
310 #ifdef BOOTSEL
311 set_err(ERR_KEY)
312 #else
313 set_err(ERR_INVPART)
314 #endif
315 /* jmp err_msg */
316
317 /* Something went wrong...
318 * Output error code,
319 * reset disk subsystem - needed after read failure,
320 * and wait for user key
321 */
322 err_msg:
323 #ifdef TERSE_ERROR
324 movb %al, errcod
325 movw $errtxt, %si
326 call message
327 #else
328 push %ax
329 movw $errtxt, %si
330 call message
331 pop %si
332 call message
333 movw $crlf, %si
334 call message
335 #endif
336 pop %dx /* drive we errored on */
337 xor %ax,%ax /* only need %ah = 0 */
338 int $0x13 /* reset disk subsystem */
339 #ifdef BOOTSEL
340 pop %dx /* original drive number */
341 push %dx
342 push %dx
343 jmp get_key
344 #else
345 int $0x18 /* BIOS might ask for a key */
346 /* press and retry boot seq. */
347 1: sti
348 hlt
349 jmp 1b
350 #endif
351
352 #ifndef NO_CHS
353 /*
354 * Active partition pointed to by si.
355 * Read the first sector.
356 *
357 * We can either do a CHS (Cylinder Head Sector) or an LBA (Logical
358 * Block Address) read. Always doing the LBA one
359 * would be nice - unfortunately not all systems support it.
360 * Also some may contain a separate (eg SCSI) bios that doesn't
361 * support it even when the main bios does.
362 *
363 * There is also the additional problem that the CHS values may be wrong
364 * (eg if fdisk was run on a different system that used different BIOS
365 * geometry). We convert the CHS value to a LBA sector number using
366 * the geometry from the BIOS, if the number matches we do a CHS read.
367 */
368 boot_si:
369 movl 8(%si), %ebp /* get sector # */
370
371 testb $MBR_BS_READ_LBA, flags
372 jnz boot_lba /* fdisk forced LBA read */
373
374 pop %dx /* collect saved drive... */
375 push %dx /* ...number to dl */
376 movb $8, %ah
377 int $0x13 /* chs info */
378
379 /*
380 * Validate geometry, if the CHS sector number doesn't match the LBA one
381 * we'll do an LBA read.
382 * calc: (cylinder * number_of_heads + head) * number_of_sectors + sector
383 * and compare against LBA sector number.
384 * Take a slight 'flier' and assume we can just check 16bits (very likely
385 * to be true because the number of sectors per track is 63).
386 */
387 movw 2(%si), %ax /* cylinder + sector */
388 push %ax /* save for sector */
389 shr $6, %al
390 xchgb %al, %ah /* 10 bit cylinder number */
391 shr $8, %dx /* last head */
392 inc %dx /* number of heads */
393 mul %dx
394 mov 1(%si), %dl /* head we want */
395 add %dx, %ax
396 and $0x3f, %cx /* number of sectors */
397 mul %cx
398 pop %dx /* recover sector we want */
399 and $0x3f, %dx
400 add %dx, %ax
401 dec %ax
402
403 cmp %bp, %ax
404 je read_chs
405
406 #ifndef NO_LBA_CHECK
407 /*
408 * Determine whether we have int13-extensions, by calling int 13, function 41.
409 * Check for the magic number returned, and the disk packet capability.
410 */
411 movw $0x55aa, %bx
412 movb $0x41, %ah
413 pop %dx
414 push %dx
415 int $0x13
416 jc 1f /* no int13 extensions */
417 cmpw $0xaa55, %bx
418 jnz 1f
419 testb $1, %cl
420 jnz boot_lba
421 1: set_err(ERR_NO_LBA)
422 jmp err_msg
423 #endif /* NO_LBA_CHECK */
424 #endif /* NO_CHS */
425
426 /*
427 * Save sector number (passed in %ebp) into lba parameter block,
428 * read the sector and leap into it.
429 */
430 boot_lba:
431 movl %ebp, lba_sector /* save sector number */
432 movw $lba_info, %si
433 movb $0x42, %ah
434 pop %dx /* recover drive # */
435 do_read:
436 push %dx /* save drive */
437 int $0x13
438
439 set_err(ERR_READ)
440 jc err_msg
441
442 /*
443 * Check signature for valid bootcode
444 */
445 movb BOOTADDR, %al /* first byte non-zero */
446 test %al, %al
447 jz 1f
448 movw BOOTADDR + MBR_MAGIC_OFFSET, %ax
449 1: cmp $MBR_MAGIC, %ax
450 set_err(ERR_NOOS)
451 jnz err_msg
452
453 /* We pass the sector number through to the next stage boot.
454 * It doesn't have to use it (indeed no other mbr code will generate) it,
455 * but it does let us have a NetBSD pbr that can identify where it was
456 * read from! This lets us use this code to select between two
457 * NetBSD system on the same physical driver.
458 * (If we've read the mbr of a different disk, it gets a random number
459 * - but it wasn't expecting anything...)
460 */
461 movl %ebp, %esi
462 pop %dx /* recover drive # */
463 jmp start - LOADADDR + BOOTADDR
464
465
466 #ifndef NO_CHS
467 /*
468 * Sector below CHS limit
469 * Do a cylinder-head-sector read instead.
470 */
471 read_chs:
472 pop %dx /* recover drive # */
473 movb 1(%si), %dh /* head */
474 movw 2(%si), %cx /* ch=cyl, cl=sect */
475 movw $BOOTADDR, %bx /* es:bx is buffer */
476 movw $0x201, %ax /* command 2, 1 sector */
477 jmp do_read
478 #endif
479
480 /*
481 * Control block for int-13 LBA read.
482 * We need a xx, 00, 01, 00 somewhere to load chs for sector zero,
483 * by a complete fluke there is one here!
484 */
485 chs_zero:
486 lba_info:
487 .word 0x10 /* control block length */
488 .word 1 /* sector count */
489 .word BOOTADDR /* offset in segment */
490 .word 0 /* segment */
491 lba_sector:
492 .long 0x0000 /* sector # goes here... */
493 .long 0x0000
494
495 errtxt: .ascii "Error " /* runs into crlf if errcod set */
496 errcod: .byte 0
497 crlf: .asciz "\r\n"
498
499 #ifdef BOOTSEL
500 prefix: .asciz "0: "
501 #endif
502
503 #ifndef TERSE_ERROR
504 ERR_INVPART: .asciz "No active partition"
505 ERR_READ: .asciz "Disk read error"
506 ERR_NOOS: .asciz "No operating system"
507 #ifndef NO_LBA_CHECK
508 ERR_NO_LBA: .asciz "Invalid CHS read"
509 #endif
510 #ifdef BOOTSEL
511 ERR_KEY: .asciz "bad key"
512 #endif
513 #endif
514
515 /*
516 * I hate #including source files, but the stuff below has to be at
517 * the correct absolute address.
518 * Clearly this could be done with a linker script.
519 */
520
521 #include <message.S>
522 #if 0
523 #include <dump_eax.S>
524 #endif
525
526 /*
527 * Stuff from here on is overwritten by fdisk - the offset must not change...
528 *
529 * Get amount of space to makefile can report it.
530 * (Unfortunately I can't seem to get the value reported when it is -ve)
531 */
532 mbr_space = defkey - .
533 . = start + MBR_BS_OFFSET
534 /*
535 * Default action, as a keyvalue we'd normally read from the BIOS.
536 */
537 defkey:
538 .byte SCAN_ENTER /* ps/2 code */
539 #ifndef BOOTSEL_FLAGS
540 #define BOOTSEL_FLAGS 0
541 #endif
542 flags: .byte MBR_BS_NEWMBR | BOOTSEL_FLAGS
543 /*
544 * Timeout value. ~65536 ticks per hour, which is ~18.2 times per second.
545 * 0xffff means never timeout.
546 */
547 timeout:
548 .word 182 /* default to 10 seconds */
549 /*
550 * mbr_bootsel
551 */
552 nametab:
553 .fill MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1), 0x01, 0x00
554
555 /* space for mbr_dsn */
556 . = start + MBR_DSN_OFFSET
557 .long 0
558
559 /* mbr_bootsel_magic */
560 . = start + MBR_BS_MAGIC_OFFSET
561 .word MBR_BS_MAGIC
562
563 /*
564 * MBR partition table
565 */
566 . = start + MBR_PART_OFFSET
567 parttab:
568 .fill 0x40, 0x01, 0x00
569
570 . = start + MBR_MAGIC_OFFSET
571 .word MBR_MAGIC
572
573 /* zeroed data space */
574 bss_off = 0
575 bss_start = .
576 #define BSS(name, size) name = bss_start + bss_off; bss_off = bss_off + size
577 BSS(ptn_list, 256 * 4) /* long[]: boot sector numbers */
578 BSS(bss_end, 0)
579