]> wimlib.net Git - wimlib/blobdiff - programs/mkwinpeimg.in
mkwinpeimg: Support writing ISO image to stdout
[wimlib] / programs / mkwinpeimg.in
index 9aa8cb119d6329bd7d6859bc2a8c239368f603c3..48ffebb3442dbf2346ada080ce44bdf482fc84ab 100755 (executable)
@@ -18,7 +18,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-script_name="$(basename $0)"
+script_name="$(basename "$0")"
 PREFIX_REG="::"
 WIMLIB_VERSION=@VERSION@
 imagex=@IMAGEX_PROGNAME@
@@ -94,13 +94,14 @@ Usage: $script_name [OPTIONS] IMAGE
   -A, --waik-dir=DIR       Get the boot files and boot.wim from the ISO of the
                               Windows Automated Installation Kit mounted on DIR
                               instead of from the Windows 7 or Windows 8 DVD.
+                              This also works if the mounted ISO is for the
+                              WAIK supplement rather than the WAIK itself.
   -s, --start-script=FILE  Add FILE to the root directory of Windows PE image
                               and adjust \Windows\System32\winpeshl.ini to
                               execute FILE when Windows PE starts up.
-  -w, --wim=WIM            Use WIM as the boot.wim file.  Defaults to
-                              sources/boot.wim in the Windows DVD directory, or
-                              F1_WINPE.WIM from the WAIK if --waik-dir is
-                              specified.
+  -w, --wim=WIM            Use WIM as the boot.wim file.  This defaults to the
+                              appropriate WIM file from the Windows DVD, WAIK,
+                              or WAIK supplement.
   -O, --overlay=DIR        Adds all the files in DIR to the Windows PE image.
   -t, --tmp-dir=DIR        Use DIR as the temporary base of the ISO filesystem.
                               Defaults to making one using "mktemp -d".
@@ -115,7 +116,7 @@ EOF
 }
 
 version() {
-       echo "$script_name (wimlib $WIMLIB_VERSION)"
+       echo "$script_name (distributed with wimlib $WIMLIB_VERSION)"
        exit 0
 }
 
