2 #include <sys/utsname.h>
4 #include "libblkid-tiny.h"
5 #include "superblocks.h"
6 #include "linux_version.h"
9 #define DEBUG(fmt, ...) printf(fmt, __VA_ARGS__)
11 #define DEBUG(fmt, ...)
14 int blkid_debug_mask = 0;
16 static unsigned char *probe_buffer;
17 static unsigned int probe_buffer_size = 0;
19 int get_linux_version (void)
33 n = sscanf(uts.release, "%d.%d.%d", &major, &minor, &teeny);
37 return kver = KERNEL_VERSION(major, minor, teeny);
40 int blkid_probe_is_tiny(blkid_probe pr)
46 int blkid_probe_set_value(blkid_probe pr, const char *name,
47 unsigned char *data, size_t len)
53 int blkid_probe_set_version(blkid_probe pr, const char *version)
55 int len = strlen(version);
56 if (len > (sizeof(pr->version) - 1)) {
57 fprintf(stderr, "version buffer too small %d\n", len);
61 strncpy(pr->version, version, sizeof(pr->version));
66 int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...)
72 n = vsnprintf(pr->version, sizeof(pr->version), fmt, ap);
75 if (n >= sizeof(pr->version))
76 fprintf(stderr, "version buffer too small %d\n", n);
81 unsigned char *blkid_probe_get_buffer(blkid_probe pr,
82 blkid_loff_t off, blkid_loff_t len)
87 if (len > probe_buffer_size) {
88 buf = realloc(probe_buffer, len);
91 fprintf(stderr, "failed to allocate %d byte buffer\n",
98 probe_buffer_size = len;
101 memset(probe_buffer, 0, probe_buffer_size);
103 lseek(pr->fd, off, SEEK_SET);
104 ret = read(pr->fd, probe_buffer, len);
107 fprintf(stderr, "faile to read blkid\n");
112 int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
114 if (len > (sizeof(pr->label) - 1)) {
115 fprintf(stderr, "label buffer too small %d > %d\n",
116 (int) len, (int) sizeof(pr->label) - 1);
119 memcpy(pr->label, label, len + 1);
124 int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name)
126 short unsigned int*u = (short unsigned int*) uuid;
128 if (u[0] && (!name || !strcmp(name, "UUID"))) {
130 "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
131 be16_to_cpu(u[0]), be16_to_cpu(u[1]), be16_to_cpu(u[2]), be16_to_cpu(u[3]),
132 be16_to_cpu(u[4]), be16_to_cpu(u[5]), be16_to_cpu(u[6]), be16_to_cpu(u[7]));
138 int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid)
140 return blkid_probe_set_uuid_as(pr, uuid, NULL);
143 int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid,
144 size_t len, const char *fmt, ...)
149 vsnprintf(pr->uuid, sizeof(pr->uuid), fmt, ap);
155 static const struct blkid_idinfo *idinfos[] =
174 int probe_block(char *block, struct blkid_struct_probe *pr)
179 if (stat(block, &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode) && !strncmp(block, "ubi", 3)))
183 pr->fd = open(block, O_RDONLY);
187 for (i = 0; i < ARRAY_SIZE(idinfos); i++) {
188 /* loop over all magic handlers */
189 const struct blkid_idmag *mag;
191 /* loop over all probe handlers */
192 DEBUG("scanning %s\n", idinfos[i]->name);
194 mag = &idinfos[i]->magics[0];
197 int off = (mag->kboff * 1024) + mag->sboff;
198 char magic[32] = { 0 };
200 lseek(pr->fd, off, SEEK_SET);
201 if (read(pr->fd, magic, mag->len) < 0)
204 DEBUG("magic: %s %s %d\n", mag->magic, magic, mag->len);
205 if (!memcmp(mag->magic, magic, mag->len))
210 if (mag && mag->magic) {
211 DEBUG("probing %s\n", idinfos[i]->name);
212 pr->err = idinfos[i]->probefunc(pr, mag);
214 strcpy(pr->dev, block);