2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
28 #include "volume_id.h"
32 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
38 for (i = 0; i + 2 <= count; i += 2) {
40 c = (buf[i+1] << 8) | buf[i];
42 c = (buf[i] << 8) | buf[i+1];
46 } else if (c < 0x80) {
49 str[j++] = (uint8_t) c;
50 } else if (c < 0x800) {
53 str[j++] = (uint8_t) (0xc0 | (c >> 6));
54 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
58 str[j++] = (uint8_t) (0xe0 | (c >> 12));
59 str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
60 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
66 static char *usage_to_string(enum volume_id_usage usage_id)
69 case VOLUME_ID_FILESYSTEM:
75 case VOLUME_ID_DISKLABEL:
77 case VOLUME_ID_CRYPTO:
79 case VOLUME_ID_UNPROBED:
81 case VOLUME_ID_UNUSED:
87 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
89 id->usage_id = usage_id;
90 id->usage = usage_to_string(usage_id);
93 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
95 memcpy(id->label_raw, buf, count);
96 id->label_raw_len = count;
99 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
103 memcpy(id->label, buf, count);
105 /* remove trailing whitespace */
106 i = strnlen(id->label, count);
108 if (!isspace(id->label[i]))
111 id->label[i+1] = '\0';
114 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
116 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
119 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
122 unsigned int count = 0;
135 case UUID_DCE_STRING:
139 memcpy(id->uuid_raw, buf, count);
140 id->uuid_raw_len = count;
142 /* if set, create string in the same format, the native platform uses */
143 for (i = 0; i < count; i++)
151 sprintf(id->uuid, "%02X%02X-%02X%02X",
152 buf[3], buf[2], buf[1], buf[0]);
155 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
156 buf[7], buf[6], buf[5], buf[4],
157 buf[3], buf[2], buf[1], buf[0]);
160 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
161 buf[0], buf[1], buf[2], buf[3],
162 buf[4], buf[5], buf[6], buf[7]);
166 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
167 buf[0], buf[1], buf[2], buf[3],
171 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
173 case UUID_DCE_STRING:
174 memcpy(id->uuid, buf, count);
175 id->uuid[count] = '\0';
180 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
184 dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
185 /* check if requested area fits in superblock buffer */
186 if (off + len <= SB_BUFFER_SIZE) {
187 if (id->sbbuf == NULL) {
188 id->sbbuf = malloc(SB_BUFFER_SIZE);
189 if (id->sbbuf == NULL) {
195 /* check if we need to read */
196 if ((off + len) > id->sbbuf_len) {
197 dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
198 if (lseek(id->fd, 0, SEEK_SET) < 0) {
199 dbg("lseek failed (%s)", strerror(errno));
202 buf_len = read(id->fd, id->sbbuf, off + len);
204 dbg("read failed (%s)", strerror(errno));
207 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
208 id->sbbuf_len = buf_len;
209 if ((size_t)buf_len < off + len) {
210 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
215 return &(id->sbbuf[off]);
217 if (len > SEEK_BUFFER_SIZE) {
218 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
222 /* get seek buffer */
223 if (id->seekbuf == NULL) {
224 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
225 if (id->seekbuf == NULL) {
231 /* check if we need to read */
232 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
233 dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
234 if (lseek(id->fd, off, SEEK_SET) < 0) {
235 dbg("lseek failed (%s)", strerror(errno));
238 buf_len = read(id->fd, id->seekbuf, len);
240 dbg("read failed (%s)", strerror(errno));
243 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
244 id->seekbuf_off = off;
245 id->seekbuf_len = buf_len;
246 if ((size_t)buf_len < len) {
247 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
252 return &(id->seekbuf[off - id->seekbuf_off]);
256 void volume_id_free_buffer(struct volume_id *id)
258 if (id->sbbuf != NULL) {
263 if (id->seekbuf != NULL) {