Plan 9 from Bell Labs’s /sources/contrib/miller/build-9pi.rc

Copyright © 2026 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


ISO=/tmp/plan9.iso
SDIMG=/tmp/9pi.img
SDBLOCKS=7743330   # for a 4GB SD card
#SDBLOCKS=3855600  # for a 2GB SD card

SDCYLS=`{hoc -e $SDBLOCKS/255/63}

test -f /srv/9660 || 9660srv
mount /srv/9660 /n/cd $ISO

# partition the SD card image

dd -if /dev/zero -of $SDIMG -bs 512 -count 1
aux/disksim -f $SDIMG
echo geometry $SDBLOCKS 512 $SDCYLS 255 63 >/dev/sdXX/ctl
disk/mbr /dev/sdXX/data
{echo a p1 0 8; echo t p1 FAT32; echo a p2 8 '$'; echo w} | disk/fdisk -b /dev/sdXX/data
{echo a nvram 2551 2552; echo a fossil 2552 '$'; echo w} | disk/prep -b /dev/sdXX/plan9

# format and populate the dos partition for booting all raspberry pi models

disk/format -b /386/pbs -df /dev/sdXX/dos
rm -f /srv/dos.rpi
dossrv -f/dev/sdXX/dos dos.rpi
mount -c /srv/dos.rpi /n/dos
cd /n/dos

mkdir overlays
RPI=(LICENCE.broadcom bcm2711-rpi-4-b.dtb bcm2711-rpi-400.dtb bcm2712-rpi-5-b.dtb bootcode.bin fixup.dat fixup4.dat fixup4cd.dat fixup_cd.dat start.elf start4.elf start4cd.elf start_cd.elf overlays/vc4-kms-v3d-pi5.dtbo)
for (i in $RPI) {
	echo $i
	hget https://github.com/raspberrypi/firmware/raw/stable/boot/$i >x && mv x $i
}

echo >cmdline-demo.txt 'readparts=1 nobootprompt=local user=glenda'
echo >cmdline-term.txt 'readparts=1 nvram=#S/sdM0/nvram ipconfig='
echo >cmdline-cpu.txt 'readparts=1 nvram=#S/sdM0/nvram bootargs=local!#S/sdM0/fossil'
echo >cmdline-wifiroot.txt 'ether1=type=4330 essid=WHATEVER wificrypt=wpa2 fs=N.N.N.N auth=N.N.N.N'
cp cmdline-demo.txt cmdline.txt

cat >config.txt <<XXX
[pi0]
kernel=9pi

[pi02]
kernel=9pi2
core_freq=250

[pi1]
# in older firmware Pi Zero W used this entry, not [pi0]
kernel=9pi

[pi2]
kernel=9pi2

[pi3]
kernel=9pi2
core_freq=250

[pi4]
kernel=9pi4
core_freq=250
device_tree=
enable_gic=1

[pi5]
kernel=9pi5
arm_boost=1
enable_uart=1
enable_rp1_uart=1
pciex4_reset=0
dtoverlay=vc4-kms-v3d-pi5

[all]
gpu_mem=16

# For more options and information see
# http://www.raspberrypi.org/documentation/configuration/config-txt.md
XXX

cat >LICENCE <<XXX
LICENCE.broadcom
	applies to these binary files in the FAT partition:
		bootcode.bin, fixup*.dat, and start*.elf

LICENCE.plan9
	applies to binary files 9pi* in the FAT partition
	and to the contents of the Plan 9 partition(s)
XXX

cp /n/cd/LICENSE LICENCE.plan9
###cp /n/cd/arm/^(9pi 9pi2 9pi4) .

# format the fossil partition

fossil/flfmt -y /dev/sdXX/fossil
fossil/conf -w /dev/sdXX/fossil <<XXX
fsys main config
fsys main open -Va
fsys main
XXX

fossil/fossil -f/dev/sdXX/fossil -c 'srv -p fscons.rpi' -c 'srv -AWP fossil.rpi'
echo uname upas :upas >>/srv/fscons.rpi
echo uname $user :$user >>/srv/fscons.rpi
mount -c /srv/fossil.rpi /n/fossil.rpi
cd /n/fossil.rpi

# copy the Plan 9 tree from cd to fossil, without 386 binaries

for (d in 386) {
	bind /n/cd/mips/bin /n/cd/$d/bin
	bind /n/cd/mips/lib /n/cd/$d/lib
	bind /n/cd/acme/bin/mips /n/cd/acme/bin/$d
	for (f in /n/cd/$d/9*) bind /dev/null $f
}
disk/mkfs -a -s /n/cd /n/cd/dist/replica/plan9.proto | disk/mkext -ud .
echo users -w >>/srv/fscons.rpi
echo fsys main create /active/usr/glenda/bin/arm glenda glenda d775 >>/srv/fscons.rpi
for (d in 386) rm $d/9*
rm -f 9load bootdisk.img pbsraw README

# adjust some config files for convenience of rpi users

ape/patch -p1 <<'XXX'
--- /n/sources/usr/glenda/bin/rc/riostart	Tue Apr 16 17:23:12 2002
+++ /usr/glenda/bin/rc/riostart	Thu Jun 24 20:01:42 2021
@@ -6,6 +6,10 @@
 
 window 0,0,161,117 stats -lmisce
 window 161,0,560,117 faces -i
+window -scroll -r 560 0 $wid 117 '
+	label console
+	echo ''Console output (/dev/kprint):''
+	cat /dev/kprint'
 
 if(~ `{screensize} small)
 	dump=acme.dump.small
