home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / ranish / SOURCES.ZIP / IPL.ASM < prev    next >
Assembly Source File  |  1998-07-15  |  5KB  |  300 lines

  1. .MODEL LARGE
  2. .DATA
  3. .CODE
  4. LOCALS
  5. PUBLIC    _IPL
  6. _IPL    PROC    NEAR
  7.     ;
  8.     ;  BIOS loads MBR at 0000:7C00h
  9.     ;
  10.     ;  Lets move code to 0000:0600h
  11.     ;
  12.     xor    bp, bp
  13.     mov    ds, bp
  14.     mov    es, bp
  15.     mov    ss, bp        ;  CPU clears interrupt flag for next command
  16.     mov    sp, 7C00h
  17.     cld
  18.     mov    si, sp
  19.     mov    di, 0600h
  20.     mov    cx, 0100h
  21.     rep
  22.      movsw
  23. ;    jmp    @@ENTRY
  24.         DB    0E9h
  25.         DW    0600h-7C00h+@@ENTRY-($+2)
  26.     ;
  27. VIRUS    EQU    (M1-_IPL-100h)
  28. ERROR    EQU    (M2-_IPL-100h)
  29. BOOTING    EQU    (M3-_IPL-100h)
  30. HD_NUM    EQU    (M4-_IPL-100h)
  31. WRITING    EQU    (M5-_IPL-100h)
  32. NL    EQU    (M6-_IPL-100h)
  33.     ;
  34. @@ENTRY:
  35.     ;
  36.     ;  Check interrupt vectors
  37.     ;
  38.     cmp    Byte Ptr [di-800h+7B8h], 0
  39.     je    @@vir3
  40.     
  41.     mov    bx, 4*1Ch+3        ;  Check interrupts 0h to 1Ch
  42. @@vir1:
  43.     sub    bx, 4
  44.     jc    @@vir3
  45.     cmp    byte ptr [bx], 0C0h    ;  They must be >= C000:0000h
  46.     jae    @@vir1
  47.  
  48.     mov    bl, VIRUS    ; Otherwize print message
  49.     call    PRINT
  50. @@vir2:
  51.     mov    ah, 0
  52.     int    16h        ;  Get a key
  53.     cmp    al, 0Dh
  54.     jne    @@vir2        ;  And loop until ENTER is pressed
  55.     mov    bl, NL
  56.     call    PRINT
  57. @@vir3:
  58.     ;
  59.     ;
  60.     ;
  61.     cmp    dl, 80h
  62.     jae    @@hd1
  63.     mov    dl, 80h
  64. @@hd1:
  65.     mov    al, dl
  66.     sub    al, 80h-'1'
  67.     mov    [di-800h+700h+HD_NUM], al
  68.  
  69.     mov    bl, BOOTING
  70.     call    PRINT
  71.     
  72.     mov    al, [di-800h+7B9h]    ; Default partition name
  73.     cmp    al, 0
  74.     jne    @@m4
  75.  
  76.     ;
  77.     ;  Find active partition
  78.     ;
  79. @@pt1:
  80.     mov    al, '1'
  81.     mov    bh, 00h
  82.     mov    di, 07BEh
  83. @@pt2:
  84.     cmp    [di], bh ; 00
  85.     mov    [di], dl
  86.     jnz    @@pt3            ; Active partition found
  87.     mov    [di], bh ; 00
  88.     add    di, 10h
  89.     inc    ax
  90.     cmp    al, '4'
  91.     jbe    @@pt2
  92.     ;
  93.     mov    di, 07BAh        ; No active partition found
  94.     cmp    [di], bh ; 00
  95.     mov    al, 26        ; Booting from the next hard drive
  96.     jne    @@pt3
  97.     mov    al, 'A'        ; Booting from the first floppy disk (A)
  98.     xor    bp, bp        ; Don't save changes in this case
  99. @@pt3:
  100.     mov    ah, 0Eh
  101.     int    10h        ; Print a character
  102.     ;
  103.     ;  Load boot sector from disk
  104.     ;
  105.     mov    si, 3        ; We will try at most three times
  106. @@rd1:
  107.     push    dx
  108.     mov    ax, 0201h    ; Read (AH=02) 1 Sector (AL=01)
  109.     mov    bx, 7C00h    ; Destination Address
  110.     mov    cx, [di+02]    ; Cyl#, Sect#
  111.     mov    dx, [di+00]    ; Head, Drive in DL
  112.     int    13h
  113.     pop    dx
  114.     jnc    @@rd2
  115.                 ; We get here if there was an error
  116.     mov    ah, 0        ; We will try to reset device
  117.     int    13h
  118.  
  119.     dec    si
  120.     jnz    @@rd1
  121.     stc            ; We have tried three times, so we will give up
  122. @@rd2:
  123.     mov    bl, ERROR
  124.     jc    @@m2        ; I/O Error!
  125.     call    DOTS
  126.     jnc    @@save        ; Time out or user hit ESC
  127.     mov    bl, 0
  128.     cmp    al, ' '
  129.     jne    @@m4
  130.     ;
  131.     ; Select new partition
  132.     ;
  133. @@m1:
  134.     mov    bl, BOOTING
  135. @@m2:
  136.     call    PRINT
  137. @@m3:
  138.     mov    ah, 00h
  139.     int    16h        ; Read character
  140. @@m4:
  141.     mov    si, 7BAh
  142.     mov    ah, 0
  143.  
  144.     cmp    al, 'A'        ; Floppy disk
  145.     je    @@m6
  146.     cmp    al, 'a'
  147.     je    @@m6
  148.  
  149.     mov    ah, dl
  150.     inc    ah
  151.     
  152.     cmp    al, 09        ; Tab - Next hard drive
  153.     je    @@m6
  154.  
  155.     sub    al, '1'        ; One of the partitions
  156.     cmp    al, 04h
  157.     jb    @@m5
  158.     or    bl, bl
  159.     jz    @@save        ; wrong key, but not for us
  160.     jmp    @@m3        ; wrong key, so lets ask again
  161. @@m5:
  162.     mov    si, 7BEh
  163.     mov    ah, 10h
  164.     mul    ah
  165.     add    si, ax
  166.     mov    ah, dl
  167.  
  168.     cmp    Byte Ptr [si], ah
  169.     jne    @@m6
  170.     dec    bp
  171. @@m6:
  172.     or    bl, bl
  173.     jz    @@m1
  174.  
  175.     mov    di, 07BEh
  176.     mov    cx, 4
  177. @@cl1:
  178.     mov    byte ptr [di], 0
  179.     add    di, 10h
  180.     loop    @@cl1
  181.     
  182.     mov    [si], ah
  183.     inc    bp
  184.     jmp    @@pt1
  185.     ;
  186.     ; Save changes to disk
  187.     ;
  188. @@save:
  189.     or    bp, bp            ; Do we have to save (0=don't)
  190.     je    @@boot
  191.     mov    bl, WRITING
  192.     call    PRINT
  193.     mov    ax, 0301h        ; Write (AH=03) 1 Sector (AL=01)
  194.     mov    bx, 0600h        ; Source located at address 600h
  195.     mov    cx, 0001h        ; Cyl = 0, Sect = 1
  196.     mov    dh, 00h            ; Head = 0, Drive in DL
  197.     int    13h
  198. @@boot:
  199.     mov    dl, [di]        ; Boot sector expects Drive# in DL
  200. ;    jmp    7C00h            ; Transfer control to loaded BootSector
  201.         DB    0E9h
  202.         DW    7C00h-600h-($+2-_IPL)
  203. _IPL    ENDP
  204.     ;
  205.     ;  PRINT
  206.     ;
  207. PRINT    PROC    NEAR
  208.     mov    ah, 0Eh
  209.     mov    bh, 00h
  210.     lea    si, [bx+700h]
  211. @@pr1:
  212.     lodsb
  213.     or    al, al
  214.     jz    @@pr2
  215.     int    10h
  216.     jmp    @@pr1
  217. @@pr2:
  218.     ret
  219. PRINT    ENDP
  220.     ;
  221.     ;  Print dots and check if key is pressed (key in al)
  222.     ;
  223. DOTS    PROC    NEAR
  224.     push    dx
  225.     mov    ax, 092Eh    ; Print '.'
  226.     mov    bx, 0007h
  227.     mov    cx, [bx-7+7B6h]    ; Here is how many dots will be printed
  228.     int    10h
  229. W1:
  230.     mov    ah, 01h        ; Check if key is pressed
  231.     int    16h
  232.     jz    W3        ; There is no key waiting
  233.  
  234.     cmp    al, 1Bh        ; ESC
  235.     je    W2
  236.     stc
  237.     jmp    short W5
  238. W2:
  239.     mov    ah, 00        ; Remove ESC from keyboard buffer
  240.     int    16h
  241.     jmp    short W4
  242. W3:
  243.     mov    ax, 0E20h
  244.     int    10h        ; Print a SPACE
  245.  
  246.     push    bx
  247.     push    cx
  248.     mov    ah, 0
  249.     int    1Ah        ; Read System Timer
  250.     mov    bx, dx
  251. WT1:    int    1Ah        ; Wait one timer tick
  252.     cmp    bx, dx
  253.     je     WT1
  254.     mov    bx, dx
  255. WT2:    int    1Ah        ; Wait another timer tick
  256.     cmp    bx, dx
  257.     je     WT2
  258.     pop    cx
  259.     pop    bx
  260.  
  261.     loop    W1
  262. W4:
  263.     clc
  264. W5:
  265.     push    ax
  266.     pushf
  267.     mov    ax, 0920h    ; Print rest of SPACEs
  268.     int    10h
  269.     mov    bl, NL
  270.     call    PRINT
  271.     popf
  272.     pop    ax
  273.  
  274.     pop    dx
  275.     ret
  276. DOTS    ENDP
  277.     ;
  278. M1:    DB "Virus!!! ",0
  279. M2:    DB " Error!",0Dh,0Ah
  280. M3:    DB "Booting from: HD"
  281. M4:    DB "1/",0
  282. M5:    DB "Writing changes..."
  283. M6:    DB 0Dh,0Ah,0
  284.     ;
  285.  
  286. GAP    PROC
  287. GAPLEN    EQU    (1B6h-(GAP-_IPL))
  288. IF GAPLEN
  289.     DB    GAPLEN DUP(0)    
  290. ENDIF
  291. GAP    ENDP
  292.  
  293. DOT_LEN    DW    30h
  294. VIR_CHK    DB    01
  295. ST_PART    DB    00
  296. FD_PARM    DB    00,00,01,00    ; Floppy disk boot sector parameters
  297.     DB    40h DUP(0)
  298.     DB    055h, 0AAh
  299. END
  300.