Update Red Hat Copyright Notices
[nbdkit.git] / filters / partition / partition-gpt.c
blob97f421829532bbdc7e738f8c8d3cfa85affa252d
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
40 #include <nbdkit-filter.h>
42 #include "byte-swapping.h"
43 #include "gpt.h"
45 #include "partition.h"
47 static void
48 get_gpt_header (uint8_t *sector,
49 uint32_t *nr_partition_entries,
50 uint32_t *size_partition_entry)
52 struct gpt_header *header = (struct gpt_header *) sector;
53 *nr_partition_entries = le32toh (header->nr_partition_entries);
54 *size_partition_entry = le32toh (header->size_partition_entry);
57 static void
58 get_gpt_partition (uint8_t *bytes,
59 uint8_t *partition_type_guid,
60 uint64_t *first_lba, uint64_t *last_lba)
62 struct gpt_entry *entry = (struct gpt_entry *) bytes;
63 memcpy (partition_type_guid, entry->partition_type_guid, 16);
64 *first_lba = le64toh (entry->first_lba);
65 *last_lba = le64toh (entry->last_lba);
68 int
69 find_gpt_partition (nbdkit_next *next,
70 int64_t size, uint8_t *header_bytes,
71 int64_t *offset_r, int64_t *range_r)
73 uint8_t partition_bytes[128];
74 uint32_t nr_partition_entries, size_partition_entry;
75 uint8_t partition_type_guid[16];
76 uint64_t first_lba, last_lba;
77 int i;
78 int err;
80 get_gpt_header (header_bytes, &nr_partition_entries, &size_partition_entry);
81 if (partnum > nr_partition_entries) {
82 nbdkit_error ("GPT partition number out of range");
83 return -1;
86 if (size_partition_entry < 128) {
87 nbdkit_error ("GPT partition entry size is < 128 bytes");
88 return -1;
91 /* Check the disk is large enough to contain the partition table
92 * array (twice) plus other GPT overheads. Otherwise it is likely
93 * that the GPT header is bogus.
95 if (size < INT64_C (3) * SECTOR_SIZE +
96 INT64_C (2) * nr_partition_entries * size_partition_entry) {
97 nbdkit_error ("GPT partition table is too large for this disk");
98 return -1;
101 for (i = 0; i < nr_partition_entries; ++i) {
102 /* We already checked these are within bounds above. */
103 if (next->pread (next, partition_bytes, sizeof partition_bytes,
104 2 * SECTOR_SIZE + i * size_partition_entry, 0, &err) == -1)
105 return -1;
106 get_gpt_partition (partition_bytes,
107 partition_type_guid, &first_lba, &last_lba);
108 if (memcmp (partition_type_guid,
109 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) != 0 &&
110 partnum == i+1) {
111 *offset_r = first_lba * SECTOR_SIZE;
112 *range_r = (1 + last_lba - first_lba) * SECTOR_SIZE;
113 return 0;
117 nbdkit_error ("GPT partition %d not found", partnum);
118 return -1;