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:### config FEATURE_VOLUMEID_MSDOS |
| 24 | //config:### bool "msdos filesystem" |
| 25 | //config:### default y |
| 26 | //config:### depends on VOLUMEID |
Sven-Göran Bergh | 15d0a86 | 2013-07-31 15:57:59 +0200 | [diff] [blame] | 27 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 28 | #include "volume_id_internal.h" |
| 29 | |
| 30 | struct msdos_partition_entry { |
| 31 | uint8_t boot_ind; |
| 32 | uint8_t head; |
| 33 | uint8_t sector; |
| 34 | uint8_t cyl; |
| 35 | uint8_t sys_ind; |
| 36 | uint8_t end_head; |
| 37 | uint8_t end_sector; |
| 38 | uint8_t end_cyl; |
| 39 | uint32_t start_sect; |
| 40 | uint32_t nr_sects; |
Denys Vlasenko | 8dc0e19 | 2009-09-16 00:58:11 +0200 | [diff] [blame] | 41 | } PACKED; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 43 | #define MSDOS_PARTTABLE_OFFSET 0x1be |
| 44 | #define MSDOS_SIG_OFF 0x1fe |
| 45 | #define BSIZE 0x200 |
| 46 | #define DOS_EXTENDED_PARTITION 0x05 |
| 47 | #define LINUX_EXTENDED_PARTITION 0x85 |
| 48 | #define WIN98_EXTENDED_PARTITION 0x0f |
| 49 | #define LINUX_RAID_PARTITION 0xfd |
| 50 | #define is_extended(type) \ |
| 51 | (type == DOS_EXTENDED_PARTITION || \ |
| 52 | type == WIN98_EXTENDED_PARTITION || \ |
| 53 | type == LINUX_EXTENDED_PARTITION) |
| 54 | #define is_raid(type) \ |
| 55 | (type == LINUX_RAID_PARTITION) |
| 56 | |
Denys Vlasenko | 8930096 | 2009-11-01 23:07:18 +0100 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | const uint8_t *buf; |
| 60 | int i; |
| 61 | uint64_t poff; |
| 62 | uint64_t plen; |
| 63 | uint64_t extended = 0; |
| 64 | uint64_t current; |
| 65 | uint64_t next; |
| 66 | int limit; |
| 67 | int empty = 1; |
| 68 | struct msdos_partition_entry *part; |
| 69 | struct volume_id_partition *p; |
| 70 | |
| 71 | dbg("probing at offset 0x%llx", (unsigned long long) off); |
| 72 | |
| 73 | buf = volume_id_get_buffer(id, off, 0x200); |
| 74 | if (buf == NULL) |
| 75 | return -1; |
| 76 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 77 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 78 | return -1; |
| 79 | |
| 80 | /* check flags on all entries for a valid partition table */ |
| 81 | part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET]; |
| 82 | for (i = 0; i < 4; i++) { |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 83 | if (part[i].boot_ind != 0 |
| 84 | && part[i].boot_ind != 0x80 |
| 85 | ) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 86 | return -1; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 87 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 88 | |
| 89 | if (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); |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 97 | id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX * |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 98 | sizeof(struct volume_id_partition)); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 99 | |
| 100 | for (i = 0; i < 4; i++) { |
| 101 | poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE; |
| 102 | plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE; |
| 103 | |
| 104 | if (plen == 0) |
| 105 | continue; |
| 106 | |
| 107 | p = &id->partitions[i]; |
| 108 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 109 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 110 | |
| 111 | if (is_extended(part[i].sys_ind)) { |
| 112 | dbg("found extended partition at 0x%llx", (unsigned long long) poff); |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 113 | // volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE); |
| 114 | // p->type = "msdos_extended_partition"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 115 | if (extended == 0) |
| 116 | extended = off + poff; |
| 117 | } else { |
| 118 | dbg("found 0x%x data partition at 0x%llx, len 0x%llx", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 119 | part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 121 | // if (is_raid(part[i].sys_ind)) |
| 122 | // volume_id_set_usage_part(p, VOLUME_ID_RAID); |
| 123 | // else |
| 124 | // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 127 | // p->pt_off = off + poff; |
| 128 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 129 | id->partition_count = i+1; |
| 130 | } |
| 131 | |
| 132 | next = extended; |
| 133 | current = extended; |
| 134 | limit = 50; |
| 135 | |
| 136 | /* follow extended partition chain and add data partitions */ |
| 137 | while (next != 0) { |
| 138 | if (limit-- == 0) { |
| 139 | dbg("extended chain limit reached"); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | buf = volume_id_get_buffer(id, current, 0x200); |
| 144 | if (buf == NULL) |
| 145 | break; |
| 146 | |
| 147 | part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET]; |
| 148 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 149 | if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | next = 0; |
| 153 | |
| 154 | for (i = 0; i < 4; i++) { |
| 155 | poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE; |
| 156 | plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE; |
| 157 | |
| 158 | if (plen == 0) |
| 159 | continue; |
| 160 | |
| 161 | if (is_extended(part[i].sys_ind)) { |
| 162 | dbg("found extended partition at 0x%llx", (unsigned long long) poff); |
| 163 | if (next == 0) |
| 164 | next = extended + poff; |
| 165 | } else { |
| 166 | dbg("found 0x%x data partition at 0x%llx, len 0x%llx", |
| 167 | part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen); |
| 168 | |
| 169 | /* we always start at the 5th entry */ |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 170 | // while (id->partition_count < 4) |
| 171 | // volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED); |
| 172 | if (id->partition_count < 4) |
| 173 | id->partition_count = 4; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 174 | |
Denys Vlasenko | 9b2a9f0 | 2013-11-29 16:43:33 +0100 | [diff] [blame] | 175 | // p = &id->partitions[id->partition_count]; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 177 | // if (is_raid(part[i].sys_ind)) |
| 178 | // volume_id_set_usage_part(p, VOLUME_ID_RAID); |
| 179 | // else |
| 180 | // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 182 | // p->pt_off = current + poff; |
| 183 | // p->pt_len = plen; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 184 | id->partition_count++; |
| 185 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 186 | // p->pt_type_raw = part[i].sys_ind; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 187 | |
| 188 | if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) { |
| 189 | dbg("too many partitions"); |
| 190 | next = 0; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | current = next; |
| 196 | } |
| 197 | |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 198 | // volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE); |
| 199 | // id->type = "msdos_partition_table"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |