[cbd64057] | 1 | /*
|
---|
[a2aa81cb] | 2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
[622a50b] | 3 | * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
|
---|
[cbd64057] | 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 |
|
---|
[271e24a] | 39 | #define LIBGPT_NAME "libgpt"
|
---|
[7570e800] | 40 |
|
---|
[271e24a] | 41 | #include <loc.h>
|
---|
[cbd64057] | 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)
|
---|
[30440ed] | 52 | /** How much fill we ignore before resizing partition array */
|
---|
| 53 | #define GPT_IGNORE_FILL_NUM 10
|
---|
[cbd64057] | 54 |
|
---|
[1c8bfe8] | 55 | /** Unused partition entry */
|
---|
| 56 | #define GPT_PTE_UNUSED 0
|
---|
| 57 |
|
---|
[cbd64057] | 58 | /** GPT header signature ("EFI PART" in ASCII) */
|
---|
[271e24a] | 59 | extern const uint8_t efi_signature[8];
|
---|
[cbd64057] | 60 |
|
---|
[30440ed] | 61 | typedef 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 |
|
---|
[7570e800] | 69 | /** GPT header
|
---|
| 70 | * - all in little endian.
|
---|
| 71 | */
|
---|
[cbd64057] | 72 | typedef 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;
|
---|
[30440ed] | 84 | uint32_t fillries;
|
---|
[cbd64057] | 85 | uint32_t entry_size;
|
---|
| 86 | uint32_t pe_array_crc32;
|
---|
| 87 | } __attribute__((packed)) gpt_header_t;
|
---|
| 88 |
|
---|
| 89 | typedef struct {
|
---|
| 90 | /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
|
---|
[44c4886] | 91 | * See gpt_alloc_header() to know why. */
|
---|
| 92 | gpt_header_t *header;
|
---|
[cbd64057] | 93 | } gpt_t;
|
---|
| 94 |
|
---|
| 95 | /** GPT partition entry */
|
---|
| 96 | typedef 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 |
|
---|
[d617050] | 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;
|
---|
| 116 | typedef gpt_entry_t gpt_part_t;
|
---|
| 117 |
|
---|
[cbd64057] | 118 |
|
---|
| 119 | typedef struct gpt_parts {
|
---|
| 120 | /** Number of entries */
|
---|
[30440ed] | 121 | size_t fill;
|
---|
[cbd64057] | 122 | /** Size of the array */
|
---|
[30440ed] | 123 | size_t arr_size;
|
---|
[cbd64057] | 124 | /** Resizable partition array */
|
---|
[44c4886] | 125 | gpt_entry_t *part_array;
|
---|
[271e24a] | 126 | } gpt_partitions_t;
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | typedef struct gpt_table {
|
---|
[44c4886] | 130 | gpt_t *gpt;
|
---|
| 131 | gpt_partitions_t *parts;
|
---|
| 132 | service_id_t device;
|
---|
[a2aa81cb] | 133 | } gpt_label_t;
|
---|
[cbd64057] | 134 |
|
---|
| 135 | struct partition_type {
|
---|
[44c4886] | 136 | const char *desc;
|
---|
| 137 | const char *guid;
|
---|
[7570e800] | 138 | };
|
---|
[cbd64057] | 139 |
|
---|
[271e24a] | 140 | extern const struct partition_type gpt_ptypes[];
|
---|
| 141 |
|
---|
[44c4886] | 142 | extern gpt_label_t * gpt_alloc_label(void);
|
---|
| 143 | extern void gpt_free_label(gpt_label_t *);
|
---|
[cbd64057] | 144 |
|
---|
[44c4886] | 145 | extern gpt_t * gpt_alloc_header(size_t);
|
---|
| 146 | extern int gpt_read_header(gpt_label_t *, service_id_t);
|
---|
| 147 | extern int gpt_write_header(gpt_label_t *, service_id_t);
|
---|
[a2aa81cb] | 148 |
|
---|
| 149 | extern gpt_partitions_t * gpt_alloc_partitions(void);
|
---|
[44c4886] | 150 | extern int gpt_read_partitions (gpt_label_t *);
|
---|
| 151 | extern int gpt_write_partitions(gpt_label_t *, service_id_t);
|
---|
| 152 | extern gpt_part_t * gpt_alloc_partition (void);
|
---|
| 153 | extern gpt_part_t * gpt_get_partition (gpt_label_t *);
|
---|
[1c8bfe8] | 154 | extern gpt_part_t * gpt_get_partition_at(gpt_label_t *, size_t);
|
---|
[44c4886] | 155 | extern int gpt_add_partition (gpt_label_t *, gpt_part_t *);
|
---|
| 156 | extern int gpt_remove_partition(gpt_label_t *, size_t);
|
---|
| 157 |
|
---|
| 158 | extern size_t gpt_get_part_type(gpt_part_t *);
|
---|
| 159 | extern void gpt_set_part_type(gpt_part_t *, size_t);
|
---|
| 160 | extern void gpt_set_start_lba(gpt_part_t *, uint64_t);
|
---|
| 161 | extern uint64_t gpt_get_start_lba(gpt_part_t *);
|
---|
| 162 | extern void gpt_set_end_lba (gpt_part_t *, uint64_t);
|
---|
| 163 | extern uint64_t gpt_get_end_lba (gpt_part_t *);
|
---|
| 164 | extern unsigned char * gpt_get_part_name(gpt_part_t *);
|
---|
[1c8bfe8] | 165 | extern void gpt_set_part_name(gpt_part_t *, char *, size_t);
|
---|
[44c4886] | 166 | extern bool gpt_get_flag (gpt_part_t *, GPT_ATTR);
|
---|
| 167 | extern void gpt_set_flag (gpt_part_t *, GPT_ATTR, bool);
|
---|
[d617050] | 168 |
|
---|
| 169 |
|
---|
[30440ed] | 170 |
|
---|
[1c8bfe8] | 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)
|
---|
[cbd64057] | 174 |
|
---|
[44c4886] | 175 | extern void gpt_free_gpt(gpt_t *);
|
---|
| 176 | extern void gpt_free_partitions(gpt_partitions_t *);
|
---|
[cbd64057] | 177 |
|
---|
| 178 | #endif
|
---|
[271e24a] | 179 |
|
---|