source: mainline/uspace/lib/mbr/libmbr.h@ 6453e306

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6453e306 was 6453e306, checked in by Martin Decky <martin@…>, 12 years ago

basic code review and coding style cleanup

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