                                                       /(
            /(____ ____________  _________   ___  ___ (__)  _____)\
           |      \|         \ \/  ___    `\|   \|   |/  \ /      |
           |   |   |   |___   .  ,  |   |   |    |   |    |   |   | pt/jp
       ___ |   |   :   __|__  :  |  |   |   :   ,    :    :   |___| ___
       )   |   |   .       |  |  |  |   '   .   |    .    .   |   |   (
    ... \  |   '  ____________|__|______________|\__________  `   |  / ...
    :    \ |     / % d e m o n i c  p r o d u c t i o n z % \     |//    :
    :....  |    / __________________________________________ \    |  ....:
        :. |__// .... .. . .. ... .. .  . .. ... .. . .. .... \___| .:
  
  ============================================================================
    XQTR // andr01d.zapto.org:999                              December 2022
  ============================================================================


  In this guide, we will see how to use QEMU to run DOS DOOR apps from our BBS,
  in a Linux/RPi machine. For this, we will need:
  
  QEMU                - sudo apt-get install qemu
  SOCAT               - sudo apt-get install socat
  BNU Fossil Driver   - https://www.pcmicro.com/bnu/
  FreeDOS             - https://www.freedos.org/
  
  You can get a ready made raw disk image, with FreeDOS, BNU and other files, 
  ready to use in Another Droid BBS, under the File section, but the complete
  details are also here.
  
  This guide is based on guides made by Gryphon, NuSkooler and various comments
  in the BBS scene. I tested and it works in my Ubuntu 20.20 machine, as well
  a RPi3B+. Depending your system or the date you are following this guide, 
  something may have changed, as i also found when i was following other guides.
  
  This guide is written in December 2022.
  
  When you see the this symbol >, it means that the following text, must be 
  written in a terminal, you don't write the > symbol!
  
  To follow this guide, we will assume, that our BBS is inside the folder:
  /home/bbs.
  

  FREEDOS IMAGE (virtual disk)
  ----------------------------
  
  In a terminal give:

  > qemu-img create -f raw freedos.img 100M
  
  It will create a 100MB virtual disk. I found that also a 50MB works. Now go to
  the FreeDOS site and download a version of it. After you have the .ISO file
  write this:
  
  > qemu -rtc base=localtime -hda freedos.img -cdrom FD12CD.iso -boot d
  
  This will boot the .ISO in the VM and start the FreeDOS installation. Follow
  the steps on the screen. It may need some reboots, as it will ask to partition
  and format the virtual disk.
  
  After the installation complete you can test it by running the VM with:
  
  > qemu-system-i386 \
  -rtc base=localtime \
  -boot c \
  -m 256 \
  -drive file=freedos.img,format=raw,index=0,media=disk 
  
  It should run and see the DOS prompt.
  
  
  COPY/EDIT FILES
  ---------------
  
  Now that you have a working FreeDOS installation, we have to copy some files
  and ready the fdconfig.sys and fdauto.bat files. To make things easier, you
  can mount the virtual disk in your Linux machine and copy/edit the 
  necessary files.
  
  As a root user, to make a mounting point, give:
  > mkdir -p /mnt/freedos
  
  ... to make the mount:
  > mount -t msdos -o loop,offset=32256 freedos.img /mnt/freedos
  
  ...when finished, to unmount the image, give:
  > umount /mnt/freedos
  
  Mount the image and copy all BNU files inside a \BNU folder
  
  Edit the FDCONFIG.SYS file, erase everything and copy/paste these:

=== START ====================================================================
  
SET DOSDIR=C:\FreeDOS
!COUNTRY=001,858,C:\FreeDOS\BIN\COUNTRY.SYS
!LASTDRIVE=Z
!BUFFERS=20
!FILES=40

DOS=HIGH
DOS=UMB
DOSDATA=UMB
DEVICE=C:\FreeDOS\BIN\HIMEMX.EXE
DEVICE=C:\FreeDOS\BIN\JEMM386.EXE X=TEST I=TEST I=B000-B7FF NOVME NOINVLPG
SHELLHIGH=C:\FreeDOS\BIN\COMMAND.COM C:\FreeDOS\BIN /E:1024 /P=C:\FDAUTO.BAT
  
=== END ======================================================================  

  ...edit the FDAUTO.BAT file, clear it and copy/paste the following:
  
=== START ====================================================================
  
@ECHO OFF

REM Standard AutoExec Batch File

set DOSDRV=C:
set DOSDIR=C:\FreeDOS
set LANG=EN
set TZ=UTC
set PATH=%dosdir%\BIN
if exist %dosdir%\LINKS\NUL set PATH=%path%;%dosdir%\LINKS
set NLSPATH=%dosdir%\NLS
set HELPPATH=%dosdir%\HELP
set TEMP=%dosdir%\TEMP
set TMP=%TEMP%

set DIRCMD=/OGN /Y
set COPYCMD=/-Y
set OS_NAME=FreeDOS
set OS_VERSION=1.3

FDAPM APMDOS
LH SHARE
MEM /C /N
SET AUTOFILE=%0
SET CFGFILE=C:\FDCONFIG.SYS

alias cfg=edit %cfgfile%
alias auto=edit %autofile%
alias reboot=fdapm warmboot
alias reset=fdisk /reboot
alias halt=fdapm poweroff
alias shutdown=fdapm poweroff

C:\BNU\BNU.COM /L0:57600,8N1 /F

CALL D:\RUN.BAT
  
=== END ====================================================================== 

  The image is ready. Unmount it and make a backup if you want. Place this image
  inside your BBS folder, under a folder named \QEMU. You now have this folder:
  
  /home/bbs/qemu
  
  Also, make a nodes folder, inside the qemu folder and inside that some temp#
  folders, where # is the number of nodes you have. the commands are:
  
  > mkdir qemu
  > cd qemu
  > mkdir nodes
  > cd nodes
  > mkdir temp{1..9}
  
  You shoud have this directory structure:
  
  /home/bbs/qemu
                 +/nodes
                    +/temp1
                    +/temp2
                    +/temp3
                    ...
                    +/temp8
                    +/temp9
                    
  Also, make a DOORS folder, where to put all of your DOOR apps/games. The
  following dir has to be present:
  
  /home/bbs/doors
  
  Test that the image is running with no errors, with the following command:
  > qemu-system-i386 \
  -rtc base=localtime \
  -boot c \
  -m 256 \
  -drive file=freedos.img,format=raw,index=0,media=disk 
  
  You will see an error, that the file RUN.BAT is missing, but this is OK. Also
  check that changing drive to D:, you see the DOOR games/apps and giving E:
  you see the temp folders we created. Lets conclude this guide, with the final
  and most important step...
  
  
  RUNDOOR.SH - Run the whole thing!
  ---------------------------------
  
  To run a DOOR, within the BBS, we must create a BASH script, that will execute
  QEMU and some other essential commands. Create the file "rundoor.sh" inside
  the folder: /home/bbs/qemu
  
  Copy and paste the following script:
  
=== START ====================================================================

#!/bin/sh

## Pass the parameters for the door and the node, from the BBS
DOOR=${1}
NODE=${2}
BBS=/home/x/mys47b
QEMUP=$BBS/qemu
DOOR=`echo ${DOOR} | tr '[a-z]' '[A-Z]'`

## The linux path for the run.bat file
gobat=/home/bbs/qemu/nodes/run.bat

# The VM will be accessable via a computer port.  I have randomly selected
# 20200 as a base port number to use.  Each node will 'add' to the base 
# number.  IE, Node #1 will be added to 20200 and the result will be
# port 20201 (20200 + 1)
# You may change the base port to any number you wish.
portnum=`expr 20200 + ${NODE}`

# Add an entry for each DOOR you want to launch. Each case label is a "short
# code" for the DOOR, that you will also pass from the BBS

case ${DOOR} in
	## Operation OverKill II
	OOII) 
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "CD \DOORS\OO" >> ${gobat}
		### Use the CALL command when calling a door batch file.
		### If not used, then the door batch file will not return
		### to the RUN.BAT script to complete execution.  If the 
		### RUN.BAT script does not complete execution, then it will
		### not shut down the VM.  It will then just go to a dos
		### prompt and the user will not see it, and will think that
		### the game is hung.
		echo "CALL OOIIDOOR.BAT ${NODE}" >> ${gobat}
		;;
	## Legend of the Red Dragon
	LORD)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "CD \DOORS\LORD" >> ${gobat}
		echo "CALL START.BAT ${NODE}" >> ${gobat}
		;;
		## Planets: The Exploration of Space
	PLAN)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "CD \DOORS\PLAN" >> ${gobat}
		echo "PLANETS.EXE /pD:\TEMP${NODE}" >> ${gobat}
		;;
	## Barren Realms Elete
	BRE)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "CD \DOORS\BRE" >> ${gobat}
		echo "SRDOOR" >> ${gobat}
		echo "BRE" >> ${gobat}
		;;
	# Space Dynasty
	DYNASTY)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "CD \DOORS\DYNASTY" >> ${gobat}
		echo "DYNASTY D:\TEMP${NODE}\DOOR.SYS" >> ${gobat}
		echo "COPY \\DOORS\\DYNASTY\\DYNASTY\\DYNASTY.SCO \\TEXT\\" >> ${gobat}
		;;
	# NetRunner
	NETRUN)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "D:\DOORS\bnu\bnu" >> ${gobat}
		echo "CD \DOORS\NETRUN" >> ${gobat}
		echo "NETRUN D:\TEMP{NODE}\DOOR.SYS" >> ${gobat}
		;;
  DARK)
		echo "@echo off" > ${gobat}
		echo "D:" >> ${gobat}
		echo "cd D:\DARKNESS" >> ${gobat}
		echo "DARK16 /N$NODE /de:\TEMP$NODE\DOOR.SYS " >> ${gobat}
		;;
	DOS)
