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