cros_bundle_firmware: wipe empty regions early on

Also: tell cbfstool to force-overwrite CBFS regions (potentially with
new CBFS data) both for wiped and legacy (one of which will overwrite
rw-legacy).

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: I4ad2a97fcb9977607b35e4d3c323bfa81a827bfd
Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Reviewed-on: https://chromium-review.googlesource.com/397124
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 d990480..46e4528 100644
--- a/host/lib/bundle_firmware.py
+++ b/host/lib/bundle_firmware.py
@@ -750,6 +750,23 @@
 
     pack.AddProperty('bootblock', bootblock_section)
 
+  def _GenerateWiped(self, label, size, value):
+    """Fill a CBFS region in cb_with_fmap with a given value
+
+    Args:
+      label: fdt-style region name
+      size: size of region
+      value: value to fill region with (int)
+    """
+    wipedfile = os.path.join(self._tools.outdir, 'wiped.%s' % label)
+    fmaplabel = self._FdtNameToFmap(label)
+
+    self._tools.WriteFile(wipedfile, size*chr(value))
+    self._tools.Run('cbfstool', [
+      self.cb_copy, 'write',
+      '--force',
+      '-r', fmaplabel, '-f', wipedfile])
+
   def _BuildBlob(self, pack, fdt, blob_type):
     """Build the blob data for a particular blob type.
 
@@ -1053,13 +1070,16 @@
                        'rw-nvram', 'ro-unused-1', 'ro-unused-2']:
             fdt.PutString(fdt_path, 'type', 'wiped')
             fdt.PutIntList(fdt_path, 'wipe-value', [0xff])
+            self._GenerateWiped(label, area['size'], 0xff)
         elif label == 'shared-data':
             fdt.PutString(fdt_path, 'type', 'wiped')
             fdt.PutIntList(fdt_path, 'wipe-value', [0])
+            self._GenerateWiped(label, area['size'], 0)
         elif label == 'vblock-dev':
             fdt_path = '/flash/rw-vblock-dev'
             fdt.PutString(fdt_path, 'type', 'wiped')
             fdt.PutIntList(fdt_path, 'wipe-value', [0xff])
+            self._GenerateWiped(label, area['size'], 0xff)
         elif label[:-1] == 'vblock-':
             fdt_path = '/flash/rw-'+slot+'-vblock'
             fdt.PutString(fdt_path, 'type', 'keyblock cbfs/rw/'+slot+'-boot')