1 ; Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
3 ; @APPLE_LICENSE_HEADER_START@
5 ; Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 ; This file contains Original Code and/or Modifications of Original Code
8 ; as defined in and that are subject to the Apple Public Source License
9 ; Version 2.0 (the 'License'). You may not use this file except in
10 ; compliance with the License. Please obtain a copy of the License at
11 ; http://www.opensource.apple.com/apsl/ and read it before using this
14 ; The Original Code and all software distributed under the License are
15 ; distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 ; EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 ; INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 ; FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 ; Please see the License for the specific language governing rights and
20 ; limitations under the License.
22 ; @APPLE_LICENSE_HEADER_END@
26 ; A small boot sector program written in x86 assembly whose only
27 ; responsibility is to locate the booter partition, load the
28 ; booter into memory, and jump to the booter's entry point.
29 ; The booter partition can be a primary or a logical partition.
31 ; This boot loader can be placed at any of the following places:
32 ; 1. Master Boot Record (MBR)
33 ; 2. Boot sector of an extended partition
34 ; 3. Boot sector of a primary partition
35 ; 4. Boot sector of a logical partition
37 ; In order to coexist with a fdisk partition table (64 bytes), and
38 ; leave room for a two byte signature (0xAA55) in the end, boot0 is
39 ; restricted to 446 bytes (512 - 64 - 2). If boot0 did not have to
40 ; live in the MBR, then we would have 510 bytes to work with.
42 ; boot0 is always loaded by the BIOS or another booter to 0:7C00h.
44 ; This code is written for the NASM assembler.
45 ; nasm boot0.s -o boot0
49 ; Set to 1 to enable obscure debug messages.
54 ; Set to 1 to support loading the booter (boot2) from a
57 EXT_PART_SUPPORT EQU 1
62 kBoot0Segment EQU 0x0000
63 kBoot0Stack EQU 0xFFF0 ; boot0 stack pointer
64 kBoot0LoadAddr EQU 0x7C00 ; boot0 load address
65 kBoot0RelocAddr EQU 0xE000 ; boot0 relocated address
67 kMBRBuffer EQU 0x1000 ; MBR buffer address
68 kExtBuffer EQU 0x1200 ; EXT boot block buffer address
70 kPartTableOffset EQU 0x1be
71 kMBRPartTable EQU kMBRBuffer + kPartTableOffset
72 kExtPartTable EQU kExtBuffer + kPartTableOffset
74 kBoot1uSectors EQU 16 ; sectors to load for boot2
75 kBoot1uAddress EQU 0x0000 ; boot2 load address
76 kBoot1uSegment EQU 0x1000 ; boot2 load segment
78 kSectorBytes EQU 512 ; sector size in bytes
79 kBootSignature EQU 0xAA55 ; boot sector signature
81 kPartCount EQU 4 ; number of paritions per table
82 kPartTypeBoot EQU 0xab ; boot2 partition type
83 kPartTypeUFS EQU 0xa8 ;
84 kPartTypeExtDOS EQU 0x05 ; DOS extended partition type
85 kPartTypeExtWin EQU 0x0f ; Windows extended partition type
86 kPartTypeExtLinux EQU 0x85 ; Linux extended partition type
96 ; In memory variables.
98 ebios_lba dd 0 ; starting LBA of the intial extended partition.
99 ebios_present db 0 ; 1 if EBIOS is supported, 0 otherwise.
102 ; Format of fdisk partition entry.
104 ; The symbol 'part_size' is automatically defined as an `EQU'
105 ; giving the size of the structure.
108 .bootid: resb 1 ; bootable or not
109 .head: resb 1 ; starting head, sector, cylinder
112 .type: resb 1 ; partition type
113 .endhead resb 1 ; ending head, sector, cylinder
116 .lba: resd 1 ; starting lba
117 .sectors resd 1 ; size in sectors
123 %macro DebugCharMacro 1
129 %define DebugChar(x) DebugCharMacro x
134 ;--------------------------------------------------------------------------
135 ; Start of text segment.
139 ORG 0xE000 ; must match kBoot0RelocAddr
141 ;--------------------------------------------------------------------------
142 ; Boot code is loaded at 0:7C00h.
146 ; Set up the stack to grow down from kBoot0Segment:kBoot0Stack.
147 ; Interrupts should be off while the stack is being manipulated.
152 mov sp, kBoot0Stack ; sp <- top of stack
153 sti ; reenable interrupts
159 ; Relocate boot0 code.
161 mov si, kBoot0LoadAddr ; si <- source
162 mov di, kBoot0RelocAddr ; di <- destination
164 cld ; auto-increment SI and/or DI registers
165 mov cx, kSectorBytes/2 ; copy 256 words
166 repnz movsw ; repeat string move (word) operation
168 ; Code relocated, jump to start_reloc in relocated location.
172 ;--------------------------------------------------------------------------
173 ; Start execution from the relocated location.
179 mov dl, kDriveNumber ; starting BIOS drive number
189 ; Clear various flags in memory.
192 mov [ebios_lba], eax ; clear EBIOS LBA offset
193 mov [ebios_present], al ; clear EBIOS support flag
196 ; Since this code may not always reside in the MBR, always start by
197 ; loading the MBR to kMBRBuffer.
199 mov al, 1 ; load one sector
201 mov es, bx ; MBR load segment = 0
202 mov bx, kMBRBuffer ; MBR load address
203 mov si, bx ; pointer to fake partition entry
204 mov WORD [si], 0x0000 ; CHS DX: head = 0
205 mov WORD [si + 2], 0x0001 ; CHS CX: cylinder = 0, sector = 1
208 jc .next_drive ; MBR load error
211 ; Check if EBIOS is supported for this hard drive.
213 mov ah, 0x41 ; Function 0x41
214 mov bx, 0x55AA ; check signature
215 ; mov dl, kDriveNumber ; Drive number
219 ; If successful, the return values are as follows:
222 ; ah = major version of EBIOS extensions (0x21 = version 1.1)
225 ; cx = support bits. bit 0 must be set for function 0x42.
228 cmp bx, 0xAA55 ; check BX = 0xAA55
229 jnz .ebios_check_done
230 test cl, 0x01 ; check enhanced drive read support
231 setnz [ebios_present] ; EBIOS supported, set flag
232 DebugChar('E') ; EBIOS supported
236 ; Look for the booter partition in the MBR partition table,
237 ; which is at offset kMBRPartTable.
239 mov di, kMBRPartTable ; pointer to partition table
240 mov ah, 0 ; initial nesting level is 0
241 call find_boot ; will not return on success
244 ;; inc dl ; next drive number
245 ;; test dl, 0x84 ; went through all 4 drives?
246 ;; jz .loop ; not yet, loop again
248 mov si, boot_error_str
255 ;--------------------------------------------------------------------------
256 ; Find the boot partition and load the booter from the partition.
259 ; AH = recursion nesting level
260 ; DL = drive number (0x80 + unit number)
261 ; DI = pointer to fdisk partition table.
267 push cx ; preserve CX and SI
271 ; Check for boot block signature 0xAA55 following the 4 partition
274 cmp WORD [di + part_size * kPartCount], kBootSignature
275 jne .exit ; boot signature not found
277 mov si, di ; make SI a pointer to partition table
278 mov cx, kPartCount ; number of partition entries per table
282 ; First scan through the partition table looking for the boot
283 ; partition. Postpone walking the extended partition chain for
284 ; the second pass. Do not merge the two without changing the
285 ; buffering scheme used to store extended partition tables.
288 mov al, ah ; indent based on nesting level
290 mov al, [si + part.type] ; print partition type
294 cmp BYTE [si + part.type], kPartTypeUFS
296 cmp BYTE [si + part.bootid], kPartActive
300 ; Found boot partition, read boot1u image to memory.
302 mov al, kBoot1uSectors
303 mov bx, kBoot1uSegment
305 mov bx, kBoot1uAddress
307 jc .continue ; load error, keep looking?
311 ; Jump to boot1u. The drive number is already in register DL.
313 ; The first sector loaded from the disk is reserved for the boot
314 ; block (boot1), adjust the jump location by adding a sector offset.
316 jmp kBoot1uSegment:kBoot1uAddress + kSectorBytes
319 add si, part_size ; advance SI to next partition entry
320 loop .loop ; loop through all partition entries
324 ; No primary (or logical) boot partition found in the current
325 ; partition table. Restart and look for extended partitions.
327 mov si, di ; make SI a pointer to partition table
328 mov cx, kPartCount ; number of partition entries per table
332 mov al, [si + part.type] ; AL <- partition type
334 cmp al, kPartTypeExtDOS ; Extended DOS
337 cmp al, kPartTypeExtWin ; Extended Windows(95)
340 cmp al, kPartTypeExtLinux ; Extended Linux
345 ; Advance si to the next partition entry in the extended
348 add si, part_size ; advance SI to next partition entry
349 loop .ext_loop ; loop through all partition entries
350 jmp .exit ; boot partition not found
354 ; Setup the arguments for the load function call to bring the
355 ; extended partition table into memory.
356 ; Remember that SI points to the extended partition entry.
358 mov al, 1 ; read 1 sector
361 mov bx, kExtBuffer ; load extended boot sector
363 jc .ext_continue ; load error
366 ; The LBA address of all extended partitions is relative based
367 ; on the LBA address of the extended partition in the MBR, or
368 ; the extended partition at the head of the chain. Thus it is
369 ; necessary to save the LBA address of the first extended partition.
373 mov ebp, [si + part.lba]
378 ; Call find_boot recursively to scan through the extended partition
379 ; table. Load DI with a pointer to the extended table in memory.
381 inc ah ; increment recursion level
382 mov di, kExtPartTable ; partition table pointer
383 call find_boot ; recursion...
387 ; Since there is an "unwritten" rule that limits each partition table
388 ; to have 0 or 1 extended partitions, there is no point in looking for
389 ; any additional extended partition entries at this point. There is no
390 ; boot partition linked beyond the extended partition that was loaded
394 %endif ; EXT_PART_SUPPORT
398 ; 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
430 mov cx, 5 ; load retry count
432 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 dl, kDriveNumber
482 mov ah, 0x02 ; Func 2
486 DebugChar('r') ; indicate INT13/F2 error
489 ; Issue a disk reset on error.
490 ; Should this be changed to Func 0xD to skip the diskette controller
495 stc ; set carry to indicate error
501 ;--------------------------------------------------------------------------
502 ; read_lba - Read sectors from a partition using LBA addressing.
505 ; AL = number of 512-byte sectors to read (valid from 1-127).
506 ; ES:BX = pointer to where the sectors should be stored.
507 ; DL = drive number (0x80 + unit number)
508 ; SI = pointer to the partition entry.
515 pusha ; save all registers
516 mov bp, sp ; save current SP
519 ; Create the Disk Address Packet structure for the
520 ; INT13/F42 (Extended Read Sectors) on the stack.
523 ; push DWORD 0 ; offset 12, upper 32-bit LBA
524 push ds ; For sake of saving memory,
525 push ds ; push DS register, which is 0.
527 mov ecx, [ebios_lba] ; offset 8, lower 32-bit LBA
528 add ecx, [si + part.lba]
531 push es ; offset 6, memory segment
533 push bx ; offset 4, memory offset
535 xor ah, ah ; offset 3, must be 0
536 push ax ; offset 2, number of sectors
538 push WORD 16 ; offset 0-1, packet size
541 ; INT13 Func 42 - Extended Read Sectors
545 ; DL = drive number (80h + drive unit)
546 ; DS:SI = pointer to Disk Address Packet
549 ; AH = return status (sucess is 0)
553 ; Packet offset 2 indicates the number of sectors read
556 ; mov dl, kDriveNumber
563 DebugChar('R') ; indicate INT13/F42 error
566 ; Issue a disk reset on error.
567 ; Should this be changed to Func 0xD to skip the diskette controller
572 stc ; set carry to indicate error
575 mov sp, bp ; restore SP
579 ;--------------------------------------------------------------------------
580 ; Write a string to the console.
583 ; DS:SI pointer to a NULL terminated string.
589 mov bx, 1 ; BH=0, BL=1 (blue)
590 cld ; increment SI after each lodsb call
592 lodsb ; load a byte from DS:SI into AL
593 cmp al, 0 ; Is it a NULL?
594 je .exit ; yes, all done
595 mov ah, 0xE ; INT10 Func 0xE
596 int 0x10 ; display byte in tty mode
603 ;--------------------------------------------------------------------------
604 ; Write a ASCII character to the console.
607 ; AL = ASCII character.
611 mov bx, 1 ; BH=0, BL=1 (blue)
612 mov ah, 0x0e ; bios INT 10, Function 0xE
613 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
636 ;--------------------------------------------------------------------------
637 ; Write the byte value to the console in hex.
640 ; AL = Value to be displayed in hex.
645 call print_nibble ; display upper nibble
647 call print_nibble ; display lower nibble
649 mov al, 10 ; carriage return
660 add al, 'A' - '9' - 1
667 ;--------------------------------------------------------------------------
668 ; NULL terminated strings.
670 boot_error_str db 10, 13, 'Error', 0
672 ;--------------------------------------------------------------------------
673 ; Pad the rest of the 512 byte sized booter with zeroes. The last
674 ; two bytes is the mandatory boot sector signature.
676 ; If the booter code becomes too large, then nasm will complain
677 ; that the 'times' argument is negative.
680 ;; times 446-($-$$) db 0
683 ;--------------------------------------------------------------------------
684 ; Put fake partition entries for the bootable floppy image
686 part1bootid db 0x80 ; first partition active
687 part1head db 0x00 ; head #
688 part1sect db 0x02 ; sector # (low 6 bits)
689 part1cyl db 0x00 ; cylinder # (+ high 2 bits of above)
690 part1systid db 0xab ; Apple boot partition
691 times 3 db 0x00 ; ignore head/cyl/sect #'s
692 part1relsect dd 0x00000001 ; start at sector 1
693 part1numsect dd 0x00000080 ; 64K for booter
694 part2bootid db 0x00 ; not active
695 times 3 db 0x00 ; ignore head/cyl/sect #'s
696 part2systid db 0xa8 ; Apple UFS partition
697 times 3 db 0x00 ; ignore head/cyl/sect #'s
698 part2relsect dd 0x00000082 ; start after booter
699 ; part2numsect dd 0x00000abe ; 1.44MB - 65K
700 part2numsect dd 0x000015fe ; 2.88MB - 65K
704 times 510-($-$$) db 0