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