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