cgpt.h revision e417185ff654ead6b8c1c6eafe5fc67a89a4210d
1// Copyright (c) 2010 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#define _GNU_SOURCE
9#define _FILE_OFFSET_BITS 64
10#include <features.h>
11#include <stdint.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include "endian.h"
15#include "gpt.h"
16#include "cgptlib.h"
17
18
19// Just for clarity
20enum {
21  CGPT_OK = 0,
22  CGPT_FAILED,
23};
24
25
26struct legacy_partition {
27  uint8_t  status;
28  uint8_t  f_head;
29  uint8_t  f_sect;
30  uint8_t  f_cyl;
31  uint8_t  type;
32  uint8_t  l_head;
33  uint8_t  l_sect;
34  uint8_t  l_cyl;
35  uint32_t f_lba;
36  uint32_t num_sect;
37} __attribute__((packed));
38
39
40// syslinux uses this format:
41struct pmbr {
42  uint8_t                 bootcode[424];
43  Guid                    boot_guid;
44  uint32_t                disk_id;
45  uint8_t                 magic[2];     // 0x1d, 0x9a
46  struct legacy_partition part[4];
47  uint8_t                 sig[2];       // 0x55, 0xaa
48} __attribute__((packed));
49
50void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen);
51
52// Handle to the drive storing the GPT.
53struct drive {
54  int fd;           /* file descriptor */
55  uint64_t size;    /* total size (in bytes) */
56  GptData gpt;
57  struct pmbr pmbr;
58};
59
60
61int DriveOpen(const char *drive_path, struct drive *drive);
62int DriveClose(struct drive *drive, int update_as_needed);
63int CheckValid(const struct drive *drive);
64
65/* GUID conversion functions. Accepted format:
66 *
67 *   "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
68 *
69 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing
70 * '\0').
71 */
72#define GUID_STRLEN 37
73int StrToGuid(const char *str, Guid *guid);
74void GuidToStr(const Guid *guid, char *str, unsigned int buflen);
75int IsZero(const Guid *guid);
76
77
78int ReadPMBR(struct drive *drive);
79int WritePMBR(struct drive *drive);
80
81
82/* Convert possibly unterminated UTF16 string to UTF8.
83 * Caller must prepare enough space for UTF8, which could be up to
84 * twice the number of UTF16 chars plus the terminating '\0'.
85 */
86void UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
87                 uint8_t *utf8, unsigned int maxoutput);
88/* Convert null-terminated UTF8 string to UTF16.
89 * Caller must prepare enough space for UTF16, including a terminating 0x0000
90 */
91void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput);
92
93/* Helper functions for supported GPT types. */
94int ResolveType(const Guid *type, char *buf);
95int SupportedType(const char *name, Guid *type);
96void PrintTypes(void);
97void EntryDetails(GptEntry *entry, uint32_t index, int raw);
98
99uint32_t GetNumberOfEntries(const GptData *gpt);
100GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index);
101void SetPriority(GptData *gpt, int secondary, uint32_t entry_index,
102                 int priority);
103int GetPriority(GptData *gpt, int secondary, uint32_t entry_index);
104void SetTries(GptData *gpt, int secondary, uint32_t entry_index, int tries);
105int GetTries(GptData *gpt, int secondary, uint32_t entry_index);
106void SetSuccessful(GptData *gpt, int secondary, uint32_t entry_index,
107                   int success);
108int GetSuccessful(GptData *gpt, int secondary, uint32_t entry_index);
109
110uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers);
111uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries);
112void UpdateCrc(GptData *gpt);
113int IsSynonymous(const GptHeader* a, const GptHeader* b);
114
115// For usage and error messages.
116extern const char* progname;
117extern const char* command;
118void Error(const char *format, ...);
119
120
121// Command functions.
122int cmd_show(int argc, char *argv[]);
123int cmd_repair(int argc, char *argv[]);
124int cmd_create(int argc, char *argv[]);
125int cmd_add(int argc, char *argv[]);
126int cmd_boot(int argc, char *argv[]);
127int cmd_find(int argc, char *argv[]);
128
129#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
130const char *GptError(int errnum);
131
132// Size in chars of the GPT Entry's PartitionName field
133#define GPT_PARTNAME_LEN 72
134
135/* The standard "assert" macro goes away when NDEBUG is defined. This doesn't.
136 */
137#define require(A) do { \
138  if (!(A)) { \
139    fprintf(stderr, "condition (%s) failed at %s:%d\n", \
140            #A, __FILE__, __LINE__); \
141    exit(1); } \
142  } while (0)
143
144#endif  // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
145