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) 2005 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 | void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count) |
| 24 | { |
| 25 | unsigned i, j; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 26 | unsigned c; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 27 | |
| 28 | j = 0; |
| 29 | for (i = 0; i + 2 <= count; i += 2) { |
| 30 | if (endianess == LE) |
| 31 | c = (buf[i+1] << 8) | buf[i]; |
| 32 | else |
| 33 | c = (buf[i] << 8) | buf[i+1]; |
Denys Vlasenko | 8019b3a | 2012-09-02 15:33:47 +0200 | [diff] [blame] | 34 | if (c == 0) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 35 | break; |
Denys Vlasenko | 8019b3a | 2012-09-02 15:33:47 +0200 | [diff] [blame] | 36 | if (j+1 >= len) |
| 37 | break; |
| 38 | if (c < 0x80) { |
| 39 | /* 0xxxxxxx */ |
| 40 | } else { |
| 41 | uint8_t topbits = 0xc0; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 42 | if (j+2 >= len) |
| 43 | break; |
Denys Vlasenko | 8019b3a | 2012-09-02 15:33:47 +0200 | [diff] [blame] | 44 | if (c < 0x800) { |
| 45 | /* 110yyyxx 10xxxxxx */ |
| 46 | } else { |
| 47 | if (j+3 >= len) |
| 48 | break; |
| 49 | /* 1110yyyy 10yyyyxx 10xxxxxx */ |
| 50 | str[j++] = (uint8_t) (0xe0 | (c >> 12)); |
| 51 | topbits = 0x80; |
| 52 | } |
| 53 | str[j++] = (uint8_t) (topbits | ((c >> 6) & 0x3f)); |
| 54 | c = 0x80 | (c & 0x3f); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 55 | } |
Denys Vlasenko | 8019b3a | 2012-09-02 15:33:47 +0200 | [diff] [blame] | 56 | str[j++] = (uint8_t) c; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 57 | } |
| 58 | str[j] = '\0'; |
| 59 | } |
| 60 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 61 | #ifdef UNUSED |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 62 | static const char *usage_to_string(enum volume_id_usage usage_id) |
| 63 | { |
| 64 | switch (usage_id) { |
| 65 | case VOLUME_ID_FILESYSTEM: |
| 66 | return "filesystem"; |
| 67 | case VOLUME_ID_PARTITIONTABLE: |
| 68 | return "partitiontable"; |
| 69 | case VOLUME_ID_OTHER: |
| 70 | return "other"; |
| 71 | case VOLUME_ID_RAID: |
| 72 | return "raid"; |
| 73 | case VOLUME_ID_DISKLABEL: |
| 74 | return "disklabel"; |
| 75 | case VOLUME_ID_CRYPTO: |
| 76 | return "crypto"; |
| 77 | case VOLUME_ID_UNPROBED: |
| 78 | return "unprobed"; |
| 79 | case VOLUME_ID_UNUSED: |
| 80 | return "unused"; |
| 81 | } |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id) |
| 86 | { |
| 87 | part->usage_id = usage_id; |
| 88 | part->usage = usage_to_string(usage_id); |
| 89 | } |
| 90 | |
| 91 | void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id) |
| 92 | { |
| 93 | id->usage_id = usage_id; |
| 94 | id->usage = usage_to_string(usage_id); |
| 95 | } |
| 96 | |
| 97 | void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count) |
| 98 | { |
| 99 | memcpy(id->label_raw, buf, count); |
| 100 | id->label_raw_len = count; |
| 101 | } |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 102 | #endif |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 103 | |
| 104 | #ifdef NOT_NEEDED |
| 105 | static size_t strnlen(const char *s, size_t maxlen) |
| 106 | { |
| 107 | size_t i; |
| 108 | if (!maxlen) return 0; |
| 109 | if (!s) return 0; |
| 110 | for (i = 0; *s && i < maxlen; ++s) ++i; |
| 111 | return i; |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count) |
| 116 | { |
| 117 | unsigned i; |
| 118 | |
| 119 | memcpy(id->label, buf, count); |
| 120 | |
| 121 | /* remove trailing whitespace */ |
| 122 | i = strnlen(id->label, count); |
| 123 | while (i--) { |
| 124 | if (!isspace(id->label[i])) |
| 125 | break; |
| 126 | } |
| 127 | id->label[i+1] = '\0'; |
| 128 | } |
| 129 | |
| 130 | void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count) |
| 131 | { |
| 132 | volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count); |
| 133 | } |
| 134 | |
| 135 | void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format) |
| 136 | { |
| 137 | unsigned i; |
Sven-Göran Bergh | 5218515 | 2013-01-14 00:50:49 +0100 | [diff] [blame^] | 138 | unsigned count = (format == UUID_DCE_STRING ? VOLUME_ID_UUID_SIZE : 4 << format); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 140 | // memcpy(id->uuid_raw, buf, count); |
| 141 | // id->uuid_raw_len = count; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 142 | |
| 143 | /* if set, create string in the same format, the native platform uses */ |
| 144 | for (i = 0; i < count; i++) |
| 145 | if (buf[i] != 0) |
| 146 | goto set; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 147 | return; /* all bytes are zero, leave it empty ("") */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 148 | |
| 149 | set: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 150 | switch (format) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 151 | case UUID_DOS: |
| 152 | sprintf(id->uuid, "%02X%02X-%02X%02X", |
| 153 | buf[3], buf[2], buf[1], buf[0]); |
| 154 | break; |
| 155 | case UUID_NTFS: |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 156 | sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X", |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 157 | buf[7], buf[6], buf[5], buf[4], |
| 158 | buf[3], buf[2], buf[1], buf[0]); |
| 159 | break; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 160 | case UUID_DCE: |
| 161 | sprintf(id->uuid, |
| 162 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 163 | buf[0], buf[1], buf[2], buf[3], |
| 164 | buf[4], buf[5], |
| 165 | buf[6], buf[7], |
| 166 | buf[8], buf[9], |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame] | 167 | buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 168 | break; |
| 169 | case UUID_DCE_STRING: |
| 170 | memcpy(id->uuid, buf, count); |
| 171 | id->uuid[count] = '\0'; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame] | 176 | /* Do not use xlseek here. With it, single corrupted filesystem |
| 177 | * may result in attempt to seek past device -> exit. |
| 178 | * It's better to ignore such fs and continue. */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 179 | void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len) |
| 180 | { |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 181 | uint8_t *dst; |
| 182 | unsigned small_off; |
| 183 | ssize_t read_len; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 185 | dbg("get buffer off 0x%llx(%llu), len 0x%zx", |
| 186 | (unsigned long long) off, (unsigned long long) off, len); |
| 187 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 188 | /* check if requested area fits in superblock buffer */ |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 189 | if (off + len <= SB_BUFFER_SIZE |
| 190 | /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */ |
| 191 | ) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 192 | if (id->sbbuf == NULL) { |
| 193 | id->sbbuf = xmalloc(SB_BUFFER_SIZE); |
| 194 | } |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 195 | small_off = off; |
| 196 | dst = id->sbbuf; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 197 | |
| 198 | /* check if we need to read */ |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 199 | len += off; |
| 200 | if (len <= id->sbbuf_len) |
| 201 | goto ret; /* we already have it */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 203 | dbg("read sbbuf len:0x%x", (unsigned) len); |
| 204 | id->sbbuf_len = len; |
| 205 | off = 0; |
| 206 | goto do_read; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (len > SEEK_BUFFER_SIZE) { |
| 210 | dbg("seek buffer too small %d", SEEK_BUFFER_SIZE); |
| 211 | return NULL; |
| 212 | } |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 213 | dst = id->seekbuf; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 214 | |
| 215 | /* check if we need to read */ |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 216 | if ((off >= id->seekbuf_off) |
| 217 | && ((off + len) <= (id->seekbuf_off + id->seekbuf_len)) |
| 218 | ) { |
| 219 | small_off = off - id->seekbuf_off; /* can't overflow */ |
| 220 | goto ret; /* we already have it */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 223 | id->seekbuf_off = off; |
| 224 | id->seekbuf_len = len; |
| 225 | id->seekbuf = xrealloc(id->seekbuf, len); |
| 226 | small_off = 0; |
| 227 | dst = id->seekbuf; |
| 228 | dbg("read seekbuf off:0x%llx len:0x%zx", |
| 229 | (unsigned long long) off, len); |
| 230 | do_read: |
| 231 | if (lseek(id->fd, off, SEEK_SET) != off) { |
| 232 | dbg("seek(0x%llx) failed", (unsigned long long) off); |
| 233 | goto err; |
| 234 | } |
| 235 | read_len = full_read(id->fd, dst, len); |
| 236 | if (read_len != len) { |
| 237 | dbg("requested 0x%x bytes, got 0x%x bytes", |
| 238 | (unsigned) len, (unsigned) read_len); |
| 239 | err: |
Denis Vlasenko | 28ea429 | 2009-02-15 05:51:19 +0000 | [diff] [blame] | 240 | /* No filesystem can be this tiny. It's most likely |
| 241 | * non-associated loop device, empty drive and so on. |
| 242 | * Flag it, making it possible to short circuit future |
| 243 | * accesses. Rationale: |
| 244 | * users complained of slow blkid due to empty floppy drives. |
| 245 | */ |
| 246 | if (off < 64*1024) |
| 247 | id->error = 1; |
| 248 | /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */ |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 249 | volume_id_free_buffer(id); |
| 250 | return NULL; |
| 251 | } |
| 252 | ret: |
| 253 | return dst + small_off; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void volume_id_free_buffer(struct volume_id *id) |
| 257 | { |
| 258 | free(id->sbbuf); |
| 259 | id->sbbuf = NULL; |
| 260 | id->sbbuf_len = 0; |
| 261 | free(id->seekbuf); |
| 262 | id->seekbuf = NULL; |
| 263 | id->seekbuf_len = 0; |
Denis Vlasenko | 1e10afc | 2008-11-30 17:41:31 +0000 | [diff] [blame] | 264 | id->seekbuf_off = 0; /* paranoia */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 265 | } |