2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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
37 #include <asm/types.h>
39 #include "volume_id.h"
43 static char *usage_to_string(enum volume_id_usage usage_id)
46 case VOLUME_ID_FILESYSTEM:
48 case VOLUME_ID_PARTITIONTABLE:
49 return "partitiontable";
54 case VOLUME_ID_DISKLABEL:
56 case VOLUME_ID_CRYPTO:
58 case VOLUME_ID_UNPROBED:
60 case VOLUME_ID_UNUSED:
66 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
68 part->usage_id = usage_id;
69 part->usage = usage_to_string(usage_id);
72 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
74 id->usage_id = usage_id;
75 id->usage = usage_to_string(usage_id);
78 void volume_id_set_label_raw(struct volume_id *id, const __u8 *buf, unsigned int count)
80 memcpy(id->label_raw, buf, count);
81 id->label_raw_len = count;
84 void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned int count)
88 memcpy(id->label, buf, count);
90 /* remove trailing whitespace */
91 i = strnlen(id->label, count);
93 if (!isspace(id->label[i]))
96 id->label[i+1] = '\0';
99 void volume_id_set_label_unicode16(struct volume_id *id, const __u8 *buf, enum endian endianess, unsigned int count)
105 for (i = 0; i + 2 <= count; i += 2) {
107 c = (buf[i+1] << 8) | buf[i];
109 c = (buf[i] << 8) | buf[i+1];
113 } else if (c < 0x80) {
114 id->label[j++] = (__u8) c;
115 } else if (c < 0x800) {
116 id->label[j++] = (__u8) (0xc0 | (c >> 6));
117 id->label[j++] = (__u8) (0x80 | (c & 0x3f));
119 id->label[j++] = (__u8) (0xe0 | (c >> 12));
120 id->label[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f));
121 id->label[j++] = (__u8) (0x80 | (c & 0x3f));
126 void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format)
129 unsigned int count = 0;
142 case UUID_DCE_STRING:
146 memcpy(id->uuid_raw, buf, count);
147 id->uuid_raw_len = count;
149 /* if set, create string in the same format, the native platform uses */
150 for (i = 0; i < count; i++)
158 sprintf(id->uuid, "%02X%02X-%02X%02X",
159 buf[3], buf[2], buf[1], buf[0]);
162 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
163 buf[7], buf[6], buf[5], buf[4],
164 buf[3], buf[2], buf[1], buf[0]);
167 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
168 buf[0], buf[1], buf[2], buf[3],
169 buf[4], buf[5], buf[6], buf[7]);
173 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
174 buf[0], buf[1], buf[2], buf[3],
178 buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
180 case UUID_DCE_STRING:
181 memcpy(id->uuid, buf, count);
182 id->uuid[count] = '\0';
187 __u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len)
189 unsigned int buf_len;
191 dbg("get buffer off 0x%llx(%llu), len 0x%x", (unsigned long long) off, (unsigned long long) off, len);
192 /* check if requested area fits in superblock buffer */
193 if (off + len <= SB_BUFFER_SIZE) {
194 if (id->sbbuf == NULL) {
195 id->sbbuf = malloc(SB_BUFFER_SIZE);
196 if (id->sbbuf == NULL)
200 /* check if we need to read */
201 if ((off + len) > id->sbbuf_len) {
202 dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
203 lseek(id->fd, 0, SEEK_SET);
204 buf_len = read(id->fd, id->sbbuf, off + len);
205 dbg("got 0x%x (%i) bytes", buf_len, buf_len);
206 id->sbbuf_len = buf_len;
207 if (buf_len < off + len)
211 return &(id->sbbuf[off]);
213 if (len > SEEK_BUFFER_SIZE) {
214 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
218 /* get seek buffer */
219 if (id->seekbuf == NULL) {
220 id->seekbuf = malloc(SEEK_BUFFER_SIZE);
221 if (id->seekbuf == NULL)
225 /* check if we need to read */
226 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
227 dbg("read seekbuf off:0x%llx len:0x%x", (unsigned long long) off, len);
228 if (lseek(id->fd, off, SEEK_SET) == -1)
230 buf_len = read(id->fd, id->seekbuf, len);
231 dbg("got 0x%x (%i) bytes", buf_len, buf_len);
232 id->seekbuf_off = off;
233 id->seekbuf_len = buf_len;
235 dbg("requested 0x%x bytes, got only 0x%x bytes", len, buf_len);
240 return &(id->seekbuf[off - id->seekbuf_off]);
244 void volume_id_free_buffer(struct volume_id *id)
246 if (id->sbbuf != NULL) {
251 if (id->seekbuf != NULL) {