cros_bundle_firmware: Avoid hard-coding the U-Boot entry point

We want to find the cold boot entry point for U-Boot x86. This is not
necessarily at the same address. So instead of using a hard-coded position,
search for the first instruction of the code boot region.

This is really just a hack, since there is no guarantee that U-Boot won't
change again the future, although it is very stable.

Still, the existing code is even worse, since it uses a hard-coded offset!

BUG=chrome-os-partner:19534
BUG=chromium:245311
TEST=manual
Build and boot on link. Type 'vboot_twostop'. See that it correctly identifies
itself as RO firmware.

Change-Id: Id586ab319d2b6d49decce7caab5e7c2efbb5fc29
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57050
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
diff --git a/host/lib/bundle_firmware.py b/host/lib/bundle_firmware.py
index 6386efa..d16bac2 100644
--- a/host/lib/bundle_firmware.py
+++ b/host/lib/bundle_firmware.py
@@ -1190,9 +1190,19 @@
         self._tools.Run('cbfstool', [bootstub, 'add-payload', '-f',
             self.coreboot_elf, '-n', 'fallback/payload', '-c', 'lzma'])
       else:
+        text_base = 0x1110000
+
+        # This is the the 'movw $GD_FLG_COLD_BOOT, %bx' instruction
+        # 1110015:       66 bb 00 01             mov    $0x100,%bx
+        marker = struct.pack('<L', 0x0100bb66)
+        pos = uboot_data.find(marker)
+        if pos == -1 or pos > 0x100:
+          raise ValueError('Cannot find U-Boot cold boot entry point')
+        entry = text_base + pos
+        self._out.Notice('U-Boot entry point %#08x' % entry)
         self._tools.Run('cbfstool', [bootstub, 'add-flat-binary', '-f',
             uboot_dtb, '-n', 'fallback/payload', '-c', 'lzma',
-            '-l', '0x1110000', '-e', '0x1110008'])
+            '-l', '%#x' % text_base, '-e', '%#x' % entry])
       self._tools.Run('cbfstool', [bootstub, 'add', '-f', fdt.fname,
           '-n', 'u-boot.dtb', '-t', '0xac'])
       data = self._tools.ReadFile(bootstub)