+/**
+ * set_protective_mbr(): Set the EFI protective MBR
+ * @param dev_desc - block device descriptor
+ *
+ * @return - zero on success, otherwise error
+ */
+static int set_protective_mbr(block_dev_desc_t *dev_desc)
+{
+ /* Setup the Protective MBR */
+ ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
+ memset(p_mbr, 0, sizeof(*p_mbr));
+
+ if (p_mbr == NULL) {
+ printf("%s: calloc failed!\n", __func__);
+ return -1;
+ }
+ /* Append signature */
+ p_mbr->signature = MSDOS_MBR_SIGNATURE;
+ p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
+ p_mbr->partition_record[0].start_sect = 1;
+ p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
+
+ /* Write MBR sector to the MMC device */
+ if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
+ printf("** Can't write to device %d **\n",
+ dev_desc->dev);
+ return -1;
+ }
+
+ return 0;
+}
+
+int write_gpt_table(block_dev_desc_t *dev_desc,
+ gpt_header *gpt_h, gpt_entry *gpt_e)
+{
+ const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
+ * sizeof(gpt_entry)), dev_desc);
+ u32 calc_crc32;
+
+ debug("max lba: %x\n", (u32) dev_desc->lba);
+ /* Setup the Protective MBR */
+ if (set_protective_mbr(dev_desc) < 0)
+ goto err;
+
+ /* Generate CRC for the Primary GPT Header */
+ calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
+ le32_to_cpu(gpt_h->num_partition_entries) *
+ le32_to_cpu(gpt_h->sizeof_partition_entry));
+ gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
+
+ calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
+ le32_to_cpu(gpt_h->header_size));
+ gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
+
+ /* Write the First GPT to the block right after the Legacy MBR */
+ if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
+ goto err;
+
+ if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
+ != pte_blk_cnt)
+ goto err;
+
+ prepare_backup_gpt_header(gpt_h);
+
+ if (dev_desc->block_write(dev_desc->dev,
+ (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
+ + 1,
+ pte_blk_cnt, gpt_e) != pte_blk_cnt)
+ goto err;
+
+ if (dev_desc->block_write(dev_desc->dev,
+ (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
+ gpt_h) != 1)
+ goto err;
+
+ debug("GPT successfully written to block device!\n");
+ return 0;
+
+ err:
+ printf("** Can't write to device %d **\n", dev_desc->dev);
+ return -1;
+}
+
+int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
+ disk_partition_t *partitions, int parts)
+{
+ lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
+ lbaint_t start;
+ lbaint_t last_usable_lba = (lbaint_t)
+ le64_to_cpu(gpt_h->last_usable_lba);
+ int i, k;
+ size_t efiname_len, dosname_len;
+#ifdef CONFIG_PARTITION_UUIDS
+ char *str_uuid;
+ unsigned char *bin_uuid;
+#endif
+
+ for (i = 0; i < parts; i++) {
+ /* partition starting lba */
+ start = partitions[i].start;
+ if (start && (start < offset)) {
+ printf("Partition overlap\n");
+ return -1;
+ }
+ if (start) {
+ gpt_e[i].starting_lba = cpu_to_le64(start);
+ offset = start + partitions[i].size;
+ } else {
+ gpt_e[i].starting_lba = cpu_to_le64(offset);
+ offset += partitions[i].size;
+ }
+ if (offset >= last_usable_lba) {
+ printf("Partitions layout exceds disk size\n");
+ return -1;
+ }
+ /* partition ending lba */
+ if ((i == parts - 1) && (partitions[i].size == 0))
+ /* extend the last partition to maximuim */
+ gpt_e[i].ending_lba = gpt_h->last_usable_lba;
+ else
+ gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
+
+ /* partition type GUID */
+ memcpy(gpt_e[i].partition_type_guid.b,
+ &PARTITION_BASIC_DATA_GUID, 16);
+
+#ifdef CONFIG_PARTITION_UUIDS
+ str_uuid = partitions[i].uuid;
+ bin_uuid = gpt_e[i].unique_partition_guid.b;
+
+ if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
+ printf("Partition no. %d: invalid guid: %s\n",
+ i, str_uuid);
+ return -1;
+ }
+#endif
+
+ /* partition attributes */
+ memset(&gpt_e[i].attributes, 0,
+ sizeof(gpt_entry_attributes));
+
+ /* partition name */
+ efiname_len = sizeof(gpt_e[i].partition_name)
+ / sizeof(efi_char16_t);
+ dosname_len = sizeof(partitions[i].name);
+
+ memset(gpt_e[i].partition_name, 0,
+ sizeof(gpt_e[i].partition_name));
+
+ for (k = 0; k < min(dosname_len, efiname_len); k++)
+ gpt_e[i].partition_name[k] =
+ (efi_char16_t)(partitions[i].name[k]);
+
+ debug("%s: name: %s offset[%d]: 0x" LBAF
+ " size[%d]: 0x" LBAF "\n",
+ __func__, partitions[i].name, i,
+ offset, i, partitions[i].size);
+ }
+
+ return 0;
+}
+
+int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
+ char *str_guid, int parts_count)
+{
+ gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
+ gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
+ gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
+ gpt_h->my_lba = cpu_to_le64(1);
+ gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
+ gpt_h->first_usable_lba = cpu_to_le64(34);
+ gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
+ gpt_h->partition_entry_lba = cpu_to_le64(2);
+ gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
+ gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
+ gpt_h->header_crc32 = 0;
+ gpt_h->partition_entry_array_crc32 = 0;
+
+ if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
+ return -1;
+
+ return 0;
+}
+
+int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
+ disk_partition_t *partitions, int parts_count)
+{
+ int ret;
+
+ gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
+ dev_desc));
+ gpt_entry *gpt_e;
+
+ if (gpt_h == NULL) {
+ printf("%s: calloc failed!\n", __func__);
+ return -1;
+ }
+
+ gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
+ * sizeof(gpt_entry),
+ dev_desc));
+ if (gpt_e == NULL) {
+ printf("%s: calloc failed!\n", __func__);
+ free(gpt_h);
+ return -1;
+ }
+
+ /* Generate Primary GPT header (LBA1) */
+ ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
+ if (ret)
+ goto err;
+
+ /* Generate partition entries */
+ ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
+ if (ret)
+ goto err;
+
+ /* Write GPT partition table */
+ ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
+
+err:
+ free(gpt_e);
+ free(gpt_h);
+ return ret;
+}
+
+int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf)
+{
+ gpt_header *gpt_h;
+ gpt_entry *gpt_e;
+
+ /* determine start of GPT Header in the buffer */
+ gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
+ dev_desc->blksz);
+ if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
+ dev_desc->lba))
+ return -1;
+
+ /* determine start of GPT Entries in the buffer */
+ gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
+ dev_desc->blksz);
+ if (validate_gpt_entries(gpt_h, gpt_e))
+ return -1;
+
+ return 0;
+}
+
+int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf)
+{
+ gpt_header *gpt_h;
+ gpt_entry *gpt_e;
+ int gpt_e_blk_cnt;
+ lbaint_t lba;
+ int cnt;
+
+ if (is_valid_gpt_buf(dev_desc, buf))
+ return -1;
+
+ /* determine start of GPT Header in the buffer */
+ gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
+ dev_desc->blksz);
+
+ /* determine start of GPT Entries in the buffer */
+ gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
+ dev_desc->blksz);
+ gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
+ le32_to_cpu(gpt_h->sizeof_partition_entry)),
+ dev_desc);
+
+ /* write MBR */
+ lba = 0; /* MBR is always at 0 */
+ cnt = 1; /* MBR (1 block) */
+ if (dev_desc->block_write(dev_desc->dev, lba, cnt, buf) != cnt) {
+ printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
+ __func__, "MBR", cnt, lba);
+ return 1;
+ }
+
+ /* write Primary GPT */
+ lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
+ cnt = 1; /* GPT Header (1 block) */
+ if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
+ printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
+ __func__, "Primary GPT Header", cnt, lba);
+ return 1;
+ }
+
+ lba = le64_to_cpu(gpt_h->partition_entry_lba);
+ cnt = gpt_e_blk_cnt;
+ if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
+ printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
+ __func__, "Primary GPT Entries", cnt, lba);
+ return 1;
+ }
+
+ prepare_backup_gpt_header(gpt_h);
+
+ /* write Backup GPT */
+ lba = le64_to_cpu(gpt_h->partition_entry_lba);
+ cnt = gpt_e_blk_cnt;
+ if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
+ printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
+ __func__, "Backup GPT Entries", cnt, lba);
+ return 1;
+ }
+
+ lba = le64_to_cpu(gpt_h->my_lba);
+ cnt = 1; /* GPT Header (1 block) */
+ if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
+ printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
+ __func__, "Backup GPT Header", cnt, lba);
+ return 1;
+ }
+
+ return 0;
+}
+#endif
+