1 ; Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
3 ; @APPLE_LICENSE_HEADER_START@
5 ; Portions Copyright (c) 1999-2002 Apple Computer, Inc. All Rights
6 ; Reserved. This file contains Original Code and/or Modifications of
7 ; Original Code as defined in and that are subject to the Apple Public
8 ; Source License Version 1.2 (the "License"). You may not use this file
9 ; except in compliance with the License. Please obtain a copy of the
10 ; License at http://www.apple.com/publicsource and read it before using
13 ; The Original Code and all software distributed under the License are
14 ; distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 ; EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 ; INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 ; FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
18 ; License for the specific language governing rights and limitations
21 ; @APPLE_LICENSE_HEADER_END@
25 ; A small boot sector program written in x86 assembly whose only
26 ; responsibility is to locate the active partition, load the
27 ; partition booter into memory, and jump to the booter's entry point.
28 ; It leaves the boot drive in DL and a pointer to the partition entry in SI.
30 ; This boot loader must be placed in the Master Boot Record.
32 ; In order to coexist with a fdisk partition table (64 bytes), and
33 ; leave room for a two byte signature (0xAA55) in the end, boot0 is
34 ; restricted to 446 bytes (512 - 64 - 2). If boot0 did not have to
35 ; live in the MBR, then we would have 510 bytes to work with.
37 ; boot0 is always loaded by the BIOS or another booter to 0:7C00h.
39 ; This code is written for the NASM assembler.
40 ; nasm boot0.s -o boot0
44 ; Set to 1 to enable obscure debug messages.
49 ; Set to 1 to support loading the partition booter (boot1) from a
52 EXT_PART_SUPPORT EQU 1
57 kBoot0Segment EQU 0x0000
58 kBoot0Stack EQU 0xFFF0 ; boot0 stack pointer
59 kBoot0LoadAddr EQU 0x7C00 ; boot0 load address
60 kBoot0RelocAddr EQU 0xE000 ; boot0 relocated address
62 kMBRBuffer EQU 0x1000 ; MBR buffer address
63 kExtBuffer EQU 0x1200 ; EXT boot block buffer address
65 kPartTableOffset EQU 0x1be
66 kMBRPartTable EQU kMBRBuffer + kPartTableOffset
67 kExtPartTable EQU kExtBuffer + kPartTableOffset
69 kSectorBytes EQU 512 ; sector size in bytes
70 kBootSignature EQU 0xAA55 ; boot sector signature
72 kPartCount EQU 4 ; number of paritions per table
73 kPartTypeBoot EQU 0xab ; boot2 partition type
74 kPartTypeExtDOS EQU 0x05 ; DOS extended partition type
75 kPartTypeExtWin EQU 0x0f ; Windows extended partition type
76 kPartTypeExtLinux EQU 0x85 ; Linux extended partition type
87 ; Format of fdisk partition entry.
89 ; The symbol 'part_size' is automatically defined as an `EQU'
90 ; giving the size of the structure.
93 .bootid: resb 1 ; bootable or not
94 .head: resb 1 ; starting head, sector, cylinder
97 .type: resb 1 ; partition type
98 .endhead resb 1 ; ending head, sector, cylinder
101 .lba: resd 1 ; starting lba
102 .sectors resd 1 ; size in sectors
108 %macro DebugCharMacro 1
114 %define DebugChar(x) DebugCharMacro x
119 ;--------------------------------------------------------------------------
120 ; Start of text segment.
124 ORG 0xE000 ; must match kBoot0RelocAddr
126 ;--------------------------------------------------------------------------
127 ; Boot code is loaded at 0:7C00h.
131 ; Set up the stack to grow down from kBoot0Segment:kBoot0Stack.
132 ; Interrupts should be off while the stack is being manipulated.
137 mov sp, kBoot0Stack ; sp <- top of stack
138 sti ; reenable interrupts
144 ; Relocate boot0 code.
146 mov si, kBoot0LoadAddr ; si <- source
147 mov di, kBoot0RelocAddr ; di <- destination
149 cld ; auto-increment SI and/or DI registers
150 mov cx, kSectorBytes/2 ; copy 256 words
151 repnz movsw ; repeat string move (word) operation
153 ; Code relocated, jump to start_reloc in relocated location.
157 ;--------------------------------------------------------------------------
158 ; Start execution from the relocated location.
164 mov dl, kDriveNumber ; starting BIOS drive number
174 ; Clear various flags in memory.
177 mov [ebios_lba], eax ; clear EBIOS LBA offset
178 mov [ebios_present], al ; clear EBIOS support flag
181 ; Check if EBIOS is supported for this hard drive.
183 mov ah, 0x41 ; Function 0x41
184 mov bx, 0x55AA ; check signature
185 ; mov dl, kDriveNumber ; Drive number
189 ; If successful, the return values are as follows:
192 ; ah = major version of EBIOS extensions (0x21 = version 1.1)
195 ; cx = support bits. bit 0 must be set for function 0x42.
198 cmp bx, 0xAA55 ; check BX = 0xAA55
199 jnz .ebios_check_done
200 test cl, 0x01 ; check enhanced drive read support
201 setnz [ebios_present] ; EBIOS supported, set flag
202 DebugChar('E') ; EBIOS supported
206 ; Since this code may not always reside in the MBR, always start by
207 ; loading the MBR to kMBRBuffer.
209 mov al, 1 ; load one sector
211 mov es, bx ; MBR load segment = 0
212 mov bx, kMBRBuffer ; MBR load address
213 mov si, bx ; pointer to fake partition entry
214 mov WORD [si], 0x0000 ; CHS DX: head = 0
215 mov WORD [si + 2], 0x0001 ; CHS CX: cylinder = 0, sector = 1
218 jc .next_drive ; MBR load error
221 ; Look for the booter partition in the MBR partition table,
222 ; which is at offset kMBRPartTable.
224 mov di, kMBRPartTable ; pointer to partition table
225 mov ah, 0 ; initial nesting level is 0
226 call find_boot ; will not return on success
229 inc dl ; next drive number
230 test dl, 0x4 ; went through all 4 drives?
231 jz .loop ; not yet, loop again
233 mov si, boot_error_str
239 ;--------------------------------------------------------------------------
240 ; Find the active (boot) partition and load the booter from the partition.
243 ; AH = recursion nesting level
244 ; DL = drive number (0x80 + unit number)
245 ; DI = pointer to fdisk partition table.
251 push cx ; preserve CX and SI
255 ; Check for boot block signature 0xAA55 following the 4 partition
258 cmp WORD [di + part_size * kPartCount], kBootSignature
259 jne .exit ; boot signature not found
261 mov si, di ; make SI a pointer to partition table
262 mov cx, kPartCount ; number of partition entries per table
266 ; First scan through the partition table looking for the active
267 ; partition. Postpone walking the extended partition chain for
268 ; the second pass. Do not merge the two without changing the
269 ; buffering scheme used to store extended partition tables.
272 mov al, ah ; indent based on nesting level
274 mov al, [si + part.type] ; print partition type
278 cmp BYTE [si + part.bootid], kPartActive
284 ; Found boot partition, read boot sector to memory.
287 mov bx, kBoot0Segment
289 mov bx, kBoot0LoadAddr
290 call load ; will not return on success
291 jc .continue ; load error, keep looking?
294 ; Fix up absolute block location in partition record.
296 mov eax, [si + part.lba]
298 mov [si + part.lba], eax
301 ; Jump to partition booter. The drive number is already in register DL.
302 ; SI is pointing to the modified partition entry.
304 jmp kBoot0Segment:kBoot0LoadAddr
307 add si, part_size ; advance SI to next partition entry
308 loop .loop ; loop through all partition entries
312 ; No primary (or logical) boot partition found in the current
313 ; partition table. Restart and look for extended partitions.
315 mov si, di ; make SI a pointer to partition table
316 mov cx, kPartCount ; number of partition entries per table
320 mov al, [si + part.type] ; AL <- partition type
322 cmp al, kPartTypeExtDOS ; Extended DOS
325 cmp al, kPartTypeExtWin ; Extended Windows(95)
328 cmp al, kPartTypeExtLinux ; Extended Linux
333 ; Advance si to the next partition entry in the extended
336 add si, part_size ; advance SI to next partition entry
337 loop .ext_loop ; loop through all partition entries
338 jmp .exit ; boot partition not found
342 ; Setup the arguments for the load function call to bring the
343 ; extended partition table into memory.
344 ; Remember that SI points to the extended partition entry.
346 mov al, 1 ; read 1 sector
349 mov bx, kExtBuffer ; load extended boot sector
351 jc .ext_continue ; load error
354 ; The LBA address of all extended partitions is relative based
355 ; on the LBA address of the extended partition in the MBR, or
356 ; the extended partition at the head of the chain. Thus it is
357 ; necessary to save the LBA address of the first extended partition.
361 mov ebp, [si + part.lba]
366 ; Call find_boot recursively to scan through the extended partition
367 ; table. Load DI with a pointer to the extended table in memory.
369 inc ah ; increment recursion level
370 mov di, kExtPartTable ; partition table pointer
371 call find_boot ; recursion...
375 ; Since there is an "unwritten" rule that limits each partition table
376 ; to have 0 or 1 extended partitions, there is no point in looking for
377 ; any additional extended partition entries at this point. There is no
378 ; boot partition linked beyond the extended partition that was loaded
382 %endif ; EXT_PART_SUPPORT
386 ; Boot partition not found. Giving up.
392 ;--------------------------------------------------------------------------
393 ; load - Load one or more sectors from a partition.
396 ; AL = number of 512-byte sectors to read.
397 ; ES:BX = pointer to where the sectors should be stored.
398 ; DL = drive number (0x80 + unit number)
399 ; SI = pointer to the partition entry.
407 test BYTE [ebios_present], 1
411 mov cx, 5 ; load retry count
413 call read_lba ; use INT13/F42
418 mov cx, 5 ; load retry count
420 call read_chs ; use INT13/F2
428 ;--------------------------------------------------------------------------
429 ; read_chs - Read sectors from a partition using CHS addressing.
432 ; AL = number of 512-byte sectors to read.
433 ; ES:BX = pointer to where the sectors should be stored.
434 ; DL = drive number (0x80 + unit number)
435 ; SI = pointer to the partition entry.
442 pusha ; save all registers
445 ; Read the CHS start values from the partition entry.
447 mov dh, [ si + part.head ] ; drive head
448 mov cx, [ si + part.sect ] ; drive sector + cylinder
451 ; INT13 Func 2 - Read Disk Sectors
455 ; AL = number of sectors to read
456 ; CH = lowest 8 bits of the 10-bit cylinder number
457 ; CL = bits 6 & 7: cylinder number bits 8 and 9
458 ; bits 0 - 5: starting sector number (1-63)
459 ; DH = starting head number (0 to 255)
460 ; DL = drive number (80h + drive unit)
461 ; es:bx = pointer where to place sectors read from disk
464 ; AH = return status (sucess is 0)
465 ; AL = burst error length if ah=0x11 (ECC corrected)
469 ; mov dl, kDriveNumber
470 mov ah, 0x02 ; Func 2
474 DebugChar('r') ; indicate INT13/F2 error
477 ; Issue a disk reset on error.
478 ; Should this be changed to Func 0xD to skip the diskette controller
483 stc ; set carry to indicate error
489 ;--------------------------------------------------------------------------
490 ; read_lba - Read sectors from a partition using LBA addressing.
493 ; AL = number of 512-byte sectors to read (valid from 1-127).
494 ; ES:BX = pointer to where the sectors should be stored.
495 ; DL = drive number (0x80 + unit number)
496 ; SI = pointer to the partition entry.
503 pusha ; save all registers
504 mov bp, sp ; save current SP
507 ; Create the Disk Address Packet structure for the
508 ; INT13/F42 (Extended Read Sectors) on the stack.
511 ; push DWORD 0 ; offset 12, upper 32-bit LBA
512 push ds ; For sake of saving memory,
513 push ds ; push DS register, which is 0.
515 mov ecx, [ebios_lba] ; offset 8, lower 32-bit LBA
516 add ecx, [si + part.lba]
519 push es ; offset 6, memory segment
521 push bx ; offset 4, memory offset
523 xor ah, ah ; offset 3, must be 0
524 push ax ; offset 2, number of sectors
526 push WORD 16 ; offset 0-1, packet size
529 ; INT13 Func 42 - Extended Read Sectors
533 ; DL = drive number (80h + drive unit)
534 ; DS:SI = pointer to Disk Address Packet
537 ; AH = return status (sucess is 0)
541 ; Packet offset 2 indicates the number of sectors read
544 ; mov dl, kDriveNumber
551 DebugChar('R') ; indicate INT13/F42 error
554 ; Issue a disk reset on error.
555 ; Should this be changed to Func 0xD to skip the diskette controller
560 stc ; set carry to indicate error
563 mov sp, bp ; restore SP
567 ;--------------------------------------------------------------------------
568 ; Write a string to the console.
571 ; DS:SI pointer to a NULL terminated string.
577 mov bx, 1 ; BH=0, BL=1 (blue)
578 cld ; increment SI after each lodsb call
580 lodsb ; load a byte from DS:SI into AL
581 cmp al, 0 ; Is it a NULL?
582 je .exit ; yes, all done
583 mov ah, 0xE ; INT10 Func 0xE
584 int 0x10 ; display byte in tty mode
592 ;--------------------------------------------------------------------------
593 ; Write a ASCII character to the console.
596 ; AL = ASCII character.
600 mov bx, 1 ; BH=0, BL=1 (blue)
601 mov ah, 0x0e ; bios INT 10, Function 0xE
602 int 0x10 ; display byte in tty mode
606 ;--------------------------------------------------------------------------
607 ; Write a variable number of spaces to the console.
610 ; AL = number to spaces.
615 mov cl, al ; use CX as the loop counter
616 mov al, ' ' ; character to print
625 ;--------------------------------------------------------------------------
626 ; Write the byte value to the console in hex.
629 ; AL = Value to be displayed in hex.
634 call print_nibble ; display upper nibble
636 call print_nibble ; display lower nibble
638 mov al, 10 ; carriage return
649 add al, 'A' - '9' - 1
663 ;--------------------------------------------------------------------------
664 ; NULL terminated strings.
666 boot_error_str db 10, 13, 'Error', 0
668 ;--------------------------------------------------------------------------
669 ; Pad the rest of the 512 byte sized booter with zeroes. The last
670 ; two bytes is the mandatory boot sector signature.
672 ; If the booter code becomes too large, then nasm will complain
673 ; that the 'times' argument is negative.
676 ; In memory variables.
678 ebios_lba dd 0 ; starting LBA of the intial extended partition.
679 ebios_present db 0 ; 1 if EBIOS is supported, 0 otherwise.
682 times 446-($-$$) db 0
685 ;--------------------------------------------------------------------------
686 ; Put fake partition entries for the bootable floppy image
688 part1bootid db 0x80 ; first partition active
689 part1head db 0x00 ; head #
690 part1sect db 0x02 ; sector # (low 6 bits)
691 part1cyl db 0x00 ; cylinder # (+ high 2 bits of above)
692 part1systid db 0xab ; Apple boot partition
693 times 3 db 0x00 ; ignore head/cyl/sect #'s
694 part1relsect dd 0x00000001 ; start at sector 1
695 part1numsect dd 0x00000080 ; 64K for booter
696 part2bootid db 0x00 ; not active
697 times 3 db 0x00 ; ignore head/cyl/sect #'s
698 part2systid db 0xa8 ; Apple UFS partition
699 times 3 db 0x00 ; ignore head/cyl/sect #'s
700 part2relsect dd 0x00000082 ; start after booter
701 ; part2numsect dd 0x00000abe ; 1.44MB - 65K
702 part2numsect dd 0x000015fe ; 2.88MB - 65K
706 times 510-($-$$) db 0