source: mainline/uspace/lib/mbr/libmbr.h@ 21799398

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 21799398 was 94c49fd3, checked in by Jiri Svoboda <jiri@…>, 11 years ago

libmdr needs to initialize new MBR block properly with zeros and boot signature.

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