2 # Copyright (c) 1998 Robert Nordier
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
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
16 # $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.10.2.3 2001/08/14 22:55:29 jhb Exp $
17 # $DragonFly: src/sys/boot/pc32/boot2/boot1.S,v 1.2 2003/06/17 04:28:18 dillon Exp $
20 .set MEM_REL,0x700 # Relocation address
21 .set MEM_ARG,0x900 # Arguments
22 .set MEM_ORG,0x7c00 # Origin
23 .set MEM_BUF,0x8c00 # Load area
24 .set MEM_BTX,0x9000 # BTX start
25 .set MEM_JMP,0x9010 # BTX entry point
26 .set MEM_USR,0xa000 # Client start
27 .set BDA_BOOT,0x472 # Boot howto flag
30 .set PRT_OFF,0x1be # Partition offset
31 .set PRT_NUM,0x4 # Partitions
32 .set PRT_BSD,0xa5 # Partition type
35 .set FL_PACKET,0x80 # Packet mode
38 .set SIZ_PAG,0x1000 # Page size
39 .set SIZ_SEC,0x200 # Sector size
45 start: jmp main # Start recognizably
47 # This is the start of a standard BIOS Parameter Block (BPB). Most bootable
48 # FAT disks have this at the start of their MBR. While normal BIOS's will
49 # work fine without this section, IBM's El Torito emulation "fixes" up the
50 # BPB by writing into the memory copy of the MBR. Rather than have data
51 # written into our xread routine, we'll define a BPB to work around it.
52 # The data marked with (T) indicates a field required for a ThinkPad to
53 # recognize the disk and (W) indicates fields written from IBM BIOS code.
54 # The use of the BPB is based on what OpenBSD and NetBSD implemented in
55 # their boot code but the required fields were determined by trial and error.
57 # Note: If additional space is needed in boot1, one solution would be to
58 # move the "prompt" message data (below) to replace the OEM ID.
61 oemid: .space 0x08, 0x00 # OEM ID
64 bpb: .word 512 # sector size (T)
65 .byte 0 # sectors/clustor
66 .word 0 # reserved sectors
67 .byte 0 # number of FATs
68 .word 0 # root entries
69 .word 0 # small sectors
70 .byte 0 # media type (W)
72 .word 18 # sectors per track (T)
73 .word 2 # number of heads (T)
74 .long 0 # hidden sectors (W)
75 .long 0 # large sectors
78 ebpb: .byte 0 # BIOS physical drive number (W)
82 # Trampoline used by boot2 to call read to read data from the disk via
83 # the BIOS. Call with:
85 # %cx:%ax - long - LBA to read in
86 # %es:(%bx) - caddr_t - buffer to read data into
87 # %dl - byte - drive to read from
88 # %dh - byte - num sectors to read
91 xread: push %ss # Address
94 # Setup an EDD disk packet and pass it to read
100 push %es # Address of
101 push %bx # transfer buffer
102 xor %ax,%ax # Number of
103 movb %dh,%al # blocks to
105 push $0x10 # Size of packet
106 mov %sp,%bp # Packet pointer
107 callw read # Read from disk
108 lea 0x10(%bp),%sp # Clear stack
111 # Load the rest of boot2 and BTX up, copy the parts to the right locations,
112 # and start it all up.
116 # Setup the segment registers to flat addressing (segment 0) and setup the
117 # stack to end just below the start of our code.
119 main: cld # String ops inc
121 mov %cx,%es # Address
124 mov $start,%sp # stack
126 # Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets
130 mov $MEM_REL,%di # Destination
131 incb %ch # Word count
135 # If we are on a hard drive, then load the MBR and look for the first
136 # FreeBSD slice. We use the fake partition entry below that points to
137 # the MBR when we call nread. The first pass looks for the first active
138 # FreeBSD slice. The second pass looks for the first non-active FreeBSD
139 # slice if the first one fails.
141 mov $part4,%si # Partition
142 cmpb $0x80,%dl # Hard drive?
144 movb $0x1,%dh # Block count
145 callw nread # Read MBR
146 mov $0x1,%cx # Two passes
147 main.1: mov $MEM_BUF+PRT_OFF,%si # Partition table
148 movb $0x1,%dh # Partition
149 main.2: cmpb $PRT_BSD,0x4(%si) # Our partition type?
151 jcxz main.5 # If second pass
152 testb $0x80,(%si) # Active?
154 main.3: add $0x10,%si # Next entry
156 cmpb $0x1+PRT_NUM,%dh # In table?
161 # If we get here, we didn't find any FreeBSD slices at all, so print an
162 # error message and die.
164 mov $msg_part,%si # Message
167 # Floppies use partition 0 of drive 0.
169 main.4: xor %dx,%dx # Partition:drive
171 # Ok, we have a slice and drive in %dx now, so use that to locate and load
172 # boot2. %si references the start of the slice we are looking for, so go
173 # ahead and load up the first 16 sectors (boot1 + boot2) from that. When
174 # we read it in, we conveniently use 0x8c00 as our transfer buffer. Thus,
175 # boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.
176 # The first part of boot2 is the disklabel, which is 0x200 bytes long.
177 # The second part is BTX, which is thus loaded into 0x9000, which is where
178 # it also runs from. The boot2.bin binary starts right after the end of
179 # BTX, so we have to figure out where the start of it is and then move the
180 # binary to 0xb000. Normally, BTX clients start at MEM_USR, or 0xa000, but
181 # when we use btxld create boot2, we use an entry point of 0x1000. That
182 # entry point is relative to MEM_USR; thus boot2.bin starts at 0xb000.
184 main.5: mov %dx,MEM_ARG # Save args
185 movb $0x10,%dh # Sector count
186 callw nread # Read disk
187 mov $MEM_BTX,%bx # BTX
188 mov 0xa(%bx),%si # Get BTX length and set
189 add %bx,%si # %si to start of boot2.bin
190 mov $MEM_USR+SIZ_PAG,%di # Client page 1
191 mov $MEM_BTX+0xe*SIZ_SEC,%cx # Byte
195 sub %di,%cx # Byte count
196 xorb %al,%al # Zero assumed bss from
197 rep # the end of boot2.bin
198 stosb # up to 0x10000
199 callw seta20 # Enable A20
200 jmp start+MEM_JMP-MEM_ORG # Start BTX
202 # Enable A20 so we can access memory above 1 meg.
204 seta20: cli # Disable interrupts
205 seta20.1: inb $0x64,%al # Get status
206 testb $0x2,%al # Busy?
208 movb $0xd1,%al # Command: Write
209 outb %al,$0x64 # output port
210 seta20.2: inb $0x64,%al # Get status
211 testb $0x2,%al # Busy?
213 movb $0xdf,%al # Enable
215 sti # Enable interrupts
218 # Trampoline used to call read from within boot1.
220 nread: mov $MEM_BUF,%bx # Transfer buffer
221 mov 0x8(%si),%ax # Get
222 mov 0xa(%si),%cx # LBA
225 jnc return # If success, return
226 mov $msg_read,%si # Otherwise, set the error
227 # message and fall through to
230 # Print out the error message pointed to by %ds:(%si) followed
231 # by a prompt, wait for a keypress, and then reboot the machine.
233 error: callw putstr # Display message
234 mov $prompt,%si # Display
235 callw putstr # prompt
236 xorb %ah,%ah # BIOS: Get
238 movw $0x1234, BDA_BOOT # Do a warm boot
239 ljmp $0xffff,$0x0 # reboot the machine
241 # Display a null-terminated string using the BIOS output.
243 putstr.0: mov $0x7,%bx # Page:attribute
244 movb $0xe,%ah # BIOS: Display
245 int $0x10 # character
246 putstr: lodsb # Get char
247 testb %al,%al # End of string?
251 # Overused return code. ereturn is used to return an error from the
252 # read function. Since we assume putstr succeeds, we (ab)use the
253 # same code when we return from putstr.
255 ereturn: movb $0x1,%ah # Invalid
257 return: retw # To caller
259 # Reads sectors from the disk. If EDD is enabled, then check if it is
260 # installed and use it if it is. If it is not installed or not enabled, then
261 # fall back to using CHS. Since we use a LBA, if we are using CHS, we have to
262 # fetch the drive parameters from the BIOS and divide it out ourselves.
265 # %dl - byte - drive number
266 # stack - 10 bytes - EDD Packet
268 read: push %dx # Save
269 movb $0x8,%ah # BIOS: Get drive
270 int $0x13 # parameters
271 movb %dh,%ch # Max head number
274 andb $0x3f,%cl # Sectors per track
276 cli # Disable interrupts
277 mov 0x8(%bp),%eax # Get LBA
279 movzbl %cl,%ebx # Divide by
280 xor %edx,%edx # sectors
282 movb %ch,%bl # Max head number
283 movb %dl,%ch # Sector number
285 xorb %dl,%dl # number
287 movb %dl,%bh # Head number
289 cmpl $0x3ff,%eax # Cylinder number supportable?
290 sti # Enable interrupts
291 ja read.7 # No, try EDD
292 xchgb %al,%ah # Set up cylinder
293 rorb $0x2,%al # number
296 xchg %ax,%cx # number
297 movb %bh,%dh # Head number
298 subb %ah,%al # Sectors this track
299 mov 0x2(%bp),%ah # Blocks to read
300 cmpb %ah,%al # To read
303 read.2: mov $0x5,%di # Try count
304 read.3: les 0x4(%bp),%bx # Transfer buffer
306 movb $0x2,%ah # BIOS: Read
307 int $0x13 # from disk
309 jnc read.4 # If success
312 xorb %ah,%ah # BIOS: Reset
313 int $0x13 # disk system
314 xchg %bx,%ax # Block count
315 jmp read.3 # Continue
316 read.4: movzbw %bl,%ax # Sectors read
317 add %ax,0x8(%bp) # Adjust
319 incw 0xa(%bp) # transfer
320 read.5: shlb %bl # buffer
321 add %bl,0x5(%bp) # pointer,
322 sub %al,0x2(%bp) # block count
323 ja read # If not done
324 read.6: retw # To caller
325 read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
326 jz ereturn # No, so return an error
327 mov $0x55aa,%bx # Magic
329 movb $0x41,%ah # BIOS: Check
330 int $0x13 # extensions present
332 jc return # If error, return an error
333 cmp $0xaa55,%bx # Magic?
334 jne ereturn # No, so return an error
335 testb $0x1,%cl # Packet interface?
336 jz ereturn # No, so return an error
337 mov %bp,%si # Disk packet
338 movb $0x42,%ah # BIOS: Extended
344 msg_read: .asciz "Read"
345 msg_part: .asciz "Boot"
347 prompt: .asciz " error\r\n"
349 flags: .byte FLAGS # Flags
356 part4: .byte 0x80, 0x00, 0x01, 0x00
357 .byte 0xa5, 0xff, 0xff, 0xff
358 .byte 0x00, 0x00, 0x00, 0x00
359 .byte 0x50, 0xc3, 0x00, 0x00 # 50000 sectors long, bleh
361 .word 0xaa55 # Magic number