chiark / gitweb /
fix GGC signed pointer warnings and switch volume_id to stdint
[elogind.git] / extras / volume_id / volume_id / msdos.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "volume_id.h"
37 #include "logging.h"
38 #include "util.h"
39 #include "msdos.h"
40
41 struct msdos_partition_entry {
42         uint8_t         boot_ind;
43         uint8_t         head;
44         uint8_t         sector;
45         uint8_t         cyl;
46         uint8_t         sys_ind;
47         uint8_t         end_head;
48         uint8_t         end_sector;
49         uint8_t         end_cyl;
50         uint32_t        start_sect;
51         uint32_t        nr_sects;
52 } __attribute__((packed));
53
54 #define MSDOS_MAGIC                     "\x55\xaa"
55 #define MSDOS_PARTTABLE_OFFSET          0x1be
56 #define MSDOS_SIG_OFF                   0x1fe
57 #define BSIZE                           0x200
58 #define DOS_EXTENDED_PARTITION          0x05
59 #define LINUX_EXTENDED_PARTITION        0x85
60 #define WIN98_EXTENDED_PARTITION        0x0f
61 #define LINUX_RAID_PARTITION            0xfd
62 #define is_extended(type) \
63         (type == DOS_EXTENDED_PARTITION ||      \
64          type == WIN98_EXTENDED_PARTITION ||    \
65          type == LINUX_EXTENDED_PARTITION)
66 #define is_raid(type) \
67         (type == LINUX_RAID_PARTITION)
68
69 int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
70 {
71         const uint8_t *buf;
72         int i;
73         uint64_t poff;
74         uint64_t plen;
75         uint64_t extended = 0;
76         uint64_t current;
77         uint64_t next;
78         int limit;
79         int empty = 1;
80         struct msdos_partition_entry *part;
81         struct volume_id_partition *p;
82
83         dbg("probing at offset 0x%llx", (unsigned long long) off);
84
85         buf = volume_id_get_buffer(id, off, 0x200);
86         if (buf == NULL)
87                 return -1;
88
89         if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
90                 return -1;
91
92         /* check flags on all entries for a valid partition table */
93         part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
94         for (i = 0; i < 4; i++) {
95                 if (part[i].boot_ind != 0 &&
96                     part[i].boot_ind != 0x80)
97                         return -1;
98
99                 if (le32_to_cpu(part[i].nr_sects) != 0)
100                         empty = 0;
101         }
102         if (empty == 1)
103                 return -1;
104
105         if (id->partitions != NULL)
106                 free(id->partitions);
107         id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
108                                 sizeof(struct volume_id_partition));
109         if (id->partitions == NULL)
110                 return -1;
111         memset(id->partitions, 0x00,
112                VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
113
114         for (i = 0; i < 4; i++) {
115                 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
116                 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
117
118                 if (plen == 0)
119                         continue;
120
121                 p = &id->partitions[i];
122
123                 p->partition_type_raw = part[i].sys_ind;
124
125                 if (is_extended(part[i].sys_ind)) {
126                         dbg("found extended partition at 0x%llx", (unsigned long long) poff);
127                         volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
128                         p->type = "msdos_extended_partition";
129                         if (extended == 0)
130                                 extended = off + poff;
131                 } else {
132                         dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
133                             part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
134
135                         if (is_raid(part[i].sys_ind))
136                                 volume_id_set_usage_part(p, VOLUME_ID_RAID);
137                         else
138                                 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
139                 }
140
141                 p->off = off + poff;
142                 p->len = plen;
143                 id->partition_count = i+1;
144         }
145
146         next = extended;
147         current = extended;
148         limit = 50;
149
150         /* follow extended partition chain and add data partitions */
151         while (next != 0) {
152                 if (limit-- == 0) {
153                         dbg("extended chain limit reached");
154                         break;
155                 }
156
157                 buf = volume_id_get_buffer(id, current, 0x200);
158                 if (buf == NULL)
159                         break;
160
161                 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
162
163                 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
164                         break;
165
166                 next = 0;
167
168                 for (i = 0; i < 4; i++) {
169                         poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
170                         plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
171
172                         if (plen == 0)
173                                 continue;
174
175                         if (is_extended(part[i].sys_ind)) {
176                                 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
177                                 if (next == 0)
178                                         next = extended + poff;
179                         } else {
180                                 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
181                                         part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
182
183                                 /* we always start at the 5th entry */
184                                 while (id->partition_count < 4)
185                                         volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
186
187                                 p = &id->partitions[id->partition_count];
188
189                                 if (is_raid(part[i].sys_ind))
190                                         volume_id_set_usage_part(p, VOLUME_ID_RAID);
191                                 else
192                                         volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
193
194                                 p->off = current + poff;
195                                 p->len = plen;
196                                 id->partition_count++;
197
198                                 p->partition_type_raw = part[i].sys_ind;
199
200                                 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
201                                         dbg("too many partitions");
202                                         next = 0;
203                                 }
204                         }
205                 }
206
207                 current = next;
208         }
209
210         volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
211         id->type = "msdos_partition_table";
212
213         return 0;
214 }