1/* 2 * Copyright (c) 1998 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are freely 6 * permitted provided that the above copyright notice and this 7 * paragraph and the following disclaimer are duplicated in all 8 * such forms. 9 * 10 * This software is provided "AS IS" and without any express or 11 * implied warranties, including, without limitation, the implied 12 * warranties of merchantability and fitness for a particular 13 * purpose. 14 */ 15 16/* Memory Locations */ 17 .set MEM_ARG,0x900 # Arguments 18 .set MEM_ORG,0x7c00 # Origin 19 .set MEM_BUF,0x8000 # Load area 20 .set MEM_BTX,0x9000 # BTX start 21 .set MEM_JMP,0x9010 # BTX entry point 22 .set MEM_USR,0xa000 # Client start 23 .set BDA_BOOT,0x472 # Boot howto flag 24 25/* Partition Constants */ 26 .set PRT_OFF,0x1be # Partition offset 27 .set PRT_NUM,0x4 # Partitions 28 .set PRT_BSD,0xa5 # Partition type 29 30/* Misc. Constants */ 31 .set SIZ_PAG,0x1000 # Page size 32 .set SIZ_SEC,0x200 # Sector size 33 .set COPY_BLKS,0x8 # Number of blocks 34 # to copy for boot2 (<= 15) 35 .set COPY_BLK_SZ,0x8000 # Copy in 32k blocks; must be 36 # a multiple of 16 bytes 37 .set NSECT,(COPY_BLK_SZ / SIZ_SEC * COPY_BLKS) 38 .globl start 39 .code16 40 41/* 42 * Load the rest of zfsboot2 and BTX up, copy the parts to the right locations, 43 * and start it all up. 44 */ 45 46/* 47 * Setup the segment registers to flat addressing (segment 0) and setup the 48 * stack to end just below the start of our code. 49 */ 50start: cld # String ops inc 51 xor %cx,%cx # Zero 52 mov %cx,%es # Address 53 mov %cx,%ds # data 54 mov %cx,%ss # Set up 55 mov $start,%sp # stack 56/* 57 * Load the MBR and look for the first FreeBSD slice. We use the fake 58 * partition entry below that points to the MBR when we call read. 59 * The first pass looks for the first active FreeBSD slice. The 60 * second pass looks for the first non-active FreeBSD slice if the 61 * first one fails. 62 */ 63 call check_edd # Make sure EDD works 64 mov $part4,%si # Dummy partition 65 xor %eax,%eax # Read MBR 66 movl $MEM_BUF,%ebx # from first 67 call read # sector 68 mov $0x1,%cx # Two passes 69main.1: mov $MEM_BUF+PRT_OFF,%si # Partition table 70 movb $0x1,%dh # Partition 71main.2: cmpb $PRT_BSD,0x4(%si) # Our partition type? 72 jne main.3 # No 73 jcxz main.5 # If second pass 74 testb $0x80,(%si) # Active? 75 jnz main.5 # Yes 76main.3: add $0x10,%si # Next entry 77 incb %dh # Partition 78 cmpb $0x1+PRT_NUM,%dh # In table? 79 jb main.2 # Yes 80 dec %cx # Do two 81 jcxz main.1 # passes 82/* 83 * If we get here, we didn't find any FreeBSD slices at all, so print an 84 * error message and die. 85 */ 86 mov $msg_part,%si # Message 87 jmp error # Error 88 89/* 90 * Ok, we have a slice and drive in %dx now, so use that to locate and 91 * load boot2. %si references the start of the slice we are looking 92 * for, so go ahead and load up the COPY_BLKS*COPY_BLK_SZ/SIZ_SEC sectors 93 * starting at sector 1024 (i.e. after the two vdev labels). We don't 94 * have do anything fancy here to allow for an extra copy of boot1 and 95 * a partition table (compare to this section of the UFS bootstrap) so we 96 * just load it all at 0x9000. The first part of boot2 is BTX, which wants 97 * to run at 0x9000. The boot2.bin binary starts right after the end of BTX, 98 * so we have to figure out where the start of it is and then move the 99 * binary to 0xc000. Normally, BTX clients start at MEM_USR, or 0xa000, 100 * but when we use btxld to create zfsboot2, we use an entry point of 101 * 0x2000. That entry point is relative to MEM_USR; thus boot2.bin 102 * starts at 0xc000. 103 * 104 * The load area and the target area for the client overlap so we have 105 * to use a decrementing string move. We also play segment register 106 * games with the destination address for the move so that the client 107 * can be larger than 16k (which would overflow the zero segment since 108 * the client starts at 0xc000). 109 */ 110main.5: mov %dx,MEM_ARG # Save args 111 mov $NSECT,%cx # Sector count 112 movl $1024,%eax # Offset to boot2 113 mov $MEM_BTX,%ebx # Destination buffer 114main.6: pushal # Save params 115 call read # Read disk 116 popal # Restore 117 incl %eax # Advance to 118 add $SIZ_SEC,%ebx # next sector 119 loop main.6 # If not last, read another 120 121 mov $MEM_BTX,%bx # BTX 122 mov 0xa(%bx),%si # Get BTX length and set 123 add %bx,%si # %si to start of boot2 124 dec %si # Set %ds:%si to point at the 125 mov %si,%ax # last byte we want to copy 126 shr $4,%ax # from boot2, with %si made as 127 add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # small as possible. 128 and $0xf,%si # 129 mov %ax,%ds # 130 mov $(MEM_USR+2*SIZ_PAG)/16,%ax # Set %es:(-1) to point at 131 add $(COPY_BLKS*COPY_BLK_SZ/16),%ax # the last byte we 132 mov %ax,%es # want to copy boot2 into. 133 mov $COPY_BLKS,%bx # Copy COPY_BLKS 32k blocks 134copyloop: 135 add $COPY_BLK_SZ,%si # Adjust %ds:%si to point at 136 mov %ds,%ax # the end of the next 32k to 137 sub $COPY_BLK_SZ/16,%ax # copy from boot2 138 mov %ax,%ds 139 mov $COPY_BLK_SZ-1,%di # Adjust %es:%di to point at 140 mov %es,%ax # the end of the next 32k into 141 sub $COPY_BLK_SZ/16,%ax # which we want boot2 copied 142 mov %ax,%es 143 mov $COPY_BLK_SZ,%cx # Copy 32k 144 std 145 rep movsb 146 dec %bx 147 jnz copyloop 148 mov %cx,%ds # Reset %ds and %es 149 mov %cx,%es 150 cld # Back to increment 151 152/* 153 * Enable A20 so we can access memory above 1 meg. 154 * Use the zero-valued %cx as a timeout for embedded hardware which do not 155 * have a keyboard controller. 156 */ 157seta20: cli # Disable interrupts 158seta20.1: dec %cx # Timeout? 159 jz seta20.3 # Yes 160 inb $0x64,%al # Get status 161 testb $0x2,%al # Busy? 162 jnz seta20.1 # Yes 163 movb $0xd1,%al # Command: Write 164 outb %al,$0x64 # output port 165seta20.2: inb $0x64,%al # Get status 166 testb $0x2,%al # Busy? 167 jnz seta20.2 # Yes 168 movb $0xdf,%al # Enable 169 outb %al,$0x60 # A20 170seta20.3: sti # Enable interrupts 171 172 jmp start+MEM_JMP-MEM_ORG # Start BTX 173 174 175/* 176 * Read a sector from the disk. Sets up an EDD packet on the stack 177 * and passes it to read. We assume that the destination address is 178 * always segment-aligned. 179 * 180 * %eax - int - LBA to read in relative to partition start 181 * %ebx - ptr - destination address 182 * %dl - byte - drive to read from 183 * %si - ptr - MBR partition entry 184 */ 185read: xor %ecx,%ecx # Get 186 addl 0x8(%si),%eax # LBA 187 adc $0,%ecx 188 pushl %ecx # Starting absolute block 189 pushl %eax # block number 190 shr $4,%ebx # Convert to segment 191 push %bx # Address of 192 push $0 # transfer buffer 193 push $0x1 # Read 1 sector 194 push $0x10 # Size of packet 195 mov %sp,%si # Packet pointer 196 mov $0x42,%ah # BIOS: Extended 197 int $0x13 # read 198 jc read.1 # If error, fail 199 lea 0x10(%si),%sp # Clear stack 200 ret # If success, return 201read.1: mov %ah,%al # Format 202 mov $read_err,%di # error 203 call hex8 # code 204 mov $msg_read,%si # Set the error message and 205 # fall through to the error 206 # routine 207/* 208 * Print out the error message pointed to by %ds:(%si) followed 209 * by a prompt, wait for a keypress, and then reboot the machine. 210 */ 211error: callw putstr # Display message 212 mov $prompt,%si # Display 213 callw putstr # prompt 214 xorb %ah,%ah # BIOS: Get 215 int $0x16 # keypress 216 movw $0x1234, BDA_BOOT # Do a warm boot 217 ljmp $0xffff,$0x0 # reboot the machine 218/* 219 * Display a null-terminated string using the BIOS output. 220 */ 221putstr.0: mov $0x7,%bx # Page:attribute 222 movb $0xe,%ah # BIOS: Display 223 int $0x10 # character 224putstr: lodsb # Get char 225 testb %al,%al # End of string? 226 jne putstr.0 # No 227 ret # To caller 228/* 229 * Check to see if the disk supports EDD. zfsboot requires EDD and does not 230 * support older C/H/S disk I/O. 231 */ 232check_edd: cmpb $0x80,%dl # Hard drive? 233 jb check_edd.1 # No, fail to boot 234 mov $0x55aa,%bx # Magic 235 push %dx # Save 236 movb $0x41,%ah # BIOS: Check 237 int $0x13 # extensions present 238 pop %dx # Restore 239 jc check_edd.1 # If error, fail 240 cmp $0xaa55,%bx # Magic? 241 jne check_edd.1 # No, so fail 242 testb $0x1,%cl # Packet interface? 243 jz check_edd.1 # No, so fail 244 ret # EDD ok, keep booting 245check_edd.1: mov $msg_chs,%si # Warn that CHS is 246 jmp error # unsupported and fail 247/* 248 * AL to hex, saving the result to [EDI]. 249 */ 250hex8: push %ax # Save 251 shrb $0x4,%al # Do upper 252 call hex8.1 # 4 253 pop %ax # Restore 254hex8.1: andb $0xf,%al # Get lower 4 255 cmpb $0xa,%al # Convert 256 sbbb $0x69,%al # to hex 257 das # digit 258 orb $0x20,%al # To lower case 259 stosb # Save char 260 ret # (Recursive) 261 262/* Messages */ 263 264msg_chs: .asciz "CHS not supported" 265msg_read: .ascii "Read error: " 266read_err: .asciz "XX" 267msg_part: .asciz "Boot error" 268 269prompt: .asciz "\r\n" 270 271 .org PRT_OFF,0x90 272 273/* Partition table */ 274 275 .fill 0x30,0x1,0x0 276part4: .byte 0x80, 0x00, 0x01, 0x00 277 .byte 0xa5, 0xfe, 0xff, 0xff 278 .byte 0x00, 0x00, 0x00, 0x00 279 .byte 0x50, 0xc3, 0x00, 0x00 # 50000 sectors long, bleh 280 281 .word 0xaa55 # Magic number 282