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 "libvolume_id.h"
31 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
37 for (i = 0; i + 2 <= count; i += 2) {
39 c = (buf[i+1] << 8) | buf[i];
41 c = (buf[i] << 8) | buf[i+1];
45 } else if (c < 0x80) {
48 str[j++] = (uint8_t) c;
49 } else if (c < 0x800) {
52 str[j++] = (uint8_t) (0xc0 | (c >> 6));
53 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
57 str[j++] = (uint8_t) (0xe0 | (c >> 12));
58 str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
59 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
65 static char *usage_to_string(enum volume_id_usage usage_id)
68 case VOLUME_ID_FILESYSTEM:
74 case VOLUME_ID_DISKLABEL:
76 case VOLUME_ID_CRYPTO:
78 case VOLUME_ID_UNPROBED:
80 case VOLUME_ID_UNUSED:
86 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
88 id->usage_id = usage_id;
89 id->usage = usage_to_string(usage_id);
92 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
94 memcpy(id->label_raw, buf, count);
95 id->label_raw_len = count;
98 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
102 memcpy(id->label, buf, count);
104 /* remove trailing whitespace */
105 i = strnlen(id->label, count);
107 if (!isspace(id->label[i]))
110 id->label[i+1] = '\0';
113 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
115 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
118 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
121 unsigned int count = 0;
134 case UUID_DCE_STRING:
138 memcpy(id->uuid_raw, buf, count);
139 id->uuid_raw_len = count;
141 /* if set, create string in the same format, the native platform uses */
142 for (i = 0; i < count; i++)
150 sprintf(id->uuid, "%02X%02X-%02X%02X",
151 buf[3], buf[2], buf[1], buf[0]);
154 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
155 buf[7], buf[6], buf[5], buf[4],
156 buf[3], buf[2], buf[1], buf[0]);
159 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
160 buf[0], buf[1], buf[2], buf[3],
161 buf[4], buf[5], buf[6], buf[7]);
165 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
166 buf[0], buf[1], buf[2], buf[3],
170 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
172 case UUID_DCE_STRING:
173 memcpy(id->uuid, buf, count);
174 id->uuid[count] = '\0';
179 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
183 info("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
184 /* check if requested area fits in superblock buffer */
185 if (off + len <= SB_BUFFER_SIZE) {
186 if (id->sbbuf == NULL) {
187 id->sbbuf = malloc(SB_BUFFER_SIZE);
188 if (id->sbbuf == NULL) {
194 /* check if we need to read */
195 if ((off + len) > id->sbbuf_len) {
196 info("read sbbuf len:0x%llx", (unsigned long long) (off + len));
197 if (lseek(id->fd, 0, SEEK_SET) < 0) {
198 dbg("lseek failed (%s)", strerror(errno));
201 buf_len = read(id->fd, id->sbbuf, off + len);
203 dbg("read failed (%s)", strerror(errno));
206 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
207 id->sbbuf_len = buf_len;
208 if ((size_t)buf_len < off + len) {
209 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
214 return &(id->sbbuf[off]);
216 if (len > SEEK_BUFFER_SIZE) {
217 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
221 /* get seek buffer */
222 if (id->seekbuf == NULL) {
223 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
224 if (id->seekbuf == NULL) {
230 /* check if we need to read */
231 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
232 info("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
233 if (lseek(id->fd, off, SEEK_SET) < 0) {
234 dbg("lseek failed (%s)", strerror(errno));
237 buf_len = read(id->fd, id->seekbuf, len);
239 dbg("read failed (%s)", strerror(errno));
242 dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
243 id->seekbuf_off = off;
244 id->seekbuf_len = buf_len;
245 if ((size_t)buf_len < len) {
246 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
251 return &(id->seekbuf[off - id->seekbuf_off]);
255 void volume_id_free_buffer(struct volume_id *id)
257 if (id->sbbuf != NULL) {
262 if (id->seekbuf != NULL) {