cros_bundle_firmware: zero out unused space in RW sections pre-signing

This mirrors the current behavior for the cb_with_fmap file, leading to
identical content.

BUG=chromium:595715
BRANCH=none
TEST=verify bit-equivalence of regions between image.bin and cb_with_fmap
with CL:397219 or similar.

Change-Id: Ied435269247efa8303d76e1b8e2dd0b2a1f7f25a
Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Reviewed-on: https://chromium-review.googlesource.com/397622
Commit-Ready: Patrick Georgi <pgeorgi@chromium.org>
Tested-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
diff --git a/host/lib/bundle_firmware.py b/host/lib/bundle_firmware.py
index 650f910..aaa89cd 100644
--- a/host/lib/bundle_firmware.py
+++ b/host/lib/bundle_firmware.py
@@ -910,8 +910,17 @@
             last_entry = stdout.strip().splitlines()[-1].split('\t')
             if last_entry[0] == '(empty)' and last_entry[2] == 'null':
                 size = int(last_entry[1], 16)
-                self._tools.Run('truncate', [
-                    '--no-create', '--size', str(size), input_data])
+                trunc_data = self._tools.ReadFile(input_data)
+                full_size = len(trunc_data)
+                trunc_data = trunc_data[:size]
+                trunc_data = (trunc_data + full_size*'\x00')[:full_size]
+                self._tools.WriteFile(input_data, trunc_data)
+                self._tools.Run('cbfstool', [
+                  self.cb_copy, 'write',
+                  '--force',
+                  '-r', region_in, '-f', input_data])
+                trunc_data = trunc_data[:size]
+                self._tools.WriteFile(input_data, trunc_data)
                 self._out.Info('truncated FW_MAIN_%s to %d bytes' %
                     (slot, size))