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