@@ -255,7 +256,7 @@ EOF
 
 check_needed_programs() {
        if [ -z "$waik_dir" -o -n "$modify_wim" ]; then
-               if ! type -P @IMAGEX_PROGNAME@ &> /dev/null ; then
+               if ! type -P "$imagex" &> /dev/null ; then
                        cat << EOF
 ERROR: To make a customized image of Windows PE, we need the "$imagex" program
 from "wimlib" so that we can modify the boot.wim file.  However, "$imagex"
@@ -297,7 +298,7 @@ EOF
                fi
        fi
 
-       if [ -n "$waik_dir" ]; then
+       if [ -n "$waik_dir" ] &&  [ -f "$waik_dir"/wAIK${arch}.msi ]; then
                if ! type -P cabextract &> /dev/null ; then
                        cat << EOF
 ERROR: The boot files in the Windows Automated Installation Kit (WAIK) are
@@ -316,7 +317,7 @@ get_primary_boot_files() {
                # Get boot files from the WAIK.
                stat_busy "Copying primary boot files from the Windows Automated Installation Kit ($waik_dir, $arch)"
 
-               if [ -f "$waik_dir"/WAIK${arch}.msi ]; then
+               if [ -f "$waik_dir"/wAIK${arch}.msi ]; then
                        if [ $make = iso ]; then
                                cabextract "$waik_dir"/wAIK${arch}.msi -F F_WINPE_${arch}_etfsboot.com -p \
                                                > "$tmp_dir"/etfsboot.com || stat_fail
@@ -367,7 +368,7 @@ get_boot_wim() {
                                                > "$boot_wim" 2>/dev/null || stat_fail
                        # WAIK supplement has different layout
                        else
-                               stat_busy "Copying boot.wim from ${arch,,}/winpe.wim"
+                               stat_busy "Copying boot.wim from \"${waik_dir}/${arch,,}/winpe.wim\""
                                cp "$waik_dir"/${arch,,}/winpe.wim "$boot_wim" || stat_fail
                                chmod +w "$boot_wim"
                        fi
@@ -407,13 +408,13 @@ modify_boot_wim() {
 
        if [ -n "$start_script" ]; then
                stat_busy "Setting \"$start_script\" as the script to be executed when Windows PE boots"
-               cp "$start_script" "$tmp_dir/$start_script"
+               start_script_base="$(basename "$start_script")"
                cat > "$tmp_dir/__mkwinpeimg.winpeshl.ini" <<- EOF
                        [LaunchApps]
-                       %SYSTEMDRIVE%\\$start_script
+                       %SYSTEMDRIVE%\\$start_script_base
                EOF
                cat 1>&3 <<- EOF
-                       add '$tmp_dir/$start_script' '/$start_script'
+                       add '$start_script' '/$start_script_base'
                        delete --force /Windows/System32/winpeshl.ini
                        add '$tmp_dir/__mkwinpeimg.winpeshl.ini' /Windows/System32/winpeshl.ini
                EOF
@@ -445,7 +446,7 @@ make_iso_img() {
 
        mkisofs -sysid ""  -A ""  -V "Microsoft Windows PE ($arch)"  -d -N \
                -b etfsboot.com  -no-emul-boot   -c boot.cat  -hide etfsboot.com  \
-               -hide boot.cat -quiet -o "$image" "$tmp_dir" || stat_fail
+               -hide boot.cat -quiet -o "$image" "$tmp_dir" 1>&4 || stat_fail
 
        stat_done
 }
@@ -472,11 +473,26 @@ make_disk_img() {
 
        export MTOOLSRC="$mtool_conf"
 
-       mformat -h 255 -s 63 -T $(( image_size / 512)) s:
-       mcopy -s "$tmp_dir"/* s:
+       mformat -h 255 -s 63 -T $(( image_size / 512)) s: || stat_fail
+       mcopy -s "$tmp_dir"/* s: || stat_fail
 
        syslinux --install "$image"
-       mcopy /usr/lib/syslinux/chain.c32 s:
+
+       syslinuxdir="/usr/lib/syslinux"
+
+       if [ -d "$syslinuxdir/bios" ]; then
+               biosdir="$syslinuxdir/bios"
+       else
+               biosdir="$syslinuxdir"
+       fi
+
+       mcopy "$biosdir/chain.c32" s: || stat_fail
+       if [ -e "$biosdir/libcom32.c32" ]; then
+               mcopy "$biosdir/libcom32.c32" s:
+       fi
+       if [ -e "$biosdir/libutil.c32" ]; then
+               mcopy "$biosdir/libutil.c32" s:
+       fi
        mcopy - 's:syslinux.cfg' <<- EOF
                DEFAULT winpe
                LABEL   winpe
@@ -490,6 +506,22 @@ make_disk_img() {
 calc_columns
 tmp_dir="$(mktemp -d)"
 process_command_line "$@"
+
+if [ "$image" = "-" ] ; then
+       # Writing image to standard output
+       if [ "$make" != iso ]; then
+               echo "ERROR: Writing image to standard output is only supported in --iso mode!"
+               exit 1
+       fi
+       # We can't print anything to standard output except the ISO image
+       # itself.  Play with the file descriptors.
+
+       exec 4>&1  # 4 is now the original standard output.
+       exec 1>&2  # Anything that goes to standard output now, by default,
+                  # actually goes to standard error.
+else
+       exec 4>&1  # 4 is now a copy of standard output
+fi
 if [ -z "$waik_dir" ]; then
        find_windows_dir
 fi
@@ -522,4 +554,6 @@ elif [ $make = disk ]; then
        make_disk_img "$image"
 fi
 
-echo "The image ($image) is $(stat -c %s "$image") bytes."
+if [ "$image" != "-" ]; then
+       echo "The image ($image) is $(stat -c %s "$image") bytes."
+fi