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

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

libmbr rewritten, alpha state

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