1 /*        $NetBSD: gpt.h,v 1.45 2025/02/23 20:47:19 christos Exp $    */
2 
3 /*-
4  * Copyright (c) 2002 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sbin/gpt/gpt.h,v 1.11 2006/06/22 22:05:28 marcel Exp $
29  */
30 
31 #ifndef _GPT_H_
32 #define   _GPT_H_
33 
34 #ifndef HAVE_NBTOOL_CONFIG_H
35 #include <util.h>
36 #else
37 #include "opendisk.h"
38 #include "namespace.h"
39 #endif
40 
41 #include "gpt_uuid.h"
42 
43 struct mbr_part {
44           uint8_t             part_flag;                    /* bootstrap flags */
45           uint8_t             part_shd;           /* starting head */
46           uint8_t             part_ssect;                   /* starting sector */
47           uint8_t             part_scyl;                    /* starting cylinder */
48           uint8_t             part_typ;           /* partition type */
49           uint8_t             part_ehd;           /* end head */
50           uint8_t             part_esect;                   /* end sector */
51           uint8_t             part_ecyl;                    /* end cylinder */
52           uint16_t  part_start_lo;                /* absolute starting ... */
53           uint16_t  part_start_hi;                /* ... sector number */
54           uint16_t  part_size_lo;                 /* partition size ... */
55           uint16_t  part_size_hi;                 /* ... in sectors */
56 };
57 
58 struct mbr {
59           uint8_t             mbr_code[446];
60           struct mbr_part     mbr_part[4];
61           uint16_t  mbr_sig;
62 #define   MBR_SIG             0xAA55
63 };
64 
65 typedef struct gpt *gpt_t;
66 typedef struct map *map_t;
67 
68 struct gpt_cmd {
69           const char *name;
70           int (*fptr)(gpt_t, int, char *[]);
71           const char **help;
72           size_t hlen;
73           int flags;
74 };
75 
76 uint32_t crc32(const void *, size_t);
77 void      gpt_close(gpt_t);
78 int       gpt_gpt(gpt_t, off_t, int);
79 gpt_t     gpt_open(const char *, int, int, off_t, u_int, time_t);
80 #define GPT_READONLY          0x01
81 #define GPT_MODIFIED          0x02
82 #define GPT_QUIET   0x04
83 #define GPT_NOSYNC  0x08
84 #define GPT_FILE    0x10
85 #define GPT_TIMESTAMP         0x20
86 #define GPT_SYNC    0x40
87 #define GPT_HYBRID  0x80
88 #define GPT_OPTGPT  0x4000
89 #define GPT_OPTDEV  0x8000
90 
91 void*     gpt_read(gpt_t, off_t, size_t);
92 off_t     gpt_last(gpt_t);
93 off_t     gpt_create(gpt_t, off_t, u_int, int);
94 int       gpt_write(gpt_t, map_t);
95 int       gpt_write_crc(gpt_t, map_t, map_t);
96 int       gpt_write_primary(gpt_t);
97 int       gpt_write_backup(gpt_t);
98 struct gpt_hdr *gpt_hdr(gpt_t);
99 void      gpt_msg(gpt_t, const char *, ...) __printflike(2, 3);
100 void      gpt_warn(gpt_t, const char *, ...) __printflike(2, 3);
101 void      gpt_warnx(gpt_t, const char *, ...) __printflike(2, 3);
102 void      gpt_create_pmbr_part(struct mbr_part *, off_t, int);
103 struct gpt_ent *gpt_ent(map_t, map_t, unsigned int);
104 struct gpt_ent *gpt_ent_primary(gpt_t, unsigned int);
105 struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);
106 int       gpt_usage(const char *, const struct gpt_cmd *);
107 
108 void      utf16_to_utf8(const uint16_t *, size_t, uint8_t *, size_t);
109 void      utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
110 
111 #define GPT_FIND "ab:i:L:s:t:"
112 
113 struct gpt_find {
114           int all;
115           gpt_uuid_t type;
116           off_t block, size;
117           unsigned int entry;
118           uint8_t *name, *label;
119           const char *msg;
120 };
121 int       gpt_change_ent(gpt_t, const struct gpt_find *,
122     void (*)(struct gpt_ent *, void *, int), void *);
123 int       gpt_change_hdr(gpt_t, const struct gpt_find *,
124     void (*)(struct gpt_hdr *, void *, int), void *);
125 int       gpt_add_find(gpt_t, struct gpt_find *, int);
126 
127 #define GPT_AIS "a:i:s:"
128 int       gpt_add_ais(gpt_t, off_t *, u_int *, off_t *, int);
129 off_t     gpt_check_ais(gpt_t, off_t, u_int, off_t);
130 
131 int       gpt_attr_get(gpt_t, uint64_t *);
132 const char *gpt_attr_list(char *, size_t, uint64_t);
133 void      gpt_attr_help(const char *);
134 int       gpt_attr_update(gpt_t, u_int, uint64_t, uint64_t);
135 int       gpt_uint_get(gpt_t, u_int *);
136 int       gpt_human_get(gpt_t, off_t *);
137 int       gpt_uuid_get(gpt_t, gpt_uuid_t *);
138 int       gpt_name_get(gpt_t, void *);
139 int       gpt_add_hdr(gpt_t, int, off_t);
140 void      gpt_show_num(const char *, uintmax_t);
141 
142 extern const struct gpt_cmd
143           c_add,
144 #ifndef HAVE_NBTOOL_CONFIG_H
145           c_backup,
146 #endif
147           c_biosboot,
148           c_create,
149           c_destroy,
150           c_header,
151           c_label,
152           c_migrate,
153           c_recover,
154           c_remove,
155           c_resize,
156           c_resizedisk,
157 #ifndef HAVE_NBTOOL_CONFIG_H
158           c_restore,
159 #endif
160           c_set,
161           c_show,
162           c_type,
163           c_unset,
164           c_uuid;
165 
166 #endif /* _GPT_H_ */
167