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 | /** Number of primary partition records */
|
---|
43 | #define N_PRIMARY 4
|
---|
44 |
|
---|
45 | /** Boot record signature */
|
---|
46 | #define BR_SIGNATURE 0xAA55
|
---|
47 |
|
---|
48 | enum {
|
---|
49 | /** Non-bootable */
|
---|
50 | B_INACTIVE = 0x00,
|
---|
51 | /** Bootable */
|
---|
52 | B_ACTIVE = 0x80,
|
---|
53 | /** Anything else means invalid */
|
---|
54 | };
|
---|
55 |
|
---|
56 | typedef enum {
|
---|
57 | /** Bootability */
|
---|
58 | ST_BOOT = 0,
|
---|
59 | /** Logical partition, 0 = primary, 1 = logical*/
|
---|
60 | ST_LOGIC = 1
|
---|
61 | } MBR_FLAGS;
|
---|
62 |
|
---|
63 | enum {
|
---|
64 | /** Unused partition entry */
|
---|
65 | PT_UNUSED = 0x00,
|
---|
66 | /** Extended partition */
|
---|
67 | PT_EXTENDED = 0x05,
|
---|
68 | /** GPT Protective partition */
|
---|
69 | PT_GPT = 0xEE,
|
---|
70 | };
|
---|
71 |
|
---|
72 | /** Structure of a partition table entry */
|
---|
73 | typedef struct {
|
---|
74 | uint8_t status;
|
---|
75 | /** CHS of fist block in partition */
|
---|
76 | uint8_t first_chs[3];
|
---|
77 | /** Partition type */
|
---|
78 | uint8_t ptype;
|
---|
79 | /** CHS of last block in partition */
|
---|
80 | uint8_t last_chs[3];
|
---|
81 | /** LBA of first block in partition */
|
---|
82 | uint32_t first_lba;
|
---|
83 | /** Number of blocks in partition */
|
---|
84 | uint32_t length;
|
---|
85 | } __attribute__((packed)) pt_entry_t;
|
---|
86 |
|
---|
87 | /** Structure of a boot-record block */
|
---|
88 | typedef struct {
|
---|
89 | /** Area for boot code */
|
---|
90 | uint8_t code_area[440];
|
---|
91 | /** Optional media ID */
|
---|
92 | uint32_t media_id;
|
---|
93 | /** Padding */
|
---|
94 | uint16_t pad0;
|
---|
95 | /** Partition table entries */
|
---|
96 | pt_entry_t pte[N_PRIMARY];
|
---|
97 | /** Boot record block signature (@c BR_SIGNATURE) */
|
---|
98 | uint16_t signature;
|
---|
99 | } __attribute__((packed)) br_block_t;
|
---|
100 |
|
---|
101 | /** MBR header */
|
---|
102 | typedef struct {
|
---|
103 | /** Raw access to data */
|
---|
104 | br_block_t raw_data;
|
---|
105 | /** Device where the data are from */
|
---|
106 | service_id_t device;
|
---|
107 | /** Pointer to partition list */
|
---|
108 | //list of partitions; //if we keep this in here, we should free() it in mbr_free_mbr()
|
---|
109 | } mbr_t;
|
---|
110 |
|
---|
111 |
|
---|
112 | //FIXME: make mbr_partitions_t as the linked list for keeping the same interface as with GPT
|
---|
113 | /** Partition */
|
---|
114 | typedef struct mbr_part {
|
---|
115 | /** The link in the doubly-linked list */
|
---|
116 | link_t link;
|
---|
117 | /** Partition type */
|
---|
118 | uint8_t type;
|
---|
119 | /** Flags */
|
---|
120 | uint8_t status;
|
---|
121 | /** Address of first block */
|
---|
122 | uint32_t start_addr;
|
---|
123 | /** Number of blocks */
|
---|
124 | uint32_t length;
|
---|
125 | /** Points to Extended Boot Record of logical partition */
|
---|
126 | br_block_t * ebr;
|
---|
127 | } mbr_part_t;
|
---|
128 |
|
---|
129 | typedef struct mbr_parts {
|
---|
130 | /** Number of primary partitions */
|
---|
131 | unsigned char n_primary;
|
---|
132 | /** Link to the extended partition in the list */
|
---|
133 | link_t * l_extended;
|
---|
134 | /** Number of logical partitions */
|
---|
135 | unsigned int n_logical;
|
---|
136 | /** Partition linked list */
|
---|
137 | list_t list;
|
---|
138 | } mbr_partitions_t;
|
---|
139 |
|
---|
140 | typedef struct mbr_table {
|
---|
141 | mbr_t * mbr;
|
---|
142 | mbr_partitions_t * parts;
|
---|
143 | } mbr_table_t;
|
---|
144 |
|
---|
145 | /** Read/Write MBR header.
|
---|
146 | * WARNING: when changing both header and partitions, write first header,
|
---|
147 | * then partitions. The MBR headers' raw_data is NOT updated to follow
|
---|
148 | * partition changes. */
|
---|
149 | extern mbr_t * mbr_read_mbr(service_id_t dev_handle);
|
---|
150 | extern int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle);
|
---|
151 | extern int mbr_is_mbr(mbr_t * mbr);
|
---|
152 |
|
---|
153 | /** Read/Write/Set MBR partitions. */
|
---|
154 | extern mbr_partitions_t * mbr_read_partitions(mbr_t * mbr);
|
---|
155 | extern int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle);
|
---|
156 | extern mbr_part_t * mbr_alloc_partition(void);
|
---|
157 | extern mbr_partitions_t * mbr_alloc_partitions(void);
|
---|
158 | extern int mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition);
|
---|
159 | extern int mbr_remove_partition(mbr_partitions_t * parts, size_t idx);
|
---|
160 | extern int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag);
|
---|
161 | extern void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value);
|
---|
162 |
|
---|
163 | #define mbr_part_foreach(parts, iterator) \
|
---|
164 | for (mbr_part_t * iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \
|
---|
165 | iterator != list_get_instance(&(parts)->list.head, mbr_part_t, link); \
|
---|
166 | iterator = list_get_instance(iterator->link.next, mbr_part_t, link))
|
---|
167 |
|
---|
168 |
|
---|
169 | /** free() wrapper functions. */
|
---|
170 | extern void mbr_free_mbr(mbr_t * mbr);
|
---|
171 | extern void mbr_free_partition(mbr_part_t * p);
|
---|
172 | extern void mbr_free_partitions(mbr_partitions_t * parts);
|
---|
173 |
|
---|
174 | #endif
|
---|
175 |
|
---|