1#! @bash@/bin/sh -e
2
3shopt -s nullglob
4
5export PATH=/empty
6for i in @path@; do PATH=$PATH:$i/bin; done
7
8default=$1
9if test -z "$1"; then
10 echo "Syntax: builder.sh <DEFAULT-CONFIG>"
11 exit 1
12fi
13
14echo "updating the boot generations directory..."
15
16mkdir -p /boot/old
17
18# Convert a path to a file in the Nix store such as
19# /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
20cleanName() {
21 local path="$1"
22 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
23}
24
25# Copy a file from the Nix store to /boot/kernels.
26declare -A filesCopied
27
28copyToKernelsDir() {
29 local src="$1"
30 local dst="/boot/old/$(cleanName $src)"
31 # Don't copy the file if $dst already exists. This means that we
32 # have to create $dst atomically to prevent partially copied
33 # kernels or initrd if this script is ever interrupted.
34 if ! test -e $dst; then
35 local dstTmp=$dst.tmp.$$
36 cp $src $dstTmp
37 mv $dstTmp $dst
38 fi
39 filesCopied[$dst]=1
40 result=$dst
41}
42
43copyForced() {
44 local src="$1"
45 local dst="$2"
46 cp $src $dst.tmp
47 mv $dst.tmp $dst
48}
49
50outdir=/boot/old
51mkdir -p $outdir || true
52
53# Copy its kernel and initrd to /boot/kernels.
54addEntry() {
55 local path="$1"
56 local generation="$2"
57
58 if ! test -e $path/kernel -a -e $path/initrd; then
59 return
60 fi
61
62 local kernel=$(readlink -f $path/kernel)
63 local initrd=$(readlink -f $path/initrd)
64 local dtb_path=$(readlink -f $path/kernel-modules/dtbs)
65
66 if test -n "@copyKernels@"; then
67 copyToKernelsDir $kernel; kernel=$result
68 copyToKernelsDir $initrd; initrd=$result
69 fi
70
71 echo $(readlink -f $path) > $outdir/$generation-system
72 echo $(readlink -f $path/init) > $outdir/$generation-init
73 cp $path/kernel-params $outdir/$generation-cmdline.txt
74 echo $initrd > $outdir/$generation-initrd
75 echo $kernel > $outdir/$generation-kernel
76
77 if test $(readlink -f "$path") = "$default"; then
78 if [ @version@ -eq 1 ]; then
79 copyForced $kernel /boot/kernel.img
80 else
81 copyForced $kernel /boot/kernel7.img
82 fi
83 copyForced $initrd /boot/initrd
84 for dtb in $dtb_path/bcm*.dtb; do
85 dst="/boot/$(basename $dtb)"
86 copyForced $dtb "$dst"
87 filesCopied[$dst]=1
88 done
89 cp "$(readlink -f "$path/init")" /boot/nixos-init
90 echo "`cat $path/kernel-params` init=$path/init" >/boot/cmdline.txt
91
92 echo "$2" > /boot/defaultgeneration
93 fi
94}
95
96# Add all generations of the system profile to the menu, in reverse
97# (most recent to least recent) order.
98for generation in $(
99 (cd /nix/var/nix/profiles && ls -d system-*-link) \
100 | sed 's/system-\([0-9]\+\)-link/\1/' \
101 | sort -n -r); do
102 link=/nix/var/nix/profiles/system-$generation-link
103 addEntry $link $generation
104done
105
106# Add the firmware files
107fwdir=@firmware@/share/raspberrypi/boot/
108copyForced $fwdir/bootcode.bin /boot/bootcode.bin
109copyForced $fwdir/fixup.dat /boot/fixup.dat
110copyForced $fwdir/fixup_cd.dat /boot/fixup_cd.dat
111copyForced $fwdir/fixup_db.dat /boot/fixup_db.dat
112copyForced $fwdir/start.elf /boot/start.elf
113copyForced $fwdir/start_cd.elf /boot/start_cd.elf
114copyForced $fwdir/start_db.elf /boot/start_db.elf
115copyForced $fwdir/start_x.elf /boot/start_x.elf
116
117# Remove obsolete files from /boot and /boot/old.
118for fn in /boot/old/*linux* /boot/old/*initrd-initrd* /boot/bcm*.dtb; do
119 if ! test "${filesCopied[$fn]}" = 1; then
120 rm -vf -- "$fn"
121 fi
122done