platform: msm_shared: Clean up partition parser

Remove partition parser dependency on mmc host & card structures.
Partition parser need not be aware of these details, instead use
mmc api's to read/write to the card.

Change-Id: I4a93ee06b90809b30204c8c78fe222a5db0edf9a
diff --git a/platform/msm_shared/partition_parser.c b/platform/msm_shared/partition_parser.c
index c80f7d3..1b554ad 100644
--- a/platform/msm_shared/partition_parser.c
+++ b/platform/msm_shared/partition_parser.c
@@ -31,6 +31,26 @@
 #include "mmc.h"
 #include "partition_parser.h"
 
+static uint32_t mmc_boot_read_gpt();
+static uint32_t mmc_boot_read_mbr();
+static void mbr_fill_name(struct partition_entry *partition_ent,
+						  uint32_t type);
+static uint32_t partition_verify_mbr_signature(uint32_t size,
+											   uint8_t *buffer);
+static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition,
+									   uint32_t *partition_type);
+
+static uint32_t partition_get_type(uint32_t size, uint8_t *partition,
+								   uint32_t *partition_type);
+static uint32_t partition_parse_gpt_header(uint8_t *buffer,
+										   uint64_t *first_usable_lba,
+										   uint32_t *partition_entry_size,
+										   uint32_t *header_size,
+										   uint32_t *max_partition_count);
+
+static uint32_t write_mbr(uint32_t, uint8_t *mbrImage);
+static uint32_t write_gpt(uint32_t size, uint8_t *gptImage);
+
 char *ext3_partitions[] =
     { "system", "userdata", "persist", "cache", "tombstones" };
 char *vfat_partitions[] = { "modem", "mdm", "NONE" };
@@ -42,49 +62,43 @@
 unsigned gpt_partitions_exist = 0;
 unsigned partition_count = 0;
 
