Bug Summary

File:3rdparty/vboot/cgpt/cgpt_common.c
Warning:line 509, column 9
Value stored to 'code_point_ready' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cgpt_common.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/home/coreboot/node-root/workspace/coreboot_scanbuild/3rdparty/vboot -resource-dir /opt/xgcc/lib/clang/17 -D CHROMEOS_ENVIRONMENT -D EC_EFS=0 -D EXTERNAL_TPM_CLEAR_REQUEST=0 -D _GNU_SOURCE -D _FILE_OFFSET_BITS=64 -D HAVE_EXECINFO_H -D HAVE_CROSID -D HAVE_NSS -I firmware/include -I firmware/lib/include -I firmware/lib/cgptlib/include -I firmware/lib/tpm_lite/include -I firmware/2lib/include -I host/include -I host/lib/include -I host/lib21/include -internal-isystem /opt/xgcc/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -source-date-epoch 1715206807 -Os -Wno-trigraphs -Wwrite-strings -Wno-format-security -Wno-address-of-packed-member -Wno-unknown-warning -std=gnu11 -fconst-strings -fdebug-compilation-dir=/home/coreboot/node-root/workspace/coreboot_scanbuild/3rdparty/vboot -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-opt-analyze-headers -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /cb-build/coreboot_scanbuild.0/sharedutils-scanbuildtmp/2024-05-09-073003-3820391-1 -x c cgpt/cgpt_common.c
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(*__errno_location ()).h>
10#include <fcntl.h>
11#include <getopt.h>
12#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
13#include <linux1/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
32static const char kErrorTag[] = "ERROR";
33static const char kWarningTag[] = "WARNING";
34
35static void LogToStderr(const char *tag, const char *format, va_list ap) {
36 fprintf(stderrstderr, "%s: ", tag);
37 vfprintf(stderrstderr, format, ap);
38}
39
40void Error(const char *format, ...) {
41 va_list ap;
42 va_start(ap, format)__builtin_va_start(ap, format);
43 LogToStderr(kErrorTag, format, ap);
44 va_end(ap)__builtin_va_end(ap);
45}
46
47void Warning(const char *format, ...) {
48 va_list ap;
49 va_start(ap, format)__builtin_va_start(ap, format);
50 LogToStderr(kWarningTag, format, ap);
51 va_end(ap)__builtin_va_end(ap);
52}
53
54int 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
62int 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
70int 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
79int 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)do { if (!(buf)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "buf", "cgpt/cgpt_common.c", 86); exit(1); } } while (0)
;
87 if (!sector_count || !sector_bytes) {
88 Error("%s() failed at line %d: sector_count=%" PRIu64"l" "u" ", sector_bytes=%" PRIu64"l" "u" "\n",
89 __FUNCTION__, __LINE__89, 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(18446744073709551615UL) / sector_count)) {
94 Error("%s() failed at line %d: sector_count=%" PRIu64"l" "u" ", sector_bytes=%" PRIu64"l" "u" "\n",
95 __FUNCTION__, __LINE__95, 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_SET0)) {
101 Error("Can't seek: %s\n", strerror(errno(*__errno_location ())));
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
115int ReadPMBR(struct drive *drive) {
116 if (-1 == lseek(drive->fd, 0, SEEK_SET0))
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
126int WritePMBR(struct drive *drive) {
127 if (-1 == lseek(drive->fd, 0, SEEK_SET0))
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
137int 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)do { if (!(buf)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "buf", "cgpt/cgpt_common.c", 144); exit(1); } } while (0)
;
145 count = sector_bytes * sector_count;
146
147 if (-1 == lseek(drive->fd, sector * sector_bytes, SEEK_SET0))
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
157static 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(128 * sizeof(GptEntry)));
169 drive->gpt.secondary_entries = malloc(GPT_ENTRIES_ALLOC_SIZE(128 * sizeof(GptEntry)));
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_EXTERNAL0x1)) {
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_SECTORS1,
182 drive->gpt.sector_bytes, GPT_HEADER_SECTORS1)) {
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_SECTORS1,
188 drive->gpt.sector_bytes, GPT_HEADER_SECTORS1)) {
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"IGNOREME",
208 GPT_HEADER_SIGNATURE_SIZE8) ? "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"IGNOREME",
226 GPT_HEADER_SIGNATURE_SIZE8) ? "invalid" : "being ignored");
227 }
228 return 0;
229}
230
231static int GptSave(struct drive *drive) {
232 int errors = 0;
233
234 if (!(drive->gpt.ignored & MASK_PRIMARY)) {
235 if (drive->gpt.modified & GPT_MODIFIED_HEADER10x01) {
236 if (CGPT_OK != Save(drive, drive->gpt.primary_header,
237 GPT_PMBR_SECTORS1,
238 drive->gpt.sector_bytes, GPT_HEADER_SECTORS1)) {
239 errors++;
240 Error("Cannot write primary header: %s\n", strerror(errno(*__errno_location ())));
241 }
242 }
243 GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header;
244 if (drive->gpt.modified & GPT_MODIFIED_ENTRIES10x04) {
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(*__errno_location ())));
252 }
253 }
254
255 // Sync primary GPT before touching secondary so one is always valid.
256 if (drive->gpt.modified & (GPT_MODIFIED_HEADER10x01 | GPT_MODIFIED_ENTRIES10x04))
257 if (fsync(drive->fd) < 0 && errno(*__errno_location ()) == EIO5) {
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_HEADER20x02) {
266 if(CGPT_OK != Save(drive, drive->gpt.secondary_header,
267 drive->gpt.gpt_drive_sectors - GPT_PMBR_SECTORS1,
268 drive->gpt.sector_bytes, GPT_HEADER_SECTORS1)) {
269 errors++;
270 Error("Cannot write secondary header: %s\n", strerror(errno(*__errno_location ())));
271 }
272 }
273 GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header;
274 if (drive->gpt.modified & GPT_MODIFIED_ENTRIES20x08) {
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(*__errno_location ())));
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 */
293static 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_IFMT0170000) != S_IFREG0100000) {
300 if (ioctl(fd, BLKGETSIZE64(((2U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8))
| (((114)) << 0) | ((((sizeof(size_t)))) << ((0 +
8)+8)))
, size) < 0) {
301 return -1;
302 }
303 if (ioctl(fd, BLKSSZGET(((0U) << (((0 +8)+8)+14)) | (((0x12)) << (0 +8))
| (((104)) << 0) | ((0) << ((0 +8)+8)))
, 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
317int 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)do { if (!(drive_path)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "drive_path", "cgpt/cgpt_common.c", 321); exit(1); } } while
(0)
;
322 require(drive)do { if (!(drive)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "drive", "cgpt/cgpt_common.c", 322); exit(1); } } while (0)
;
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_LARGEFILE0 |
330#endif
331 O_NOFOLLOW0400000);
332 if (drive->fd == -1) {
333 Error("Can't open %s: %s\n", drive_path, strerror(errno(*__errno_location ())));
334 return CGPT_FAILED;
335 }
336
337 uint64_t gpt_drive_size;
338 if (ObtainDriveSize(drive->fd, &gpt_drive_size, &sector_bytes) != 0) {
339 Error("Can't get drive size and bytes per sector for %s: %s\n",
340 drive_path, strerror(errno(*__errno_location ())));
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_EXTERNAL0x1;
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
361error_close:
362 (void) DriveClose(drive, 0);
363 return CGPT_FAILED;
364}
365
366
367int 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((void*)0);
378 free(drive->gpt.primary_entries);
379 drive->gpt.primary_entries = NULL((void*)0);
380 free(drive->gpt.secondary_header);
381 drive->gpt.secondary_header = NULL((void*)0);
382 free(drive->gpt.secondary_entries);
383 drive->gpt.secondary_entries = NULL((void*)0);
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
395
396/* GUID conversion functions. Accepted format:
397 *
398 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
399 *
400 * Returns CGPT_OK if parsing is successful; otherwise CGPT_FAILED.
401 */
402int 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)__uint32_identity (time_low);
429 guid->u.Uuid.time_mid = htole16(time_mid)__uint16_identity (time_mid);
430 guid->u.Uuid.time_high_and_version = htole16(time_high_and_version)__uint16_identity (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}
443void GuidToStr(const Guid *guid, char *str, unsigned int buflen) {
444 require(buflen >= GUID_STRLEN)do { if (!(buflen >= 37)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "buflen >= GUID_STRLEN", "cgpt/cgpt_common.c", 444); exit
(1); } } while (0)
;
445 require(snprintf(str, buflen,do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
446 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
447 le32toh(guid->u.Uuid.time_low),do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
448 le16toh(guid->u.Uuid.time_mid),do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
449 le16toh(guid->u.Uuid.time_high_and_version),do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
450 guid->u.Uuid.clock_seq_high_and_reserved,do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
451 guid->u.Uuid.clock_seq_low,do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
452 guid->u.Uuid.node[0], guid->u.Uuid.node[1],do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
453 guid->u.Uuid.node[2], guid->u.Uuid.node[3],do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
454 guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1)do { if (!(snprintf(str, buflen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
, __uint32_identity (guid->u.Uuid.time_low), __uint16_identity
(guid->u.Uuid.time_mid), __uint16_identity (guid->u.Uuid
.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved
, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid
->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid
.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) ==
37 -1)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\", le32toh(guid->u.Uuid.time_low), le16toh(guid->u.Uuid.time_mid), le16toh(guid->u.Uuid.time_high_and_version), guid->u.Uuid.clock_seq_high_and_reserved, guid->u.Uuid.clock_seq_low, guid->u.Uuid.node[0], guid->u.Uuid.node[1], guid->u.Uuid.node[2], guid->u.Uuid.node[3], guid->u.Uuid.node[4], guid->u.Uuid.node[5]) == GUID_STRLEN-1"
, "cgpt/cgpt_common.c", 454); exit(1); } } while (0)
;
455}
456
457/* Convert possibly unterminated UTF16 string to UTF8.
458 * Caller must prepare enough space for UTF8, which could be up to
459 * twice the byte length of UTF16 string plus the terminating '\0'.
460 * See the following table for encoding lengths.
461 *
462 * Code point UTF16 UTF8
463 * 0x0000-0x007F 2 bytes 1 byte
464 * 0x0080-0x07FF 2 bytes 2 bytes
465 * 0x0800-0xFFFF 2 bytes 3 bytes
466 * 0x10000-0x10FFFF 4 bytes 4 bytes
467 *
468 * This function uses a simple state meachine to convert UTF-16 char(s) to
469 * a code point. Once a code point is parsed out, the state machine throws
470 * out sequencial UTF-8 chars in one time.
471 *
472 * Return: CGPT_OK --- all character are converted successfully.
473 * CGPT_FAILED --- convert error, i.e. output buffer is too short.
474 */
475int 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; // code point is ready to output.
481 int retval = CGPT_OK;
482
483 if (!utf16 || !maxinput || !utf8 || !maxoutput)
484 return CGPT_FAILED;
485
486 maxoutput--; /* plan for termination now */
487
488 for (s16idx = s8idx = 0;
489 s16idx < maxinput && utf16[s16idx] && maxoutput;
490 s16idx++) {
491 uint16_t codeunit = le16toh(utf16[s16idx])__uint16_identity (utf16[s16idx]);
492
493 if (code_point_ready) {
494 if (codeunit >= 0xD800 && codeunit <= 0xDBFF) {
495 /* high surrogate, need the low surrogate. */
496 code_point_ready = 0;
497 code_point = (codeunit & 0x03FF) + 0x0040;
498 } else {
499 /* BMP char, output it. */
500 code_point = codeunit;
501 }
502 } else {
503 /* expect the low surrogate */
504 if (codeunit >= 0xDC00 && codeunit <= 0xDFFF) {
505 code_point = (code_point << 10) | (codeunit & 0x03FF);
506 code_point_ready = 1;
507 } else {
508 /* the second code unit is NOT the low surrogate. Unexpected. */
509 code_point_ready = 0;
Value stored to 'code_point_ready' is never read
510 retval = CGPT_FAILED;
511 break;
512 }
513 }
514
515 /* If UTF code point is ready, output it. */
516 if (code_point_ready) {
517 require(code_point <= 0x10FFFF)do { if (!(code_point <= 0x10FFFF)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "code_point <= 0x10FFFF", "cgpt/cgpt_common.c", 517); exit
(1); } } while (0)
;
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 /* buffer underrun */
538 retval = CGPT_FAILED;
539 break;
540 }
541 }
542 }
543 utf8[s8idx++] = 0;
544 return retval;
545}
546
547/* Convert UTF8 string to UTF16. The UTF8 string must be null-terminated.
548 * Caller must prepare enough space for UTF16, including a terminating 0x0000.
549 * See the following table for encoding lengths. In any case, the caller
550 * just needs to prepare the byte length of UTF8 plus the terminating 0x0000.
551 *
552 * Code point UTF16 UTF8
553 * 0x0000-0x007F 2 bytes 1 byte
554 * 0x0080-0x07FF 2 bytes 2 bytes
555 * 0x0800-0xFFFF 2 bytes 3 bytes
556 * 0x10000-0x10FFFF 4 bytes 4 bytes
557 *
558 * This function converts UTF8 chars to a code point first. Then, convrts it
559 * to UTF16 code unit(s).
560 *
561 * Return: CGPT_OK --- all character are converted successfully.
562 * CGPT_FAILED --- convert error, i.e. output buffer is too short.
563 */
564int 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--; /* plan for termination */
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 /* Trailing bytes of multi-byte character */
585 if ((code_unit & 0xC0) == 0x80) {
586 code_point = (code_point << 6) | (code_unit & 0x3F);
587 ++decoded_units;
588 } else {
589 /* Unexpected code unit. */
590 retval = CGPT_FAILED;
591 break;
592 }
593 } else {
594 /* parsing a new code point. */
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 /* 0x80-0xBF must NOT be the heading byte unit of a new code point. */
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 /* illegal code unit: 0xC0-0xC1, 0xF5-0xFF */
614 retval = CGPT_FAILED;
615 break;
616 }
617 }
618
619 /* If no more unit is needed, output the UTF16 unit(s). */
620 if ((retval == CGPT_OK) &&
621 (expected_units == decoded_units)) {
622 /* Check if the encoding is the shortest possible UTF-8 sequence. */
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; /* leave immediately */
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 /* Three possibilities fall into here. Both are failure cases.
647 * a. surrogate pair (non-BMP characters; 0xD800~0xDFFF)
648 * b. invalid code point > 0x10FFFF
649 * c. buffer underrun
650 */
651 retval = CGPT_FAILED;
652 break;
653 }
654 }
655 }
656
657 /* A null-terminator shows up before the UTF8 sequence ends. */
658 if (expected_units != decoded_units) {
659 retval = CGPT_FAILED;
660 }
661
662 utf16[s16idx++] = 0;
663 return retval;
664}
665
666/* global types to compare against */
667const Guid guid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE{{{0xcab6e88e,0xabf3,0x4102,0xa0,0x7a,{0xd4,0xbb,0x9b,0xe3,0xc1
,0xd3}}}}
;
668const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL{{{0xfe3a2a5d,0x4f32,0x41a7,0xb7,0x25,{0xac,0xcc,0x32,0x85,0xa3
,0x09}}}}
;
669const Guid guid_chromeos_rootfs = GPT_ENT_TYPE_CHROMEOS_ROOTFS{{{0x3cb8e202,0x3b7e,0x47dd,0x8a,0x3c,{0x7f,0xf2,0xa1,0x3c,0xfc
,0xec}}}}
;
670const Guid guid_basic_data = GPT_ENT_TYPE_BASIC_DATA{{{0xebd0a0a2,0xb9e5,0x4433,0x87,0xc0,{0x68,0xb6,0xb7,0x26,0x99
,0xc7}}}}
;
671const Guid guid_linux_data = GPT_ENT_TYPE_LINUX_FS{{{0x0fc63daf,0x8483,0x4772,0x8e,0x79,{0x3d,0x69,0xd8,0x47,0x7d
,0xe4}}}}
;
672const Guid guid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED{{{0x2e0a753d,0x9e48,0x43b0,0x83,0x37,{0xb1,0x51,0x92,0xcb,0x1b
,0x5e}}}}
;
673const Guid guid_efi = GPT_ENT_TYPE_EFI{{{0xc12a7328,0xf81f,0x11d2,0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9
,0x3b}}}}
;
674const Guid guid_unused = GPT_ENT_TYPE_UNUSED{{{0x00000000,0x0000,0x0000,0x00,0x00,{0x00,0x00,0x00,0x00,0x00
,0x00}}}}
;
675const Guid guid_chromeos_minios = GPT_ENT_TYPE_CHROMEOS_MINIOS{{{0x09845860,0x705f,0x4bb5,0xb1,0x6c,{0x8a,0x8a,0x09,0x9c,0xaf
,0x52}}}}
;
676const Guid guid_chromeos_hibernate = GPT_ENT_TYPE_CHROMEOS_HIBERNATE{{{0x3f0f8318,0xf146,0x4e6b,0x82,0x22,{0xc2,0x8c,0x8f,0x02,0xe0
,0xd5}}}}
;
677
678static 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/* Resolves human-readable GPT type.
696 * Returns CGPT_OK if found.
697 * Returns CGPT_FAILED if no known type found. */
698int ResolveType(const Guid *type, char *buf) {
699 int i;
700 for (i = 0; i < ARRAY_COUNT(supported_types)(sizeof(supported_types)/sizeof((supported_types)[0])); ++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
709int SupportedType(const char *name, Guid *type) {
710 int i;
711 for (i = 0; i < ARRAY_COUNT(supported_types)(sizeof(supported_types)/sizeof((supported_types)[0])); ++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
720void 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)(sizeof(supported_types)/sizeof((supported_types)[0])); ++i) {
724 printf(" %-10s %s\n", supported_types[i].name,
725 supported_types[i].description);
726 }
727 printf("\n");
728}
729
730static 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
739uint32_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
747GptEntry *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)do { if (!(stride)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "stride", "cgpt/cgpt_common.c", 751); exit(1); } } while (0
)
;
752 require(entry_index < header->number_of_entries)do { if (!(entry_index < header->number_of_entries)) { fprintf
(stderr, "condition (%s) failed at %s:%d\n", "entry_index < header->number_of_entries"
, "cgpt/cgpt_common.c", 752); exit(1); } } while (0)
;
753
754 if (secondary == PRIMARY) {
755 entries = gpt->primary_entries;
756 } else if (secondary == SECONDARY) {
757 entries = gpt->secondary_entries;
758 } else { /* ANY_VALID */
759 require(secondary == ANY_VALID)do { if (!(secondary == ANY_VALID)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "secondary == ANY_VALID", "cgpt/cgpt_common.c", 759); exit(
1); } } while (0)
;
760 if (gpt->valid_entries & MASK_PRIMARY) {
761 entries = gpt->primary_entries;
762 } else {
763 require(gpt->valid_entries & MASK_SECONDARY)do { if (!(gpt->valid_entries & MASK_SECONDARY)) { fprintf
(stderr, "condition (%s) failed at %s:%d\n", "gpt->valid_entries & MASK_SECONDARY"
, "cgpt/cgpt_common.c", 763); exit(1); } } while (0)
;
764 entries = gpt->secondary_entries;
765 }
766 }
767
768 return (GptEntry*)(&entries[stride * entry_index]);
769}
770
771void SetRequired(struct drive *drive, int secondary, uint32_t entry_index,
772 int required) {
773 require(required >= 0 && required <= CGPT_ATTRIBUTE_MAX_REQUIRED)do { if (!(required >= 0 && required <= (1ULL))
) { fprintf(stderr, "condition (%s) failed at %s:%d\n", "required >= 0 && required <= CGPT_ATTRIBUTE_MAX_REQUIRED"
, "cgpt/cgpt_common.c", 773); exit(1); } } while (0)
;
774 GptEntry *entry;
775 entry = GetEntry(&drive->gpt, secondary, entry_index);
776 SetEntryRequired(entry, required);
777}
778
779int 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
785void 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)do { if (!(legacy_boot >= 0 && legacy_boot <= (
1ULL))) { fprintf(stderr, "condition (%s) failed at %s:%d\n",
"legacy_boot >= 0 && legacy_boot <= CGPT_ATTRIBUTE_MAX_LEGACY_BOOT"
, "cgpt/cgpt_common.c", 787); exit(1); } } while (0)
;
788 GptEntry *entry;
789 entry = GetEntry(&drive->gpt, secondary, entry_index);
790 SetEntryLegacyBoot(entry, legacy_boot);
791}
792
793int 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
799void SetPriority(struct drive *drive, int secondary, uint32_t entry_index,
800 int priority) {
801 require(priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY)do { if (!(priority >= 0 && priority <= (15ULL)
)) { fprintf(stderr, "condition (%s) failed at %s:%d\n", "priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY"
, "cgpt/cgpt_common.c", 801); exit(1); } } while (0)
;
802 GptEntry *entry;
803 entry = GetEntry(&drive->gpt, secondary, entry_index);
804 SetEntryPriority(entry, priority);
805}
806
807int 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
813void SetTries(struct drive *drive, int secondary, uint32_t entry_index,
814 int tries) {
815 require(tries >= 0 && tries <= CGPT_ATTRIBUTE_MAX_TRIES)do { if (!(tries >= 0 && tries <= (15ULL))) { fprintf
(stderr, "condition (%s) failed at %s:%d\n", "tries >= 0 && tries <= CGPT_ATTRIBUTE_MAX_TRIES"
, "cgpt/cgpt_common.c", 815); exit(1); } } while (0)
;
816 GptEntry *entry;
817 entry = GetEntry(&drive->gpt, secondary, entry_index);
818 SetEntryTries(entry, tries);
819}
820
821int 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
827void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index,
828 int success) {
829 require(success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESSFUL)do { if (!(success >= 0 && success <= (1ULL))) {
fprintf(stderr, "condition (%s) failed at %s:%d\n", "success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESSFUL"
, "cgpt/cgpt_common.c", 829); exit(1); } } while (0)
;
830 GptEntry *entry;
831 entry = GetEntry(&drive->gpt, secondary, entry_index);
832 SetEntrySuccessful(entry, success);
833}
834
835int 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
841void SetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index,
842 int error_counter) {
843 require(error_counter >= 0 &&do { if (!(error_counter >= 0 && error_counter <=
(1ULL))) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "error_counter >= 0 && error_counter <= CGPT_ATTRIBUTE_MAX_ERROR_COUNTER"
, "cgpt/cgpt_common.c", 844); exit(1); } } while (0)
844 error_counter <= CGPT_ATTRIBUTE_MAX_ERROR_COUNTER)do { if (!(error_counter >= 0 && error_counter <=
(1ULL))) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "error_counter >= 0 && error_counter <= CGPT_ATTRIBUTE_MAX_ERROR_COUNTER"
, "cgpt/cgpt_common.c", 844); exit(1); } } while (0)
;
845 GptEntry *entry;
846 entry = GetEntry(&drive->gpt, secondary, entry_index);
847 SetEntryErrorCounter(entry, error_counter);
848}
849
850int 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
856void 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
863void UpdateAllEntries(struct drive *drive) {
864 RepairEntries(&drive->gpt, MASK_PRIMARY);
865 RepairHeader(&drive->gpt, MASK_PRIMARY);
866
867 drive->gpt.modified |= (GPT_MODIFIED_HEADER10x01 | GPT_MODIFIED_ENTRIES10x04 |
868 GPT_MODIFIED_HEADER20x02 | GPT_MODIFIED_ENTRIES20x08);
869 UpdateCrc(&drive->gpt);
870}
871
872int 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
878int 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" #A
886const char *GptError(int errnum) {
887 const char *error_string[] = {
888 TOSTRING(GPT_SUCCESS)"GPT_SUCCESS",
889 TOSTRING(GPT_ERROR_NO_VALID_KERNEL)"GPT_ERROR_NO_VALID_KERNEL",
890 TOSTRING(GPT_ERROR_INVALID_HEADERS)"GPT_ERROR_INVALID_HEADERS",
891 TOSTRING(GPT_ERROR_INVALID_ENTRIES)"GPT_ERROR_INVALID_ENTRIES",
892 TOSTRING(GPT_ERROR_INVALID_SECTOR_SIZE)"GPT_ERROR_INVALID_SECTOR_SIZE",
893 TOSTRING(GPT_ERROR_INVALID_SECTOR_NUMBER)"GPT_ERROR_INVALID_SECTOR_NUMBER",
894 TOSTRING(GPT_ERROR_INVALID_UPDATE_TYPE)"GPT_ERROR_INVALID_UPDATE_TYPE"
895 };
896 if (errnum < 0 || errnum >= ARRAY_COUNT(error_string)(sizeof(error_string)/sizeof((error_string)[0])))
897 return "<illegal value>";
898 return error_string[errnum];
899}
900
901/* Update CRC value if necessary. */
902void 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_ENTRIES10x04 &&
909 memcmp(primary_header, GPT_HEADER_SIGNATURE2"CHROMEOS",
910 GPT_HEADER_SIGNATURE_SIZE8)) {
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_ENTRIES20x08) {
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_HEADER10x01) {
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_HEADER20x02) {
928 secondary_header->header_crc32 = 0;
929 secondary_header->header_crc32 = Crc32(
930 (const uint8_t *)secondary_header, sizeof(GptHeader));
931 }
932}
933/* Two headers are NOT bitwise identical. For example, my_lba pointers to header
934 * itself so that my_lba in primary and secondary is definitely different.
935 * Only the following fields should be identical.
936 *
937 * first_usable_lba
938 * last_usable_lba
939 * number_of_entries
940 * size_of_entry
941 * disk_uuid
942 *
943 * If any of above field are not matched, overwrite secondary with primary since
944 * we always trust primary.
945 * If any one of header is invalid, copy from another. */
946int 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/* Primary entries and secondary entries should be bitwise identical.
957 * If two entries tables are valid, compare them. If not the same,
958 * overwrites secondary with primary (primary always has higher priority),
959 * and marks secondary as modified.
960 * If only one is valid, overwrites invalid one.
961 * If all are invalid, does nothing.
962 * This function returns bit masks for GptData.modified field.
963 * Note that CRC is NOT re-computed in this function.
964 */
965uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries) {
966 /* If we have an alternate GPT header signature, don't overwrite
967 * the secondary GPT with the primary one as that might wipe the
968 * partition table. Also don't overwrite the primary one with the
969 * secondary one as that will stop Windows from booting. */
970 GptHeader* h = (GptHeader*)(gpt->primary_header);
971 if (!memcmp(h->signature, GPT_HEADER_SIGNATURE2"CHROMEOS", GPT_HEADER_SIGNATURE_SIZE8))
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 /* We cannot trust any header, don't update entries. */
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_ENTRIES20x08;
988 }
989 } else if (valid_entries == MASK_PRIMARY) {
990 memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size);
991 return GPT_MODIFIED_ENTRIES20x08;
992 } else if (valid_entries == MASK_SECONDARY) {
993 memcpy(gpt->primary_entries, gpt->secondary_entries, entries_size);
994 return GPT_MODIFIED_ENTRIES10x04;
995 }
996
997 return 0;
998}
999
1000/* The above five fields are shared between primary and secondary headers.
1001 * We can recover one header from another through copying those fields. */
1002static 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/* This function repairs primary and secondary headers if possible.
1011 * If both headers are valid (CRC32 is correct) but
1012 * a) indicate inconsistent usable LBA ranges,
1013 * b) inconsistent partition entry size and number,
1014 * c) inconsistent disk_uuid,
1015 * we will use the primary header to overwrite secondary header.
1016 * If primary is invalid (CRC32 is wrong), then we repair it from secondary.
1017 * If secondary is invalid (CRC32 is wrong), then we repair it from primary.
1018 * This function returns the bitmasks for modified header.
1019 * Note that CRC value is NOT re-computed in this function. UpdateCrc() will
1020 * do it later.
1021 */
1022uint8_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_HEADER20x02;
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; /* the last sector */
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_HEADER20x02;
1040 } else if (valid_headers == MASK_SECONDARY) {
1041 memcpy(primary_header, secondary_header, sizeof(GptHeader));
1042 primary_header->my_lba = GPT_PMBR_SECTORS1; /* the second sector on drive */
1043 primary_header->alternate_lba = secondary_header->my_lba;
1044 /* TODO (namnguyen): Preserve (header, entries) padding space. */
1045 primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTORS1;
1046 return GPT_MODIFIED_HEADER10x01;
1047 }
1048
1049 return 0;
1050}
1051
1052int CgptGetNumNonEmptyPartitions(CgptShowParams *params) {
1053 struct drive drive;
1054 int gpt_retval;
1055 int retval;
1056
1057 if (params == NULL((void*)0))
1058 return CGPT_FAILED;
1059
1060 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY00,
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
1084done:
1085 DriveClose(&drive, 0);
1086 return retval;
1087}
1088
1089int GuidEqual(const Guid *guid1, const Guid *guid2) {
1090 return (0 == memcmp(guid1, guid2, sizeof(Guid)));
1091}
1092
1093int GuidIsZero(const Guid *gp) {
1094 return GuidEqual(gp, &guid_unused);
1095}
1096
1097void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen) {
1098 char buf[GUID_STRLEN37];
1099 if (GuidIsZero(&pmbr->boot_guid)) {
1100 require(snprintf(str, buflen, "PMBR") < buflen)do { if (!(snprintf(str, buflen, "PMBR") < buflen)) { fprintf
(stderr, "condition (%s) failed at %s:%d\n", "snprintf(str, buflen, \"PMBR\") < buflen"
, "cgpt/cgpt_common.c", 1100); exit(1); } } while (0)
;
1101 } else {
1102 GuidToStr(&pmbr->boot_guid, buf, sizeof(buf));
1103 require(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf) < buflen)do { if (!(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf)
< buflen)) { fprintf(stderr, "condition (%s) failed at %s:%d\n"
, "snprintf(str, buflen, \"PMBR (Boot GUID: %s)\", buf) < buflen"
, "cgpt/cgpt_common.c", 1103); exit(1); } } while (0)
;
1104 }
1105}
1106
1107/*
1108 * This is here because some CGPT functionality is provided in libvboot_host.a
1109 * for other host utilities. GenerateGuid() is implemented (in cgpt.c which is
1110 * *not* linked into libvboot_host.a) by calling into libuuid. We don't want to
1111 * mandate libuuid as a dependency for every utilitity that wants to link
1112 * libvboot_host.a, since they usually don't use the functionality that needs
1113 * to generate new UUIDs anyway (just other functionality implemented in the
1114 * same files).
1115 */
1116#ifndef HAVE_MACOS
1117__attribute__((weak)) int GenerateGuid(Guid *newguid) { return CGPT_FAILED; };
1118#endif