platform/msm_shared/partition_parser.c
unsigned int
write_gpt(unsigned size, unsigned char *gptImage,
	  struct mmc_boot_host *mmc_host, struct mmc_boot_card *mmc_card,
	  int erase)
{
	unsigned int ret = MMC_BOOT_E_INVAL;
	unsigned int header_size;
	unsigned long long first_usable_lba;
	unsigned long long backup_header_lba;
	unsigned int max_partition_count = 0;
	unsigned int partition_entry_size;
	unsigned int partition_entry_array_start;
	unsigned char *primary_gpt_header;
	unsigned char *secondary_gpt_header;
	unsigned int offset;
	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 */

	/* Verify that passed block has a valid GPT primary header */
	primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
	ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
					 &partition_entry_size, &header_size,
					 &max_partition_count);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Primary signature invalid cannot write GPT\n");
		goto end;
	}

	/* 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) {
		partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
	}
	offset = (2 * partition_entry_array_size);
	secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
	ret =
	    partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
				       &partition_entry_size, &header_size,
				       &max_partition_count);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Backup signature invalid cannot write GPT\n");
		goto end;
	}

	/* Patching the primary and the backup header of the GPT table */
	patch_gpt(gptImage, mmc_card, partition_entry_array_size,
		  max_partition_count, partition_entry_size);

	if (erase) {
		/* Erasing the eMMC card before writing */
		ret = mmc_erase_card(0x00000000, mmc_card->capacity);
		if (ret) {
			dprintf(CRITICAL, "Failed to erase the eMMC card\n");
			goto end;
		}
	}

	/* Writing protective MBR */
	ret = mmc_write(0, PROTECTIVE_MBR_SIZE, (unsigned int *)gptImage);
	if (ret) {
		dprintf(CRITICAL, "Failed to write Protective MBR\n");
		goto end;
	}
	/* Writing the primary GPT header */
	primary_header_location = PROTECTIVE_MBR_SIZE;
	ret = mmc_write(primary_header_location, BLOCK_SIZE,
			(unsigned int *)primary_gpt_header);
	if (ret) {
		dprintf(CRITICAL, "Failed to write GPT header\n");
		goto end;
	}

	/* Writing the backup GPT header */
	backup_header_lba = GET_LLWORD_FROM_BYTE
	    (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
	secondary_header_location = backup_header_lba * BLOCK_SIZE;
	ret = mmc_write(secondary_header_location, BLOCK_SIZE,
			(unsigned int *)secondary_gpt_header);
	if (ret) {
		dprintf(CRITICAL, "Failed to write GPT backup header\n");
		goto end;
	}

	/* Writing the partition entries array for the primary header */
	partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
	ret = write_gpt_partition_array(primary_gpt_header,
					partition_entry_array_start,
					partition_entry_array_size);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Could not write GPT Partition entries array\n");
		goto end;
	}

	/*Writing the partition entries array for the backup header */
	partition_entry_array_start = primary_gpt_header + BLOCK_SIZE +
	    partition_entry_array_size;
	ret = write_gpt_partition_array(secondary_gpt_header,
					partition_entry_array_start,
					partition_entry_array_size);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Could not write GPT Partition entries array\n");
		goto end;
	}

	/* Re-read the GPT partition table */
	dprintf(INFO, "Re-reading the GPT Partition Table\n");
	ret = mmc_boot_read_gpt(mmc_host, mmc_card);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Failure to re- read the GPT Partition table\n");
		goto end;
	}
	partition_dump();
	dprintf(CRITICAL, "GPT: Partition Table written\n");
	memset(primary_gpt_header, 0x00, size);

 end:
	return ret;
}
/*
 * Write the GPT to the MMC.
 */
