chiark / gitweb /
[PATCH] udev_volume_id: version 39
[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 #include <asm/types.h>
36
37 #include "volume_id.h"
38 #include "logging.h"
39 #include "util.h"
40 #include "msdos.h"
41
42 struct msdos_partition_entry {
43         __u8    boot_ind;
44         __u8    head;
45         __u8    sector;
46         __u8    cyl;
47         __u8    sys_ind;
48         __u8    end_head;
49         __u8    end_sector;
50         __u8    end_cyl;
51         __u32   start_sect;
52         __u32   nr_sects;
53 } __attribute__((packed));
54
55 #define MSDOS_MAGIC                     "\x55\xaa"
56 #define MSDOS_PARTTABLE_OFFSET          0x1be
57 #define MSDOS_SIG_OFF                   0x1fe
58 #define BSIZE                           0x200
59 #define DOS_EXTENDED_PARTITION          0x05
60 #define LINUX_EXTENDED_PARTITION        0x85
61 #define WIN98_EXTENDED_PARTITION        0x0f
62 #define LINUX_RAID_PARTITION            0xfd
63 #define is_extended(type) \
64         (type == DOS_EXTENDED_PARTITION ||      \
65          type == WIN98_EXTENDED_PARTITION ||    \
66          type == LINUX_EXTENDED_PARTITION)
67 #define is_raid(type) \
68         (type == LINUX_RAID_PARTITION)
69
70 int volume_id_probe_msdos_part_table(struct volume_id *id, __u64 off)
71 {
72         const __u8 *buf;
73         int i;
74         __u64 poff;
75         __u64 plen;
76         __u64 extended = 0;
77         __u64 current;
78         __u64 next;
79         int limit;
80         int empty = 1;
81         struct msdos_partition_entry *part;
82         struct volume_id_partition *p;
83
84         dbg("probing at offset %llu", off);
85
86         buf = volume_id_get_buffer(id, off, 0x200);
87         if (buf == NULL)
88                 return -1;
89
90         if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
91                 return -1;
92
93         /* check flags on all entries for a valid partition table */
94         part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
95         for (i = 0; i < 4; i++) {
96                 if (part[i].boot_ind != 0 &&
97                     part[i].boot_ind != 0x80)
98                         return -1;
99
100                 if (le32_to_cpu(part[i].nr_sects) != 0)
101                         empty = 0;
102         }
103         if (empty == 1)
104                 return -1;
105
106         if (id->partitions != NULL)
107                 free(id->partitions);
108         id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
109                                 sizeof(struct volume_id_partition));
110         if (id->partitions == NULL)
111                 return -1;
112         memset(id->partitions, 0x00,
113                VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
114
115         for (i = 0; i < 4; i++) {
116                 poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
117                 plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
118
119                 if (plen == 0)
120                         continue;
121
122                 p = &id->partitions[i];
123
124                 p->partition_type_raw = part[i].sys_ind;
125
126                 if (is_extended(part[i].sys_ind)) {
127                         dbg("found extended partition at 0x%llx", poff);
128                         volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
129                         p->type = "msdos_extended_partition";
130                         if (extended == 0)
131                                 extended = off + poff;
132                 } else {
133                         dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
134                             part[i].sys_ind, poff, plen);
135
136                         if (is_raid(part[i].sys_ind))
137                                 volume_id_set_usage_part(p, VOLUME_ID_RAID);
138                         else
139                                 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
140                 }
141
142                 p->off = off + poff;
143                 p->len = plen;
144                 id->partition_count = i+1;
145         }
146
147         next = extended;
148         current = extended;
149         limit = 50;
150
151         /* follow extended partition chain and add data partitions */
152         while (next != 0) {
153                 if (limit-- == 0) {
154                         dbg("extended chain limit reached");
155                         break;
156                 }
157
158                 buf = volume_id_get_buffer(id, current, 0x200);
159                 if (buf == NULL)
160                         break;
161
162                 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
163
164                 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
165                         break;
166
167                 next = 0;
168
169                 for (i = 0; i < 4; i++) {
170                         poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
171                         plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
172
173                         if (plen == 0)
174                                 continue;
175
176                         if (is_extended(part[i].sys_ind)) {
177                                 dbg("found extended partition at 0x%llx", poff);
178                                 if (next == 0)
179                                         next = extended + poff;
180                         } else {
181                                 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
182                                         part[i].sys_ind, poff, plen);
183
184                                 /* we always start at the 5th entry */
185                                 while (id->partition_count < 4)
186                                         volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
187
188                                 p = &id->partitions[id->partition_count];
189
190                                 if (is_raid(part[i].sys_ind))
191                                         volume_id_set_usage_part(p, VOLUME_ID_RAID);
192                                 else
193                                         volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
194
195                                 p->off = current + poff;
196                                 p->len = plen;
197                                 id->partition_count++;
198
199                                 p->partition_type_raw = part[i].sys_ind;
200
201                                 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
202                                         dbg("too many partitions");
203                                         next = 0;
204                                 }
205                         }
206                 }
207
208                 current = next;
209         }
210
211         volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
212         id->type = "msdos_partition_table";
213
214         return 0;
215 }