fix image size tests

There are currently two errors in the way we test the size of built
images against the size of the partition on the hardware:

- the limits in BoardConfig.mk are set with the data size only, but
  images contain an extra 64 bytes per 2048-byte page.  This means we
  think the partition is about 1/32 smaller than it really is.

- when we deliver a build via OTA, the system partition ends up with
  one more file than when it's flashed via fastboot.  That file is a
  copy of the recovery image.  In order to be able to OTA a build, we
  need to make sure the system partition has enough room for all the
  system files plus the recovery image as well.

For the kila system partition, these errors are roughly the same order
of magnitude -- about 2MB, one in the "safe" direction, one in the
"unsafe" direction.  This change fixes both to give us a more accurate
notion of how close we are to the limit.

Make the build emit a warning (but not fail) when the size is within
32kb of the limit.

Also, include the values of the partition size limits in an info file
in the target-files package, so post-processing tools can use them
without parsing the BoardConfig.mk file.
diff --git a/core/definitions.mk b/core/definitions.mk
index 1d9dd74..485c2ae 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1540,8 +1540,16 @@
 $(error HOST_OS must define get-file-size)
 endif
 
-# $(1): The file to check (often $@)
-# $(2): The maximum size, in decimal bytes
+# Convert a partition data size (eg, as reported in /proc/mtd) to the
+# size of the image used to flash that partition (which includes a
+# 64-byte spare area for each 2048-byte page).
+# $(1): the partition data size
+define image-size-from-data-size
+$(shell echo $$(($(1) / 2048 * (2048+64))))
+endef
+
+# $(1): The file(s) to check (often $@)
+# $(2): The maximum total image size, in decimal bytes
 #
 # If $(2) is empty, evaluates to "true"
 #
@@ -1550,19 +1558,21 @@
 # next whole flash block size.
 define assert-max-file-size
 $(if $(2), \
-  fileSize=`$(call get-file-size,$(1))`; \
-  maxSize=$(2); \
-  onePct=`expr "(" $$maxSize + 99 ")" / 100`; \
-  onePct=`expr "(" "(" $$onePct + $(BOARD_FLASH_BLOCK_SIZE) - 1 ")" / \
-          $(BOARD_FLASH_BLOCK_SIZE) ")" "*" $(BOARD_FLASH_BLOCK_SIZE)`; \
-  reserve=`expr 2 "*" $(BOARD_FLASH_BLOCK_SIZE)`; \
-  if [ "$$onePct" -gt "$$reserve" ]; then \
-      reserve="$$onePct"; \
+  size=$$(for i in $(1); do $(call get-file-size,$$i); done); \
+  total=$$(( $$( echo "$$size" | tr '\n' + ; echo 0 ) )); \
+  printname=$$(echo -n "$(1)" | tr " " +); \
+  echo "$$printname total size is $$total"; \
+  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
+  twoblocks=$$((img_blocksize * 2)); \
+  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
+  reserve=$$((twoblocks > onepct ? twoblocks : onepct)); \
+  maxsize=$$(($(2) - reserve)); \
+  if [ "$$total" -gt "$$maxsize" ]; then \
+    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
+    false; \
   fi; \
-  maxSize=`expr $$maxSize - $$reserve`; \
-  if [ "$$fileSize" -gt "$$maxSize" ]; then \
-      echo "error: $(1) too large ($$fileSize > [$(2) - $$reserve])"; \
-      false; \
+  if [ "$$total" -gt $$((maxsize - 32768)) ]; then \
+    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
   fi \
  , \
   true \