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 | */ |
Sven-Göran Bergh | 15d0a86 | 2013-07-31 15:57:59 +0200 | [diff] [blame] | 20 | //config:### config FEATURE_VOLUMEID_MSDOS |
| 21 | //config:### bool "msdos filesystem" |
| 22 | //config:### default y |
| 23 | //config:### depends on VOLUMEID |
Sven-Göran Bergh | 15d0a86 | 2013-07-31 15:57:59 +0200 | [diff] [blame] | 24 | |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 25 | //kbuild:### lib-$(CONFIG_FEATURE_VOLUMEID_MSDOS) += msdos.o |
| 26 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 27 | #include "volume_id_internal.h" |
| 28 | |
| 29 | struct 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 Vlasenko | 8dc0e19 | 2009-09-16 00:58:11 +0200 | [diff] [blame] | 40 | } PACKED; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 42 | #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 Vlasenko | 8930096 | 2009-11-01 23:07:18 +0100 | [diff] [blame] | 56 | 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] | 57 | { |
| 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 76 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 77 | 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 Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 82 | if (part[i].boot_ind != 0 |
| 83 | && part[i].boot_ind != 0x80 |
| 84 | ) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 85 | return -1; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 86 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 87 | |
| 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 96 | id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX * |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 97 | sizeof(struct volume_id_partition)); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 98 | |
| 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 108 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 109 | |
| 110 | if (is_extended(part[i].sys_ind)) { |
| 111 | dbg("found extended partition at 0x%llx", (unsigned long long) poff); |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 112 | // volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE); |
| 113 | // p->type = "msdos_extended_partition"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 114 | if (extended == 0) |
| 115 | extended = off + poff; |
| 116 | } else { |
| 117 | dbg("found 0x%x data partition at 0x%llx, len 0x%llx", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 118 | part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 120 | // 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 Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 126 | // p->pt_off = off + poff; |
| 127 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 128 | 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 148 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 149 | 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 169 | // 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 Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 173 | |
Denys Vlasenko | 9b2a9f0 | 2013-11-29 16:43:33 +0100 | [diff] [blame] | 174 | // p = &id->partitions[id->partition_count]; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 176 | // 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 Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 181 | // p->pt_off = current + poff; |
| 182 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 183 | id->partition_count++; |
| 184 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 185 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 186 | |
| 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 Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 197 | // volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE); |
| 198 | // id->type = "msdos_partition_table"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |