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

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