--- /n/sources/usr/glenda/lib/profile	Fri Jan  7 20:11:03 2005
+++ /usr/glenda/lib/profile	Mon Apr  9 20:28:42 2018
@@ -6,6 +6,11 @@
 font = /lib/font/bit/pelm/euro.9.font
 upas/fs
 fn cd { builtin cd $* && awd }  # for acme
+
+# make vfp the default for ARM
+fn 5l { /$cputype/bin/5l -f $* }
+fn pcc { /$cputype/bin/pcc -f $* }
+
 switch($service){
 case terminal
 	plumber
--- /n/sources/sys/lib/newuser	Sat Apr 13 00:28:29 2013
+++ /sys/lib/newuser	Mon Apr  9 20:28:38 2018
@@ -21,6 +21,11 @@
 bind -a $x^home/bin/$x^cputype /bin
 bind -c $x^home/tmp /tmp
 font = /lib/font/bit/pelm/euro.9.font
+
+# make vfp the default for ARM
+fn 5l { /$x^cputype/bin/5l -f $x^* }
+fn pcc { /$x^cputype/bin/pcc -f $x^* }
+
 switch($x^service){
 case terminal
 	plumber
XXX

# to avoid 5-minute dhcp delay if not connected to a network,
# use the ipconfig env variable to control network initialisation: for example
#	ipconfig=        (starts ipconfig with no arguments, to use dhcp)
#	ipconfig=-N      (get network parameters by searching ethernet address in /lib/ndb)
#	ipconfig='-g N.N.N.N ether /net/ether0 N.N.N.N'   (set local and gateway addresses)
# if ipconfig env variable is undefined, don't run ipconfig automatically from termrc

ape/patch -p1 <<'XXX'
--- /n/sources/rc/bin/termrc.local	Fri Nov  8 22:56:06 2013
+++ /rc/bin/termrc.local	Mon Jun 23 20:02:37 2014
@@ -11,3 +11,12 @@
 facedom=FACEDOM
 # replace CPU with the name of your cpu server
 cpu=CPU
+# start network if ipconfig variable is set
+if(test ! -e /net/ipifc/0/ctl -a -e /env/ipconfig) {
+	echo -n ipconfig...
+	eval ip/ipconfig $ipconfig
+	echo
+}
+TIMESYNCARGS=''
+if(test -e /net/ipifc/0/ctl)
+	TIMESYNCARGS=(-n pool.ntp.org)
XXX

# apply selected 9legacy patches

# essential for raspberry pi
p=(bcm-20210411 bcm-brian-man bcm-20221222-fixes bcm-lpae bcm-uartpl011 libc-arm-atom usb-ether-rpi usb-ether-cdc usb-ether-lan78xx usb-usbd-usb3 usb-usb3)
# corrections to 4e bugs
p=($p ramfs-fixes ramfs-wstat-qid ramfs-open ramfs-refcount usb-serial-ftdi-writelen 9-segment-imagereclaim 9-segment-isoverlap 9-proc-rebalance 9-proc-procflushseg 9-proc-pexit 9-taslock-unlock 9-sysproc-close-on-exec 9-pread-offset fossil-wstat-qid fossil-qidspace-dirty fossil-time-backward fossil-iostate-deadlock man-2-ainc libc-atom-dmp 9-confinit-page 9-tcp-mss 9-screenputc pc-i2h)
# interoperability with modern internet
p=($p aes-ctr ssh2-aes-ctr ssh2-dh-group14 aux-wpa factotum-wpa tls-tlshand12 libsec-pbkdf2 libsec-tlshand12-norc4 libsec-tlshand12-nossl3 libsec-tlshand12-fixes libsec-tlshand-sigalgs libsec-x509-sha2 libsec-x509-sig tls-devtls12)
# ssh fixes
p=($p ssh2-factotum ssh2-publickey ssh2-status ssh2-sshsession-output)
# support for golang
p=($p 9-tcp-close ca 9-fpnote)

for (i in $p) {
	echo $i:
	hget http://9legacy.org/9legacy/patch/$i.diff | ape/patch -p1 --no-backup-if-mismatch
}

# this fossil bug fix needs altering because fossil-libventi isn't applied on 9pi
hget http://9legacy.org/9legacy/patch/fossil-deadlocks.diff |
	sed 's/canqlock\(&/vtCanLock(/;s/qlock\(&/vtLock(/;s/qunlock\(&/vtUnlock(/;s/%r/%R/' |
	ape/patch -p1 --no-backup-if-mismatch

# compatibility with 9legacy patch 9-ainc
ape/patch -p1 <<'XXX'
--- /n/sources/sys/src/9/bcm/archbcm.c	Fri Dec 24 11:41:23 2021
+++ /sys/src/9/bcm/archbcm.c	Fri Dec 24 11:56:29 2021
@@ -138,6 +138,27 @@
  */
 
 long
+_xdec(long *p)
+{
+	int s, v;
+
+	s = splhi();
+	v = --*p;
+	splx(s);
+	return v;
+}
+
+void
+_xinc(long *p)
+{
+	int s;
+
+	s = splhi();
+	++*p;
+	splx(s);
+}
+
+long
 ainc(long *p)
 {
 	int s, v;
XXX

# fix ramfs-refcount patch

ape/patch -p1 <<XXX
--- a/sys/src/cmd/ramfs.c	Wed Apr 22 10:31:49 2026
+++ b/sys/src/cmd/ramfs.c	Tue Dec 16 20:35:13 2025
@@ -493,6 +494,7 @@
 	r->atime = time(0);
 	r->mtime = r->atime;
 	f->ram->mtime = r->atime;
+	f->ram->ref--;
 	f->ram = r;
 	rhdr.qid = r->qid;
 	rhdr.iounit = messagesize-IOHDRSZ;
XXX

# install riscv + riscv64 toolchain & libs

du /arm | sed 's/.*\/arm(.*)/fsys main create \/active\/riscv\1 sys sys d775\nfsys main create \/active\/riscv64\1 sys sys d775/g'|sort >>/srv/fscons.rpi
hget http://9legacy.org/9legacy/patch/riscv-20210528.diff >/tmp/riscv-20210528.diff
ape/patch -p0 <<XXX
--- /tmp/riscv-20210528.diff	Tue Apr 21 08:14:13 2026
+++ /tmp/riscv-20210528.diff	Tue Apr 21 19:06:44 2026
@@ -21238,11 +21238,11 @@
  	kobj\
  	sparc64obj\
 @@ -40,6 +43,7 @@ FILES=\
+	alphaobj\
  	8obj\
  	9obj\
  	qobj\
 +	iobj\
- 	4obj\
  	vcodas\
  
 diff --git a/sys/src/libmach/obj.c b/sys/src/libmach/obj.c
@@ -21268,12 +21268,12 @@
  typedef struct Obj	Obj;
  typedef struct Symtab	Symtab;
 @@ -65,6 +67,8 @@ static Obj	obj[] =
+	[ObjPower]	"power .q",	_isq, _readq,
  	[ObjMips]	"mips .v",	_isv, _readv,
  	[ObjSparc64]	"sparc64 .u",	_isu, _readu,
  	[ObjPower64]	"power64 .9",	_is9, _read9,
 +	[ObjRiscv]	"riscv .i",	_isi, _readi,
 +	[ObjRiscv64]	"riscv64 .j",	_isi, _readi,
- 	[ObjMips2]	"mips64 .4",	_is4, _read4,
  	[Maxobjtype]	0, 0
  };
 diff --git a/sys/src/libmach/setmach.c b/sys/src/libmach/setmach.c
@@ -21284,9 +21284,9 @@
  	Machdata	*machdata;		/* machine functions */
  };
  
--extern	Mach		mmips, mmips64, msparc, m68020, mi386, mamd64,
-+extern	Mach		mmips, mmips64, msparc, m68020, mi386, mamd64, mriscv, mriscv64,
- 			marm, /* mmips2be, mmips2le, */ mpower, mpower64, malpha, msparc64;
+-extern	Mach		mmips, msparc, m68020, mi386, mamd64,
++extern	Mach		mmips, msparc, m68020, mi386, mamd64, mriscv, mriscv64,
+			marm, mmips2be, mmips2le, mpower, mpower64, malpha, msparc64;
 -extern	Machdata	mipsmach, mipsmachle, sparcmach, m68020mach, i386mach,
 +extern	Machdata	mipsmach, mipsmachle, sparcmach, m68020mach, i386mach, riscvmach, riscv64mach,
  			armmach, mipsmach2le, powermach, alphamach, sparc64mach;
@@ -21455,10 +21455,10 @@
  # common mkfile parameters shared by all architectures
  #
  
--OS=568qv
--CPUS=arm amd64 386 power mips
-+OS=568ijqv
-+CPUS=arm amd64 386 riscv riscv64 power mips
+-OS=58qv
+-CPUS=arm 386 power mips
++OS=58ijqv
++CPUS=arm 386 riscv riscv64 power mips
  CFLAGS=-FTVw
  LEX=lex
  YACC=yacc
XXX

for (i in pcc-mips64) {
	echo $i:
	hget http://9legacy.org/9legacy/patch/$i.diff | ape/patch -p1
}
echo riscv-20210528:
ape/patch -p1 </tmp/riscv-20210528.diff
for (i in riscv-ainc riscv-update-2022 dist-replica-proto-riscv) {
	echo $i:
	hget http://9legacy.org/9legacy/patch/$i.diff | ape/patch -p1
}

# install arm64 toolchain & libs

for (i in 7a 7c 7l) rm sys/src/cmd/$i/*

du /arm | sed 's/.*\/arm(.*)/fsys main create \/active\/arm64\1 sys sys d775/g'|sort >>/srv/fscons.rpi
for (i in arm64-toolchain arm64-libs) {
	echo $i:
	hget http://9legacy.org/9legacy/patch/$i.diff >/tmp/$i.diff
}
ape/patch -p0 <<XXX
--- /tmp/arm64-toolchain.diff	Sat Mar 14 16:45:38 2026
+++ /tmp/arm64-toolchain.diff	Tue Apr 21 20:01:17 2026
@@ -25798,9 +25798,9 @@
 @@ -17,9 +17,9 @@
  };
  
- extern	Mach		mmips, mmips64, msparc, m68020, mi386, mamd64, mriscv, mriscv64,
--			marm, /* mmips2be, mmips2le, */ mpower, mpower64, malpha, msparc64;
-+			marm, marm64, /* mmips2be, mmips2le, */ mpower, mpower64, malpha, msparc64;
+ extern	Mach		mmips, msparc, m68020, mi386, mamd64, mriscv, mriscv64,
+-			marm, mmips2be, mmips2le, mpower, mpower64, malpha, msparc64;
++			marm, marm64, mmips2be, mmips2le, mpower, mpower64, malpha, msparc64;
  extern	Machdata	mipsmach, mipsmachle, sparcmach, m68020mach, i386mach, riscvmach, riscv64mach,
 -			armmach, mipsmach2le, powermach, alphamach, sparc64mach;
 +			armmach, arm64mach, mipsmach2le, powermach, alphamach, sparc64mach;
--- /tmp/arm64-libs.diff	Sat Mar 14 16:47:26 2026
+++ /tmp/arm64-libs.diff	Tue Apr 21 19:56:57 2026
@@ -1558,10 +1558,10 @@
  # common mkfile parameters shared by all architectures
  #
  
--OS=568ijqv
--CPUS=arm amd64 386 riscv riscv64 power mips
-+OS=5678ijqv
-+CPUS=arm arm64 amd64 386 riscv riscv64 power mips
+-OS=58ijqv
+-CPUS=arm 386 riscv riscv64 power mips
++OS=578ijqv
++CPUS=arm arm64 386 riscv riscv64 power mips
  CFLAGS=-FTVw
  LEX=lex
  YACC=yacc
XXX

for (i in arm64-toolchain arm64-libs) {
	echo $i:
	ape/patch -p1 </tmp/$i.diff
}

# essential fix from 9legacy forsyth-cc.diff
ape/patch -p1 <<XXX
--- a/sys/src/cmd/cc/sub.c	Tue Apr 23 19:19:31 2013
+++ b/sys/src/cmd/cc/sub.c	Tue Apr 28 10:02:01 2026
@@ -917,6 +917,10 @@
 	case ONOT:
 	case OADDR:
 	case OIND:
+	case OCOM:
+	case ONEG:
+	case OPOS:
+	case OTST:
 		n = n->left;
 		goto loop;
 
@@ -949,6 +953,8 @@
 	case OOROR:
 	case OCOMMA:
 	case ODOT:
+	case OFAS:
+	case OINDEX:
 		if(side(n->left))
 			break;
 		n = n->right;
@@ -960,6 +966,10 @@
 	case OSTRING:
 	case OLSTRING:
 	case ONAME:
+	case OREGPAIR:
+	case OEXREG:
+	case OREGISTER:
+	case OINDREG:
 		return 0;
 	}
 	return 1;
