xref: /freebsd/release/amd64/mkisoimages.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2#
3# Module: mkisoimages.sh
4# Author: Jordan K Hubbard
5# Date:   22 June 2001
6#
7#
8# This script is used by release/Makefile to build the (optional) ISO images
9# for a FreeBSD release.  It is considered architecture dependent since each
10# platform has a slightly unique way of making bootable CDs.  This script
11# is also allowed to generate any number of images since that is more of
12# publishing decision than anything else.
13#
14# Usage:
15#
16# mkisoimages.sh [-b] image-label image-name base-bits-dir [extra-bits-dir]
17#
18# Where -b is passed if the ISO image should be made "bootable" by
19# whatever standards this architecture supports (may be unsupported),
20# image-label is the ISO image label, image-name is the filename of the
21# resulting ISO image, base-bits-dir contains the image contents and
22# extra-bits-dir, if provided, contains additional files to be merged
23# into base-bits-dir as part of making the image.
24
25set -e
26
27scriptdir=$(dirname $(realpath $0))
28. ${scriptdir}/../../tools/boot/install-boot.sh
29
30if [ -z $ETDUMP ]; then
31	ETDUMP=etdump
32fi
33
34if [ -z $MAKEFS ]; then
35	MAKEFS=makefs
36fi
37
38if [ -z $MKIMG ]; then
39	MKIMG=mkimg
40fi
41
42if [ "$1" = "-b" ]; then
43	MAKEFSARG="$4"
44else
45	MAKEFSARG="$3"
46fi
47
48if [ -f ${MAKEFSARG} ]; then
49	BASEBITSDIR=`dirname ${MAKEFSARG}`
50	METALOG=${MAKEFSARG}
51elif [ -d ${MAKEFSARG} ]; then
52	BASEBITSDIR=${MAKEFSARG}
53	METALOG=
54else
55	echo "${MAKEFSARG} must exist"
56	exit 1
57fi
58
59if [ "$1" = "-b" ]; then
60	# This is highly x86-centric and will be used directly below.
61	bootable="-o bootimage=i386;$BASEBITSDIR/boot/cdboot -o no-emul-boot"
62
63	# Make EFI system partition.
64	espfilename=$(mktemp /tmp/efiboot.XXXXXX)
65	# ESP file size in KB.
66	espsize="2048"
67	if [ -f "${BASEBITSDIR}/boot/loader_ia32.efi" ]; then
68		make_esp_file ${espfilename} ${espsize} ${BASEBITSDIR}/boot/loader.efi bootx64 \
69		    ${BASEBITSDIR}/boot/loader_ia32.efi bootia32
70	else
71		make_esp_file ${espfilename} ${espsize} ${BASEBITSDIR}/boot/loader.efi
72	fi
73	bootable="$bootable -o bootimage=i386;${espfilename} -o no-emul-boot -o platformid=efi"
74
75	shift
76else
77	bootable=""
78fi
79
80if [ $# -lt 3 ]; then
81	echo "Usage: $0 [-b] image-label image-name base-bits-dir [extra-bits-dir]"
82	exit 1
83fi
84
85LABEL=`echo "$1" | tr '[:lower:]' '[:upper:]'`; shift
86NAME="$1"; shift
87# MAKEFSARG extracted already
88shift
89
90publisher="The FreeBSD Project.  https://www.FreeBSD.org/"
91echo "/dev/iso9660/$LABEL / cd9660 ro 0 0" > "$BASEBITSDIR/etc/fstab"
92if [ -n "${METALOG}" ]; then
93	metalogfilename=$(mktemp /tmp/metalog.XXXXXX)
94	cat ${METALOG} > ${metalogfilename}
95	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" >> ${metalogfilename}
96	MAKEFSARG=${metalogfilename}
97fi
98$MAKEFS -D -N ${BASEBITSDIR}/etc -t cd9660 $bootable -o rockridge -o label="$LABEL" -o publisher="$publisher" "$NAME" "$MAKEFSARG" "$@"
99rm -f "$BASEBITSDIR/etc/fstab"
100rm -f ${espfilename}
101if [ -n "${METALOG}" ]; then
102	rm ${metalogfilename}
103fi
104
105if [ "$bootable" != "" ]; then
106	# Look for the EFI System Partition image we dropped in the ISO image.
107	for entry in `$ETDUMP --format shell $NAME`; do
108		eval $entry
109		if [ "$et_platform" = "efi" ]; then
110			espstart=`expr $et_lba \* 2048`
111			espsize=`expr $et_sectors \* 512`
112			espparam="-p efi::$espsize:$espstart"
113			break
114		fi
115	done
116
117	# Create a GPT image containing the partitions we need for hybrid boot.
118	hybridfilename=$(mktemp /tmp/hybrid.img.XXXXXX)
119	if [ "$(uname -s)" = "Linux" ]; then
120		imgsize=`stat -c %s "$NAME"`
121	else
122		imgsize=`stat -f %z "$NAME"`
123	fi
124	$MKIMG -s gpt \
125	    --capacity $imgsize \
126	    -b "$BASEBITSDIR/boot/pmbr" \
127	    -p freebsd-boot:="$BASEBITSDIR/boot/isoboot" \
128	    $espparam \
129	    -o $hybridfilename
130
131	# Drop the PMBR, GPT, and boot code into the System Area of the ISO.
132	dd if=$hybridfilename of="$NAME" bs=32k count=1 conv=notrunc
133	rm -f $hybridfilename
134fi
135