Merge "dev: gcdb: display: decrease porch to fix screen flicker on sdm429"
diff --git a/platform/msm_shared/partition_parser.c b/platform/msm_shared/partition_parser.c
index 33fa458..7874adb 100755
--- a/platform/msm_shared/partition_parser.c
+++ b/platform/msm_shared/partition_parser.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -83,6 +83,8 @@
 struct partition_entry *partition_entries;
 static unsigned gpt_partitions_exist = 0;
 static unsigned partition_count;
+/* this is a pointer to ptn_entries_buffer */
+static unsigned char *new_buffer = NULL;
 
 unsigned partition_get_partition_count()
 {
@@ -273,10 +275,12 @@
 	unsigned int n = 0;	/* Counter for UTF-16 -> 8 conversion */
 	unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
 	/* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
-	unsigned long long partition_0;
 	uint64_t device_density;
 	uint8_t *data = NULL;
+	uint8_t *data_org_ptr = NULL;
 	uint32_t part_entry_cnt = block_size / ENTRY_SIZE;
+	uint32_t blocks_for_entries =
+			(NUM_PARTITIONS * PARTITION_ENTRY_SIZE)/block_size;
 
 	/* Get the density of the mmc device */
 
@@ -289,6 +293,7 @@
 		ret = -1;
 		goto end;
 	}
+	data_org_ptr = data;
 
 	/* Print out the GPT first */
 	ret = mmc_read(block_size, (unsigned int *)data, block_size);
@@ -330,19 +335,11 @@
 		}
 		parse_secondary_gpt = 0;
 	}
-	partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
 	/* Read GPT Entries */
 	for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
 		ASSERT(partition_count < NUM_PARTITIONS);
-		ret = mmc_read((partition_0 * block_size) + (i * block_size),
-						(uint32_t *) data, block_size);
-
-		if (ret) {
-			dprintf(CRITICAL,
-				"GPT: mmc read card failed reading partition entries.\n");
-			goto end;
-		}
-
+		
+		data = (new_buffer + (i * block_size));
 		for (j = 0; j < part_entry_cnt; j++) {
 			memcpy(&(partition_entries[partition_count].type_guid),
 			       &data[(j * partition_entry_size)],
@@ -368,13 +365,28 @@
 			    GET_LLWORD_FROM_BYTE(&data
 						 [(j * partition_entry_size) +
 						  LAST_LBA_OFFSET]);
+
+			/* If partition entry LBA is not valid, skip this entry
+				and parse next entry */
+			if (partition_entries[partition_count].first_lba < first_usable_lba
+				|| partition_entries[partition_count].last_lba >
+						(device_density/block_size -
+						(blocks_for_entries + GPT_HEADER_BLOCKS + 1))
+				|| partition_entries[partition_count].first_lba >
+					partition_entries[partition_count].last_lba)
+			{
+				dprintf(CRITICAL, "Partition entry(%d), lba not valid\n", j);
+				partition_count++;
+				continue;
+			}
+
 			partition_entries[partition_count].size =
 			    partition_entries[partition_count].last_lba -
 			    partition_entries[partition_count].first_lba + 1;
 			partition_entries[partition_count].attribute_flag =
 			    GET_LLWORD_FROM_BYTE(&data
 						 [(j * partition_entry_size) +
-						  ATTRIBUTE_FLAG_OFFSET]);
+				 		  ATTRIBUTE_FLAG_OFFSET]);
 
 			memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
 			memcpy(UTF16_name, &data[(j * partition_entry_size) +
@@ -394,8 +406,10 @@
 		}
 	}
 end:
-	if (data)
-		free(data);
+	if (data_org_ptr)
+		free(data_org_ptr);
+	if (new_buffer)
+		free(new_buffer);
 
 	return ret;
 }
@@ -1170,11 +1184,12 @@
 	uint32_t ret = 0;
 	uint32_t partitions_for_block = 0;
 	uint32_t blocks_to_read = 0;
-	unsigned char *new_buffer = NULL;
 	unsigned long long last_usable_lba = 0;
 	unsigned long long partition_0 = 0;
 	unsigned long long current_lba = 0;
 	uint32_t block_size = mmc_get_device_blocksize();
+	uint32_t blocks_for_entries =
+			(NUM_PARTITIONS * PARTITION_ENTRY_SIZE)/ block_size;
 	/* Get the density of the mmc device */
 	uint64_t device_density = mmc_get_device_capacity();
 
@@ -1222,10 +1237,19 @@
 	/*current lba and GPT lba should be same*/
 	if (!parse_secondary_gpt) {
 		if (current_lba != GPT_LBA) {
-			dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
+			dprintf(CRITICAL,"Primary GPT first usable LBA mismatch\n");
 			return 1;
 		}
 	}
+	else
+	{
+		if (current_lba != ((device_density/block_size) - 1))
+		{
+			dprintf(CRITICAL,"Secondary GPT first usable LBA mismatch\n");
+			return 1;
+		}
+	}
+
 	/*check for first lba should be with in the valid range*/
 	if (*first_usable_lba > (device_density/block_size)) {
 		dprintf(CRITICAL,"Invalid first_usable_lba\n");
@@ -1265,11 +1289,24 @@
 	if (!flashing_gpt) {
 		partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
 		/*start LBA should always be 2 in primary GPT*/
-		if(partition_0 != 0x2 && !parse_secondary_gpt) {
-			dprintf(CRITICAL, "Starting LBA mismatch\n");
-			ret = 1;
-			goto fail;
-
+		if (!parse_secondary_gpt)
+		{
+			if (partition_0 != 0x2)
+			{
+				dprintf(CRITICAL, "PrimaryGPT starting LBA mismatch\n");
+				ret = 1;
+				goto fail;
+			}
+		}
+		else
+		{
+			if (partition_0 != ((device_density/block_size) -
+						(blocks_for_entries + GPT_HEADER_BLOCKS)))
+			{
+				dprintf(CRITICAL, "BackupGPT starting LBA mismatch\n");
+				ret = 1;
+				goto fail;
+			}
 		}
 		/*read the partition entries to new_buffer*/
 		ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
@@ -1285,6 +1322,7 @@
 			dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
 			ret = 1;
 		}
+		return ret;
 	}
 fail:
 	free(new_buffer);
diff --git a/target/msm8952/oem_panel.c b/target/msm8952/oem_panel.c
index 7c869e1..7b26285 100644
--- a/target/msm8952/oem_panel.c
+++ b/target/msm8952/oem_panel.c
@@ -585,7 +585,9 @@
 		memcpy(phy_db->timing,
 						hx8394f_720p_video_timings, TIMING_SIZE);
 		pinfo->mipi.signature = HX8394F_720P_VIDEO_SIGNATURE;
-		pinfo->disable_wled_labibb = true;
+		if ((board_platform_id() == SDM429) || (board_platform_id() == SDM439) ||
+				(board_platform_id() == SDA429) || (board_platform_id() == SDA439))
+			pinfo->disable_wled_labibb = true;
 		break;
 	case BYD_1200P_VIDEO_PANEL:
 		panelstruct->paneldata    = &byd_1200p_video_panel_data;