Contiki-Inga 3.x
mbr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Institute of Operating Systems and Computer Networks (TU Brunswick).
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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Author: Christoph Peltz <peltz@ibr.cs.tu-bs.de>
30  */
31 
32 /**
33  * \addtogroup Drivers
34  * @{
35  *
36  * \defgroup mbr_driver Master Boot Record Support
37  *
38  * <p></p>
39  * @{
40  *
41  */
42 
43 /**
44  * \file
45  * MBR Support definitions
46  * \author
47  * Christoph Peltz <peltz@ibr.cs.tu-bs.de>
48  */
49 
50 #ifndef _MBR_H_
51 #define _MBR_H_
52 
53 #include "diskio.h"
54 
55 #define MBR_SUCCESS DISKIO_SUCCESS
56 #define MBR_ERROR_DISKIO_ERROR 1
57 #define MBR_ERROR_NO_MBR_FOUND 2
58 #define MBR_ERROR_INVALID_PARTITION 3
59 #define MBR_ERROR_PARTITION_EXISTS 4
60 
61 #define MBR_PARTITION_TYPE_FAT32_LBA 0x0C
62 #define MBR_PARTITION_TYPE_FAT16_LBA 0x0E
63 
64 /**
65  * Represents a primary partition in the mbr.
66  */
68  /** 0x00 non-bootable, 0x80 bootable, other values indicate that it is invalid */
69  uint8_t status;
70  /**
71  * Saves the first sector of the partition in the old CHS format (cylinder, head, sector)
72  *
73  * First byte stores the head value.
74  * Second byte stores the sector in the positions 0 - 5 and the upper to bits of the cylinder (Bits 8 and 9)
75  * Third byte stores the lower 8 bits of the cylinder value (0 - 7)
76  */
77  uint8_t chs_first_sector[3];
78  /** The type of partition this is, like FAT16 or FAT32; type = 0 means it's unused*/
79  uint8_t type;
80  /** Stores the last sector of the partition in the old CHS format */
81  uint8_t chs_last_sector[3];
82  /** Stores the absolute address of the first sector of the partition */
83  uint32_t lba_first_sector;
84  /** Stores the number of sectors the partition is long */
85  uint32_t lba_num_sectors;
86 };
87 
88 /**
89  * Represents the MBR of a disk, without the code portion and the constant bytes
90  */
91 struct mbr { //ignores everything but primary partitions (saves 448 bytes)
92  /** The MBR supports max 4 Primary partitions.
93  * For the sake of simplicity Extended partitions are not implemented.
94  */
96 };
97 
98 /**
99  * Initializes a mbr structure.
100  *
101  * Should be called first on a new mbr structure.
102  * \param *mbr the mbr which should be initialized
103  * \param disk_size the size of the disk on which the mbr will reside
104  */
105 void mbr_init( struct mbr *mbr );
106 
107 /**
108  * Reads the MBR from the specified device.
109  *
110  * The MBR is 512 Bytes long. That is normally one block to be read.
111  * \param *from device from which the mbr is read
112  * \param *to whe mbr structure in which the data is parsed
113  * \return MBR_SUCCESS on success, MBR_ERROR_DISKIO_ERROR if there was a problem reading the block or MBR_ERROR_NO_MBR_FOUND when there is no MBR on this device.
114  */
115 int mbr_read( struct diskio_device_info *from, struct mbr *to );
116 
117 /**
118  * Write the MBR to the specified device.
119  *
120  * \param *from the mbr structure which should be written on the device
121  * \param *to the device pointer to which we will write the mbr
122  * \return MBR_SUCCESS on success, !0 on a diskio error which is returned, see diskio_write_block for more information
123  */
124 int mbr_write( struct mbr *from, struct diskio_device_info *to );
125 
126 /**
127  * Adds a Partition to the mbr-structure.
128  *
129  * \param *mbr The mbr-structure in which to insert the partition.
130  * \param part_num Number of the Partition which should be added.
131  * \param part_type Type of the partition.
132  * \param start LBA-style start of the partition.
133  * \param len LBA-style length of the partition.
134  * \return MBR_SUCCESS on success or MBR_ERROR_PARTITION_EXISTS.
135  */
136 int mbr_addPartition(struct mbr *mbr, uint8_t part_num, uint8_t part_type, uint32_t start, uint32_t len );
137 
138 /**
139  * Deletes a Partition from the mbr-structure.
140  *
141  * \param *mbr The mbr-structure which should be edited.
142  * \param part_num Number of the partition which should be removed.
143  * \return MBR_SUCCESS on success or MBR_ERROR_INVALID_PARTITION.
144  */
145 int mbr_delPartition(struct mbr *mbr, uint8_t part_num );
146 
147 /**
148  * Checks if the given Partition exists.
149  *
150  * \param part_num Number of the Partition which should be checked.
151  * \return 1 if there is a Partition with the given number, otherwise 0.
152  */
153 int mbr_hasPartition(struct mbr *mbr, uint8_t part_num );
154 
155 #endif