mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 20:00:16 +08:00
186 lines
6.5 KiB
Bash
Executable File
186 lines
6.5 KiB
Bash
Executable File
#!/bin/sh
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
# dosbox [DOSBox arguments]: DOSBox 0.74-3 on /dev/fb0 (the host display:
|
|
# the page, or the CLI's --display), in the mode that each DOS video mode
|
|
# needs; installed as /usr/bin/dosbox.
|
|
#
|
|
# The first run downloads dosbox-riscv64.tar.gz from $DOSBOX_BASE_URL
|
|
# (DOSBox built as a static riscv64 musl binary with SDL 1.2's fbcon
|
|
# driver, kernel/guest/dosbox-build/build.sh), checks its sha256 and unpacks
|
|
# it; later runs start at once. A DOSBox built in the guest by dosbox-build
|
|
# (kernel/guest/dosbox-build/guest-build.sh) is used instead, without a
|
|
# download, while DIR/.built marks it. Everything lives in /data/dosbox when the
|
|
# persistent data disk is mounted (kept across boots, safe mode, overlay
|
|
# resets and image updates), else in /var/lib/dosbox (gone at power off);
|
|
# DOSBOX_DIR overrides the place:
|
|
# DIR/dosbox/ the program, its license and patches
|
|
# DIR/unkernel.conf settings for this system (rewritten at every start)
|
|
# DIR/dosbox.conf your settings, read after it (written once)
|
|
# DIR/c/ drive C: (mounted when no arguments are given)
|
|
# `dosbox --remove` deletes DIR, drive C: included.
|
|
#
|
|
# There is no x86 recompiler for riscv64: DOSBox runs its normal core, an
|
|
# interpreter, on top of the guest CPU emulator, so expect early-PC speeds.
|
|
# Sound (Sound Blaster 16, AdLib, PC speaker) goes to /dev/dsp; it costs
|
|
# time too (nosound=true in dosbox.conf). Keys come from an evdev keyboard
|
|
# (/dev/input/event*) if there is one, else from the console: a terminal
|
|
# reports no key releases, so a key counts as held for 0.25 s; Ctrl, Alt and
|
|
# Shift come with the character (Ctrl-A, Alt via ESC, capitals). Ctrl-C ends
|
|
# DOSBox; `exit` at the DOS prompt too.
|
|
|
|
# Where dosbox-riscv64.tar.gz is published. Placeholder: not published yet.
|
|
DOSBOX_BASE_URL=${DOSBOX_BASE_URL:-https://github.com/pithings/unkernel/releases/download/guest-bin}
|
|
# A copy of the package shipped in the image is used instead of a download.
|
|
pkg_dir=/usr/share/unkernel
|
|
bin_file=dosbox-riscv64.tar.gz
|
|
bin_sha256=661d723876b74e11efc0eeaf64515b8140adfb7f57697ba624356890104b27a6
|
|
need_kib=8192 # 1 MiB download + 2.4 MiB program, with some room
|
|
|
|
die() {
|
|
echo "dosbox: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
if [ -n "$DOSBOX_DIR" ]; then
|
|
dir=$DOSBOX_DIR
|
|
keep=
|
|
elif mountpoint -q /data 2>/dev/null && [ -w /data ]; then
|
|
dir=/data/dosbox
|
|
keep=
|
|
else
|
|
dir=/var/lib/dosbox
|
|
keep=" (no data disk: kept only until power off)"
|
|
fi
|
|
|
|
if [ "$1" = --remove ]; then
|
|
rm -rf "$dir" && echo "dosbox: removed $dir"
|
|
exit
|
|
fi
|
|
|
|
[ -c /dev/fb0 ] || die "no /dev/fb0: the host gives this system no display (CLI: --display)"
|
|
|
|
if [ -f "$dir/.built" ] && [ -x "$dir/dosbox/dosbox" ]; then
|
|
: # built here by dosbox-build (dosbox-build/guest-build.sh): use it
|
|
elif [ "$(cat "$dir/.installed" 2>/dev/null)" != "$bin_sha256" ] ||
|
|
[ ! -x "$dir/dosbox/dosbox" ]; then
|
|
# First run (or this script now names another build): install.
|
|
mkdir -p "$dir" || die "cannot create $dir"
|
|
rm -f "$dir/.installed"
|
|
cd "$dir" || exit 1
|
|
rm -rf .part
|
|
mkdir .part || die "cannot write to $dir"
|
|
# Free KiB; a file system without a size (ramfs) is not checked.
|
|
avail=$(df -Pk . | awk 'NR == 2 && $2 > 0 { print $4 }')
|
|
[ -n "$avail" ] && [ "$avail" -lt $need_kib ] &&
|
|
die "not enough space in $dir: $((avail / 1024)) MiB free, $((need_kib / 1024)) MiB needed"
|
|
case $DOSBOX_BASE_URL in
|
|
*.invalid/*) die "no download location for the DOSBox binary is set (DOSBOX_BASE_URL is a placeholder)" ;;
|
|
esac
|
|
echo "dosbox: installing into $dir$keep"
|
|
if [ -r "$pkg_dir/$bin_file" ]; then
|
|
url=$pkg_dir/$bin_file
|
|
cp "$url" .part/bin.tar.gz || die "cannot write to $dir"
|
|
else
|
|
ip route 2>/dev/null | grep -q '^default' ||
|
|
die "no network: the first run downloads DOSBox (about 1 MiB)"
|
|
url=$DOSBOX_BASE_URL/$bin_file
|
|
echo "dosbox: downloading $url"
|
|
wget -q -O .part/bin.tar.gz "$url" || {
|
|
rm -rf .part
|
|
die "download failed: $url (no network, or the package is not published there yet; DOSBOX_BASE_URL sets another place)"
|
|
}
|
|
fi
|
|
sum=$(sha256sum .part/bin.tar.gz | awk '{ print $1 }')
|
|
[ "$sum" = "$bin_sha256" ] || {
|
|
rm -rf .part
|
|
die "checksum mismatch for $url: expected $bin_sha256, got $sum"
|
|
}
|
|
mkdir .part/new
|
|
tar -xzf .part/bin.tar.gz -C .part/new || die "cannot unpack $bin_file (out of space?)"
|
|
[ -x .part/new/dosbox/dosbox ] || die "the downloaded file is not as expected"
|
|
rm -rf dosbox
|
|
mv .part/new/dosbox . || die "cannot write to $dir"
|
|
rm -rf .part
|
|
echo "$bin_sha256" > .installed
|
|
sync
|
|
fi
|
|
|
|
mkdir -p "$dir/c"
|
|
# unkernel.conf: this system's settings, rewritten at every start (only what
|
|
# differs from DOSBox's defaults); dosbox.conf, read after it, is yours.
|
|
cat > "$dir/unkernel.conf" <<'EOF'
|
|
# Written by /usr/bin/dosbox at every start; put your changes in dosbox.conf.
|
|
[sdl]
|
|
# The framebuffer takes any size: SDL sets the one DOSBox asks for, and
|
|
# the display scales it to its window.
|
|
fullscreen=false
|
|
output=surface
|
|
# Key symbols, not scancodes: the scancode table assumes X11.
|
|
usescancodes=false
|
|
waitonerror=false
|
|
# Mouse motion always goes to DOS (no capture click needed); with this,
|
|
# DOSBox hides SDL's cursor at start (and the build never shows it).
|
|
autolock=false
|
|
|
|
[render]
|
|
# No scaler: 320x200 stays 320x200 (cheaper; the display scales it), and
|
|
# aspect=true stretches it to 320x240, as a 4:3 monitor showed it.
|
|
scaler=none
|
|
aspect=true
|
|
|
|
[cpu]
|
|
core=normal
|
|
cycles=auto
|
|
|
|
[mixer]
|
|
# /dev/dsp (OSS, through SDL); a lower rate costs less time.
|
|
rate=22050
|
|
blocksize=2048
|
|
prebuffer=40
|
|
|
|
[speaker]
|
|
pcrate=22050
|
|
tandy=off
|
|
disney=false
|
|
|
|
[sblaster]
|
|
sbtype=sb16
|
|
oplrate=22050
|
|
|
|
[gus]
|
|
gus=false
|
|
|
|
[midi]
|
|
mpu401=none
|
|
mididevice=none
|
|
|
|
[joystick]
|
|
joysticktype=none
|
|
|
|
[serial]
|
|
serial1=disabled
|
|
serial2=disabled
|
|
|
|
[dos]
|
|
keyboardlayout=none
|
|
EOF
|
|
# dosbox.conf from an earlier version of this script held everything; it is
|
|
# replaced once by the short one below.
|
|
if [ ! -f "$dir/dosbox.conf" ] || grep -q '^# DOSBox settings for the unkernel' "$dir/dosbox.conf"; then
|
|
cat > "$dir/dosbox.conf" <<'EOF'
|
|
# Your DOSBox settings, read after unkernel.conf (dosbox -printconf lists
|
|
# them all). For example, without sound (it costs time):
|
|
# [mixer]
|
|
# nosound=true
|
|
EOF
|
|
fi
|
|
|
|
[ $# -eq 0 ] && set -- -c "mount c $dir/c" -c "c:"
|
|
cd "$dir" || exit 1
|
|
# HOME: DOSBox keeps its own files in $HOME/.dosbox. SDL_NOMOUSE: SDL's
|
|
# fbcon finds no gpm or PS/2 mouse here (an evdev mouse still works).
|
|
# SDL_AUDIODRIVER=dsp: OSS /dev/dsp (without a sound card DOSBox runs silent).
|
|
HOME=$dir SDL_NOMOUSE=1 SDL_AUDIODRIVER=dsp exec ./dosbox/dosbox \
|
|
-conf "$dir/unkernel.conf" -conf "$dir/dosbox.conf" "$@"
|