[d3a92c87] | 1 | /*
|
---|
[a2aa81cb] | 2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
[6453e306] | 3 | * Copyright (c) 2011-2013 Dominik Taborsky
|
---|
[d3a92c87] | 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 |
|
---|
[6453e306] | 30 | /** @addtogroup libmbr
|
---|
[d3a92c87] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef LIBMBR_LIBMBR_H_
|
---|
| 37 | #define LIBMBR_LIBMBR_H_
|
---|
| 38 |
|
---|
[94c49fd3] | 39 | #include <adt/list.h>
|
---|
| 40 | #include <loc.h>
|
---|
[271e24a] | 41 | #include <sys/types.h>
|
---|
[dc76f4a] | 42 | #include "mbr.h"
|
---|
[271e24a] | 43 |
|
---|
[6453e306] | 44 | /*
|
---|
| 45 | * WARNING: When changing both header and partitions, write first header,
|
---|
| 46 | * then partitions. The MBR headers' raw_data is not updated to follow
|
---|
| 47 | * partition changes.
|
---|
| 48 | *
|
---|
| 49 | * NOTE: Writing partitions writes the complete header as well.
|
---|
| 50 | */
|
---|
[d3a92c87] | 51 |
|
---|
| 52 | typedef enum {
|
---|
[a2aa81cb] | 53 | /** Other flags unknown - saving previous state */
|
---|
[d3a92c87] | 54 | /** Bootability */
|
---|
[a2aa81cb] | 55 | ST_BOOT = 7,
|
---|
[d3a92c87] | 56 | /** Logical partition, 0 = primary, 1 = logical*/
|
---|
[a2aa81cb] | 57 | ST_LOGIC = 8
|
---|
[6453e306] | 58 | } mbr_flags_t;
|
---|
[d3a92c87] | 59 |
|
---|
[d617050] | 60 | typedef enum {
|
---|
| 61 | /** No error */
|
---|
| 62 | ERR_OK = 0,
|
---|
| 63 | /** All primary partitions already present */
|
---|
| 64 | ERR_PRIMARY_FULL,
|
---|
| 65 | /** Extended partition already present */
|
---|
| 66 | ERR_EXTENDED_PRESENT,
|
---|
| 67 | /** No extended partition present */
|
---|
| 68 | ERR_NO_EXTENDED,
|
---|
| 69 | /** Partition overlapping */
|
---|
| 70 | ERR_OVERLAP,
|
---|
[a2aa81cb] | 71 | /** Partition out of bounds */
|
---|
[d617050] | 72 | ERR_OUT_BOUNDS,
|
---|
[8f6c7785] | 73 | /** No space left for EBR */
|
---|
| 74 | ERR_NO_EBR,
|
---|
| 75 | /** Out of memory */
|
---|
| 76 | ERR_NOMEM,
|
---|
[a2aa81cb] | 77 | /** Libblock error */
|
---|
| 78 | ERR_LIBBLOCK,
|
---|
| 79 | } mbr_err_val;
|
---|
[d617050] | 80 |
|
---|
[d3a92c87] | 81 | /** MBR header */
|
---|
| 82 | typedef struct {
|
---|
| 83 | /** Raw access to data */
|
---|
| 84 | br_block_t raw_data;
|
---|
| 85 | } mbr_t;
|
---|
| 86 |
|
---|
| 87 | /** Partition */
|
---|
[271e24a] | 88 | typedef struct mbr_part {
|
---|
[d3a92c87] | 89 | /** The link in the doubly-linked list */
|
---|
| 90 | link_t link;
|
---|
| 91 | /** Partition type */
|
---|
| 92 | uint8_t type;
|
---|
| 93 | /** Flags */
|
---|
[a2aa81cb] | 94 | uint16_t status;
|
---|
[d3a92c87] | 95 | /** Address of first block */
|
---|
[271e24a] | 96 | uint32_t start_addr;
|
---|
[d3a92c87] | 97 | /** Number of blocks */
|
---|
[271e24a] | 98 | uint32_t length;
|
---|
[d3a92c87] | 99 | /** Points to Extended Boot Record of logical partition */
|
---|
[6453e306] | 100 | br_block_t *ebr;
|
---|
[8f6c7785] | 101 | /** EBR address */
|
---|
| 102 | uint32_t ebr_addr;
|
---|
[271e24a] | 103 | } mbr_part_t;
|
---|
[d3a92c87] | 104 |
|
---|
[30440ed] | 105 | /** Partition list structure */
|
---|
[d3a92c87] | 106 | typedef struct mbr_parts {
|
---|
| 107 | /** Number of primary partitions */
|
---|
| 108 | unsigned char n_primary;
|
---|
[d617050] | 109 | /** Index to the extended partition in the array */
|
---|
[6453e306] | 110 | link_t *l_extended;
|
---|
[d3a92c87] | 111 | /** Number of logical partitions */
|
---|
| 112 | unsigned int n_logical;
|
---|
[d617050] | 113 | /** Logical partition linked list */
|
---|
[d3a92c87] | 114 | list_t list;
|
---|
[271e24a] | 115 | } mbr_partitions_t;
|
---|
[d3a92c87] | 116 |
|
---|
[30440ed] | 117 | /** Both header and partition list */
|
---|
[a2aa81cb] | 118 | typedef struct mbr_label {
|
---|
| 119 | /** MBR header */
|
---|
[6453e306] | 120 | mbr_t *mbr;
|
---|
[a2aa81cb] | 121 | /** Partition list */
|
---|
[271e24a] | 122 | mbr_partitions_t * parts;
|
---|
[a2aa81cb] | 123 | /** Device where the data are from (or for) */
|
---|
| 124 | service_id_t device;
|
---|
| 125 | } mbr_label_t;
|
---|
| 126 |
|
---|
[6453e306] | 127 | #define mbr_part_foreach(label, iterator) \
|
---|
| 128 | for (iterator = list_get_instance((label)->parts->list.head.next, mbr_part_t, link); \
|
---|
| 129 | iterator != list_get_instance(&((label)->parts->list.head), mbr_part_t, link); \
|
---|
| 130 | iterator = list_get_instance(iterator->link.next, mbr_part_t, link))
|
---|
[d3a92c87] | 131 |
|
---|
[6453e306] | 132 | extern mbr_label_t *mbr_alloc_label(void);
|
---|
| 133 |
|
---|
| 134 | extern void mbr_set_device(mbr_label_t *, service_id_t);
|
---|
| 135 | extern mbr_t *mbr_alloc_mbr(void);
|
---|
[a2aa81cb] | 136 | extern int mbr_read_mbr(mbr_label_t *, service_id_t);
|
---|
| 137 | extern int mbr_write_mbr(mbr_label_t *, service_id_t);
|
---|
| 138 | extern int mbr_is_mbr(mbr_label_t *);
|
---|
[d3a92c87] | 139 |
|
---|
[6453e306] | 140 | extern int mbr_read_partitions(mbr_label_t *);
|
---|
| 141 | extern int mbr_write_partitions(mbr_label_t *, service_id_t);
|
---|
| 142 | extern mbr_part_t *mbr_alloc_partition(void);
|
---|
| 143 | extern mbr_partitions_t *mbr_alloc_partitions(void);
|
---|
| 144 | extern mbr_err_val mbr_add_partition(mbr_label_t *, mbr_part_t *);
|
---|
| 145 | extern int mbr_remove_partition(mbr_label_t *, size_t);
|
---|
| 146 | extern int mbr_get_flag(mbr_part_t *, mbr_flags_t);
|
---|
| 147 | extern void mbr_set_flag(mbr_part_t *, mbr_flags_t, bool);
|
---|
| 148 | extern uint32_t mbr_get_next_aligned(uint32_t, unsigned int);
|
---|
| 149 | extern list_t *mbr_get_list(mbr_label_t *);
|
---|
| 150 | extern mbr_part_t *mbr_get_first_partition(mbr_label_t *);
|
---|
| 151 | extern mbr_part_t *mbr_get_next_partition(mbr_label_t *, mbr_part_t *);
|
---|
[d3a92c87] | 152 |
|
---|
[a2aa81cb] | 153 | extern void mbr_free_label(mbr_label_t *);
|
---|
| 154 | extern void mbr_free_mbr(mbr_t *);
|
---|
| 155 | extern void mbr_free_partition(mbr_part_t *);
|
---|
| 156 | extern void mbr_free_partitions(mbr_partitions_t *);
|
---|
[d3a92c87] | 157 |
|
---|
| 158 | #endif
|
---|