// This method gets the partition details such as the attributes, the // guids of the partitions, etc. Input is the partition number or the // unique id of the partition. Output is populated in the respective // fields of params. int CgptGetPartitionDetails(CgptAddParams *params) { struct drive drive; int result = CGPT_FAILED; int index; if (params == NULL) return CGPT_FAILED; if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR)) return CGPT_FAILED; if (CgptCheckAddValidity(&drive)) { goto bad; } int max_part = GetNumberOfEntries(&drive); if (params->partition > 0) { if (params->partition >= max_part) { Error("invalid partition number: %d\n", params->partition); goto bad; } } else { if (!params->set_unique) { Error("either partition or unique_id must be specified\n"); goto bad; } for (index = 0; index < max_part; index++) { GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index); if (GuidEqual(&entry->unique, ¶ms->unique_guid)) { params->partition = index + 1; break; } } if (index >= max_part) { Error("no partitions with the given unique id available\n"); goto bad; } } index = params->partition - 1; { // GPT-specific code GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index); params->begin = entry->starting_lba; params->size = entry->ending_lba - entry->starting_lba + 1; memcpy(¶ms->type_guid, &entry->type, sizeof(Guid)); memcpy(¶ms->unique_guid, &entry->unique, sizeof(Guid)); params->raw_value = entry->attrs.fields.gpt_att; } params->successful = GetSuccessful(&drive, PRIMARY, index); params->tries = GetTries(&drive, PRIMARY, index); params->priority = GetPriority(&drive, PRIMARY, index); result = CGPT_OK; bad: DriveClose(&drive, 0); return result; }
static int do_search(CgptNextParams *params) { struct drive drive; uint32_t max_part; int gpt_retval; int priority, tries, successful; int i; if (CGPT_OK != DriveOpen(params->drive_name, &drive, 0, O_RDONLY)) return CGPT_FAILED; if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { Error("GptSanityCheck() returned %d: %s\n", gpt_retval, GptError(gpt_retval)); return CGPT_FAILED; } max_part = GetNumberOfEntries(&drive); for (i = 0; i < max_part; i++) { if (!IsRoot(&drive, PRIMARY, i)) continue; priority = GetPriority(&drive, PRIMARY, i); tries = GetTries(&drive, PRIMARY, i); successful = GetSuccessful(&drive, PRIMARY, i); if (next_index == -1 || ((priority > next_priority) && (successful || tries))) { strncpy(next_file_name, params->drive_name, BUFSIZE); if (successful || tries) { next_priority = priority; } else { next_priority = -1; } next_index = i; } } return DriveClose(&drive, 0); }