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