cgpt.h revision 8577b5360ca4c9514d9091ed9aded2bb3193f1f0
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#include <fcntl.h>
9#include <features.h>
10#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include "cgpt_endian.h"
14#include "cgptlib.h"
15#include "gpt.h"
16
17struct legacy_partition {
18  uint8_t  status;
19  uint8_t  f_head;
20  uint8_t  f_sect;
21  uint8_t  f_cyl;
22  uint8_t  type;
23  uint8_t  l_head;
24  uint8_t  l_sect;
25  uint8_t  l_cyl;
26  uint32_t f_lba;
27  uint32_t num_sect;
28} __attribute__((packed));
29
30// syslinux uses this format:
31struct pmbr {
32  uint8_t                 bootcode[424];
33  Guid                    boot_guid;
34  uint32_t                disk_id;
35  uint8_t                 magic[2];     // 0x1d, 0x9a
36  struct legacy_partition part[4];
37  uint8_t                 sig[2];       // 0x55, 0xaa
38} __attribute__((packed));
39
40void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen);
41
42// Handle to the drive storing the GPT.
43struct drive {
44  uint64_t size;    /* total size (in bytes) */
45  GptData gpt;
46  struct pmbr pmbr;
47  int fd;       /* file descriptor */
48};
49
50// Opens a block device or file, loads raw GPT data from it.
51// 'mode' should be O_RDONLY or O_RDWR.
52// If 'drive_size' is 0, both the partitions and GPT structs reside on the same
53// 'drive_path'.
54// Otherwise, 'drive_size' is taken as the size of the device that all
55// partitions will reside on, and 'drive_path' is where we store GPT structs.
56//
57// Returns CGPT_FAILED if any error happens.
58// Returns CGPT_OK if success and information are stored in 'drive'. */
59int DriveOpen(const char *drive_path, struct drive *drive, int mode,
60              uint64_t drive_size);
61int DriveClose(struct drive *drive, int update_as_needed);
62int CheckValid(const struct drive *drive);
63
64/* Loads sectors from 'drive'.
65 * *buf is pointed to an allocated memory when returned, and should be
66 * freed.
67 *
68 *   drive -- open drive.
69 *   buf -- pointer to buffer pointer
70 *   sector -- offset of starting sector (in sectors)
71 *   sector_bytes -- bytes per sector
72 *   sector_count -- number of sectors to load
73 *
74 * Returns CGPT_OK for successful. Aborts if any error occurs.
75 */
76int Load(struct drive *drive, uint8_t **buf,
77                const uint64_t sector,
78                const uint64_t sector_bytes,
79                const uint64_t sector_count);
80
81/* Saves sectors to 'drive'.
82 *
83 *   drive -- open drive
84 *   buf -- pointer to buffer
85 *   sector -- starting sector offset
86 *   sector_bytes -- bytes per sector
87 *   sector_count -- number of sector to save
88 *
89 * Returns CGPT_OK for successful, CGPT_FAILED for failed.
90 */
91int Save(struct drive *drive, const uint8_t *buf,
92                const uint64_t sector,
93                const uint64_t sector_bytes,
94                const uint64_t sector_count);
95
96
97/* GUID conversion functions. Accepted format:
98 *
99 *   "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
100 *
101 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing
102 * '\0').
103 */
104#define GUID_STRLEN 37
105int StrToGuid(const char *str, Guid *guid);
106void GuidToStr(const Guid *guid, char *str, unsigned int buflen);
107int GuidEqual(const Guid *guid1, const Guid *guid2);
108int IsZero(const Guid *guid);
109
110/* Constant global type values to compare against */
111extern const Guid guid_chromeos_firmware;
112extern const Guid guid_chromeos_kernel;
113extern const Guid guid_chromeos_rootfs;
114extern const Guid guid_linux_data;
115extern const Guid guid_chromeos_reserved;
116extern const Guid guid_efi;
117extern const Guid guid_unused;
118
119int ReadPMBR(struct drive *drive);
120int WritePMBR(struct drive *drive);
121
122/* Convert possibly unterminated UTF16 string to UTF8.
123 * Caller must prepare enough space for UTF8, which could be up to
124 * twice the byte length of UTF16 string plus the terminating '\0'.
125 *
126 * Return: CGPT_OK --- all character are converted successfully.
127 *         CGPT_FAILED --- convert error, i.e. output buffer is too short.
128 */
129int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
130                uint8_t *utf8, unsigned int maxoutput);
131
132/* Convert null-terminated UTF8 string to UTF16.
133 * Caller must prepare enough space for UTF16, which is the byte length of UTF8
134 * plus the terminating 0x0000.
135 *
136 * Return: CGPT_OK --- all character are converted successfully.
137 *         CGPT_FAILED --- convert error, i.e. output buffer is too short.
138 */
139int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput);
140
141/* Helper functions for supported GPT types. */
142int ResolveType(const Guid *type, char *buf);
143int SupportedType(const char *name, Guid *type);
144void PrintTypes(void);
145void EntryDetails(GptEntry *entry, uint32_t index, int raw);
146
147uint32_t GetNumberOfEntries(const struct drive *drive);
148GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index);
149
150void SetPriority(struct drive *drive, int secondary, uint32_t entry_index,
151                 int priority);
152int GetPriority(struct drive *drive, int secondary, uint32_t entry_index);
153void SetTries(struct drive *drive, int secondary, uint32_t entry_index,
154              int tries);
155int GetTries(struct drive *drive, int secondary, uint32_t entry_index);
156void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index,
157                   int success);
158int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index);
159
160void SetRaw(struct drive *drive, int secondary, uint32_t entry_index,
161           uint32_t raw);
162
163void UpdateAllEntries(struct drive *drive);
164
165uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers);
166uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries);
167void UpdateCrc(GptData *gpt);
168int IsSynonymous(const GptHeader* a, const GptHeader* b);
169
170int IsUnused(struct drive *drive, int secondary, uint32_t index);
171int IsKernel(struct drive *drive, int secondary, uint32_t index);
172
173// Optional. Applications that need this must provide an implementation.
174//
175// Explanation:
176//   Some external utilities need to manipulate the GPT, but don't create new
177//   partitions from scratch. The cgpt executable uses libuuid to provide this
178//   functionality, but we don't want to have to build or install a separate
179//   instance of that library just for the 32-bit static post-install tool,
180//   which doesn't need this function.
181int GenerateGuid(Guid *newguid);
182
183// For usage and error messages.
184void Error(const char *format, ...);
185void Warning(const char *format, ...);
186
187// Command functions.
188int cmd_show(int argc, char *argv[]);
189int cmd_repair(int argc, char *argv[]);
190int cmd_create(int argc, char *argv[]);
191int cmd_add(int argc, char *argv[]);
192int cmd_boot(int argc, char *argv[]);
193int cmd_find(int argc, char *argv[]);
194int cmd_prioritize(int argc, char *argv[]);
195int cmd_legacy(int argc, char *argv[]);
196
197#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
198const char *GptError(int errnum);
199
200// Size in chars of the GPT Entry's PartitionName field
201#define GPT_PARTNAME_LEN 72
202
203/* The standard "assert" macro goes away when NDEBUG is defined. This doesn't.
204 */
205#define require(A) do { \
206  if (!(A)) { \
207    fprintf(stderr, "condition (%s) failed at %s:%d\n", \
208            #A, __FILE__, __LINE__); \
209    exit(1); } \
210  } while (0)
211
212#endif  // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
213