cgpt.h revision 3f806a2abf07d7b801852a4a6f3a9080a4b5c427
1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
6#define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
7
8#ifndef _GNU_SOURCE
9#define _GNU_SOURCE
10#endif
11
12#define _FILE_OFFSET_BITS 64
13#include <fcntl.h>
14#include <features.h>
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include "endian.h"
19#include "gpt.h"
20#include "cgptlib.h"
21
22
23// Just for clarity
24enum {
25  CGPT_OK = 0,
26  CGPT_FAILED,
27};
28
29
30struct legacy_partition {
31  uint8_t  status;
32  uint8_t  f_head;
33  uint8_t  f_sect;
34  uint8_t  f_cyl;
35  uint8_t  type;
36  uint8_t  l_head;
37  uint8_t  l_sect;
38  uint8_t  l_cyl;
39  uint32_t f_lba;
40  uint32_t num_sect;
41} __attribute__((packed));
42
43
44// syslinux uses this format:
45struct pmbr {
46  uint8_t                 bootcode[424];
47  Guid                    boot_guid;
48  uint32_t                disk_id;
49  uint8_t                 magic[2];     // 0x1d, 0x9a
50  struct legacy_partition part[4];
51  uint8_t                 sig[2];       // 0x55, 0xaa
52} __attribute__((packed));
53
54void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen);
55
56// Handle to the drive storing the GPT.
57struct drive {
58  int fd;           /* file descriptor */
59  uint64_t size;    /* total size (in bytes) */
60  GptData gpt;
61  struct pmbr pmbr;
62};
63
64
65/* mode should be O_RDONLY or O_RDWR */
66int DriveOpen(const char *drive_path, struct drive *drive, int mode);
67int DriveClose(struct drive *drive, int update_as_needed);
68int CheckValid(const struct drive *drive);
69
70/* GUID conversion functions. Accepted format:
71 *
72 *   "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
73 *
74 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing
75 * '\0').
76 */
77#define GUID_STRLEN 37
78int StrToGuid(const char *str, Guid *guid);
79void GuidToStr(const Guid *guid, char *str, unsigned int buflen);
80int GuidEqual(const Guid *guid1, const Guid *guid2);
81int GuidIsZero(const Guid *guid);
82
83/* Constant global type values to compare against */
84extern const Guid guid_chromeos_firmware;
85extern const Guid guid_chromeos_kernel;
86extern const Guid guid_chromeos_rootfs;
87extern const Guid guid_linux_data;
88extern const Guid guid_chromeos_reserved;
89extern const Guid guid_efi;
90extern const Guid guid_unused;
91
92int ReadPMBR(struct drive *drive);
93int WritePMBR(struct drive *drive);
94
95/* Convert possibly unterminated UTF16 string to UTF8.
96 * Caller must prepare enough space for UTF8, which could be up to
97 * twice the byte length of UTF16 string plus the terminating '\0'.
98 *
99 * Return: CGPT_OK --- all character are converted successfully.
100 *         CGPT_FAILED --- convert error, i.e. output buffer is too short.
101 */
102int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
103                uint8_t *utf8, unsigned int maxoutput);
104
105/* Convert null-terminated UTF8 string to UTF16.
106 * Caller must prepare enough space for UTF16, which is the byte length of UTF8
107 * plus the terminating 0x0000.
108 *
109 * Return: CGPT_OK --- all character are converted successfully.
110 *         CGPT_FAILED --- convert error, i.e. output buffer is too short.
111 */
112int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput);
113
114/* Helper functions for supported GPT types. */
115int ResolveType(const Guid *type, char *buf);
116int SupportedType(const char *name, Guid *type);
117void PrintTypes(void);
118void EntryDetails(GptEntry *entry, uint32_t index, int raw);
119
120uint32_t GetNumberOfEntries(const GptData *gpt);
121GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index);
122void SetPriority(GptData *gpt, int secondary, uint32_t entry_index,
123                 int priority);
124int GetPriority(GptData *gpt, int secondary, uint32_t entry_index);
125void SetTries(GptData *gpt, int secondary, uint32_t entry_index, int tries);
126int GetTries(GptData *gpt, int secondary, uint32_t entry_index);
127void SetSuccessful(GptData *gpt, int secondary, uint32_t entry_index,
128                   int success);
129int GetSuccessful(GptData *gpt, int secondary, uint32_t entry_index);
130
131uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers);
132uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries);
133void UpdateCrc(GptData *gpt);
134int IsSynonymous(const GptHeader* a, const GptHeader* b);
135
136// For usage and error messages.
137extern const char* progname;
138extern const char* command;
139void Error(const char *format, ...);
140
141// The code paths that require uuid_generate are not used currently in
142// libcgpt-cc.a so using this method would create an unnecessary dependency
143// on libuuid which then requires us to build it for 32-bit for the static
144// post-installer. So, we just expose this function pointer which should be
145// set to uuid_generate in case of the cgpt binary and can be null or some
146// no-op method in case of ilbcgpt-cc.a.
147extern void (*uuid_generator)(uint8_t* buffer);
148
149// Command functions.
150int cmd_show(int argc, char *argv[]);
151int cmd_repair(int argc, char *argv[]);
152int cmd_create(int argc, char *argv[]);
153int cmd_add(int argc, char *argv[]);
154int cmd_boot(int argc, char *argv[]);
155int cmd_find(int argc, char *argv[]);
156int cmd_prioritize(int argc, char *argv[]);
157int cmd_legacy(int argc, char *argv[]);
158
159#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
160const char *GptError(int errnum);
161
162// Size in chars of the GPT Entry's PartitionName field
163#define GPT_PARTNAME_LEN 72
164
165/* The standard "assert" macro goes away when NDEBUG is defined. This doesn't.
166 */
167#define require(A) do { \
168  if (!(A)) { \
169    fprintf(stderr, "condition (%s) failed at %s:%d\n", \
170            #A, __FILE__, __LINE__); \
171    exit(1); } \
172  } while (0)
173
174#endif  // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
175