; SYS.ASM     - System file transfer utility      gryphonphyre@bluebottle.com     
; ------------- (c) 2004-2005 dataMAverick, Ltd.  tekno1911@yahoo.com
; 3-7-2004 Turbo C 2.01
; 3-6-2005 Turbo Assembler port, modeled after MSDOS 5.0 SYS.COM
; 3-4-2006 Fixed Filecopy bug, seems WinXP could not lock drive for exclusive
;          access. so I reordered the calls around. Files copied first then
;          boot sector. If bootsector doesnt write under WinXP then just use
;          writeb.com. 
; ---------------------------------------------------------------------------

code            segment byte public
                assume  cs:code, ds:code, es:code

		org	100h

include         \nxdos\src\kernel\nxdos.equ

sys             proc    far

start:          jmp     initialize
_sysid           db ' - SYS - Utility ',0
include         \nxdos\src\toolbox.asm
include         \nxdos\src\kernel\dma.asm
include         \nxdos\src\kernel\disk.asm
include         \nxdos\src\kernel\dataseg.inc
initialize:
                call cs:main
                xor  ax, ax
                xor  bx, bx
                xor  cx, cx
                xor  dx, dx
                xor  si, si
                xor  di, di
                call cs:copyfiles
exit:           call dosexit
sys             endp

main proc far

                mov  bh, '/'
                mov  bl, '?'
                call scancmdline
                mov  argc, cx
                or   cx, cx
                jnz  _chkparams
                _print missingparam, 1
                xor  ax, ax
                call dosexit
_chkparams:
                cmp  dl, 1
                jne  _nohelp
                ; jnc  _nohelp
                _print help, 1
                xor  ax, ax
                jmp  _makebootable
_nohelp:
                mov  bh, 'A'
                mov  bl, ':'
                call scancmdline
                jc   _firstfloppy
                mov  bh, 'B'
                mov  bl, ':'
                call scancmdline
                jc   _firstfloppy
                mov  al, 1
                mov  cl, 'B'
                mov  byte ptr kernelpath, cl
                mov  byte ptr commandpath, cl
                jmp  _setdrivenum
_firstfloppy:   mov  al, 0
_setdrivenum:   mov  _diskdrive, al

                mov  bh, '/'
                mov  bl, 'N'
                call scancmdline
                jnc  _chksimplehello
                mov  al, 1
                mov  nonbootdisk, al
                jmp  _makebootable
_chksimplehello:
                mov  bh, '/'
                mov  bl, 'H'
                call scancmdline
                jnc  _makeminios
                mov  al, 2
                mov  nonbootdisk, al
                jmp  _makebootable
_makeminios: 
                mov  bh, '/'
                mov  bl, 'M'
                call scancmdline
                jnc  _makebootable
                mov  al, 3
                mov  nonbootdisk, al
_makebootable:
     ret
main endp

copyfiles proc far
               _print transfering, 1

               push cs
               pop  es
               lea  si, kernel
               lea  di, kernelpath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, command_com
               lea  di, commandpath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, boot
               lea  di, bootpath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, syscom
               lea  di, syscompath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, writeb
               lea  di, writebcompath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, fdisk 
               lea  di, fdiskcompath
               call filecopy
               jc   systemfileerror

               push cs
               pop  es
               lea  si, _format
               lea  di, _formatcompath
               call filecopy
               jc   systemfileerror
               jmp  success
systemfileerror:
          call putnum
          _newline
          _print errormsg2, 1
          mov  ax, 2
          stc
          ret
missingkernel:
          _print errormsg, 1
          mov  ax, 3
          stc
          ret
success:
          call WaitForFDC     ; wait for volume

          mov  dl, _diskdrive
          call resetdisk

          mov  dl, _diskdrive
          call cs:wsector

          _print successful, 1
copyfilesx:
              ret
copyfiles endp

; ===========================================================================
wsector proc far
; Input : DL = Drive letter
; ===========================================================================
    mov  al, nonbootdisk
cmp3:
    cmp  al, 3
    jne  cmp2
    mov  bx, offset minios_bootcode
    jmp  short _nofreedos
