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