1 | /*
|
---|
2 | * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libmbr
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef LIBMBR_LIBMBR_H_
|
---|
36 | #define LIBMBR_LIBMBR_H_
|
---|
37 |
|
---|
38 | #include <sys/types.h>
|
---|
39 |
|
---|
40 | #define LIBMBR_NAME "libmbr"
|
---|
41 |
|
---|
42 | #ifdef DEBUG_CONFIG
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #define DEBUG_PRINT_0(str) \
|
---|
46 | printf("%s:%d: " str, __FILE__, __LINE__)
|
---|
47 | #define DEBUG_PRINT_1(str, arg1) \
|
---|
48 | printf("%s:%d: " str, __FILE__, __LINE__, arg1)
|
---|
49 | #define DEBUG_PRINT_2(str, arg1, arg2) \
|
---|
50 | printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2)
|
---|
51 | #define DEBUG_PRINT_3(str, arg1, arg2, arg3) \
|
---|
52 | printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2, arg3)
|
---|
53 | #else
|
---|
54 | #define DEBUG_PRINT_0(str)
|
---|
55 | #define DEBUG_PRINT_1(str, arg1)
|
---|
56 | #define DEBUG_PRINT_2(str, arg1, arg2)
|
---|
57 | #define DEBUG_PRINT_3(str, arg1, arg2, arg3)
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | /** Number of primary partition records */
|
---|
61 | #define N_PRIMARY 4
|
---|
62 |
|
---|
63 | /** Boot record signature */
|
---|
64 | #define BR_SIGNATURE 0xAA55
|
---|
65 |
|
---|
66 | enum {
|
---|
67 | /** Non-bootable */
|
---|
68 | B_INACTIVE = 0x00,
|
---|
69 | /** Bootable */
|
---|
70 | B_ACTIVE = 0x80,
|
---|
71 | /** Anything else means invalid */
|
---|
72 | };
|
---|
73 |
|
---|
74 | typedef enum {
|
---|
75 | /** Bootability */
|
---|
76 | ST_BOOT = 0,
|
---|
77 | /** Logical partition, 0 = primary, 1 = logical*/
|
---|
78 | ST_LOGIC = 1
|
---|
79 | } MBR_FLAGS;
|
---|
80 |
|
---|
81 | enum {
|
---|
82 | /** Unused partition entry */
|
---|
83 | PT_UNUSED = 0x00,
|
---|
84 | /** Extended partition */
|
---|
85 | PT_EXTENDED = 0x05,
|
---|
86 | /** Extended partition with LBA */
|
---|
87 | PT_EXTENDED_LBA = 0x0F,
|
---|
88 | /** GPT Protective partition */
|
---|
89 | PT_GPT = 0xEE,
|
---|
90 | };
|
---|
91 |
|
---|
92 | typedef enum {
|
---|
93 | /** No error */
|
---|
94 | ERR_OK = 0,
|
---|
95 | /** All primary partitions already present */
|
---|
96 | ERR_PRIMARY_FULL,
|
---|
97 | /** Extended partition already present */
|
---|
98 | ERR_EXTENDED_PRESENT,
|
---|
99 | /** No extended partition present */
|
---|
100 | ERR_NO_EXTENDED,
|
---|
101 | /** Partition overlapping */
|
---|
102 | ERR_OVERLAP,
|
---|
103 | /** Logical partition out of bounds */
|
---|
104 | ERR_OUT_BOUNDS,
|
---|
105 | /** No space left for EBR */
|
---|
106 | ERR_NO_EBR,
|
---|
107 | /** Out of memory */
|
---|
108 | ERR_NOMEM,
|
---|
109 | } MBR_ERR_VAL;
|
---|
110 |
|
---|
111 |
|
---|
112 | /** Structure of a partition table entry */
|
---|
113 | typedef struct {
|
---|
114 | uint8_t status;
|
---|
115 | /** CHS of fist block in partition */
|
---|
116 | uint8_t first_chs[3];
|
---|
117 | /** Partition type */
|
---|
118 | uint8_t ptype;
|
---|
119 | /** CHS of last block in partition */
|
---|
120 | uint8_t last_chs[3];
|
---|
121 | /** LBA of first block in partition */
|
---|
122 | uint32_t first_lba;
|
---|
123 | /** Number of blocks in partition */
|
---|
124 | uint32_t length;
|
---|
125 | } __attribute__((packed)) pt_entry_t;
|
---|
126 |
|
---|
127 | /** Structure of a boot-record block */
|
---|
128 | typedef struct {
|
---|
129 | /** Area for boot code */
|
---|
130 | uint8_t code_area[440];
|
---|
131 | /** Optional media ID */
|
---|
132 | uint32_t media_id;
|
---|
133 | /** Padding */
|
---|
134 | uint16_t pad0;
|
---|
135 | /** Partition table entries */
|
---|
136 | pt_entry_t pte[N_PRIMARY];
|
---|
137 | /** Boot record block signature (@c BR_SIGNATURE) */
|
---|
138 | uint16_t signature;
|
---|
139 | } __attribute__((packed)) br_block_t;
|
---|
140 |
|
---|
141 | /** MBR header */
|
---|
142 | typedef struct {
|
---|
143 | /** Raw access to data */
|
---|
144 | br_block_t raw_data;
|
---|
145 | /** Device where the data are from */
|
---|
146 | service_id_t device;
|
---|
147 | } mbr_t;
|
---|
148 |
|
---|
149 |
|
---|
150 | /** Partition */
|
---|
151 | typedef struct mbr_part {
|
---|
152 | /** The link in the doubly-linked list */
|
---|
153 | link_t link;
|
---|
154 | /** Partition type */
|
---|
155 | uint8_t type;
|
---|
156 | /** Flags */
|
---|
157 | uint8_t status;
|
---|
158 | /** Address of first block */
|
---|
159 | uint32_t start_addr;
|
---|
160 | /** Number of blocks */
|
---|
161 | uint32_t length;
|
---|
162 | /** Points to Extended Boot Record of logical partition */
|
---|
163 | br_block_t * ebr;
|
---|
164 | /** EBR address */
|
---|
165 | uint32_t ebr_addr;
|
---|
166 | } mbr_part_t;
|
---|
167 |
|
---|
168 | /** Partition list structure */
|
---|
169 | typedef struct mbr_parts {
|
---|
170 | /** Number of primary partitions */
|
---|
171 | unsigned char n_primary;
|
---|
172 | /** Index to the extended partition in the array */
|
---|
173 | link_t * l_extended;
|
---|
174 | /** Number of logical partitions */
|
---|
175 | unsigned int n_logical;
|
---|
176 | /** Logical partition linked list */
|
---|
177 | list_t list;
|
---|
178 | } mbr_partitions_t;
|
---|
179 |
|
---|
180 | /** Both header and partition list */
|
---|
181 | typedef struct mbr_table {
|
---|
182 | mbr_t * mbr;
|
---|
183 | mbr_partitions_t * parts;
|
---|
184 | } mbr_table_t;
|
---|
185 |
|
---|
186 | /** Read/Write MBR header.
|
---|
187 | * WARNING: when changing both header and partitions, write first header,
|
---|
188 | * then partitions. The MBR headers' raw_data is NOT updated to follow
|
---|
189 | * partition changes. */
|
---|
190 | extern mbr_t * mbr_alloc_mbr();
|
---|
191 | extern mbr_t * mbr_read_mbr(service_id_t dev_handle);
|
---|
192 | extern int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle);
|
---|
193 | extern int mbr_is_mbr(mbr_t * mbr);
|
---|
194 |
|
---|
195 | /** Read/Write/Set MBR partitions.
|
---|
196 | * NOTE: Writing partitions writes the complete header as well. */
|
---|
197 | extern mbr_partitions_t * mbr_read_partitions(mbr_t * mbr);
|
---|
198 | extern int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle);
|
---|
199 | extern mbr_part_t * mbr_alloc_partition(void);
|
---|
200 | extern mbr_partitions_t * mbr_alloc_partitions(void);
|
---|
201 | extern MBR_ERR_VAL mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition);
|
---|
202 | extern int mbr_remove_partition(mbr_partitions_t * parts, size_t idx);
|
---|
203 | extern int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag);
|
---|
204 | extern void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value);
|
---|
205 | extern uint32_t mbr_get_next_aligned(uint32_t addr, unsigned int alignment);
|
---|
206 |
|
---|
207 | #define mbr_part_foreach(parts, iterator) \
|
---|
208 | for (iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \
|
---|
209 | iterator != list_get_instance(&((parts)->list.head), mbr_part_t, link); \
|
---|
210 | iterator = list_get_instance(iterator->link.next, mbr_part_t, link))
|
---|
211 |
|
---|
212 |
|
---|
213 | /** free() wrapper functions. */
|
---|
214 | extern void mbr_free_mbr(mbr_t * mbr);
|
---|
215 | extern void mbr_free_partition(mbr_part_t * p);
|
---|
216 | extern void mbr_free_partitions(mbr_partitions_t * parts);
|
---|
217 |
|
---|
218 | #endif
|
---|
219 |
|
---|