cros_bundle_firmware: Update the BL2 checksum for exynos

We must update the checksum for the BL2. USB download has always required
it, and now with new BL1 apparently other booting options require it also.

BUG=chromium-os:28394
TEST=manual:
cros_bundle_firmware -b daisy -w usb -B spi
See that it now downloads and runs the BL2 correctly

Change-Id: I6c814fe85df68faefcc9d4a195aab5f912312e72
Reviewed-on: https://gerrit.chromium.org/gerrit/23377
Tested-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Doug Anderson <dianders@chromium.org>
Tested-by: Doug Anderson <dianders@chromium.org>
Commit-Ready: Doug Anderson <dianders@chromium.org>
(cherry picked from commit e5e8afb753618ab0c57561161d116a48a0e152c0)
Reviewed-on: https://gerrit.chromium.org/gerrit/23401
diff --git a/host/lib/bundle_firmware.py b/host/lib/bundle_firmware.py
index d153b69..3a86641 100644
--- a/host/lib/bundle_firmware.py
+++ b/host/lib/bundle_firmware.py
@@ -611,6 +611,23 @@
     self._out.Info('BL2 configuration complete')
     return data
 
+  def _UpdateChecksum(self, data):
+    """Update the BL2 checksum.
+
+    The checksum is a 4 byte sum of all the bytes in the image before the
+    last 4 bytes (which hold the checksum).
+
+    Args:
+      data: The BL2 data to update.
+
+    Returns:
+      The new contents of the BL2 data, after updating the checksum.
+    """
+    checksum = 0
+    for ch in data[:-4]:
+      checksum += ord(ch)
+    return data[:-4] + struct.pack('<L', checksum & 0xffffffff)
+
   def _ConfigureExynosBl2(self, fdt, pack, orig_bl2):
     """Configure an Exynos BL2 binary for our needs.
 
@@ -634,6 +651,7 @@
       raise CmdError("Could not find machine parameter block in '%s'" %
           orig_bl2)
     data = self._UpdateBl2Parameters(fdt, pack, data, pos)
+    data = self._UpdateChecksum(data)
     self._tools.WriteFile(bl2, data)
     return bl2