1 ; Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 ; @APPLE_LICENSE_HEADER_START@
5 ; Portions Copyright (c) 1999-2003 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 2.0 (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 kPartTypeUFS EQU 0xa8 ; UFS partition type
75 kPartTypeHFS EQU 0xaf ; HFS partition type
76 kPartTypeExtDOS EQU 0x05 ; DOS extended partition type
77 kPartTypeExtWin EQU 0x0f ; Windows extended partition type
78 kPartTypeExtLinux EQU 0x85 ; Linux extended partition type
83 ; Format of fdisk partition entry.
85 ; The symbol 'part_size' is automatically defined as an `EQU'
86 ; giving the size of the structure.
89 .bootid: resb 1 ; bootable or not
90 .head: resb 1 ; starting head, sector, cylinder
93 .type: resb 1 ; partition type
94 .endhead resb 1 ; ending head, sector, cylinder
97 .lba: resd 1 ; starting lba
98 .sectors resd 1 ; size in sectors
104 %macro DebugCharMacro 1
110 %define DebugChar(x) DebugCharMacro x
115 ;--------------------------------------------------------------------------
116 ; Start of text segment.
120 ORG 0xE000 ; must match kBoot0RelocAddr
122 ;--------------------------------------------------------------------------
123 ; Boot code is loaded at 0:7C00h.
127 ; Set up the stack to grow down from kBoot0Segment:kBoot0Stack.
128 ; Interrupts should be off while the stack is being manipulated.
133 mov sp, kBoot0Stack ; sp <- top of stack
134 sti ; reenable interrupts
140 ; Relocate boot0 code.
142 mov si, kBoot0LoadAddr ; si <- source
143 mov di, kBoot0RelocAddr ; di <- destination
145 cld ; auto-increment SI and/or DI registers
146 mov cx, kSectorBytes/2 ; copy 256 words
147 repnz movsw ; repeat string move (word) operation
149 ; Code relocated, jump to start_reloc in relocated location.
153 ;--------------------------------------------------------------------------
154 ; Start execution from the relocated location.
168 ; Clear various flags in memory.
171 mov [ebios_lba], eax ; clear EBIOS LBA offset
172 mov [ebios_present], al ; clear EBIOS support flag
175 ; Check if EBIOS is supported for this hard drive.
177 mov ah, 0x41 ; Function 0x41
178 mov bx, 0x55AA ; check signature
182 ; If successful, the return values are as follows:
185 ; ah = major version of EBIOS extensions (0x21 = version 1.1)
188 ; cx = support bits. bit 0 must be set for function 0x42.
191 cmp bx, 0xAA55 ; check BX = 0xAA55
192 jnz .ebios_check_done
193 test cl, 0x01 ; check enhanced drive read support
194 setnz [ebios_present] ; EBIOS supported, set flag
195 DebugChar('E') ; EBIOS supported
199 ; Since this code may not always reside in the MBR, always start by
200 ; loading the MBR to kMBRBuffer.
202 mov al, 1 ; load one sector
204 mov es, bx ; MBR load segment = 0
205 mov bx, kMBRBuffer ; MBR load address
206 mov si, bx ; pointer to fake partition entry
207 mov WORD [si], 0x0000 ; CHS DX: head = 0
208 mov WORD [si + 2], 0x0001 ; CHS CX: cylinder = 0, sector = 1
209 mov DWORD [si + part.lba], 0x00000000 ; LBA sector 0
212 jc .next_drive ; MBR load error
215 ; Look for the booter partition in the MBR partition table,
216 ; which is at offset kMBRPartTable.
218 mov di, kMBRPartTable ; pointer to partition table
219 mov ah, 0 ; initial nesting level is 0
220 call find_boot ; will not return on success
223 inc dl ; next drive number
224 test dl, 0x4 ; went through all 4 drives?
225 jz .loop ; not yet, loop again
227 mov si, boot_error_str
233 ;--------------------------------------------------------------------------
234 ; Find the active (boot) partition and load the booter from the partition.
237 ; AH = recursion nesting level
238 ; DL = drive number (0x80 + unit number)
239 ; DI = pointer to fdisk partition table.
245 push cx ; preserve CX and SI
249 ; Check for boot block signature 0xAA55 following the 4 partition
252 cmp WORD [di + part_size * kPartCount], kBootSignature
253 jne NEAR .exit ; boot signature not found
255 mov si, di ; make SI a pointer to partition table
256 mov cx, kPartCount ; number of partition entries per table
260 ; First scan through the partition table looking for the active
261 ; partition. Postpone walking the extended partition chain for
262 ; the second pass. Do not merge the two without changing the
263 ; buffering scheme used to store extended partition tables.
266 mov al, ah ; indent based on nesting level
268 mov al, [si + part.type] ; print partition type
272 cmp BYTE [si + part.type], kPartTypeBoot
274 cmp BYTE [si + part.type], kPartTypeUFS
276 cmp BYTE [si + part.type], kPartTypeHFS
285 ; Found boot partition, read boot sector to memory.
288 mov bx, kBoot0Segment
290 mov bx, kBoot0LoadAddr
292 jc .continue ; load error, keep looking?
297 cmp WORD [bx + 510], kBootSignature
298 jne NEAR .exit ; boot signature not found
303 ; Fix up absolute block location in partition record.
305 mov eax, [si + part.lba]
307 mov [si + part.lba], eax
312 ; Jump to partition booter. The drive number is already in register DL.
313 ; SI is pointing to the modified partition entry.
315 jmp kBoot0Segment:kBoot0LoadAddr
318 add si, part_size ; advance SI to next partition entry
319 loop .loop ; loop through all partition entries
323 ; No primary (or logical) boot partition found in the current
324 ; partition table. Restart and look for extended partitions.
326 mov si, di ; make SI a pointer to partition table
327 mov cx, kPartCount ; number of partition entries per table
331 mov al, [si + part.type] ; AL <- partition type
333 cmp al, kPartTypeExtDOS ; Extended DOS
336 cmp al, kPartTypeExtWin ; Extended Windows(95)
339 cmp al, kPartTypeExtLinux ; Extended Linux
344 ; Advance si to the next partition entry in the extended
347 add si, part_size ; advance SI to next partition entry
348 loop .ext_loop ; loop through all partition entries
349 jmp .exit ; boot partition not found
353 ; Setup the arguments for the load function call to bring the
354 ; extended partition table into memory.
355 ; Remember that SI points to the extended partition entry.
357 mov al, 1 ; read 1 sector
360 mov bx, kExtBuffer ; load extended boot sector
362 jc .ext_continue ; load error
365 ; The LBA address of all extended partitions is relative based
366 ; on the LBA address of the extended partition in the MBR, or
367 ; the extended partition at the head of the chain. Thus it is
368 ; necessary to save the LBA address of the first extended partition.
372 mov ebp, [si + part.lba]
377 ; Call find_boot recursively to scan through the extended partition
378 ; table. Load DI with a pointer to the extended table in memory.
380 inc ah ; increment recursion level
381 mov di, kExtPartTable ; partition table pointer
382 call find_boot ; recursion...
386 ; Since there is an "unwritten" rule that limits each partition table
387 ; to have 0 or 1 extended partitions, there is no point in looking for
388 ; any additional extended partition entries at this point. There is no
389 ; boot partition linked beyond the extended partition that was loaded
393 %endif ; EXT_PART_SUPPORT
397 ; Boot partition not found. Giving up.
404 ;--------------------------------------------------------------------------
405 ; load - Load one or more sectors from a partition.
408 ; AL = number of 512-byte sectors to read.
409 ; ES:BX = pointer to where the sectors should be stored.
410 ; DL = drive number (0x80 + unit number)
411 ; SI = pointer to the partition entry.
419 test BYTE [ebios_present], 1
423 mov cx, 5 ; load retry count
425 call read_lba ; use INT13/F42
429 mov cx, 5 ; load retry count
431 call read_chs ; use INT13/F2
440 ;--------------------------------------------------------------------------
441 ; read_chs - Read sectors from a partition using CHS addressing.
444 ; AL = number of 512-byte sectors to read.
445 ; ES:BX = pointer to where the sectors should be stored.
446 ; DL = drive number (0x80 + unit number)
447 ; SI = pointer to the partition entry.
454 pusha ; save all registers
457 ; Read the CHS start values from the partition entry.
459 mov dh, [ si + part.head ] ; drive head
460 mov cx, [ si + part.sect ] ; drive sector + cylinder
463 ; INT13 Func 2 - Read Disk Sectors
467 ; AL = number of sectors to read
468 ; CH = lowest 8 bits of the 10-bit cylinder number
469 ; CL = bits 6 & 7: cylinder number bits 8 and 9
470 ; bits 0 - 5: starting sector number (1-63)
471 ; DH = starting head number (0 to 255)
472 ; DL = drive number (80h + drive unit)
473 ; es:bx = pointer where to place sectors read from disk
476 ; AH = return status (sucess is 0)
477 ; AL = burst error length if ah=0x11 (ECC corrected)
481 mov ah, 0x02 ; Func 2
485 DebugChar('r') ; indicate INT13/F2 error
488 ; Issue a disk reset on error.
489 ; Should this be changed to Func 0xD to skip the diskette controller
494 stc ; set carry to indicate error
500 ;--------------------------------------------------------------------------
501 ; read_lba - Read sectors from a partition using LBA addressing.
504 ; AL = number of 512-byte sectors to read (valid from 1-127).
505 ; ES:BX = pointer to where the sectors should be stored.
506 ; DL = drive number (0x80 + unit number)
507 ; SI = pointer to the partition entry.
514 pusha ; save all registers
515 mov bp, sp ; save current SP
518 ; Create the Disk Address Packet structure for the
519 ; INT13/F42 (Extended Read Sectors) on the stack.
522 ; push DWORD 0 ; offset 12, upper 32-bit LBA
523 push ds ; For sake of saving memory,
524 push ds ; push DS register, which is 0.
526 mov ecx, [ebios_lba] ; offset 8, lower 32-bit LBA
527 add ecx, [si + part.lba]
530 push es ; offset 6, memory segment
532 push bx ; offset 4, memory offset
534 xor ah, ah ; offset 3, must be 0
535 push ax ; offset 2, number of sectors
537 push WORD 16 ; offset 0-1, packet size
540 ; INT13 Func 42 - Extended Read Sectors
544 ; DL = drive number (80h + drive unit)
545 ; DS:SI = pointer to Disk Address Packet
548 ; AH = return status (sucess is 0)
552 ; Packet offset 2 indicates the number of sectors read
561 DebugChar('R') ; indicate INT13/F42 error
564 ; Issue a disk reset on error.
565 ; Should this be changed to Func 0xD to skip the diskette controller
570 stc ; set carry to indicate error
573 mov sp, bp ; restore SP
577 ;--------------------------------------------------------------------------
578 ; Write a string to the console.
581 ; DS:SI pointer to a NULL terminated string.
587 mov bx, 1 ; BH=0, BL=1 (blue)
588 cld ; increment SI after each lodsb call
590 lodsb ; load a byte from DS:SI into AL
591 cmp al, 0 ; Is it a NULL?
592 je .exit ; yes, all done
593 mov ah, 0xE ; INT10 Func 0xE
594 int 0x10 ; display byte in tty mode
602 ;--------------------------------------------------------------------------
603 ; Write a ASCII character to the console.
606 ; AL = ASCII character.
610 mov bx, 1 ; BH=0, BL=1 (blue)
611 mov ah, 0x0e ; bios INT 10, Function 0xE
612 int 0x10 ; display byte in tty mode
617 ;--------------------------------------------------------------------------
618 ; Write a variable number of spaces to the console.
621 ; AL = number to spaces.
626 mov cl, al ; use CX as the loop counter
627 mov al, ' ' ; character to print
637 ;--------------------------------------------------------------------------
638 ; Write the byte value to the console in hex.
641 ; AL = Value to be displayed in hex.
646 call print_nibble ; display upper nibble
648 call print_nibble ; display lower nibble
650 mov al, 10 ; carriage return
661 add al, 'A' - '9' - 1
675 ;--------------------------------------------------------------------------
676 ; NULL terminated strings.
678 boot_error_str db 10, 13, 'Chain booting error', 0
680 ;--------------------------------------------------------------------------
681 ; Pad the rest of the 512 byte sized booter with zeroes. The last
682 ; two bytes is the mandatory boot sector signature.
684 ; If the booter code becomes too large, then nasm will complain
685 ; that the 'times' argument is negative.
688 ; In memory variables.
690 ebios_lba dd 0 ; starting LBA of the intial extended partition.
691 ebios_present db 0 ; 1 if EBIOS is supported, 0 otherwise.
694 times 446-($-$$) db 0
697 times 510-($-$$) db 0