1/*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sbin/gpt/gpt.h,v 1.11.2.1.6.1 2010/02/10 00:26:20 kensmith Exp $
27 */
28
29#ifndef _GPT_H_
30#define	_GPT_H_
31
32#ifdef __APPLE__
33#include <libkern/OSByteOrder.h>
34#include <IOKit/storage/IOGUIDPartitionScheme.h>
35#else
36#include <sys/endian.h>
37#include <sys/gpt.h>
38#endif
39
40#ifdef __APPLE__
41#include <uuid/uuid.h>
42#else
43#include <uuid.h>
44#endif
45
46#ifdef __APPLE__
47#ifndef htole16
48#define htole16(x)  OSSwapHostToLittleInt16((x))
49#endif
50#ifndef htole32
51#define htole32(x)  OSSwapHostToLittleInt32((x))
52#endif
53#ifndef htole64
54#define htole64(x)  OSSwapHostToLittleInt64((x))
55#endif
56#ifndef le16toh
57#define le16toh(x)  OSSwapLittleToHostInt16((x))
58#endif
59#ifndef le32toh
60#define le32toh(x)  OSSwapLittleToHostInt32((x))
61#endif
62#ifndef le64toh
63#define le64toh(x)  OSSwapLittleToHostInt64((x))
64#endif
65#ifndef uuid_create
66#define uuid_create(x, z)  uuid_generate(*(x))
67#endif
68#ifndef uuid_create_nil
69#define uuid_create_nil(x, z)  uuid_copy(*(x), GPT_ENT_TYPE_UNUSED)
70#endif
71#ifndef uuid_equal
72#define uuid_equal(x, y, z)  !uuid_compare(*(x), *(y))
73#endif
74#ifndef uuid_is_nil
75#define uuid_is_nil(x, z)  uuid_is_null(*(x))
76#endif
77#ifndef uuid_to_string
78#define uuid_to_string(x, y, z)  *(y) = malloc(40);  uuid_unparse(*(x), *(y))
79#endif
80UUID_DEFINE(GPT_ENT_TYPE_APPLE_HFS,0x48,0x46,0x53,0x00,0x00,0x00,0x11,0xAA,0xAA,0x11,0x00,0x30,0x65,0x43,0xEC,0xAC);
81UUID_DEFINE(GPT_ENT_TYPE_EFI,0xC1,0x2A,0x73,0x28,0xF8,0x1F,0x11,0xD2,0xBA,0x4B,0x00,0xA0,0xC9,0x3E,0xC9,0x3B);
82UUID_DEFINE(GPT_ENT_TYPE_MS_BASIC_DATA,0xEB,0xD0,0xA0,0xA2,0xB9,0xE5,0x44,0x33,0x87,0xC0,0x68,0xB6,0xB7,0x26,0x99,0xC7);
83UUID_DEFINE(GPT_ENT_TYPE_UNUSED,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
84#endif
85
86void	le_uuid_dec(void const *, uuid_t *);
87void	le_uuid_enc(void *, uuid_t const *);
88int	parse_uuid(const char *, uuid_t *);
89
90struct mbr_part {
91	uint8_t		part_flag;		/* bootstrap flags */
92	uint8_t		part_shd;		/* starting head */
93	uint8_t		part_ssect;		/* starting sector */
94	uint8_t		part_scyl;		/* starting cylinder */
95	uint8_t		part_typ;		/* partition type */
96	uint8_t		part_ehd;		/* end head */
97	uint8_t		part_esect;		/* end sector */
98	uint8_t		part_ecyl;		/* end cylinder */
99	uint16_t	part_start_lo;		/* absolute starting ... */
100	uint16_t	part_start_hi;		/* ... sector number */
101	uint16_t	part_size_lo;		/* partition size ... */
102	uint16_t	part_size_hi;		/* ... in sectors */
103};
104
105struct mbr {
106	uint16_t	mbr_code[223];
107	struct mbr_part	mbr_part[4];
108	uint16_t	mbr_sig;
109#define	MBR_SIG		0xAA55
110};
111
112extern char *device_name;
113extern off_t mediasz;
114extern u_int parts;
115extern u_int secsz;
116extern int readonly, verbose;
117
118uint32_t crc32(const void *, size_t);
119map_t	*gpt_add_part(int, uuid_t *, off_t, off_t, unsigned int *);
120void	gpt_close(int);
121int	gpt_open(const char *);
122void*	gpt_read(int, off_t, size_t);
123int	gpt_write(int, map_t *);
124
125uint8_t *utf16_to_utf8(uint16_t *);
126void	utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
127
128int	cmd_add(int, char *[]);
129int	cmd_boot(int, char *[]);
130int	cmd_create(int, char *[]);
131int	cmd_destroy(int, char *[]);
132int	cmd_label(int, char *[]);
133int	cmd_migrate(int, char *[]);
134int	cmd_recover(int, char *[]);
135int	cmd_remove(int, char *[]);
136int	cmd_show(int, char *[]);
137
138#endif /* _GPT_H_ */
139