home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / lowlevel / fdisk / mfdisk.asm < prev    next >
Assembly Source File  |  1989-08-24  |  18KB  |  450 lines

  1.  
  2.         page    ,132
  3.         title   MFDISK - FDISK clone & multiple boot-record modifier
  4.   
  5. ;------------------------------------------------------------------------------
  6. ;       This program will write a new boot sector to the first HD drive
  7. ;       that prompts for the partition you want to boot from (1-4).
  8. ;       To the question 'Boot partition (1-4):' you have to answer with
  9. ;       the number of the partition you want to boot from (1-4).
  10. ;
  11. ;       The program must be compiled as a .COM file (remember exe2bin)
  12. ;       and run under MesSDOS.
  13. ;
  14. ;       The makefile for compiling under MesSDOS should look like:
  15. ;
  16. ;           mfdisk.obj:  mfdisk.asm
  17. ;                        masm mfdisk;
  18. ;
  19. ;           mfdisk.com:  mfdisk.obj
  20. ;                        link mfdisk;
  21. ;                        exe2bin mfdisk.exe mfdisk.com
  22. ;                        del mfdisk.exe
  23. ;
  24. ;       When the program is run, it produces a message telling you what
  25. ;       it is going to do. At this time you can abort it by typing
  26. ;       <CTRL-C> or type <CR> to allow it to modify the hard-disk.
  27. ;
  28. ;       Now you can boot multiple operating systems (MesSDOS, OS/2,
  29. ;       SCO Xenix and Minix) from the same hard-disk). To do so you
  30. ;       had to partition your hard-disk to separate partitions, each
  31. ;       with another operating-system boot.
  32. ;
  33. ;           Motti (Mordehai) Bazar
  34. ;
  35. ;           uucp    : ..uunet!ocsmd!motti
  36. ;           bitnet  : motti%ocsmd@uunet.uu.net
  37. ;           internet: motti@ocsmd.uu.net  
  38. ;
  39. ;           snail:    Online Computer Systems, Inc.
  40. ;                     20251 Century Blvd.
  41. ;                     Germantown, MD 20874
  42. ;
  43. ;           vox:      (301) 428-3700
  44. ;
  45. ;       This utility is distributed for personal use only. Use it, modify it
  46. ;       or do anything you like, just live this comment and let me know of
  47. ;       any bugs or enhancements you made to it, thanks.
  48. ;
  49. ;------------------------------------------------------------------------------
  50.   
  51.   
  52. ;-------------------------
  53. ; First, some definitions
  54. ;-------------------------
  55.   
  56. VIDEO   equ     10h
  57. KBD     equ     16h
  58. DISKIO  equ     13h
  59. DOS     equ     21h
  60.   
  61. CR      equ     0Dh
  62. LF      equ     0Ah
  63.   
  64.   
  65. ;---------------------------
  66. ; Here comes the real thing
  67. ;---------------------------
  68.   
  69. CODE    segment byte public 'CODE'
  70.         assume  cs:CODE, ds:CODE
  71.   
  72.         org     100h
  73.   
  74. WRITBOOT:
  75.         jmp     START                           ; jump around
  76.   
  77. DISKBUF db      512 dup(?)
  78.   
  79. HELLOMS db      CR, LF
  80.         db      'MFDISK - Multiple boot FDISK clone', CR, LF
  81.         db      CR, LF
  82.         db      'This program will read the boot sector from the first disk', CR, LF
  83.         db      'and merge the partition table from it with the boot program', CR, LF
  84.         db      'supplied and write it all back to the disk overlaying what', CR, LF
  85.         db      'was there. The resultant boot program will prompt for the', CR, LF
  86.         db      'desired partition for booting (1-4).', CR, LF
  87.         db      CR, LF
  88.         db      'The original boot program will be saved in a file named', CR, LF
  89.         db      'IBMBOOT.SVE in the default directory.', CR, LF
  90.         db      CR, LF
  91.         db      '<CR> = continue, CONTROL-C = abort ... $'
  92.   
  93. CRLF    db      CR, LF, '$'
  94.   
  95. IBMBOOT db      'IBMBOOT.SVE',0
  96.   
  97. OKMSG   db      CR, LF, 'Boot sector updated', CR, LF, '$'
  98. ERMSG1  db      CR, LF, 'Error reading boot sector', CR, LF, '$'
  99. ERMSG2  db      CR, LF, 'Cannot open save file', CR, LF, '$'
  100. ERMSG3  db      CR, LF, 'Cannot write save file', CR, LF, '$'
  101. ERMSG4  db      CR, LF, 'Cannot close save file', CR, LF, '$'
  102. ERMSG5  db      CR, LF, 'Cannot write boot sector', CR, LF, '$'
  103.   
  104. BOOTHDL dw      0                               ; Boot backup file handle
  105.   
  106.   
  107. START:
  108.         ;----------------------
  109.         ; Show opening message
  110.         ;----------------------
  111.         mov     dx, offset HELLOMS
  112.         mov     ah, 9
  113.         int     DOS
  114.   
  115. RDCNS:
  116.         ;---------------------------------------------------------
  117.         ; Get user's response: <CR> or CONTROL-C (aborted by DOS)
  118.         ;---------------------------------------------------------
  119.         mov     ah, 0Ch
  120.         mov     al, 1
  121.         int     DOS
  122.         cmp     al, CR                          ; <CR> key ?
  123.         jne     RDCNS                           ;  No, retry
  124.   
  125.         ;-----------------------------------------
  126.         ; <CR> typed, go and modify the hard-disk
  127.         ;-----------------------------------------
  128.         mov     dx, offset CRLF                 ; Echo <CR><LF>
  129.         mov     ah, 9
  130.         int     DOS
  131.   
  132.         ;------------------------------------
  133.         ; Read the sector from the hard-disk
  134.         ;------------------------------------
  135.         mov     si, 5                           ; Retry for 5 times
  136. RDDSK:
  137.         push    si                              ; Save retry counter
  138.         mov     ah, 2                           ; Read disk
  139.         mov     al, 1                           ; 1 sector
  140.         mov     bx, offset DISKBUF              ; Point to buffer
  141.         mov     ch, 0                           ; Cylinder 0
  142.         mov     cl, 1                           ; Sector 1
  143.         mov     dh, 0                           ; Track 0
  144.         mov     dl, 80h                         ; Harddisk 0
  145.         int     DISKIO
  146.         pop     si
  147.         jnc     RDOK                            ; Good read
  148.         dec     si                              ; More tries?
  149.         jnz     RDDSK                           ;  Yes, try again
  150.         mov     dx, offset ERMSG1               ;  No, point to trouble message
  151.         mov     ah, 9                           ; say console write
  152.         int     DOS                             ; can't read boot sector
  153.         mov     ah, 4Ch                         ; say terminate
  154.         mov     al, 1                           ; error return code
  155.         int     DOS
  156.   
  157.         ;---------------------------------
  158.         ; Create the file to save it into
  159.         ;---------------------------------
  160. RDOK:
  161.         mov     dx, offset IBMBOOT              ; get file name
  162.         mov     ah, 3Ch                         ; say create it
  163.         sub     cx, cx                          ; zero attribute
  164.         int     DOS
  165.         jnc     OPENOK                          ; file opened
  166.         mov     dx, offset ERMSG2               ; can't open save file
  167.         mov     ah, 9
  168.         int     DOS                             ; tell 'em
  169.         mov     ah, 4Ch                         ; say terminate
  170.         mov     al, 2                           ; reason
  171.         int     DOS
  172.   
  173.         ;---------------------------------
  174.         ; Write sector to the backup file
  175.         ;---------------------------------
  176. OPENOK:
  177.         mov     BOOTHDL, ax                     ; save handle
  178.         mov     bx, ax                          ; and put in bx
  179.         mov     ah, 40h                         ; say write to file
  180.         mov     dx, offset DISKBUF              ; get location
  181.         mov     cx, 512                         ; say 1 sector
  182.         int     DOS                             ; ask to have it done
  183.         jnc     WRTOK                           ; good write
  184.         mov     dx, offset ERMSG3               ; can't write to save file
  185.         mov     ah, 9
  186.         int     DOS                             ; tell 'em
  187.         mov     ah, 4Ch                         ; say terminate
  188.         mov     al, 3                           ; reason
  189.         int     DOS
  190.   
  191.         ;-------------------
  192.         ; Close backup file
  193.         ;-------------------
  194. WRTOK:
  195.         mov     ah, 3Eh                         ; say close file
  196.         mov     bx, BOOTHDL
  197.         int     DOS
  198.         jnc     CLOSOK                          ; closed ok
  199.         mov     dx, offset ERMSG4               ; can't close save file
  200.         mov     ah, 9
  201.         int     DOS                             ; tell 'em
  202.         mov     ah, 4Ch                         ; say terminate
  203.         mov     al, 4                           ; reason
  204.         int     DOS
  205.   
  206.         ;-----------------------------------------------------
  207.         ; Copy the read partition table into our sector so it
  208.         ; will use it when booting.
  209.         ;-----------------------------------------------------
  210. CLOSOK:
  211.         push    si
  212.         mov     si, offset DISKBUF+1BEh         ; start of partition table
  213.         mov     di, offset BOOTSEC+1BEh         ; ditto for new sector
  214.         cld                                     ; make direction positive
  215.         mov     cx, 4*16+2                      ; how many bytes to move
  216.         rep     movsb                           ; move em
  217.   
  218.         ;----------------------------------------
  219.         ; Write our sector back to the hard-disk
  220.         ;----------------------------------------
  221.         mov     si, 5                           ; 5 retries
  222. WRTBOOT:
  223.         push    si
  224.         mov     ah, 3                           ; say write to disk
  225.         mov     al, 1                           ; say 1 sector
  226.         mov     bx, offset BOOTSEC              ; point to sector to write
  227.         mov     cx, 1                           ; say sector 1
  228.         mov     dx, 80h
  229.         int     DISKIO                          ; do the io
  230.         pop     si
  231.         jnc     UPDOK                           ; good write to boot sector
  232.         dec     si                              ; count retries
  233.         jnz     WRTBOOT                         ; try again
  234.   
  235.         mov     dx, offset ERMSG5               ; can't write boot sector file
  236.         mov     ah, 9
  237.         int     DOS                             ; tell 'em
  238.         mov     ah, 4Ch                         ; say terminate
  239.         mov     al, 5                           ; reason
  240.         int     DOS
  241.   
  242.         ;----------------------------------------------------------
  243.         ; Inform the user that the operation finished successfully
  244.         ; and exit back to DOS.
  245.         ;----------------------------------------------------------
  246. UPDOK:
  247.         mov     dx, offset OKMSG                ; say we did it
  248.         mov     ah, 9
  249.         int     DOS                             ; tell 'em
  250.         mov     ah, 4Ch                         ; say terminate
  251.         mov     al, 0                           ; good return code
  252.         int     DOS
  253.   
  254.   
  255.   
  256. ;****************************************************************
  257. ;*      BOOTSEC                                                 *
  258. ;*                                                              *
  259. ;*      Here comes the sector patched into the hard-disk        *
  260. ;****************************************************************
  261.   
  262. BOOTSEC:
  263.   
  264. BSTART  equ     $                               ; Start offset of our boot sector
  265. BOOTLOC equ     7C00h                           ; Boot sector loaded there
  266. BOOTSIG equ     7DFEh                           ; Boot sector signal address
  267. MOVETO  equ     600h                            ; Where to move it
  268. PARTTBL equ     BSTART + 1BEh                   ; Start of partition table in our code
  269.   
  270.   
  271.         cli
  272.         xor     ax, ax                          ; (AX) = 0
  273.         mov     ss, ax                          ; (SS) = Set stack segment to 0
  274.         mov     sp, offset BOOTLOC              ; Get boot code address
  275.         mov     si, sp                          ; (SI) = Source address
  276.         mov     es, ax                          ; (ES) = 0
  277.         mov     ds, ax                          ; (DS) = 0
  278.         sti
  279.         cld
  280.         mov     di, MOVETO                      ; (DI) = Destination address
  281.         mov     cx, 100h                        ; (CX) = # of words to move
  282.         rep     movsw                           ; Move it
  283.   
  284.         ;------------------------------------
  285.         ; Setup to continue the boot process
  286.         ;------------------------------------
  287.         mov     ax, MOVETO + offset RELBOOT - BSTART
  288.         push    ax
  289.         ret
  290.   
  291. RELBOOT:
  292.         ;----------------------------------
  293.         ; Set all partitions as not active
  294.         ;----------------------------------
  295.         mov     bx, MOVETO + 1BEh
  296.         mov     byte ptr [bx],   0
  297.         mov     byte ptr 16[bx], 0
  298.         mov     byte ptr 32[bx], 0
  299.         mov     byte ptr 48[bx], 0
  300.   
  301.         ;------------------------------------------
  302.         ; Show boot prompt 'Boot partition (1-4):'
  303.         ;------------------------------------------
  304. WRPRMPT:
  305.         mov     si, MOVETO + offset BOOTMSG - BSTART
  306.         lodsb
  307.         mov     cl, al
  308.         xor     ch, ch
  309. WR000:
  310.         lodsb
  311.         mov     bx, 7
  312.         mov     ah, 0Eh
  313.         int     VIDEO
  314.         loop    WR000
  315.   
  316.         ;-------------------------------
  317.         ; Get user's response & show it
  318.         ;-------------------------------
  319.         mov     ah, 0                           ; Get keyboard input
  320.         int     KBD
  321.         push    ax
  322.         mov     ah, 10                          ; Show it
  323.         mov     cx, 1
  324.         int     VIDEO
  325.         pop     ax
  326.   
  327.         ;----------------------------------
  328.         ; Check for legal partition number
  329.         ;----------------------------------
  330.         cmp     al, '1'                         ; # below 1 ?
  331.         jb      WRPRMPT                         ;  Yes, error
  332.         cmp     al, '4'                         ; # above 4 ?
  333.         ja      WRPRMPT                         ;  Yes, error
  334.   
  335.         ;--------------------------------------
  336.         ; AL contains partition # to boot from
  337.         ;--------------------------------------
  338.         and     al, 7                           ; Mask partition #
  339.   
  340.         ;----------------------------------------
  341.         ; Calculate partition table entry to use
  342.         ;----------------------------------------
  343.         mov     si, MOVETO + 1BEh               ; point to first entry
  344. CALCPART:
  345.         dec     al
  346.         jz      GOTPART
  347.         add     si, 16                          ; Advance to next partition
  348.         jmp     short CALCPART
  349.   
  350.   
  351. GOTPART:
  352.         ;-----------------------------------------------------
  353.         ; Set the requested partition as the active partition
  354.         ;-----------------------------------------------------
  355.         mov     byte ptr[si], 80h
  356.         push    si
  357.         push    bx
  358.         mov     ax, 0301h
  359.         mov     bx, MOVETO
  360.         mov     cx, 1
  361.         mov     dx, 80h
  362.         int     diskio
  363.         pop     bx
  364.         pop     si
  365.   
  366.         ;-------------------------------------------------------------
  367.         ; Now go and try to read the selected partition's boot sector
  368.         ;-------------------------------------------------------------
  369.         mov     dx, [si]                        ; (DH) = drive, (DL) = head
  370.         mov     cx, [si+2]                      ; (CH) = track, (CL) = sector
  371.         mov     bp, si                          ; Save partition pointer
  372.         mov     di, 5                           ; Set retry count
  373. RDBOOT:
  374.         mov     bx, BOOTLOC                     ; Location for boot
  375.         mov     ax, 0201h                       ; Read 1 sector
  376.         push    di
  377.         int     DISKIO                          ; Go read
  378.   
  379.         pop     di
  380.         jnc     GOODRD                          ; Good read
  381.   
  382.         xor     ax, ax                          ; Recalibrate
  383.         int     DISKIO
  384.         dec     di                              ; Decrement retries count
  385.         jnz     RDBOOT                          ; Counter at zero ?
  386.   
  387.         ;---------------------------------------------------
  388.         ; Can't read boot sector, show a message and hangup
  389.         ;---------------------------------------------------
  390.         mov     si, MOVETO + offset MSG2 - BSTART
  391. WRMSG:
  392.         lodsb                                   ; Get message length
  393.         mov     cl, al
  394.         xor     ch, ch
  395. WR002:
  396.         lodsb
  397.         mov     bx, 7
  398.         mov     ah, 0Eh
  399.         int     VIDEO
  400.         loop    WR002
  401.         jmp     WRPRMPT
  402.   
  403.   
  404.         ;------------------------------------------------
  405.         ; Boot sector read, check for the boot signature
  406.         ;------------------------------------------------
  407. GOODRD:
  408.         mov     si, MOVETO + offset MSG3 - BSTART       ; Point to no boot msg
  409.         mov     bx, BOOTSIG
  410.         cmp     word ptr [bx], 0AA55h           ; Check for the boot signature
  411.         jne     WRMSG
  412.   
  413.         ;-----------------------------------------------------------------
  414.         ; Finaly, go and boot.
  415.         ;
  416.         ; Before booting set:
  417.         ;       SI - points to partition table entry we are booting from
  418.         ;       BX - starting cylinder number of this partition
  419.         ;-----------------------------------------------------------------
  420.         mov     si, bp                          ; Restore partition table pointer
  421.   
  422.         mov     ax, word ptr 2[si]              ; AH-cyl, AL-sec
  423.         xor     bh, bh
  424.         mov     bl, al
  425.         shl     bx, 1
  426.         shl     bx, 1                           ; BH-2 msb bits
  427.         mov     bl, ah                          ; BL-8 lsb bits
  428.   
  429.         mov     ax, offset BOOTLOC              ; Where partition boot start
  430.         push    ax
  431.         ret
  432.   
  433.   
  434. BOOTMSG db      LMSGB, CR, LF, 'Boot partition (1-4): '
  435. LMSGB   equ     ($-BOOTMSG)-1
  436.   
  437. MSG2    db      LMSG2, CR, LF, 'Error loading operating system'
  438. LMSG2   equ     ($-MSG2)-1
  439.   
  440. MSG3    db      LMSG3, CR, LF, 'Missing operating system'
  441. LMSG3   equ     ($-MSG3)-1
  442.   
  443.         org     BSTART + 512
  444.   
  445. CODE    ends
  446.         end  WRITBOOT
  447.   
  448.  
  449.  
  450.