netrom.asm (3864B)
1 2 3 ; 0x02000 [dword] Memory Size 4 ; 0x02004 [dword] ptable addr 5 ; 0x03000 page table 6 ; 7 ; 0x80000 reloc addr 8 ; 9 ; 0x90000 prot mode targ 10 ; 0x93FFF 11 12 memamt equ 0x2000 13 ptaddr equ 0x2004 14 entry equ 0x90000 15 romsize equ 0x4000 16 17 bits 16 18 19 rom_start: 20 dw 0xaa55 ; rom signature 21 db 0x20 ; rom size (512 byte units) 22 jmp short s1 ; entrypoint 23 db 0x42 ; checksum (dummy) 24 25 times 0x18 - ($ - rom_start) db 0 26 27 dw pci_header ; PCI Header Offset 28 dw 0x0000 ; PNP Gunk 29 30 pci_header: 31 db 'PCIR' 32 dw 0x10ec ; vendor 33 dw 0x8029 ; device 34 dw 0x0000 ; vital data 35 dw 0x0018 ; length 36 db 0 ; pci_header version (0) 37 db 0 ; protocol 38 db 0 ; subclass (ethernet) 39 db 2 ; class (network) 40 dw 0x0000 ; image length 41 dw 0x0000 ; revision 42 db 0 ; code type (x86) 43 db 0x80 ; indicates this is last image 44 dw 0x0000 ; reserved 45 46 s1: 47 push ds ; hook into the boot sequence 48 49 xor ax, ax 50 mov ds, ax 51 52 mov word [0x19 * 4], start 53 mov word [0x19 * 4 + 2], cs 54 55 pop ds 56 retf 57 58 start: 59 cli 60 cld 61 62 xor di,di ; relocate to 8000:0000 63 xor si,si 64 65 mov ax,0x8000 66 mov es,ax 67 68 mov cx,romsize/4 69 cs 70 rep 71 movsd 72 73 xor ax,ax 74 mov ss,ax 75 mov sp,0xfffc 76 77 mov ax,0x8000 78 mov ds,ax 79 80 jmp 0x8000:start_reloc 81 82 start_reloc: 83 84 ; mov ax, 0x4f02 ; VESA Gunk 85 ; mov bx, (0x8000 + 0x4000 + 0x105) 86 ; int 0x10 87 88 call enableA20 89 90 lgdt [unGDT] 91 92 mov eax,cr0 93 or al, 1 94 mov cr0, eax 95 96 ; jump dword 0x8:(0x80000 + setsegs) 97 db 0x66, 0xea 98 dw setsegs 99 dw 0x0008 100 dw 0x0008 101 102 setsegs: 103 mov bx,0x10 104 mov ds,bx 105 mov es,bx 106 mov ss,bx 107 108 and al,0xfe 109 mov cr0, eax 110 111 jmp 0x8000:newip 112 113 newip: 114 xor ax,ax 115 mov es,ax 116 mov ds,ax 117 mov ss,ax 118 119 mov edi,0x90000 ; relocate rom to just below 1MB 120 mov esi, eof + 0x80000 121 mov ecx, romsize/4 122 rep a32 movsd 123 124 call countmemory 125 126 mov dword [memamt], eax 127 mov dword [ptaddr], 0x3000 128 129 ; make some pages tables 130 mov ecx, eax 131 shr ecx, 12 132 mov edi, [ptaddr] 133 add ecx, 0x1000/4 134 135 xor eax, eax 136 rep a32 stosd 137 138 mov ebx, [ptaddr] 139 mov ecx, [memamt] 140 141 shr ecx, 12 142 mov eax, 0x0003 143 mov edi, 0x1000 144 145 l1: 146 mov [ebx+edi],eax 147 add eax, 0x1000 148 add edi, 4 149 loop l1 150 151 mov ecx, [memamt] 152 shr ecx, 22 153 inc ecx 154 155 mov eax, [ptaddr] 156 add eax, 0x1003 157 xor edi, edi 158 159 l2: 160 mov [ebx+edi], eax 161 mov [ebx+edi+0xf00], eax 162 add eax, 0x1000 163 add edi, 4 164 loop l2 165 166 167 mov eax, [ptaddr] 168 mov ecx, eax 169 or al, 3 170 171 mov [ebx+0xffc], eax 172 173 mov ebx, 0x90000 174 175 goprot: 176 push dword 0x0002 177 popfd 178 179 lgdt [cs:GDT] 180 181 mov cr3, ecx 182 mov eax, 0x80000001 183 mov cr0, eax 184 185 ; jmp far prot 186 db 0x66, 0xea 187 dw 0x0000 + prot, 0x0008, 0x0008 188 189 prot: 190 bits 32 191 192 mov eax, 0x10 193 mov ds, eax 194 mov es, eax 195 mov fs, eax 196 mov gs, eax 197 mov ss, eax 198 199 mov esp, ecx 200 sub esp, 4 201 202 push dword [memamt] ; memory amount 203 204 call ebx 205 jmp $ 206 207 align 8 208 209 unGDT: 210 dw 0xffff, unGDT + 0x0000, 0x0008, 0x0000 ; offset unGDT 211 db 0xff, 0xff, 0x00, 0x00, 0x00, 0x9a, 0x8f, 0x00 ; kCS (0x08) CPL0 212 db 0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0x8f, 0x00 ; kDS (0x10) 213 214 GDT: 215 dw 0xffff, GDT + 0x0000, 0x0008, 0x0000 ; offset GDT 216 db 0xff, 0xff, 0x00, 0x00, 0x00, 0x9a, 0xcf, 0x00 ; kCS (0x08) 217 db 0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0xcf, 0x00 ; kDS (0x10) 218 219 bits 16 220 221 enableA20: 222 call enableA20o1 223 jnz short enableA20done 224 mov al,0xd1 225 out 0x64,al 226 call enableA20o1 227 jnz short enableA20done 228 mov al,0xdf 229 out 0x60,al 230 enableA20o1: 231 mov ecx,0x20000 232 enableA20o1l: 233 jmp short enableA20next 234 enableA20next: 235 in al,0x64 236 test al,2 237 loopnz enableA20o1l 238 enableA20done: 239 ret 240 241 countmemory: 242 mov ax, '12' 243 mov ebx, 0x100ff0 244 245 b0: 246 mov dx, [ebx] ; save contents 247 mov [ebx], ax ; write signature 248 249 mov di, [ebx] ; read back 250 mov [ebx], dx ; restore contents 251 252 cmp di, ax ; did it stick? 253 jnz b1 ; nope, no more memory 254 255 add ebx, 0x1000 256 jmp b0 257 258 b1: 259 mov eax, ebx 260 sub eax, 0x1000 261 add eax, 0x10 262 263 ret 264 265 eof: