Deleted Added
full compact
generate-fat.sh (302408) generate-fat.sh (329114)
1#!/bin/sh
2
3# This script generates the dummy FAT filesystem used for the EFI boot
4# blocks. It uses newfs_msdos to generate a template filesystem with the
5# relevant interesting files. These are then found by grep, and the offsets
6# written to a Makefile snippet.
7#
8# Because it requires root, and because it is overkill, we do not
9# do this as part of the normal build. If makefs(8) grows workable FAT
10# support, this should be revisited.
11
1#!/bin/sh
2
3# This script generates the dummy FAT filesystem used for the EFI boot
4# blocks. It uses newfs_msdos to generate a template filesystem with the
5# relevant interesting files. These are then found by grep, and the offsets
6# written to a Makefile snippet.
7#
8# Because it requires root, and because it is overkill, we do not
9# do this as part of the normal build. If makefs(8) grows workable FAT
10# support, this should be revisited.
11
12# $FreeBSD: stable/11/sys/boot/efi/boot1/generate-fat.sh 297871 2016-04-12 20:52:28Z emaste $
12# $FreeBSD: stable/11/sys/boot/efi/boot1/generate-fat.sh 329114 2018-02-11 02:27:50Z kevans $
13
14FAT_SIZE=1600 #Size in 512-byte blocks of the produced image
15
13
14FAT_SIZE=1600 #Size in 512-byte blocks of the produced image
15
16BOOT1_SIZE=128k
16BOOT1_OFFSET=2d
17BOOT1_SIZE=384k
17
18
18#
19# Known filenames
20# amd64: BOOTx64.efi
21# arm64: BOOTaa64.efi
22# arm: BOOTarm.efi
23# i386: BOOTia32.efi
24#
25if [ -z "$2" ]; then
26 echo "Usage: $0 arch boot-filename"
19if [ $(id -u) != 0 ]; then
20 echo "${0##*/}: must run as root" >&2
27 exit 1
28fi
29
21 exit 1
22fi
23
30ARCH=$1
31FILENAME=$2
32
33# Generate 800K FAT image
34OUTPUT_FILE=fat-${ARCH}.tmpl
35
36dd if=/dev/zero of=$OUTPUT_FILE bs=512 count=$FAT_SIZE
37DEVICE=`mdconfig -a -f $OUTPUT_FILE`
38newfs_msdos -F 12 -L EFI $DEVICE
39mkdir stub
40mount -t msdosfs /dev/$DEVICE stub
41
42# Create and bless a directory for the boot loader
43mkdir -p stub/efi/boot
44
45# Make a dummy file for boot1
46echo 'Boot1 START' | dd of=stub/efi/boot/$FILENAME cbs=$BOOT1_SIZE count=1 conv=block
47# Provide a fallback startup.nsh
48echo $FILENAME > stub/efi/boot/startup.nsh
49
50umount stub
51mdconfig -d -u $DEVICE
52rmdir stub
53
54# Locate the offset of the fake file
55BOOT1_OFFSET=$(hd $OUTPUT_FILE | grep 'Boot1 START' | cut -f 1 -d ' ')
56
57# Convert to number of blocks
58BOOT1_OFFSET=$(echo 0x$BOOT1_OFFSET | awk '{printf("%x\n",$1/512);}')
59
60# Record maximum boot1 size in bytes
61case $BOOT1_SIZE in
62*k)
63 BOOT1_MAXSIZE=$(expr ${BOOT1_SIZE%k} '*' 1024)
64 ;;
65*)
66 BOOT1_MAXSIZE=$BOOT1_SIZE
67 ;;
68esac
69
70echo '# This file autogenerated by generate-fat.sh - DO NOT EDIT' > Makefile.fat
71echo "# \$FreeBSD\$" >> Makefile.fat
72echo "BOOT1_OFFSET=0x$BOOT1_OFFSET" >> Makefile.fat
73echo "BOOT1_MAXSIZE=$BOOT1_MAXSIZE" >> Makefile.fat
74
24# Record maximum boot1 size in bytes
25case $BOOT1_SIZE in
26*k)
27 BOOT1_MAXSIZE=$(expr ${BOOT1_SIZE%k} '*' 1024)
28 ;;
29*)
30 BOOT1_MAXSIZE=$BOOT1_SIZE
31 ;;
32esac
33
34echo '# This file autogenerated by generate-fat.sh - DO NOT EDIT' > Makefile.fat
35echo "# \$FreeBSD\$" >> Makefile.fat
36echo "BOOT1_OFFSET=0x$BOOT1_OFFSET" >> Makefile.fat
37echo "BOOT1_MAXSIZE=$BOOT1_MAXSIZE" >> Makefile.fat
38
75bzip2 $OUTPUT_FILE
76echo 'FAT template boot filesystem created by generate-fat.sh' > $OUTPUT_FILE.bz2.uu
77echo 'DO NOT EDIT' >> $OUTPUT_FILE.bz2.uu
78echo "\$FreeBSD\$" >> $OUTPUT_FILE.bz2.uu
39while read ARCH FILENAME; do
40 # Generate 800K FAT image
41 OUTPUT_FILE=fat-${ARCH}.tmpl
79
42
80uuencode $OUTPUT_FILE.bz2 $OUTPUT_FILE.bz2 >> $OUTPUT_FILE.bz2.uu
81rm $OUTPUT_FILE.bz2
43 dd if=/dev/zero of=$OUTPUT_FILE bs=512 count=$FAT_SIZE
44 DEVICE=`mdconfig -a -f $OUTPUT_FILE`
45 newfs_msdos -F 12 -L EFI $DEVICE
46 mkdir stub
47 mount -t msdosfs /dev/$DEVICE stub
82
48
49 # Create and bless a directory for the boot loader
50 mkdir -p stub/efi/boot
51
52 # Make a dummy file for boot1
53 echo 'Boot1 START' | dd of=stub/efi/boot/$FILENAME cbs=$BOOT1_SIZE count=1 conv=block
54 # Provide a fallback startup.nsh
55 echo $FILENAME > stub/efi/boot/startup.nsh
56
57 umount stub
58 mdconfig -d -u $DEVICE
59 rmdir stub
60
61 # Locate the offset of the fake file
62 OFFSET=$(hd $OUTPUT_FILE | grep 'Boot1 START' | cut -f 1 -d ' ')
63
64 # Convert to number of blocks
65 OFFSET=$(echo 0x$OFFSET | awk '{printf("%x\n",$1/512);}')
66
67 # Validate the offset
68 if [ $OFFSET != $BOOT1_OFFSET ]; then
69 echo "Incorrect offset $OFFSET != $BOOT1_OFFSET" >&2
70 exit 1
71 fi
72
73 xz -f $OUTPUT_FILE
74done <<EOF
75 amd64 BOOTx64.efi
76 arm64 BOOTaa64.efi
77 arm BOOTarm.efi
78 i386 BOOTia32.efi
79EOF