Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / boot / pc32 / boot2 / boot1.S
CommitLineData
409cbc03 1/*
cacaceec
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
409cbc03
MD
34 * Copyright (c) 1998 Robert Nordier
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms are freely
38 * permitted provided that the above copyright notice and this
39 * paragraph and the following disclaimer are duplicated in all
40 * such forms.
41 *
42 * This software is provided "AS IS" and without any express or
43 * implied warranties, including, without limitation, the implied
44 * warranties of merchantability and fitness for a particular
45 * purpose.
46 *
47 * $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.23 2003/08/22 01:59:28 imp Exp $
ed987dc9 48 * $DragonFly: src/sys/boot/pc32/boot2/boot1.S,v 1.9 2004/09/30 18:31:58 dillon Exp $
409cbc03 49 */
984263bc 50
409cbc03 51#include "../bootasm.h"
984263bc 52
5ee58eed
MD
53// Partition Constants
54 .set PRT_OFF,0x1be // Partition offset
55 .set PRT_NUM,0x4 // Partitions
56 .set PRT_BSD,0xa5 // Partition type
984263bc 57
5ee58eed
MD
58// Flag Bits
59 .set FL_PACKET,0x80 // Packet mode
984263bc 60
5ee58eed
MD
61// Misc. Constants
62 .set SIZ_PAG,0x1000 // Page size
63 .set SIZ_SEC,0x200 // Sector size
5ee58eed 64 .set NSECT,0x10
409cbc03 65
984263bc
MD
66 .globl start
67 .globl xread
68 .code16
69
5ee58eed 70start: jmp main // Start recognizably
984263bc 71
5ee58eed
MD
72// This is the start of a standard BIOS Parameter Block (BPB). Most bootable
73// FAT disks have this at the start of their MBR. While normal BIOS's will
74// work fine without this section, IBM's El Torito emulation "fixes" up the
75// BPB by writing into the memory copy of the MBR. Rather than have data
76// written into our xread routine, we'll define a BPB to work around it.
77// The data marked with (T) indicates a field required for a ThinkPad to
78// recognize the disk and (W) indicates fields written from IBM BIOS code.
79// The use of the BPB is based on what OpenBSD and NetBSD implemented in
80// their boot code but the required fields were determined by trial and error.
81//
82// Note: If additional space is needed in boot1, one solution would be to
83// move the "prompt" message data (below) to replace the OEM ID.
984263bc
MD
84
85 .org 0x03, 0x00
5ee58eed 86oemid: .space 0x08, 0x00 // OEM ID
984263bc
MD
87
88 .org 0x0b, 0x00
5ee58eed
MD
89bpb: .word 512 // sector size (T)
90 .byte 0 // sectors/clustor
91 .word 0 // reserved sectors
92 .byte 0 // number of FATs
93 .word 0 // root entries
94 .word 0 // small sectors
95 .byte 0 // media type (W)
96 .word 0 // sectors/fat
97 .word 18 // sectors per track (T)
98 .word 2 // number of heads (T)
99 .long 0 // hidden sectors (W)
100 .long 0 // large sectors
984263bc
MD
101
102 .org 0x24, 0x00
5ee58eed 103ebpb: .byte 0 // BIOS physical drive number (W)
984263bc
MD
104
105 .org 0x25,0x90
5ee58eed
MD
106//
107// Trampoline used by boot2 to call read to read data from the disk via
108// the BIOS. Call with:
109//
110// %cx:%ax - long - LBA to read in
111// %es:(%bx) - caddr_t - buffer to read data into
112// %dl - byte - drive to read from
113// %dh - byte - num sectors to read
114//
984263bc 115
5ee58eed
MD
116xread: push %ss // Address
117 pop %ds // data
118//
119// Setup an EDD disk packet and pass it to read
120//
121xread.1: // Starting
122 pushl $0x0 // absolute
123 push %cx // block
124 push %ax // number
125 push %es // Address of
126 push %bx // transfer buffer
127 xor %ax,%ax // Number of
128 movb %dh,%al // blocks to
129 push %ax // transfer
130 push $0x10 // Size of packet
131 mov %sp,%bp // Packet pointer
132 callw read // Read from disk
133 lea 0x10(%bp),%sp // Clear stack
134 lret // To far caller
135//
136// Load the rest of boot2 and BTX up, copy the parts to the right locations,
137// and start it all up.
138//
984263bc 139
5ee58eed
MD
140//
141// Setup the segment registers to flat addressing (segment 0) and setup the
142// stack to end just below the start of our code.
143//
144main: cld // String ops inc
145 xor %cx,%cx // Zero
146 mov %cx,%es // Address
147 mov %cx,%ds // data
148 mov %cx,%ss // Set up
149 mov $start,%sp // stack
150//
cacaceec
MD
151// Relocate ourself to BOOT1_ORIGIN. Since %cx == 0, the inc %ch sets
152// %cx == 0x100 (256 words == 512 bytes).
5ee58eed
MD
153//
154 mov %sp,%si // Source
cacaceec 155 mov $BOOT1_ORIGIN,%di // Destination
5ee58eed
MD
156 incb %ch // Word count
157 rep // Copy
158 movsw // code
159//
160// If we are on a hard drive, then load the MBR and look for the first
161// FreeBSD slice. We use the fake partition entry below that points to
162// the MBR when we call nread. The first pass looks for the first active
163// FreeBSD slice. The second pass looks for the first non-active FreeBSD
164// slice if the first one fails.
165//
166 mov $part4,%si // Partition
167 cmpb $0x80,%dl // Hard drive?
168 jb main.4 // No
169 movb $0x1,%dh // Block count
170 callw nread // Read MBR
171 mov $0x1,%cx // Two passes
04144d62 172main.1: mov $BOOT2_LOAD_BUF+PRT_OFF,%si // Partition table
5ee58eed
MD
173 movb $0x1,%dh // Partition
174main.2: cmpb $PRT_BSD,0x4(%si) // Our partition type?
175 jne main.3 // No
176 jcxz main.5 // If second pass
177 testb $0x80,(%si) // Active?
178 jnz main.5 // Yes
179main.3: add $0x10,%si // Next entry
180 incb %dh // Partition
181 cmpb $0x1+PRT_NUM,%dh // In table?
182 jb main.2 // Yes
183 dec %cx // Do two
184 jcxz main.1 // passes
185//
186// If we get here, we didn't find any FreeBSD slices at all, so print an
187// error message and die.
188//
189 mov $msg_part,%si // Message
190 jmp error // Error
191//
192// Floppies use partition 0 of drive 0.
193//
194main.4: xor %dx,%dx // Partition:drive
195//
196// Ok, we have a slice and drive in %dx now, so use that to locate and load
197// boot2. %si references the start of the slice we are looking for, so go
04144d62
MD
198// ahead and load up the first 16 sectors (boot1 + boot2) from that.
199//
200// When we read it in, we conveniently use BOOT2_LOAD_BUF (0x8c00) as our
201// transfer buffer. Thus, boot1 ends up at 0x8c00, and boot2 starts at
202// 0x8c00 + 0x200 = 0x8e00.
203//
2d7f6790 204// The first part of boot2 is the disklabel, which is 0x200 bytes long.
5ee58eed
MD
205// The second part is BTX, which is thus loaded into 0x9000, which is where
206// it also runs from. The boot2.bin binary starts right after the end of
207// BTX, so we have to figure out where the start of it is and then move the
04144d62
MD
208// binary to 0xc000. Normally, BTX clients start at MEM_BTX_USR, or 0xa000,
209// but when we use btxld to create boot2, we use an entry point of 0x2000.
210// That entry point is relative to MEM_BTX_USR; thus boot2.bin starts
211// at 0xc000.
5ee58eed 212//
cacaceec
MD
213// MEM_BTX_USR_ARG will be overwritten by the disk read and the relocation
214// loop, so we must store the argument after completing said loops.
215//
216main.5: pushw %dx // Save args
5ee58eed
MD
217 movb $NSECT,%dh // Sector count
218 callw nread // Read disk
4e06dda7
MD
219 mov $MEM_BTX_ORG,%bx // Base of BTX header
220 mov 0xa(%bx),%si // Get BTX text length (btx.S)
221 add %bx,%si // %si = start of boot2.bin
222 // %di = relocation target
223 mov $MEM_BTX_USR+BOOT2_VORIGIN,%di
224 mov $MEM_BTX_ORG+(NSECT-1)*SIZ_SEC,%cx
225 sub %si,%cx // %cx = Size of boot2 client
226 rep // Relocate boot2
227 movsb
cacaceec 228 popw MEM_BTX_USR_ARG // save (disk,slice) for boot2
4e06dda7
MD
229
230#if 0
231 // XXX DISABLED. This makes incorrect assumptions about
232 // where BSS begins, potentially leaving garbage in the BSS
233 // space. The BSS zeroing code has been moved to
234 // btx/lib/btxcsu.S (BTX client startup code) where we have
235 // more definitive knowledge about where BSS resides.
236 //
237 // %cx now contains 0. Calculate 0x[1]0000 - %di to get a
238 // count of assumed BSS bytes from the end of boot2.bin up
239 // to 0x10000, then zero it out.
240 //
241 sub %di,%cx
242 xorb %al,%al
243 rep
244 stosb
245#endif
5ee58eed 246 callw seta20 // Enable A20
cacaceec
MD
247
248 // YYY
249 pushw $MEM_BTX_ENTRY // Start BTX
250 retw
5ee58eed
MD
251//
252// Enable A20 so we can access memory above 1 meg.
253//
254seta20: cli // Disable interrupts
255seta20.1: inb $0x64,%al // Get status
256 testb $0x2,%al // Busy?
257 jnz seta20.1 // Yes
258 movb $0xd1,%al // Command: Write
259 outb %al,$0x64 // output port
260seta20.2: inb $0x64,%al // Get status
261 testb $0x2,%al // Busy?
262 jnz seta20.2 // Yes
263 movb $0xdf,%al // Enable
264 outb %al,$0x60 // A20
265 sti // Enable interrupts
266 retw // To caller
267//
268// Trampoline used to call read from within boot1.
269//
04144d62 270nread: mov $BOOT2_LOAD_BUF,%bx // Transfer buffer
5ee58eed
MD
271 mov 0x8(%si),%ax // Get
272 mov 0xa(%si),%cx // LBA
273 push %cs // Read from
274 callw xread.1 // disk
275 jnc return // If success, return
276 mov $msg_read,%si // Otherwise, set the error
277 // message and fall through to
278 // the error routine
279//
280// Print out the error message pointed to by %ds:(%si) followed
281// by a prompt, wait for a keypress, and then reboot the machine.
282//
283error: callw putstr // Display message
284 mov $prompt,%si // Display
285 callw putstr // prompt
286 xorb %ah,%ah // BIOS: Get
287 int $0x16 // keypress
288 movw $0x1234, BDA_BOOT // Do a warm boot
289 ljmp $0xffff,$0x0 // reboot the machine
290//
291// Display a null-terminated string using the BIOS output.
292//
293putstr.0: mov $0x7,%bx // Page:attribute
294 movb $0xe,%ah // BIOS: Display
295 int $0x10 // character
296putstr: lodsb // Get char
297 testb %al,%al // End of string?
298 jne putstr.0 // No
984263bc 299
5ee58eed
MD
300//
301// Overused return code. ereturn is used to return an error from the
302// read function. Since we assume putstr succeeds, we (ab)use the
303// same code when we return from putstr.
304//
305ereturn: movb $0x1,%ah // Invalid
306 stc // argument
307return: retw // To caller
308//
309// Reads sectors from the disk. If EDD is enabled, then check if it is
310// installed and use it if it is. If it is not installed or not enabled, then
311// fall back to using CHS. Since we use a LBA, if we are using CHS, we have to
312// fetch the drive parameters from the BIOS and divide it out ourselves.
313// Call with:
314//
315// %dl - byte - drive number
316// stack - 10 bytes - EDD Packet
ed987dc9
MD
317
318read:
319 /*
320 * Try EDD mode first. If not enabled or no BIOS support
321 * exists, fall back to CHS mode.
322 */
323 testb $FL_PACKET,%cs:BOOT1_ORIGIN+flags-start
324 jz read.1
325
326 /*
327 * BIOS: check extensions present
328 */
329 mov $0x55aa,%bx
330 push %dx
331 movb $0x41,%ah
332 int $0x13
333 pop %dx
334 jc read.1 /* BIOS error return */
335 cmp $0xaa55,%bx /* check for proper magic */
336 jne read.1
337 testb $0x1,%cl /* packet interface support? */
338 jz read.1
339
340 /*
341 * Issue packet command.
342 * BIOS: Extended read command
343 */
344 mov %bp,%si
345 movb $0x42,%ah
346 int $0x13
347 retw
348
349 /*
350 * Fallback to CHS mode
351 */
352read.1:
353 push %dx // Save
5ee58eed
MD
354 movb $0x8,%ah // BIOS: Get drive
355 int $0x13 // parameters
356 movb %dh,%ch // Max head number
357 pop %dx // Restore
358 jc return // If error
359 andb $0x3f,%cl // Sectors per track
360 jz ereturn // If zero
361 cli // Disable interrupts
362 mov 0x8(%bp),%eax // Get LBA
363 push %dx // Save
364 movzbl %cl,%ebx // Divide by
365 xor %edx,%edx // sectors
366 div %ebx // per track
367 movb %ch,%bl // Max head number
368 movb %dl,%ch // Sector number
369 inc %bx // Divide by
370 xorb %dl,%dl // number
371 div %ebx // of heads
372 movb %dl,%bh // Head number
373 pop %dx // Restore
374 cmpl $0x3ff,%eax // Cylinder number supportable?
375 sti // Enable interrupts
ed987dc9 376 ja ereturn // No, failed
5ee58eed
MD
377 xchgb %al,%ah // Set up cylinder
378 rorb $0x2,%al // number
379 orb %ch,%al // Merge
380 inc %ax // sector
381 xchg %ax,%cx // number
382 movb %bh,%dh // Head number
383 subb %ah,%al // Sectors this track
384 mov 0x2(%bp),%ah // Blocks to read
385 cmpb %ah,%al // To read
386 jb read.2 // this
387#ifdef TRACK_AT_A_TIME
388 movb %ah,%al // track
389#else
390 movb $1,%al // one sector
391#endif
392read.2: mov $0x5,%di // Try count
393read.3: les 0x4(%bp),%bx // Transfer buffer
394 push %ax // Save
395 movb $0x2,%ah // BIOS: Read
396 int $0x13 // from disk
397 pop %bx // Restore
398 jnc read.4 // If success
399 dec %di // Retry?
400 jz read.6 // No
401 xorb %ah,%ah // BIOS: Reset
402 int $0x13 // disk system
403 xchg %bx,%ax // Block count
404 jmp read.3 // Continue
405read.4: movzbw %bl,%ax // Sectors read
406 add %ax,0x8(%bp) // Adjust
407 jnc read.5 // LBA,
408 incw 0xa(%bp) // transfer
409read.5: shlb %bl // buffer
410 add %bl,0x5(%bp) // pointer,
411 sub %al,0x2(%bp) // block count
ed987dc9 412 ja read.1 // If not done
5ee58eed 413read.6: retw // To caller
984263bc 414
5ee58eed 415// Messages
984263bc
MD
416
417msg_read: .asciz "Read"
418msg_part: .asciz "Boot"
419
420prompt: .asciz " error\r\n"
421
5ee58eed 422flags: .byte FLAGS // Flags
984263bc
MD
423
424 .org PRT_OFF,0x90
425
5ee58eed 426// Partition table
984263bc
MD
427
428 .fill 0x30,0x1,0x0
429part4: .byte 0x80, 0x00, 0x01, 0x00
5ee58eed 430 .byte 0xa5, 0xfe, 0xff, 0xff
984263bc 431 .byte 0x00, 0x00, 0x00, 0x00
5ee58eed 432 .byte 0x50, 0xc3, 0x00, 0x00 // 50000 sectors long, bleh
984263bc 433
5ee58eed 434 .word 0xaa55 // Magic number