Merge "WindCharger: Fix u-boot lower 16MB boundary checks"
diff --git a/board/atheros/board953x/flash.c b/board/atheros/board953x/flash.c
index db47a41..3b17afb 100755
--- a/board/atheros/board953x/flash.c
+++ b/board/atheros/board953x/flash.c
@@ -38,6 +38,8 @@
 	flash_info->flash_id = FLASH_M25P64;
 	flash_info->size = CFG_FLASH_SIZE; /* bytes */
 	flash_info->sector_count = flash_info->size / CFG_FLASH_SECTOR_SIZE;
+	flash_info->p1_size = flash_info->size >> 1;
+	flash_info->p1_sector_count = flash_info->sector_count >> 1;
 
 	for (i = 0; i < flash_info->sector_count; i++) {
 		flash_info->start[i] = CFG_FLASH_BASE +
diff --git a/common/flash.c b/common/flash.c
index a64bc98..b61f491 100755
--- a/common/flash.c
+++ b/common/flash.c
@@ -34,6 +34,26 @@
  * Functions
  */
 
+/*
+ * Get flash erase sector count.
+ */
+ushort
+get_sector_count(flash_info_t *info)
+{
+	if (info->p1_sector_count) {
+		return info->p1_sector_count;
+	}
+	return info->sector_count;
+}
+
+ulong
+get_size(flash_info_t *info) {
+	if (info->p1_size) {
+		return info->p1_size;
+	}
+	return info->size;
+}
+
 /*-----------------------------------------------------------------------
  * Set protection status for monitor sectors
  *
@@ -43,8 +63,8 @@
 void
 flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
 {
-	ulong b_end = info->start[0] + info->size - 1;	/* bank end address */
-	short s_end = info->sector_count - 1;	/* index of last sector */
+	ulong b_end = info->start[0] + get_size(info) - 1;	/* bank end address */
+	short s_end = get_sector_count(info) - 1;	/* index of last sector */
 	int i;
 
 	debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n",
@@ -53,7 +73,7 @@
 		from, to);
 
 	/* Do nothing if input data is bad. */
-	if (info->sector_count == 0 || info->size == 0 || to < from) {
+	if (get_sector_count(info) == 0 || get_size(info) == 0 || to < from) {
 		return;
 	}
 
@@ -65,7 +85,7 @@
 		return;
 	}
 
-	for (i=0; i<info->sector_count; ++i) {
+	for (i=0; i<get_sector_count(info); ++i) {
 		ulong end;		/* last address in current sect	*/
 
 		end = (i == s_end) ? b_end : info->start[i + 1] - 1;
@@ -109,10 +129,10 @@
 		    addr >= info->start[0] &&
 		    /* WARNING - The '- 1' is needed if the flash
 		     * is at the end of the address space, since
-		     * info->start[0] + info->size wraps back to 0.
+		     * info->start[0] + get_size(info) wraps back to 0.
 		     * Please don't change this unless you understand this.
 		     */
-		    addr <= info->start[0] + info->size - 1) {
+		    addr <= info->start[0] + get_size(info) - 1) {
 			return (info);
 		}
 	}
@@ -155,9 +175,9 @@
 	}
 
 	for (info = info_first; info <= info_last; ++info) {
-		ulong b_end = info->start[0] + info->size;	/* bank end addr */
-		short s_end = info->sector_count - 1;
-		for (i=0; i<info->sector_count; ++i) {
+		ulong b_end = info->start[0] + get_size(info);	/* bank end addr */
+		short s_end = get_sector_count(info) - 1;
+		for (i=0; i<get_sector_count(info); ++i) {
 			ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
 
 			if ((end >= info->start[i]) && (addr < e_addr) &&
@@ -171,7 +191,7 @@
 	for (info = info_first; info <= info_last && cnt>0; ++info) {
 		ulong len;
 
-		len = info->start[0] + info->size - addr;
+		len = info->start[0] + get_size(info) - addr;
 		if (len > cnt)
 			len = cnt;
 		if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
diff --git a/include/flash.h b/include/flash.h
index 79031a4..c4b362e 100755
--- a/include/flash.h
+++ b/include/flash.h
@@ -49,6 +49,8 @@
 	ushort	interface;		/* used for x8/x16 adjustments		*/
 	ushort	legacy_unlock;		/* support Intel legacy (un)locking	*/
 #endif
+	ulong	p1_size;		/* Size of lower partition.		*/
+	ushort	p1_sector_count;	/* Erase units for lower partition.	*/
 } flash_info_t;
 
 /*