mbr.S revision 1.9 1 /* $NetBSD: mbr.S,v 1.9 2004/08/14 21:03:23 dsl 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 cmpw timeout, %dx /* always wait for 1 tick... */
253 jbe 3b /* 0xffff means never timeout */
254 def_key:
255 movb defkey, %al /* timedout - pick default key */
256 jmp check_key
257 get_key:
258 xorb %ah, %ah
259 int $0x16 /* 'read key', code ah, ascii al */
260 shr $8, %ax /* code in %al, %ah zero */
261
262 /*
263 * We have a keycode, see what it means.
264 * If we don't know we generate error '?' and go ask again
265 */
266 check_key:
267 /*
268 * F1-F10 -> boot disk 0-9. Check if the requested disk isn't above
269 * the number of disks actually in the system as stored in 0:0475 by
270 * the BIOS.
271 * If we trust loc 475, we needn't check the upper bound on the keystroke
272 * This is always sector 0, so always read using chs.
273 */
274 subb $SCAN_F1, %al
275 cmpb 0x0475, %al
276 jae boot_ptn
277 addb $0x80, %al
278 pop %dx /* dump saved drive # */
279 push %ax /* replace with new */
280 #ifdef NO_CHS
281 xorl %ebp, %ebp /* read sector number 0 */
282 jmp boot_lba
283 #else
284 movw $chs_zero, %si /* chs read sector zero info */
285 jmp read_chs
286 #endif
287 #endif /* BOOTSEL */
288
289 /*
290 * Boot requested partition.
291 * Use keycode to index the table we generated when we scanned the mbr
292 * while generating the menu.
293 *
294 * We very carfully saved the values in the correct part of the table.
295 */
296
297 boot_ptn:
298 shl $2, %ax
299 movw %ax, %si
300 #ifdef NO_CHS
301 movl ptn_list(%si), %ebp
302 testl %ebp, %ebp
303 jnz boot_lba
304 #else
305 mov ptn_list(%si), %si
306 test %si, %si
307 jnz boot_si
308 #endif
309 #ifdef BOOTSEL
310 set_err(ERR_KEY)
311 #else
312 set_err(ERR_INVPART)
313 #endif
314 /* jmp err_msg */
315
316 /* Something went wrong...
317 * Output error code,
318 * reset disk subsystem - needed after read failure,
319 * and wait for user key
320 */
321 err_msg:
322 #ifdef TERSE_ERROR
323 movb %al, errcod
324 movw $errtxt, %si
325 call message
326 #else
327 push %ax
328 movw $errtxt, %si
329 call message
330 pop %si
331 call message
332 movw $crlf, %si
333 call message
334 #endif
335 pop %dx /* drive we errored on */
336 xor %ax,%ax /* only need %ah = 0 */
337 int $0x13 /* reset disk subsystem */
338 #ifdef BOOTSEL
339 pop %dx /* original drive number */
340 push %dx
341 push %dx
342 jmp get_key
343 #else
344 int $0x18 /* BIOS might ask for a key */
345 /* press and retry boot seq. */
346 1: sti
347 hlt
348 jmp 1b
349 #endif
350
351 #ifndef NO_CHS
352 /*
353 * Active partition pointed to by si.
354 * Read the first sector.
355 *
356 * We can either do a CHS (Cylinder Head Sector) or an LBA (Logical
357 * Block Address) read. Always doing the LBA one
358 * would be nice - unfortunately not all systems support it.
359 * Also some may contain a separate (eg SCSI) bios that doesn't
360 * support it even when the main bios does.
361 *
362 * There is also the additional problem that the CHS values may be wrong
363 * (eg if fdisk was run on a different system that used different BIOS
364 * geometry). We convert the CHS value to a LBA sector number using
365 * the geometry from the BIOS, if the number matches we do a CHS read.
366 */
367 boot_si:
368 movl 8(%si), %ebp /* get sector # */
369
370 testb $MBR_BS_READ_LBA, flags
371 jnz boot_lba /* fdisk forced LBA read */
372
373 pop %dx /* collect saved drive... */
374 push %dx /* ...number to dl */
375 movb $8, %ah
376 int $0x13 /* chs info */
377
378 /*
379 * Validate geometry, if the CHS sector number doesn't match the LBA one
380 * we'll do an LBA read.
381 * calc: (cylinder * number_of_heads + head) * number_of_sectors + sector
382 * and compare against LBA sector number.
383 * Take a slight 'flier' and assume we can just check 16bits (very likely
384 * to be true because the number of sectors per track is 63).
385 */
386 movw 2(%si), %ax /* cylinder + sector */
387 push %ax /* save for sector */
388 shr $6, %al
389 xchgb %al, %ah /* 10 bit cylinder number */
390 shr $8, %dx /* last head */
391 inc %dx /* number of heads */
392 mul %dx
393 mov 1(%si), %dl /* head we want */
394 add %dx, %ax
395 and $0x3f, %cx /* number of sectors */
396 mul %cx
397 pop %dx /* recover sector we want */
398 and $0x3f, %dx
399 add %dx, %ax
400 dec %ax
401
402 cmp %bp, %ax
403 je read_chs
404
405 #ifndef NO_LBA_CHECK
406 /*
407 * Determine whether we have int13-extensions, by calling int 13, function 41.
408 * Check for the magic number returned, and the disk packet capability.
409 */
410 movw $0x55aa, %bx
411 movb $0x41, %ah
412 pop %dx
413 push %dx
414 int $0x13
415 jc 1f /* no int13 extensions */
416 cmpw $0xaa55, %bx
417 jnz 1f
418 testb $1, %cl
419 jnz boot_lba
420 1: set_err(ERR_NO_LBA)
421 jmp err_msg
422 #endif /* NO_LBA_CHECK */
423 #endif /* NO_CHS */
424
425 /*
426 * Save sector number (passed in %ebp) into lba parameter block,
427 * read the sector and leap into it.
428 */
429 boot_lba:
430 movl %ebp, lba_sector /* save sector number */
431 movw $lba_info, %si
432 movb $0x42, %ah
433 pop %dx /* recover drive # */
434 do_read:
435 push %dx /* save drive */
436 int $0x13
437
438 set_err(ERR_READ)
439 jc err_msg
440
441 /*
442 * Check signature for valid bootcode
443 */
444 movb BOOTADDR, %al /* first byte non-zero */
445 test %al, %al
446 jz 1f
447 movw BOOTADDR + MBR_MAGIC_OFFSET, %ax
448 1: cmp $MBR_MAGIC, %ax
449 set_err(ERR_NOOS)
450 jnz err_msg
451
452 /* We pass the sector number through to the next stage boot.
453 * It doesn't have to use it (indeed no other mbr code will generate) it,
454 * but it does let us have a NetBSD pbr that can identify where it was
455 * read from! This lets us use this code to select between two
456 * NetBSD system on the same physical driver.
457 * (If we've read the mbr of a different disk, it gets a random number
458 * - but it wasn't expecting anything...)
459 */
460 movl %ebp, %esi
461 pop %dx /* recover drive # */
462 jmp start - LOADADDR + BOOTADDR
463
464
465 #ifndef NO_CHS
466 /*
467 * Sector below CHS limit
468 * Do a cylinder-head-sector read instead.
469 */
470 read_chs:
471 pop %dx /* recover drive # */
472 movb 1(%si), %dh /* head */
473 movw 2(%si), %cx /* ch=cyl, cl=sect */
474 movw $BOOTADDR, %bx /* es:bx is buffer */
475 movw $0x201, %ax /* command 2, 1 sector */
476 jmp do_read
477 #endif
478
479 /*
480 * Control block for int-13 LBA read.
481 * We need a xx, 00, 01, 00 somewhere to load chs for sector zero,
482 * by a complete fluke there is one here!
483 */
484 chs_zero:
485 lba_info:
486 .word 0x10 /* control block length */
487 .word 1 /* sector count */
488 .word BOOTADDR /* offset in segment */
489 .word 0 /* segment */
490 lba_sector:
491 .long 0x0000 /* sector # goes here... */
492 .long 0x0000
493
494 errtxt: .ascii "Error " /* runs into crlf if errcod set */
495 errcod: .byte 0
496 crlf: .asciz "\r\n"
497
498 #ifdef BOOTSEL
499 prefix: .asciz "0: "
500 #endif
501
502 #ifndef TERSE_ERROR
503 ERR_INVPART: .asciz "No active partition"
504 ERR_READ: .asciz "Disk read error"
505 ERR_NOOS: .asciz "No operating system"
506 #ifndef NO_LBA_CHECK
507 ERR_NO_LBA: .asciz "Invalid CHS read"
508 #endif
509 #ifdef BOOTSEL
510 ERR_KEY: .asciz "bad key"
511 #endif
512 #endif
513
514 /*
515 * I hate #including source files, but the stuff below has to be at
516 * the correct absolute address.
517 * Clearly this could be done with a linker script.
518 */
519
520 #include <message.S>
521 #if 0
522 #include <dump_eax.S>
523 #endif
524
525 /*
526 * Stuff from here on is overwritten by fdisk - the offset must not change...
527 *
528 * Get amount of space to makefile can report it.
529 * (Unfortunately I can't seem to get the value reported when it is -ve)
530 */
531 mbr_space = defkey - .
532 . = start + MBR_BS_OFFSET
533 /*
534 * Default action, as a keyvalue we'd normally read from the BIOS.
535 */
536 defkey:
537 .byte SCAN_ENTER /* ps/2 code */
538 #ifndef BOOTSEL_FLAGS
539 #define BOOTSEL_FLAGS 0
540 #endif
541 flags: .byte MBR_BS_NEWMBR | BOOTSEL_FLAGS
542 /*
543 * Timeout value. ~65536 ticks per hour, which is ~18.2 times per second.
544 * 0xffff means never timeout.
545 */
546 timeout:
547 .word 182 /* default to 10 seconds */
548 /*
549 * mbr_bootsel
550 */
551 nametab:
552 .fill MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1), 0x01, 0x00
553
554 /* space for mbr_dsn */
555 . = start + MBR_DSN_OFFSET
556 .long 0
557
558 /* mbr_bootsel_magic */
559 . = start + MBR_BS_MAGIC_OFFSET
560 .word MBR_BS_MAGIC
561
562 /*
563 * MBR partition table
564 */
565 . = start + MBR_PART_OFFSET
566 parttab:
567 .fill 0x40, 0x01, 0x00
568
569 . = start + MBR_MAGIC_OFFSET
570 .word MBR_MAGIC
571
572 /* zeroed data space */
573 bss_off = 0
574 bss_start = .
575 #define BSS(name, size) name = bss_start + bss_off; bss_off = bss_off + size
576 BSS(ptn_list, 256 * 4) /* long[]: boot sector numbers */
577 BSS(bss_end, 0)
578