vuls: init at 0.27.0
[NixPkgs.git] / nixos / modules / system / boot / loader / raspberrypi / raspberrypi-builder.sh
blob0541ca1ba6221051444e655e0de1f7b48b4d533c
1 #! @bash@/bin/sh
3 # This can end up being called disregarding the shebang.
4 set -e
6 shopt -s nullglob
8 export PATH=/empty
9 for i in @path@; do PATH=$PATH:$i/bin; done
11 usage() {
12 echo "usage: $0 -c <path-to-default-configuration> [-d <boot-dir>]" >&2
13 exit 1
16 default= # Default configuration
17 target=/boot # Target directory
19 while getopts "c:d:" opt; do
20 case "$opt" in
21 c) default="$OPTARG" ;;
22 d) target="$OPTARG" ;;
23 \?) usage ;;
24 esac
25 done
27 echo "updating the boot generations directory..."
29 mkdir -p $target/old
31 # Convert a path to a file in the Nix store such as
32 # /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
33 cleanName() {
34 local path="$1"
35 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
38 # Copy a file from the Nix store to $target/kernels.
39 declare -A filesCopied
41 copyToKernelsDir() {
42 local src="$1"
43 local dst="$target/old/$(cleanName $src)"
44 # Don't copy the file if $dst already exists. This means that we
45 # have to create $dst atomically to prevent partially copied
46 # kernels or initrd if this script is ever interrupted.
47 if ! test -e $dst; then
48 local dstTmp=$dst.tmp.$$
49 cp $src $dstTmp
50 mv $dstTmp $dst
52 filesCopied[$dst]=1
53 result=$dst
56 copyForced() {
57 local src="$1"
58 local dst="$2"
59 cp $src $dst.tmp
60 mv $dst.tmp $dst
63 outdir=$target/old
64 mkdir -p $outdir || true
66 # Copy its kernel and initrd to $target/old.
67 addEntry() {
68 local path="$1"
69 local generation="$2"
71 if ! test -e $path/kernel -a -e $path/initrd; then
72 return
75 local kernel=$(readlink -f $path/kernel)
76 local initrd=$(readlink -f $path/initrd)
77 local dtb_path=$(readlink -f $path/dtbs)
79 if test -n "@copyKernels@"; then
80 copyToKernelsDir $kernel; kernel=$result
81 copyToKernelsDir $initrd; initrd=$result
84 echo $(readlink -f $path) > $outdir/$generation-system
85 echo $(readlink -f $path/init) > $outdir/$generation-init
86 cp $path/kernel-params $outdir/$generation-cmdline.txt
87 echo $initrd > $outdir/$generation-initrd
88 echo $kernel > $outdir/$generation-kernel
90 if test "$generation" = "default"; then
91 copyForced $kernel $target/kernel.img
92 copyForced $initrd $target/initrd
93 for dtb in $dtb_path/{broadcom,}/bcm*.dtb; do
94 dst="$target/$(basename $dtb)"
95 copyForced $dtb "$dst"
96 filesCopied[$dst]=1
97 done
98 cp "$(readlink -f "$path/init")" $target/nixos-init
99 echo "`cat $path/kernel-params` init=$path/init" >$target/cmdline.txt
103 addEntry $default default
105 # Add all generations of the system profile to the menu, in reverse
106 # (most recent to least recent) order.
107 for generation in $(
108 (cd /nix/var/nix/profiles && ls -d system-*-link) \
109 | sed 's/system-\([0-9]\+\)-link/\1/' \
110 | sort -n -r); do
111 link=/nix/var/nix/profiles/system-$generation-link
112 addEntry $link $generation
113 done
115 # Add the firmware files
116 fwdir=@firmware@/share/raspberrypi/boot/
117 copyForced $fwdir/bootcode.bin $target/bootcode.bin
118 copyForced $fwdir/fixup.dat $target/fixup.dat
119 copyForced $fwdir/fixup4.dat $target/fixup4.dat
120 copyForced $fwdir/fixup4cd.dat $target/fixup4cd.dat
121 copyForced $fwdir/fixup4db.dat $target/fixup4db.dat
122 copyForced $fwdir/fixup4x.dat $target/fixup4x.dat
123 copyForced $fwdir/fixup_cd.dat $target/fixup_cd.dat
124 copyForced $fwdir/fixup_db.dat $target/fixup_db.dat
125 copyForced $fwdir/fixup_x.dat $target/fixup_x.dat
126 copyForced $fwdir/start.elf $target/start.elf
127 copyForced $fwdir/start4.elf $target/start4.elf
128 copyForced $fwdir/start4cd.elf $target/start4cd.elf
129 copyForced $fwdir/start4db.elf $target/start4db.elf
130 copyForced $fwdir/start4x.elf $target/start4x.elf
131 copyForced $fwdir/start_cd.elf $target/start_cd.elf
132 copyForced $fwdir/start_db.elf $target/start_db.elf
133 copyForced $fwdir/start_x.elf $target/start_x.elf
135 # Add the config.txt
136 copyForced @configTxt@ $target/config.txt
138 # Remove obsolete files from $target and $target/old.
139 for fn in $target/old/*linux* $target/old/*initrd-initrd* $target/bcm*.dtb; do
140 if ! test "${filesCopied[$fn]}" = 1; then
141 rm -vf -- "$fn"
143 done