⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpt.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 2 页
字号:
		/* 		   printf("GUID Partition Table Header signature is wrong: %" PRIx64" != %" PRIx64 "\n",		   __le64_to_cpu((*gpt)->signature), GUID_PT_HEADER_SIGNATURE);		 */		free(*gpt);		*gpt = NULL;		return rc;	}	/* Check the GUID Partition Table Header CRC */	origcrc = __le32_to_cpu((*gpt)->header_crc32);	(*gpt)->header_crc32 = 0;	crc = efi_crc32(*gpt, __le32_to_cpu((*gpt)->header_size));	if (crc != origcrc) {		// printf( "GPTH CRC check failed, %x != %x.\n", origcrc, crc);		(*gpt)->header_crc32 = __cpu_to_le32(origcrc);		free(*gpt);		*gpt = NULL;		return 0;	}	(*gpt)->header_crc32 = __cpu_to_le32(origcrc);	/* Check that the my_lba entry points to the LBA	 * that contains the GPT we read */	if (__le64_to_cpu((*gpt)->my_lba) != lba) {		// printf( "my_lba % PRIx64 "x != lba %"PRIx64 "x.\n", __le64_to_cpu((*gpt)->my_lba), lba);		free(*gpt);		*gpt = NULL;		return 0;	}	if (!(*ptes = alloc_read_gpt_entries(fd, *gpt))) {		free(*gpt);		*gpt = NULL;		return 0;	}	/* Check the GUID Partition Entry Array CRC */	crc = efi_crc32(*ptes,                        __le32_to_cpu((*gpt)->num_partition_entries) *			__le32_to_cpu((*gpt)->sizeof_partition_entry));	if (crc != __le32_to_cpu((*gpt)->partition_entry_array_crc32)) {		// printf("GUID Partitition Entry Array CRC check failed.\n");		free(*gpt);		*gpt = NULL;		free(*ptes);		*ptes = NULL;		return 0;	}	/* We're done, all's well */	return 1;}/** * compare_gpts() - Search disk for valid GPT headers and PTEs * @pgpt is the primary GPT header * @agpt is the alternate GPT header * @lastlba is the last LBA number * Description: Returns nothing.  Sanity checks pgpt and agpt fields * and prints warnings on discrepancies. *  */static voidcompare_gpts(gpt_header *pgpt, gpt_header *agpt, uint64_t lastlba){	int error_found = 0;	if (!pgpt || !agpt)		return;	if (__le64_to_cpu(pgpt->my_lba) != __le64_to_cpu(agpt->alternate_lba)) {		fprintf(stderr, 		       "GPT:Primary header LBA != Alt. header alternate_lba\n");		fprintf(stderr,  "GPT:%" PRIx64 "x != %" PRIx64 "x\n",		       __le64_to_cpu(pgpt->my_lba),                       __le64_to_cpu(agpt->alternate_lba));		error_found++;	}	if (__le64_to_cpu(pgpt->alternate_lba) != __le64_to_cpu(agpt->my_lba)) {		fprintf(stderr, 		       "GPT:Primary header alternate_lba != Alt. header my_lba\n");		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",		       __le64_to_cpu(pgpt->alternate_lba),                       __le64_to_cpu(agpt->my_lba));		error_found++;	}	if (__le64_to_cpu(pgpt->first_usable_lba) !=            __le64_to_cpu(agpt->first_usable_lba)) {		fprintf(stderr,  "GPT:first_usable_lbas don't match.\n");		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",		       __le64_to_cpu(pgpt->first_usable_lba),                       __le64_to_cpu(agpt->first_usable_lba));		error_found++;	}	if (__le64_to_cpu(pgpt->last_usable_lba) !=            __le64_to_cpu(agpt->last_usable_lba)) {		fprintf(stderr,  "GPT:last_usable_lbas don't match.\n");		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",		       __le64_to_cpu(pgpt->last_usable_lba),                       __le64_to_cpu(agpt->last_usable_lba));		error_found++;	}	if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {		fprintf(stderr,  "GPT:disk_guids don't match.\n");		error_found++;	}	if (__le32_to_cpu(pgpt->num_partition_entries) !=            __le32_to_cpu(agpt->num_partition_entries)) {		fprintf(stderr,  "GPT:num_partition_entries don't match: "		       "0x%x != 0x%x\n",		       __le32_to_cpu(pgpt->num_partition_entries),		       __le32_to_cpu(agpt->num_partition_entries));		error_found++;	}	if (__le32_to_cpu(pgpt->sizeof_partition_entry) !=            __le32_to_cpu(agpt->sizeof_partition_entry)) {		fprintf(stderr, 		       "GPT:sizeof_partition_entry values don't match: "		       "0x%x != 0x%x\n",                       __le32_to_cpu(pgpt->sizeof_partition_entry),		       __le32_to_cpu(agpt->sizeof_partition_entry));		error_found++;	}	if (__le32_to_cpu(pgpt->partition_entry_array_crc32) !=            __le32_to_cpu(agpt->partition_entry_array_crc32)) {		fprintf(stderr, 		       "GPT:partition_entry_array_crc32 values don't match: "		       "0x%x != 0x%x\n",                       __le32_to_cpu(pgpt->partition_entry_array_crc32),		       __le32_to_cpu(agpt->partition_entry_array_crc32));		error_found++;	}	if (__le64_to_cpu(pgpt->alternate_lba) != lastlba) {		fprintf(stderr, 		       "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",		       __le64_to_cpu(pgpt->alternate_lba), lastlba);		error_found++;	}	if (__le64_to_cpu(agpt->my_lba) != lastlba) {		fprintf(stderr, 		       "GPT:Alternate GPT header not at the end of the disk.\n");		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",		       __le64_to_cpu(agpt->my_lba), lastlba);		error_found++;	}	if (error_found)		fprintf(stderr, 		       "GPT: Use GNU Parted to correct GPT errors.\n");	return;}/** * find_valid_gpt() - Search disk for valid GPT headers and PTEs * @fd  is an open file descriptor to the whole disk * @gpt is a GPT header ptr, filled on return. * @ptes is a PTEs ptr, filled on return. * Description: Returns 1 if valid, 0 on error. * If valid, returns pointers to newly allocated GPT header and PTEs. * Validity depends on finding either the Primary GPT header and PTEs valid, * or the Alternate GPT header and PTEs valid, and the PMBR valid. */static intfind_valid_gpt(int fd, gpt_header ** gpt, gpt_entry ** ptes){        extern int force_gpt;	int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;	gpt_header *pgpt = NULL, *agpt = NULL;	gpt_entry *pptes = NULL, *aptes = NULL;	legacy_mbr *legacymbr = NULL;	uint64_t lastlba;	if (!gpt || !ptes)		return 0;	lastlba = last_lba(fd);	good_pgpt = is_gpt_valid(fd, GPT_PRIMARY_PARTITION_TABLE_LBA,				 &pgpt, &pptes);        if (good_pgpt) {		good_agpt = is_gpt_valid(fd,                                         __le64_to_cpu(pgpt->alternate_lba),					 &agpt, &aptes);                if (!good_agpt) {                        good_agpt = is_gpt_valid(fd, lastlba,                                                 &agpt, &aptes);                }        }        else {                good_agpt = is_gpt_valid(fd, lastlba,                                         &agpt, &aptes);        }        /* The obviously unsuccessful case */        if (!good_pgpt && !good_agpt) {                goto fail;        }	/* This will be added to the EFI Spec. per Intel after v1.02. */        legacymbr = malloc(sizeof (*legacymbr));        if (legacymbr) {                memset(legacymbr, 0, sizeof (*legacymbr));                read_lba(fd, 0, (uint8_t *) legacymbr,                         sizeof (*legacymbr));                good_pmbr = is_pmbr_valid(legacymbr);                free(legacymbr);                legacymbr=NULL;        }        /* Failure due to bad PMBR */        if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {                fprintf(stderr,                       "  Warning: Disk has a valid GPT signature "                       "but invalid PMBR.\n"                       "  Assuming this disk is *not* a GPT disk anymore.\n"                       "  Use gpt kernel option to override.  "                       "Use GNU Parted to correct disk.\n");                goto fail;        }        /* Would fail due to bad PMBR, but force GPT anyhow */        if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {                fprintf(stderr,                        "  Warning: Disk has a valid GPT signature but "                       "invalid PMBR.\n"                       "  Use GNU Parted to correct disk.\n"                       "  gpt option taken, disk treated as GPT.\n");        }        compare_gpts(pgpt, agpt, lastlba);        /* The good cases */        if (good_pgpt && (good_pmbr || force_gpt)) {                *gpt  = pgpt;                *ptes = pptes;                if (agpt)  { free(agpt);   agpt = NULL; }                if (aptes) { free(aptes); aptes = NULL; }                if (!good_agpt) {                        fprintf(stderr, 			       "Alternate GPT is invalid, "                               "using primary GPT.\n");                }                return 1;        }        else if (good_agpt && (good_pmbr || force_gpt)) {                *gpt  = agpt;                *ptes = aptes;                if (pgpt)  { free(pgpt);   pgpt = NULL; }                if (pptes) { free(pptes); pptes = NULL; }                fprintf(stderr,                        "Primary GPT is invalid, using alternate GPT.\n");                return 1;        } fail:        if (pgpt)  { free(pgpt);   pgpt=NULL; }        if (agpt)  { free(agpt);   agpt=NULL; }        if (pptes) { free(pptes); pptes=NULL; }        if (aptes) { free(aptes); aptes=NULL; }        *gpt = NULL;        *ptes = NULL;        return 0;}/** * read_gpt_pt()  * @fd * @all - slice with start/size of whole disk * *  0 if this isn't our partition table *  number of partitions if successful * */intread_gpt_pt (int fd, struct slice all, struct slice *sp, int ns){	gpt_header *gpt = NULL;	gpt_entry *ptes = NULL;	uint32_t i;	int n = 0;        int last_used_index=-1;	if (!find_valid_gpt (fd, &gpt, &ptes) || !gpt || !ptes) {		if (gpt)			free (gpt);		if (ptes)			free (ptes);		return 0;	}	for (i = 0; i < __le32_to_cpu(gpt->num_partition_entries) && i < ns; i++) {		if (!efi_guidcmp (NULL_GUID, ptes[i].partition_type_guid)) {			sp[n].start = 0;			sp[n].size = 0;			n++;		} else {			sp[n].start = __le64_to_cpu(ptes[i].starting_lba);			sp[n].size  = __le64_to_cpu(ptes[i].ending_lba) -				__le64_to_cpu(ptes[i].starting_lba) + 1;                        last_used_index=n;			n++;		}	}	free (ptes);	free (gpt);	return last_used_index+1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -