Handle xloader in addition to bootloader

Change-Id: Iac2499e064686187203dd7106497d720380a8a0a
diff --git a/clear-factory-images-variables.sh b/clear-factory-images-variables.sh
index e0c6758..65319b0 100644
--- a/clear-factory-images-variables.sh
+++ b/clear-factory-images-variables.sh
@@ -12,6 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+unset XLOADER
+unset XLOADERSRC
 unset BOOTLOADER
 unset BOOTLOADERFILE
 unset BOOTLOADERSRC
diff --git a/generate-factory-images-common.sh b/generate-factory-images-common.sh
index d6674bc..32c4a00 100644
--- a/generate-factory-images-common.sh
+++ b/generate-factory-images-common.sh
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright 2011 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -12,16 +12,33 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-if test "$RADIOSRC" = ""
+# Use the default values if they weren't explicitly set
+if test "$XLOADERSRC" = ""
 then
-  RADIOSRC=radio.img
+  XLOADERSRC=xloader.img
 fi
 if test "$BOOTLOADERSRC" = ""
 then
   BOOTLOADERSRC=bootloader.img
 fi
+if test "$RADIOSRC" = ""
+then
+  RADIOSRC=radio.img
+fi
+if test "$SLEEPDURATION" = ""
+then
+  SLEEPDURATION=5
+fi
+
+# Prepare the staging directory
 rm -rf tmp
 mkdir -p tmp/$PRODUCT-$VERSION
+
+# Extract the bootloader(s) and radio(s) as necessary
+if test "$XLOADER" != ""
+then
+  unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/$XLOADERSRC
+fi
 if test "$BOOTLOADERFILE" = ""
 then
   unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/$BOOTLOADERSRC
@@ -34,7 +51,10 @@
 then
   unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/radio-cdma.img
 fi
+
+# Copy the various images in their staging location
 cp ${SRCPREFIX}$PRODUCT-img-$BUILD.zip tmp/$PRODUCT-$VERSION/image-$PRODUCT-$VERSION.zip
+cp tmp/RADIO/$BOOTLOADERSRC tmp/$PRODUCT-$VERSION/xloader-$DEVICE-$XLOADER.img
 if test "$BOOTLOADERFILE" = ""
 then
   cp tmp/RADIO/$BOOTLOADERSRC tmp/$PRODUCT-$VERSION/bootloader-$DEVICE-$BOOTLOADER.img
@@ -49,14 +69,12 @@
 then
   cp tmp/RADIO/radio-cdma.img tmp/$PRODUCT-$VERSION/radio-cdma-$DEVICE-$CDMARADIO.img
 fi
-if test "$SLEEPDURATION" = ""
-then
-  SLEEPDURATION=5
-fi
+
+# Write flash-all.sh
 cat > tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
 #!/bin/sh
 
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright 2011 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -81,6 +99,12 @@
 fastboot erase userdata
 EOF
 fi
+if test "$XLOADER" != ""
+then
+cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
+fastboot flash xloader xloader-$DEVICE-$XLOADER.img
+EOF
+fi
 cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
 fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
 fastboot reboot-bootloader
@@ -106,6 +130,8 @@
 fastboot -w update image-$PRODUCT-$VERSION.zip
 EOF
 chmod a+x tmp/$PRODUCT-$VERSION/flash-all.sh
+
+# Write flash-all.bat
 cat > tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
 @ECHO OFF
 :: Copyright 2012 The Android Open Source Project
@@ -134,6 +160,12 @@
 fastboot erase userdata
 EOF
 fi
+if test "$XLOADER" != ""
+then
+cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
+fastboot flash xloader xloader-$DEVICE-$XLOADER.img
+EOF
+fi
 cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
 fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
 fastboot reboot-bootloader
@@ -162,10 +194,12 @@
 pause >nul
 exit
 EOF
+
+# Write flash-base.sh
 cat > tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
 #!/bin/sh
 
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright 2011 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -179,6 +213,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+EOF
+if test "$XLOADER" != ""
+then
+cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
+fastboot flash xloader xloader-$DEVICE-$XLOADER.img
+EOF
+fi
+cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
 fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
 fastboot reboot-bootloader
 sleep $SLEEPDURATION
@@ -200,6 +242,10 @@
 EOF
 fi
 chmod a+x tmp/$PRODUCT-$VERSION/flash-base.sh
+
+# Create the distributable package
 (cd tmp ; tar zcvf ../$PRODUCT-$VERSION-factory.tgz $PRODUCT-$VERSION)
 mv $PRODUCT-$VERSION-factory.tgz $PRODUCT-$VERSION-factory-$(sha1sum < $PRODUCT-$VERSION-factory.tgz | cut -b -8).tgz
+
+# Clean up
 rm -rf tmp