wok-current annotate memtest/stuff/bootloader.S @ rev 15205

hg merge
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Sep 09 03:32:26 2013 +0300 (2013-09-09)
parents
children d9dd83ccc85f
rev   line source
pascal@15188 1 SYSSEG = 0x1000
pascal@15188 2 INITSEG = 0x9000
pascal@15188 3 SETUPSEG = 0x9020
pascal@15188 4
pascal@15188 5 setup_sects = 497
pascal@15188 6 syssize = 500
pascal@15188 7
pascal@15188 8 .text
pascal@15188 9 .code16
pascal@15188 10 .org 0
pascal@15188 11 .globl _start
pascal@15188 12 _start:
pascal@15188 13
pascal@15188 14 #define CODESZ 512
pascal@15188 15
pascal@15188 16 /* some extra features */
pascal@15188 17 #define EXE_SUPPORT real mode dos .exe file support
pascal@15188 18 #define CMDLINE 0x9E00
pascal@15188 19 #define HELP store help message for /? argument
pascal@15188 20 #define CHECK_REALMODE does not support vm86
pascal@15188 21
pascal@15188 22 /* some contraints to reduce the size */
pascal@15188 23 //#define FLOPPY_1440K_ONLY 1.44M floppies support only
pascal@15188 24 #define NO_CURSOR_DEFINITION
pascal@15188 25
pascal@15188 26 #ifdef EXE_SUPPORT
pascal@15188 27 #define EXEADRS(x) x+0xE0
pascal@15188 28 stacktop = 0x9E00 # in 0x8000 .. 0xA000
pascal@15188 29 decw %bp // Magic number: MZ
pascal@15188 30 popw %dx
pascal@15188 31 jmp start // Bytes on last page of file
pascal@15188 32 .word (CODESZ+511)/512 // Pages in file
pascal@15188 33 .word 0 // Relocations
pascal@15188 34 .word (end_header-_start)/16 // Size of header in paragraphs
pascal@15188 35 .word 4096 // Minimum extra paragraphs needed
pascal@15188 36 .word -1 // Maximum extra paragraphs needed
pascal@15188 37 .word (CODESZ+15)/16 // Initial (relative) SS value
pascal@15188 38 .word stacktop // Initial SP value
pascal@15188 39 .word 0 // Checksum
pascal@15188 40 .word EXEADRS(comstart) // Initial IP value
pascal@15188 41 .word 0xFFF0 // Initial (relative) CS value
pascal@15188 42 // .word 0x001C // File address of relocation table
pascal@15188 43 // .word 0,0,0 // Overlay number
pascal@15188 44 .ascii "(SliTaz)"
pascal@15188 45 end_header:
pascal@15188 46 comstart:
pascal@15188 47 #ifdef CMDLINE
pascal@15188 48 .word 0xA33A // CMP AH,[BP+DI+stacktop]
pascal@15188 49 .word stacktop
pascal@15188 50 #else
pascal@15188 51 #undef HELP
pascal@15188 52 #endif
pascal@15188 53 cld # assume nothing
pascal@15188 54 pushw $INITSEG
pascal@15188 55 popw %es
pascal@15188 56 #ifdef CMDLINE
pascal@15188 57 movw %sp, %di
pascal@15188 58 movw $0x80, %si
pascal@15188 59 lodsb
pascal@15188 60 cbw
pascal@15188 61 xchgw %ax, %cx
pascal@15188 62 jcxz nocmdline
pascal@15188 63 movb $0x3F, 0x7F(%si)
pascal@15188 64 skipspace:
pascal@15188 65 lodsb
pascal@15188 66 cmpb $0x20, %al
pascal@15188 67 je skipspace
pascal@15188 68 decw %si
pascal@15188 69 rep
pascal@15188 70 movsb
pascal@15188 71 # ifdef HELP
pascal@15188 72 # define PUTS
pascal@15188 73 movw $EXEADRS(helpmsg), %si
pascal@15188 74 cmpb $'/', %al
pascal@15188 75 je puts
pascal@15188 76 # endif
pascal@15188 77 nocmdline:
pascal@15188 78 #endif
pascal@15188 79 #ifdef CHECK_REALMODE
pascal@15188 80 #define PUTS
pascal@15188 81 movw $EXEADRS(realmode_expected), %si
pascal@15188 82 pushfw // save flags
pascal@15188 83 // bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
pascal@15188 84 // flags 0 NT IOPL OF DF IF TF SF ZF 0 AF 0 PF 1 CF
pascal@15188 85 movb $0x10, %ah // DF = IF = TF = 0
pascal@15188 86 pushw %ax
pascal@15188 87 popfw // < 286 : flags[12..15] are forced 1
pascal@15188 88 pushfw // = 286 : flags[12..15] are forced 0
pascal@15188 89 popw %cx // > 286 : only flags[15] is forced 0
pascal@15188 90 popfw // restore flags
pascal@15188 91 addb %ah, %ch // test F0 and 00 cases
pascal@15188 92 cmpb %ah, %ch
pascal@15188 93 jbe puts // C=8086/80186, Z=80286
pascal@15188 94 smsww %ax
pascal@15188 95 andb $1, %al
pascal@15188 96 jne puts
pascal@15188 97 #endif
pascal@15188 98 movw $0x100, %si
pascal@15188 99 movw $end_header, %di
pascal@15188 100 movb EXEADRS(setup_sects), %ch
pascal@15188 101 movb $(512-(end_header-_start))/2, %cl
pascal@15188 102 rep
pascal@15188 103 movsw
pascal@15188 104 ljmp $INITSEG, $movesys
pascal@15188 105 start:
pascal@15188 106 pushw %dx
pascal@15188 107 xorw %dx, %dx
pascal@15188 108 #else
pascal@15188 109 #undef HELP
pascal@15188 110 #undef CMDLINE
pascal@15188 111 #undef CHECK_REALMODE
pascal@15188 112 #endif
pascal@15188 113 cld # assume nothing
pascal@15188 114 stacktop = 0x9E00 # in 0x8000 .. 0xA000
pascal@15188 115 zeroed = 12 # zeroed registers
pascal@15188 116 movw $stacktop-12-zeroed, %di # stacktop is an arbitrary value >=
pascal@15188 117 # length of bootsect + length of
pascal@15188 118 # setup + room for stack;
pascal@15188 119 # 12 is disk parm size.
pascal@15188 120 pushw $INITSEG
pascal@15188 121 popw %ss # %ss contain INITSEG
pascal@15188 122 movw %di, %sp # put stack at INITSEG:stacktop-...
pascal@15188 123
pascal@15188 124 # Many BIOS's default disk parameter tables will not recognize
pascal@15188 125 # multi-sector reads beyond the maximum sector number specified
pascal@15188 126 # in the default diskette parameter tables - this may mean 7
pascal@15188 127 # sectors in some cases.
pascal@15188 128 #
pascal@15188 129 # Since single sector reads are slow and out of the question,
pascal@15188 130 # we must take care of this by creating new parameter tables
pascal@15188 131 # (for the first disk) in RAM. We can set the maximum sector
pascal@15188 132 # count to 36 - the most we will encounter on an ED 2.88.
pascal@15188 133 #
pascal@15188 134 # High doesn't hurt. Low does. Let's use the max: 63
pascal@15188 135
pascal@15188 136 pushw %ss
pascal@15188 137 popw %es # %es = %ss = INITSEG
pascal@15188 138 xorw %ax, %ax # %ax = 0
pascal@15188 139 movw $zeroed/2, %cx # clear gdt + offset, %ds, limits
pascal@15188 140 rep # don't worry about cld
pascal@15188 141 stosw # already done above
pascal@15188 142 popw %bx # offset = 0
pascal@15188 143 popw %ds # %ds = 0
pascal@15188 144 popw %fs # %fs = 0
pascal@15188 145
pascal@15188 146 movb setup_sects+0x7C00, %al # read bootsector + setup (%ds = 0)
pascal@15188 147 incw %ax
pascal@15188 148
pascal@15188 149 ldsw 0x78(%bx), %si # %ds:%bx+0x78 is parameter table address
pascal@15188 150 pushw %es
pascal@15188 151 pushw %di
pascal@15188 152 movb $6, %cl # copy 12 bytes
pascal@15188 153 rep # don't worry about cld
pascal@15188 154 movsw # already done above
pascal@15188 155 pushw %ss
pascal@15188 156 popw %ds # now %ds = %es = %ss = INITSEG
pascal@15188 157 popl %fs:0x78(%bx) # update parameter table address
pascal@15188 158 movb $63, 0x4-12(%di) # patch sector count, %di = stacktop
pascal@15188 159 cli
pascal@15188 160
pascal@15188 161 xchg %ax, %di # sector count
pascal@15188 162 popw %ax # limits = 0
pascal@15188 163 incw %cx # cylinder 0, sector 1, clear Z
pascal@15188 164 call read_first_sectors # read setup
pascal@15188 165
pascal@15188 166 # This routine loads the system at address LOADSEG, making sure
pascal@15188 167 # no 64kB boundaries are crossed. We try to load it as fast as
pascal@15188 168 # possible, loading whole tracks whenever we can.
pascal@15188 169
pascal@15188 170 popw %bx # clear %bx
pascal@15188 171 movw syssize, %di
pascal@15188 172 addw $(512/16)-1, %di
pascal@15188 173 shrw $9-4, %di
pascal@15188 174 movw $SYSSEG, %cx
pascal@15188 175 call read_sectorsCX
pascal@15188 176
pascal@15188 177 # This procedure turns off the floppy drive motor, so
pascal@15188 178 # that we enter the kernel in a known state, and
pascal@15188 179 # don't have to worry about it later.
pascal@15188 180
pascal@15188 181 kill_motor:
pascal@15188 182 xchgw %ax, %di # reset FDC (%di < 128)
pascal@15188 183 int $0x13
pascal@15188 184
pascal@15188 185 # After that (everything loaded), we jump to the setup-routine
pascal@15188 186 # loaded directly after the bootblock:
pascal@15188 187 # Segments are as follows: %ds = %ss = INITSEG
pascal@15188 188
pascal@15188 189 jmp_setup:
pascal@15188 190 ljmp $SETUPSEG, $0
pascal@15188 191
pascal@15188 192 #ifdef PUTS
pascal@15188 193 #define PUTC
pascal@15188 194 puts:
pascal@15188 195 lodsb
pascal@15188 196 orb %al, %al
pascal@15188 197 je exit
pascal@15188 198 call putc
pascal@15188 199 jmp puts
pascal@15188 200 #endif
pascal@15188 201 #ifdef EXE_SUPPORT
pascal@15188 202 movesys:
pascal@15188 203 pushw %es
pascal@15188 204 popw %ss
pascal@15188 205 movw EXEADRS(syssize), %bp // %ds untouched
pascal@15188 206 movw $SYSSEG, %ax
pascal@15188 207 movw %ds, %bx
pascal@15188 208 cwd
pascal@15188 209 incw %dx
pascal@15188 210 cmpw %ax, %bx
pascal@15188 211 jnc forward
pascal@15188 212 negw %dx
pascal@15188 213 addw %bp, %ax
pascal@15188 214 addw %bp, %bx
pascal@15188 215 forward:
pascal@15188 216 movw %ax, %es
pascal@15188 217 movw %bx, %ds
pascal@15188 218 xorw %di, %di
pascal@15188 219 movb $8, %cl
pascal@15188 220 rep
pascal@15188 221 movsw
pascal@15188 222 subw $16, %si
pascal@15188 223 addw %dx, %ax
pascal@15188 224 addw %dx, %bx
pascal@15188 225 decw %bp
pascal@15188 226 jns forward
pascal@15188 227 #ifndef NO_CURSOR_DEFINITION
pascal@15188 228 movb $1, %ah
pascal@15188 229 movb $0, %bh
pascal@15188 230 movb $0x20, %ch // 0x2000
pascal@15188 231 int $0x10
pascal@15188 232 #endif
pascal@15188 233 pushw %ss
pascal@15188 234 popw %ds
pascal@15188 235 jmp jmp_setup
pascal@15188 236 #endif
pascal@15188 237 putcdot:
pascal@15188 238 #ifdef PUTC
pascal@15188 239 movb $0x2E, %al
pascal@15188 240 putc:
pascal@15188 241 movb $0xE, %ah
pascal@15188 242 movw $7, %bx
pascal@15188 243 int $0x10
pascal@15188 244 #endif
pascal@15188 245 exit:
pascal@15188 246 ret
pascal@15188 247
pascal@15188 248
pascal@15188 249 # read_sectors reads %di sectors into %es:0 buffer.
pascal@15188 250 # %es:0 is updated to the next memory location.
pascal@15188 251 # First, sectors are read sector by sector until
pascal@15188 252 # sector per track count is known. Then they are
pascal@15188 253 # read track by track.
pascal@15188 254 # Assume no error on first track.
pascal@15188 255
pascal@15188 256 #ifdef FLOPPY_1440K_ONLY
pascal@15188 257 #define FLOPPY_HEADS 2 /* 2 heads */
pascal@15188 258 #define FLOPPY_SECTORS 18 /* 18 sectors */
pascal@15188 259 #else
pascal@15188 260 #define FLOPPY_HEADS 2 /* 2 heads minimum */
pascal@15188 261 #define FLOPPY_SECTORS 9 /* 9 sectors minimum */
pascal@15188 262 #endif
pascal@15188 263
pascal@15188 264 check_limits:
pascal@15188 265 #ifndef FLOPPY_1440K_ONLY
pascal@15188 266 popw %dx
pascal@15188 267 #ifdef FLOPPY_SECTORS
pascal@15188 268 cmpb $FLOPPY_SECTORS+1, %cl # minimum sector count
pascal@15188 269 jb check_head
pascal@15188 270 #endif
pascal@15188 271 cmpb %al, %cl # max sector known ?
pascal@15188 272 ja next_head # no -> store it
pascal@15188 273 check_head:
pascal@15188 274 #ifdef FLOPPY_HEADS
pascal@15188 275 cmpb $FLOPPY_HEADS, %dh # 2 heads minimum
pascal@15188 276 jb check_cylinder
pascal@15188 277 #endif
pascal@15188 278 cmpb %ah, %dh # max head known ?
pascal@15188 279 ja next_cylinder # no -> store it
pascal@15188 280 check_cylinder:
pascal@15188 281 #endif
pascal@15188 282 pushaw
pascal@15188 283 #ifndef FLOPPY_1440K_ONLY
pascal@15188 284 cbw # %ah = 0
pascal@15188 285 #endif
pascal@15188 286 int $0x13 # reset controler
pascal@15188 287 popaw
pascal@15188 288 movb $1, %al # sector by sector...
pascal@15188 289 read_sectorslp:
pascal@15188 290 pushw %dx # some bios break dx...
pascal@15188 291 #ifndef FLOPPY_1440K_ONLY
pascal@15188 292 pushw %ax # limits
pascal@15188 293 subb %cl, %al # sectors remaining in track
pascal@15188 294 ja tolastsect
pascal@15188 295 movb $1, %al # 1 sector mini
pascal@15188 296 tolastsect:
pascal@15188 297 #else
pascal@15188 298 mov $FLOPPY_SECTORS+1, %al
pascal@15188 299 subb %cl, %al # sectors remaining in track
pascal@15188 300 #endif
pascal@15188 301 cbw
pascal@15188 302 cmpw %di, %ax
pascal@15188 303 jb more1trk
pascal@15188 304 movw %di, %ax # sectors to read
pascal@15188 305 more1trk:
pascal@15188 306 pushw %ax # save context
pascal@15188 307 movb $2, %ah # cmd: read chs
pascal@15188 308 int $0x13
pascal@15188 309 #ifndef FLOPPY_1440K_ONLY
pascal@15188 310 popw %dx # save %ax
pascal@15188 311 popw %ax # limits
pascal@15188 312 #else
pascal@15188 313 popw %ax # restore context
pascal@15188 314 popw %dx
pascal@15188 315 #endif
pascal@15188 316 jc check_limits
pascal@15188 317 #ifndef FLOPPY_1440K_ONLY
pascal@15188 318 xchgw %ax, %bp
pascal@15188 319 addw %dx,%cx # next sector
pascal@15188 320 movw %cx, %gs
pascal@15188 321 movw %es, %cx
pascal@15188 322 pushw %dx
pascal@15188 323 shlw $5, %dx
pascal@15188 324 addw %dx, %cx
pascal@15188 325 popw %dx
pascal@15188 326 subw %dx,%di # update sector counter
pascal@15188 327 popw %dx
pascal@15188 328 read_sectorsCX:
pascal@15188 329 movw %cx, %es # next location
pascal@15188 330 jz putcdot
pascal@15188 331 #else
pascal@15188 332 addw %ax,%cx # next sector
pascal@15188 333 movw %cx, %gs
pascal@15188 334 movw %es, %cx
pascal@15188 335 pushw %ax
pascal@15188 336 shlw $5, %ax
pascal@15188 337 addw %ax, %cx
pascal@15188 338 popw %ax
pascal@15188 339 subw %ax,%di # update sector counter
pascal@15188 340 read_sectorsCX:
pascal@15188 341 movw %cx, %es # next location
pascal@15188 342 jz putcdot
pascal@15188 343 #endif
pascal@15188 344 read_sectors:
pascal@15188 345 movw %gs, %cx
pascal@15188 346 #ifndef FLOPPY_1440K_ONLY
pascal@15188 347 # al is last sector+1
pascal@15188 348 # ah is last cylinder+1
pascal@15188 349 xchgw %ax, %bp
pascal@15188 350 #endif
pascal@15188 351 #ifndef FLOPPY_1440K_ONLY
pascal@15188 352 cmpb %al,%cl # reach sector limit ?
pascal@15188 353 jne bdendlp
pascal@15188 354 next_head:
pascal@15188 355 movb %cl,%al
pascal@15188 356 #else
pascal@15188 357 cmpb $FLOPPY_SECTORS+1,%cl # reach sector limit ?
pascal@15188 358 jne bdendlp
pascal@15188 359 #endif
pascal@15188 360 incb %dh # next head
pascal@15188 361 movb $1,%cl # first sector
pascal@15188 362 #ifndef FLOPPY_1440K_ONLY
pascal@15188 363 cmpb %ah, %dh # reach head limit ?
pascal@15188 364 jne bdendlp
pascal@15188 365 next_cylinder:
pascal@15188 366 movb %dh,%ah
pascal@15188 367 #else
pascal@15188 368 cmpb %cl,%dh # reach head limit ?
pascal@15188 369 je bdendlp
pascal@15188 370 #endif
pascal@15188 371 # NOTE : support 256 cylinders max
pascal@15188 372 incb %ch # next cylinder
pascal@15188 373 read_first_sectors:
pascal@15188 374 movb $0,%dh # first head
pascal@15188 375 bdendlp:
pascal@15188 376 jmp read_sectorslp
pascal@15188 377
pascal@15188 378 #ifdef CHECK_REALMODE
pascal@15188 379 realmode_expected:
pascal@15188 380 .ascii "386 real mode only."
pascal@15188 381 .byte 13,10,0
pascal@15188 382 #endif
pascal@15188 383 #ifdef HELP
pascal@15188 384 helpmsg:
pascal@15188 385 .ascii "No help available."
pascal@15188 386 .byte 13,10
pascal@15188 387 .byte 0
pascal@15188 388 #endif
pascal@15188 389 .org 497