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