[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 |
|
---|
[dc76f4a] | 36 | #ifndef LIBGPT_LIBGPT_H_
|
---|
| 37 | #define LIBGPT_LIBGPT_H_
|
---|
[cbd64057] | 38 |
|
---|
[271e24a] | 39 | #define LIBGPT_NAME "libgpt"
|
---|
[7570e800] | 40 |
|
---|
[271e24a] | 41 | #include <loc.h>
|
---|
[cbd64057] | 42 | #include <sys/types.h>
|
---|
| 43 |
|
---|
[dc76f4a] | 44 | #include "gpt.h"
|
---|
| 45 |
|
---|
[cbd64057] | 46 | /** Block address of GPT header. */
|
---|
| 47 | #define GPT_HDR_BA 1
|
---|
| 48 | /** Block size of GPT header. */
|
---|
| 49 | #define GPT_HDR_BS 1
|
---|
| 50 | /** Minimum number of GPT partition entries */
|
---|
| 51 | #define GPT_MIN_PART_NUM 128
|
---|
| 52 | /** Basic number of GPT partition entries */
|
---|
| 53 | #define GPT_BASE_PART_NUM (GPT_MIN_PART_NUM)
|
---|
[30440ed] | 54 | /** How much fill we ignore before resizing partition array */
|
---|
| 55 | #define GPT_IGNORE_FILL_NUM 10
|
---|
[cbd64057] | 56 |
|
---|
[1c8bfe8] | 57 | /** Unused partition entry */
|
---|
| 58 | #define GPT_PTE_UNUSED 0
|
---|
| 59 |
|
---|
[cbd64057] | 60 | /** GPT header signature ("EFI PART" in ASCII) */
|
---|
[271e24a] | 61 | extern const uint8_t efi_signature[8];
|
---|
[8c95dff] | 62 | extern const uint8_t revision[4];
|
---|
[cbd64057] | 63 |
|
---|
| 64 | typedef struct {
|
---|
| 65 | /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
|
---|
[44c4886] | 66 | * See gpt_alloc_header() to know why. */
|
---|
| 67 | gpt_header_t *header;
|
---|
[cbd64057] | 68 | } gpt_t;
|
---|
| 69 |
|
---|
[d617050] | 70 | typedef gpt_entry_t gpt_part_t;
|
---|
| 71 |
|
---|
[cbd64057] | 72 | typedef struct gpt_parts {
|
---|
| 73 | /** Number of entries */
|
---|
[30440ed] | 74 | size_t fill;
|
---|
[cbd64057] | 75 | /** Size of the array */
|
---|
[30440ed] | 76 | size_t arr_size;
|
---|
[cbd64057] | 77 | /** Resizable partition array */
|
---|
[44c4886] | 78 | gpt_entry_t *part_array;
|
---|
[271e24a] | 79 | } gpt_partitions_t;
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | typedef struct gpt_table {
|
---|
[44c4886] | 83 | gpt_t *gpt;
|
---|
| 84 | gpt_partitions_t *parts;
|
---|
| 85 | service_id_t device;
|
---|
[a2aa81cb] | 86 | } gpt_label_t;
|
---|
[cbd64057] | 87 |
|
---|
| 88 | struct partition_type {
|
---|
[44c4886] | 89 | const char *desc;
|
---|
| 90 | const char *guid;
|
---|
[7570e800] | 91 | };
|
---|
[cbd64057] | 92 |
|
---|
[271e24a] | 93 | extern const struct partition_type gpt_ptypes[];
|
---|
| 94 |
|
---|
[44c4886] | 95 | extern gpt_label_t * gpt_alloc_label(void);
|
---|
| 96 | extern void gpt_free_label(gpt_label_t *);
|
---|
[cbd64057] | 97 |
|
---|
[44c4886] | 98 | extern gpt_t * gpt_alloc_header(size_t);
|
---|
| 99 | extern int gpt_read_header(gpt_label_t *, service_id_t);
|
---|
| 100 | extern int gpt_write_header(gpt_label_t *, service_id_t);
|
---|
[a2aa81cb] | 101 |
|
---|
| 102 | extern gpt_partitions_t * gpt_alloc_partitions(void);
|
---|
[44c4886] | 103 | extern int gpt_read_partitions (gpt_label_t *);
|
---|
| 104 | extern int gpt_write_partitions(gpt_label_t *, service_id_t);
|
---|
| 105 | extern gpt_part_t * gpt_alloc_partition (void);
|
---|
| 106 | extern gpt_part_t * gpt_get_partition (gpt_label_t *);
|
---|
[1c8bfe8] | 107 | extern gpt_part_t * gpt_get_partition_at(gpt_label_t *, size_t);
|
---|
[44c4886] | 108 | extern int gpt_add_partition (gpt_label_t *, gpt_part_t *);
|
---|
| 109 | extern int gpt_remove_partition(gpt_label_t *, size_t);
|
---|
| 110 |
|
---|
| 111 | extern size_t gpt_get_part_type(gpt_part_t *);
|
---|
| 112 | extern void gpt_set_part_type(gpt_part_t *, size_t);
|
---|
| 113 | extern void gpt_set_start_lba(gpt_part_t *, uint64_t);
|
---|
| 114 | extern uint64_t gpt_get_start_lba(gpt_part_t *);
|
---|
| 115 | extern void gpt_set_end_lba (gpt_part_t *, uint64_t);
|
---|
| 116 | extern uint64_t gpt_get_end_lba (gpt_part_t *);
|
---|
| 117 | extern unsigned char * gpt_get_part_name(gpt_part_t *);
|
---|
[1c8bfe8] | 118 | extern void gpt_set_part_name(gpt_part_t *, char *, size_t);
|
---|
[44c4886] | 119 | extern bool gpt_get_flag (gpt_part_t *, GPT_ATTR);
|
---|
| 120 | extern void gpt_set_flag (gpt_part_t *, GPT_ATTR, bool);
|
---|
[d617050] | 121 |
|
---|
[c3cbbb2] | 122 | extern void gpt_set_random_uuid(uint8_t *);
|
---|
[dc76f4a] | 123 | extern uint64_t gpt_get_next_aligned(uint64_t, unsigned int);
|
---|
[d617050] | 124 |
|
---|
[30440ed] | 125 |
|
---|
[1c8bfe8] | 126 | #define gpt_part_foreach(label, iterator) \
|
---|
| 127 | for(gpt_part_t * iterator = (label)->parts->part_array; \
|
---|
[c3cbbb2] | 128 | iterator < (label)->parts->part_array + (label)->parts->arr_size; ++iterator)
|
---|
[cbd64057] | 129 |
|
---|
[44c4886] | 130 | extern void gpt_free_gpt(gpt_t *);
|
---|
| 131 | extern void gpt_free_partitions(gpt_partitions_t *);
|
---|
[cbd64057] | 132 |
|
---|
| 133 | #endif
|
---|
[271e24a] | 134 |
|
---|