save file block allocations in target_files

make_ext4fs can now output a file listing the blocks used for each
file in the image.  Request this file and save it in the target_files;
it will be used for future improvements to block OTAs.

Bug: 16984795
Change-Id: Id1e60465e3b5a9d126a7934b4d089cf34d8fec44
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index 7be1929..568a3f1 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -49,23 +49,38 @@
 def AddSystem(output_zip, sparse=True, prefix="IMAGES/"):
   """Turn the contents of SYSTEM into a system image and store it in
   output_zip."""
-  data = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse)
+  block_list = tempfile.NamedTemporaryFile()
+  data = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse,
+                     block_list=block_list.name)
   common.ZipWriteStr(output_zip, prefix + "system.img", data)
+  with open(block_list.name, "rb") as f:
+    block_list_data = f.read()
+  common.ZipWriteStr(output_zip, prefix + "system.map", block_list_data)
+  block_list.close()
 
-def BuildSystem(input_dir, info_dict, sparse=True, map_file=None):
+def BuildSystem(input_dir, info_dict, sparse=True, map_file=None,
+                block_list=None):
   return CreateImage(input_dir, info_dict, "system",
-                     sparse=sparse, map_file=map_file)
+                     sparse=sparse, map_file=map_file, block_list=block_list)
 
 def AddVendor(output_zip, sparse=True, prefix="IMAGES/"):
-  data = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse)
+  block_list = tempfile.NamedTemporaryFile()
+  data = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse,
+                     block_list=block_list.name)
   common.ZipWriteStr(output_zip, prefix + "vendor.img", data)
+  with open(block_list.name, "rb") as f:
+    block_list_data = f.read()
+  common.ZipWriteStr(output_zip, prefix + "vendor.map", block_list_data)
+  block_list.close()
 
-def BuildVendor(input_dir, info_dict, sparse=True, map_file=None):
+def BuildVendor(input_dir, info_dict, sparse=True, map_file=None,
+                block_list=None):
   return CreateImage(input_dir, info_dict, "vendor",
-                     sparse=sparse, map_file=map_file)
+                     sparse=sparse, map_file=map_file, block_list=block_list)
 
 
-def CreateImage(input_dir, info_dict, what, sparse=True, map_file=None):
+def CreateImage(input_dir, info_dict, what, sparse=True, map_file=None,
+                block_list=None):
   print "creating " + what + ".img..."
 
   img = tempfile.NamedTemporaryFile()
@@ -104,7 +119,8 @@
   succ = build_image.BuildImage(os.path.join(input_dir, what),
                                 image_props, img.name,
                                 fs_config=fs_config,
-                                fc_config=fc_config)
+                                fc_config=fc_config,
+                                block_list=block_list)
   assert succ, "build " + what + ".img image failed"
 
   mapdata = None
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 712e0cd..5ae8d3c 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -209,7 +209,8 @@
 
 def BuildImage(in_dir, prop_dict, out_file,
                fs_config=None,
-               fc_config=None):
+               fc_config=None,
+               block_list=None):
   """Build an image to out_file from in_dir with property prop_dict.
 
   Args:
@@ -252,6 +253,8 @@
       build_command.extend(["-T", str(prop_dict["timestamp"])])
     if fs_config is not None:
       build_command.extend(["-C", fs_config])
+    if block_list is not None:
+      build_command.extend(["-B", block_list])
     if fc_config is not None:
       build_command.append(fc_config)
     elif "selinux_fc" in prop_dict:
diff --git a/tools/releasetools/img_from_target_files.py b/tools/releasetools/img_from_target_files.py
index 6463047..4b88e73 100755
--- a/tools/releasetools/img_from_target_files.py
+++ b/tools/releasetools/img_from_target_files.py
@@ -90,6 +90,7 @@
       if images:
         for i in images:
           if bootable_only and i not in ("boot.img", "recovery.img"): continue
+          if not i.endswith(".img"): continue
           with open(os.path.join(images_path, i), "r") as f:
             common.ZipWriteStr(output_zip, i, f.read())
         done = True