XXX

# install bcm64 kernel and dependencies

for (i in 9-boot-service 9-boot-usbdevs 9-concurrency 9-fpnote-update 9-tcp-recovery \
	bcm-20260423-fixes replica-proto-arm64 9-port-64bit 9-bcm64) {
		echo $i:
		hget http://9legacy.org/9legacy/patch/$i.diff | ape/patch -p1
}

# install wifi firmware

mkdir sys/lib/firmware
repo=https://github.com/RPi-Distro/firmware-nonfree/raw/buster/brcm
for (f in \
	brcmfmac434^(30 36 55)^-sdio.bin \
	brcmfmac434^(30 36 55)^-sdio.txt \
	brcmfmac434^(36 55)^-sdio.clm_blob) {
		hget $repo/$f >sys/lib/firmware/$f
}

echo fsys main sync >>/srv/fscons.rpi

echo Building libraries, command binaries and kernels is left as an exercise for the reader ☺

# Something like this perhaps:

# cd /n/fossil.rpi
# bind -c acme /acme
# bind -c sys /sys
# bind -c arm /arm
# bind -c mail/lib /mail/lib
# bind -c rc/bin /rc/bin
# fn 5l {/$cputype/bin/5l -f $*}
# fn pcc {/$cputype/bin/pcc -f $*}
# cd /sys/src
# objtype=arm mk install
# cd /sys/src/9/bcm
# mk installall
# cd /sys/src/9/bcm64
# mk installall
# echo fsys main sync >>/srv/fscons.rpi
# cp /arm^(9pi 9picpu 9pi2 9pi2cpu 9pi4 9pi4cpu) /n/dos
# cp /arm64/9pi5 /n/dos

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2026 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.