cros_bundle_firmware: Extract text base from payload for tegra

For Tegra, we need to know the text base. When we don't have access to
the original U-Boot, extract it from the payload and get the text base
that way.

This allows cros_write_firmware to work without the '-F spi' option.

BUG=chromium-os:32468
TEST=manual
cros_write_firmware -b tegra2_seaboard -i out/image.bin

Change-Id: I31d4dc0825dbfa07797e2d9277db5c7909f0ff82
Reviewed-on: https://gerrit.chromium.org/gerrit/27165
Commit-Ready: Simon Glass <sjg@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
diff --git a/host/lib/write_firmware.py b/host/lib/write_firmware.py
index a95c74b..b0d7aab 100644
--- a/host/lib/write_firmware.py
+++ b/host/lib/write_firmware.py
@@ -252,9 +252,17 @@
     if flash_dest:
       image = self.PrepareFlasher(uboot, payload, self.update, self.verify,
                                     boot_type, 0)
-    else:
+    elif bootstub:
       image = bootstub
 
+    else:
+      image = payload
+      # If we don't know the textbase, extract it from the payload.
+      if self.text_base == -1:
+        data = self._tools.ReadFile(payload)
+        # Skip the BCT which is the first 64KB
+        self.text_base = self._bundle.DecodeTextBase(data[0x10000:])
+
     self._out.Notice('TEXT_BASE is %#x' % self.text_base)
     self._out.Progress('Uploading flasher image')
     args = [
@@ -614,7 +622,7 @@
 
 def DoWriteFirmware(output, tools, fdt, flasher, file_list, image_fname,
                     bundle, update=True, verify=False, dest=None,
-                    flash_dest=None, kernel=None, props=None):
+                    flash_dest=None, kernel=None, props={}):
   """A simple function to write firmware to a device.
 
   This creates a WriteFirmware object and uses it to write the firmware image
@@ -642,10 +650,10 @@
     method = fdt.GetString('/chromeos-config', 'flash-method', 'tegra')
     if method == 'tegra':
       tools.CheckTool('tegrarcm')
-      bootstub = props['bootstub']
+      bootstub = props.get('bootstub')
       if flash_dest:
         write.text_base = bundle.CalcTextBase('flasher ', fdt, flasher)
-      else:
+      elif bootstub:
         write.text_base = bundle.CalcTextBase('bootstub ', fdt, bootstub)
       ok = write._NvidiaFlashImage(flash_dest, flasher, file_list['bct'],
           image_fname, bootstub)