| 1 | /*- |
| 2 | * Copyright (c) 1990 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * William Jolitz. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 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 the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. All advertising materials mentioning features or use of this software |
| 17 | * must display the following acknowledgement: |
| 18 | * This product includes software developed by the University of |
| 19 | * California, Berkeley and its contributors. |
| 20 | * 4. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | * |
| 36 | * from: @(#)wdbootblk.c 7.1 (Berkeley) 4/28/91 |
| 37 | * $Id$ |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * wdbootblk.s: |
| 42 | * Written 7/6/90 by William F. Jolitz |
| 43 | * Initial block boot for AT/386 with typical Western Digital |
| 44 | * WD 1002-WA2 (or upwards compatable). Works either as |
| 45 | * first and sole partition bootstrap, or as loaded by a |
| 46 | * earlier BIOS boot when on an inner partition of the disk. |
| 47 | * |
| 48 | * Goal is to read in sucessive 7.5Kbytes of bootstrap to |
| 49 | * execute. |
| 50 | * |
| 51 | * No attempt is made to handle disk errors. |
| 52 | */ |
| 53 | #include "i386/isa/isa.h" |
| 54 | #include "i386/isa/wdreg.h" |
| 55 | #define NOP inb $0x84,%al |
| 56 | #define BIOSRELOC 0x7c00 |
| 57 | #define start RELOC+0x400 |
| 58 | |
| 59 | /* step 0 force descriptors to bottom of address space */ |
| 60 | |
| 61 | cli |
| 62 | .byte 0xb8,0x30,0x00 /* mov $0x30,%ax */ |
| 63 | mov %ax, %ss |
| 64 | .byte 0xbc,0x00,0x01 /* mov $0x100,%sp */ |
| 65 | |
| 66 | xorl %eax,%eax |
| 67 | movl %ax,%ds |
| 68 | movl %ax,%es |
| 69 | |
| 70 | /* obtain BIOS parameters for hard disk XXX */ |
| 71 | movb $0x9f,%ah /* write to 0x9ff00 XXX */ |
| 72 | movb $0xf0,%al |
| 73 | mov %ax,%es |
| 74 | xor %edi,%edi |
| 75 | |
| 76 | .byte 0xf, 0xb4, 0x36 ; .word 0x41*4 /* lfs 0x41*4, %si */ |
| 77 | xorb %ch,%ch |
| 78 | movb $0x10,%cl |
| 79 | fs |
| 80 | rep |
| 81 | movsb |
| 82 | |
| 83 | .byte 0xf, 0xb4, 0x36 ; .word 0x46*4 /* lfs 0x46*4, %si */ |
| 84 | xorb %ch,%ch |
| 85 | movb $0x10,%cl |
| 86 | fs |
| 87 | rep |
| 88 | movsb |
| 89 | |
| 90 | xorl %eax,%eax |
| 91 | movl %ax,%es |
| 92 | |
| 93 | /* step 1 load new descriptor table */ |
| 94 | |
| 95 | .byte 0x3E,0x0F,1,0x16 |
| 96 | .word BIOSRELOC+0x6e #GDTptr |
| 97 | # word aword cs lgdt GDTptr |
| 98 | |
| 99 | /* step 2 turn on protected mode */ |
| 100 | |
| 101 | smsw %ax |
| 102 | orb $1,%al |
| 103 | lmsw %ax |
| 104 | jmp 1f |
| 105 | nop |
| 106 | |
| 107 | /* step 3 reload segment descriptors */ |
| 108 | |
| 109 | 1: |
| 110 | xorl %eax,%eax |
| 111 | movb $0x10,%al |
| 112 | movl %ax,%ds |
| 113 | movl %ax,%es |
| 114 | movl %ax,%ss |
| 115 | word |
| 116 | ljmp $0x8,$ BIOSRELOC+0x74 /* would be nice if .-RELOC+0x7c00 worked */ |
| 117 | |
| 118 | /* Global Descriptor Table contains three descriptors: |
| 119 | * 0x00: Null: not used |
| 120 | * 0x08: Code: code segment starts at 0 and extents for 4 gigabytes |
| 121 | * 0x10: Data: data segment starts at 0 and extends for 4 gigabytes |
| 122 | * (overlays code) |
| 123 | */ |
| 124 | GDT: |
| 125 | NullDesc: .word 0,0,0,0 # null descriptor - not used |
| 126 | CodeDesc: .word 0xFFFF # limit at maximum: (bits 15:0) |
| 127 | .byte 0,0,0 # base at 0: (bits 23:0) |
| 128 | .byte 0x9f # present/priv level 0/code/conforming/readable |
| 129 | .byte 0xcf # page granular/default 32-bit/limit(bits 19:16) |
| 130 | .byte 0 # base at 0: (bits 31:24) |
| 131 | DataDesc: .word 0xFFFF # limit at maximum: (bits 15:0) |
| 132 | .byte 0,0,0 # base at 0: (bits 23:0) |
| 133 | .byte 0x93 # present/priv level 0/data/expand-up/writeable |
| 134 | .byte 0xcf # page granular/default 32-bit/limit(bits 19:16) |
| 135 | .byte 0 # base at 0: (bits 31:24) |
| 136 | |
| 137 | /* Global Descriptor Table pointer |
| 138 | * contains 6-byte pointer information for LGDT |
| 139 | */ |
| 140 | GDTptr: .word 0x17 # limit to three 8 byte selectors(null,code,data) |
| 141 | .long BIOSRELOC+0x56 # GDT -- arrgh, gas again! |
| 142 | |
| 143 | /* step 4 relocate to final bootstrap address. */ |
| 144 | reloc: |
| 145 | movl $ BIOSRELOC,%esi |
| 146 | movl $ RELOC,%edi |
| 147 | movl $512,%ecx |
| 148 | rep |
| 149 | movsb |
| 150 | movl $0xa0000, %esp |
| 151 | pushl $dodisk |
| 152 | ret |
| 153 | |
| 154 | /* step 5 load remaining 15 sectors off disk */ |
| 155 | dodisk: |
| 156 | movl $ IO_WD1+wd_seccnt,%edx |
| 157 | movb $ 15,%al |
| 158 | outb %al,%dx |
| 159 | NOP |
| 160 | movl $ IO_WD1+wd_sector,%edx |
| 161 | movb $ 2,%al |
| 162 | outb %al,%dx |
| 163 | NOP |
| 164 | #outb(wdc+wd_cyl_lo, (cyloffset & 0xff)); |
| 165 | #outb(wdc+wd_cyl_hi, (cyloffset >> 8)); |
| 166 | #outb(wdc+wd_sdh, WDSD_IBM | (unit << 4)); |
| 167 | |
| 168 | movl $ IO_WD1+wd_command,%edx |
| 169 | movb $ WDCC_READ,%al |
| 170 | outb %al,%dx |
| 171 | NOP |
| 172 | cld |
| 173 | |
| 174 | /* check to make sure controller is not busy and we have data ready */ |
| 175 | readblk: |
| 176 | movl $ IO_WD1+wd_status,%edx |
| 177 | NOP |
| 178 | inb %dx,%al |
| 179 | testb $ WDCS_BUSY,%al |
| 180 | jnz readblk |
| 181 | testb $ WDCS_DRQ,%al |
| 182 | jz readblk |
| 183 | |
| 184 | /* read a block into final position in memory */ |
| 185 | |
| 186 | movl $ IO_WD1+wd_data,%edx |
| 187 | movl $ 256,%ecx |
| 188 | .byte 0x66,0xf2,0x6d # rep insw |
| 189 | NOP |
| 190 | |
| 191 | /* need more blocks to be read in? */ |
| 192 | |
| 193 | cmpl $ RELOC+16*512-1,%edi |
| 194 | jl readblk |
| 195 | |
| 196 | /* for clever bootstrap, dig out boot unit and cylinder */ |
| 197 | |
| 198 | movl $ IO_WD1+wd_cyl_lo,%edx |
| 199 | inb %dx,%al |
| 200 | xorl %ecx,%ecx |
| 201 | movb %al,%cl |
| 202 | incl %edx |
| 203 | NOP |
| 204 | inb %dx,%al /* cyl_hi */ |
| 205 | movb %al,%ch |
| 206 | pushl %ecx /* cyloffset */ |
| 207 | |
| 208 | incl %edx |
| 209 | xorl %eax,%eax |
| 210 | NOP |
| 211 | inb %dx,%al /* sdh */ |
| 212 | andb $0x10,%al /* isolate unit # bit */ |
| 213 | shrb $4,%al |
| 214 | pushl %eax /* unit */ |
| 215 | |
| 216 | /* wd controller is major device 0 */ |
| 217 | xorl %eax,%eax |
| 218 | pushl %eax /* bootdev */ |
| 219 | |
| 220 | /* sorry, no flags at this point! */ |
| 221 | |
| 222 | movl $ start, %eax |
| 223 | call %eax /* main (dev, unit, offset) */ |
| 224 | |
| 225 | ebootblkcode: |
| 226 | |
| 227 | /* remaining space usable for a disk label */ |
| 228 | |
| 229 | .org 0x1fe |
| 230 | .word 0xaa55 /* signature -- used by BIOS ROM */ |
| 231 | |
| 232 | ebootblk: /* MUST BE EXACTLY 0x200 BIG FOR SURE */ |