app: aboot: Check to avoid overflow, while roundoff operation.

This change return UNIT_MAX in ROUND_TO_PAGE operation, if overflow
occurs.

Change-Id: I93843959c6e5e131737a0c7db8d56c81d30da285
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index df71a56..437c45b 100755
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -1011,7 +1011,6 @@
 		return 0;
 }
 
-#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
 
 BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
 #if DEVICE_TREE
diff --git a/app/aboot/mdtp.h b/app/aboot/mdtp.h
index adb3878..4e6fa73 100644
--- a/app/aboot/mdtp.h
+++ b/app/aboot/mdtp.h
@@ -40,7 +40,6 @@
 #define INITIAL_DELAY_MSECONDS      5000
 #define INVALID_PIN_DELAY_MSECONDS  5000
 
-#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
 #define MDTP_FWLOCK_BLOCK_SIZE          (1024*1024*16)
 #define MDTP_FWLOCK_MAX_FILES           (100)
 #define MDTP_FWLOCK_MAX_FILE_NAME_LEN   (100)
diff --git a/app/aboot/recovery.c b/app/aboot/recovery.c
index cecfb27..a4048ab 100644
--- a/app/aboot/recovery.c
+++ b/app/aboot/recovery.c
@@ -43,14 +43,13 @@
 #include <partition_parser.h>
 #include <mmc.h>
 #include <malloc.h>
-
+#include <stdlib.h>
 #include "recovery.h"
 #include "bootimg.h"
 #include "smem.h"
 
 #define BOOT_FLAGS	1
 #define UPDATE_STATUS	2
-#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
 
 static const int MISC_PAGES = 3;			// number of pages to save
 static const int MISC_COMMAND_PAGE = 1;		// bootloader command is this page
diff --git a/include/stdlib.h b/include/stdlib.h
index 22dcf4c..4a90831 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -49,6 +49,9 @@
 #define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
 #define ROUNDDOWN(a, b) ((a) & ~((b)-1))
 
+/* Macro returns UINT_MAX in case of overflow */
+#define ROUND_TO_PAGE(x,y) (ROUNDUP((x),(y)+1) < (x))?UINT_MAX:ROUNDUP((x),(y)+1)
+
 /* allocate a buffer on the stack aligned and padded to the cpu's cache line size */
 #define STACKBUF_DMA_ALIGN(var, size) \
 	uint8_t __##var[(size) + CACHE_LINE] __attribute__((aligned(CACHE_LINE))); uint8_t *var = (uint8_t *)(ROUNDUP((addr_t)__##var, CACHE_LINE))