blob: b3f7ff3ba427e1a1df0e02ecc25bc6f1d0518c0d [file] [log] [blame]
Jay Srinivasan250549d2012-02-16 17:40:45 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardsonf1372d92010-06-11 09:15:55 -07002// 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
Jay Srinivasan250549d2012-02-16 17:40:45 -08008#ifndef _GNU_SOURCE
Bill Richardsonf1372d92010-06-11 09:15:55 -07009#define _GNU_SOURCE
Jay Srinivasan250549d2012-02-16 17:40:45 -080010#endif
11
Bill Richardsonf1372d92010-06-11 09:15:55 -070012#define _FILE_OFFSET_BITS 64
Bill Richardson23429d32012-04-30 11:33:13 -070013#include <fcntl.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -070014#include <features.h>
15#include <stdint.h>
Bill Richardsonc4e92af2010-10-12 07:33:15 -070016#include <stdio.h>
17#include <stdlib.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -070018#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
Bill Richardsonc4e92af2010-10-12 07:33:15 -070054void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen);
Bill Richardsonf1372d92010-06-11 09:15:55 -070055
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
Bill Richardson23429d32012-04-30 11:33:13 -070065/* mode should be O_RDONLY or O_RDWR */
66int DriveOpen(const char *drive_path, struct drive *drive, int mode);
Bill Richardsonf1372d92010-06-11 09:15:55 -070067int 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);
Bill Richardsonc4e92af2010-10-12 07:33:15 -070079void GuidToStr(const Guid *guid, char *str, unsigned int buflen);
Bill Richardson3430b322010-11-29 14:24:51 -080080int GuidEqual(const Guid *guid1, const Guid *guid2);
Bill Richardsonf1372d92010-06-11 09:15:55 -070081int IsZero(const Guid *guid);
82
Bill Richardson3430b322010-11-29 14:24:51 -080083/* Constant global type values to compare against */
Gabe Black93cf15e2011-07-07 16:00:00 -070084extern const Guid guid_chromeos_firmware;
Bill Richardson3430b322010-11-29 14:24:51 -080085extern 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;
Bill Richardsonf1372d92010-06-11 09:15:55 -070091
92int ReadPMBR(struct drive *drive);
93int WritePMBR(struct drive *drive);
94
Bill Richardsonc4e92af2010-10-12 07:33:15 -070095/* Convert possibly unterminated UTF16 string to UTF8.
96 * Caller must prepare enough space for UTF8, which could be up to
Louis Yung-Chieh Lo500b3c22010-11-22 18:19:11 +080097 * 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.
Bill Richardsonf1372d92010-06-11 09:15:55 -0700101 */
Louis Yung-Chieh Lo500b3c22010-11-22 18:19:11 +0800102int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
103 uint8_t *utf8, unsigned int maxoutput);
104
Bill Richardsonc4e92af2010-10-12 07:33:15 -0700105/* Convert null-terminated UTF8 string to UTF16.
Louis Yung-Chieh Lo500b3c22010-11-22 18:19:11 +0800106 * 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.
Bill Richardsonf1372d92010-06-11 09:15:55 -0700111 */
Louis Yung-Chieh Lo500b3c22010-11-22 18:19:11 +0800112int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700113
114/* Helper functions for supported GPT types. */
115int ResolveType(const Guid *type, char *buf);
116int SupportedType(const char *name, Guid *type);
117void PrintTypes(void);
Bill Richardsonc4e92af2010-10-12 07:33:15 -0700118void EntryDetails(GptEntry *entry, uint32_t index, int raw);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700119
120uint32_t GetNumberOfEntries(const GptData *gpt);
Bill Richardsonc4e92af2010-10-12 07:33:15 -0700121GptEntry *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);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700130
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
Jay Srinivasan5fac7572012-02-23 10:59:01 -0800141// 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);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700148
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[]);
Bill Richardson4a209312010-07-02 11:34:38 -0700155int cmd_find(int argc, char *argv[]);
Bill Richardson3430b322010-11-29 14:24:51 -0800156int cmd_prioritize(int argc, char *argv[]);
Stefan Reinauerb7b865c2012-08-23 15:06:25 -0700157int cmd_legacy(int argc, char *argv[]);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700158
159#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
160const char *GptError(int errnum);
161
Bill Richardsonc4e92af2010-10-12 07:33:15 -0700162// 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)
Bill Richardsonf1372d92010-06-11 09:15:55 -0700173
174#endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_