avbtool: Keep FEC data around when using --keep_hashtree with erase_footer. am: fbb61fa831 am: 3ffdb43a76
am: eef70611e1

Change-Id: I63353a6d66cfa4654e2d1acb0ab2798a1c992d44
diff --git a/README b/README
index 1c63f3f..1439785 100644
--- a/README
+++ b/README
@@ -227,6 +227,7 @@
      [--hash_algorithm HASH_ALG] [--salt HEX] [--block_size SIZE]               \
      [--include_descriptors_from_footer /path/to/image.bin]                     \
      [--setup_rootfs_from_kernel /path/to/image.bin]                            \
+     [--generate_fec] [--fec_num_roots FEC_NUM_ROOTS]                           \
      [--signing_helper /path/to/external/signer]
 
 The integrity footer on an image can be removed from an image. The
diff --git a/avbtool b/avbtool
index 3da0279..b62bd03 100755
--- a/avbtool
+++ b/avbtool
@@ -1622,7 +1622,7 @@
 
     Arguments:
       image_filename: File to erase a footer from.
-      keep_hashtree: If True, keep the hashtree around.
+      keep_hashtree: If True, keep the hashtree and FEC around.
 
     Raises:
       AvbError: If there's no footer in the image.
@@ -1640,12 +1640,17 @@
       new_image_size = footer.original_image_size
     else:
       # If requested to keep the hashtree, search for a hashtree
-      # descriptor to figure out the location and size of the hashtree.
+      # descriptor to figure out the location and size of the hashtree
+      # and FEC.
       for desc in descriptors:
         if isinstance(desc, AvbHashtreeDescriptor):
           # The hashtree is always just following the main data so the
           # new size is easily derived.
           new_image_size = desc.tree_offset + desc.tree_size
+          # If the image has FEC codes, also keep those.
+          if desc.fec_offset > 0:
+            fec_end = desc.fec_offset + desc.fec_size
+            new_image_size = max(new_image_size, fec_end)
           break
       if not new_image_size:
         raise AvbError('Requested to keep hashtree but no hashtree '
@@ -2874,7 +2879,7 @@
                             type=argparse.FileType('rwb+'),
                             required=True)
     sub_parser.add_argument('--keep_hashtree',
-                            help='Keep the hashtree in the image',
+                            help='Keep the hashtree and FEC in the image',
                             action='store_true')
     sub_parser.set_defaults(func=self.erase_footer)
 
diff --git a/test/avbtool_unittest.cc b/test/avbtool_unittest.cc
index 273f9ca..b089e3c 100644
--- a/test/avbtool_unittest.cc
+++ b/test/avbtool_unittest.cc
@@ -921,14 +921,15 @@
       "'root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)'\n",
       InfoImage(vbmeta_dmv_path));
 
-  // Check that the footer is correctly erased and the hashtree
-  // remains - see above for why the constant 1069056 is used.
+  // Check that the footer is correctly erased and the hashtree and
+  // FEC data remains. The constant 1085440 is used because it's where
+  // the FEC data ends (it's at offset 1069056 and size 16384).
   EXPECT_COMMAND(0,
                  "./avbtool erase_footer --image %s --keep_hashtree",
                  rootfs_path.value().c_str());
   int64_t erased_footer_file_size;
   ASSERT_TRUE(base::GetFileSize(rootfs_path, &erased_footer_file_size));
-  EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), 1069056UL);
+  EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), 1085440UL);
 }
 
 TEST_F(AvbToolTest, AddHashtreeFooterFEC) {