home *** CD-ROM | disk | FTP | other *** search
/ PC Open 19 / pcopen19.iso / Zipped / PART231.ZIP / SOURCES.ZIP / IPL.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-01-12  |  5.0 KB  |  287 lines

  1. .MODEL    SMALL
  2. .DATA
  3.     ASSUME    CS:DGROUP, DS:DGROUP, SS:DGROUP
  4.     PUBLIC    _IPL, _IPL_SELECTOR, _ENTRY_S, _ENTRY_V
  5. _IPL    PROC
  6.     ;
  7.     ;  BIOS loads MBR at 0000:7C00h
  8.     ;
  9.     ;  Lets move code to 0000:0600h
  10.     ;
  11.     xor    bp, bp
  12.     mov    ds, bp
  13.     mov    es, bp
  14.     mov    ss, bp        ;  CPU clears interrupt flag for next command
  15.     mov    sp, 7C00h
  16.     cld
  17.     mov    si, sp
  18.     mov    di, 0600h
  19.     mov    cx, 0100h
  20.     rep
  21.      movsw
  22. ;    jmp    0000:ENTRY_POINT
  23.         DB 0EAh
  24. _IPL_SELECTOR    DW 0000, 0000    ;  Will be initialized during installation
  25.     ;
  26. VIRUS    EQU    (M1-_IPL)
  27. BOOTNG    EQU    (M2-_IPL)
  28. IO_ERR    EQU    (M3-_IPL)
  29. WRITNG    EQU    (M4-_IPL)
  30. NL    EQU    (M5-_IPL)
  31.     ;
  32. M1:    DB "Virus in memory! ",0
  33. M2:    DB "Booting from: ",0
  34. M3:    DB ". I/O Error!",0Dh,0Ah,0
  35. M4:    DB "Writing changes..."
  36. M5:    DB 0Dh,0Ah,0
  37.     ;
  38.     ;  Check interrupt vectors
  39.     ;
  40. _ENTRY_V:            ;  Entry point for virus detection option
  41. V0:
  42.     mov    cl, 1Bh        ;  Check 26 interrupt vectors from 03h to 1Ch
  43.     mov    di, 0Bh
  44. V1:
  45.     add    di, 4
  46.     dec    cx
  47.     jz    V3
  48.     cmp    byte ptr [di], 0C0h    ;  They must be >= C000:0000
  49.     jnb    V1
  50.  
  51.     mov    bl, VIRUS    ;  If any of interrupt vectors was changed
  52.     call    PRINT
  53. V2:
  54.     mov    ah, 0
  55.     int    16h        ;  Get a key
  56.     cmp    al, 0Dh
  57.     jne    V2        ;  Loop until ENTER is pressed
  58.     mov    bl, NL
  59.     call    PRINT
  60. V3:
  61.     ;
  62.     ;  Check if HD number passed in DL is OK. 
  63.     ;  Allowed values 80h (HD0) and 81h (HD1).
  64.     ;
  65. _ENTRY_S:                ;  Entry point for select only option
  66. H0:
  67.     cmp    dl, 81h            ;  Are we on the second Hard Drive?
  68.     je    H1
  69.     mov    dl, 80h
  70. H1:
  71.     mov    bl, BOOTNG
  72.     call    PRINT
  73.     ;
  74.     ;  Find active partition
  75.     ;
  76. L0:
  77.     mov    al, '1'
  78.     mov    di, 07BEh        ; Address of the first record in PT
  79.     push    dx
  80. L1:
  81.     cmp    Byte Ptr [di], 00
  82.     jnz    L2            ; Active partition found
  83.     add    di, 10h
  84.     inc    al
  85.     cmp    al, '4'
  86.     jbe    L1
  87.     ;
  88.     mov    di, 07BAh        ; No active partition found
  89.     mov    dl, [di]
  90.     mov    al, 'D'        ; Booting from the next hard drive (D)
  91.     cmp    dl, 0
  92.     jne    L2
  93.     mov    al, 'A'        ; Booting from the first floppy disk (A)
  94.     xor    bp, bp        ; Don't save changes in this case
  95. L2:
  96.     mov    ah, 0Eh
  97.     mov    bh, 00h
  98.     int    10h        ; Print a character
  99.     ;
  100.     ;  Load boot sector from disk
  101.     ;
  102.     mov    si, 3        ; We will try at most three times
  103. L21:
  104.     mov    ax, 0201h    ; Read (AH=02) 1 Sector (AL=01)
  105.     mov    bx, 7C00h    ; Destination Address
  106.     mov    cx, [di+02]    ; Cyl#, Sect#
  107.     mov    dx, [di+00]    ; Head, Drive in DL
  108.     int    13h
  109.     jnc    L23
  110.                 ; We get here if there was an error
  111.     mov    ah, 0        ; We will try to reset device
  112.     int    13h
  113.  
  114.     dec    si
  115.     jnz    L21
  116.     stc            ; We have tried three time and will give up
  117. L23:
  118.     pop    dx
  119.     jc    L4        ; I/O Error!
  120.     call    DOTS
  121.     jnc    L25        ; Time out or user hit ESC
  122.     cmp    al, ' '
  123.     je    L5
  124.     cmp    al, 'A'
  125.     je    L5
  126.     cmp    al, 'a'
  127.     je    L5
  128.     cmp    al, 'D'
  129.     je    L5
  130.     cmp    al, 'd'
  131.     je    L5
  132.     sub    al, '1'
  133.     cmp    al, 04h
  134.     jbe    L5
  135.     ;
  136.     ; Save changes to disk
  137.     ;
  138. L25:
  139.     push    di
  140.     or    bp, bp            ; Do we have to save (0=don't)
  141.     je    L3
  142.     mov    bl, WRITNG
  143.     call    PRINT
  144.     mov    ax, 0301h        ; Write (AH=03) 1 Sector (AL=01)
  145.     mov    bx, 0600h        ; Source located at address 600h
  146.     mov    cx, 0001h        ; Cyl = 0, Sect = 1
  147.     mov    dh, 00h            ; Head = 0, Drive in DL
  148.     int    13h
  149. L3:
  150.     pop    si            ; Boot sector expects Drive# in DL
  151.     mov    dl, [si]        ; and pointer to its own params in SI
  152. ;    jmp    0000:7C00h        ; Transfer control to loaded BootSector
  153.         DB 0EAh
  154.         DW 7C00h, 0000h
  155.     ;
  156.     ; Print Error
  157.     ;
  158. L4:
  159.     mov    bl, IO_ERR
  160.     call    PRINT
  161.     ;
  162.     ; Select new partition
  163.     ;
  164. L5:
  165.     mov    bl, BOOTNG
  166.     call    PRINT
  167. L6:
  168.     mov    ah, 00h
  169.     int    16h        ; Read character
  170.     
  171.     mov    ah, 00h
  172.     mov    [di], ah    ; disabling old partition
  173.     mov    di, 07BAh
  174.     cmp    al, 'A'
  175.     je    L7
  176.     cmp    al, 'a'
  177.     je    L7
  178.     mov    ah, dl        ; Next hard drive
  179.     inc    ah
  180.     cmp    al, 'D'
  181.     je    L7
  182.     cmp    al, 'd'
  183.     je    L7
  184.     sub    al, '1'
  185.     cmp    al, 04h
  186.     jnb    L6        ; if not a valid choice ask again
  187.  
  188.     mov    di, 07BEh    ; Address of first record in PT
  189.     mov    ah, 16
  190.     mul    ah
  191.     add    di, ax
  192.     mov    ah, dl
  193. L7:
  194.     mov    [di], ah
  195.     inc    bp        ; indication that we need to save changes
  196.     jmp    L0
  197. _IPL    ENDP
  198.     ;
  199.     ;  PRINT
  200.     ;
  201. PRINT    PROC
  202.     push    ax
  203.     mov    ah, 0Eh
  204.     mov    bh, 00h
  205.     lea    si, [bx+0600h]
  206. P1:
  207.     lodsb
  208.     or    al, al
  209.     jz    P2
  210.     int    10h
  211.     jmp    P1
  212. P2:
  213.     pop    ax
  214.     ret
  215. PRINT    ENDP
  216.     ;
  217.     ;  Print dots and check if key is pressed (key in al)
  218.     ;
  219. DOTS    PROC
  220. ;    push    bx
  221. ;    push    cx
  222.     push    dx
  223.     mov    ax, 092Eh    ; Print '.'
  224.     mov    bx, 0007h
  225.     mov    cx, 0030h    ; Here is how many dots will be printed
  226.     int    10h
  227. W1:
  228.     mov    ah, 01h        ; Check if key is pressed
  229.     int    16h
  230.     jz    W3        ; There is no key waiting
  231.  
  232.     cmp    al, 1Bh        ; ESC
  233.     je    W2
  234.     push    ax
  235.     stc
  236.     jmp    short W5
  237. W2:
  238.     mov    ah, 00        ; Remove ESC from keyboard buffer
  239.     int    16h
  240.     jmp    short W4
  241. W3:
  242.     mov    ax, 0E20h
  243.     int    10h        ; Print a SPACE
  244.  
  245.     push    bx
  246.     push    cx
  247.     mov    ah, 0
  248.     int    1Ah        ; Read System Timer
  249.     mov    bx, dx
  250. WT1:    int    1Ah        ; Wait one timer tick
  251.     cmp    bx, dx
  252.     je     WT1
  253.     mov    bx, dx
  254. WT2:    int    1Ah        ; Wait another timer tick
  255.     cmp    bx, dx
  256.     je     WT2
  257.     pop    cx
  258.     pop    bx
  259.  
  260.     loop    W1
  261. W4:
  262.     push    ax
  263.     clc
  264. W5:
  265.     pushf
  266.     mov    ax, 0920h    ; Print rest of SPACEs
  267.     int    10h
  268.     mov    bl, NL
  269.     call    PRINT
  270.     popf
  271.     pop    ax
  272.     pop    dx
  273. ;    pop    cx
  274. ;    pop    bx
  275.     ret
  276. DOTS    ENDP
  277.  
  278. GAP    PROC
  279. GAPLEN    EQU    (1BAh-(GAP-_IPL))
  280.     DB    GAPLEN DUP(0)    
  281. GAP    ENDP
  282.  
  283. FD_PARM    DB    00,00,01,00    ; Floppy disk boot sector parameters
  284.     DB    40h DUP(0)
  285.     DB    055h, 0AAh
  286. END
  287.