4 * Automatically retry all BIOS reads 0x80<->0x81 (and if (drive&0x80) drive^=1; in genral)
7 * http://www.jankratochvil.net/project/int13sniff/
11 /* !!! All defines below must be COMMENTED-OUT !
12 * !!! Define to value 0 has no effect.
15 /* Reduce size by omitting DOS-boot header signature to fit in 512 bytes.
16 * Cylinder 0 is requierd, sectors per track == 63 assumed!
17 * This constant is counted from 0 !!!
19 #define BACKUP_SECTOR 63
21 /* Reduce size by omitting DOS-boot header signature to fit in 512 bytes.
23 #define STRIP_BOOT_HEADER 1
25 /* Reduce size by omitting INT 0x18 handler.
29 /* Maintainers: Disable .org macroinstructions: ONLY FOR compilation tuning!
30 * - image produced doen't have any functionality!
34 /* Internal defines */
35 #define MEM_KBYTES 0x413 /* BIOS variable */
37 #define MAX_BOOT_SIZE_100 0xb6 /* LILO: leave some space for NT's and DR DOS' dirty */
38 #define MAX_BOOT_SIZE (MAX_BOOT_SIZE_100+0x100)
44 movw $0x0E00 | \char,%ax
52 #define MYORG(offset) .org (offset) + START /* "+ START" MUST be on the end of line - WHY?!? */
75 #define PUSHALL_SIZE (7*2)
76 #define PUSHALL_SI ((1/*%bp*/+1/*%di*/)*2)
78 .macro CallJumpVector0x13
79 pushf /* create 'lret' stack by this pushf... */
80 pushw %cs /* ...this segment... */
81 call JumpVector0x13 /* ...and this return address */
86 /* Code starts here */
87 /********************/
97 cli /* just a paranoia, shouldn't be needed */
98 jmp InitRawStack /* We don't want to have 0x90 on offset 2 (one of recognization rules by DOS) */
101 .macro places offset length string
102 .org (\offset) + START
104 .org (\offset) + (\length) + START /* we can't check whether it isn't too short :-( */
106 .macro placex offset length bytes
107 .org (\offset) + START
109 .org (\offset) + (\length) + START /* we can't check whether it isn't too short :-( */
112 /* We rather supply empty (=zeroed) FAT filesystem table
114 #ifndef STRIP_BOOT_HEADER
115 places 0x03,8,"BIOSRAID" /* OEM ID */
116 placex 0x43,3,"0x5E,0x81,0xA2" /* Volume Serial Number: 5E81A2?? as "SERIAL??" */
117 placex 0x43+3,1,BACKUP_SECTOR /* Volume Serial Number: 5E81A23F as "SERIAL??" */
118 places 0x47,11,"BIOSauRAID\0" /* Volume Label */
119 places 0x52,8,"BootOnly" /* Filesystem ID */
121 places 0x03,8,"BIOSRAI2" /* OEM ID */
122 placex 0x03+8,1,BACKUP_SECTOR /* Sanity checked by: biosautoraid.pl */
123 placex 0x03+9,1,MAX_BOOT_SIZE_100 /* Sanity checked by: biosautoraid.pl */
127 #ifndef STRIP_BOOT_HEADER
138 ljmp $0x7C0/*segment*/ , $InitContinue7C0-START/*offset*/
145 /* move to top memory */
146 xorw %dx,%dx /* DX=null */
148 movw %ds:MEM_KBYTES,%ax
149 decw %ax /* Allocation size: 1KB */
150 movw %ax,%ds:MEM_KBYTES
154 xorw %di,%di /* buffer=ES:DI */
160 movw $InitContinueTop-START,%ax
165 movw $TrailNL_Msg-START,%si
166 jmp PrintString_noPref
168 /* String is given in SI, returns updated SI */
180 movw $PrintString_loop-START,%bx /* from now do just 'ret' to close the loop */
184 /* Character is given in AL
185 * Preserves: DS, ES, SI, DI
186 * Destroys: AX (even AL!), BX, CX, DX
187 * DS is sometimes left preserved, sometimes set to DS=CS
190 movb $0x0E,%ah /* print character */
207 /* Main initialization */
208 /***********************/
209 /* DS invalid, ES==CS */
218 movw $1*1024,%sp /* Allocated 1 KB */
221 movw $HelloMsg-START,%si
224 /* now install our SniffFunction0x13 sniffer */
231 movw $OrigVector0x13-START,%di
233 movw $SniffFunction0x13-START,%ax
238 popw %di /* = $0x13*4 */
240 popw %es /* = null */
241 stosw /* $SniffFunction0x13-START */
246 movw $0x18*4,%di /* ROM basic - failed boot */
247 movw $SniffFunction0x18-START,%ax
252 #endif /* !STRIP_INT18 */
254 /* we WANT DS left with 0x0000 */
255 /* we WANT ES left with 0x0000 */
257 /* and give the system control to disk=0x80/masterboot */
258 movw $0x0201,%ax /* READ16 1 sector */
259 movw $0x7C00,%bx /* buffer=ES:BX= 0x0000:0x7C00 */
261 pushw %bx /* BX/DS on stack for later 'lret', S=2, prepared for far ret */
262 /* Interface to retry-capable INT13, set everything except CX */
263 /* function may destroy DS value! */
265 /* These two numbers get remapped to: BACKUP_SECTOR */
266 movw $0x0080,%dx /* Head 0, Drive 0x80 */
267 movw $0x0001,%cx /* Cylinder 0, Sector 1 */
270 pushw %dx /* save drive for resetting the controller */
271 movw $SF13_02_Err_Msg-START,%si
273 movb $0x00,%ah /* reset controller - FDC or HDC */
274 popw %dx /* restore the drive */
279 cmpw $0xAA55,%es:(0x7C00+0x1FE) /* 0x55,0xAA */
280 movw $BadSignatureMsg-START,%si
282 cli /* IMPORTANT: Boot sectors must be run with CLI! */
283 lret /* lret to 0x0000:0x7C00 */
287 movw $Interrupt0x18Msg-START,%si /* %ds gets fixed in PrintFatal */
288 #endif /* !STRIP_INT18 */
296 /**************************************************************/
297 /* Section for INT13 sniffing */
298 /* THIS point may start to be after 0x200 boundary */
299 /**************************************************************/
302 cmpb $0x41,%ah /* presence32 */
303 jne JumpVector0x13popa
305 jne JumpVector0x13popa
313 incw %sp /* trash discarded */
315 incw %sp /* original %ax discarded */
318 .byte 0xEA /* ljmp */
324 pushw %dx /* original %dx */
325 pushw %ax /* original %ax */
326 pushw %ax /* trash */
342 cmpb $0x02,%ah /* read16 */
360 movb /*orig-%dl:*/ %ss:2/*%bp*/+PUSHALL_SIZE+2/*%ds*/+2/*trash*/+2/*orig-%ax*/(%bp),%dl
361 xorb %cs:DriveFlipFlag-START,%dl /* %ds not yet initialized by: PrintString */
362 movb %dl,/*%dl:*/ %ss:2/*%bp*/+2/*%bp*/+2/*%di*/+2/*%si*/(%bp)
366 movw $SF13_02_Err_Msg-START,%si
371 jz SF13_leave /* just a floppy? */
373 jnz SF13_leave /* 0x82+ disk? It may be correct unsuccessful read. */
374 xorb $0x01,%ds:DriveFlipFlag-START /* %ds already initialized by: PrintString */
378 * as we must hide ourselves to let read GRUB itself frmo the masterboot.
382 jz SF13_leave /* just a floppy? */
384 jnz SF13_leave /* 0x82+ disk? It may be correct unsuccessful read. */
385 cmpw $0x0001,%cx /* Cylinder 0, Sector 1 ? */
387 cmpb $0x00,%dh /* Head 0 ? */
389 movb $BACKUP_SECTOR/63,%dh
390 movb $1+(BACKUP_SECTOR%63),%cl
393 xorb %cs:DriveFlipFlag-START,%dl /* %ds not yet initialized by: PrintString */
398 xorb $0x01,%cs:DriveFlipFlag-START /* %ds not yet initialized by: PrintString */
404 andb $0xFE,%ss:2/*%bp*/+PUSHALL_SIZE+2/*%ds*/+2/*trash*/+2/*orig-%ax*/+2/*orig-%dx*/+4/*ret-seg:offs*/(%bp) /* flags; clear CF */
411 movb /*%al:*/ %ss:2/*%bp*/+2/*%bp*/+2/*%di*/+2/*%si*/+2/*%dx*/+2/*%cx*/+2/*%bx*/(%bp),%al /* count */
412 movw /*%cx:*/ %ss:2/*%bp*/+2/*%bp*/+2/*%di*/+2/*%si*/+2/*%dx*/(%bp),%cx /* cylinder, sector */
413 cmpb $BACKUP_SECTOR/63,/*%dh:*/ %ss:2/*%bp*/+2/*%bp*/+2/*%di*/+2/*%si*/+1/*%dl*/(%bp) /* head */
416 SF13_leave_zero_loop:
419 cmpw $1+(BACKUP_SECTOR%63),%cx
420 je SF13_leave_zero_found
423 decb %al /* --count */
424 jmp SF13_leave_zero_loop
426 SF13_leave_zero_found:
437 incw %sp /* trash discarded */
439 incw %sp /* original %ax discarded */
444 popw %ax /* return address */
447 movw %ax,%ss:2/*%bp*/+PUSHALL_SIZE+2/*%ds*/(%bp) /* store %ax to 'trash' */
448 movw %ss:2/*%bp*/+PUSHALL_SIZE+2/*%ds*/+2/*trash*/(%bp),%ax /* restore original %ax */
449 movw %ax,%ss:2/*%bp*/+2/*%bp*/+2/*%di*/+2/*%si*/+2/*%dx*/+2/*%cx*/+2/*%bx*/(%bp) /* rewrite from original %ax */
455 pushALL /* stack is back to normal NOW */
456 pushw %bp /* trash - just to prepare return address16 */
460 movw %ss:2/*%bp*/+2/*%ax*/+2/*return16*/+PUSHALL_SIZE+2/*%ds*/(%bp),%ax
461 movw %ax,%ss:2/*%bp*/+2/*ax*/(%bp)
464 movw %ax,%ss:2/*%bp*/+2/*%ax*/+2/*return16*/+PUSHALL_SIZE+2/*%ds*/+2/*trash*/+2/*orig-%ax*/+2/*orig-%dx*/+4/*ret-seg:offs*/(%bp)
467 ret /* to prepared return16 and now will be stack in normal again */
469 /* Main upper half initialization messages */
470 /*******************************************/
474 .ascii "BIOSautoRAID, $Id$"
483 .asciz "Disk 0x80 has invalid signature!"
489 /* Interrupt sniffing messages */
490 /*******************************/
495 .asciz "Interrupt 0x18 - Failed boot!"
499 #endif /* !STRIP_INT18 */
502 /**************************************************************/
503 /* Final sector signature */
504 /**************************************************************/
507 /* Partition table */