4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * 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.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <nbdkit-filter.h>
44 #include "byte-swapping.h"
45 #include "isaligned.h"
47 #include "partition.h"
49 /* See also linux.git/block/partitions/msdos.c:is_extended_partition */
50 #define is_extended(byte) ((byte) == 0x5 || (byte) == 0xf || (byte) == 0x85)
52 struct mbr_partition
{
53 uint8_t part_type_byte
;
54 uint32_t start_sector
;
59 get_mbr_partition (uint8_t *sector
, int i
, struct mbr_partition
*part
)
61 int offset
= 0x1BE + i
*0x10;
63 part
->part_type_byte
= sector
[offset
+4];
64 memcpy (&part
->start_sector
, §or
[offset
+8], 4);
65 part
->start_sector
= le32toh (part
->start_sector
);
66 memcpy (&part
->nr_sectors
, §or
[offset
+0xC], 4);
67 part
->nr_sectors
= le32toh (part
->nr_sectors
);
71 find_mbr_partition (nbdkit_next
*next
,
72 int64_t size
, uint8_t *mbr
,
73 int64_t *offset_r
, int64_t *range_r
)
76 struct mbr_partition partition
;
77 uint32_t ep_start_sector
, ep_nr_sectors
;
78 uint64_t ebr
, next_ebr
;
79 uint8_t sector
[SECTOR_SIZE
];
81 /* Primary partition. */
83 for (i
= 0; i
< 4; ++i
) {
84 get_mbr_partition (mbr
, i
, &partition
);
85 if (partition
.nr_sectors
> 0 &&
86 partition
.part_type_byte
!= 0 &&
87 !is_extended (partition
.part_type_byte
) &&
89 *offset_r
= partition
.start_sector
* (int64_t) SECTOR_SIZE
;
90 *range_r
= partition
.nr_sectors
* (int64_t) SECTOR_SIZE
;
95 /* Not found falls through to error case at the end of the function. */
98 /* Logical partition. */
100 /* Find the extended partition in the primary partition table. */
101 for (i
= 0; i
< 4; ++i
) {
102 get_mbr_partition (mbr
, i
, &partition
);
103 if (partition
.nr_sectors
> 0 &&
104 is_extended (partition
.part_type_byte
)) {
108 nbdkit_error ("MBR logical partition selected, "
109 "but there is no extended partition in the partition table");
113 ep_start_sector
= partition
.start_sector
;
114 ep_nr_sectors
= partition
.nr_sectors
;
115 ebr
= ep_start_sector
* (uint64_t)SECTOR_SIZE
;
117 /* This loop will terminate eventually because we only accept
118 * links which strictly increase the EBR pointer. There are valid
119 * partition tables which do odd things like arranging the
120 * partitions in reverse order, but we will not accept them here.
123 /* Check that the ebr is aligned and pointing inside the disk
124 * and doesn't point to the MBR.
126 if (!IS_ALIGNED (ebr
, SECTOR_SIZE
) ||
127 ebr
< SECTOR_SIZE
|| ebr
>= size
-SECTOR_SIZE
) {
128 nbdkit_error ("invalid EBR chain: "
129 "next EBR boot sector is located outside disk boundary");
133 /* Read the EBR sector. */
134 nbdkit_debug ("partition: reading EBR at %" PRIi64
, ebr
);
135 if (next
->pread (next
, sector
, sizeof sector
, ebr
, 0, &errno
) == -1)
139 uint64_t offset
, range
;
141 /* First entry in EBR points to the logical partition. */
142 get_mbr_partition (sector
, 0, &partition
);
144 /* The first entry start sector is relative to the EBR. */
145 offset
= ebr
+ partition
.start_sector
* (uint64_t)SECTOR_SIZE
;
146 range
= partition
.nr_sectors
* (uint64_t)SECTOR_SIZE
;
148 /* Logical partition cannot be before the corresponding EBR,
149 * and it cannot extend beyond the enclosing extended
154 ((uint64_t)ep_start_sector
+ ep_nr_sectors
) * SECTOR_SIZE
) {
155 nbdkit_error ("logical partition start or size out of range "
156 "(offset=%" PRIu64
", range=%" PRIu64
", "
157 "ep:startsect=%" PRIu32
", ep:nrsects=%" PRIu32
")",
158 offset
, range
, ep_start_sector
, ep_nr_sectors
);
166 /* Second entry in EBR links to the next EBR. */
167 get_mbr_partition (sector
, 1, &partition
);
169 /* All zeroes means the end of the chain. */
170 if (partition
.start_sector
== 0 && partition
.nr_sectors
== 0)
173 /* The second entry start sector is relative to the start to the
174 * extended partition.
177 ((uint64_t)ep_start_sector
+ partition
.start_sector
) * SECTOR_SIZE
;
179 /* Make sure the next EBR > current EBR. */
180 if (next_ebr
<= ebr
) {
181 nbdkit_error ("invalid EBR chain: "
182 "next EBR %" PRIu64
" <= current EBR %" PRIu64
,
190 nbdkit_error ("MBR partition %d not found", partnum
);