]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/boot/i386/zfsboot/zfsldr.S
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / boot / i386 / zfsboot / zfsldr.S
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  * $FreeBSD$
16  */
17
18 /* Memory Locations */
19                 .set MEM_REL,0x700              # Relocation address
20                 .set MEM_ARG,0x900              # Arguments
21                 .set MEM_ORG,0x7c00             # Origin
22                 .set MEM_BUF,0x8000             # Load area
23                 .set MEM_BTX,0x9000             # BTX start
24                 .set MEM_JMP,0x9010             # BTX entry point
25                 .set MEM_USR,0xa000             # Client start
26                 .set BDA_BOOT,0x472             # Boot howto flag
27         
28 /* Partition Constants */
29                 .set PRT_OFF,0x1be              # Partition offset
30                 .set PRT_NUM,0x4                # Partitions
31                 .set PRT_BSD,0xa5               # Partition type
32
33 /* Flag Bits */
34                 .set FL_PACKET,0x80             # Packet mode
35
36 /* Misc. Constants */
37                 .set SIZ_PAG,0x1000             # Page size
38                 .set SIZ_SEC,0x200              # Sector size
39
40                 .set NSECT,0x40
41                 .globl start
42                 .globl xread
43                 .code16
44
45 start:          jmp main                        # Start recognizably
46
47 /*
48  * This is the start of a standard BIOS Parameter Block (BPB). Most bootable
49  * FAT disks have this at the start of their MBR. While normal BIOS's will
50  * work fine without this section, IBM's El Torito emulation "fixes" up the
51  * BPB by writing into the memory copy of the MBR. Rather than have data
52  * written into our xread routine, we'll define a BPB to work around it.
53  * The data marked with (T) indicates a field required for a ThinkPad to
54  * recognize the disk and (W) indicates fields written from IBM BIOS code.
55  * The use of the BPB is based on what OpenBSD and NetBSD implemented in
56  * their boot code but the required fields were determined by trial and error.
57  *
58  * Note: If additional space is needed in boot1, one solution would be to
59  * move the "prompt" message data (below) to replace the OEM ID.
60  */
61                 .org 0x03, 0x00
62 oemid:          .space 0x08, 0x00       # OEM ID
63
64                 .org 0x0b, 0x00
65 bpb:            .word   512             # sector size (T)
66                 .byte   0               # sectors/clustor
67                 .word   0               # reserved sectors
68                 .byte   0               # number of FATs
69                 .word   0               # root entries
70                 .word   0               # small sectors
71                 .byte   0               # media type (W)
72                 .word   0               # sectors/fat
73                 .word   18              # sectors per track (T)
74                 .word   2               # number of heads (T)
75                 .long   0               # hidden sectors (W)
76                 .long   0               # large sectors
77
78                 .org 0x24, 0x00
79 ebpb:           .byte   0               # BIOS physical drive number (W)
80
81                 .org 0x25,0x90
82 /*
83  * Trampoline used by boot2 to call read to read data from the disk via
84  * the BIOS.  Call with:
85  *
86  * %cx:%ax      - long    - LBA to read in
87  * %es:(%bx)    - caddr_t - buffer to read data into
88  * %dl          - byte    - drive to read from
89  * %dh          - byte    - num sectors to read
90  */
91
92 xread:          push %ss                        # Address
93                 pop %ds                         #  data
94 /*
95  * Setup an EDD disk packet and pass it to read
96  */
97 xread.1:                                        # Starting
98                 pushl $0x0                      #  absolute
99                 push %cx                        #  block
100                 push %ax                        #  number
101                 push %es                        # Address of
102                 push %bx                        #  transfer buffer
103                 xor %ax,%ax                     # Number of
104                 movb %dh,%al                    #  blocks to
105                 push %ax                        #  transfer
106                 push $0x10                      # Size of packet
107                 mov %sp,%bp                     # Packet pointer
108                 callw read                      # Read from disk
109                 lea 0x10(%bp),%sp               # Clear stack
110                 lret                            # To far caller
111 /*
112  * Load the rest of boot2 and BTX up, copy the parts to the right locations,
113  * and start it all up.
114  */
115
116 /*
117  * Setup the segment registers to flat addressing (segment 0) and setup the
118  * stack to end just below the start of our code.
119  */
120 main:           cld                             # String ops inc
121                 xor %cx,%cx                     # Zero
122                 mov %cx,%es                     # Address
123                 mov %cx,%ds                     #  data
124                 mov %cx,%ss                     # Set up
125                 mov $start,%sp                  #  stack
126 /*
127  * Relocate ourself to MEM_REL.  Since %cx == 0, the inc %ch sets
128  * %cx == 0x100.
129  */
130                 mov %sp,%si                     # Source
131                 mov $MEM_REL,%di                # Destination
132                 incb %ch                        # Word count
133                 rep                             # Copy
134                 movsw                           #  code
135 /*
136  * If we are on a hard drive, then load the MBR and look for the first
137  * FreeBSD slice.  We use the fake partition entry below that points to
138  * the MBR when we call nread.  The first pass looks for the first active
139  * FreeBSD slice.  The second pass looks for the first non-active FreeBSD
140  * slice if the first one fails.
141  */
142                 mov $part4,%si                  # Partition
143                 cmpb $0x80,%dl                  # Hard drive?
144                 jb main.4                       # No
145                 movb $0x1,%dh                   # Block count
146                 callw nread                     # Read MBR
147                 mov $0x1,%cx                    # Two passes
148 main.1:         mov $MEM_BUF+PRT_OFF,%si        # Partition table
149                 movb $0x1,%dh                   # Partition
150 main.2:         cmpb $PRT_BSD,0x4(%si)          # Our partition type?
151                 jne main.3                      # No
152                 jcxz main.5                     # If second pass
153                 testb $0x80,(%si)               # Active?
154                 jnz main.5                      # Yes
155 main.3:         add $0x10,%si                   # Next entry
156                 incb %dh                        # Partition
157                 cmpb $0x1+PRT_NUM,%dh           # In table?
158                 jb main.2                       # Yes
159                 dec %cx                         # Do two
160                 jcxz main.1                     #  passes
161 /*
162  * If we get here, we didn't find any FreeBSD slices at all, so print an
163  * error message and die.
164  */
165                 mov $msg_part,%si               # Message
166                 jmp error                       # Error
167 /*
168  * Floppies use partition 0 of drive 0.
169  */
170 main.4:         xor %dx,%dx                     # Partition:drive
171
172 /*
173  * Ok, we have a slice and drive in %dx now, so use that to locate and
174  * load boot2.  %si references the start of the slice we are looking
175  * for, so go ahead and load up the 64 sectors starting at sector 1024
176  * (i.e. after the two vdev labels).  We don't have do anything fancy
177  * here to allow for an extra copy of boot1 and a partition table
178  * (compare to this section of the UFS bootstrap) so we just load it
179  * all at 0x8000. The first part of boot2 is BTX, which wants to run
180  * at 0x9000. The boot2.bin binary starts right after the end of BTX,
181  * so we have to figure out where the start of it is and then move the
182  * binary to 0xc000. After we have moved the client, we relocate BTX
183  * itself to 0x9000 - doing it in this order means that none of the
184  * memcpy regions overlap which would corrupt the copy.  Normally, BTX
185  * clients start at MEM_USR, or 0xa000, but when we use btxld to
186  * create boot2, we use an entry point of 0x2000.  That entry point is
187  * relative to MEM_USR; thus boot2.bin starts at 0xc000.
188  *
189  * The load area and the target area for the client overlap so we have
190  * to use a decrementing string move. We also play segment register
191  * games with the destination address for the move so that the client
192  * can be larger than 16k (which would overflow the zero segment since
193  * the client starts at 0xc000). Relocating BTX is easy since the load
194  * area and target area do not overlap.
195  */
196 main.5:         mov %dx,MEM_ARG                 # Save args
197                 movb $NSECT,%dh                 # Sector count
198                 movw $1024,%ax                  # Offset to boot2
199                 callw nread.1                   # Read disk
200 main.6:         mov $MEM_BUF,%si                # BTX (before reloc)
201                 mov 0xa(%si),%bx                # Get BTX length and set
202                 mov $NSECT*SIZ_SEC-1,%di        # Size of load area (less one)
203                 mov %di,%si                     # End of load
204                 add $MEM_BUF,%si                #  area
205                 sub %bx,%di                     # End of client, 0xc000 rel
206                 mov %di,%cx                     # Size of
207                 inc %cx                         #  client
208                 mov $(MEM_USR+2*SIZ_PAG)>>4,%dx # Segment
209                 mov %dx,%es                     #   addressing 0xc000
210                 std                             # Move with decrement
211                 rep                             # Relocate
212                 movsb                           #  client
213                 mov %ds,%dx                     # Back to
214                 mov %dx,%es                     #  zero segment
215                 mov $MEM_BUF,%si                # BTX (before reloc)
216                 mov $MEM_BTX,%di                # BTX
217                 mov %bx,%cx                     # Get BTX length
218                 cld                             # Increment this time
219                 rep                             # Relocate
220                 movsb                           #  BTX
221
222 /*
223  * Enable A20 so we can access memory above 1 meg.
224  * Use the zero-valued %cx as a timeout for embedded hardware which do not
225  * have a keyboard controller.
226  */
227 seta20:         cli                             # Disable interrupts
228 seta20.1:       dec %cx                         # Timeout?
229                 jz seta20.3                     # Yes
230                 inb $0x64,%al                   # Get status
231                 testb $0x2,%al                  # Busy?
232                 jnz seta20.1                    # Yes
233                 movb $0xd1,%al                  # Command: Write
234                 outb %al,$0x64                  #  output port
235 seta20.2:       inb $0x64,%al                   # Get status
236                 testb $0x2,%al                  # Busy?
237                 jnz seta20.2                    # Yes
238                 movb $0xdf,%al                  # Enable
239                 outb %al,$0x60                  #  A20
240 seta20.3:       sti                             # Enable interrupts
241
242                 jmp start+MEM_JMP-MEM_ORG       # Start BTX
243
244
245 /*
246  * Trampoline used to call read from within boot1.
247  */
248 nread:          xor %ax,%ax                     # Sector offset in partition
249 nread.1:        mov $MEM_BUF,%bx                # Transfer buffer
250                 add 0x8(%si),%ax                # Get
251                 mov 0xa(%si),%cx                #  LBA
252                 push %cs                        # Read from
253                 callw xread.1                   #  disk
254                 jnc return                      # If success, return
255                 mov $msg_read,%si               # Otherwise, set the error
256                                                 #  message and fall through to
257                                                 #  the error routine
258 /*
259  * Print out the error message pointed to by %ds:(%si) followed
260  * by a prompt, wait for a keypress, and then reboot the machine.
261  */
262 error:          callw putstr                    # Display message
263                 mov $prompt,%si                 # Display
264                 callw putstr                    #  prompt
265                 xorb %ah,%ah                    # BIOS: Get
266                 int $0x16                       #  keypress
267                 movw $0x1234, BDA_BOOT          # Do a warm boot
268                 ljmp $0xffff,$0x0               # reboot the machine
269 /*
270  * Display a null-terminated string using the BIOS output.
271  */
272 putstr.0:       mov $0x7,%bx                    # Page:attribute
273                 movb $0xe,%ah                   # BIOS: Display
274                 int $0x10                       #  character
275 putstr:         lodsb                           # Get char
276                 testb %al,%al                   # End of string?
277                 jne putstr.0                    # No
278
279 /*
280  * Overused return code.  ereturn is used to return an error from the
281  * read function.  Since we assume putstr succeeds, we (ab)use the
282  * same code when we return from putstr.
283  */
284 ereturn:        movb $0x1,%ah                   # Invalid
285                 stc                             #  argument
286 return:         retw                            # To caller
287 /*
288  * Reads sectors from the disk.  If EDD is enabled, then check if it is
289  * installed and use it if it is.  If it is not installed or not enabled, then
290  * fall back to using CHS.  Since we use a LBA, if we are using CHS, we have to
291  * fetch the drive parameters from the BIOS and divide it out ourselves.
292  * Call with:
293  *
294  * %dl  - byte     - drive number
295  * stack - 10 bytes - EDD Packet
296  */
297 read:           testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
298                 jz read.1                       # No, use CHS
299                 cmpb $0x80,%dl                  # Hard drive?
300                 jb read.1                       # No, use CHS
301                 mov $0x55aa,%bx                 # Magic
302                 push %dx                        # Save
303                 movb $0x41,%ah                  # BIOS: Check
304                 int $0x13                       #  extensions present
305                 pop %dx                         # Restore
306                 jc read.1                       # If error, use CHS
307                 cmp $0xaa55,%bx                 # Magic?
308                 jne read.1                      # No, so use CHS
309                 testb $0x1,%cl                  # Packet interface?
310                 jz read.1                       # No, so use CHS
311                 mov %bp,%si                     # Disk packet
312                 movb $0x42,%ah                  # BIOS: Extended
313                 int $0x13                       #  read
314                 retw                            # To caller
315 #if 0   
316 read.1:         push %dx                        # Save
317                 movb $0x8,%ah                   # BIOS: Get drive
318                 int $0x13                       #  parameters
319                 movb %dh,%ch                    # Max head number
320                 pop %dx                         # Restore
321                 jc return                       # If error
322                 andb $0x3f,%cl                  # Sectors per track
323                 jz ereturn                      # If zero
324                 cli                             # Disable interrupts
325                 mov 0x8(%bp),%eax               # Get LBA
326                 push %dx                        # Save
327                 movzbl %cl,%ebx                 # Divide by
328                 xor %edx,%edx                   #  sectors
329                 div %ebx                        #  per track
330                 movb %ch,%bl                    # Max head number
331                 movb %dl,%ch                    # Sector number
332                 inc %bx                         # Divide by
333                 xorb %dl,%dl                    #  number
334                 div %ebx                        #  of heads
335                 movb %dl,%bh                    # Head number
336                 pop %dx                         # Restore
337                 cmpl $0x3ff,%eax                # Cylinder number supportable?
338                 sti                             # Enable interrupts
339                 ja ereturn                      # No, return an error
340                 xchgb %al,%ah                   # Set up cylinder
341                 rorb $0x2,%al                   #  number
342                 orb %ch,%al                     # Merge
343                 inc %ax                         #  sector
344                 xchg %ax,%cx                    #  number
345                 movb %bh,%dh                    # Head number
346                 subb %ah,%al                    # Sectors this track
347                 mov 0x2(%bp),%ah                # Blocks to read
348                 cmpb %ah,%al                    # To read
349                 jb read.2                       #  this
350 #ifdef  TRACK_AT_A_TIME
351                 movb %ah,%al                    #  track
352 #else
353                 movb $1,%al                     #  one sector
354 #endif
355 read.2:         mov $0x5,%di                    # Try count
356 read.3:         les 0x4(%bp),%bx                # Transfer buffer
357                 push %ax                        # Save
358                 movb $0x2,%ah                   # BIOS: Read
359                 int $0x13                       #  from disk
360                 pop %bx                         # Restore
361                 jnc read.4                      # If success
362                 dec %di                         # Retry?
363                 jz read.6                       # No
364                 xorb %ah,%ah                    # BIOS: Reset
365                 int $0x13                       #  disk system
366                 xchg %bx,%ax                    # Block count
367                 jmp read.3                      # Continue
368 read.4:         movzbw %bl,%ax                  # Sectors read
369                 add %ax,0x8(%bp)                # Adjust
370                 jnc read.5                      #  LBA,
371                 incw 0xa(%bp)                   #  transfer
372 read.5:         shlb %bl                        #  buffer
373                 add %bl,0x5(%bp)                #  pointer,
374                 sub %al,0x2(%bp)                #  block count
375                 ja read.1                       # If not done
376 read.6:         retw                            # To caller
377 #else
378 read.1:         mov $msg_chs,%si
379                 jmp error
380 msg_chs:        .asciz "CHS not supported"
381 #endif
382
383 /* Messages */
384
385 msg_read:       .asciz "Read"
386 msg_part:       .asciz "Boot"
387
388 prompt:         .asciz " error\r\n"
389
390 flags:          .byte FLAGS                     # Flags
391
392                 .org PRT_OFF,0x90
393
394 /* Partition table */
395
396                 .fill 0x30,0x1,0x0
397 part4:          .byte 0x80, 0x00, 0x01, 0x00
398                 .byte 0xa5, 0xfe, 0xff, 0xff
399                 .byte 0x00, 0x00, 0x00, 0x00
400                 .byte 0x50, 0xc3, 0x00, 0x00    # 50000 sectors long, bleh
401
402                 .word 0xaa55                    # Magic number