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 int get_linux_version (void)
30 n
= sscanf(uts
.release
, "%d.%d.%d", &major
, &minor
, &teeny
);
34 return kver
= KERNEL_VERSION(major
, minor
, teeny
);
37 int blkid_probe_is_tiny(blkid_probe pr
)
43 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
44 unsigned char *data
, size_t len
)
50 int blkid_probe_set_version(blkid_probe pr
, const char *version
)
52 int len
= strlen(version
);
53 if (len
> (sizeof(pr
->version
) - 1)) {
54 fprintf(stderr
, "version buffer too small %d\n", len
);
58 strncpy(pr
->version
, version
, sizeof(pr
->version
));
63 int blkid_probe_sprintf_version(blkid_probe pr
, const char *fmt
, ...)
69 n
= vsnprintf(pr
->version
, sizeof(pr
->version
), fmt
, ap
);
72 if (n
>= sizeof(pr
->version
))
73 fprintf(stderr
, "version buffer too small %d\n", n
);
78 unsigned char *blkid_probe_get_buffer(blkid_probe pr
,
79 blkid_loff_t off
, blkid_loff_t len
)
81 struct blkid_bufinfo
*bf
;
84 bf
= malloc(sizeof(*bf
) + len
);
87 memset(bf
, 0, sizeof(*bf
));
88 bf
->data
= ((unsigned char *)bf
) + sizeof(*bf
);
90 if (lseek(pr
->fd
, off
, SEEK_SET
) < 0) {
91 fprintf(stderr
, "failed to seek\n");
95 ret
= read(pr
->fd
, bf
->data
, len
);
98 fprintf(stderr
, "failed to read blkid\n");
103 list_add_tail(&bf
->bufs
, &pr
->buffers
);
108 int blkid_probe_set_id_label(blkid_probe pr
, const char *name
,
109 const unsigned char *data
, size_t len
)
114 int blkid_probe_set_label(blkid_probe pr
, unsigned char *label
, size_t len
)
116 if (len
> (sizeof(pr
->label
) - 1)) {
117 fprintf(stderr
, "label buffer too small %d > %d\n",
118 (int) len
, (int) sizeof(pr
->label
) - 1);
121 memcpy(pr
->label
, label
, len
+ 1);
126 int blkid_probe_set_utf8label(blkid_probe pr
, unsigned char *label
,
129 if (len
> (sizeof(pr
->label
) - 1)) {
130 fprintf(stderr
, "label buffer too small %d > %d\n",
131 (int) len
, (int) sizeof(pr
->label
) - 1);
135 blkid_encode_to_utf8(enc
,(unsigned char*) pr
->label
, len
,
141 int blkid_probe_set_uuid_as(blkid_probe pr
, unsigned char *uuid
, const char *name
)
143 short unsigned int*u
= (short unsigned int*) uuid
;
145 if (u
[0] && (!name
|| !strcmp(name
, "UUID"))) {
147 "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
148 be16_to_cpu(u
[0]), be16_to_cpu(u
[1]), be16_to_cpu(u
[2]), be16_to_cpu(u
[3]),
149 be16_to_cpu(u
[4]), be16_to_cpu(u
[5]), be16_to_cpu(u
[6]), be16_to_cpu(u
[7]));
155 int blkid_probe_set_uuid(blkid_probe pr
, unsigned char *uuid
)
157 return blkid_probe_set_uuid_as(pr
, uuid
, NULL
);
160 int blkid_probe_sprintf_uuid(blkid_probe pr
, unsigned char *uuid
,
161 size_t len
, const char *fmt
, ...)
166 vsnprintf(pr
->uuid
, sizeof(pr
->uuid
), fmt
, ap
);
172 static const struct blkid_idinfo
*idinfos
[] =
193 int probe_block(char *block
, struct blkid_struct_probe
*pr
)
198 if (stat(block
, &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
) && !strncmp(block
, "ubi", 3)))
202 pr
->fd
= open(block
, O_RDONLY
);
206 for (i
= 0; i
< ARRAY_SIZE(idinfos
); i
++) {
207 /* loop over all magic handlers */
208 const struct blkid_idmag
*mag
;
210 /* loop over all probe handlers */
211 DEBUG("scanning %s\n", idinfos
[i
]->name
);
213 mag
= &idinfos
[i
]->magics
[0];
216 int off
= (mag
->kboff
* 1024) + mag
->sboff
;
217 char magic
[32] = { 0 };
219 if (lseek(pr
->fd
, off
, SEEK_SET
) < 0) {
223 if (read(pr
->fd
, magic
, mag
->len
) < 0) {
227 DEBUG("magic: %s %s %d\n", mag
->magic
, magic
, mag
->len
);
228 if (!memcmp(mag
->magic
, magic
, mag
->len
))
233 if (mag
&& mag
->magic
) {
234 DEBUG("probing %s\n", idinfos
[i
]->name
);
235 pr
->err
= idinfos
[i
]->probefunc(pr
, mag
);