blob: baa14c552989f09b293e1b5886142ee008f7245f [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/*
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 */
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020020//config:### config FEATURE_VOLUMEID_MSDOS
21//config:### bool "msdos filesystem"
22//config:### default y
23//config:### depends on VOLUMEID
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020024
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020025//kbuild:### lib-$(CONFIG_FEATURE_VOLUMEID_MSDOS) += msdos.o
26
Denis Vlasenkode7684a2008-02-18 21:08:49 +000027#include "volume_id_internal.h"
28
29struct msdos_partition_entry {
30 uint8_t boot_ind;
31 uint8_t head;
32 uint8_t sector;
33 uint8_t cyl;
34 uint8_t sys_ind;
35 uint8_t end_head;
36 uint8_t end_sector;
37 uint8_t end_cyl;
38 uint32_t start_sect;
39 uint32_t nr_sects;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020040} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041
Denis Vlasenkode7684a2008-02-18 21:08:49 +000042#define MSDOS_PARTTABLE_OFFSET 0x1be
43#define MSDOS_SIG_OFF 0x1fe
44#define BSIZE 0x200
45#define DOS_EXTENDED_PARTITION 0x05
46#define LINUX_EXTENDED_PARTITION 0x85
47#define WIN98_EXTENDED_PARTITION 0x0f
48#define LINUX_RAID_PARTITION 0xfd
49#define is_extended(type) \
50 (type == DOS_EXTENDED_PARTITION || \
51 type == WIN98_EXTENDED_PARTITION || \
52 type == LINUX_EXTENDED_PARTITION)
53#define is_raid(type) \
54 (type == LINUX_RAID_PARTITION)
55
Denys Vlasenko89300962009-11-01 23:07:18 +010056int FAST_FUNC volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000057{
58 const uint8_t *buf;
59 int i;
60 uint64_t poff;
61 uint64_t plen;
62 uint64_t extended = 0;
63 uint64_t current;
64 uint64_t next;
65 int limit;
66 int empty = 1;
67 struct msdos_partition_entry *part;
68 struct volume_id_partition *p;
69
70 dbg("probing at offset 0x%llx", (unsigned long long) off);
71
72 buf = volume_id_get_buffer(id, off, 0x200);
73 if (buf == NULL)
74 return -1;
75
Denis Vlasenkod25c33f2008-03-17 09:25:05 +000076 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000077 return -1;
78
79 /* check flags on all entries for a valid partition table */
80 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
81 for (i = 0; i < 4; i++) {
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010082 if (part[i].boot_ind != 0
83 && part[i].boot_ind != 0x80
84 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000085 return -1;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010086 }
Denis Vlasenkode7684a2008-02-18 21:08:49 +000087
88 if (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);
Denis Vlasenkod25c33f2008-03-17 09:25:05 +000096 id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX *
Denis Vlasenkode7684a2008-02-18 21:08:49 +000097 sizeof(struct volume_id_partition));
Denis Vlasenkode7684a2008-02-18 21:08:49 +000098
99 for (i = 0; i < 4; i++) {
100 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
101 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
102
103 if (plen == 0)
104 continue;
105
106 p = &id->partitions[i];
107
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000108// p->pt_type_raw = part[i].sys_ind;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000109
110 if (is_extended(part[i].sys_ind)) {
111 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000112// volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
113// p->type = "msdos_extended_partition";
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000114 if (extended == 0)
115 extended = off + poff;
116 } else {
117 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100118 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000119
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000120// if (is_raid(part[i].sys_ind))
121// volume_id_set_usage_part(p, VOLUME_ID_RAID);
122// else
123// volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000124 }
125
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000126// p->pt_off = off + poff;
127// p->pt_len = plen;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000128 id->partition_count = i+1;
129 }
130
131 next = extended;
132 current = extended;
133 limit = 50;
134
135 /* follow extended partition chain and add data partitions */
136 while (next != 0) {
137 if (limit-- == 0) {
138 dbg("extended chain limit reached");
139 break;
140 }
141
142 buf = volume_id_get_buffer(id, current, 0x200);
143 if (buf == NULL)
144 break;
145
146 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
147
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000148 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000149 break;
150
151 next = 0;
152
153 for (i = 0; i < 4; i++) {
154 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
155 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
156
157 if (plen == 0)
158 continue;
159
160 if (is_extended(part[i].sys_ind)) {
161 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
162 if (next == 0)
163 next = extended + poff;
164 } else {
165 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
166 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
167
168 /* we always start at the 5th entry */
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000169// while (id->partition_count < 4)
170// volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
171 if (id->partition_count < 4)
172 id->partition_count = 4;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000173
Denys Vlasenko9b2a9f02013-11-29 16:43:33 +0100174// p = &id->partitions[id->partition_count];
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000175
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000176// if (is_raid(part[i].sys_ind))
177// volume_id_set_usage_part(p, VOLUME_ID_RAID);
178// else
179// volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000180
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000181// p->pt_off = current + poff;
182// p->pt_len = plen;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000183 id->partition_count++;
184
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000185// p->pt_type_raw = part[i].sys_ind;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000186
187 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
188 dbg("too many partitions");
189 next = 0;
190 }
191 }
192 }
193
194 current = next;
195 }
196
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000197// volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
198// id->type = "msdos_partition_table";
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000199
200 return 0;
201}