Write the GPT to the MMC.
Write the GPT to the MMC .
unsigned int
write_gpt(unsigned size, unsigned char *gptImage,
	  struct mmc_boot_host *mmc_host, struct mmc_boot_card *mmc_card,
	  int erase)
{
	unsigned int ret = MMC_BOOT_E_INVAL;
	unsigned int header_size;
	unsigned long long first_usable_lba;
	unsigned long long backup_header_lba;
	unsigned int max_partition_count = 0;
	unsigned int partition_entry_size;
	unsigned int partition_entry_array_start;
	unsigned char *primary_gpt_header;
	unsigned char *secondary_gpt_header;
	unsigned int offset;
	unsigned int partition_entry_array_size;
	unsigned long long primary_header_location;	
	unsigned long long secondary_header_location;	
	primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
	ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
					 &partition_entry_size, &header_size,
					 &max_partition_count);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Primary signature invalid cannot write GPT\n");
		goto end;
	}
	partition_entry_array_size = partition_entry_size * max_partition_count;
	if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
		partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
	}
	offset = (2 * partition_entry_array_size);
	secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
	ret =
	    partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
				       &partition_entry_size, &header_size,
				       &max_partition_count);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Backup signature invalid cannot write GPT\n");
		goto end;
	}
	patch_gpt(gptImage, mmc_card, partition_entry_array_size,
		  max_partition_count, partition_entry_size);
	if (erase) {
		ret = mmc_erase_card(0x00000000, mmc_card->capacity);
		if (ret) {
			dprintf(CRITICAL, "Failed to erase the eMMC card\n");
			goto end;
		}
	}
	ret = mmc_write(0, PROTECTIVE_MBR_SIZE, (unsigned int *)gptImage);
	if (ret) {
		dprintf(CRITICAL, "Failed to write Protective MBR\n");
		goto end;
	}
	primary_header_location = PROTECTIVE_MBR_SIZE;
	ret = mmc_write(primary_header_location, BLOCK_SIZE,
			(unsigned int *)primary_gpt_header);
	if (ret) {
		dprintf(CRITICAL, "Failed to write GPT header\n");
		goto end;
	}
	backup_header_lba = GET_LLWORD_FROM_BYTE
	    (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
	secondary_header_location = backup_header_lba * BLOCK_SIZE;
	ret = mmc_write(secondary_header_location, BLOCK_SIZE,
			(unsigned int *)secondary_gpt_header);
	if (ret) {
		dprintf(CRITICAL, "Failed to write GPT backup header\n");
		goto end;
	}
	partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
	ret = write_gpt_partition_array(primary_gpt_header,
					partition_entry_array_start,
					partition_entry_array_size);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Could not write GPT Partition entries array\n");
		goto end;
	}
	partition_entry_array_start = primary_gpt_header + BLOCK_SIZE +
	    partition_entry_array_size;
	ret = write_gpt_partition_array(secondary_gpt_header,
					partition_entry_array_start,
					partition_entry_array_size);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Could not write GPT Partition entries array\n");
		goto end;
	}
	dprintf(INFO, "Re-reading the GPT Partition Table\n");
	ret = mmc_boot_read_gpt(mmc_host, mmc_card);
	if (ret) {
		dprintf(CRITICAL,
			"GPT: Failure to re- read the GPT Partition table\n");
		goto end;
	}
	partition_dump();
	dprintf(CRITICAL, "GPT: Partition Table written\n");
	memset(primary_gpt_header, 0x00, size);
 end:
	return ret;
}
unsigned int write_gpt ( unsigned size , unsigned char * gptImage , struct mmc_boot_host * mmc_host , struct mmc_boot_card * mmc_card , int erase ) { unsigned int ret = MMC_BOOT_E_INVAL ; unsigned int header_size ; unsigned long long first_usable_lba ; unsigned long long backup_header_lba ; unsigned int max_partition_count = 0 ; unsigned int partition_entry_size ; unsigned int partition_entry_array_start ; unsigned char * primary_gpt_header ; unsigned char * secondary_gpt_header ; unsigned int offset ; unsigned int partition_entry_array_size ; unsigned long long primary_header_location ; unsigned long long secondary_header_location ; primary_gpt_header = ( gptImage + PROTECTIVE_MBR_SIZE ) ; ret = partition_parse_gpt_header ( primary_gpt_header , & first_usable_lba , & partition_entry_size , & header_size , & max_partition_count ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } partition_entry_array_size = partition_entry_size * max_partition_count ; if ( partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE ) { partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE ; } offset = ( 2 * partition_entry_array_size ) ; secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header ; ret = partition_parse_gpt_header ( secondary_gpt_header , & first_usable_lba , & partition_entry_size , & header_size , & max_partition_count ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } patch_gpt ( gptImage , mmc_card , partition_entry_array_size , max_partition_count , partition_entry_size ) ; if ( erase ) { ret = mmc_erase_card ( 0x00000000 , mmc_card -> capacity ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } } ret = mmc_write ( 0 , PROTECTIVE_MBR_SIZE , ( unsigned int * ) gptImage ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } primary_header_location = PROTECTIVE_MBR_SIZE ; ret = mmc_write ( primary_header_location , BLOCK_SIZE , ( unsigned int * ) primary_gpt_header ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } backup_header_lba = GET_LLWORD_FROM_BYTE ( & primary_gpt_header [ BACKUP_HEADER_OFFSET ] ) ; secondary_header_location = backup_header_lba * BLOCK_SIZE ; ret = mmc_write ( secondary_header_location , BLOCK_SIZE , ( unsigned int * ) secondary_gpt_header ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } partition_entry_array_start = primary_gpt_header + BLOCK_SIZE ; ret = write_gpt_partition_array ( primary_gpt_header , partition_entry_array_start , partition_entry_array_size ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } partition_entry_array_start = primary_gpt_header + BLOCK_SIZE + partition_entry_array_size ; ret = write_gpt_partition_array ( secondary_gpt_header , partition_entry_array_start , partition_entry_array_size ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } dprintf ( INFO , " \n " ) ; ret = mmc_boot_read_gpt ( mmc_host , mmc_card ) ; if ( ret ) { dprintf ( CRITICAL , " \n " ) ; goto end ; } partition_dump ( ) ; dprintf ( CRITICAL , " \n " ) ; memset ( primary_gpt_header , 0x00 , size ) ; end : return ret ; }
Write the GPT to the MMC.
Write the GPT to the MMC .
/* address on the emmc card */ /* address on the emmc card */ /* Verify that passed block has a valid GPT primary header */ /* Verify that passed block has a valid backup GPT HEADER */ /* Patching the primary and the backup header of the GPT table */ /* Erasing the eMMC card before writing */ /* Writing protective MBR */ /* Writing the primary GPT header */ /* Writing the backup GPT header */ /* Writing the partition entries array for the primary header */ /*Writing the partition entries array for the backup header */ /* Re-read the GPT partition table */