| 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 |
|
|---|
| 46 | enum {
|
|---|
| 47 | /** Non-bootable */
|
|---|
| 48 | B_INACTIVE = 0x00,
|
|---|
| 49 | /** Bootable */
|
|---|
| 50 | B_ACTIVE = 0x80,
|
|---|
| 51 | /** Anything else means invalid */
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | typedef enum {
|
|---|
| 55 | /** Bootability */
|
|---|
| 56 | ST_BOOT = 0,
|
|---|
| 57 | /** Logical partition, 0 = primary, 1 = logical*/
|
|---|
| 58 | ST_LOGIC = 1
|
|---|
| 59 | } MBR_FLAGS;
|
|---|
| 60 |
|
|---|
| 61 | enum {
|
|---|
| 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 */
|
|---|
| 71 | typedef 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 */
|
|---|
| 86 | typedef 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 */
|
|---|
| 100 | typedef 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 */
|
|---|
| 112 | typedef 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 |
|
|---|
| 127 | typedef 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. */
|
|---|
| 143 | extern mbr_t * mbr_read_mbr(service_id_t dev_handle);
|
|---|
| 144 | extern int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle);
|
|---|
| 145 | extern int mbr_is_mbr(mbr_t * mbr);
|
|---|
| 146 |
|
|---|
| 147 | /** Read/Write/Set MBR partitions. */
|
|---|
| 148 | extern mbr_parts_t * mbr_read_partitions(mbr_t * mbr);
|
|---|
| 149 | extern int mbr_write_partitions(mbr_parts_t * parts, mbr_t * mbr, service_id_t dev_handle);
|
|---|
| 150 | extern part_t * mbr_alloc_partition(void);
|
|---|
| 151 | extern mbr_parts_t * mbr_alloc_partitions(void);
|
|---|
| 152 | extern void mbr_add_partition(mbr_parts_t * parts, part_t * partition);
|
|---|
| 153 | extern void mbr_remove_partition(mbr_parts_t * parts, int idx);
|
|---|
| 154 | extern int mbr_get_flag(part_t * p, MBR_FLAGS flag);
|
|---|
| 155 | extern 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. */
|
|---|
| 161 | extern void mbr_free_mbr(mbr_t * mbr);
|
|---|
| 162 | extern void mbr_free_partition(part_t * p);
|
|---|
| 163 | extern void mbr_free_partitions(mbr_parts_t * parts);
|
|---|
| 164 |
|
|---|
| 165 | #endif
|
|---|
| 166 |
|
|---|