| 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 | /** GPT header signature ("EFI PART" in ASCII) */
|
|---|
| 56 | extern const uint8_t efi_signature[8];
|
|---|
| 57 |
|
|---|
| 58 | typedef enum {
|
|---|
| 59 | AT_REQ_PART = 0,
|
|---|
| 60 | AT_NO_BLOCK_IO,
|
|---|
| 61 | AT_LEGACY_BOOT,
|
|---|
| 62 | AT_UNDEFINED,
|
|---|
| 63 | AT_SPECIFIC = 48
|
|---|
| 64 | } GPT_ATTR;
|
|---|
| 65 |
|
|---|
| 66 | /** GPT header
|
|---|
| 67 | * - all in little endian.
|
|---|
| 68 | */
|
|---|
| 69 | typedef struct {
|
|---|
| 70 | uint8_t efi_signature[8];
|
|---|
| 71 | uint32_t revision;
|
|---|
| 72 | uint32_t header_size;
|
|---|
| 73 | uint32_t header_crc32;
|
|---|
| 74 | uint32_t reserved;
|
|---|
| 75 | uint64_t my_lba;
|
|---|
| 76 | uint64_t alternate_lba;
|
|---|
| 77 | uint64_t first_usable_lba;
|
|---|
| 78 | uint64_t last_usable_lba;
|
|---|
| 79 | uint8_t disk_guid[16];
|
|---|
| 80 | uint64_t entry_lba;
|
|---|
| 81 | uint32_t fillries;
|
|---|
| 82 | uint32_t entry_size;
|
|---|
| 83 | uint32_t pe_array_crc32;
|
|---|
| 84 | } __attribute__((packed)) gpt_header_t;
|
|---|
| 85 |
|
|---|
| 86 | typedef struct {
|
|---|
| 87 | /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
|
|---|
| 88 | * See gpt_read_gpt_header() to know why. */
|
|---|
| 89 | gpt_header_t * raw_data;
|
|---|
| 90 | /** Device where the data are from */
|
|---|
| 91 | service_id_t device;
|
|---|
| 92 | /** Linked list of partitions (initially NULL) */
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 118 |
|
|---|
| 119 | typedef 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 |
|
|---|
| 129 | typedef struct gpt_table {
|
|---|
| 130 | gpt_t * gpt;
|
|---|
| 131 | gpt_partitions_t * parts;
|
|---|
| 132 | } gpt_label_t;
|
|---|
| 133 |
|
|---|
| 134 | struct partition_type {
|
|---|
| 135 | const char * desc;
|
|---|
| 136 | const char * guid;
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 | extern const struct partition_type gpt_ptypes[];
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | extern gpt_t * gpt_alloc_gpt_header(void);
|
|---|
| 143 | extern gpt_t * gpt_read_gpt_header(service_id_t dev_handle);
|
|---|
| 144 | extern int gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle);
|
|---|
| 145 |
|
|---|
| 146 | extern gpt_partitions_t * gpt_alloc_partitions(void);
|
|---|
| 147 | extern gpt_partitions_t * gpt_read_partitions(gpt_t * gpt);
|
|---|
| 148 | extern int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle);
|
|---|
| 149 | extern gpt_part_t * gpt_alloc_partition (gpt_partitions_t * parts);
|
|---|
| 150 | extern int gpt_add_partition (gpt_partitions_t * parts, gpt_part_t * partition);
|
|---|
| 151 | extern int gpt_remove_partition(gpt_partitions_t * parts, size_t idx);
|
|---|
| 152 |
|
|---|
| 153 | extern size_t gpt_get_part_type(gpt_part_t * p);
|
|---|
| 154 | extern void gpt_set_part_type(gpt_part_t * p, size_t type);
|
|---|
| 155 | extern void gpt_set_start_lba(gpt_part_t * p, uint64_t start);
|
|---|
| 156 | extern uint64_t gpt_get_start_lba(gpt_part_t * p);
|
|---|
| 157 | extern void gpt_set_end_lba (gpt_part_t * p, uint64_t end);
|
|---|
| 158 | extern uint64_t gpt_get_end_lba (gpt_part_t * p);
|
|---|
| 159 | extern unsigned char * gpt_get_part_name(gpt_part_t * p);
|
|---|
| 160 | extern void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length);
|
|---|
| 161 | extern bool gpt_get_flag (gpt_part_t * p, GPT_ATTR flag);
|
|---|
| 162 | extern void gpt_set_flag (gpt_part_t * p, GPT_ATTR flag, bool value);
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | #define gpt_part_foreach(parts, iterator) \
|
|---|
| 167 | for(gpt_part_t * iterator = (parts)->part_array; \
|
|---|
| 168 | iterator < (parts)->part_array + (parts)->fill; ++iterator)
|
|---|
| 169 |
|
|---|
| 170 | extern void gpt_free_gpt(gpt_t * gpt);
|
|---|
| 171 | extern void gpt_free_partitions(gpt_partitions_t * parts);
|
|---|
| 172 |
|
|---|
| 173 | #endif
|
|---|
| 174 |
|
|---|