chiark / gitweb /
volume_id: provide libvolume_id.a file
[elogind.git] / extras / volume_id / libvolume_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 program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29
30 struct msdos_partition_entry {
31         uint8_t         boot_ind;
32         uint8_t         head;
33         uint8_t         sector;
34         uint8_t         cyl;
35         uint8_t         sys_ind;
36         uint8_t         end_head;
37         uint8_t         end_sector;
38         uint8_t         end_cyl;
39         uint32_t        start_sect;
40         uint32_t        nr_sects;
41 } __attribute__((packed));
42
43 #define MSDOS_MAGIC                     "\x55\xaa"
44 #define MSDOS_PARTTABLE_OFFSET          0x1be
45 #define MSDOS_SIG_OFF                   0x1fe
46 #define BSIZE                           0x200
47 #define DOS_EXTENDED_PARTITION          0x05
48 #define LINUX_EXTENDED_PARTITION        0x85
49 #define WIN98_EXTENDED_PARTITION        0x0f
50 #define LINUX_RAID_PARTITION            0xfd
51 #define is_extended(type) \
52         (type == DOS_EXTENDED_PARTITION ||      \
53          type == WIN98_EXTENDED_PARTITION ||    \
54          type == LINUX_EXTENDED_PARTITION)
55 #define is_raid(type) \
56         (type == LINUX_RAID_PARTITION)
57
58 int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
59 {
60         const uint8_t *buf;
61         int i;
62         uint64_t poff;
63         uint64_t plen;
64         uint64_t extended = 0;
65         uint64_t current;
66         uint64_t next;
67         int limit;
68         int empty = 1;
69         struct msdos_partition_entry *part;
70         struct volume_id_partition *p;
71
72         dbg("probing at offset 0x%llx", (unsigned long long) off);
73
74         buf = volume_id_get_buffer(id, off, 0x200);
75         if (buf == NULL)
76                 return -1;
77
78         if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
79                 return -1;
80
81         /* check flags on all entries for a valid partition table */
82         part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
83         for (i = 0; i < 4; i++) {
84                 if (part[i].boot_ind != 0 &&
85                     part[i].boot_ind != 0x80)
86                         return -1;
87
88                 if (le32_to_cpu(part[i].nr_sects) != 0)
89                         empty = 0;
90         }
91         if (empty == 1)
92                 return -1;
93
94         if (id->partitions != NULL)
95                 free(id->partitions);
96         id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
97                                 sizeof(struct volume_id_partition));
98         if (id->partitions == NULL)
99                 return -1;
100         memset(id->partitions, 0x00,
101                VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
102
103         for (i = 0; i < 4; i++) {
104                 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
105                 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
106
107                 if (plen == 0)
108                         continue;
109
110                 p = &id->partitions[i];
111
112                 p->partition_type_raw = part[i].sys_ind;
113
114                 if (is_extended(part[i].sys_ind)) {
115                         dbg("found extended partition at 0x%llx", (unsigned long long) poff);
116                         volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
117                         p->type = "msdos_extended_partition";
118                         if (extended == 0)
119                                 extended = off + poff;
120                 } else {
121                         dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
122                             part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
123
124                         if (is_raid(part[i].sys_ind))
125                                 volume_id_set_usage_part(p, VOLUME_ID_RAID);
126                         else
127                                 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
128                 }
129
130                 p->off = off + poff;
131                 p->len = plen;
132                 id->partition_count = i+1;
133         }
134
135         next = extended;
136         current = extended;
137         limit = 50;
138
139         /* follow extended partition chain and add data partitions */
140         while (next != 0) {
141                 if (limit-- == 0) {
142                         dbg("extended chain limit reached");
143                         break;
144                 }
145
146                 buf = volume_id_get_buffer(id, current, 0x200);
147                 if (buf == NULL)
148                         break;
149
150                 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
151
152                 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
153                         break;
154
155                 next = 0;
156
157                 for (i = 0; i < 4; i++) {
158                         poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
159                         plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
160
161                         if (plen == 0)
162                                 continue;
163
164                         if (is_extended(part[i].sys_ind)) {
165                                 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
166                                 if (next == 0)
167                                         next = extended + poff;
168                         } else {
169                                 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
170                                         part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
171
172                                 /* we always start at the 5th entry */
173                                 while (id->partition_count < 4)
174                                         volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
175
176                                 p = &id->partitions[id->partition_count];
177
178                                 if (is_raid(part[i].sys_ind))
179                                         volume_id_set_usage_part(p, VOLUME_ID_RAID);
180                                 else
181                                         volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
182
183                                 p->off = current + poff;
184                                 p->len = plen;
185                                 id->partition_count++;
186
187                                 p->partition_type_raw = part[i].sys_ind;
188
189                                 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
190                                         dbg("too many partitions");
191                                         next = 0;
192                                 }
193                         }
194                 }
195
196                 current = next;
197         }
198
199         volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
200         id->type = "msdos_partition_table";
201
202         return 0;
203 }