Merge "KW fix: replaced sprintf with snprintf"
diff --git a/board/qca/arm/common/board_init.c b/board/qca/arm/common/board_init.c
index 19f1bf6..3102477 100644
--- a/board/qca/arm/common/board_init.c
+++ b/board/qca/arm/common/board_init.c
@@ -240,30 +240,32 @@
 #ifdef CONFIG_FLASH_PROTECT
 void board_flash_protect(void)
 {
-	unsigned int num_part;
+	int num_part;
 	int i;
+	int ret;
 #ifdef CONFIG_QCA_MMC
 	block_dev_desc_t *mmc_dev;
 	disk_partition_t info;
 
 	mmc_dev = mmc_get_dev(mmc_host.dev_num);
 	if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
-		ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header,
-					     gpt_head, 1, mmc_dev->blksz);
-		if (mmc_dev->block_read(mmc_dev->dev,
-		    (lbaint_t)GPT_PRIMARY_PARTITION_TABLE_LBA,
-		    1, gpt_head) == 1) {
-			num_part = le32_to_cpu(gpt_head->num_partition_entries);
-			for (i = 1; i <= num_part; i++) {
-				if (!get_partition_info_efi(mmc_dev, i, &info)
-				    && info.readonly
-				    && !mmc_write_protect(mmc_host.mmc,
-							  info.start,
-							  info.size, 1))
-					printf("\"%s\""
-						"-protected MMC partition\n",
-						info.name);
-			}
+		num_part = get_partition_count_efi(mmc_dev);
+		if (num_part < 0) {
+			printf("Both primary & backup GPT are invalid, skipping mmc write protection.\n");
+			return;
+		}
+
+		for (i = 1; i <= num_part; i++) {
+			ret = get_partition_info_efi(mmc_dev, i, &info);
+			if (ret == -1)
+				return;
+			if (!ret && info.readonly
+			    && !mmc_write_protect(mmc_host.mmc,
+						  info.start,
+						  info.size, 1))
+				printf("\"%s\""
+					"-protected MMC partition\n",
+					info.name);
 		}
 	}
 #endif
@@ -343,12 +345,12 @@
 }
 #endif
 
-void enable_caches(void)
+__weak void enable_caches(void)
 {
 	icache_enable();
 }
 
-void disable_caches(void)
+__weak void disable_caches(void)
 {
 	icache_disable();
 }
diff --git a/board/qca/arm/ipq806x/ipq806x.c b/board/qca/arm/ipq806x/ipq806x.c
index 57fc729..b23e609f 100644
--- a/board/qca/arm/ipq806x/ipq806x.c
+++ b/board/qca/arm/ipq806x/ipq806x.c
@@ -968,3 +968,15 @@
         set_l2_indirect_reg(L2ESR_IND_ADDR, val);
 #endif
 }
+
+void enable_caches(void)
+{
+	icache_enable();
+	dcache_enable();
+}
+
+void disable_caches(void)
+{
+	icache_disable();
+	dcache_disable();
+}
diff --git a/disk/part_efi.c b/disk/part_efi.c
index 57840b0..6d8eacf 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -340,6 +340,37 @@
 	return 0;
 }
 
+int get_partition_count_efi(block_dev_desc_t * dev_desc)
+{
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
+	gpt_entry *gpt_pte = NULL;
+
+	if (!dev_desc) {
+		printf("%s: Invalid Argument(s)\n", __func__);
+		return -1;
+	}
+
+	/* This function validates AND fills in the GPT header and PTE */
+	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
+			gpt_head, &gpt_pte) != 1) {
+		printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
+		if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
+				 gpt_head, &gpt_pte) != 1) {
+			printf("%s: *** ERROR: Invalid Backup GPT ***\n",
+			       __func__);
+			if (gpt_pte != NULL)
+				free(gpt_pte);
+			return -1;
+		} else {
+			printf("%s: ***        Using Backup GPT ***\n",
+			       __func__);
+		}
+	}
+
+	free(gpt_pte);
+	return le32_to_cpu(gpt_head->num_partition_entries);
+}
+
 /**
  * set_protective_mbr(): Set the EFI protective MBR
  * @param dev_desc - block device descriptor
diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index 0320942..6d401bc 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -385,6 +385,11 @@
 	if (err)
 		return err;
 	mmc->ocr = cmd.response[0];
+
+	/*1ms delay is added to give cards time to respond*/
+	if(!use_arg)
+		udelay(1000);
+
 	return 0;
 }
 
@@ -416,6 +421,7 @@
 	uint start;
 	int err;
 
+	udelay(100);
 	mmc->op_cond_pending = 0;
 	if (!(mmc->ocr & OCR_BUSY)) {
 		start = get_timer(0);
diff --git a/include/configs/ipq806x.h b/include/configs/ipq806x.h
index 06b2f1e..52129cf 100644
--- a/include/configs/ipq806x.h
+++ b/include/configs/ipq806x.h
@@ -334,7 +334,7 @@
  * Cache flush and invalidation based on L1 cache, so the cache line
  * size is configured to 64 */
 #define CONFIG_SYS_CACHELINE_SIZE  64
-#define CONFIG_SYS_DCACHE_OFF
+/*#define CONFIG_SYS_DCACHE_OFF*/
 
 /* Enabling this flag will report any L2 errors.
  * By default we are disabling it */
diff --git a/include/part.h b/include/part.h
index ce8b497..647a0e9 100644
--- a/include/part.h
+++ b/include/part.h
@@ -197,6 +197,7 @@
 	const char *name, disk_partition_t *info);
 void print_part_efi (block_dev_desc_t *dev_desc);
 int   test_part_efi (block_dev_desc_t *dev_desc);
+int get_partition_count_efi(block_dev_desc_t * dev_desc);
 
 /**
  * write_gpt_table() - Write the GUID Partition Table to disk
diff --git a/tools/sysupgrade.c b/tools/sysupgrade.c
index 35b01a9..82dece4 100644
--- a/tools/sysupgrade.c
+++ b/tools/sysupgrade.c
@@ -1427,28 +1427,41 @@
 int sec_image_auth()
 {
 	int fd, i, len;
-	char buf[256];
+	char *buf = NULL;
 
 	fd = open(SEC_AUTHENTICATE_FILE, O_RDWR);
 	if (-1 == fd) {
 		perror(SEC_AUTHENTICATE_FILE);
 		return 1;
 	}
-
+	buf = (char*)malloc(SIG_SIZE);
+	if (buf == NULL) {
+		perror("Memory allocation failed\n");
+		close(fd);
+		return 1;
+	}
 	for (i = 0; i < NO_OF_SECTIONS; i++) {
 		if (!sections[i].is_present) {
 			continue;
 		}
 
-		len = snprintf(buf, sizeof(buf), "%s %s", sections[i].img_code, sections[i].file);
+		len = snprintf(buf, SIG_SIZE, "%s %s", sections[i].img_code, sections[i].file);
+		if (len < 0 || len > SIG_SIZE) {
+			perror("Array out of Index\n");
+			free(buf);
+			close(fd);
+			return 1;
+		}
 		if (write(fd, buf, len) != len) {
 			perror("write");
+			free(buf);
 			close(fd);
 			printf("%s Image authentication failed\n", buf);
 			return 1;
 		}
 	}
 	close(fd);
+	free(buf);
 	return 0;
 }