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
15 * $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.23 2003/08/22 01:59:28 imp Exp $
16 * $DragonFly: src/sys/boot/pc32/boot2/boot1.S,v 1.5 2004/07/18 23:40:01 dillon Exp $
19 #include "../bootasm.h"
21 // Partition Constants
22 .set PRT_OFF,0x1be // Partition offset
23 .set PRT_NUM,0x4 // Partitions
24 .set PRT_BSD,0xa5 // Partition type
27 .set FL_PACKET,0x80 // Packet mode
30 .set SIZ_PAG,0x1000 // Page size
31 .set SIZ_SEC,0x200 // Sector size
38 start: jmp main // Start recognizably
40 // This is the start of a standard BIOS Parameter Block (BPB). Most bootable
41 // FAT disks have this at the start of their MBR. While normal BIOS's will
42 // work fine without this section, IBM's El Torito emulation "fixes" up the
43 // BPB by writing into the memory copy of the MBR. Rather than have data
44 // written into our xread routine, we'll define a BPB to work around it.
45 // The data marked with (T) indicates a field required for a ThinkPad to
46 // recognize the disk and (W) indicates fields written from IBM BIOS code.
47 // The use of the BPB is based on what OpenBSD and NetBSD implemented in
48 // their boot code but the required fields were determined by trial and error.
50 // Note: If additional space is needed in boot1, one solution would be to
51 // move the "prompt" message data (below) to replace the OEM ID.
54 oemid: .space 0x08, 0x00 // OEM ID
57 bpb: .word 512 // sector size (T)
58 .byte 0 // sectors/clustor
59 .word 0 // reserved sectors
60 .byte 0 // number of FATs
61 .word 0 // root entries
62 .word 0 // small sectors
63 .byte 0 // media type (W)
64 .word 0 // sectors/fat
65 .word 18 // sectors per track (T)
66 .word 2 // number of heads (T)
67 .long 0 // hidden sectors (W)
68 .long 0 // large sectors
71 ebpb: .byte 0 // BIOS physical drive number (W)
75 // Trampoline used by boot2 to call read to read data from the disk via
76 // the BIOS. Call with:
78 // %cx:%ax - long - LBA to read in
79 // %es:(%bx) - caddr_t - buffer to read data into
80 // %dl - byte - drive to read from
81 // %dh - byte - num sectors to read
84 xread: push %ss // Address
87 // Setup an EDD disk packet and pass it to read
90 pushl $0x0 // absolute
93 push %es // Address of
94 push %bx // transfer buffer
95 xor %ax,%ax // Number of
96 movb %dh,%al // blocks to
98 push $0x10 // Size of packet
99 mov %sp,%bp // Packet pointer
100 callw read // Read from disk
101 lea 0x10(%bp),%sp // Clear stack
102 lret // To far caller
104 // Load the rest of boot2 and BTX up, copy the parts to the right locations,
105 // and start it all up.
109 // Setup the segment registers to flat addressing (segment 0) and setup the
110 // stack to end just below the start of our code.
112 main: cld // String ops inc
114 mov %cx,%es // Address
116 mov %cx,%ss // Set up
117 mov $start,%sp // stack
119 // Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets
122 mov %sp,%si // Source
123 mov $MEM_REL,%di // Destination
124 incb %ch // Word count
128 // If we are on a hard drive, then load the MBR and look for the first
129 // FreeBSD slice. We use the fake partition entry below that points to
130 // the MBR when we call nread. The first pass looks for the first active
131 // FreeBSD slice. The second pass looks for the first non-active FreeBSD
132 // slice if the first one fails.
134 mov $part4,%si // Partition
135 cmpb $0x80,%dl // Hard drive?
137 movb $0x1,%dh // Block count
138 callw nread // Read MBR
139 mov $0x1,%cx // Two passes
140 main.1: mov $MEM_BUF+PRT_OFF,%si // Partition table
141 movb $0x1,%dh // Partition
142 main.2: cmpb $PRT_BSD,0x4(%si) // Our partition type?
144 jcxz main.5 // If second pass
145 testb $0x80,(%si) // Active?
147 main.3: add $0x10,%si // Next entry
148 incb %dh // Partition
149 cmpb $0x1+PRT_NUM,%dh // In table?
152 jcxz main.1 // passes
154 // If we get here, we didn't find any FreeBSD slices at all, so print an
155 // error message and die.
157 mov $msg_part,%si // Message
160 // Floppies use partition 0 of drive 0.
162 main.4: xor %dx,%dx // Partition:drive
164 // Ok, we have a slice and drive in %dx now, so use that to locate and load
165 // boot2. %si references the start of the slice we are looking for, so go
166 // ahead and load up the first 16 sectors (boot1 + boot2) from that. When
167 // we read it in, we conveniently use 0x8c00 as our transfer buffer. Thus,
168 // boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.
169 // The first part of boot2 is the disklabel, which is 0x200 bytes long.
170 // The second part is BTX, which is thus loaded into 0x9000, which is where
171 // it also runs from. The boot2.bin binary starts right after the end of
172 // BTX, so we have to figure out where the start of it is and then move the
173 // binary to 0xc000. Normally, BTX clients start at MEM_USR, or 0xa000, but
174 // when we use btxld to create boot2, we use an entry point of 0x2000. That
175 // entry point is relative to MEM_USR; thus boot2.bin starts at 0xc000.
177 main.5: mov %dx,MEM_ARG // Save args
178 movb $NSECT,%dh // Sector count
179 callw nread // Read disk
180 mov $MEM_BTX,%bx // BTX
181 mov 0xa(%bx),%si // Get BTX length and set
182 add %bx,%si // %si to start of boot2.bin
183 mov $MEM_USR+SIZ_PAG*2,%di // Client page 2
184 mov $MEM_BTX+(NSECT-1)*SIZ_SEC,%cx // Byte
188 sub %di,%cx // Byte count
189 xorb %al,%al // Zero assumed bss from
190 rep // the end of boot2.bin
191 stosb // up to 0x10000
192 callw seta20 // Enable A20
193 jmp start+MEM_JMP-MEM_ORG // Start BTX
195 // Enable A20 so we can access memory above 1 meg.
197 seta20: cli // Disable interrupts
198 seta20.1: inb $0x64,%al // Get status
199 testb $0x2,%al // Busy?
201 movb $0xd1,%al // Command: Write
202 outb %al,$0x64 // output port
203 seta20.2: inb $0x64,%al // Get status
204 testb $0x2,%al // Busy?
206 movb $0xdf,%al // Enable
207 outb %al,$0x60 // A20
208 sti // Enable interrupts
211 // Trampoline used to call read from within boot1.
213 nread: mov $MEM_BUF,%bx // Transfer buffer
214 mov 0x8(%si),%ax // Get
215 mov 0xa(%si),%cx // LBA
216 push %cs // Read from
217 callw xread.1 // disk
218 jnc return // If success, return
219 mov $msg_read,%si // Otherwise, set the error
220 // message and fall through to
223 // Print out the error message pointed to by %ds:(%si) followed
224 // by a prompt, wait for a keypress, and then reboot the machine.
226 error: callw putstr // Display message
227 mov $prompt,%si // Display
228 callw putstr // prompt
229 xorb %ah,%ah // BIOS: Get
230 int $0x16 // keypress
231 movw $0x1234, BDA_BOOT // Do a warm boot
232 ljmp $0xffff,$0x0 // reboot the machine
234 // Display a null-terminated string using the BIOS output.
236 putstr.0: mov $0x7,%bx // Page:attribute
237 movb $0xe,%ah // BIOS: Display
238 int $0x10 // character
239 putstr: lodsb // Get char
240 testb %al,%al // End of string?
244 // Overused return code. ereturn is used to return an error from the
245 // read function. Since we assume putstr succeeds, we (ab)use the
246 // same code when we return from putstr.
248 ereturn: movb $0x1,%ah // Invalid
250 return: retw // To caller
252 // Reads sectors from the disk. If EDD is enabled, then check if it is
253 // installed and use it if it is. If it is not installed or not enabled, then
254 // fall back to using CHS. Since we use a LBA, if we are using CHS, we have to
255 // fetch the drive parameters from the BIOS and divide it out ourselves.
258 // %dl - byte - drive number
259 // stack - 10 bytes - EDD Packet
261 read: push %dx // Save
262 movb $0x8,%ah // BIOS: Get drive
263 int $0x13 // parameters
264 movb %dh,%ch // Max head number
266 jc return // If error
267 andb $0x3f,%cl // Sectors per track
268 jz ereturn // If zero
269 cli // Disable interrupts
270 mov 0x8(%bp),%eax // Get LBA
272 movzbl %cl,%ebx // Divide by
273 xor %edx,%edx // sectors
274 div %ebx // per track
275 movb %ch,%bl // Max head number
276 movb %dl,%ch // Sector number
278 xorb %dl,%dl // number
280 movb %dl,%bh // Head number
282 cmpl $0x3ff,%eax // Cylinder number supportable?
283 sti // Enable interrupts
284 ja read.7 // No, try EDD
285 xchgb %al,%ah // Set up cylinder
286 rorb $0x2,%al // number
289 xchg %ax,%cx // number
290 movb %bh,%dh // Head number
291 subb %ah,%al // Sectors this track
292 mov 0x2(%bp),%ah // Blocks to read
293 cmpb %ah,%al // To read
295 #ifdef TRACK_AT_A_TIME
296 movb %ah,%al // track
298 movb $1,%al // one sector
300 read.2: mov $0x5,%di // Try count
301 read.3: les 0x4(%bp),%bx // Transfer buffer
303 movb $0x2,%ah // BIOS: Read
304 int $0x13 // from disk
306 jnc read.4 // If success
309 xorb %ah,%ah // BIOS: Reset
310 int $0x13 // disk system
311 xchg %bx,%ax // Block count
312 jmp read.3 // Continue
313 read.4: movzbw %bl,%ax // Sectors read
314 add %ax,0x8(%bp) // Adjust
316 incw 0xa(%bp) // transfer
317 read.5: shlb %bl // buffer
318 add %bl,0x5(%bp) // pointer,
319 sub %al,0x2(%bp) // block count
320 ja read // If not done
321 read.6: retw // To caller
322 read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start // LBA support enabled?
323 jz ereturn // No, so return an error
324 mov $0x55aa,%bx // Magic
326 movb $0x41,%ah // BIOS: Check
327 int $0x13 // extensions present
329 jc return // If error, return an error
330 cmp $0xaa55,%bx // Magic?
331 jne ereturn // No, so return an error
332 testb $0x1,%cl // Packet interface?
333 jz ereturn // No, so return an error
334 mov %bp,%si // Disk packet
335 movb $0x42,%ah // BIOS: Extended
341 msg_read: .asciz "Read"
342 msg_part: .asciz "Boot"
344 prompt: .asciz " error\r\n"
346 flags: .byte FLAGS // Flags
353 part4: .byte 0x80, 0x00, 0x01, 0x00
354 .byte 0xa5, 0xfe, 0xff, 0xff
355 .byte 0x00, 0x00, 0x00, 0x00
356 .byte 0x50, 0xc3, 0x00, 0x00 // 50000 sectors long, bleh
358 .word 0xaa55 // Magic number