Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 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 | |
Sven-Göran Bergh | 3b45801 | 2013-07-31 15:45:20 +0200 | [diff] [blame] | 21 | //kbuild:### lib-$(CONFIG_FEATURE_VOLUMEID_MSDOS) += msdos.o |
| 22 | |
Sven-Göran Bergh | 15d0a86 | 2013-07-31 15:57:59 +0200 | [diff] [blame^] | 23 | //config: |
| 24 | //config:### config FEATURE_VOLUMEID_MSDOS |
| 25 | //config:### bool "msdos filesystem" |
| 26 | //config:### default y |
| 27 | //config:### depends on VOLUMEID |
| 28 | //config:### help |
| 29 | //config:### TODO |
| 30 | //config: |
| 31 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 32 | #include "volume_id_internal.h" |
| 33 | |
| 34 | struct msdos_partition_entry { |
| 35 | uint8_t boot_ind; |
| 36 | uint8_t head; |
| 37 | uint8_t sector; |
| 38 | uint8_t cyl; |
| 39 | uint8_t sys_ind; |
| 40 | uint8_t end_head; |
| 41 | uint8_t end_sector; |
| 42 | uint8_t end_cyl; |
| 43 | uint32_t start_sect; |
| 44 | uint32_t nr_sects; |
Denys Vlasenko | 8dc0e19 | 2009-09-16 00:58:11 +0200 | [diff] [blame] | 45 | } PACKED; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 47 | #define MSDOS_PARTTABLE_OFFSET 0x1be |
| 48 | #define MSDOS_SIG_OFF 0x1fe |
| 49 | #define BSIZE 0x200 |
| 50 | #define DOS_EXTENDED_PARTITION 0x05 |
| 51 | #define LINUX_EXTENDED_PARTITION 0x85 |
| 52 | #define WIN98_EXTENDED_PARTITION 0x0f |
| 53 | #define LINUX_RAID_PARTITION 0xfd |
| 54 | #define is_extended(type) \ |
| 55 | (type == DOS_EXTENDED_PARTITION || \ |
| 56 | type == WIN98_EXTENDED_PARTITION || \ |
| 57 | type == LINUX_EXTENDED_PARTITION) |
| 58 | #define is_raid(type) \ |
| 59 | (type == LINUX_RAID_PARTITION) |
| 60 | |
Denys Vlasenko | 8930096 | 2009-11-01 23:07:18 +0100 | [diff] [blame] | 61 | int FAST_FUNC volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 62 | { |
| 63 | const uint8_t *buf; |
| 64 | int i; |
| 65 | uint64_t poff; |
| 66 | uint64_t plen; |
| 67 | uint64_t extended = 0; |
| 68 | uint64_t current; |
| 69 | uint64_t next; |
| 70 | int limit; |
| 71 | int empty = 1; |
| 72 | struct msdos_partition_entry *part; |
| 73 | struct volume_id_partition *p; |
| 74 | |
| 75 | dbg("probing at offset 0x%llx", (unsigned long long) off); |
| 76 | |
| 77 | buf = volume_id_get_buffer(id, off, 0x200); |
| 78 | if (buf == NULL) |
| 79 | return -1; |
| 80 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 81 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | |
| 84 | /* check flags on all entries for a valid partition table */ |
| 85 | part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET]; |
| 86 | for (i = 0; i < 4; i++) { |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 87 | if (part[i].boot_ind != 0 |
| 88 | && part[i].boot_ind != 0x80 |
| 89 | ) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 90 | return -1; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 91 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 92 | |
| 93 | if (part[i].nr_sects != 0) |
| 94 | empty = 0; |
| 95 | } |
| 96 | if (empty == 1) |
| 97 | return -1; |
| 98 | |
| 99 | if (id->partitions != NULL) |
| 100 | free(id->partitions); |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 101 | id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX * |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 102 | sizeof(struct volume_id_partition)); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 113 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 114 | |
| 115 | if (is_extended(part[i].sys_ind)) { |
| 116 | dbg("found extended partition at 0x%llx", (unsigned long long) poff); |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 117 | // volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE); |
| 118 | // p->type = "msdos_extended_partition"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 119 | if (extended == 0) |
| 120 | extended = off + poff; |
| 121 | } else { |
| 122 | dbg("found 0x%x data partition at 0x%llx, len 0x%llx", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 123 | part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 131 | // p->pt_off = off + poff; |
| 132 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 153 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 174 | // while (id->partition_count < 4) |
| 175 | // volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED); |
| 176 | if (id->partition_count < 4) |
| 177 | id->partition_count = 4; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 178 | |
| 179 | p = &id->partitions[id->partition_count]; |
| 180 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 181 | // if (is_raid(part[i].sys_ind)) |
| 182 | // volume_id_set_usage_part(p, VOLUME_ID_RAID); |
| 183 | // else |
| 184 | // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 186 | // p->pt_off = current + poff; |
| 187 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 188 | id->partition_count++; |
| 189 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 190 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 191 | |
| 192 | if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) { |
| 193 | dbg("too many partitions"); |
| 194 | next = 0; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | current = next; |
| 200 | } |
| 201 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 202 | // volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE); |
| 203 | // id->type = "msdos_partition_table"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |