| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <errno.h> |
| 10 |
#include <fcntl.h> |
| 11 |
#include <getopt.h> |
| 12 |
#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__) |
| 13 |
#include <linux/major.h> |
| 14 |
#include <mtd/mtd-user.h> |
| 15 |
#endif |
| 16 |
#include <stdarg.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <sys/mount.h> |
| 23 |
#include <sys/stat.h> |
| 24 |
#include <sys/types.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include "cgpt.h" |
| 28 |
#include "cgptlib_internal.h" |
| 29 |
#include "crc32.h" |
| 30 |
#include "vboot_host.h" |
| 31 |
|
| 32 |
static const char kErrorTag[] = "ERROR"; |
| 33 |
static const char kWarningTag[] = "WARNING"; |
| 34 |
|
| 35 |
static void LogToStderr(const char *tag, const char *format, va_list ap) { |
| 36 |
fprintf(stderr, "%s: ", tag); |
| 37 |
vfprintf(stderr, format, ap); |
| 38 |
} |
| 39 |
|
| 40 |
void Error(const char *format, ...) { |
| 41 |
va_list ap; |
| 42 |
va_start(ap, format); |
| 43 |
LogToStderr(kErrorTag, format, ap); |
| 44 |
va_end(ap); |
| 45 |
} |
| 46 |
|
| 47 |
void Warning(const char *format, ...) { |
| 48 |
va_list ap; |
| 49 |
va_start(ap, format); |
| 50 |
LogToStderr(kWarningTag, format, ap); |
| 51 |
va_end(ap); |
| 52 |
} |
| 53 |
|
| 54 |
int check_int_parse(char option, const char *buf) { |
| 55 |
if (!*optarg || (buf && *buf)) { |
| 56 |
Error("invalid argument to -%c: \"%s\"\n", option, optarg); |
| 57 |
return 1; |
| 58 |
} |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
int check_int_limit(char option, int val, int low, int high) { |
| 63 |
if (val < low || val > high) { |
| 64 |
Error("value for -%c must be between %d and %d", option, low, high); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
int CheckValid(const struct drive *drive) { |
| 71 |
if ((drive->gpt.valid_headers != MASK_BOTH) || |
| 72 |
(drive->gpt.valid_entries != MASK_BOTH)) { |
| 73 |
Warning("One of the GPT headers/entries is invalid\n\n"); |
| 74 |
return CGPT_FAILED; |
| 75 |
} |
| 76 |
return CGPT_OK; |
| 77 |
} |
| 78 |
|
| 79 |
int Load(struct drive *drive, uint8_t *buf, |
| 80 |
const uint64_t sector, |
| 81 |
const uint64_t sector_bytes, |
| 82 |
const uint64_t sector_count) { |
| 83 |
int count; |
| 84 |
int nread; |
| 85 |
|
| 86 |
require(buf); |
| 87 |
if (!sector_count || !sector_bytes) { |
| 88 |
Error("%s() failed at line %d: sector_count=%" PRIu64 ", sector_bytes=%" PRIu64 "\n", |
| 89 |
__FUNCTION__, __LINE__, sector_count, sector_bytes); |
| 90 |
return CGPT_FAILED; |
| 91 |
} |
| 92 |
|
| 93 |
if (sector_bytes > (UINT64_MAX / sector_count)) { |
| 94 |
Error("%s() failed at line %d: sector_count=%" PRIu64 ", sector_bytes=%" PRIu64 "\n", |
| 95 |
__FUNCTION__, __LINE__, sector_count, sector_bytes); |
| 96 |
return CGPT_FAILED; |
| 97 |
} |
| 98 |
count = sector_bytes * sector_count; |
| 99 |
|
| 100 |
if (-1 == lseek(drive->fd, sector * sector_bytes, SEEK_SET)) { |
| 101 |
Error("Can't seek: %s\n", strerror(errno)); |
| 102 |
return CGPT_FAILED; |
| 103 |
} |
| 104 |
|
| 105 |
nread = read(drive->fd, buf, count); |
| 106 |
if (nread < count) { |
| 107 |
Error("Can't read enough: %d, not %d\n", nread, count); |
| 108 |
return CGPT_FAILED; |
| 109 |
} |
| 110 |
|
| 111 |
return CGPT_OK; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
int ReadPMBR(struct drive *drive) { |
| 116 |
if (-1 == lseek(drive->fd, 0, SEEK_SET)) |
| 117 |
return CGPT_FAILED; |
| 118 |
|
| 119 |
int nread = read(drive->fd, &drive->pmbr, sizeof(struct pmbr)); |
| 120 |
if (nread != sizeof(struct pmbr)) |
| 121 |
return CGPT_FAILED; |
| 122 |
|
| 123 |
return CGPT_OK; |
| 124 |
} |
| 125 |
|
| 126 |
int WritePMBR(struct drive *drive) { |
| 127 |
if (-1 == lseek(drive->fd, 0, SEEK_SET)) |
| 128 |
return CGPT_FAILED; |
| 129 |
|
| 130 |
int nwrote = write(drive->fd, &drive->pmbr, sizeof(struct pmbr)); |
| 131 |
if (nwrote != sizeof(struct pmbr)) |
| 132 |
return CGPT_FAILED; |
| 133 |
|
| 134 |
return CGPT_OK; |
| 135 |
} |
| 136 |
|
| 137 |
int Save(struct drive *drive, const uint8_t *buf, |
| 138 |
const uint64_t sector, |
| 139 |
const uint64_t sector_bytes, |
| 140 |
const uint64_t sector_count) { |
| 141 |
int count; |
| 142 |
int nwrote; |
| 143 |
|
| 144 |
require(buf); |
| 145 |
count = sector_bytes * sector_count; |
| 146 |
|
| 147 |
if (-1 == lseek(drive->fd, sector * sector_bytes, SEEK_SET)) |
| 148 |
return CGPT_FAILED; |
| 149 |
|
| 150 |
nwrote = write(drive->fd, buf, count); |
| 151 |
if (nwrote < count) |
| 152 |
return CGPT_FAILED; |
| 153 |
|
| 154 |
return CGPT_OK; |
| 155 |
} |
| 156 |
|
| 157 |
static int GptLoad(struct drive *drive, uint32_t sector_bytes) { |
| 158 |
drive->gpt.sector_bytes = sector_bytes; |
| 159 |
if (drive->size % drive->gpt.sector_bytes) { |
| 160 |
Error("Media size (%llu) is not a multiple of sector size(%d)\n", |
| 161 |
(long long unsigned int)drive->size, drive->gpt.sector_bytes); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
drive->gpt.streaming_drive_sectors = drive->size / drive->gpt.sector_bytes; |
| 165 |
|
| 166 |
drive->gpt.primary_header = malloc(drive->gpt.sector_bytes); |
| 167 |
drive->gpt.secondary_header = malloc(drive->gpt.sector_bytes); |
| 168 |
drive->gpt.primary_entries = malloc(GPT_ENTRIES_ALLOC_SIZE); |
| 169 |
drive->gpt.secondary_entries = malloc(GPT_ENTRIES_ALLOC_SIZE); |
| 170 |
if (!drive->gpt.primary_header || !drive->gpt.secondary_header || |
| 171 |
!drive->gpt.primary_entries || !drive->gpt.secondary_entries) |
| 172 |
return -1; |
| 173 |
|
| 174 |
|
| 175 |
if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL)) { |
| 176 |
drive->gpt.gpt_drive_sectors = drive->gpt.streaming_drive_sectors; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
if (CGPT_OK != Load(drive, drive->gpt.primary_header, |
| 181 |
GPT_PMBR_SECTORS, |
| 182 |
drive->gpt.sector_bytes, GPT_HEADER_SECTORS)) { |
| 183 |
Error("Cannot read primary GPT header\n"); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
if (CGPT_OK != Load(drive, drive->gpt.secondary_header, |
| 187 |
drive->gpt.gpt_drive_sectors - GPT_PMBR_SECTORS, |
| 188 |
drive->gpt.sector_bytes, GPT_HEADER_SECTORS)) { |
| 189 |
Error("Cannot read secondary GPT header\n"); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header; |
| 193 |
if (CheckHeader(primary_header, 0, drive->gpt.streaming_drive_sectors, |
| 194 |
drive->gpt.gpt_drive_sectors, |
| 195 |
drive->gpt.flags, |
| 196 |
drive->gpt.sector_bytes) == 0) { |
| 197 |
if (CGPT_OK != Load(drive, drive->gpt.primary_entries, |
| 198 |
primary_header->entries_lba, |
| 199 |
drive->gpt.sector_bytes, |
| 200 |
CalculateEntriesSectors(primary_header, |
| 201 |
drive->gpt.sector_bytes))) { |
| 202 |
Error("Cannot read primary partition entry array\n"); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
} else { |
| 206 |
Warning("Primary GPT header is %s\n", |
| 207 |
memcmp(primary_header->signature, GPT_HEADER_SIGNATURE_IGNORED, |
| 208 |
GPT_HEADER_SIGNATURE_SIZE) ? "invalid" : "being ignored"); |
| 209 |
} |
| 210 |
GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header; |
| 211 |
if (CheckHeader(secondary_header, 1, drive->gpt.streaming_drive_sectors, |
| 212 |
drive->gpt.gpt_drive_sectors, |
| 213 |
drive->gpt.flags, |
| 214 |
drive->gpt.sector_bytes) == 0) { |
| 215 |
if (CGPT_OK != Load(drive, drive->gpt.secondary_entries, |
| 216 |
secondary_header->entries_lba, |
| 217 |
drive->gpt.sector_bytes, |
| 218 |
CalculateEntriesSectors(secondary_header, |
| 219 |
drive->gpt.sector_bytes))) { |
| 220 |
Error("Cannot read secondary partition entry array\n"); |
| 221 |
return -1; |
| 222 |
} |
| 223 |
} else { |
| 224 |
Warning("Secondary GPT header is %s\n", |
| 225 |
memcmp(primary_header->signature, GPT_HEADER_SIGNATURE_IGNORED, |
| 226 |
GPT_HEADER_SIGNATURE_SIZE) ? "invalid" : "being ignored"); |
| 227 |
} |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
static int GptSave(struct drive *drive) { |
| 232 |
int errors = 0; |
| 233 |
|
| 234 |
if (!(drive->gpt.ignored & MASK_PRIMARY)) { |
| 235 |
if (drive->gpt.modified & GPT_MODIFIED_HEADER1) { |
| 236 |
if (CGPT_OK != Save(drive, drive->gpt.primary_header, |
| 237 |
GPT_PMBR_SECTORS, |
| 238 |
drive->gpt.sector_bytes, GPT_HEADER_SECTORS)) { |
| 239 |
errors++; |
| 240 |
Error("Cannot write primary header: %s\n", strerror(errno)); |
| 241 |
} |
| 242 |
} |
| 243 |
GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header; |
| 244 |
if (drive->gpt.modified & GPT_MODIFIED_ENTRIES1) { |
| 245 |
if (CGPT_OK != Save(drive, drive->gpt.primary_entries, |
| 246 |
primary_header->entries_lba, |
| 247 |
drive->gpt.sector_bytes, |
| 248 |
CalculateEntriesSectors(primary_header, |
| 249 |
drive->gpt.sector_bytes))) { |
| 250 |
errors++; |
| 251 |
Error("Cannot write primary entries: %s\n", strerror(errno)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
if (drive->gpt.modified & (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1)) |
| 257 |
if (fsync(drive->fd) < 0 && errno == EIO) { |
| 258 |
errors++; |
| 259 |
Error("I/O error when trying to write primary GPT\n"); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
if (!errors && !(drive->gpt.ignored & MASK_SECONDARY)) { |
| 265 |
if (drive->gpt.modified & GPT_MODIFIED_HEADER2) { |
| 266 |
if (CGPT_OK != Save(drive, drive->gpt.secondary_header, |
| 267 |
drive->gpt.gpt_drive_sectors - GPT_PMBR_SECTORS, |
| 268 |
drive->gpt.sector_bytes, GPT_HEADER_SECTORS)) { |
| 269 |
errors++; |
| 270 |
Error("Cannot write secondary header: %s\n", strerror(errno)); |
| 271 |
} |
| 272 |
} |
| 273 |
GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header; |
| 274 |
if (drive->gpt.modified & GPT_MODIFIED_ENTRIES2) { |
| 275 |
if (CGPT_OK != Save(drive, drive->gpt.secondary_entries, |
| 276 |
secondary_header->entries_lba, |
| 277 |
drive->gpt.sector_bytes, |
| 278 |
CalculateEntriesSectors(secondary_header, |
| 279 |
drive->gpt.sector_bytes))) { |
| 280 |
errors++; |
| 281 |
Error("Cannot write secondary entries: %s\n", strerror(errno)); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
return errors ? -1 : 0; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
static int ObtainDriveSize(int fd, uint64_t* size, uint32_t* sector_bytes) { |
| 294 |
struct stat stat; |
| 295 |
if (fstat(fd, &stat) == -1) { |
| 296 |
return -1; |
| 297 |
} |
| 298 |
#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__) |
| 299 |
if ((stat.st_mode & S_IFMT) != S_IFREG) { |
| 300 |
if (ioctl(fd, BLKGETSIZE64, size) < 0) { |
| 301 |
return -1; |
| 302 |
} |
| 303 |
if (ioctl(fd, BLKSSZGET, sector_bytes) < 0) { |
| 304 |
return -1; |
| 305 |
} |
| 306 |
} else { |
| 307 |
*sector_bytes = 512; |
| 308 |
*size = stat.st_size; |
| 309 |
} |
| 310 |
#else |
| 311 |
*sector_bytes = 512; |
| 312 |
*size = stat.st_size; |
| 313 |
#endif |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
int DriveOpen(const char *drive_path, struct drive *drive, int mode, |
| 318 |
uint64_t drive_size) { |
| 319 |
uint32_t sector_bytes; |
| 320 |
|
| 321 |
require(drive_path); |
| 322 |
require(drive); |
| 323 |
|
| 324 |
|
| 325 |
memset(drive, 0, sizeof(struct drive)); |
| 326 |
|
| 327 |
drive->fd = open(drive_path, mode | |
| 328 |
#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__) |
| 329 |
O_LARGEFILE | |
| 330 |
#endif |
| 331 |
O_NOFOLLOW); |
| 332 |
if (drive->fd == -1) { |
| 333 |
Error("Can't open %s: %s\n", drive_path, strerror(errno)); |
| 334 |
return CGPT_FAILED; |
| 335 |
} |
| 336 |
|
| 337 |
uint64_t gpt_drive_size; |
| 338 |
if (ObtainDriveSize(drive->fd, &gpt_drive_size, §or_bytes) != 0) { |
| 339 |
Error("Can't get drive size and bytes per sector for %s: %s\n", |
| 340 |
drive_path, strerror(errno)); |
| 341 |
goto error_close; |
| 342 |
} |
| 343 |
|
| 344 |
drive->gpt.gpt_drive_sectors = gpt_drive_size / sector_bytes; |
| 345 |
if (drive_size == 0) { |
| 346 |
drive->size = gpt_drive_size; |
| 347 |
drive->gpt.flags = 0; |
| 348 |
} else { |
| 349 |
drive->size = drive_size; |
| 350 |
drive->gpt.flags = GPT_FLAG_EXTERNAL; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
if (GptLoad(drive, sector_bytes)) { |
| 355 |
goto error_close; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
return CGPT_OK; |
| 360 |
|
| 361 |
error_close: |
| 362 |
(void) DriveClose(drive, 0); |
| 363 |
return CGPT_FAILED; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
int DriveClose(struct drive *drive, int update_as_needed) { |
| 368 |
int errors = 0; |
| 369 |
|
| 370 |
if (update_as_needed) { |
| 371 |
if (GptSave(drive)) { |
| 372 |
errors++; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
free(drive->gpt.primary_header); |
| 377 |
drive->gpt.primary_header = NULL; |
| 378 |
free(drive->gpt.primary_entries); |
| 379 |
drive->gpt.primary_entries = NULL; |
| 380 |
free(drive->gpt.secondary_header); |
| 381 |
drive->gpt.secondary_header = NULL; |
| 382 |
free(drive->gpt.secondary_entries); |
| 383 |
drive->gpt.secondary_entries = NULL; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
fsync(drive->fd); |
| 389 |
|
| 390 |
close(drive->fd); |
| 391 |
|
| 392 |
return errors ? CGPT_FAILED : CGPT_OK; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
int StrToGuid(const char *str, Guid *guid) { |
| 403 |
uint32_t time_low; |
| 404 |
uint16_t time_mid; |
| 405 |
uint16_t time_high_and_version; |
| 406 |
unsigned int chunk[11]; |
| 407 |
|
| 408 |
if (11 != sscanf(str, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", |
| 409 |
chunk+0, |
| 410 |
chunk+1, |
| 411 |
chunk+2, |
| 412 |
chunk+3, |
| 413 |
chunk+4, |
| 414 |
chunk+5, |
| 415 |
chunk+6, |
| 416 |
chunk+7, |
| 417 |
chunk+8, |
| 418 |
chunk+9, |
| 419 |
chunk+10)) { |
| 420 |
printf("FAILED\n"); |
| 421 |
return CGPT_FAILED; |
| 422 |
} |
| 423 |
|
| 424 |
time_low = chunk[0] & 0xffffffff; |
| 425 |
time_mid = chunk[1] & 0xffff; |
| 426 |
time_high_and_version = chunk[2] & 0xffff; |
| 427 |
|
| 428 |
guid->u.Uuid.time_low = htole32(time_low); |
| 429 |
guid->u.Uuid.time_mid = htole16(time_mid); |
| 430 |
guid->u.Uuid.time_high_and_version = htole16(time_high_and_version); |
| 431 |
|
| 432 |
guid->u.Uuid.clock_seq_high_and_reserved = chunk[3] & 0xff; |
| 433 |
guid->u.Uuid.clock_seq_low = chunk[4] & 0xff; |
| 434 |
guid->u.Uuid.node[0] = chunk[5] & 0xff; |
| 435 |
guid->u.Uuid.node[1] = chunk[6] & 0xff; |
| 436 |
guid->u.Uuid.node[2] = chunk[7] & 0xff; |
| 437 |
guid->u.Uuid.node[3] = chunk[8] & 0xff; |
| 438 |
guid->u.Uuid.node[4] = chunk[9] & 0xff; |
| 439 |
guid->u.Uuid.node[5] = chunk[10] & 0xff; |
| 440 |
|
| 441 |
return CGPT_OK; |
| 442 |
} |
| 443 |
void GuidToStr(const Guid *guid, char *str, unsigned int buflen) { |
| 444 |
require(buflen >= GUID_STRLEN); |
| 445 |
require(snprintf(str, buflen, |
| 446 |
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", |
| 447 |
le32toh(guid->u.Uuid.time_low), |
| 448 |
le16toh(guid->u.Uuid.time_mid), |
| 449 |
le16toh(guid->u.Uuid.time_high_and_version), |
| 450 |
guid->u.Uuid.clock_seq_high_and_reserved, |
| 451 |
guid->u.Uuid.clock_seq_low, |
| 452 |
guid->u.Uuid.node[0], guid->u.Uuid.node[1], |
| 453 |
guid->u.Uuid.node[2], guid->u.Uuid.node[3], |
| 454 |
guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput, |
| 476 |
uint8_t *utf8, unsigned int maxoutput) |
| 477 |
{ |
| 478 |
size_t s16idx, s8idx; |
| 479 |
uint32_t code_point = 0; |
| 480 |
int code_point_ready = 1; |
| 481 |
int retval = CGPT_OK; |
| 482 |
|
| 483 |
if (!utf16 || !maxinput || !utf8 || !maxoutput) |
| 484 |
return CGPT_FAILED; |
| 485 |
|
| 486 |
maxoutput--; |
| 487 |
|
| 488 |
for (s16idx = s8idx = 0; |
| 489 |
s16idx < maxinput && utf16[s16idx] && maxoutput; |
| 490 |
s16idx++) { |
| 491 |
uint16_t codeunit = le16toh(utf16[s16idx]); |
| 492 |
|
| 493 |
if (code_point_ready) { |
| 494 |
if (codeunit >= 0xD800 && codeunit <= 0xDBFF) { |
| 495 |
|
| 496 |
code_point_ready = 0; |
| 497 |
code_point = (codeunit & 0x03FF) + 0x0040; |
| 498 |
} else { |
| 499 |
|
| 500 |
code_point = codeunit; |
| 501 |
} |
| 502 |
} else { |
| 503 |
|
| 504 |
if (codeunit >= 0xDC00 && codeunit <= 0xDFFF) { |
| 505 |
code_point = (code_point << 10) | (codeunit & 0x03FF); |
| 506 |
code_point_ready = 1; |
| 507 |
} else { |
| 508 |
|
| 509 |
code_point_ready = 0; |
| 510 |
retval = CGPT_FAILED; |
| 511 |
break; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
if (code_point_ready) { |
| 517 |
require(code_point <= 0x10FFFF); |
| 518 |
if (code_point <= 0x7F && maxoutput >= 1) { |
| 519 |
maxoutput -= 1; |
| 520 |
utf8[s8idx++] = code_point & 0x7F; |
| 521 |
} else if (code_point <= 0x7FF && maxoutput >= 2) { |
| 522 |
maxoutput -= 2; |
| 523 |
utf8[s8idx++] = 0xC0 | (code_point >> 6); |
| 524 |
utf8[s8idx++] = 0x80 | (code_point & 0x3F); |
| 525 |
} else if (code_point <= 0xFFFF && maxoutput >= 3) { |
| 526 |
maxoutput -= 3; |
| 527 |
utf8[s8idx++] = 0xE0 | (code_point >> 12); |
| 528 |
utf8[s8idx++] = 0x80 | ((code_point >> 6) & 0x3F); |
| 529 |
utf8[s8idx++] = 0x80 | (code_point & 0x3F); |
| 530 |
} else if (code_point <= 0x10FFFF && maxoutput >= 4) { |
| 531 |
maxoutput -= 4; |
| 532 |
utf8[s8idx++] = 0xF0 | (code_point >> 18); |
| 533 |
utf8[s8idx++] = 0x80 | ((code_point >> 12) & 0x3F); |
| 534 |
utf8[s8idx++] = 0x80 | ((code_point >> 6) & 0x3F); |
| 535 |
utf8[s8idx++] = 0x80 | (code_point & 0x3F); |
| 536 |
} else { |
| 537 |
|
| 538 |
retval = CGPT_FAILED; |
| 539 |
break; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
utf8[s8idx++] = 0; |
| 544 |
return retval; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput) |
| 565 |
{ |
| 566 |
size_t s16idx, s8idx; |
| 567 |
uint32_t code_point = 0; |
| 568 |
unsigned int expected_units = 1; |
| 569 |
unsigned int decoded_units = 1; |
| 570 |
int retval = CGPT_OK; |
| 571 |
|
| 572 |
if (!utf8 || !utf16 || !maxoutput) |
| 573 |
return CGPT_FAILED; |
| 574 |
|
| 575 |
maxoutput--; |
| 576 |
|
| 577 |
for (s8idx = s16idx = 0; |
| 578 |
utf8[s8idx] && maxoutput; |
| 579 |
s8idx++) { |
| 580 |
uint8_t code_unit; |
| 581 |
code_unit = utf8[s8idx]; |
| 582 |
|
| 583 |
if (expected_units != decoded_units) { |
| 584 |
|
| 585 |
if ((code_unit & 0xC0) == 0x80) { |
| 586 |
code_point = (code_point << 6) | (code_unit & 0x3F); |
| 587 |
++decoded_units; |
| 588 |
} else { |
| 589 |
|
| 590 |
retval = CGPT_FAILED; |
| 591 |
break; |
| 592 |
} |
| 593 |
} else { |
| 594 |
|
| 595 |
decoded_units = 1; |
| 596 |
if (code_unit <= 0x7F) { |
| 597 |
code_point = code_unit; |
| 598 |
expected_units = 1; |
| 599 |
} else if (code_unit <= 0xBF) { |
| 600 |
|
| 601 |
retval = CGPT_FAILED; |
| 602 |
break; |
| 603 |
} else if (code_unit >= 0xC2 && code_unit <= 0xDF) { |
| 604 |
code_point = code_unit & 0x1F; |
| 605 |
expected_units = 2; |
| 606 |
} else if (code_unit >= 0xE0 && code_unit <= 0xEF) { |
| 607 |
code_point = code_unit & 0x0F; |
| 608 |
expected_units = 3; |
| 609 |
} else if (code_unit >= 0xF0 && code_unit <= 0xF4) { |
| 610 |
code_point = code_unit & 0x07; |
| 611 |
expected_units = 4; |
| 612 |
} else { |
| 613 |
|
| 614 |
retval = CGPT_FAILED; |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
if ((retval == CGPT_OK) && |
| 621 |
(expected_units == decoded_units)) { |
| 622 |
|
| 623 |
switch (expected_units) { |
| 624 |
case 2: |
| 625 |
if (code_point <= 0x7F) retval = CGPT_FAILED; |
| 626 |
break; |
| 627 |
case 3: |
| 628 |
if (code_point <= 0x7FF) retval = CGPT_FAILED; |
| 629 |
break; |
| 630 |
case 4: |
| 631 |
if (code_point <= 0xFFFF) retval = CGPT_FAILED; |
| 632 |
break; |
| 633 |
} |
| 634 |
if (retval == CGPT_FAILED) break; |
| 635 |
|
| 636 |
if ((code_point <= 0xD7FF) || |
| 637 |
(code_point >= 0xE000 && code_point <= 0xFFFF)) { |
| 638 |
utf16[s16idx++] = code_point; |
| 639 |
maxoutput -= 1; |
| 640 |
} else if (code_point >= 0x10000 && code_point <= 0x10FFFF && |
| 641 |
maxoutput >= 2) { |
| 642 |
utf16[s16idx++] = 0xD800 | ((code_point >> 10) - 0x0040); |
| 643 |
utf16[s16idx++] = 0xDC00 | (code_point & 0x03FF); |
| 644 |
maxoutput -= 2; |
| 645 |
} else { |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
retval = CGPT_FAILED; |
| 652 |
break; |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
if (expected_units != decoded_units) { |
| 659 |
retval = CGPT_FAILED; |
| 660 |
} |
| 661 |
|
| 662 |
utf16[s16idx++] = 0; |
| 663 |
return retval; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
const Guid guid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE; |
| 668 |
const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; |
| 669 |
const Guid guid_chromeos_rootfs = GPT_ENT_TYPE_CHROMEOS_ROOTFS; |
| 670 |
const Guid guid_basic_data = GPT_ENT_TYPE_BASIC_DATA; |
| 671 |
const Guid guid_linux_data = GPT_ENT_TYPE_LINUX_FS; |
| 672 |
const Guid guid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED; |
| 673 |
const Guid guid_efi = GPT_ENT_TYPE_EFI; |
| 674 |
const Guid guid_unused = GPT_ENT_TYPE_UNUSED; |
| 675 |
const Guid guid_chromeos_minios = GPT_ENT_TYPE_CHROMEOS_MINIOS; |
| 676 |
const Guid guid_chromeos_hibernate = GPT_ENT_TYPE_CHROMEOS_HIBERNATE; |
| 677 |
|
| 678 |
static const struct { |
| 679 |
const Guid *type; |
| 680 |
const char *name; |
| 681 |
const char *description; |
| 682 |
} supported_types[] = { |
| 683 |
{&guid_chromeos_firmware, "firmware", "ChromeOS firmware"}, |
| 684 |
{&guid_chromeos_kernel, "kernel", "ChromeOS kernel"}, |
| 685 |
{&guid_chromeos_rootfs, "rootfs", "ChromeOS rootfs"}, |
| 686 |
{&guid_linux_data, "data", "Linux data"}, |
| 687 |
{&guid_basic_data, "basicdata", "Basic data"}, |
| 688 |
{&guid_chromeos_reserved, "reserved", "ChromeOS reserved"}, |
| 689 |
{&guid_efi, "efi", "EFI System Partition"}, |
| 690 |
{&guid_unused, "unused", "Unused (nonexistent) partition"}, |
| 691 |
{&guid_chromeos_minios, "minios", "ChromeOS miniOS"}, |
| 692 |
{&guid_chromeos_hibernate, "hibernate", "ChromeOS hibernate"}, |
| 693 |
}; |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
int ResolveType(const Guid *type, char *buf) { |
| 699 |
int i; |
| 700 |
for (i = 0; i < ARRAY_COUNT(supported_types); ++i) { |
| 701 |
if (!memcmp(type, supported_types[i].type, sizeof(Guid))) { |
| 702 |
strcpy(buf, supported_types[i].description); |
| 703 |
return CGPT_OK; |
| 704 |
} |
| 705 |
} |
| 706 |
return CGPT_FAILED; |
| 707 |
} |
| 708 |
|
| 709 |
int SupportedType(const char *name, Guid *type) { |
| 710 |
int i; |
| 711 |
for (i = 0; i < ARRAY_COUNT(supported_types); ++i) { |
| 712 |
if (!strcmp(name, supported_types[i].name)) { |
| 713 |
memcpy(type, supported_types[i].type, sizeof(Guid)); |
| 714 |
return CGPT_OK; |
| 715 |
} |
| 716 |
} |
| 717 |
return CGPT_FAILED; |
| 718 |
} |
| 719 |
|
| 720 |
void PrintTypes(void) { |
| 721 |
int i; |
| 722 |
printf("The partition type may also be given as one of these aliases:\n\n"); |
| 723 |
for (i = 0; i < ARRAY_COUNT(supported_types); ++i) { |
| 724 |
printf(" %-10s %s\n", supported_types[i].name, |
| 725 |
supported_types[i].description); |
| 726 |
} |
| 727 |
printf("\n"); |
| 728 |
} |
| 729 |
|
| 730 |
static GptHeader* GetGptHeader(const GptData *gpt) { |
| 731 |
if (gpt->valid_headers & MASK_PRIMARY) |
| 732 |
return (GptHeader*)gpt->primary_header; |
| 733 |
else if (gpt->valid_headers & MASK_SECONDARY) |
| 734 |
return (GptHeader*)gpt->secondary_header; |
| 735 |
else |
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
uint32_t GetNumberOfEntries(const struct drive *drive) { |
| 740 |
GptHeader *header = GetGptHeader(&drive->gpt); |
| 741 |
if (!header) |
| 742 |
return 0; |
| 743 |
return header->number_of_entries; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index) { |
| 748 |
GptHeader *header = GetGptHeader(gpt); |
| 749 |
uint8_t *entries; |
| 750 |
uint32_t stride = header->size_of_entry; |
| 751 |
require(stride); |
| 752 |
require(entry_index < header->number_of_entries); |
| 753 |
|
| 754 |
if (secondary == PRIMARY) { |
| 755 |
entries = gpt->primary_entries; |
| 756 |
} else if (secondary == SECONDARY) { |
| 757 |
entries = gpt->secondary_entries; |
| 758 |
} else { |
| 759 |
require(secondary == ANY_VALID); |
| 760 |
if (gpt->valid_entries & MASK_PRIMARY) { |
| 761 |
entries = gpt->primary_entries; |
| 762 |
} else { |
| 763 |
require(gpt->valid_entries & MASK_SECONDARY); |
| 764 |
entries = gpt->secondary_entries; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
return (GptEntry*)(&entries[stride * entry_index]); |
| 769 |
} |
| 770 |
|
| 771 |
void SetRequired(struct drive *drive, int secondary, uint32_t entry_index, |
| 772 |
int required) { |
| 773 |
require(required >= 0 && required <= CGPT_ATTRIBUTE_MAX_REQUIRED); |
| 774 |
GptEntry *entry; |
| 775 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 776 |
SetEntryRequired(entry, required); |
| 777 |
} |
| 778 |
|
| 779 |
int GetRequired(struct drive *drive, int secondary, uint32_t entry_index) { |
| 780 |
GptEntry *entry; |
| 781 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 782 |
return GetEntryRequired(entry); |
| 783 |
} |
| 784 |
|
| 785 |
void SetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index, |
| 786 |
int legacy_boot) { |
| 787 |
require(legacy_boot >= 0 && legacy_boot <= CGPT_ATTRIBUTE_MAX_LEGACY_BOOT); |
| 788 |
GptEntry *entry; |
| 789 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 790 |
SetEntryLegacyBoot(entry, legacy_boot); |
| 791 |
} |
| 792 |
|
| 793 |
int GetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index) { |
| 794 |
GptEntry *entry; |
| 795 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 796 |
return GetEntryLegacyBoot(entry); |
| 797 |
} |
| 798 |
|
| 799 |
void SetPriority(struct drive *drive, int secondary, uint32_t entry_index, |
| 800 |
int priority) { |
| 801 |
require(priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY); |
| 802 |
GptEntry *entry; |
| 803 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 804 |
SetEntryPriority(entry, priority); |
| 805 |
} |
| 806 |
|
| 807 |
int GetPriority(struct drive *drive, int secondary, uint32_t entry_index) { |
| 808 |
GptEntry *entry; |
| 809 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 810 |
return GetEntryPriority(entry); |
| 811 |
} |
| 812 |
|
| 813 |
void SetTries(struct drive *drive, int secondary, uint32_t entry_index, |
| 814 |
int tries) { |
| 815 |
require(tries >= 0 && tries <= CGPT_ATTRIBUTE_MAX_TRIES); |
| 816 |
GptEntry *entry; |
| 817 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 818 |
SetEntryTries(entry, tries); |
| 819 |
} |
| 820 |
|
| 821 |
int GetTries(struct drive *drive, int secondary, uint32_t entry_index) { |
| 822 |
GptEntry *entry; |
| 823 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 824 |
return GetEntryTries(entry); |
| 825 |
} |
| 826 |
|
| 827 |
void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index, |
| 828 |
int success) { |
| 829 |
require(success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESSFUL); |
| 830 |
GptEntry *entry; |
| 831 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 832 |
SetEntrySuccessful(entry, success); |
| 833 |
} |
| 834 |
|
| 835 |
int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index) { |
| 836 |
GptEntry *entry; |
| 837 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 838 |
return GetEntrySuccessful(entry); |
| 839 |
} |
| 840 |
|
| 841 |
void SetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index, |
| 842 |
int error_counter) { |
| 843 |
require(error_counter >= 0 && |
| 844 |
error_counter <= CGPT_ATTRIBUTE_MAX_ERROR_COUNTER); |
| 845 |
GptEntry *entry; |
| 846 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 847 |
SetEntryErrorCounter(entry, error_counter); |
| 848 |
} |
| 849 |
|
| 850 |
int GetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index) { |
| 851 |
GptEntry *entry; |
| 852 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 853 |
return GetEntryErrorCounter(entry); |
| 854 |
} |
| 855 |
|
| 856 |
void SetRaw(struct drive *drive, int secondary, uint32_t entry_index, |
| 857 |
uint32_t raw) { |
| 858 |
GptEntry *entry; |
| 859 |
entry = GetEntry(&drive->gpt, secondary, entry_index); |
| 860 |
entry->attrs.fields.gpt_att = (uint16_t)raw; |
| 861 |
} |
| 862 |
|
| 863 |
void UpdateAllEntries(struct drive *drive) { |
| 864 |
RepairEntries(&drive->gpt, MASK_PRIMARY); |
| 865 |
RepairHeader(&drive->gpt, MASK_PRIMARY); |
| 866 |
|
| 867 |
drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | |
| 868 |
GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); |
| 869 |
UpdateCrc(&drive->gpt); |
| 870 |
} |
| 871 |
|
| 872 |
int IsUnused(struct drive *drive, int secondary, uint32_t index) { |
| 873 |
GptEntry *entry; |
| 874 |
entry = GetEntry(&drive->gpt, secondary, index); |
| 875 |
return GuidIsZero(&entry->type); |
| 876 |
} |
| 877 |
|
| 878 |
int IsKernel(struct drive *drive, int secondary, uint32_t index) { |
| 879 |
GptEntry *entry; |
| 880 |
entry = GetEntry(&drive->gpt, secondary, index); |
| 881 |
return GuidEqual(&entry->type, &guid_chromeos_kernel); |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
#define TOSTRING(A) #A |
| 886 |
const char *GptError(int errnum) { |
| 887 |
const char *error_string[] = { |
| 888 |
TOSTRING(GPT_SUCCESS), |
| 889 |
TOSTRING(GPT_ERROR_NO_VALID_KERNEL), |
| 890 |
TOSTRING(GPT_ERROR_INVALID_HEADERS), |
| 891 |
TOSTRING(GPT_ERROR_INVALID_ENTRIES), |
| 892 |
TOSTRING(GPT_ERROR_INVALID_SECTOR_SIZE), |
| 893 |
TOSTRING(GPT_ERROR_INVALID_SECTOR_NUMBER), |
| 894 |
TOSTRING(GPT_ERROR_INVALID_UPDATE_TYPE) |
| 895 |
}; |
| 896 |
if (errnum < 0 || errnum >= ARRAY_COUNT(error_string)) |
| 897 |
return "<illegal value>"; |
| 898 |
return error_string[errnum]; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
void UpdateCrc(GptData *gpt) { |
| 903 |
GptHeader *primary_header, *secondary_header; |
| 904 |
|
| 905 |
primary_header = (GptHeader*)gpt->primary_header; |
| 906 |
secondary_header = (GptHeader*)gpt->secondary_header; |
| 907 |
|
| 908 |
if (gpt->modified & GPT_MODIFIED_ENTRIES1 && |
| 909 |
memcmp(primary_header, GPT_HEADER_SIGNATURE2, |
| 910 |
GPT_HEADER_SIGNATURE_SIZE)) { |
| 911 |
size_t entries_size = primary_header->size_of_entry * |
| 912 |
primary_header->number_of_entries; |
| 913 |
primary_header->entries_crc32 = |
| 914 |
Crc32(gpt->primary_entries, entries_size); |
| 915 |
} |
| 916 |
if (gpt->modified & GPT_MODIFIED_ENTRIES2) { |
| 917 |
size_t entries_size = secondary_header->size_of_entry * |
| 918 |
secondary_header->number_of_entries; |
| 919 |
secondary_header->entries_crc32 = |
| 920 |
Crc32(gpt->secondary_entries, entries_size); |
| 921 |
} |
| 922 |
if (gpt->modified & GPT_MODIFIED_HEADER1) { |
| 923 |
primary_header->header_crc32 = 0; |
| 924 |
primary_header->header_crc32 = Crc32( |
| 925 |
(const uint8_t *)primary_header, sizeof(GptHeader)); |
| 926 |
} |
| 927 |
if (gpt->modified & GPT_MODIFIED_HEADER2) { |
| 928 |
secondary_header->header_crc32 = 0; |
| 929 |
secondary_header->header_crc32 = Crc32( |
| 930 |
(const uint8_t *)secondary_header, sizeof(GptHeader)); |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
int IsSynonymous(const GptHeader* a, const GptHeader* b) { |
| 947 |
if ((a->first_usable_lba == b->first_usable_lba) && |
| 948 |
(a->last_usable_lba == b->last_usable_lba) && |
| 949 |
(a->number_of_entries == b->number_of_entries) && |
| 950 |
(a->size_of_entry == b->size_of_entry) && |
| 951 |
(!memcmp(&a->disk_uuid, &b->disk_uuid, sizeof(Guid)))) |
| 952 |
return 1; |
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries) { |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
GptHeader* h = (GptHeader*)(gpt->primary_header); |
| 971 |
if (!memcmp(h->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE)) |
| 972 |
return 0; |
| 973 |
|
| 974 |
if (gpt->valid_headers & MASK_PRIMARY) { |
| 975 |
h = (GptHeader*)gpt->primary_header; |
| 976 |
} else if (gpt->valid_headers & MASK_SECONDARY) { |
| 977 |
h = (GptHeader*)gpt->secondary_header; |
| 978 |
} else { |
| 979 |
|
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
size_t entries_size = h->number_of_entries * h->size_of_entry; |
| 984 |
if (valid_entries == MASK_BOTH) { |
| 985 |
if (memcmp(gpt->primary_entries, gpt->secondary_entries, entries_size)) { |
| 986 |
memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size); |
| 987 |
return GPT_MODIFIED_ENTRIES2; |
| 988 |
} |
| 989 |
} else if (valid_entries == MASK_PRIMARY) { |
| 990 |
memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size); |
| 991 |
return GPT_MODIFIED_ENTRIES2; |
| 992 |
} else if (valid_entries == MASK_SECONDARY) { |
| 993 |
memcpy(gpt->primary_entries, gpt->secondary_entries, entries_size); |
| 994 |
return GPT_MODIFIED_ENTRIES1; |
| 995 |
} |
| 996 |
|
| 997 |
return 0; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
static void CopySynonymousParts(GptHeader* target, const GptHeader* source) { |
| 1003 |
target->first_usable_lba = source->first_usable_lba; |
| 1004 |
target->last_usable_lba = source->last_usable_lba; |
| 1005 |
target->number_of_entries = source->number_of_entries; |
| 1006 |
target->size_of_entry = source->size_of_entry; |
| 1007 |
memcpy(&target->disk_uuid, &source->disk_uuid, sizeof(Guid)); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers) { |
| 1023 |
GptHeader *primary_header, *secondary_header; |
| 1024 |
|
| 1025 |
primary_header = (GptHeader*)gpt->primary_header; |
| 1026 |
secondary_header = (GptHeader*)gpt->secondary_header; |
| 1027 |
|
| 1028 |
if (valid_headers == MASK_BOTH) { |
| 1029 |
if (!IsSynonymous(primary_header, secondary_header)) { |
| 1030 |
CopySynonymousParts(secondary_header, primary_header); |
| 1031 |
return GPT_MODIFIED_HEADER2; |
| 1032 |
} |
| 1033 |
} else if (valid_headers == MASK_PRIMARY) { |
| 1034 |
memcpy(secondary_header, primary_header, sizeof(GptHeader)); |
| 1035 |
secondary_header->my_lba = gpt->gpt_drive_sectors - 1; |
| 1036 |
secondary_header->alternate_lba = primary_header->my_lba; |
| 1037 |
secondary_header->entries_lba = secondary_header->my_lba - |
| 1038 |
CalculateEntriesSectors(primary_header, gpt->sector_bytes); |
| 1039 |
return GPT_MODIFIED_HEADER2; |
| 1040 |
} else if (valid_headers == MASK_SECONDARY) { |
| 1041 |
memcpy(primary_header, secondary_header, sizeof(GptHeader)); |
| 1042 |
primary_header->my_lba = GPT_PMBR_SECTORS; |
| 1043 |
primary_header->alternate_lba = secondary_header->my_lba; |
| 1044 |
|
| 1045 |
primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTORS; |
| 1046 |
return GPT_MODIFIED_HEADER1; |
| 1047 |
} |
| 1048 |
|
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
|
| 1052 |
int CgptGetNumNonEmptyPartitions(CgptShowParams *params) { |
| 1053 |
struct drive drive; |
| 1054 |
int gpt_retval; |
| 1055 |
int retval; |
| 1056 |
|
| 1057 |
if (params == NULL) |
| 1058 |
return CGPT_FAILED; |
| 1059 |
|
| 1060 |
if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY, |
| 1061 |
params->drive_size)) |
| 1062 |
return CGPT_FAILED; |
| 1063 |
|
| 1064 |
if (GPT_SUCCESS != (gpt_retval = GptValidityCheck(&drive.gpt))) { |
| 1065 |
Error("GptValidityCheck() returned %d: %s\n", |
| 1066 |
gpt_retval, GptError(gpt_retval)); |
| 1067 |
retval = CGPT_FAILED; |
| 1068 |
goto done; |
| 1069 |
} |
| 1070 |
|
| 1071 |
params->num_partitions = 0; |
| 1072 |
int numEntries = GetNumberOfEntries(&drive); |
| 1073 |
int i; |
| 1074 |
for (i = 0; i < numEntries; i++) { |
| 1075 |
GptEntry *entry = GetEntry(&drive.gpt, ANY_VALID, i); |
| 1076 |
if (GuidIsZero(&entry->type)) |
| 1077 |
continue; |
| 1078 |
|
| 1079 |
params->num_partitions++; |
| 1080 |
} |
| 1081 |
|
| 1082 |
retval = CGPT_OK; |
| 1083 |
|
| 1084 |
done: |
| 1085 |
DriveClose(&drive, 0); |
| 1086 |
return retval; |
| 1087 |
} |
| 1088 |
|
| 1089 |
int GuidEqual(const Guid *guid1, const Guid *guid2) { |
| 1090 |
return (0 == memcmp(guid1, guid2, sizeof(Guid))); |
| 1091 |
} |
| 1092 |
|
| 1093 |
int GuidIsZero(const Guid *gp) { |
| 1094 |
return GuidEqual(gp, &guid_unused); |
| 1095 |
} |
| 1096 |
|
| 1097 |
void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen) { |
| 1098 |
char buf[GUID_STRLEN]; |
| 1099 |
if (GuidIsZero(&pmbr->boot_guid)) { |
| 1100 |
require(snprintf(str, buflen, "PMBR") < buflen); |
| 1101 |
} else { |
| 1102 |
GuidToStr(&pmbr->boot_guid, buf, sizeof(buf)); |
| 1103 |
require(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf) < buflen); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
#ifndef HAVE_MACOS |
| 1117 |
__attribute__((weak)) int GenerateGuid(Guid *newguid) { return CGPT_FAILED; }; |
| 1118 |
#endif |