-//TODO: Remove the dependency of mmc in these functions
-unsigned int
-partition_read_table(void *mmc_host,
-		     struct mmc_card *mmc_card)
+unsigned int partition_read_table()
 {
 	unsigned int ret;
 
 	/* Read MBR of the card */
-	ret = mmc_boot_read_mbr(mmc_host, mmc_card);
-	if (ret != MMC_BOOT_E_SUCCESS) {
+	ret = mmc_boot_read_mbr();
+	if (ret) {
 		dprintf(CRITICAL, "MMC Boot: MBR read failed!\n");
-		return MMC_BOOT_E_FAILURE;
+		return 1;
 	}
 
 	/* Read GPT of the card if exist */
 	if (gpt_partitions_exist) {
-		ret = mmc_boot_read_gpt(mmc_host, mmc_card);
-		if (ret != MMC_BOOT_E_SUCCESS) {
+		ret = mmc_boot_read_gpt();
+		if (ret) {
 			dprintf(CRITICAL, "MMC Boot: GPT read failed!\n");
-			return MMC_BOOT_E_FAILURE;
+			return 1;
 		}
 	}
-	return MMC_BOOT_E_SUCCESS;
+	return 0;
 }
 
 /*
  * Read MBR from MMC card and fill partition table.
  */
-unsigned int
-mmc_boot_read_mbr(struct mmc_host *mmc_host,
-		  struct mmc_card *mmc_card)
+static unsigned int mmc_boot_read_mbr()
 {
 	unsigned char buffer[BLOCK_SIZE];
 	unsigned int dtype;
 	unsigned int dfirstsec;
 	unsigned int EBR_first_sec;
 	unsigned int EBR_current_sec;
-	int ret = MMC_BOOT_E_SUCCESS;
+	int ret = 0;
 	int idx, i;
 
 	/* Print out the MBR first */
-	ret = mmc_boot_read_from_card(mmc_host, mmc_card, 0,
-				      BLOCK_SIZE, (unsigned int *)buffer);
+	ret = mmc_read(0, (unsigned int *)buffer, BLOCK_SIZE);
 	if (ret) {
 		dprintf(CRITICAL, "Could not read partition from mmc\n");
 		return ret;
@@ -136,17 +150,15 @@
 	EBR_first_sec = dfirstsec;
 	EBR_current_sec = dfirstsec;
 
-	ret = mmc_boot_read_from_card(mmc_host, mmc_card,
-				      (EBR_first_sec * 512),
-				      BLOCK_SIZE, (unsigned int *)buffer);
-	if (ret) {
+	ret = mmc_read((EBR_first_sec * 512), (unsigned int *)buffer, BLOCK_SIZE);
+	if (ret)
 		return ret;
-	}
+
 	/* Loop to parse the EBR */
 	for (i = 0;; i++) {
 		ret = partition_verify_mbr_signature(BLOCK_SIZE, buffer);
 		if (ret) {
-			ret = MMC_BOOT_E_SUCCESS;
+			ret = 0;
 			break;
 		}
 		partition_entries[partition_count].attribute_flag =
@@ -175,13 +187,11 @@
 		/* More EBR to follow - read in the next EBR sector */
 		dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec
 			+ dfirstsec);
-		ret = mmc_boot_read_from_card(mmc_host, mmc_card,
-					      ((EBR_first_sec +
-						dfirstsec) * 512), BLOCK_SIZE,
-					      (unsigned int *)buffer);
-		if (ret) {
+		ret = mmc_read(((EBR_first_sec + dfirstsec) * 512),(unsigned int *)buffer,
+						BLOCK_SIZE);
+		if (ret)
 			return ret;
-		}
+
 		EBR_current_sec = EBR_first_sec + dfirstsec;
 	}
 	return ret;
@@ -190,12 +200,10 @@
 /*
  * Read GPT from MMC and fill partition table
  */
-unsigned int
-mmc_boot_read_gpt(struct mmc_host *mmc_host,
-		  struct mmc_card *mmc_card)
+static unsigned int mmc_boot_read_gpt()
 {
 
-	int ret = MMC_BOOT_E_SUCCESS;
+	int ret = 0;
 	unsigned int header_size;
 	unsigned long long first_usable_lba;
 	unsigned long long backup_header_lba;
@@ -209,15 +217,18 @@
 	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;
+
 	partition_count = 0;
 
+	/* Get the density of the mmc device */
+
+	device_density = mmc_get_device_capacity();
+
 	/* Print out the GPT first */
-	ret = mmc_boot_read_from_card(mmc_host, mmc_card,
-				      PROTECTIVE_MBR_SIZE,
-				      BLOCK_SIZE, (unsigned int *)data);
-	if (ret){
+	ret = mmc_read(PROTECTIVE_MBR_SIZE, (unsigned int *)data, BLOCK_SIZE);
+	if (ret)
 		dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
-	}
 
 	ret = partition_parse_gpt_header(data, &first_usable_lba,
 					 &partition_entry_size, &header_size,
@@ -228,14 +239,12 @@
 		/* Check the backup gpt */
 
 		/* Get size of MMC */
-		card_size_sec = (mmc_card->capacity) / BLOCK_SIZE;
+		card_size_sec = (device_density) / BLOCK_SIZE;
 		ASSERT (card_size_sec > 0);
 
 		backup_header_lba = card_size_sec - 1;
-		ret =
-		    mmc_boot_read_from_card(mmc_host, mmc_card,
-					    (backup_header_lba * BLOCK_SIZE),
-					    BLOCK_SIZE, (unsigned int *)data);
+		ret = mmc_read((backup_header_lba * BLOCK_SIZE), (unsigned int *)data,
+						BLOCK_SIZE);
 
 		if (ret) {
 			dprintf(CRITICAL,
@@ -257,10 +266,8 @@
 	/* Read GPT Entries */
 	for (i = 0; i < (ROUNDUP(max_partition_count, 4)) / 4; i++) {
 		ASSERT(partition_count < NUM_PARTITIONS);
-		ret = mmc_boot_read_from_card(mmc_host, mmc_card,
-					      (partition_0 * BLOCK_SIZE) +
-					      (i * BLOCK_SIZE),
-					      BLOCK_SIZE, (uint32_t *) data);
+		ret = mmc_read((partition_0 * BLOCK_SIZE) + (i * BLOCK_SIZE),
+						(uint32_t *) data, BLOCK_SIZE);
 
 		if (ret) {
 			dprintf(CRITICAL,
@@ -384,9 +391,7 @@
 }
 
 /* Write the MBR/EBR to the MMC. */
-unsigned int
-write_mbr(unsigned size, unsigned char *mbrImage,
-	  struct mmc_host *mmc_host, struct mmc_card *mmc_card)
+static unsigned int write_mbr(unsigned size, unsigned char *mbrImage)
 {
 	unsigned int ret;
 
@@ -403,7 +408,7 @@
 		goto end;
 	}
 	/* Re-read the MBR partition into mbr table */
-	ret = mmc_boot_read_mbr(mmc_host, mmc_card);
+	ret = mmc_boot_read_mbr();
 	if (ret) {
 		dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
 		goto end;
@@ -472,7 +477,7 @@
 			  unsigned int partition_array_start,
 			  unsigned int array_size)
 {
-	unsigned int ret = MMC_BOOT_E_INVAL;
+	unsigned int ret = 1;
 	unsigned long long partition_entry_lba;
 	unsigned long long partition_entry_array_start_location;
 
@@ -493,10 +498,8 @@
 }
 
 static void
-patch_gpt(unsigned char *gptImage,
-	  struct mmc_card *mmc_card,
-	  unsigned int array_size,
-	  unsigned int max_part_count, unsigned int part_entry_size)
+patch_gpt(unsigned char *gptImage, uint64_t density, unsigned int array_size,
+		  unsigned int max_part_count, unsigned int part_entry_size)
 {
 	unsigned int partition_entry_array_start;
 	unsigned char *primary_gpt_header;
@@ -508,7 +511,7 @@
 	unsigned int crc_value;
 
 	/* Get size of MMC */
-	card_size_sec = (mmc_card->capacity) / 512;
+	card_size_sec = (density) / 512;
 	/* Working around cap at 4GB */
 	if (card_size_sec == 0) {
 		card_size_sec = 4 * 1024 * 1024 * 2 - 1;
@@ -569,11 +572,9 @@
 /*
  * Write the GPT to the MMC.
  */
-unsigned int
-write_gpt(unsigned size, unsigned char *gptImage,
-	  struct mmc_host *mmc_host, struct mmc_card *mmc_card)
+static unsigned int write_gpt(unsigned size, unsigned char *gptImage)
 {
-	unsigned int ret = MMC_BOOT_E_INVAL;
+	unsigned int ret = 1;
 	unsigned int header_size;
 	unsigned long long first_usable_lba;
 	unsigned long long backup_header_lba;
@@ -586,6 +587,7 @@
 	unsigned int partition_entry_array_size;
 	unsigned long long primary_header_location;	/* address on the emmc card */
 	unsigned long long secondary_header_location;	/* address on the emmc card */
+	uint64_t device_density;
 
 	/* Verify that passed block has a valid GPT primary header */
 	primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
@@ -598,6 +600,10 @@
 		goto end;
 	}
 
+	/* Get the density of the mmc device */
+
+	device_density = mmc_get_device_capacity();
+
 	/* Verify that passed block has a valid backup GPT HEADER */
 	partition_entry_array_size = partition_entry_size * max_partition_count;
 	if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
@@ -616,11 +622,11 @@
 	}
 
 	/* Patching the primary and the backup header of the GPT table */
-	patch_gpt(gptImage, mmc_card, partition_entry_array_size,
+	patch_gpt(gptImage, device_density, partition_entry_array_size,
 		  max_partition_count, partition_entry_size);
 
 	/* Erasing the eMMC card before writing */
-	ret = mmc_erase_card(0x00000000, mmc_card->capacity);
+	ret = mmc_erase_card(0x00000000, device_density);
 	if (ret) {
 		dprintf(CRITICAL, "Failed to erase the eMMC card\n");
 		goto end;
@@ -677,7 +683,7 @@
 
 	/* Re-read the GPT partition table */
 	dprintf(INFO, "Re-reading the GPT Partition Table\n");
-	ret = mmc_boot_read_gpt(mmc_host, mmc_card);
+	ret = mmc_boot_read_gpt();
 	if (ret) {
 		dprintf(CRITICAL,
 			"GPT: Failure to re- read the GPT Partition table\n");
@@ -693,10 +699,8 @@
 
 unsigned int write_partition(unsigned size, unsigned char *partition)
 {
-	unsigned int ret = MMC_BOOT_E_INVAL;
+	unsigned int ret = 1;
 	unsigned int partition_type;
-	struct mmc_host *mmc_host;
-	struct mmc_card *mmc_card;
 
 	if (partition == 0) {
 		dprintf(CRITICAL, "NULL partition\n");
@@ -704,28 +708,24 @@
 	}
 
 	ret = partition_get_type(size, partition, &partition_type);
-	if (ret != MMC_BOOT_E_SUCCESS) {
+	if (ret)
 		goto end;
-	}
-
-	mmc_host = get_mmc_host();
-	mmc_card = get_mmc_card();
 
 	switch (partition_type) {
 	case PARTITION_TYPE_MBR:
 		dprintf(INFO, "Writing MBR partition\n");
-		ret = write_mbr(size, partition, mmc_host, mmc_card);
+		ret = write_mbr(size, partition);
 		break;
 
 	case PARTITION_TYPE_GPT:
 		dprintf(INFO, "Writing GPT partition\n");
-		ret = write_gpt(size, partition, mmc_host, mmc_card);
+		ret = write_gpt(size, partition);
 		dprintf(CRITICAL, "Re-Flash all the partitions\n");
 		break;
 
 	default:
 		dprintf(CRITICAL, "Invalid partition\n");
-		ret = MMC_BOOT_E_INVAL;
+		ret = 1;
 		goto end;
 	}
 
@@ -862,23 +862,23 @@
 	}
 }
 
-unsigned int
+static unsigned int
 partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
 {
 	/* Avoid checking past end of buffer */
 	if ((TABLE_SIGNATURE + 1) > size) {
-		return MMC_BOOT_E_FAILURE;
+		return 1;
 	}
 	/* Check to see if signature exists */
 	if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
 	    (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
 		dprintf(CRITICAL, "MBR signature does not match.\n");
-		return MMC_BOOT_E_FAILURE;
+		return 1;
 	}
-	return MMC_BOOT_E_SUCCESS;
+	return 0;
 }
 
-unsigned int
+static unsigned int
 mbr_partition_get_type(unsigned size, unsigned char *partition,
 		       unsigned int *partition_type)
 {
@@ -890,14 +890,14 @@
 
 	*partition_type = partition[type_offset];
  end:
-	return MMC_BOOT_E_SUCCESS;
+	return 0;
 }
 
-unsigned int
+static unsigned int
 partition_get_type(unsigned size, unsigned char *partition,
 		   unsigned int *partition_type)
 {
-	unsigned int ret = MMC_BOOT_E_SUCCESS;
+	unsigned int ret = 0;
 
 	/*
 	 * If the block contains the MBR signature, then it's likely either
@@ -907,14 +907,14 @@
 
 	/* First check the MBR signature */
 	ret = partition_verify_mbr_signature(size, partition);
-	if (ret == MMC_BOOT_E_SUCCESS) {
+	if (!ret) {
 		unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
 
 		/* MBR signature verified.  This could be MBR, MBR + EBR, or GPT */
 		ret =
 		    mbr_partition_get_type(size, partition,
 					   &mbr_partition_type);
-		if (ret != MMC_BOOT_E_SUCCESS) {
+		if (ret) {
 			dprintf(CRITICAL, "Cannot get TYPE of partition");
 		} else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
 			*partition_type = PARTITION_TYPE_GPT;
@@ -936,7 +936,7 @@
  * Parse the gpt header and get the required header fields
  * Return 0 on valid signature
  */
-unsigned int
+static unsigned int
 partition_parse_gpt_header(unsigned char *buffer,
 			   unsigned long long *first_usable_lba,
 			   unsigned int *partition_entry_size,