vboot/make_dev_firmware: extract bitmaps from system firmware instead of prebuilts.

make_dev_firmware should trust system firmware blobs instead of using its own
prebuilt binaries.

BUG=chromium-os:12400
TEST=1. make a build with .49+ BIOS, and create the firmware updater
     2. install the new BIOS, turn on developer switch and reboot
     3. an ugly "DEV" screen (in .49) is shown on screen
     4. run the new make_dev_firmware.sh
     5. reboot. seeing the ugly DEV instead of the prebuilt blue screen
     (verified successfully)

Change-Id: If75d2821170649fa869db6a82d879b697f7588e6

Review URL: http://codereview.chromium.org/6614006
diff --git a/scripts/image_signing/make_dev_firmware.sh b/scripts/image_signing/make_dev_firmware.sh
index cecbbff..3a9348d 100755
--- a/scripts/image_signing/make_dev_firmware.sh
+++ b/scripts/image_signing/make_dev_firmware.sh
@@ -14,14 +14,16 @@
 # Constants used by DEFINE_*
 VBOOT_BASE='/usr/share/vboot'
 DEFAULT_KEYS_FOLDER="$VBOOT_BASE/devkeys"
-DEFAULT_BMPFV_FILE="$DEFAULT_KEYS_FOLDER/firmware_bmpfv.bin"
+DEFAULT_BMPFV_FILE="<auto>"
 DEFAULT_BACKUP_FOLDER='/mnt/stateful_partition/backups'
+DEFAULT_FIRMWARE_UPDATER='/usr/sbin/chromeos-firmwareupdate'
 
 # DEFINE_string name default_value description flag
 DEFINE_string from "" "Path of input file (empty for system live firmware)" "f"
 DEFINE_string to "" "Path of output file (empty for system live firmware)" "t"
 DEFINE_string keys "$DEFAULT_KEYS_FOLDER" "Path to folder of dev keys" "k"
-DEFINE_string bmpfv "$DEFAULT_BMPFV_FILE" "Path to the new bitmap FV" ""
+DEFINE_string bmpfv "$DEFAULT_BMPFV_FILE" \
+  "Path to the new bitmaps, <auto> to extract from system, empty to keep." ""
 DEFINE_boolean force_backup \
   $FLAGS_TRUE "Create backup even if source is not live" ""
 DEFINE_string backup_dir \
@@ -109,6 +111,28 @@
   echo "$hwid_dev"
 }
 
+# Explores compatible firmware bitmaps
+explore_bmpfv() {
+  local tmp_folder=""
+
+  if [ -s "$DEFAULT_FIRMWARE_UPDATER" ]; then
+    # try to extract from built-in firmware updater
+    debug_msg "found default firmware updater, trying to fetch bitmap..."
+    tmp_folder=$("$DEFAULT_FIRMWARE_UPDATER" --sb_extract | sed "s'[^/]*''")
+    debug_msg "updater resources extrated to: $tmp_folder"
+
+    if [ -d "$tmp_folder" -a -s "$tmp_folder/bios.bin" ]; then
+      new_bmpfv="$tmp_folder/bmpfv.bin"
+      echo "$new_bmpfv"
+      gbb_utility --bmpfv="$new_bmpfv" "$tmp_folder/bios.bin" >/dev/null 2>&1
+    else
+      debug_msg "failed to find valid BIOS image file."
+    fi
+  else
+    debug_msg "no firmware updater in system. not changing bitmaps."
+  fi
+}
+
 # Main
 # ----------------------------------------------------------------------------
 main() {
@@ -121,6 +145,12 @@
   local new_bmpfv="$FLAGS_bmpfv"
   local is_from_live=0
   local backup_image=
+  local opt_bmpfv=""
+
+  if [ "$new_bmpfv" = "$DEFAULT_BMPFV_FILE" ]; then
+    new_bmpfv=$(explore_bmpfv) &&
+      debug_msg "Using bitmaps from $new_bmpfv"
+  fi
 
   debug_msg "Prerequisite check"
   ensure_files_exist \
@@ -128,14 +158,18 @@
     "$recovery_pubkey" \
     "$firmware_keyblock" \
     "$firmware_prvkey" \
-    "$kernel_sub_pubkey" \
-    "$new_bmpfv" ||
+    "$kernel_sub_pubkey" ||
     exit 1
 
+  if [ -n "$new_bmpfv" ]; then
+    opt_bmpfv="--bmpfv=$new_bmpfv"
+    ensure_files_exist "$new_bmpfv" || exit 1
+  fi
+
   if [ -z "$FLAGS_from" ]; then
     is_from_live=1
   else
-    ensure_files_exist "$FLAGS_from"
+    ensure_files_exist "$FLAGS_from" || exit 1
   fi
 
   debug_msg "Checking software write protection status"
@@ -177,9 +211,9 @@
   debug_msg "Replace GBB parts (gbb_utility allows changing on-the-fly)"
   gbb_utility --set \
     --hwid="$new_hwid" \
-    --bmpfv="$new_bmpfv" \
     --rootkey="$root_pubkey" \
     --recoverykey="$recovery_pubkey" \
+    $opt_bmpfv \
     "$IMAGE" >"$EXEC_LOG" 2>&1 ||
     err_die "Failed to change GBB Data. (message: $(cat "$EXEC_LOG"))"