| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
|
|---|
| 3 | * Copyright (c) 2009 Jiri Svoboda (for some definitions from uspace/srv/bd/part/mbr_part)
|
|---|
| 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 |
|
|---|
| 67 | enum {
|
|---|
| 68 | /** Non-bootable */
|
|---|
| 69 | B_INACTIVE = 0x00,
|
|---|
| 70 | /** Bootable */
|
|---|
| 71 | B_ACTIVE = 0x80,
|
|---|
| 72 | /** Anything else means invalid */
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | typedef enum {
|
|---|
| 76 | /** Bootability */
|
|---|
| 77 | ST_BOOT = 0,
|
|---|
| 78 | /** Logical partition, 0 = primary, 1 = logical*/
|
|---|
| 79 | ST_LOGIC = 1
|
|---|
| 80 | } MBR_FLAGS;
|
|---|
| 81 |
|
|---|
| 82 | enum {
|
|---|
| 83 | /** Unused partition entry */
|
|---|
| 84 | PT_UNUSED = 0x00,
|
|---|
| 85 | /** Extended partition */
|
|---|
| 86 | PT_EXTENDED = 0x05,
|
|---|
| 87 | /** Extended partition with LBA */
|
|---|
| 88 | PT_EXTENDED_LBA = 0x0F,
|
|---|
| 89 | /** GPT Protective partition */
|
|---|
| 90 | PT_GPT = 0xEE,
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | typedef enum {
|
|---|
| 94 | /** No error */
|
|---|
| 95 | ERR_OK = 0,
|
|---|
| 96 | /** All primary partitions already present */
|
|---|
| 97 | ERR_PRIMARY_FULL,
|
|---|
| 98 | /** Extended partition already present */
|
|---|
| 99 | ERR_EXTENDED_PRESENT,
|
|---|
| 100 | /** No extended partition present */
|
|---|
| 101 | ERR_NO_EXTENDED,
|
|---|
| 102 | /** Partition overlapping */
|
|---|
| 103 | ERR_OVERLAP,
|
|---|
| 104 | /** Logical partition out of bounds */
|
|---|
| 105 | ERR_OUT_BOUNDS,
|
|---|
| 106 | /** No space left for EBR */
|
|---|
| 107 | ERR_NO_EBR,
|
|---|
| 108 | /** Out of memory */
|
|---|
| 109 | ERR_NOMEM,
|
|---|
| 110 | } MBR_ERR_VAL;
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | /** Structure of a partition table entry */
|
|---|
| 114 | typedef struct {
|
|---|
| 115 | uint8_t status;
|
|---|
| 116 | /** CHS of fist block in partition */
|
|---|
| 117 | uint8_t first_chs[3];
|
|---|
| 118 | /** Partition type */
|
|---|
| 119 | uint8_t ptype;
|
|---|
| 120 | /** CHS of last block in partition */
|
|---|
| 121 | uint8_t last_chs[3];
|
|---|
| 122 | /** LBA of first block in partition */
|
|---|
| 123 | uint32_t first_lba;
|
|---|
| 124 | /** Number of blocks in partition */
|
|---|
| 125 | uint32_t length;
|
|---|
| 126 | } __attribute__((packed)) pt_entry_t;
|
|---|
| 127 |
|
|---|
| 128 | /** Structure of a boot-record block */
|
|---|
| 129 | typedef struct {
|
|---|
| 130 | /** Area for boot code */
|
|---|
| 131 | uint8_t code_area[440];
|
|---|
| 132 | /** Optional media ID */
|
|---|
| 133 | uint32_t media_id;
|
|---|
| 134 | /** Padding */
|
|---|
| 135 | uint16_t pad0;
|
|---|
| 136 | /** Partition table entries */
|
|---|
| 137 | pt_entry_t pte[N_PRIMARY];
|
|---|
| 138 | /** Boot record block signature (@c BR_SIGNATURE) */
|
|---|
| 139 | uint16_t signature;
|
|---|
| 140 | } __attribute__((packed)) br_block_t;
|
|---|
| 141 |
|
|---|
| 142 | /** MBR header */
|
|---|
| 143 | typedef struct {
|
|---|
| 144 | /** Raw access to data */
|
|---|
| 145 | br_block_t raw_data;
|
|---|
| 146 | /** Device where the data are from */
|
|---|
| 147 | service_id_t device;
|
|---|
| 148 | } mbr_t;
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /** Partition */
|
|---|
| 152 | typedef struct mbr_part {
|
|---|
| 153 | /** The link in the doubly-linked list */
|
|---|
| 154 | link_t link;
|
|---|
| 155 | /** Partition type */
|
|---|
| 156 | uint8_t type;
|
|---|
| 157 | /** Flags */
|
|---|
| 158 | uint8_t status;
|
|---|
| 159 | /** Address of first block */
|
|---|
| 160 | uint32_t start_addr;
|
|---|
| 161 | /** Number of blocks */
|
|---|
| 162 | uint32_t length;
|
|---|
| 163 | /** Points to Extended Boot Record of logical partition */
|
|---|
| 164 | br_block_t * ebr;
|
|---|
| 165 | /** EBR address */
|
|---|
| 166 | uint32_t ebr_addr;
|
|---|
| 167 | } mbr_part_t;
|
|---|
| 168 |
|
|---|
| 169 | /** Partition list structure */
|
|---|
| 170 | typedef struct mbr_parts {
|
|---|
| 171 | /** Number of primary partitions */
|
|---|
| 172 | unsigned char n_primary;
|
|---|
| 173 | /** Index to the extended partition in the array */
|
|---|
| 174 | link_t * l_extended;
|
|---|
| 175 | /** Number of logical partitions */
|
|---|
| 176 | unsigned int n_logical;
|
|---|
| 177 | /** Logical partition linked list */
|
|---|
| 178 | list_t list;
|
|---|
| 179 | } mbr_partitions_t;
|
|---|
| 180 |
|
|---|
| 181 | /** Both header and partition list */
|
|---|
| 182 | typedef struct mbr_table {
|
|---|
| 183 | mbr_t * mbr;
|
|---|
| 184 | mbr_partitions_t * parts;
|
|---|
| 185 | } mbr_table_t;
|
|---|
| 186 |
|
|---|
| 187 | /** Read/Write MBR header.
|
|---|
| 188 | * WARNING: when changing both header and partitions, write first header,
|
|---|
| 189 | * then partitions. The MBR headers' raw_data is NOT updated to follow
|
|---|
| 190 | * partition changes. */
|
|---|
| 191 | extern mbr_t * mbr_alloc_mbr(void);
|
|---|
| 192 | extern mbr_t * mbr_read_mbr(service_id_t dev_handle);
|
|---|
| 193 | extern int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle);
|
|---|
| 194 | extern int mbr_is_mbr(mbr_t * mbr);
|
|---|
| 195 |
|
|---|
| 196 | /** Read/Write/Set MBR partitions.
|
|---|
| 197 | * NOTE: Writing partitions writes the complete header as well. */
|
|---|
| 198 | extern mbr_partitions_t * mbr_read_partitions(mbr_t * mbr);
|
|---|
| 199 | extern int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle);
|
|---|
| 200 | extern mbr_part_t * mbr_alloc_partition(void);
|
|---|
| 201 | extern mbr_partitions_t * mbr_alloc_partitions(void);
|
|---|
| 202 | extern MBR_ERR_VAL mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition);
|
|---|
| 203 | extern int mbr_remove_partition(mbr_partitions_t * parts, size_t idx);
|
|---|
| 204 | extern int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag);
|
|---|
| 205 | extern void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value);
|
|---|
| 206 | extern uint32_t mbr_get_next_aligned(uint32_t addr, unsigned int alignment);
|
|---|
| 207 |
|
|---|
| 208 | #define mbr_part_foreach(parts, iterator) \
|
|---|
| 209 | for (iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \
|
|---|
| 210 | iterator != list_get_instance(&((parts)->list.head), mbr_part_t, link); \
|
|---|
| 211 | iterator = list_get_instance(iterator->link.next, mbr_part_t, link))
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 | /** free() wrapper functions. */
|
|---|
| 215 | extern void mbr_free_mbr(mbr_t * mbr);
|
|---|
| 216 | extern void mbr_free_partition(mbr_part_t * p);
|
|---|
| 217 | extern void mbr_free_partitions(mbr_partitions_t * parts);
|
|---|
| 218 |
|
|---|
| 219 | #endif
|
|---|
| 220 |
|
|---|