blob: 1ef74de38f91f42660a94c431d50e8c6f9e0c760 [file] [log] [blame]
Bill Richardsonf1372d92010-06-11 09:15:55 -07001// 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 "endian.h"
13#include "gpt.h"
14#include "cgptlib.h"
15
16
17// Just for clarity
18enum {
19 CGPT_OK = 0,
20 CGPT_FAILED,
21};
22
23
24struct legacy_partition {
25 uint8_t status;
26 uint8_t f_head;
27 uint8_t f_sect;
28 uint8_t f_cyl;
29 uint8_t type;
30 uint8_t l_head;
31 uint8_t l_sect;
32 uint8_t l_cyl;
33 uint32_t f_lba;
34 uint32_t num_sect;
35} __attribute__((packed));
36
37
38// syslinux uses this format:
39struct pmbr {
40 uint8_t bootcode[424];
41 Guid boot_guid;
42 uint32_t disk_id;
43 uint8_t magic[2]; // 0x1d, 0x9a
44 struct legacy_partition part[4];
45 uint8_t sig[2]; // 0x55, 0xaa
46} __attribute__((packed));
47
48void PMBRToStr(struct pmbr *pmbr, char *str);
49
50// Handle to the drive storing the GPT.
51struct drive {
52 int fd; /* file descriptor */
53 uint64_t size; /* total size (in bytes) */
54 GptData gpt;
55 struct pmbr pmbr;
56};
57
58
59int DriveOpen(const char *drive_path, struct drive *drive);
60int DriveClose(struct drive *drive, int update_as_needed);
61int CheckValid(const struct drive *drive);
62
63/* GUID conversion functions. Accepted format:
64 *
65 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
66 *
67 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing
68 * '\0').
69 */
70#define GUID_STRLEN 37
71int StrToGuid(const char *str, Guid *guid);
72void GuidToStr(const Guid *guid, char *str);
73int IsZero(const Guid *guid);
74
75
76int ReadPMBR(struct drive *drive);
77int WritePMBR(struct drive *drive);
78
79
80/* Convert UTF16 string to UTF8. Rewritten from gpt utility.
81 * Caller must prepare enough space for UTF8. The rough estimation is:
82 *
83 * utf8 length = bytecount(utf16) * 1.5
84 */
85void UTF16ToUTF8(const uint16_t *utf16, uint8_t *utf8);
86/* Convert UTF8 string to UTF16. Rewritten from gpt utility.
87 * Caller must prepare enough space for UTF16. The conservative estimation is:
88 *
89 * utf16 bytecount = bytecount(utf8) / 3 * 4
90 */
91void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16);
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, int index, int raw);
98
99uint32_t GetNumberOfEntries(const GptData *gpt);
100GptEntry *GetEntry(GptData *gpt, int secondary, int entry_index);
101void SetPriority(GptData *gpt, int secondary, int entry_index, int priority);
102int GetPriority(GptData *gpt, int secondary, int entry_index);
103void SetTries(GptData *gpt, int secondary, int entry_index, int tries);
104int GetTries(GptData *gpt, int secondary, int entry_index);
105void SetSuccessful(GptData *gpt, int secondary, int entry_index, int success);
106int GetSuccessful(GptData *gpt, int secondary, int entry_index);
107
108uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers);
109uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries);
110void UpdateCrc(GptData *gpt);
111int IsSynonymous(const GptHeader* a, const GptHeader* b);
112
113// For usage and error messages.
114extern const char* progname;
115extern const char* command;
116void Error(const char *format, ...);
117
118
119// Command functions.
120int cmd_show(int argc, char *argv[]);
121int cmd_repair(int argc, char *argv[]);
122int cmd_create(int argc, char *argv[]);
123int cmd_add(int argc, char *argv[]);
124int cmd_boot(int argc, char *argv[]);
Bill Richardson4a209312010-07-02 11:34:38 -0700125int cmd_find(int argc, char *argv[]);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700126
127#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
128const char *GptError(int errnum);
129
130
131#endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_