[cbd64057] | 1 | /*
|
---|
[5beb1ff] | 2 | * Copyright (c) 2009 Jiri Svoboda, 2011, 2012, 2013 Dominik Taborsky
|
---|
[cbd64057] | 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 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 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 |
|
---|
| 29 | /** @addtogroup libgpt
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #ifndef __GPT_H__
|
---|
| 36 | #define __GPT_H__
|
---|
| 37 |
|
---|
[271e24a] | 38 | #define LIBGPT_NAME "libgpt"
|
---|
[7570e800] | 39 |
|
---|
[271e24a] | 40 | #include <loc.h>
|
---|
[cbd64057] | 41 | #include <sys/types.h>
|
---|
| 42 |
|
---|
| 43 | /** Block address of GPT header. */
|
---|
| 44 | #define GPT_HDR_BA 1
|
---|
| 45 | /** Block size of GPT header. */
|
---|
| 46 | #define GPT_HDR_BS 1
|
---|
| 47 | /** Minimum number of GPT partition entries */
|
---|
| 48 | #define GPT_MIN_PART_NUM 128
|
---|
| 49 | /** Basic number of GPT partition entries */
|
---|
| 50 | #define GPT_BASE_PART_NUM (GPT_MIN_PART_NUM)
|
---|
[30440ed] | 51 | /** How much fill we ignore before resizing partition array */
|
---|
| 52 | #define GPT_IGNORE_FILL_NUM 10
|
---|
[cbd64057] | 53 |
|
---|
| 54 | /** GPT header signature ("EFI PART" in ASCII) */
|
---|
[271e24a] | 55 | extern const uint8_t efi_signature[8];
|
---|
[cbd64057] | 56 |
|
---|
[30440ed] | 57 | typedef enum {
|
---|
| 58 | AT_REQ_PART = 0,
|
---|
| 59 | AT_NO_BLOCK_IO,
|
---|
| 60 | AT_LEGACY_BOOT,
|
---|
| 61 | AT_UNDEFINED,
|
---|
| 62 | AT_SPECIFIC = 48
|
---|
| 63 | } GPT_ATTR;
|
---|
| 64 |
|
---|
[7570e800] | 65 | /** GPT header
|
---|
| 66 | * - all in little endian.
|
---|
| 67 | */
|
---|
[cbd64057] | 68 | typedef struct {
|
---|
| 69 | uint8_t efi_signature[8];
|
---|
| 70 | uint32_t revision;
|
---|
| 71 | uint32_t header_size;
|
---|
| 72 | uint32_t header_crc32;
|
---|
| 73 | uint32_t reserved;
|
---|
| 74 | uint64_t my_lba;
|
---|
| 75 | uint64_t alternate_lba;
|
---|
| 76 | uint64_t first_usable_lba;
|
---|
| 77 | uint64_t last_usable_lba;
|
---|
| 78 | uint8_t disk_guid[16];
|
---|
| 79 | uint64_t entry_lba;
|
---|
[30440ed] | 80 | uint32_t fillries;
|
---|
[cbd64057] | 81 | uint32_t entry_size;
|
---|
| 82 | uint32_t pe_array_crc32;
|
---|
| 83 | } __attribute__((packed)) gpt_header_t;
|
---|
| 84 |
|
---|
| 85 | typedef struct {
|
---|
| 86 | /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
|
---|
| 87 | * See gpt_read_gpt_header() to know why. */
|
---|
| 88 | gpt_header_t * raw_data;
|
---|
| 89 | /** Device where the data are from */
|
---|
| 90 | service_id_t device;
|
---|
| 91 | /** Linked list of partitions (initially NULL) */
|
---|
| 92 | } gpt_t;
|
---|
| 93 |
|
---|
| 94 | /** GPT partition entry */
|
---|
| 95 | typedef struct {
|
---|
| 96 | uint8_t part_type[16];
|
---|
| 97 | uint8_t part_id[16];
|
---|
| 98 | uint64_t start_lba;
|
---|
| 99 | uint64_t end_lba;
|
---|
| 100 | uint64_t attributes;
|
---|
| 101 | uint8_t part_name[72];
|
---|
| 102 | } __attribute__((packed)) gpt_entry_t;
|
---|
| 103 |
|
---|
[d617050] | 104 |
|
---|
| 105 | //typedef struct g_part {
|
---|
| 106 | ///** Partition entry is in use **/
|
---|
| 107 | //bool present;
|
---|
| 108 | ///** Address of first block */
|
---|
| 109 | //aoff64_t start_addr;
|
---|
| 110 | ///** Number of blocks */
|
---|
| 111 | //aoff64_t length;
|
---|
| 112 | ///** Raw data access */
|
---|
| 113 | //gpt_entry_t raw_data; //TODO: a pointer or just a member?
|
---|
| 114 | //}gpt_part_t;
|
---|
| 115 | typedef gpt_entry_t gpt_part_t;
|
---|
| 116 |
|
---|
[cbd64057] | 117 |
|
---|
| 118 | typedef struct gpt_parts {
|
---|
| 119 | /** Number of entries */
|
---|
[30440ed] | 120 | size_t fill;
|
---|
[cbd64057] | 121 | /** Size of the array */
|
---|
[30440ed] | 122 | size_t arr_size;
|
---|
[cbd64057] | 123 | /** Resizable partition array */
|
---|
| 124 | gpt_entry_t * part_array;
|
---|
[271e24a] | 125 | } gpt_partitions_t;
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | typedef struct gpt_table {
|
---|
| 129 | gpt_t * gpt;
|
---|
| 130 | gpt_partitions_t * parts;
|
---|
| 131 | } gpt_table_t;
|
---|
[cbd64057] | 132 |
|
---|
| 133 | struct partition_type {
|
---|
| 134 | const char * desc;
|
---|
| 135 | const char * guid;
|
---|
[7570e800] | 136 | };
|
---|
[cbd64057] | 137 |
|
---|
[271e24a] | 138 | extern const struct partition_type gpt_ptypes[];
|
---|
| 139 |
|
---|
[cbd64057] | 140 |
|
---|
| 141 |
|
---|
| 142 | extern gpt_t * gpt_read_gpt_header(service_id_t dev_handle);
|
---|
[7570e800] | 143 | extern int gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle);
|
---|
[cbd64057] | 144 |
|
---|
[d617050] | 145 | extern gpt_partitions_t * gpt_read_partitions (gpt_t * gpt);
|
---|
| 146 | extern int gpt_write_partitions (gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle);
|
---|
| 147 | extern gpt_part_t * gpt_alloc_partition (gpt_partitions_t * parts);
|
---|
| 148 | extern int gpt_add_partition (gpt_partitions_t * parts, gpt_part_t * partition);
|
---|
| 149 | extern int gpt_remove_partition(gpt_partitions_t * parts, size_t idx);
|
---|
| 150 |
|
---|
| 151 | extern size_t gpt_get_part_type (gpt_part_t * p);
|
---|
| 152 | extern void gpt_set_part_type (gpt_part_t * p, size_t type);
|
---|
| 153 | extern void gpt_set_start_lba (gpt_part_t * p, uint64_t start);
|
---|
| 154 | extern uint64_t gpt_get_start_lba (gpt_part_t * p);
|
---|
[8f6c7785] | 155 | extern void gpt_set_end_lba (gpt_part_t * p, uint64_t end);
|
---|
[d617050] | 156 | extern uint64_t gpt_get_end_lba (gpt_part_t * p);
|
---|
| 157 | extern unsigned char * gpt_get_part_name (gpt_part_t * p);
|
---|
| 158 | extern void gpt_set_part_name (gpt_part_t * p, char * name[], size_t length);
|
---|
| 159 | extern bool gpt_get_flag (gpt_part_t * p, GPT_ATTR flag);
|
---|
| 160 | extern void gpt_set_flag (gpt_part_t * p, GPT_ATTR flag, bool value);
|
---|
| 161 |
|
---|
| 162 |
|
---|
[30440ed] | 163 |
|
---|
[8f6c7785] | 164 | #define gpt_part_foreach(parts, iterator) \
|
---|
| 165 | for(gpt_part_t * iterator = (parts)->part_array; \
|
---|
| 166 | iterator < (parts)->part_array + (parts)->fill; ++iterator)
|
---|
[cbd64057] | 167 |
|
---|
| 168 | extern void gpt_free_gpt(gpt_t * gpt);
|
---|
[271e24a] | 169 | extern void gpt_free_partitions(gpt_partitions_t * parts);
|
---|
[cbd64057] | 170 |
|
---|
| 171 | #endif
|
---|
[271e24a] | 172 |
|
---|