How to install SunOS 4.1.4 in QEMU
----------------------------------

1) Download QEMU 7.0.0, or clone it from gitlab

2) Apply the patches to hw/scsi/scsi-disk.c, hw/scsi/esp.c and
   hw/sparc/sun4m.c provided here. These patches do the following:

esp.c:
       Fixes two things:

       1) The do_command_phase() function does allocates a "buf"
       byte array on the stack, but does not initialize/zero it
       before populating it with data from the command FIFO.
       The result is that sometimes stack garbage will be
       incorrectly seen as command data. This eventually causes
       data overrun errors in SunOS. Note that this may not be
       immediately evident if you compile QEMU with GCC, but it
       seems to show up right away if you build with LLVM/Clang
       instead.

       2) When the guest writes a CMD_BUSRESET command to the
       command register, the esp emulator doesn't actually
       reset the devices on the bus. This messes up SunOS's
       prope logic such that it thinks the targets are
       "non-CCS" devices. You can get away with this for disk
       devices, but it causes CD-ROMs to be ignored. This is
       what ultimately leads to the initial MUNIX installation
       environment failing to install the miniroot with a
       strange "ERROR: unknown loadfrom:" message.

scsi-disk.c:
       Adds support for setting the logical block size of SCSI
       CD-ROM devices. Normally QEMU hard-codes the block size
       for SCSI CD-ROMs to 2048 bytes. This complies with the
       spec, but due to hysterical raisins, SunOS 4 only works
       with CD-ROM drives that have been configured for a block
       size of 512 bytes. With this patch in place, the default
       is still 2048 bytes, but can now be overridden from the
       command line (which is what the supplied qemu-sparc.sh
       script does).

sun4m.c:
       QEMU tries to create a dummy SUNW,dbri device, which on
       actual hardware provides several functions, including
       audio support. However the fake device created by QEMU
       has no FCode (it creates a PROM region that's empty).
       This causes the SPARCstation 20 OpenProm firmware to
       create a node with an empty "name" property, which in
       turn causes the SunOS 4.1.4 kernel to crash if you try
       to use -machine SS-20 with QEMU.

       The supplied patch changes the way the dummy SUNW,dbri
       device is created, and assigns it a dummy FCode which sets
       the name correctly.

3) Build and install QEMU

4) Create a virtual QCOW2 disk to use as the SunOS install and boot
   SCSI drive

% qemu-img create -f qcow2 -o compat=1.1 sunos414.img 2G

5) Download the solaris112.iso.gz file here and uncompress it with
   gzip. This is an image of the Solaris 1.1.2/SunOS 4.1.4 install CD.
   (I created it myself from my own original physical copy.)

6) Also download the ss20_v2.25_rom OpenProm image provided here.

7) Copy the QEMU,dbri.bin file into the "share" directory for QEMU,
   where all its bios and firmware files live. You must have this file
   in place in order to boot a virtual SPARCstation 20 system.

8) Run the provided qemu_sparc.sh script to launch QEMU. It assumes
   that the qemu-system-sparc executable is in your path.

   This script creates a virtual SPARCstation 20 machine with 512MB of
   RAM and a SCSI hard disk at target 3 and a SCSI CD-ROM device at
   target 6. The CD-ROM device is set to use the provided solaris112.iso
   image as the media.

9) You should get to the "ok" prompt in the SS20 OpenProm. It may be
   helpful to turn off the diagnostic setting:

ok setenv diag-switch? false

Then boot the install CD:

ok boot cdrom

10) This should boot the SunOS kernel and run the install script. You'll
    see the following prompt:

What would you like to do?
  1 - install SunOS mini-root
  2 - exit to single user shell
Enter a 1 or 2:

Select 1 here.

11) You'll now see this prompt:


Beginning system installation - probing for disks.
installing miniroot on disk "sd0", (only disk found).
Do you want to format and/or label disk "sd0"?
  1 - yes, run format
  2 - no, continue with loading miniroot
  3 - no, exit to single user shell
Enter a 1, 2, or 3:

Again, select 1 here.

12) At the format> prompt, select the sd0 disk, and label it as
    a SUN2.1G disk:

Searching for disks...done



FORMAT MENU:
        disk       - select a disk
        type       - select (define) a disk type
        partition  - select (define) a partition table
        current    - describe the current disk
        format     - format and analyze the disk
        repair     - repair a defective sector
        show       - translate a disk address
        label      - write label to the disk
        analyze    - surface analysis
        defect     - defect list management
        backup     - search for backup labels
        quit
format> disk


AVAILABLE DISK SELECTIONS:
        0. sd0 at esp0 slave 24
           sd0: <drive type unknown>
Specify disk (enter its number) [0]: 0



AVAILABLE DRIVE TYPES:
        0. Quantum ProDrive 80S
        1. Quantum ProDrive 105S
        2. CDC Wren IV 94171-344
        3. SUN0104
        4. SUN0207
        5. SUN0320
        6. SUN0327
        7. SUN0424
        8. SUN0535
        9. SUN0669
        10. SUN1.0G
        11. SUN1.05
        12. SUN1.3G
        13. SUN2.1G
        14. other
Specify disk type (enter its number): 13
selecting sd0: <SUN2.1G>
[disk formatted, no defect list found]
format> label
Ready to label disk, continue? y

format>

13) Now quit the format utility. When you do, you'll see output like
    this:

format> quit
checking writeability of /dev/rsd0b
0+1 records in
1+0 records out
Extracting miniroot ...
using cdrom partition number 3
|

Mini-root installation complete.

What would you like to do?
  1 - reboot using the just-installed miniroot
  2 - exit into single user shell
Enter a 1 or 2:

14) Select 1, and you should see something like this:

Enter a 1 or 2: 1
rebooting from: /iommu@f,e0000000/sbus@f,e0001000/espdma@f,400000/esp@f,800000/sd@3,0:b -sw
syncing file systems... done
rebooting...
Resetting ... 

15) This will boot the miniroot image and should leave you at a
    root shell prompt. From here, run suninstall:

# suninstall

16) The suninstall script will guide you through installing SunOS
    on the hard disk. It's recommended to use the "Quick
    installation" path initially, and select the "Full_install"
    option. This will load all the available software onto the
    disk.

17) Once the installation finishes, you will be prompted to reboot
    the system once more. At this point, you can boot from the
    root filesystem on the sd0 disk. If the system doesn't restart
    automatically, you can start it up manually as follows:

ok boot disk

18) You should now be guided through some final configuration steps,
    such as setting a hostname and IP address for the machine. You
    will also be prompted to set a root password and create an initial
    non-root user. Once this is complete, the system will finish
    booting to the login prompt, and you should now have a working
    SunOS 4.1.4 installation.
