#!/bin/bash
#
# Copyright 2024 Oxide Computer Company
#

set -o errexit
set -o pipefail
set -o xtrace

#
# XXX This should be done by image-builder like everything else...
#
root=$1
outfile=$2
tmpdir=$3

#
# Files that we get from the image:
#
(cd "$root" && cpio -qo -H odc -O "$outfile") <<'EOF'
etc/dacf.conf
etc/driver_aliases
etc/driver_classes
etc/mach
etc/name_to_major
etc/name_to_sysnum
etc/path_to_inst
etc/system
etc/system.d/.self-assembly
etc/versions/build
kernel/amd64/genunix
kernel/crypto/amd64/aes
kernel/crypto/amd64/arcfour
kernel/crypto/amd64/blowfish
kernel/crypto/amd64/des
kernel/crypto/amd64/ecc
kernel/crypto/amd64/edonr
kernel/crypto/amd64/md4
kernel/crypto/amd64/md5
kernel/crypto/amd64/rsa
kernel/crypto/amd64/sha1
kernel/crypto/amd64/sha2
kernel/crypto/amd64/skein
kernel/crypto/amd64/swrand
kernel/dacf/amd64/net_dacf
kernel/drv/amd64/bl
kernel/drv/amd64/blkdev
kernel/drv/amd64/clone
kernel/drv/amd64/cn
kernel/drv/amd64/cpuid
kernel/drv/amd64/cpunex
kernel/drv/amd64/crypto
kernel/drv/amd64/cryptoadm
kernel/drv/amd64/cxgbe
kernel/drv/amd64/devinfo
kernel/drv/amd64/dld
kernel/drv/amd64/dlpistub
kernel/drv/amd64/e1000g
kernel/drv/amd64/fm
kernel/drv/amd64/igb
kernel/drv/amd64/iwscn
kernel/drv/amd64/ixgbe
kernel/drv/amd64/kmdb
kernel/drv/amd64/lofi
kernel/drv/amd64/log
kernel/drv/amd64/mm
kernel/drv/amd64/nulldriver
kernel/drv/amd64/nvme
kernel/drv/amd64/openeepr
kernel/drv/amd64/options
kernel/drv/amd64/pci_pci
kernel/drv/amd64/pcieb
kernel/drv/amd64/physmem
kernel/drv/amd64/poll
kernel/drv/amd64/pseudo
kernel/drv/amd64/ptc
kernel/drv/amd64/ptsl
kernel/drv/amd64/ramdisk
kernel/drv/amd64/random
kernel/drv/amd64/rge
kernel/drv/amd64/sad
kernel/drv/amd64/softmac
kernel/drv/amd64/sy
kernel/drv/amd64/sysevent
kernel/drv/amd64/sysmsg
kernel/drv/amd64/t4nex
kernel/drv/amd64/ucode
kernel/drv/amd64/ufm
kernel/drv/amd64/vnic
kernel/drv/amd64/zfs
kernel/drv/bl.conf
kernel/drv/bridge.conf
kernel/drv/clone.conf
kernel/drv/cn.conf
kernel/drv/consms.conf
kernel/drv/cpuid.conf
kernel/drv/crypto.conf
kernel/drv/cryptoadm.conf
kernel/drv/devinfo.conf
kernel/drv/dld.conf
kernel/drv/dlpistub.conf
kernel/drv/e1000g.conf
kernel/drv/fm.conf
kernel/drv/igb.conf
kernel/drv/iwscn.conf
kernel/drv/ixgbe.conf
kernel/drv/kmdb.conf
kernel/drv/llc1.conf
kernel/drv/lofi.conf
kernel/drv/log.conf
kernel/drv/mm.conf
kernel/drv/nvme.conf
kernel/drv/openeepr.conf
kernel/drv/options.conf
kernel/drv/pcieb.conf
kernel/drv/physmem.conf
kernel/drv/poll.conf
kernel/drv/pseudo.conf
kernel/drv/ptc.conf
kernel/drv/ptsl.conf
kernel/drv/ramdisk.conf
kernel/drv/random.conf
kernel/drv/sad.conf
kernel/drv/sgen.conf
kernel/drv/simnet.conf
kernel/drv/smbios.conf
kernel/drv/softmac.conf
kernel/drv/st.conf
kernel/drv/sy.conf
kernel/drv/sysevent.conf
kernel/drv/sysmsg.conf
kernel/drv/ucode.conf
kernel/drv/ufm.conf
kernel/drv/vnic.conf
kernel/drv/zfs.conf
kernel/exec/amd64/elfexec
kernel/exec/amd64/intpexec
kernel/fs/amd64/bootfs
kernel/fs/amd64/ctfs
kernel/fs/amd64/dev
kernel/fs/amd64/devfs
kernel/fs/amd64/fifofs
kernel/fs/amd64/lofs
kernel/fs/amd64/mntfs
kernel/fs/amd64/namefs
kernel/fs/amd64/objfs
kernel/fs/amd64/procfs
kernel/fs/amd64/sharefs
kernel/fs/amd64/specfs
kernel/fs/amd64/tmpfs
kernel/fs/amd64/zfs
kernel/kmdb/amd64/cpc
kernel/kmdb/amd64/cpu_ms.AuthenticAMD.15
kernel/kmdb/amd64/cpu.generic
kernel/kmdb/amd64/crypto
kernel/kmdb/amd64/genunix
kernel/kmdb/amd64/hook
kernel/kmdb/amd64/ipc
kernel/kmdb/amd64/krtld
kernel/kmdb/amd64/lofs
kernel/kmdb/amd64/logindmux
kernel/kmdb/amd64/mac
kernel/kmdb/amd64/mdb_ds
kernel/kmdb/amd64/mm
kernel/kmdb/amd64/neti
kernel/kmdb/amd64/nfs
kernel/kmdb/amd64/ptm
kernel/kmdb/amd64/random
kernel/kmdb/amd64/sata
kernel/kmdb/amd64/sctp
kernel/kmdb/amd64/specfs
kernel/kmdb/amd64/zfs
kernel/mac/amd64/mac_ether
kernel/mac/amd64/mac_ipv4
kernel/mac/amd64/mac_ipv6
kernel/misc/amd64/bignum
kernel/misc/amd64/bootdev
kernel/misc/amd64/busra
kernel/misc/amd64/cmlb
kernel/misc/amd64/consconfig
kernel/misc/amd64/ctf
kernel/misc/amd64/dls
kernel/misc/amd64/edonr
kernel/misc/amd64/fssnap_if
kernel/misc/amd64/gld
kernel/misc/amd64/hidparser
kernel/misc/amd64/hook
kernel/misc/amd64/hpcsvc
kernel/misc/amd64/idmap
kernel/misc/amd64/iommulib
kernel/misc/amd64/ipc
kernel/misc/amd64/kcf
kernel/misc/amd64/kmdbmod
kernel/misc/amd64/mac
kernel/misc/amd64/md5
kernel/misc/amd64/mii
kernel/misc/amd64/neti
kernel/misc/amd64/pci_autoconfig
kernel/misc/amd64/pcicfg
kernel/misc/amd64/pcihp
kernel/misc/amd64/rpcsec
kernel/misc/amd64/sata
kernel/misc/amd64/scsi
kernel/misc/amd64/sha1
kernel/misc/amd64/sha2
kernel/misc/amd64/skein
kernel/misc/amd64/strplumb
kernel/misc/amd64/tlimod
kernel/sched/amd64/SDC
kernel/sched/amd64/TS
kernel/sched/amd64/TS_DPTBL
kernel/strmod/amd64/rpcmod
kernel/sys/amd64/autofs
kernel/sys/amd64/c2audit
kernel/sys/amd64/doorfs
kernel/sys/amd64/inst_sync
kernel/sys/amd64/kaio
kernel/sys/amd64/msgsys
kernel/sys/amd64/pipe
kernel/sys/amd64/portfs
kernel/sys/amd64/pset
kernel/sys/amd64/rpcmod
kernel/sys/amd64/semsys
kernel/sys/amd64/shmsys
platform/oxide/kernel/amd64/unix
platform/oxide/kernel/cpu/amd64/cpu.generic
platform/oxide/kernel/dacf/amd64/consconfig_dacf
platform/oxide/kernel/drv/amd64/cpc
platform/oxide/kernel/drv/amd64/dwu
platform/oxide/kernel/drv/amd64/fch
platform/oxide/kernel/drv/amd64/npe
platform/oxide/kernel/drv/amd64/rootnex
platform/oxide/kernel/drv/cpc.conf
platform/oxide/kernel/drv/dwu.conf
platform/oxide/kernel/kmdb/amd64/apix
platform/oxide/kernel/kmdb/amd64/unix
platform/oxide/kernel/mach/amd64/apix
platform/oxide/kernel/misc/amd64/boot_image
platform/oxide/kernel/misc/amd64/pci_prd
platform/oxide/kernel/misc/amd64/pcie
platform/oxide/kernel/sys/amd64/cpc
platform/oxide/ucode/AuthenticAMD/A011-00
platform/oxide/ucode/AuthenticAMD/fallback/A011-00
platform/oxide/ucode/AuthenticAMD/B021-00
platform/oxide/ucode/AuthenticAMD/B110-00
platform/oxide/ucode/AuthenticAMD/equivalence-table
usr/kernel/pcbe/amd64/pcbe.AuthenticAMD
EOF

#
# The checksum appears in the boot archive, but nowhere else, because we don't
# know the checksum until after the image itself is sealed up:
#
(cd "$tmpdir" && cpio -qo -H odc -AO "$outfile") <<<boot_image_csum
