cros_bundle_firmware: store cbfs files

This change makes cros_bundle_firmware store all files discovered under
the directory specified by --cbfs-files option in CBFS.

BUG=none
BRANCH=tot
TEST=Tested by emerge-samus

Change-Id: I7b9e3e89af6ebb4b98eb5296d559174c0932ea47
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/302623
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/host/cros_bundle_firmware b/host/cros_bundle_firmware
index a1b6bb4..4f6dc1c 100755
--- a/host/cros_bundle_firmware
+++ b/host/cros_bundle_firmware
@@ -84,7 +84,8 @@
                   exynos_bl1=options.exynos_bl1, exynos_bl2=options.exynos_bl2,
                   skeleton=options.skeleton, ecrw=options.ecrw,
                   ecro=options.ecro, pdrw=options.pdrw, kernel=options.kernel,
-                  blobs=options.add_blob, skip_bmpblk=options.skip_bmpblk)
+                  blobs=options.add_blob, skip_bmpblk=options.skip_bmpblk,
+                  cbfs_files=options.cbfs_files)
   bundle.SetOptions(small=options.small, gbb_flags=options.gbb_flags,
                     force_rw=options.force_rw, force_efs=options.force_efs)
   if options.use_defaults:
@@ -173,6 +174,8 @@
       action='store', help='Hardware ID string to use')
   parser.add_option('--bmpblk', dest='bmpblk', type='string',
       action='store', help='Bitmap block to use')
+  parser.add_option('--cbfs-files', dest='cbfs_files', type='string',
+      action='store', help='Root directory of the files to be stored in CBFS')
   parser.add_option('--skip-bmpblk', action='store_true',
                     help='no bitmap block is needed')
   parser.add_option('-e', '--ec', dest='ecrw', type='string',
diff --git a/host/lib/bundle_firmware.py b/host/lib/bundle_firmware.py
index bf17a16..fc0ae57 100644
--- a/host/lib/bundle_firmware.py
+++ b/host/lib/bundle_firmware.py
@@ -162,7 +162,7 @@
                coreboot_elf=None,
                postload=None, seabios=None, exynos_bl1=None, exynos_bl2=None,
                skeleton=None, ecrw=None, ecro=None, pdrw=None,
-               kernel=None, blobs=None, skip_bmpblk=False):
+               kernel=None, blobs=None, skip_bmpblk=False, cbfs_files=None):
     """Set up files required for Bundle.
 
     Args:
@@ -183,6 +183,7 @@
       kernel: The filename of the kernel file if any.
       blobs: List of (type, filename) of arbitrary blobs.
       skip_bmpblk: True if no bmpblk is required
+      cbfs_files: Root directory of files to be stored in CBFS
     """
     self._board = board
     self.uboot_fname = uboot
@@ -201,6 +202,7 @@
     self.kernel_fname = kernel
     self.blobs = dict(blobs or ())
     self.skip_bmpblk = skip_bmpblk
+    self.cbfs_files = cbfs_files
 
   def SetOptions(self, small, gbb_flags, force_rw=False, force_efs=False):
     """Set up options supported by Bundle.
@@ -877,6 +879,14 @@
       raise CmdError("Unknown blob type '%s' required in flash map" %
           blob_type)
 
+  def _AddCbfsFiles(self, bootstub):
+    for dir, subs, files in os.walk(self.cbfs_files):
+      for file in files:
+        file = os.path.join(dir, file)
+        cbfs_name = file.replace(self.cbfs_files, '', 1).strip('/')
+        self._tools.Run('cbfstool', [bootstub, 'add', '-f', file,
+                                '-n', cbfs_name, '-t', '0x50', '-c', 'lzma'])
+
   def _CreateImage(self, gbb, fdt):
     """Create a full firmware image, along with various by-products.
 
@@ -989,6 +999,8 @@
             '-l', '%#x' % text_base, '-e', '%#x' % entry])
       self._tools.Run('cbfstool', [bootstub, 'add', '-f', fdt.fname,
           '-n', 'u-boot.dtb', '-t', '0xac'])
+      if self.cbfs_files:
+        self._AddCbfsFiles(bootstub)
       data = self._tools.ReadFile(bootstub)
       bootstub_copy = os.path.join(self._tools.outdir, 'coreboot-8mb.rom')
       self._tools.WriteFile(bootstub_copy, data)