cmp2:
    cmp  al, 2
    jne  cmp1
    mov  bx, offset simple_bootcode
    jmp  short _nofreedos
cmp1:
    cmp  al, 1
    jne  cmp0
    mov  bx, offset nonbootable_bootcode
    jmp  short _nofreedos
cmp0:
    mov  bx, offset boot144
_nofreedos:
    mov ah, 03h                                                    ; write!
    mov al, 1                                                      ; sector
    mov cx, 1                                     ; ch = track, cl = sector
    mov dx, 0                                       ; dh = head, dl = drive
    ; lea    bx, boot144
    int 13h
    ret
wsector endp

; ===========================================================================
;loadboot proc far
; ===========================================================================
;            _fopen boot, 0, 0, 2
;            jc     _lbberr
;            mov    handle, ax
;            _fread handle, SYSBUFFER, 512
;            _fclose handle
;            clc
;            jmp _lbbx
;_lbberr:    stc
;_lbbx:      ret
;loadboot endp

; ===========================================================================
;loadboot2 proc far
; ===========================================================================
;          mov  ax, 3d00h
;          mov  dx, offset boot
;          int  21h
;          jc   _lb2err
;          mov  bx, ax
;          mov  ah, 3fh
;          mov  cx, 0200h
;          mov  dx, offset SYSBUFFER
;          int  21h
;          jnc  _lb2x
;_lb2err:  stc
;_lb2x:    ret
;loadboot2 endp

;loadkernel proc far
;            _fopen kernel, 0, 0, 2
;            jc     _lkberr
;            mov    handle, ax
;            _fread handle, SYSBUFFER+512, 20000
;            _fclose handle
;_lkberr:    stc
;_lkbx:      ret
;loadkernel endp

;          mov  al, dl
;          add  al, 41h
;          mov  byte ptr [kernelpath], al
;          _print kernelpath, 1
;          _fopen kernelpath, 1, 0, 2
;          jc     _ikerr
;          mov    handle, ax
;          _fwrite handle, SYSBUFFER+512, 20000
;          _fclose handle
;_ikerr:    stc

;    _dispword 200h+8   ; wsect
;    _dispbyte 200h+0ah ; bclusterpersect
;    _dispword 200h+0bh ; wressect
;    _dispbyte 200h+0dh ; bfatcount
;    _dispword 200h+0eh ; wrootentries
;    _dispword 200h+10h ; Wtotsects
;    _dispbyte 200h+12h ; bmedia

nonbootdisk   db 0
argc            dw ?
handle          dw ?
_diskdrive      db 0
missingparam    db 'Required parameter missing',0ffh,0
help            db 'Copies NX-DOS system files and command interpreter to disk specified.',0ffh
                db 0ffh,'SYS [Drive1:][Path] drive2:',0ffh
                db '[Drive1:][Path] Specifies location of the system files.',0ffh
                db 'Drive2:         Specifies the drive the files are to be copied to.',0ffh
                db '/N              Makes a non-bootable floppy.',0ffh,0
boot            db 'BOOT.BIN',0   
bootpath        db 'A:\BOOT.BIN',0
kernel          db 'NXBIO.SYS',0
kernelpath      db 'A:\NXBIO.SYS',0
command_com     db 'COMMAND.COM',0
commandpath     db 'A:\COMMAND.COM',0
syscom          db 'SYS.COM',0
syscompath      db 'A:\SYS.COM',0     
writeb          db 'WRITEB.COM',0     
writebcompath   db 'A:\WRITEB.COM',0     
_format          db '_format.COM',0     
_formatcompath   db 'A:\_format.COM',0     
fdisk           db 'FDISK.EXE',0     
fdiskcompath    db 'A:\FDISK.EXE',0     
errormsg        db 'System file(s) not found.',0ffh,0
errormsg2       db 'Error in transfering system.',0ffh,0
transfering     db 'Transfering system...',0

successful      db 'OK',0ffh,0

include         \nxdos\src\boot\bootcore.inc

SYSBUFFER       DB ?            ;uninitialized array to hold contents of file

code            ends

ProgramEnd      EQU $-01h

		end	start
