source: mainline/uspace/lib/mbr/libmbr.h@ 6e8e4e19

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6e8e4e19 was 6e8e4e19, checked in by Dominik Taborsky (AT DOT) <brembyseznamcz>, 13 years ago

libmbr & hdisk changes, fixes, updates

  • Property mode set to 100644
File size: 6.8 KB
Line 
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
41#define LIBMBR_NAME "libmbr"
42
43#ifdef DEBUG_CONFIG
44#include <stdio.h>
45#include <str_error.h>
46#define DEBUG_PRINT_0(str) \
47 printf("%s:%d: " str, __FILE__, __LINE__)
48#define DEBUG_PRINT_1(str, arg1) \
49 printf("%s:%d: " str, __FILE__, __LINE__, arg1)
50#define DEBUG_PRINT_2(str, arg1, arg2) \
51 printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2)
52#define DEBUG_PRINT_3(str, arg1, arg2, arg3) \
53 printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2, arg3)
54#else
55#define DEBUG_PRINT_0(str)
56#define DEBUG_PRINT_1(str, arg1)
57#define DEBUG_PRINT_2(str, arg1, arg2)
58#define DEBUG_PRINT_3(str, arg1, arg2, arg3)
59#endif
60
61/** Number of primary partition records */
62#define N_PRIMARY 4
63
64/** Boot record signature */
65#define BR_SIGNATURE 0xAA55
66
67enum {
68 /** Non-bootable */
69 B_INACTIVE = 0x00,
70 /** Bootable */
71 B_ACTIVE = 0x80,
72 /** Anything else means invalid */
73};
74
75typedef enum {
76 /** Other flags unknown - saving previous state */
77 /** Bootability */
78 ST_BOOT = 7,
79 /** Logical partition, 0 = primary, 1 = logical*/
80 ST_LOGIC = 8
81} MBR_FLAGS;
82
83enum {
84 /** Unused partition entry */
85 PT_UNUSED = 0x00,
86 /** Extended partition */
87 PT_EXTENDED = 0x05,
88 /** Extended partition with LBA */
89 PT_EXTENDED_LBA = 0x0F,
90 /** GPT Protective partition */
91 PT_GPT = 0xEE,
92};
93
94typedef enum {
95 /** No error */
96 ERR_OK = 0,
97 /** All primary partitions already present */
98 ERR_PRIMARY_FULL,
99 /** Extended partition already present */
100 ERR_EXTENDED_PRESENT,
101 /** No extended partition present */
102 ERR_NO_EXTENDED,
103 /** Partition overlapping */
104 ERR_OVERLAP,
105 /** Partition out of bounds */
106 ERR_OUT_BOUNDS,
107 /** No space left for EBR */
108 ERR_NO_EBR,
109 /** Out of memory */
110 ERR_NOMEM,
111 /** Libblock error */
112 ERR_LIBBLOCK,
113} mbr_err_val;
114
115
116/** Structure of a partition table entry */
117typedef struct {
118 uint8_t status;
119 /** CHS of fist block in partition */
120 uint8_t first_chs[3];
121 /** Partition type */
122 uint8_t ptype;
123 /** CHS of last block in partition */
124 uint8_t last_chs[3];
125 /** LBA of first block in partition */
126 uint32_t first_lba;
127 /** Number of blocks in partition */
128 uint32_t length;
129} __attribute__((packed)) pt_entry_t;
130
131/** Structure of a boot-record block */
132typedef struct {
133 /** Area for boot code */
134 uint8_t code_area[440];
135 /** Optional media ID */
136 uint32_t media_id;
137 /** Padding */
138 uint16_t pad0;
139 /** Partition table entries */
140 pt_entry_t pte[N_PRIMARY];
141 /** Boot record block signature (@c BR_SIGNATURE) */
142 uint16_t signature;
143} __attribute__((packed)) br_block_t;
144
145/** MBR header */
146typedef struct {
147 /** Raw access to data */
148 br_block_t raw_data;
149} mbr_t;
150
151
152/** Partition */
153typedef struct mbr_part {
154 /** The link in the doubly-linked list */
155 link_t link;
156 /** Partition type */
157 uint8_t type;
158 /** Flags */
159 uint16_t status;
160 /** Address of first block */
161 uint32_t start_addr;
162 /** Number of blocks */
163 uint32_t length;
164 /** Points to Extended Boot Record of logical partition */
165 br_block_t * ebr;
166 /** EBR address */
167 uint32_t ebr_addr;
168} mbr_part_t;
169
170/** Partition list structure */
171typedef struct mbr_parts {
172 /** Number of primary partitions */
173 unsigned char n_primary;
174 /** Index to the extended partition in the array */
175 link_t * l_extended;
176 /** Number of logical partitions */
177 unsigned int n_logical;
178 /** Logical partition linked list */
179 list_t list;
180} mbr_partitions_t;
181
182/** Both header and partition list */
183typedef struct mbr_label {
184 /** MBR header */
185 mbr_t * mbr;
186 /** Partition list */
187 mbr_partitions_t * parts;
188 /** Device where the data are from (or for) */
189 service_id_t device;
190} mbr_label_t;
191
192/* Alloc complete label structure */
193extern mbr_label_t * mbr_alloc_label(void);
194
195/* Read/Write MBR header.
196 * WARNING: when changing both header and partitions, write first header,
197 * then partitions. The MBR headers' raw_data is NOT updated to follow
198 * partition changes. */
199extern mbr_t * mbr_alloc_mbr(void);
200extern int mbr_read_mbr(mbr_label_t *, service_id_t);
201extern int mbr_write_mbr(mbr_label_t *, service_id_t);
202extern int mbr_is_mbr(mbr_label_t *);
203
204/* Read/Write/Set MBR partitions.
205 * NOTE: Writing partitions writes the complete header as well. */
206extern int mbr_read_partitions(mbr_label_t *);
207extern int mbr_write_partitions(mbr_label_t *, service_id_t);
208extern mbr_part_t * mbr_alloc_partition(void);
209extern mbr_partitions_t * mbr_alloc_partitions(void);
210extern mbr_err_val mbr_add_partition(mbr_label_t *, mbr_part_t *);
211extern int mbr_remove_partition(mbr_label_t *, size_t);
212extern int mbr_get_flag(mbr_part_t *, MBR_FLAGS);
213extern void mbr_set_flag(mbr_part_t *, MBR_FLAGS, bool);
214extern uint32_t mbr_get_next_aligned(uint32_t, unsigned int);
215extern list_t * mbr_get_list(mbr_label_t *);
216extern mbr_part_t * mbr_get_first_partition(mbr_label_t *);
217extern mbr_part_t * mbr_get_next_partition(mbr_label_t *, mbr_part_t *);
218
219#define mbr_part_foreach(label, iterator) \
220 for (iterator = list_get_instance((label)->parts->list.head.next, mbr_part_t, link); \
221 iterator != list_get_instance(&((label)->parts->list.head), mbr_part_t, link); \
222 iterator = list_get_instance(iterator->link.next, mbr_part_t, link))
223
224
225/* free() wrapper functions. */
226extern void mbr_free_label(mbr_label_t *);
227extern void mbr_free_mbr(mbr_t *);
228extern void mbr_free_partition(mbr_part_t *);
229extern void mbr_free_partitions(mbr_partitions_t *);
230
231#endif
232
Note: See TracBrowser for help on using the repository browser.