1 /* Copyright 2010 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c
6 * files for more details.
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
LogToStderr(const char * tag,const char * format,va_list ap)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
Error(const char * format,...)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
Warning(const char * format,...)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
check_int_parse(char option,const char * buf)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
check_int_limit(char option,int val,int low,int high)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
CheckValid(const struct drive * drive)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
Load(struct drive * drive,uint8_t * buf,const uint64_t sector,const uint64_t sector_bytes,const uint64_t sector_count)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; /* byte count to read */
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 /* Make sure that sector_bytes * sector_count doesn't roll over. */
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
ReadPMBR(struct drive * drive)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
WritePMBR(struct drive * drive)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
Save(struct drive * drive,const uint8_t * buf,const uint64_t sector,const uint64_t sector_bytes,const uint64_t sector_count)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; /* byte count to write */
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
GptLoad(struct drive * drive,uint32_t sector_bytes)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 /* TODO(namnguyen): Remove this and totally trust gpt_drive_sectors. */
175 if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL)) {
176 drive->gpt.gpt_drive_sectors = drive->gpt.streaming_drive_sectors;
177 } /* Else, we trust gpt.gpt_drive_sectors. */
178
179 // Read the data.
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
GptSave(struct drive * drive)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 // Sync primary GPT before touching secondary so one is always valid.
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 // Only start writing secondary GPT if primary was written correctly.
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 * Query drive size and bytes per sector. Return zero on success. On error,
291 * -1 is returned and errno is set appropriately.
292 */
ObtainDriveSize(int fd,uint64_t * size,uint32_t * sector_bytes)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; /* bytes */
308 *size = stat.st_size;
309 }
310 #else
311 *sector_bytes = 512; /* bytes */
312 *size = stat.st_size;
313 #endif
314 return 0;
315 }
316
DriveOpen(const char * drive_path,struct drive * drive,int mode,uint64_t drive_size)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 // Clear struct for proper error handling.
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 // We just load the data. Caller must validate it.
359 return CGPT_OK;
360
361 error_close:
362 (void) DriveClose(drive, 0);
363 return CGPT_FAILED;
364 }
365
366
DriveClose(struct drive * drive,int update_as_needed)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 // Sync early! Only sync file descriptor here, and leave the whole system sync
386 // outside cgpt because whole system sync would trigger tons of disk accesses
387 // and timeout tests.
388 fsync(drive->fd);
389
390 close(drive->fd);
391
392 return errors ? CGPT_FAILED : CGPT_OK;
393 }
394
DriveLastUsableLBA(const struct drive * drive)395 uint64_t DriveLastUsableLBA(const struct drive *drive) {
396 GptHeader *h = (GptHeader *)drive->gpt.primary_header;
397
398 if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL))
399 return (drive->gpt.streaming_drive_sectors - GPT_HEADER_SECTORS
400 - CalculateEntriesSectors(h, drive->gpt.sector_bytes) - 1);
401
402 return (drive->gpt.streaming_drive_sectors - 1);
403 }
404
405 /* GUID conversion functions. Accepted format:
406 *
407 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
408 *
409 * Returns CGPT_OK if parsing is successful; otherwise CGPT_FAILED.
410 */
StrToGuid(const char * str,Guid * guid)411 int StrToGuid(const char *str, Guid *guid) {
412 uint32_t time_low;
413 uint16_t time_mid;
414 uint16_t time_high_and_version;
415 unsigned int chunk[11];
416
417 if (11 != sscanf(str, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
418 chunk+0,
419 chunk+1,
420 chunk+2,
421 chunk+3,
422 chunk+4,
423 chunk+5,
424 chunk+6,
425 chunk+7,
426 chunk+8,
427 chunk+9,
428 chunk+10)) {
429 printf("FAILED\n");
430 return CGPT_FAILED;
431 }
432
433 time_low = chunk[0] & 0xffffffff;
434 time_mid = chunk[1] & 0xffff;
435 time_high_and_version = chunk[2] & 0xffff;
436
437 guid->u.Uuid.time_low = htole32(time_low);
438 guid->u.Uuid.time_mid = htole16(time_mid);
439 guid->u.Uuid.time_high_and_version = htole16(time_high_and_version);
440
441 guid->u.Uuid.clock_seq_high_and_reserved = chunk[3] & 0xff;
442 guid->u.Uuid.clock_seq_low = chunk[4] & 0xff;
443 guid->u.Uuid.node[0] = chunk[5] & 0xff;
444 guid->u.Uuid.node[1] = chunk[6] & 0xff;
445 guid->u.Uuid.node[2] = chunk[7] & 0xff;
446 guid->u.Uuid.node[3] = chunk[8] & 0xff;
447 guid->u.Uuid.node[4] = chunk[9] & 0xff;
448 guid->u.Uuid.node[5] = chunk[10] & 0xff;
449
450 return CGPT_OK;
451 }
GuidToStr(const Guid * guid,char * str,unsigned int buflen)452 void GuidToStr(const Guid *guid, char *str, unsigned int buflen) {
453 require(buflen >= GUID_STRLEN);
454 require(snprintf(str, buflen,
455 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
456 le32toh(guid->u.Uuid.time_low),
457 le16toh(guid->u.Uuid.time_mid),
458 le16toh(guid->u.Uuid.time_high_and_version),
459 guid->u.Uuid.clock_seq_high_and_reserved,
460 guid->u.Uuid.clock_seq_low,
461 guid->u.Uuid.node[0], guid->u.Uuid.node[1],
462 guid->u.Uuid.node[2], guid->u.Uuid.node[3],
463 guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1);
464 }
465
466 /* Convert possibly unterminated UTF16 string to UTF8.
467 * Caller must prepare enough space for UTF8, which could be up to
468 * twice the byte length of UTF16 string plus the terminating '\0'.
469 * See the following table for encoding lengths.
470 *
471 * Code point UTF16 UTF8
472 * 0x0000-0x007F 2 bytes 1 byte
473 * 0x0080-0x07FF 2 bytes 2 bytes
474 * 0x0800-0xFFFF 2 bytes 3 bytes
475 * 0x10000-0x10FFFF 4 bytes 4 bytes
476 *
477 * This function uses a simple state meachine to convert UTF-16 char(s) to
478 * a code point. Once a code point is parsed out, the state machine throws
479 * out sequencial UTF-8 chars in one time.
480 *
481 * Return: CGPT_OK --- all character are converted successfully.
482 * CGPT_FAILED --- convert error, i.e. output buffer is too short.
483 */
UTF16ToUTF8(const uint16_t * utf16,unsigned int maxinput,uint8_t * utf8,unsigned int maxoutput)484 int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
485 uint8_t *utf8, unsigned int maxoutput)
486 {
487 size_t s16idx, s8idx;
488 uint32_t code_point = 0;
489 int code_point_ready = 1; // code point is ready to output.
490 int retval = CGPT_OK;
491
492 if (!utf16 || !maxinput || !utf8 || !maxoutput)
493 return CGPT_FAILED;
494
495 maxoutput--; /* plan for termination now */
496
497 for (s16idx = s8idx = 0;
498 s16idx < maxinput && utf16[s16idx] && maxoutput;
499 s16idx++) {
500 uint16_t codeunit = le16toh(utf16[s16idx]);
501
502 if (code_point_ready) {
503 if (codeunit >= 0xD800 && codeunit <= 0xDBFF) {
504 /* high surrogate, need the low surrogate. */
505 code_point_ready = 0;
506 code_point = (codeunit & 0x03FF) + 0x0040;
507 } else {
508 /* BMP char, output it. */
509 code_point = codeunit;
510 }
511 } else {
512 /* expect the low surrogate */
513 if (codeunit >= 0xDC00 && codeunit <= 0xDFFF) {
514 code_point = (code_point << 10) | (codeunit & 0x03FF);
515 code_point_ready = 1;
516 } else {
517 /* the second code unit is NOT the low surrogate. Unexpected. */
518 code_point_ready = 0;
519 retval = CGPT_FAILED;
520 break;
521 }
522 }
523
524 /* If UTF code point is ready, output it. */
525 if (code_point_ready) {
526 require(code_point <= 0x10FFFF);
527 if (code_point <= 0x7F && maxoutput >= 1) {
528 maxoutput -= 1;
529 utf8[s8idx++] = code_point & 0x7F;
530 } else if (code_point <= 0x7FF && maxoutput >= 2) {
531 maxoutput -= 2;
532 utf8[s8idx++] = 0xC0 | (code_point >> 6);
533 utf8[s8idx++] = 0x80 | (code_point & 0x3F);
534 } else if (code_point <= 0xFFFF && maxoutput >= 3) {
535 maxoutput -= 3;
536 utf8[s8idx++] = 0xE0 | (code_point >> 12);
537 utf8[s8idx++] = 0x80 | ((code_point >> 6) & 0x3F);
538 utf8[s8idx++] = 0x80 | (code_point & 0x3F);
539 } else if (code_point <= 0x10FFFF && maxoutput >= 4) {
540 maxoutput -= 4;
541 utf8[s8idx++] = 0xF0 | (code_point >> 18);
542 utf8[s8idx++] = 0x80 | ((code_point >> 12) & 0x3F);
543 utf8[s8idx++] = 0x80 | ((code_point >> 6) & 0x3F);
544 utf8[s8idx++] = 0x80 | (code_point & 0x3F);
545 } else {
546 /* buffer underrun */
547 retval = CGPT_FAILED;
548 break;
549 }
550 }
551 }
552 utf8[s8idx++] = 0;
553 return retval;
554 }
555
556 /* Convert UTF8 string to UTF16. The UTF8 string must be null-terminated.
557 * Caller must prepare enough space for UTF16, including a terminating 0x0000.
558 * See the following table for encoding lengths. In any case, the caller
559 * just needs to prepare the byte length of UTF8 plus the terminating 0x0000.
560 *
561 * Code point UTF16 UTF8
562 * 0x0000-0x007F 2 bytes 1 byte
563 * 0x0080-0x07FF 2 bytes 2 bytes
564 * 0x0800-0xFFFF 2 bytes 3 bytes
565 * 0x10000-0x10FFFF 4 bytes 4 bytes
566 *
567 * This function converts UTF8 chars to a code point first. Then, convrts it
568 * to UTF16 code unit(s).
569 *
570 * Return: CGPT_OK --- all character are converted successfully.
571 * CGPT_FAILED --- convert error, i.e. output buffer is too short.
572 */
UTF8ToUTF16(const uint8_t * utf8,uint16_t * utf16,unsigned int maxoutput)573 int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput)
574 {
575 size_t s16idx, s8idx;
576 uint32_t code_point = 0;
577 unsigned int expected_units = 1;
578 unsigned int decoded_units = 1;
579 int retval = CGPT_OK;
580
581 if (!utf8 || !utf16 || !maxoutput)
582 return CGPT_FAILED;
583
584 maxoutput--; /* plan for termination */
585
586 for (s8idx = s16idx = 0;
587 utf8[s8idx] && maxoutput;
588 s8idx++) {
589 uint8_t code_unit;
590 code_unit = utf8[s8idx];
591
592 if (expected_units != decoded_units) {
593 /* Trailing bytes of multi-byte character */
594 if ((code_unit & 0xC0) == 0x80) {
595 code_point = (code_point << 6) | (code_unit & 0x3F);
596 ++decoded_units;
597 } else {
598 /* Unexpected code unit. */
599 retval = CGPT_FAILED;
600 break;
601 }
602 } else {
603 /* parsing a new code point. */
604 decoded_units = 1;
605 if (code_unit <= 0x7F) {
606 code_point = code_unit;
607 expected_units = 1;
608 } else if (code_unit <= 0xBF) {
609 /* 0x80-0xBF must NOT be the heading byte unit of a new code point. */
610 retval = CGPT_FAILED;
611 break;
612 } else if (code_unit >= 0xC2 && code_unit <= 0xDF) {
613 code_point = code_unit & 0x1F;
614 expected_units = 2;
615 } else if (code_unit >= 0xE0 && code_unit <= 0xEF) {
616 code_point = code_unit & 0x0F;
617 expected_units = 3;
618 } else if (code_unit >= 0xF0 && code_unit <= 0xF4) {
619 code_point = code_unit & 0x07;
620 expected_units = 4;
621 } else {
622 /* illegal code unit: 0xC0-0xC1, 0xF5-0xFF */
623 retval = CGPT_FAILED;
624 break;
625 }
626 }
627
628 /* If no more unit is needed, output the UTF16 unit(s). */
629 if ((retval == CGPT_OK) &&
630 (expected_units == decoded_units)) {
631 /* Check if the encoding is the shortest possible UTF-8 sequence. */
632 switch (expected_units) {
633 case 2:
634 if (code_point <= 0x7F) retval = CGPT_FAILED;
635 break;
636 case 3:
637 if (code_point <= 0x7FF) retval = CGPT_FAILED;
638 break;
639 case 4:
640 if (code_point <= 0xFFFF) retval = CGPT_FAILED;
641 break;
642 }
643 if (retval == CGPT_FAILED) break; /* leave immediately */
644
645 if ((code_point <= 0xD7FF) ||
646 (code_point >= 0xE000 && code_point <= 0xFFFF)) {
647 utf16[s16idx++] = code_point;
648 maxoutput -= 1;
649 } else if (code_point >= 0x10000 && code_point <= 0x10FFFF &&
650 maxoutput >= 2) {
651 utf16[s16idx++] = 0xD800 | ((code_point >> 10) - 0x0040);
652 utf16[s16idx++] = 0xDC00 | (code_point & 0x03FF);
653 maxoutput -= 2;
654 } else {
655 /* Three possibilities fall into here. Both are failure cases.
656 * a. surrogate pair (non-BMP characters; 0xD800~0xDFFF)
657 * b. invalid code point > 0x10FFFF
658 * c. buffer underrun
659 */
660 retval = CGPT_FAILED;
661 break;
662 }
663 }
664 }
665
666 /* A null-terminator shows up before the UTF8 sequence ends. */
667 if (expected_units != decoded_units) {
668 retval = CGPT_FAILED;
669 }
670
671 utf16[s16idx++] = 0;
672 return retval;
673 }
674
675 /* global types to compare against */
676 const Guid guid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
677 const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
678 const Guid guid_chromeos_rootfs = GPT_ENT_TYPE_CHROMEOS_ROOTFS;
679 const Guid guid_android_vbmeta = GPT_ENT_TYPE_ANDROID_VBMETA;
680 const Guid guid_basic_data = GPT_ENT_TYPE_BASIC_DATA;
681 const Guid guid_linux_data = GPT_ENT_TYPE_LINUX_FS;
682 const Guid guid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
683 const Guid guid_efi = GPT_ENT_TYPE_EFI;
684 const Guid guid_unused = GPT_ENT_TYPE_UNUSED;
685 const Guid guid_chromeos_minios = GPT_ENT_TYPE_CHROMEOS_MINIOS;
686 const Guid guid_chromeos_hibernate = GPT_ENT_TYPE_CHROMEOS_HIBERNATE;
687
688 static const struct {
689 const Guid *type;
690 const char *name;
691 const char *description;
692 } supported_types[] = {
693 {&guid_chromeos_firmware, "firmware", "ChromeOS firmware"},
694 {&guid_chromeos_kernel, "kernel", "ChromeOS kernel"},
695 {&guid_chromeos_rootfs, "rootfs", "ChromeOS rootfs"},
696 {&guid_android_vbmeta, "vbmeta", "Android vbmeta"},
697 {&guid_linux_data, "data", "Linux data"},
698 {&guid_basic_data, "basicdata", "Basic data"},
699 {&guid_chromeos_reserved, "reserved", "ChromeOS reserved"},
700 {&guid_efi, "efi", "EFI System Partition"},
701 {&guid_unused, "unused", "Unused (nonexistent) partition"},
702 {&guid_chromeos_minios, "minios", "ChromeOS miniOS"},
703 {&guid_chromeos_hibernate, "hibernate", "ChromeOS hibernate"},
704 };
705
706 /* Resolves human-readable GPT type.
707 * Returns CGPT_OK if found.
708 * Returns CGPT_FAILED if no known type found. */
ResolveType(const Guid * type,char * buf)709 int ResolveType(const Guid *type, char *buf) {
710 int i;
711 for (i = 0; i < ARRAY_COUNT(supported_types); ++i) {
712 if (!memcmp(type, supported_types[i].type, sizeof(Guid))) {
713 strcpy(buf, supported_types[i].description);
714 return CGPT_OK;
715 }
716 }
717 return CGPT_FAILED;
718 }
719
SupportedType(const char * name,Guid * type)720 int SupportedType(const char *name, Guid *type) {
721 int i;
722 for (i = 0; i < ARRAY_COUNT(supported_types); ++i) {
723 if (!strcmp(name, supported_types[i].name)) {
724 memcpy(type, supported_types[i].type, sizeof(Guid));
725 return CGPT_OK;
726 }
727 }
728 return CGPT_FAILED;
729 }
730
PrintTypes(void)731 void PrintTypes(void) {
732 int i;
733 printf("The partition type may also be given as one of these aliases:\n\n");
734 for (i = 0; i < ARRAY_COUNT(supported_types); ++i) {
735 printf(" %-10s %s\n", supported_types[i].name,
736 supported_types[i].description);
737 }
738 printf("\n");
739 }
740
GetGptHeader(const GptData * gpt)741 static GptHeader* GetGptHeader(const GptData *gpt) {
742 if (gpt->valid_headers & MASK_PRIMARY)
743 return (GptHeader*)gpt->primary_header;
744 else if (gpt->valid_headers & MASK_SECONDARY)
745 return (GptHeader*)gpt->secondary_header;
746 else
747 return 0;
748 }
749
GetNumberOfEntries(const struct drive * drive)750 uint32_t GetNumberOfEntries(const struct drive *drive) {
751 GptHeader *header = GetGptHeader(&drive->gpt);
752 if (!header)
753 return 0;
754 return header->number_of_entries;
755 }
756
757
GetEntry(GptData * gpt,int secondary,uint32_t entry_index)758 GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index) {
759 GptHeader *header = GetGptHeader(gpt);
760 uint8_t *entries;
761 uint32_t stride = header->size_of_entry;
762 require(stride);
763 require(entry_index < header->number_of_entries);
764
765 if (secondary == PRIMARY) {
766 entries = gpt->primary_entries;
767 } else if (secondary == SECONDARY) {
768 entries = gpt->secondary_entries;
769 } else { /* ANY_VALID */
770 require(secondary == ANY_VALID);
771 if (gpt->valid_entries & MASK_PRIMARY) {
772 entries = gpt->primary_entries;
773 } else {
774 require(gpt->valid_entries & MASK_SECONDARY);
775 entries = gpt->secondary_entries;
776 }
777 }
778
779 return (GptEntry*)(&entries[stride * entry_index]);
780 }
781
SetRequired(struct drive * drive,int secondary,uint32_t entry_index,int required)782 void SetRequired(struct drive *drive, int secondary, uint32_t entry_index,
783 int required) {
784 require(required >= 0 && required <= CGPT_ATTRIBUTE_MAX_REQUIRED);
785 GptEntry *entry;
786 entry = GetEntry(&drive->gpt, secondary, entry_index);
787 SetEntryRequired(entry, required);
788 }
789
GetRequired(struct drive * drive,int secondary,uint32_t entry_index)790 int GetRequired(struct drive *drive, int secondary, uint32_t entry_index) {
791 GptEntry *entry;
792 entry = GetEntry(&drive->gpt, secondary, entry_index);
793 return GetEntryRequired(entry);
794 }
795
SetLegacyBoot(struct drive * drive,int secondary,uint32_t entry_index,int legacy_boot)796 void SetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index,
797 int legacy_boot) {
798 require(legacy_boot >= 0 && legacy_boot <= CGPT_ATTRIBUTE_MAX_LEGACY_BOOT);
799 GptEntry *entry;
800 entry = GetEntry(&drive->gpt, secondary, entry_index);
801 SetEntryLegacyBoot(entry, legacy_boot);
802 }
803
GetLegacyBoot(struct drive * drive,int secondary,uint32_t entry_index)804 int GetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index) {
805 GptEntry *entry;
806 entry = GetEntry(&drive->gpt, secondary, entry_index);
807 return GetEntryLegacyBoot(entry);
808 }
809
SetPriority(struct drive * drive,int secondary,uint32_t entry_index,int priority)810 void SetPriority(struct drive *drive, int secondary, uint32_t entry_index,
811 int priority) {
812 require(priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY);
813 GptEntry *entry;
814 entry = GetEntry(&drive->gpt, secondary, entry_index);
815 SetEntryPriority(entry, priority);
816 }
817
GetPriority(struct drive * drive,int secondary,uint32_t entry_index)818 int GetPriority(struct drive *drive, int secondary, uint32_t entry_index) {
819 GptEntry *entry;
820 entry = GetEntry(&drive->gpt, secondary, entry_index);
821 return GetEntryPriority(entry);
822 }
823
SetTries(struct drive * drive,int secondary,uint32_t entry_index,int tries)824 void SetTries(struct drive *drive, int secondary, uint32_t entry_index,
825 int tries) {
826 require(tries >= 0 && tries <= CGPT_ATTRIBUTE_MAX_TRIES);
827 GptEntry *entry;
828 entry = GetEntry(&drive->gpt, secondary, entry_index);
829 SetEntryTries(entry, tries);
830 }
831
GetTries(struct drive * drive,int secondary,uint32_t entry_index)832 int GetTries(struct drive *drive, int secondary, uint32_t entry_index) {
833 GptEntry *entry;
834 entry = GetEntry(&drive->gpt, secondary, entry_index);
835 return GetEntryTries(entry);
836 }
837
SetSuccessful(struct drive * drive,int secondary,uint32_t entry_index,int success)838 void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index,
839 int success) {
840 require(success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESSFUL);
841 GptEntry *entry;
842 entry = GetEntry(&drive->gpt, secondary, entry_index);
843 SetEntrySuccessful(entry, success);
844 }
845
GetSuccessful(struct drive * drive,int secondary,uint32_t entry_index)846 int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index) {
847 GptEntry *entry;
848 entry = GetEntry(&drive->gpt, secondary, entry_index);
849 return GetEntrySuccessful(entry);
850 }
851
SetErrorCounter(struct drive * drive,int secondary,uint32_t entry_index,int error_counter)852 void SetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index,
853 int error_counter) {
854 require(error_counter >= 0 &&
855 error_counter <= CGPT_ATTRIBUTE_MAX_ERROR_COUNTER);
856 GptEntry *entry;
857 entry = GetEntry(&drive->gpt, secondary, entry_index);
858 SetEntryErrorCounter(entry, error_counter);
859 }
860
GetErrorCounter(struct drive * drive,int secondary,uint32_t entry_index)861 int GetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index) {
862 GptEntry *entry;
863 entry = GetEntry(&drive->gpt, secondary, entry_index);
864 return GetEntryErrorCounter(entry);
865 }
866
SetRaw(struct drive * drive,int secondary,uint32_t entry_index,uint32_t raw)867 void SetRaw(struct drive *drive, int secondary, uint32_t entry_index,
868 uint32_t raw) {
869 GptEntry *entry;
870 entry = GetEntry(&drive->gpt, secondary, entry_index);
871 entry->attrs.fields.gpt_att = (uint16_t)raw;
872 }
873
UpdateAllEntries(struct drive * drive)874 void UpdateAllEntries(struct drive *drive) {
875 RepairEntries(&drive->gpt, MASK_PRIMARY);
876 RepairHeader(&drive->gpt, MASK_PRIMARY);
877
878 drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
879 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
880 UpdateCrc(&drive->gpt);
881 }
882
IsUnused(struct drive * drive,int secondary,uint32_t index)883 int IsUnused(struct drive *drive, int secondary, uint32_t index) {
884 GptEntry *entry;
885 entry = GetEntry(&drive->gpt, secondary, index);
886 return GuidIsZero(&entry->type);
887 }
888
IsBootable(struct drive * drive,int secondary,uint32_t index)889 int IsBootable(struct drive *drive, int secondary, uint32_t index) {
890 GptEntry *entry;
891 entry = GetEntry(&drive->gpt, secondary, index);
892 return (GuidEqual(&entry->type, &guid_chromeos_kernel) ||
893 GuidEqual(&entry->type, &guid_android_vbmeta));
894 }
895
896
897 #define TOSTRING(A) #A
GptError(int errnum)898 const char *GptError(int errnum) {
899 const char *error_string[] = {
900 TOSTRING(GPT_SUCCESS),
901 TOSTRING(GPT_ERROR_NO_VALID_KERNEL),
902 TOSTRING(GPT_ERROR_INVALID_HEADERS),
903 TOSTRING(GPT_ERROR_INVALID_ENTRIES),
904 TOSTRING(GPT_ERROR_INVALID_SECTOR_SIZE),
905 TOSTRING(GPT_ERROR_INVALID_SECTOR_NUMBER),
906 TOSTRING(GPT_ERROR_INVALID_UPDATE_TYPE)
907 };
908 if (errnum < 0 || errnum >= ARRAY_COUNT(error_string))
909 return "<illegal value>";
910 return error_string[errnum];
911 }
912
913 /* Update CRC value if necessary. */
UpdateCrc(GptData * gpt)914 void UpdateCrc(GptData *gpt) {
915 GptHeader *primary_header, *secondary_header;
916
917 primary_header = (GptHeader*)gpt->primary_header;
918 secondary_header = (GptHeader*)gpt->secondary_header;
919
920 if (gpt->modified & GPT_MODIFIED_ENTRIES1 &&
921 memcmp(primary_header, GPT_HEADER_SIGNATURE2,
922 GPT_HEADER_SIGNATURE_SIZE)) {
923 size_t entries_size = primary_header->size_of_entry *
924 primary_header->number_of_entries;
925 primary_header->entries_crc32 =
926 Crc32(gpt->primary_entries, entries_size);
927 }
928 if (gpt->modified & GPT_MODIFIED_ENTRIES2) {
929 size_t entries_size = secondary_header->size_of_entry *
930 secondary_header->number_of_entries;
931 secondary_header->entries_crc32 =
932 Crc32(gpt->secondary_entries, entries_size);
933 }
934 if (gpt->modified & GPT_MODIFIED_HEADER1) {
935 primary_header->header_crc32 = 0;
936 primary_header->header_crc32 = Crc32(
937 (const uint8_t *)primary_header, sizeof(GptHeader));
938 }
939 if (gpt->modified & GPT_MODIFIED_HEADER2) {
940 secondary_header->header_crc32 = 0;
941 secondary_header->header_crc32 = Crc32(
942 (const uint8_t *)secondary_header, sizeof(GptHeader));
943 }
944 }
945 /* Two headers are NOT bitwise identical. For example, my_lba pointers to header
946 * itself so that my_lba in primary and secondary is definitely different.
947 * Only the following fields should be identical.
948 *
949 * first_usable_lba
950 * last_usable_lba
951 * number_of_entries
952 * size_of_entry
953 * disk_uuid
954 *
955 * If any of above field are not matched, overwrite secondary with primary since
956 * we always trust primary.
957 * If any one of header is invalid, copy from another. */
IsSynonymous(const GptHeader * a,const GptHeader * b)958 int IsSynonymous(const GptHeader* a, const GptHeader* b) {
959 if ((a->first_usable_lba == b->first_usable_lba) &&
960 (a->last_usable_lba == b->last_usable_lba) &&
961 (a->number_of_entries == b->number_of_entries) &&
962 (a->size_of_entry == b->size_of_entry) &&
963 (!memcmp(&a->disk_uuid, &b->disk_uuid, sizeof(Guid))))
964 return 1;
965 return 0;
966 }
967
968 /* Primary entries and secondary entries should be bitwise identical.
969 * If two entries tables are valid, compare them. If not the same,
970 * overwrites secondary with primary (primary always has higher priority),
971 * and marks secondary as modified.
972 * If only one is valid, overwrites invalid one.
973 * If all are invalid, does nothing.
974 * This function returns bit masks for GptData.modified field.
975 * Note that CRC is NOT re-computed in this function.
976 */
RepairEntries(GptData * gpt,const uint32_t valid_entries)977 uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries) {
978 /* If we have an alternate GPT header signature, don't overwrite
979 * the secondary GPT with the primary one as that might wipe the
980 * partition table. Also don't overwrite the primary one with the
981 * secondary one as that will stop Windows from booting. */
982 GptHeader* h = (GptHeader*)(gpt->primary_header);
983 if (!memcmp(h->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE))
984 return 0;
985
986 if (gpt->valid_headers & MASK_PRIMARY) {
987 h = (GptHeader*)gpt->primary_header;
988 } else if (gpt->valid_headers & MASK_SECONDARY) {
989 h = (GptHeader*)gpt->secondary_header;
990 } else {
991 /* We cannot trust any header, don't update entries. */
992 return 0;
993 }
994
995 size_t entries_size = h->number_of_entries * h->size_of_entry;
996 if (valid_entries == MASK_BOTH) {
997 if (memcmp(gpt->primary_entries, gpt->secondary_entries, entries_size)) {
998 memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size);
999 return GPT_MODIFIED_ENTRIES2;
1000 }
1001 } else if (valid_entries == MASK_PRIMARY) {
1002 memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size);
1003 return GPT_MODIFIED_ENTRIES2;
1004 } else if (valid_entries == MASK_SECONDARY) {
1005 memcpy(gpt->primary_entries, gpt->secondary_entries, entries_size);
1006 return GPT_MODIFIED_ENTRIES1;
1007 }
1008
1009 return 0;
1010 }
1011
1012 /* The above five fields are shared between primary and secondary headers.
1013 * We can recover one header from another through copying those fields. */
CopySynonymousParts(GptHeader * target,const GptHeader * source)1014 static void CopySynonymousParts(GptHeader* target, const GptHeader* source) {
1015 target->first_usable_lba = source->first_usable_lba;
1016 target->last_usable_lba = source->last_usable_lba;
1017 target->number_of_entries = source->number_of_entries;
1018 target->size_of_entry = source->size_of_entry;
1019 memcpy(&target->disk_uuid, &source->disk_uuid, sizeof(Guid));
1020 }
1021
1022 /* This function repairs primary and secondary headers if possible.
1023 * If both headers are valid (CRC32 is correct) but
1024 * a) indicate inconsistent usable LBA ranges,
1025 * b) inconsistent partition entry size and number,
1026 * c) inconsistent disk_uuid,
1027 * we will use the primary header to overwrite secondary header.
1028 * If primary is invalid (CRC32 is wrong), then we repair it from secondary.
1029 * If secondary is invalid (CRC32 is wrong), then we repair it from primary.
1030 * This function returns the bitmasks for modified header.
1031 * Note that CRC value is NOT re-computed in this function. UpdateCrc() will
1032 * do it later.
1033 */
RepairHeader(GptData * gpt,const uint32_t valid_headers)1034 uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers) {
1035 GptHeader *primary_header, *secondary_header;
1036
1037 primary_header = (GptHeader*)gpt->primary_header;
1038 secondary_header = (GptHeader*)gpt->secondary_header;
1039
1040 if (valid_headers == MASK_BOTH) {
1041 if (!IsSynonymous(primary_header, secondary_header)) {
1042 CopySynonymousParts(secondary_header, primary_header);
1043 return GPT_MODIFIED_HEADER2;
1044 }
1045 } else if (valid_headers == MASK_PRIMARY) {
1046 memcpy(secondary_header, primary_header, sizeof(GptHeader));
1047 secondary_header->my_lba = gpt->gpt_drive_sectors - 1; /* the last sector */
1048 secondary_header->alternate_lba = primary_header->my_lba;
1049 secondary_header->entries_lba = secondary_header->my_lba -
1050 CalculateEntriesSectors(primary_header, gpt->sector_bytes);
1051 return GPT_MODIFIED_HEADER2;
1052 } else if (valid_headers == MASK_SECONDARY) {
1053 memcpy(primary_header, secondary_header, sizeof(GptHeader));
1054 primary_header->my_lba = GPT_PMBR_SECTORS; /* the second sector on drive */
1055 primary_header->alternate_lba = secondary_header->my_lba;
1056 /* TODO (namnguyen): Preserve (header, entries) padding space. */
1057 primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTORS;
1058 return GPT_MODIFIED_HEADER1;
1059 }
1060
1061 return 0;
1062 }
1063
CgptGetNumNonEmptyPartitions(CgptShowParams * params)1064 int CgptGetNumNonEmptyPartitions(CgptShowParams *params) {
1065 struct drive drive;
1066 int gpt_retval;
1067 int retval;
1068
1069 if (params == NULL)
1070 return CGPT_FAILED;
1071
1072 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY,
1073 params->drive_size))
1074 return CGPT_FAILED;
1075
1076 if (GPT_SUCCESS != (gpt_retval = GptValidityCheck(&drive.gpt))) {
1077 Error("GptValidityCheck() returned %d: %s\n",
1078 gpt_retval, GptError(gpt_retval));
1079 retval = CGPT_FAILED;
1080 goto done;
1081 }
1082
1083 params->num_partitions = 0;
1084 int numEntries = GetNumberOfEntries(&drive);
1085 int i;
1086 for (i = 0; i < numEntries; i++) {
1087 GptEntry *entry = GetEntry(&drive.gpt, ANY_VALID, i);
1088 if (GuidIsZero(&entry->type))
1089 continue;
1090
1091 params->num_partitions++;
1092 }
1093
1094 retval = CGPT_OK;
1095
1096 done:
1097 DriveClose(&drive, 0);
1098 return retval;
1099 }
1100
GuidEqual(const Guid * guid1,const Guid * guid2)1101 int GuidEqual(const Guid *guid1, const Guid *guid2) {
1102 return (0 == memcmp(guid1, guid2, sizeof(Guid)));
1103 }
1104
GuidIsZero(const Guid * gp)1105 int GuidIsZero(const Guid *gp) {
1106 return GuidEqual(gp, &guid_unused);
1107 }
1108
PMBRToStr(struct pmbr * pmbr,char * str,unsigned int buflen)1109 void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen) {
1110 char buf[GUID_STRLEN];
1111 if (GuidIsZero(&pmbr->boot_guid)) {
1112 require(snprintf(str, buflen, "PMBR") < buflen);
1113 } else {
1114 GuidToStr(&pmbr->boot_guid, buf, sizeof(buf));
1115 require(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf) < buflen);
1116 }
1117 }
1118
1119 /*
1120 * This is here because some CGPT functionality is provided in libvboot_host.a
1121 * for other host utilities. GenerateGuid() is implemented (in cgpt.c which is
1122 * *not* linked into libvboot_host.a) by calling into libuuid. We don't want to
1123 * mandate libuuid as a dependency for every utilitity that wants to link
1124 * libvboot_host.a, since they usually don't use the functionality that needs
1125 * to generate new UUIDs anyway (just other functionality implemented in the
1126 * same files).
1127 */
1128 #ifndef HAVE_MACOS
GenerateGuid(Guid * newguid)1129 __attribute__((weak)) int GenerateGuid(Guid *newguid) { return CGPT_FAILED; };
1130 #endif
1131