home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Games / WHDLoad / Src / programs / InstallBB.asm < prev    next >
Assembly Source File  |  1998-11-22  |  10KB  |  493 lines

  1. ;*---------------------------------------------------------------------------
  2. ;  :Program.    InstallBB.asm
  3. ;  :Contents.    write/read BB to/from EXE
  4. ;  :Author.    Bert Jahn
  5. ;  :EMail.    wepl@kagi.com
  6. ;  :Address.    Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
  7. ;  :History.    V 1.0 ???
  8. ;        V 1.1 23.09.95
  9. ;        1.2    20.01.96 adjusted for Sources:
  10. ;            21.01.96 reworked
  11. ;        1.3    28.07.96 no longer dos.Inhibit if reading from disk
  12. ;                 now diskchange will create an error
  13. ;                 now checks disk for write protection
  14. ;                 verify implemented
  15. ;  :Requires.    OS V37+
  16. ;  :Copyright.    © 1995,1996,1997,1998 Bert Jahn, All Rights Reserved
  17. ;  :Language.    68000 Assembler
  18. ;  :Translator.    Barfly V1.130
  19. ;---------------------------------------------------------------------------*
  20. ;##########################################################################
  21.  
  22.     INCDIR    Includes:
  23.     INCLUDE    lvo/exec.i
  24.     INCLUDE    exec/io.i
  25.     INCLUDE    exec/memory.i
  26.     INCLUDE    lvo/dos.i
  27.     INCLUDE    dos/dos.i
  28.     INCLUDE    devices/trackdisk.i
  29.     
  30. ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  31.  
  32. GL    EQUR    A4        ;a4 ptr to Globals
  33. LOC    EQUR    A5        ;a5 for local vars
  34.  
  35.     STRUCTURE    Globals,0
  36.         APTR    gl_execbase
  37.         APTR    gl_dosbase
  38.         APTR    gl_rdargs
  39.         LABEL    gl_rdarray
  40.         ULONG    gl_rdloadfile
  41.         ULONG    gl_rddiskunit
  42.         ULONG    gl_rdwrite
  43.         STRUCT    gl_bb,1024
  44.         LABEL    gl_SIZEOF
  45.  
  46. ;##########################################################################
  47.  
  48.     SECTION    "",CODE,RELOC16
  49.     OUTPUT    "c:InstallBB"
  50.  
  51.  
  52. VER    MACRO
  53.         dc.b    "InstallBB 1.3 "
  54.     DOSCMD    "WDate >t:date"
  55.     INCBIN    "t:date"
  56.         dc.b    " by Bert Jahn"
  57.     ENDM
  58.  
  59.         bra    .start
  60.         dc.b    "$VER: "
  61.         VER
  62.         dc.b    " V37+"
  63.         dc.b    " (read/write BB to/from EXE)",0
  64.     CNOP 0,2
  65. .start
  66.  
  67.         move.l    #gl_SIZEOF,d0
  68.         move.l    #MEMF_PUBLIC|MEMF_CLEAR,d1
  69.         move.l    (4).w,a6
  70.         jsr    (_LVOAllocMem,a6)
  71.         tst.l    d0
  72.         beq    .nostrucmem
  73.         move.l    d0,GL            ;GL = PubMem
  74.         move.l    a6,(gl_execbase,GL)
  75.  
  76.         move.l    #37,d0
  77.         lea    (_dosname),a1
  78.         move.l    (gl_execbase,GL),a6
  79.         jsr    _LVOOpenLibrary(a6)
  80.         move.l    d0,(gl_dosbase,GL)
  81.         beq    .nodoslib
  82.  
  83.         lea    (_ver),a0
  84.         bsr    _Print
  85.  
  86.         lea    (_template),a0
  87.         move.l    a0,d1
  88.         lea    (gl_rdarray,GL),a0
  89.         move.l    a0,d2
  90.         moveq    #0,d3
  91.         move.l    (gl_dosbase,GL),a6
  92.         jsr    (_LVOReadArgs,a6)
  93.         move.l    d0,(gl_rdargs,GL)
  94.         bne    .argsok
  95.         lea    (_readargs),a0
  96.         bsr    _PrintErrorDOS
  97.         bra    .noargs
  98. .argsok
  99.         move.l    (gl_rddiskunit,GL),d0
  100.         beq    .unitok
  101.         move.l    d0,a0
  102.         move.l    (a0),(gl_rddiskunit,GL)
  103. .unitok
  104.         pea    (.back)
  105.         tst.l    (gl_rdwrite,GL)
  106.         bne    _Write
  107.         bra    _Read
  108. .back
  109.         move.l    (gl_rdargs,GL),d1
  110.         move.l    (gl_dosbase,GL),a6
  111.         jsr    (_LVOFreeArgs,a6)
  112. .noargs
  113.         move.l    (gl_dosbase,GL),a1
  114.         move.l    (gl_execbase,GL),a6
  115.         jsr    (_LVOCloseLibrary,a6)
  116. .nodoslib
  117.         move.l    #gl_SIZEOF,d0
  118.         move.l    (gl_execbase,GL),a6
  119.         move.l    GL,a1
  120.         jsr    (_LVOFreeMem,a6)
  121. .nostrucmem
  122.         moveq    #0,d0
  123.         rts
  124.  
  125. ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  126. ;----------------------------------------
  127. ; Operation File
  128. ; Übergabe :    
  129. ; Rückgabe :    
  130.  
  131. _Write        moveq    #BB_READ,d0            ;read old bootblock
  132.         bsr    _ReadWriteBB
  133.         tst.l    d0
  134.         beq    .end
  135.  
  136.         lea    (gl_bb,GL),a0            ;chk FS-ID (DOS\0 DOS\1 ...)
  137.         move.l    (a0),d0
  138.         clr.b    d0
  139.         move.l    #"DOS"<<8,d1
  140.         cmp.l    d1,d0
  141.         beq    .bbvalid
  142.         move.l    d1,(a0)
  143. .bbvalid
  144.         addq.w    #4,a0
  145.         clr.l    (a0)+                ;reset chksum
  146.         move.l    #$0000370,(a0)+
  147.         move.w    #253-1,d1
  148.         moveq    #-1,d0                ;init data area with $FFFFFFFF
  149. .bbclear    move.l    d0,(a0)+
  150.         dbf    d1,.bbclear
  151.  
  152.         move.l    (gl_rdloadfile,GL),a0
  153.         bsr    _LoadFile
  154.         move.l    d0,d6                ;D6 = aptr src
  155.         beq    .end
  156.         move.l    d1,d7                ;D7 = size of src
  157.  
  158.         move.l    d6,a0
  159.         cmp.l    #$000003F3,(a0)+    ;hunk CODE
  160.         bne    .noexe
  161.  
  162. .nextname    move.l    (a0)+,d0        ;name
  163.         beq    .noname
  164.         add.l    d0,d0
  165.         add.l    d0,d0
  166.         bmi    .corrupt
  167.         add.l    d0,a0
  168.         bra    .nextname
  169.  
  170. .noname        cmp.l    #1,(a0)+        ;count of hunks
  171.         bne    .no1hunk
  172.  
  173.         addq.l    #8,a0            ;lowhunk,highhunk
  174.         cmp.l    #253,(a0)+        ;
  175.         bhi    .tolarge
  176.         cmp.l    #$000003E9,(a0)+    ;code
  177.         bne    .nocode
  178.         move.l    (a0)+,d0        ;lw's
  179.         add.l    d0,d0
  180.         add.l    d0,d0
  181.         lea    (a0,d0.l),a1
  182.         cmp.l    #$3EC,(a1)        ;reloc32
  183.         beq    .relocs
  184.         cmp.l    #$3ED,(a1)        ;reloc16
  185.         beq    .relocs
  186.         cmp.l    #$3EE,(a1)        ;reloc8
  187.         beq    .relocs
  188.         cmp.l    #$3F2,(a1)        ;end
  189.         beq    .srcok
  190.         cmp.l    #$3F0,(a1)        ;symbol
  191.         beq    .srcok
  192.         cmp.l    #$3F1,(a1)        ;debug
  193.         beq    .srcok
  194. .uhunk        lea    (_err_uhunk),a0
  195.         bra    .errgo
  196. .corrupt    lea    (_err_corrupt),a0
  197.         bra    .errgo
  198. .relocs        lea    (_err_relocs),a0
  199.         bra    .errgo
  200. .tolarge    lea    (_err_tolarge),a0
  201.         bra    .errgo
  202. .no1hunk    lea    (_err_no1hunk),a0
  203.         bra    .errgo
  204. .noexe        lea    (_err_noexe),a0
  205.         bra    .errgo
  206. .nocode        lea    (_err_nocode),a0
  207. .errgo        moveq    #0,d0
  208.         sub.l    a1,a1
  209.         bsr    _PrintError
  210.         bra    .srcerr
  211.         
  212.     ;a0 = begin d0 = size
  213. .srcok        lea    (gl_bb+12,GL),a1    ;copy code to bb
  214.         lsr.l    #2,d0
  215.         subq.l    #1,d0
  216. .cp        move.l    (a0)+,(a1)+
  217.         dbf    d0,.cp
  218.         
  219.         lea    (gl_bb,GL),a1        ;calculate chksum
  220.         moveq    #0,d0
  221.         move.w    #255,d1
  222. .sum        add.l    (a1)+,d0
  223.         bcc    .sum2
  224.         addq.l    #1,d0
  225. .sum2        dbf    d1,.sum
  226.         not.l    d0
  227.         move.l    d0,(gl_bb+4,GL)        ;set the chksum
  228.  
  229.         move.l    d7,d0
  230.         move.l    d6,a1
  231.         move.l    (gl_execbase,GL),a6
  232.         jsr    (_LVOFreeMem,a6)
  233.         
  234.         moveq    #BB_WRITE,d0        ;write the bootblock
  235.         bsr    _ReadWriteBB
  236.         tst.l    d0
  237.         beq    .end
  238.         
  239.         bsr    .vsum
  240.         move.l    d0,d3
  241.         moveq    #BB_READ,d0        ;read the bootblock
  242.         bsr    _ReadWriteBB
  243.         tst.l    d0
  244.         beq    .end
  245.         bsr    .vsum
  246.         cmp.l    d0,d3
  247.         beq    .end
  248.         moveq    #0,d0
  249.         lea    _err_verify,a0
  250.         sub.l    a1,a1
  251.         bsr    _PrintError
  252. .srcerr
  253. .end        rts
  254.  
  255. .vsum        lea    (gl_bb,GL),a0
  256.         move.l    (a0)+,d0
  257.         move.w    #254,d1
  258. .s1        move.l    (a0)+,d2
  259.         eor.l    d2,d0
  260.         dbf    d1,.s1
  261.         rts
  262.  
  263. ;----------------------------------------
  264. ; Operation File
  265. ; Übergabe :    
  266. ; Rückgabe :    
  267.  
  268. _Read        moveq    #BB_READ,d0            ;read
  269.         bsr    _ReadWriteBB
  270.         tst.l    d0
  271.         beq    .end
  272.  
  273.         move.l    (gl_rdloadfile,GL),d1        ;name
  274.         move.l    #MODE_NEWFILE,d2        ;mode
  275.         move.l    (gl_dosbase,GL),a6
  276.         jsr    (_LVOOpen,a6)
  277.         move.l    d0,d7                ;D7 = fh
  278.         bne    .openok
  279.         lea    (_openfile),a0
  280.         bsr    _PrintErrorDOS
  281.         bra    .erropen
  282. .openok
  283.         move.l    d7,d1            ;fh
  284.         lea    (.bbexe_begin),a0
  285.         move.l    a0,d2            ;buffer
  286.         move.l    #8*4,d3            ;length
  287.         move.l    (gl_dosbase,GL),a6
  288.         jsr    (_LVOWrite,a6)
  289.         cmp.l    #8*4,d0
  290.         bne    .writeerr
  291.         
  292.         move.l    d7,d1            ;fh
  293.         lea    (gl_bb+12,GL),a0
  294.         move.l    a0,d2            ;buffer
  295.         move.l    #1024-12,d3        ;length
  296.         move.l    (gl_dosbase,GL),a6
  297.         jsr    (_LVOWrite,a6)
  298.         cmp.l    #1024-12,d0
  299.         bne    .writeerr
  300.  
  301.         move.l    d7,d1            ;fh
  302.         lea    (.bbexe_end),a0
  303.         move.l    a0,d2            ;buffer
  304.         move.l    #4,d3            ;length
  305.         move.l    (gl_dosbase,GL),a6
  306.         jsr    (_LVOWrite,a6)
  307.         cmp.l    #4,d0
  308.         beq    .allok
  309.  
  310. .writeerr    lea    (_writefile),a0
  311.         bsr    _PrintErrorDOS
  312. .allok
  313.         move.l    d7,d1            ;fh
  314.         move.l    (gl_dosbase,GL),a6
  315.         jsr    (_LVOClose,a6)
  316. .erropen
  317. .end        rts
  318.  
  319. .bbexe_begin    dc.l    $000003F3,$00000000,$00000001,$00000000
  320.         dc.l    $00000000,$000000FD,$000003E9,$000000FD
  321. .bbexe_end    dc.l    $000003F2
  322.  
  323. ;----------------------------------------
  324. ; Lesen / Schreiben Bootblock
  325. ; Übergabe :    D0 = WORD operation
  326. ; Rückgabe :    D0 = BOOL success
  327.  
  328. BB_READ        = 0
  329. BB_WRITE    = 1
  330.  
  331. _ReadWriteBB    movem.l    d5-d7/a2/a6,-(a7)
  332.         move.l    d0,d6            ;D6 = operation
  333.         moveq    #0,d7            ;D7 = return
  334.  
  335.         move.l    (gl_execbase,GL),a6
  336.         jsr    (_LVOCreateMsgPort,a6)
  337.         move.l    d0,d5            ;D5 = msgport
  338.         bne    .portok
  339.         moveq    #0,d0
  340.         lea    (_noport),a0
  341.         sub.l    a1,a1
  342.         bsr    _PrintError
  343.         bra    .noport
  344. .portok        
  345.         move.l    d5,a0
  346.         move.l    #IOTD_SIZE,d0
  347.         jsr    (_LVOCreateIORequest,a6)
  348.         move.l    d0,a2            ;A2 = ioreq
  349.         tst.l    d0
  350.         bne    .ioreqok
  351.         moveq    #0,d0
  352.         lea    (_noioreq),a0
  353.         sub.l    a1,a1
  354.         bsr    _PrintError
  355.         bra    .noioreq
  356. .ioreqok
  357.         lea    (_tdname),a0
  358.         move.l    (gl_rddiskunit,GL),d0    ;unit
  359.         move.l    a2,a1            ;ioreq
  360.         move.l    #0,d1            ;flags
  361.         jsr    (_LVOOpenDevice,a6)
  362.         tst.l    d0
  363.         beq    .deviceok
  364.         move.b    (IO_ERROR,a2),d0
  365.         lea    (_opendevice),a0
  366.         bsr    _PrintErrorTD
  367.         bra    .nodevice
  368. .deviceok
  369.         move.l    a2,a1
  370.         move.w    #TD_CHANGENUM,(IO_COMMAND,a1)
  371.         jsr    (_LVODoIO,a6)
  372.         move.l    (IO_ACTUAL,a2),(IOTD_COUNT,a2)    ;the diskchanges
  373.  
  374.         cmp.w    #BB_WRITE,d6
  375.         bne    .nw1            ;only if writing
  376.         moveq    #-1,d0
  377.         bsr    _InhibitDrive
  378.  
  379.         move.l    a2,a1
  380.         move.w    #TD_PROTSTATUS,(IO_COMMAND,a1)
  381.         jsr    (_LVODoIO,a6)
  382.         move.b    #TDERR_WriteProt,d0
  383.         tst.l    (IO_ACTUAL,a2)
  384.         bne    .error
  385. .nw1
  386.         move.l    a2,a1
  387.         lea    (gl_bb,GL),a0
  388.         move.l    a0,(IO_DATA,a1)                ;Buffer
  389.         move.l    #0,(IO_OFFSET,a1)            ;ab Block 0
  390.         move.l    #$400,(IO_LENGTH,a1)            ;2 Blöcke
  391.         move.w    #ETD_WRITE,(IO_COMMAND,a1)
  392.         cmp.w    #BB_READ,d6
  393.         bne    .notread
  394.         move.w    #ETD_READ,(IO_COMMAND,a1)
  395. .notread    jsr    (_LVODoIO,a6)
  396.         moveq    #-1,d7
  397.         move.b    (IO_ERROR,a2),d0
  398.         beq    .readok
  399. .error        sub.l    a0,a0
  400.         bsr    _PrintErrorTD
  401.         moveq    #0,d7
  402. .readok
  403.         move.l    a2,a1
  404.         move.l    #0,(IO_LENGTH,a1)
  405.         move.w    #TD_MOTOR,(IO_COMMAND,a1)
  406.         jsr    (_LVODoIO,a6)
  407.         
  408.         cmp.w    #BB_WRITE,d6
  409.         bne    .nw2            ;only if writing
  410.         moveq    #0,d0
  411.         bsr    _InhibitDrive
  412. .nw2
  413.         move.l    a2,a1
  414.         jsr    (_LVOCloseDevice,a6)
  415.         
  416. .nodevice    move.l    a2,a0
  417.         jsr    (_LVODeleteIORequest,a6)
  418.         
  419. .noioreq    move.l    d5,a0
  420.         jsr    (_LVODeleteMsgPort,a6)
  421.         
  422. .noport        move.l    d7,d0
  423.         movem.l    (a7)+,d5-d7/a2/a6
  424.         rts
  425.  
  426. ;----------------------------------------
  427. ; Laufwerk busy schalten
  428. ; Übergabe :    D0 = BOOL off
  429. ; Rückgabe :    D0 = BOOL success
  430.  
  431. _InhibitDrive    movem.l    d2/a6,-(a7)
  432.         lea    (.name),a0
  433.         move.l    (gl_rddiskunit,GL),d1
  434.         add.b    #"0",d1
  435.         move.b    d1,(2,a0)
  436.         move.l    a0,d1
  437.         move.l    d0,d2
  438.         move.l    (gl_dosbase,GL),a6
  439.         jsr    (_LVOInhibit,a6)
  440.         movem.l    (a7)+,d2/a6
  441.         rts
  442.  
  443. .name        dc.b    "DFx:",0
  444.     EVEN
  445.  
  446. ;##########################################################################
  447.  
  448.     INCDIR    Sources:
  449.     INCLUDE    dosio.i
  450.         Print
  451.         PrintLn
  452.     INCLUDE    error.i
  453.         PrintErrorDOS
  454.         PrintErrorTD
  455.     INCLUDE    files.i
  456.         LoadFile
  457.  
  458. ;##########################################################################
  459.  
  460. _noport        dc.b    "can't create MessagePort",0
  461. _noioreq    dc.b    "can't create IO-Request",0
  462. _opendevice    dc.b    "can't open trackdisk.device",0
  463.  
  464. _err_noexe    dc.b    "source is not an executable",0
  465. _err_relocs    dc.b    "source contain relocs (not pc-relative)",0
  466. _err_tolarge    dc.b    "code size to large  (>1012 Bytes)",0
  467. _err_nocode    dc.b    "first hunk isn't code",0
  468. _err_no1hunk    dc.b    "more than 1 hunk in source",0
  469. _err_corrupt    dc.b    "executable is corrupt",0
  470. _err_uhunk    dc.b    "unknown hunk in source",0
  471. _err_verify    dc.b    "verify error",0
  472.  
  473. ; Operationen
  474. _readargs    dc.b    "read arguments",0
  475. _openfile    dc.b    "open file",0
  476. _writefile    dc.b    "write file",0
  477.  
  478. _dosname    dc.b    "dos.library",0
  479. _tdname        dc.b    "trackdisk.device",0
  480.  
  481. _template    dc.b    "PROGRAM/A"        ;File to load or save
  482.         dc.b    ",UNIT/K/N"        ;unit for trackdisk.device
  483.         dc.b    ",WRITE/S"        ;if not then read
  484.         dc.b    0
  485.  
  486. _ver        VER
  487.         dc.b    10,0
  488.  
  489. ;##########################################################################
  490.  
  491.     END
  492.  
  493.