1#!/bin/sh
2# $NetBSD: mkimage,v 1.92 2025/04/01 00:04:00 gutteridge Exp $
3#
4# Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Christos Zoulas.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. Neither the name of The NetBSD Foundation nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32# POSSIBILITY OF SUCH DAMAGE.
33#
34
35#
36# Makes a bootable image for the host architecture given.
37# The host-specific functions are pulled in from a /bin/sh script in the
38# "conf" directory, which is expected to provide the following shell
39# functions, which are called in the following order:
40#
41#  - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}.
42#    If -m is given, a number of directories are put on a tmpfs RAM disk
43#  - customize: After unpacking the sets, this gets the system to
44#    a working state, e.g., by setting up /etc/rc.conf and /dev
45#  - populate: Add common goods like kernel and bootloader
46#  - make_label: Prints disklabel to stdout
47#
48
49set -e
50
51DIR="$(cd "$(dirname "$0")" && pwd)"
52PROG="$(basename "$0")"
53
54MAKE=${TOOL_MAKE:-make}
55DISKLABEL=${TOOL_DISKLABEL:-disklabel}
56FDISK=${TOOL_FDISK:-fdisk}
57GPT=${TOOL_GPT:-gpt}
58MAKEFS=${TOOL_MAKEFS:-makefs}
59MTREE=${TOOL_MTREE:-mtree}
60INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
61MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
62GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
63SED=${TOOL_SED:-sed}
64PWD_MKDB=${TOOL_PWD_MKDB:-pwd_mkdb}
65
66postfix=false
67[ "${MKPOSTFIX:-yes}" = no ] || postfix=true
68
69src="/usr/src"
70sets="base comp etc games gpufw man manhtml misc modules rescue tests text"
71xsets="xbase xcomp xetc xfont xserver"
72minfree="10%"
73bar="==="
74
75tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
76mnt="${tmp}/mnt"
77mkdir -p "${mnt}/etc" "${mnt}/dev"
78
79trap "cleanup" 0 1 2 3 15
80
81cleanup() {
82          case "$tmp" in
83          "${TMPDIR:-/tmp}/$PROG."*)    rm -fr "$tmp";;
84          esac
85}
86
87fail() {
88          IFS=' '
89          echo >&2 "${PROG}: $*"
90          exit 1
91}
92
93getsize() {
94          set -- $(ls -l $1)
95          echo $5
96}
97
98getsectors() {
99          case "$1" in
100          *g)
101                    m=1073741824
102                    v=${1%g}
103                    ;;
104          *m)
105                    m=1048576
106                    v=${1%m}
107                    ;;
108          *k)
109                    m=1024
110                    v=${1%k}
111                    ;;
112          *[0-9b])
113                    m=1
114                    v=${1%b}
115                    ;;
116          esac
117          echo $((m * v / 512))
118}
119
120minwrites_fstab_entries() {
121          $minwrites || return 0
122          cat << EOF
123tmpfs               /var/log            tmpfs     rw,union,-s32M
124tmpfs               /var/run            tmpfs     rw,union,-s1M
125tmpfs               /var/mail           tmpfs     rw,union,-s10M
126tmpfs               /var/chroot                   tmpfs     rw,union,-s10M
127EOF
128          if $postfix; then
129          cat << EOF
130tmpfs               /var/spool/postfix  tmpfs     rw,union,-s20M
131tmpfs               /var/db/postfix               tmpfs     rw,union,-s1M
132EOF
133          fi
134}
135
136make_fstab_gpt() {
137          local boot=$1
138          local rootopts=
139          if $minwrites; then
140                    rootopts=,log,nodevmtime
141          fi
142
143          cat > ${mnt}/etc/fstab << EOF
144# NetBSD /etc/fstab
145# See /usr/share/examples/fstab/ for more examples.
146NAME=${gpt_label_ffs:-netbsd-root}      /                   ffs       rw,noatime${rootopts}         1 1
147NAME=${gpt_label_boot:-$boot}           /boot               msdos     rw        1 1
148ptyfs               /dev/pts  ptyfs     rw
149procfs              /proc               procfs    rw
150tmpfs               /var/shm  tmpfs     rw,-m1777,-sram%25
151EOF
152          minwrites_fstab_entries >> ${mnt}/etc/fstab
153}
154
155# From Richard Neswold's:
156# http://rich-tbp.blogspot.com/2013/03/netbsd-on-rpi-minimizing-disk-writes.html
157# Also for the postfix stuff below
158make_fstab_normal() {
159          local rootopts=
160          if $minwrites; then
161                    rootopts=,nodevmtime
162          fi
163          cat > ${mnt}/etc/fstab << EOF
164# NetBSD /etc/fstab
165# See /usr/share/examples/fstab/ for more examples.
166ROOT.a              /                             ffs       rw,noatime${rootopts}         1 1
167ROOT.e              /boot                         msdos     rw                                      1 1
168ptyfs               /dev/pts            ptyfs     rw
169procfs              /proc                         procfs    rw
170tmpfs               /tmp                          tmpfs     rw,-s32M
171tmpfs               /var/shm            tmpfs     rw,-m1777,-sram%25
172EOF
173          minwrites_fstab_entries >> ${mnt}/etc/fstab
174}
175
176make_fstab_default() {
177          if $gpt; then
178                    make_fstab_gpt "$@"
179          else
180                    make_fstab_normal
181          fi
182          echo "./etc/fstab type=file uname=root gname=wheel mode=0644" \
183              >> "$tmp/selected_sets"
184
185          # Missing mount points from fstab
186          echo "./proc type=dir uname=root gname=wheel mode=0755" \
187              >> "$tmp/selected_sets"
188}
189
190usage() {
191          cat << EOF 1>&2
192Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
193
194-b        Boot only, no sets loaded
195-r        root device kind (sd, wd, ld)
196-d        Add the debug sets
197-m        Optimize the OS installation to mimimize disk writes for SSDs
198-x        Load the X sets too, not just the base ones
199EOF
200          exit 1
201}
202
203# First pass for options to get the host and src directories
204OPTS="B:D:K:S:bc:dh:mr:s:x"
205while getopts "$OPTS" f
206do
207          case $f in
208          h)        h="$OPTARG";;
209          S)        src="$OPTARG";;
210          *)        ;;
211          esac
212done
213
214if [ -z "$h" ]
215then
216          usage
217fi
218
219if [ ! -f "${DIR}/conf/${h}.conf" ]
220then
221          echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
222          exit 1
223fi
224
225resize=false
226gpt=false
227gpt_hybrid=false
228fsize=8192
229bsize=65536
230ffsversion=1
231
232. "${DIR}/conf/${h}.conf"
233release="/usr/obj/${MACHINE}/release"
234
235selected_sets="$sets"
236dsets_p=false
237xsets_p=false
238minwrites=false
239rootdev=ld
240endian=
241
242OPTIND=1
243while getopts "$OPTS" f
244do
245          case $f in
246          B)        endian="-B $OPTARG";;
247          D)        release="$OPTARG";;
248          K)        kernel="$OPTARG";;
249          S)        ;;
250          b)        bootonly=true;;
251          d)        dsets_p=true
252                    selected_sets="$selected_sets debug"
253                    if $xsets_p; then
254                              selected_sets="$selected_sets xdebug"
255                    fi
256                    ;;
257          c)        custom="$OPTARG";;
258          h)        ;;
259          m)        minwrites=true;;
260          r)        rootdev="$OPTARG";;
261          s)        size="$OPTARG";;
262          x)        xsets_p=true
263                    selected_sets="$selected_sets $xsets"
264                    if $dsets_p; then
265                        selected_sets="$selected_sets xdebug"
266                    fi
267                    ;;
268          *)        usage;;
269          esac
270done
271if [ -n "${MKREPRO_TIMESTAMP}" ]; then
272          timestamp_opt="-T ${MKREPRO_TIMESTAMP}"
273          volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))"
274fi
275
276shift $(( $OPTIND - 1 ))
277if [ -n "$1" ]; then
278          # take the next argument as being the image name
279          image="$1"
280          shift
281fi
282
283case "$image" in
284*.gz)     compress=true; image="${image%.gz}";;
285*)        compress=false;;
286esac
287
288if [ -z "${bootonly}" ]; then
289          echo ${bar} configuring sets ${bar}
290          (cat "${release}/etc/mtree/NetBSD.dist"
291          for i in $selected_sets; do
292                    s="${release}/etc/mtree/set.$i"
293                    if [ -f "$s" ]; then
294                              cat "$s"
295                    fi
296          done) > "$tmp/selected_sets"
297fi
298
299make_fstab
300customize
301populate
302
303if [ ! "${MKDTB}" = "no" ]; then
304          #
305          # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
306          # the rest on FFS. Split it up here.
307          #
308          echo ${bar} Installing devicetree blobs ${bar}
309          mkdir -p "${mnt}/boot"
310          cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
311
312          mkdir -p "${mnt}/etc/mtree"
313          cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
314          echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
315
316          mkdir -p "${mnt}/var/db/obsolete"
317          cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
318          echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
319fi
320
321if [ -n "${msdosid}" ]; then
322          echo ${bar} Populating msdos filesystem ${bar}
323
324          case $(( ${msdosid} )) in
325          1)        fat_opt=",fat_type=12";;
326          4|6|14)   fat_opt=",fat_type=16";;
327          11|12)    fat_opt=",fat_type=32";;
328          *)        fat_opt=;;
329          esac
330          ${MAKEFS} -N ${release}/etc -t msdos \
331              -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \
332              -O $((${init} / 2))m -s $((${boot} / 2))m \
333              ${image} ${mnt}/boot
334fi
335
336if [ -z "${bootonly}" ]; then
337          echo ${bar} Populating ffs filesystem ${bar}
338          ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
339              -O ${ffsoffset} ${timestamp_opt} \
340              -o d=4096,f=${fsize},b=${bsize},v=${ffsversion} -b $((${extra}))m \
341              -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
342fi
343
344if [ "${size}" = 0 ]; then
345          size="$(getsize "${image}")"
346          # Round up to a multiple of 4m and add 1m of slop.
347          alignunit=$((4*1024*1024))
348          alignsize=$((alignunit*((size + alignunit - 1)/alignunit)))
349          alignsize=$((alignsize + 1024*1024))
350          if [ "${size}" -lt "${alignsize}" ]; then
351                    dd bs=1 count="$((alignsize - size))" if=/dev/zero \
352                              >> "${image}" 2> /dev/null
353                    size="${alignsize}"
354          fi
355fi
356
357if $gpt; then
358          if $gpt_hybrid; then
359                    gpt_flags="-H"
360          fi
361          gpt_flags="${gpt_flags} ${timestamp_opt}"
362          initsecs=$((${init} * 1024))
363          bootsecs=$((${boot} * 1024))
364          ffsstart="$(getsectors ${ffsoffset})"
365
366          echo ${bar} Clearing existing partitions ${bar}
367          ${GPT} ${gpt_flags} ${image} destroy || true
368
369          echo ${bar} Creating partitions ${bar}
370          ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
371          ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
372          ${GPT} ${gpt_flags} ${image} set -a required -i 1
373          ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
374          ${GPT} ${gpt_flags} ${image} show
375          if $gpt_hybrid; then
376                    echo ${bar} Creating hybrid MBR ${bar}
377                    ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
378                    ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
379                    ${FDISK} -F ${image}
380          fi
381else
382          if [ -n "${msdosid}" ]; then
383                    echo ${bar} Running fdisk ${bar}
384                    initsecs=$((${init} * 1024))
385                    bootsecs=$((${boot} * 1024))
386                    ${FDISK} -f -i ${image}
387                    ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
388                    if [ -z "${bootonly}" ]; then
389                              ffsstart="$(getsectors ${ffsoffset})"
390                              imagesize="$(getsize "${image}")"
391                              imagesecs="$(getsectors ${imagesize})"
392                              ffssize="$(expr ${imagesecs} - ${ffsstart})"
393                              ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
394                    fi
395
396                    echo ${bar} Adding label ${bar}
397                    make_label > ${tmp}/label
398                    ${DISKLABEL} -m -R -F ${image} ${tmp}/label
399          elif [ -n "${netbsdid}" ]; then
400                    echo ${bar} Adding label ${bar}
401                    make_label > ${tmp}/label
402                    ${DISKLABEL} -m -R -F ${image} ${tmp}/label
403
404                    echo ${bar} Running fdisk ${bar}
405                    ${FDISK} -f -i ${image}
406                    ${FDISK} -f -a -u -0 -s 169/${init} ${image}
407                    ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
408          fi
409fi
410
411if $compress; then
412          echo ${bar} Compressing image ${bar}
413          rm -f "${image}.gz"
414          ${GZIP_CMD} -n -9 ${image}
415          image="${image}.gz"
416fi
417
418cd "${IMAGEDIR}"
419${CKSUM} -a MD5 "$(basename "${image}")" > MD5
420${CKSUM} -a SHA512 "$(basename "${image}")" > SHA512
421
422echo ${bar} Image is ${image} ${bar}
423