Truncate source partitions to a multiple of 4 KiB.

When flashing partitions that aren't a multiple of 4 KiB using fastboot,
the last block of these partitions would not be padded with zeros. Full
and incremental paylaods will pad with zeros and write them on the new
target partitions, but the first incremental package after flashing a
device would fail.

This patch truncates the source partitions that aren't multiple of 4
KiB, discarding the data on the last partial 4 KiB block.

Bug: 28626303
TEST=`brillo_update_payload generate ..` a null-incremental payload;
boot.img includes mostly SOURCE_COPY and one REPLACE operation at the
end.

Change-Id: I25e0172fb074392488d96cd00e711c68adfa0e7f
diff --git a/scripts/brillo_update_payload b/scripts/brillo_update_payload
index a3eec4b..18f5b86 100755
--- a/scripts/brillo_update_payload
+++ b/scripts/brillo_update_payload
@@ -422,12 +422,19 @@
       part_file="${temp_raw}"
     fi
 
-    # delta_generator only supports images multiple of 4 KiB, so we pad with
-    # zeros if needed.
+    # delta_generator only supports images multiple of 4 KiB. For target images
+    # we pad the data with zeros if needed, but for source images we truncate
+    # down the data since the last block of the old image could be padded on
+    # disk with unknown data.
     filesize=$(stat -c%s "${part_file}")
     if [[ $(( filesize % 4096 )) -ne 0 ]]; then
-      echo "Rounding up partition ${part}.img to multiple of 4 KiB."
-      : $(( filesize = (filesize + 4095) & -4096 ))
+      if [[ "${partitions_array}" == "SRC_PARTITIONS" ]]; then
+        echo "Rounding DOWN partition ${part}.img to a multiple of 4 KiB."
+        : $(( filesize = filesize & -4096 ))
+      else
+        echo "Rounding UP partition ${part}.img to a multiple of 4 KiB."
+        : $(( filesize = (filesize + 4095) & -4096 ))
+      fi
       truncate --size="${filesize}" "${part_file}"
     fi