;;
esac
# At the end of run.bat, we add this line to gracefully shutdown the VM.
# This will return the user back to the BBS.
echo "cls" >> ${gobat}
echo "echo Shutting down procedure. Please Wait..." >> ${gobat}
echo "C:\FreeDOS\BIN\FDAPM POWEROFF" >> ${gobat}

# Now that we set the RUN.BAT file, we copy the drop files, from the BBS, into
# our virtual E: drive for easier access to the DOORs
cp /home/bbs/temp$NODE/* /home/bbs/qemu/nodes/temp$NODE

# Ready to launch QEMU. We create a telnet/serial connection, which will be 
# mirrored to the user terminal

qemu-system-i386 \
-rtc base=localtime \
-boot c \
-m 256 \
-serial telnet::${portnum},server,nowait,nodelay \
-drive file=/home/bbs/qemu/freedos.img,format=raw,index=0,media=disk \
-drive file=fat:rw:/home/bbs/doors,format=raw,index=1 \
-drive file=fat:rw:/home/bbs/qemu/nodes,format=raw,index=2 &

#-display none \        # No QEMU window

### Command line explanation
#-rtc base=localtime \  # set time to VM, to be the same as the local machine
#-boot c \              # boot from the C: drive
#-m 256 \               # 256MB memory for the VM
#-serial telnet::${portnum},server,nowait,nodelay \   # Serial connection
#-drive file=/home/bbs/qemu/freedos.img,format=raw,index=0,media=disk \   # C: Drive
#-drive file=fat:rw:/home/bbs/doors,format=raw,index=1 \                  # D: Drive
#-drive file=fat:rw:/home/bbs/qemu/nodes,format=raw,index=2 &             # E: Drive

### Inform the user that it will take several seconds before the game
### loads up.
clear
echo
echo "[33mLoading door... please be patient...."
echo
sleep 5  # Give time for QEMU to load
# socat is used to communicate with the VM's serial modem.
socat file:`tty`,raw,echo=0 TCP:localhost:${portnum}

if [ -f ${gobat} ]; then
        rm -rf ${gobat}   # remove the run.bat file
        rm /home/bbs/qemu/nodes/temp$NODE/*  # clear the temp# dir.
fi

exit 0


=== END ======================================================================

  Make sure that the script is executable or do it with: chmod +x rundoor.sh
  
  
  MYSTIC BBS - DOOR SETUP
  -----------------------
  
  Now, to be able to launch the DOOR from your BBS, you want to add a custom
  entry for the new DOOR, into your BBS. Lets take Mystic BBS for example.
  
  Go into the Mystic Configuration editor, edit the main theme and open the 
  DOORS menu. Add a new entry and edit the text to what you like. The important
  stuff is the command in the Action List. Press / and insert a new Command, 
  now edit it.
  
  Set Command to DD, Exec external program. Set Data to something similar with
  this:

  /home/bbs/qemu/rundoor.sh DARK %3 /DOS
  
  Lets explain it a bit more. /home/bbs/qemu is the folder where you put the 
  rundoor.sh script from above.
  
  - %3, is a mystic mci code for the Node number.
  - DARK, is a shortcode/parameter for the rundoor.sh script to launch the 
    specific DOOR (aka Darkness).
  - /DOS, tells Mystic to create the drop files with DOS line endings CRLF and
    not perhaps Linux.
  
  Save the command option and go test to see if everything is OK. You should
  be able to run any DOS DOOR now!




         _____         _   _              ____          _   _ 
        |  _  |___ ___| |_| |_ ___ ___   |    \ ___ ___|_|_| |        8888
        |     |   | . |  _|   | -_|  _|  |  |  |  _| . | | . |     8 888888 8
        |__|__|_|_|___|_| |_|_|___|_|    |____/|_| |___|_|___|     8888888888
                                                                   8888888888
                DoNt Be aNoTHeR DrOiD fOR tHe SySteM               88 8888 88
                                                                   8888888888
 /: HaM RaDiO   /: ANSi ARt!     /: MySTiC MoDS   /: DooRS         '88||||88'
 /: NeWS        /: WeATheR       /: FiLEs         /: zer0net        ''8888"'
 /: GaMeS       /: TeXtFiLeS     /: PrEPardNeSS   /: FsxNet            88
 /: TuTors      /: bOOkS/PdFs    /: SuRVaViLiSM   /:            8 8 88888888888
                                                              888 8888][][][888
   TeLNeT : andr01d.zapto.org:9999 / ssh: 8888                  8 888888##88888
   SySoP  : xqtr                   eMAiL: xqtr@gmx.com          8 8888.####.888
   DoNaTe : https://paypal.me/xqtr                              8 8888##88##888

