source: mainline/uspace/lib/gpt/libgpt.h@ 6317b33

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6317b33 was 1c8bfe8, checked in by Dominik Taborsky (AT DOT) <brembyseznamcz>, 12 years ago

GPT updates

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * Copyright (c) 2009 Jiri Svoboda
3 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libgpt
31 * @{
32 */
33/** @file
34 */
35
36#ifndef __GPT_H__
37#define __GPT_H__
38
39#define LIBGPT_NAME "libgpt"
40
41#include <loc.h>
42#include <sys/types.h>
43
44/** Block address of GPT header. */
45#define GPT_HDR_BA 1
46/** Block size of GPT header. */
47#define GPT_HDR_BS 1
48/** Minimum number of GPT partition entries */
49#define GPT_MIN_PART_NUM 128
50/** Basic number of GPT partition entries */
51#define GPT_BASE_PART_NUM (GPT_MIN_PART_NUM)
52/** How much fill we ignore before resizing partition array */
53#define GPT_IGNORE_FILL_NUM 10
54
55/** Unused partition entry */
56#define GPT_PTE_UNUSED 0
57
58/** GPT header signature ("EFI PART" in ASCII) */
59extern const uint8_t efi_signature[8];
60
61typedef enum {
62 AT_REQ_PART = 0,
63 AT_NO_BLOCK_IO,
64 AT_LEGACY_BOOT,
65 AT_UNDEFINED,
66 AT_SPECIFIC = 48
67} GPT_ATTR;
68
69/** GPT header
70 * - all in little endian.
71 */
72typedef struct {
73 uint8_t efi_signature[8];
74 uint32_t revision;
75 uint32_t header_size;
76 uint32_t header_crc32;
77 uint32_t reserved;
78 uint64_t my_lba;
79 uint64_t alternate_lba;
80 uint64_t first_usable_lba;
81 uint64_t last_usable_lba;
82 uint8_t disk_guid[16];
83 uint64_t entry_lba;
84 uint32_t fillries;
85 uint32_t entry_size;
86 uint32_t pe_array_crc32;
87} __attribute__((packed)) gpt_header_t;
88
89typedef struct {
90 /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
91 * See gpt_alloc_header() to know why. */
92 gpt_header_t *header;
93} gpt_t;
94
95/** GPT partition entry */
96typedef struct {
97 uint8_t part_type[16];
98 uint8_t part_id[16];
99 uint64_t start_lba;
100 uint64_t end_lba;
101 uint64_t attributes;
102 uint8_t part_name[72];
103} __attribute__((packed)) gpt_entry_t;
104
105
106//typedef struct g_part {
107 ///** Partition entry is in use **/
108 //bool present;
109 ///** Address of first block */
110 //aoff64_t start_addr;
111 ///** Number of blocks */
112 //aoff64_t length;
113 ///** Raw data access */
114 //gpt_entry_t raw_data; //TODO: a pointer or just a member?
115//}gpt_part_t;
116typedef gpt_entry_t gpt_part_t;
117
118
119typedef struct gpt_parts {
120 /** Number of entries */
121 size_t fill;
122 /** Size of the array */
123 size_t arr_size;
124 /** Resizable partition array */
125 gpt_entry_t *part_array;
126} gpt_partitions_t;
127
128
129typedef struct gpt_table {
130 gpt_t *gpt;
131 gpt_partitions_t *parts;
132 service_id_t device;
133} gpt_label_t;
134
135struct partition_type {
136 const char *desc;
137 const char *guid;
138};
139
140extern const struct partition_type gpt_ptypes[];
141
142extern gpt_label_t * gpt_alloc_label(void);
143extern void gpt_free_label(gpt_label_t *);
144
145extern gpt_t * gpt_alloc_header(size_t);
146extern int gpt_read_header(gpt_label_t *, service_id_t);
147extern int gpt_write_header(gpt_label_t *, service_id_t);
148
149extern gpt_partitions_t * gpt_alloc_partitions(void);
150extern int gpt_read_partitions (gpt_label_t *);
151extern int gpt_write_partitions(gpt_label_t *, service_id_t);
152extern gpt_part_t * gpt_alloc_partition (void);
153extern gpt_part_t * gpt_get_partition (gpt_label_t *);
154extern gpt_part_t * gpt_get_partition_at(gpt_label_t *, size_t);
155extern int gpt_add_partition (gpt_label_t *, gpt_part_t *);
156extern int gpt_remove_partition(gpt_label_t *, size_t);
157
158extern size_t gpt_get_part_type(gpt_part_t *);
159extern void gpt_set_part_type(gpt_part_t *, size_t);
160extern void gpt_set_start_lba(gpt_part_t *, uint64_t);
161extern uint64_t gpt_get_start_lba(gpt_part_t *);
162extern void gpt_set_end_lba (gpt_part_t *, uint64_t);
163extern uint64_t gpt_get_end_lba (gpt_part_t *);
164extern unsigned char * gpt_get_part_name(gpt_part_t *);
165extern void gpt_set_part_name(gpt_part_t *, char *, size_t);
166extern bool gpt_get_flag (gpt_part_t *, GPT_ATTR);
167extern void gpt_set_flag (gpt_part_t *, GPT_ATTR, bool);
168
169
170
171#define gpt_part_foreach(label, iterator) \
172 for(gpt_part_t * iterator = (label)->parts->part_array; \
173 iterator < (label)->parts->part_array + (label)->parts->fill; ++iterator)
174
175extern void gpt_free_gpt(gpt_t *);
176extern void gpt_free_partitions(gpt_partitions_t *);
177
178#endif
179
Note: See TracBrowser for help on using the repository browser.