iPXE - Open Source Boot Firmware

[libc] Fix strcmp()/strncmp() to return proper values
[ipxe.git] / src / util / geniso
1 #!/bin/bash
2 #
3 # Generate a isolinux ISO boot image
4
5 function help() {
6         echo "usage: ${0} [OPTIONS] foo.lkrn [bar.lkrn,...]"
7         echo
8         echo "where OPTIONS are:"
9         echo " -h       show this help"
10         echo " -l       build legacy image with floppy emulation"
11         echo " -o FILE  save iso image to file"
12 }
13
14 LEGACY=0
15 FIRST=""
16
17 while getopts "hlo:" opt; do
18         case ${opt} in
19                 h)
20                         help
21                         exit 0
22                         ;;
23                 l)
24                         LEGACY=1
25                         ;;
26                 o)
27                         OUT="${OPTARG}"
28                         ;;
29         esac
30 done
31
32 shift $((OPTIND - 1))
33
34 if [ -z "${OUT}" ]; then
35         echo "${0}: no output file given" >&2
36         help
37         exit 1
38 fi
39
40 # There should either be mkisofs or the compatible genisoimage program
41 for command in genisoimage mkisofs; do
42         if ${command} --version >/dev/null 2>/dev/null; then
43                 mkisofs=(${command})
44                 break
45         fi
46 done
47
48 if [ -z "${mkisofs}" ]; then
49         echo "${0}: mkisofs or genisoimage not found, please install or set PATH" >&2
50         exit 1
51 fi
52
53 dir=$(mktemp -d bin/iso.dir.XXXXXX)
54 cfg=${dir}/isolinux.cfg
55
56 mkisofs+=(-quiet -l -volid "iPXE" -preparer "iPXE build system"
57         -appid "iPXE ${VERSION} - Open Source Network Boot Firmware"
58         -publisher "http://ipxe.org/" -c boot.cat)
59
60 # generate the config
61 cat > ${cfg} <<EOF
62 # These default options can be changed in the geniso script
63 SAY iPXE ISO boot image
64 TIMEOUT 30
65 EOF
66 for f; do
67         if [ ! -r ${f} ]; then
68                 echo "${f} does not exist, skipping" >&2
69                 continue
70         fi
71         b=$(basename ${f})
72         g=${b%.lkrn}
73         g=${g//[^a-z0-9]}
74         g=${g:0:8}.krn
75         case "${FIRST}" in
76                 "")
77                         echo "DEFAULT ${b}"
78                         FIRST=${g}
79                         ;;
80         esac
81         echo "LABEL ${b}"
82         echo " KERNEL ${g}"
83         cp ${f} ${dir}/${g}
84 done >> ${cfg}
85
86 case "${LEGACY}" in
87         1)
88                 # check for mtools
89                 case "$(mtools -V)" in
90                         Mtools\ version\ 3.9.9*|Mtools\ version\ 3.9.1[0-9]*|[mM]tools\ *\ [4-9].*)
91                                 ;;
92                         *)
93                                 echo "Mtools version 3.9.9 or later is required" >&2
94                                 exit 1
95                                 ;;
96                 esac
97
98                 # generate floppy image
99                 img=${dir}/boot.img
100                 mformat -f 1440 -C -i ${img} ::
101
102                 # copy lkrn file to floppy image
103                 for f in ${dir}/*.krn; do
104                         mcopy -m -i ${img} ${f} ::$(basename ${g})
105                         rm -f ${f}
106                 done
107
108                 # copy config file to floppy image
109                 mcopy -i ${img} ${cfg} ::syslinux.cfg
110                 rm -f ${cfg}
111
112                 # write syslinux bootloader to floppy image
113                 if ! syslinux ${img}; then
114                         echo "${0}: failed writing syslinux to floppy image ${img}" >&2
115                         exit 1
116                 fi
117
118                 # generate the iso image
119                 "${mkisofs[@]}" -b boot.img -output ${OUT} ${dir}
120                 ;;
121         0)
122                 # copy isolinux bootloader
123                 cp ${ISOLINUX_BIN} ${dir}
124
125                 # syslinux 6.x needs a file called ldlinux.c32
126                 if [ -n "${LDLINUX_C32}" -a -s "${LDLINUX_C32}" ]; then
127                         cp ${LDLINUX_C32} ${dir}
128                 fi
129
130                 # generate the iso image
131                 "${mkisofs[@]}" -b isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -output ${OUT} ${dir}
132
133                 # isohybrid will be used if available
134                 if isohybrid --version >/dev/null 2>/dev/null; then
135                         isohybrid ${OUT} >/dev/null
136                 fi
137                 ;;
138 esac
139
140 # clean up temporary dir
141 rm -fr ${dir}