Improve handling of invalid filenames
[wimlib] / programs / mkwinpeimg.in
1 #!/usr/bin/env bash
2 #
3 # This script can make a customized bootable image of Windows PE.
4 #
5
6 # Copyright (C) 2012, 2013 Eric Biggers
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 script_name="$(basename $0)"
22 PREFIX_REG="::"
23 WIMLIB_VERSION=@VERSION@
24 imagex=@IMAGEX_PROGNAME@
25
26 calc_columns () {
27         STAT_COL=80
28         if [[ -t 0 ]]; then
29                 # stty will fail when stdin isn't a terminal
30                 STAT_COL=$(stty size)
31                 # stty gives "rows cols"; strip the rows number, we just want columns
32                 STAT_COL=${STAT_COL##* }
33         elif tput cols &>/dev/null; then
34                 # is /usr/share/terminfo already mounted, and TERM recognized?
35                 STAT_COL=$(tput cols)
36         fi
37         if (( STAT_COL == 0 )); then
38                 # if output was 0 (serial console), set default width to 80
39                 STAT_COL=80
40         fi
41
42         # we use 13 characters for our own stuff
43         STAT_COL=$(( STAT_COL - 13 ))
44
45         if [[ -t 1 ]]; then
46                 SAVE_POSITION="\e[s"
47                 RESTORE_POSITION="\e[u"
48                 DEL_TEXT="\e[$(( STAT_COL + 4 ))G"
49         else
50                 SAVE_POSITION=""
51                 RESTORE_POSITION=""
52                 DEL_TEXT=""
53         fi
54 }
55
56
57 deltext() {
58         printf "${DEL_TEXT}"
59 }
60
61 stat_busy() {
62         printf "${PREFIX_REG} ${1} "
63         printf "${SAVE_POSITION}"
64         deltext
65         printf "   [BUSY] "
66 }
67
68 stat_done() {
69         deltext
70         printf "   [DONE] \n"
71 }
72
73 stat_fail() {
74         deltext
75         printf "   [FAIL] \n"
76         exit 1
77 }
78
79
80 cleanup() {
81         rm -rf "$tmp_dir"
82 }
83
84 usage() {
85         cat << EOF
86 Usage: $script_name [OPTIONS] IMAGE
87
88   -i, --iso                Make an ISO image instead of a disk image.
89   -o, --only-wim           Make neither a disk image nor an ISO image;
90                               instead, only make a modified boot.wim file.
91   -W, --windows-dir=DIR    Use DIR as the location of the mounted Windows 7
92                               or Windows 8 DVD.  Default is /mnt/windows,
93                               then /mnt/windows7, then /mnt/windows8.
94   -A, --waik-dir=DIR       Get the boot files and boot.wim from the ISO of the
95                               Windows Automated Installation Kit mounted on DIR
96                               instead of from the Windows 7 or Windows 8 DVD.
97   -s, --start-script=FILE  Add FILE to the root directory of Windows PE image
98                               and adjust \Windows\System32\winpeshl.ini to
99                               execute FILE when Windows PE starts up.
100   -w, --wim=WIM            Use WIM as the boot.wim file.  Defaults to
101                               sources/boot.wim in the Windows DVD directory, or
102                               F1_WINPE.WIM from the WAIK if --waik-dir is
103                               specified.
104   -O, --overlay=DIR        Adds all the files in DIR to the Windows PE image.
105   -t, --tmp-dir=DIR        Use DIR as the temporary base of the ISO filesystem.
106                               Defaults to making one using "mktemp -d".
107   -a, --arch=ARCH          Use the Windows PE version from the WAIK that has
108                               the CPU architecture ARCH.  Possible values:
109                               "x86" or "amd64".  Default is "x86".
110   -h, --help               Display this information.
111   -v, --version            Show version information.
112
113   See \`man mkwinpeimg' for more information.
114 EOF
115 }
116
117 version() {
118         echo "$script_name (wimlib $WIMLIB_VERSION)"
119         exit 0
120 }
121
122 make=disk
123
124 process_command_line() {
125
126         if ! options=$(getopt -o oiw:W:s:O:t:A:a:hv -l \
127                 only-wim,iso,wim:,windows-dir:,start-script:,overlay:,tmp-dir:,waik-dir:,arch:,help,version \
128                                 -- "$@" ); then
129                 usage
130                 exit 1
131         fi
132
133         # default arch value
134         arch="X86"
135         arch_id="1"
136
137         eval set -- "$options"
138         while [ $# -gt 0 ]; do
139                 case "$1" in
140                 -i|--iso)
141                         make=iso
142                         ;;
143                 -o|--only-wim)
144                         make=wim
145                         ;;
146                 -W|--windows-dir)
147                         windows_dir="$2"
148                         windows_dir_specified=yes
149                         if [ -n "$waik_dir" ]; then
150                                 echo "ERROR: Cannot specify both --windows-dir and --waik-dir!"
151                                 exit 1
152                         fi
153                         shift
154                         ;;
155                 -A|--waik-dir)
156                         waik_dir="$2"
157                         if [ -n "$windows_dir" ]; then
158                                 echo "ERROR: Cannot specify both --windows-dir and --waik-dir!"
159                                 exit 1
160                         fi
161                         shift
162                         ;;
163                 -w|--wim)
164                         wim="$2"
165                         shift
166                         ;;
167                 -s|--start-script)
168                         start_script="$2"
169                         shift
170                         ;;
171                 -O|--overlay)
172                         overlay="$2"
173                         shift
174                         ;;
175                 -t|--tmp-dir)
176                         rmdir "$tmp_dir"
177                         tmp_dir="$2"
178                         shift
179                         ;;
180                 -a|--arch)
181                         if [ "$2" == "x86" ]; then
182                                 arch="X86"
183                                 arch_id="1"
184                         # Need to test Itanium images before making it an
185                         # option.  Note: syslinux is x86 only so can't be used
186                         # for the Itanium disk image.
187                         #elif [ "$2" == "ia64" ]; then
188                                 #arch="IA64"
189                                 #arch_id="2"
190                         elif [ "$2" == "amd64" ]; then
191                                 arch="AMD64"
192                                 arch_id="3"
193                         else
194                                 echo "ERROR: $2 is not a valid arch (x86/amd64)"
195                                 exit 1
196                         fi
197                         shift
198                         ;;
199                 -h|--help)
200                         usage
201                         exit 0
202                         ;;
203                 -v|--version)
204                         version
205                         ;;
206                 --)
207                         shift
208                         break
209                         ;;
210                 *)
211                         echo "Invalid option \"$1\""
212                         usage
213                         exit 1
214                         ;;
215                 esac
216                 shift
217         done
218
219         if [ $# -ne 1 ]; then
220                 echo "You must specify the name of the image file to create!"
221                 echo "Run \"$script_name -h\" to see usage information."
222                 exit 1
223         else
224                 image="$1"
225         fi
226 }
227
228 find_windows_dir() {
229         if [ -z "$windows_dir_specified" ]; then
230                 for windows_dir in /mnt/windows /mnt/windows7 /mnt/windows8; do
231                         if [ -d "$windows_dir"/sources ]; then
232                                 break
233                         fi
234                 done
235         fi
236         if [ ! -d "$windows_dir" ]; then
237                 if [ -z "$windows_dir_specified" ]; then
238                         cat << EOF
239 ERROR: Could not find the directory that the Windows 7 or 8 ISO image is mounted
240 on!  Please specify this directory using the --windows-dir option.
241 EOF
242                 else
243                         echo "ERROR: Could not find the directory \"$windows_dir\"!"
244                 fi
245                 exit 1
246         fi
247         if [ ! -d "$windows_dir/sources" ]; then
248                 cat << EOF
249 ERROR: The directory "$windows_dir" exists, but it seems that the Windows 7 or 8
250 ISO image is not mounted on it.  Please mount the image to continue.
251 EOF
252                 exit 1
253         fi
254 }
255
256 check_needed_programs() {
257         if [ -z "$waik_dir" -o -n "$modify_wim" ]; then
258                 if ! type -P @IMAGEX_PROGNAME@ &> /dev/null ; then
259                         cat << EOF
260 ERROR: To make a customized image of Windows PE, we need the "$imagex" program
261 from "wimlib" so that we can modify the boot.wim file.  However, "$imagex"
262 doesn't seem to be installed.  Please install "wimlib" to continue.
263 EOF
264                         exit 1
265                 fi
266         fi
267
268         if [ $make = iso ]; then
269                 if ! type -P mkisofs &> /dev/null ; then
270                         cat << EOF
271 ERROR: To make a bootable ISO image of Windows PE, we need the "mkisofs"
272 program, but it doesn't seem to be installed.  Please install the "cdrkit"
273 package to continue, or try omitting the --iso option to make a disk image
274 instead of an ISO image.
275 EOF
276                         exit 1
277                 fi
278         elif [ $make = disk ] ; then
279                 if ! type -P syslinux &> /dev/null ; then
280                         cat << EOF
281 ERROR: To make a bootable disk image of Windows PE, we need the "syslinux"
282 program, but it doesn't seem to be installed.  Please install the "syslinux"
283 package to continue, or try using the --iso option to make an ISO image instead
284 of a disk image.
285 EOF
286                         exit 1
287                 fi
288
289                 if ! type -P mformat mcopy &> /dev/null; then
290                         cat << EOF
291 ERROR: To make a bootable disk image of Windows PE, we need the "mformat" and
292 "mcopy" programs from the "mtools" package.  These programs allow us to
293 format a FAT filesystem and copy files to it without needing root privileges.
294 Please install "mtools" if you want to make a disk image of Windows PE.  Or,
295 try using the --iso option to make an ISO image instead of a disk image.
296 EOF
297                 fi
298         fi
299
300         if [ -n "$waik_dir" ]; then
301                 if ! type -P cabextract &> /dev/null ; then
302                         cat << EOF
303 ERROR: The boot files in the Windows Automated Installation Kit (WAIK) are
304 inside cabinet archives.  To extract these files, we need the "cabextract"
305 program, but it doesn't seem to be installed.  Please install "cabextract" to
306 continue.
307 EOF
308                         exit 1
309                 fi
310         fi
311
312 }
313
314 get_primary_boot_files() {
315         if [ -n "$waik_dir" ]; then
316                 # Get boot files from the WAIK.
317
318                 stat_busy "Copying primary boot files from the Windows Automated Installation Kit ($waik_dir, $arch)"
319                 if [ $make = iso ]; then
320                         cabextract "$waik_dir"/wAIK${arch}.msi -F F_WINPE_${arch}_etfsboot.com -p \
321                                         > "$tmp_dir"/etfsboot.com || stat_fail
322                 fi
323                 cabextract "$waik_dir"/wAIK${arch}.msi -F F${arch_id}_BOOTMGR -p \
324                                 > "$tmp_dir"/bootmgr || stat_fail
325                 cabextract "$waik_dir"/wAIK${arch}.msi -F F_WINPE_${arch}_boot.sdi -p \
326                                 > "$tmp_dir"/boot/boot.sdi || stat_fail
327                 cabextract "$waik_dir"/wAIK${arch}.msi -F F_WINPE_${arch}_bcd -p \
328                                 > "$tmp_dir"/boot/bcd || stat_fail
329                 stat_done
330         else
331                 # Get boot files from the Windows ISO
332
333                 stat_busy "Copying primary boot files from mounted Windows DVD ($windows_dir)"
334                 if [ $make = iso ]; then
335                         cp "$windows_dir"/boot/etfsboot.com "$tmp_dir" || stat_fail
336                 fi
337                 cp "$windows_dir"/bootmgr "$tmp_dir" || stat_fail
338                 cp "$windows_dir"/boot/{bcd,boot.sdi} "$tmp_dir"/boot || stat_fail
339                 stat_done
340         fi
341 }
342
343 get_boot_wim() {
344         boot_wim="$1"
345         # Copy the WIM over, or export the 2nd image in the WIM in the case of boot.wim
346         # from the Windows DVD.
347         remove_setup=
348         if [ -z "$wim" ]; then
349
350                 # WIM file unspecified- grab it from the WAIK or the Windows DVD
351                 if [ -n "$waik_dir" ]; then
352                         # WAIK
353                         stat_busy "Extracting boot.wim from \"$waik_dir/WinPE.cab\""
354                         cabextract "$waik_dir/WinPE.cab" -F F${arch_id}_WINPE.WIM -p \
355                                         > "$boot_wim" 2>/dev/null || stat_fail
356                         stat_done
357                 else
358                         # Windows DVD
359                         remove_setup=yes
360                         wim="$windows_dir/sources/boot.wim"
361                         stat_busy "Exporting image from \"$wim\""
362                         "$imagex" export "$windows_dir"/sources/boot.wim 2 \
363                                                 --boot "$boot_wim" || stat_fail
364                         stat_done
365                 fi
366         else
367                 # WIM file specified
368                 stat_busy "Copying $wim to temporary directory"
369                 cp "$wim" "$boot_wim"|| stat_fail
370                 stat_done
371         fi
372 }
373
374 # Make modifications to the WIM.
375 modify_boot_wim() {
376         boot_wim="$1"
377         tmp_dir="$2"
378
379         exec 3>"$tmp_dir/__mkwinpeimg.update.cmds"
380
381         if [ -n "$remove_setup" ]; then
382                 stat_busy "Renaming setup.exe to prevent it from bothering us"
383                 cat 1>&3 <<- EOF
384                         rename /setup.exe /setup.exe.orig
385                         rename /sources/setup.exe /sources/setup.exe.orig
386                 EOF
387                 stat_done
388         fi
389
390         if [ -n "$start_script" ]; then
391                 stat_busy "Setting \"$start_script\" as the script to be executed when Windows PE boots"
392                 cp "$start_script" "$tmp_dir/$start_script"
393                 cat > "$tmp_dir/__mkwinpeimg.winpeshl.ini" <<- EOF
394                         [LaunchApps]
395                         %SYSTEMDRIVE%\\$start_script
396                 EOF
397                 cat 1>&3 <<- EOF
398                         add '$tmp_dir/$start_script' '/$start_script'
399                         delete --force /Windows/System32/winpeshl.ini
400                         add '$tmp_dir/__mkwinpeimg.winpeshl.ini' /Windows/System32/winpeshl.ini
401                 EOF
402                 stat_done
403         fi
404
405         if [ -n "$overlay" ]; then
406                 stat_busy "Overlaying \"$overlay\" on the Windows PE filesystem"
407                 cat 1>&3 <<- EOF
408                         add '$overlay' /
409                 EOF
410                 stat_done
411         fi
412
413         exec 3>&-
414
415         stat_busy "Rebuilding WIM with changes made"
416         "$imagex" update "$boot_wim" --rebuild \
417                 < "$tmp_dir/__mkwinpeimg.update.cmds" > /dev/null || stat_fail
418         stat_done
419 }
420
421 make_iso_img() {
422         image="$1"
423
424         # Make the ISO using the mkisofs command from cdrkit
425
426         stat_busy "Making ISO image \"$image\""
427
428         mkisofs -sysid ""  -A ""  -V "Microsoft Windows PE ($arch)"  -d -N \
429                 -b etfsboot.com  -no-emul-boot   -c boot.cat  -hide etfsboot.com  \
430                 -hide boot.cat -quiet -o "$image" "$tmp_dir" || stat_fail
431
432         stat_done
433 }
434
435 make_disk_img() {
436         image="$1"
437
438         stat_busy "Making disk image \"$image\""
439
440         image_du=$(du -s -b "$tmp_dir" | cut -f 1)
441         image_size=$(( image_du + 10000000 ))
442
443         mtool_conf="$(mktemp)"
444
445         dd if=/dev/zero of="$image" count=$(( (image_size + 4095) / 4096)) \
446                         bs=4096 &> /dev/null
447
448         cat > "$mtool_conf" <<- EOF
449                 MTOOLS_SKIP_CHECK=1
450                 MTOOLS_FAT_COMPATIBILITY=1
451                 drive s:
452                         file="$image"
453         EOF
454
455         export MTOOLSRC="$mtool_conf"
456
457         mformat -h 255 -s 63 -T $(( image_size / 512)) s:
458         mcopy -s "$tmp_dir"/* s:
459
460         syslinux --install "$image"
461         mcopy /usr/lib/syslinux/chain.c32 s:
462         mcopy - 's:syslinux.cfg' <<- EOF
463                 DEFAULT winpe
464                 LABEL   winpe
465                 COM32   chain.c32
466                 APPEND  ntldr=/bootmgr
467         EOF
468         rm -f "$mtool_conf"
469         stat_done
470 }
471
472 calc_columns
473 tmp_dir="$(mktemp -d)"
474 process_command_line "$@"
475 if [ -z "$waik_dir" ]; then
476         find_windows_dir
477 fi
478 if [ -n "$start_script" -o -n "$overlay" -o -n "$remove_setup" ]; then
479         modify_wim=yes
480 fi
481 check_needed_programs
482 trap cleanup exit
483
484 if [ $make != wim ]; then
485         mkdir -p "$tmp_dir"/{boot,sources}
486         get_primary_boot_files
487 fi
488
489 if [ $make = wim ]; then
490         boot_wim="$image"
491 else
492         boot_wim="$tmp_dir"/sources/boot.wim
493 fi
494
495 get_boot_wim "$boot_wim"
496
497 if [ -n "$modify_wim" ]; then
498         modify_boot_wim "$boot_wim" "$tmp_dir"
499 fi
500
501 if [ $make = iso ]; then
502         make_iso_img "$image"
503 elif [ $make = disk ]; then
504         make_disk_img "$image"
505 fi
506
507 echo "The image ($image) is $(stat -c %s "$image") bytes."