1 /* blkid.c - Prints type, label and UUID of filesystem(s).
2 *
3 * Copyright 2013 Brad Conroy <[email protected]>
4 *
5 * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html
6 * TODO: -U and -L should require arguments
7
8 USE_BLKID(NEWTOY(blkid, "ULo:s*[!LU]", TOYFLAG_BIN|TOYFLAG_LINEBUF))
9 USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN|TOYFLAG_LINEBUF))
10
11 config BLKID
12 bool "blkid"
13 default y
14 help
15 usage: blkid [-o TYPE] [-s TAG] [-UL] DEV...
16
17 Print type, label and UUID of filesystem on a block device or image.
18
19 -U Show UUID only (or device with that UUID)
20 -L Show LABEL only (or device with that LABEL)
21 -o TYPE Output format (full, value, export)
22 -s TAG Only show matching tags (default all)
23
24 config FSTYPE
25 bool "fstype"
26 default y
27 help
28 usage: fstype DEV...
29
30 Print type of filesystem on a block device or image.
31 */
32
33 #define FOR_blkid
34 #include "toys.h"
35
36 GLOBALS(
37 struct arg_list *s;
38 char *o;
39 )
40
41 struct fstype {
42 char *name;
43 uint64_t magic;
44 int magic_len, magic_offset, uuid_off, label_len, label_off;
45 } static const fstypes[] = {
46 {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144}, // keep this first for ext3/4 check
47 {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
48 // NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via
49 // codepage, something called a uuid that's only 8 bytes long...
50 {"ntfs", 0x5346544e, 4, 3, 0x48, 0, 0},
51
52 {"adfs", 0xadf5, 2, 0xc00, 0,0,0},
53 {"bfs", 0x1badface, 4, 0, 0,0,0},
54 {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
55 {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
56 {"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c},
57 {"iso9660", 0x444301, 3, 0x8000, 0x832d, 32, 0x8028},
58 {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
59 {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
60 {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
61 {"reiserfs", 0x724573496552ULL, 6, 65588, 65620, 16, 65636},
62 {"romfs", 0x2d6d6f72, 4, 0, 0,0,0},
63 {"squashfs", 0x73717368, 4, 0, 0,0,0},
64 {"xiafs", 0x012fd16d, 4, 572, 0,0,0},
65 {"xfs", 0x42534658, 4, 0, 32, 12, 108},
66 {"vfat", 0x3233544146ULL, 5, 82, 67, 11, 71}, // fat32
67 {"vfat", 0x31544146, 4, 54, 39, 11, 43} // fat1
68 };
69
escape(char * str,int force)70 static void escape(char *str, int force)
71 {
72 if (!force && str[strcspn(str, "\" \\\n\t$<>|&;`'~()!#?")]) force++;
73 if (!force) return xputsn(str);
74
75 putchar('"');
76 while (*str) {
77 if (strchr("\" \\", *str)) putchar('\\');
78 putchar(*str++);
79 }
80 putchar('"');
81 }
82
show_tag(char * key,char * value)83 static void show_tag(char *key, char *value)
84 {
85 int show = 0;
86 struct arg_list *al;
87
88 if (TT.s) {
89 for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1;
90 } else show = 1;
91
92 if (!show || !*value) return;
93 if (!strcasecmp(TT.o, "full") || !strcasecmp(TT.o, "export")) {
94 printf(" %s="+!(*TT.o=='f'), key);
95 escape(value, *TT.o=='f');
96 if (*TT.o=='e') xputc('\n');
97 } else if (!strcasecmp(TT.o, "value")) xputs(value);
98 else error_exit("bad -o %s", TT.o);
99 }
100
flagshow(char * s,char * name)101 static void flagshow(char *s, char *name)
102 {
103 if (*toys.optargs && strcmp(s, *toys.optargs)) return;
104 printf("%s\n", *toys.optargs ? name : s);
105 if (*toys.optargs) xexit();
106 }
107
do_blkid(int fd,char * name)108 static void do_blkid(int fd, char *name)
109 {
110 int off, i, j, len;
111 char buf[128], *type, *s;
112
113 off = i = 0;
114
115 for (;;) {
116 int pass = 0;
117
118 // Read next block of data
119 len = readall(fd, toybuf, sizeof(toybuf));
120 if (len != sizeof(toybuf)) return;
121
122 // Iterate through types in range
123 for (i=0; i<ARRAY_LEN(fstypes); i++) {
124 uint64_t test;
125
126 // Skip tests not in this 4k block
127 if (fstypes[i].magic_offset + fstypes[i].magic_len > off+sizeof(toybuf)) {
128 pass++;
129 continue;
130 }
131 if (fstypes[i].magic_offset < off) continue;
132
133 // Populate 64 bit little endian magic value
134 test = 0;
135 for (j = 0; j < fstypes[i].magic_len; j++)
136 test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j);
137 if (test == fstypes[i].magic) break;
138 }
139
140 if (i == ARRAY_LEN(fstypes)) {
141 off += len;
142 if (pass) continue;
143 return;
144 }
145 break;
146 }
147
148 // distinguish ext2/3/4
149 type = fstypes[i].name;
150 if (!i) {
151 if (toybuf[1116]&4) type = "ext3";
152 if (toybuf[1120]&64) type = "ext4";
153 }
154
155 // Output for fstype
156 if (*toys.which->name == 'f') {
157 puts(type);
158 return;
159 }
160
161 // output for blkid
162 if (!FLAG(L) && !FLAG(U)) {
163 if (!TT.o || !strcasecmp(TT.o, "full")) printf("%s:", name);
164 else if (!strcasecmp(TT.o, "export")) show_tag("DEVNAME", name);
165 }
166
167 len = fstypes[i].label_len;
168 if (!FLAG(U) && len) {
169 s = toybuf+fstypes[i].label_off-off;
170 if (!strcmp(type, "vfat") || !strcmp(type, "iso9660")) {
171 if (*type=='v' && fstypes[i].magic_len==4 && !FLAG(L))
172 show_tag("SEC_TYPE", "msdos");
173 while (len && s[len-1]==' ') len--;
174 if (strstart(&s, "NO NAME")) len=0;
175 }
176 // TODO: special case NTFS $VOLUME_NAME here...
177 if (len) {
178 if (!strcmp(type, "f2fs")) {
179 // Convert UTF16LE to ASCII by replacing non-ASCII with '?'.
180 // TODO: support non-ASCII.
181 for (j=0; j<len; j++) {
182 buf[j] = s[2*j];
183 if (s[2*j+1]) buf[j]='?';
184 if (!buf[j]) break;
185 }
186 } else sprintf(buf, "%.*s", len, s);
187 if (FLAG(L)) return flagshow(buf, name);
188 show_tag("LABEL", buf);
189 }
190 }
191
192 len = fstypes[i].uuid_off;
193 if (!FLAG(L) && len) {
194 int uoff = len-off;
195
196 // Assemble UUID with whatever size and set of dashes this filesystem uses
197 s = buf;
198 if (!strcmp(type, "ntfs")) {
199 for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
200 } else if (!strcmp(type, "vfat")) {
201 s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
202 toybuf[uoff+1], toybuf[uoff]);
203 } else if (!strcmp(type, "iso9660")) {
204 s = stpncpy(s, toybuf+uoff, 4);
205 for (i = 0, uoff += 4; i<12; i++) {
206 if (!(i&1)) *s++ = '-';
207 *s++ = toybuf[uoff++];
208 }
209 *s = 0;
210 } else {
211 for (j = 0; j < 16; j++)
212 s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
213 }
214
215 if (FLAG(U)) return flagshow(buf, name);
216 show_tag("UUID", buf);
217 }
218
219 if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
220 show_tag("SEC_TYPE", "ext2");
221
222 if (FLAG(U) || FLAG(L)) return;
223
224 show_tag("TYPE", type);
225 if (!strcasecmp(TT.o, "full")) xputc('\n');
226 }
227
blkid_main(void)228 void blkid_main(void)
229 {
230 if (!TT.o) TT.o = "full";
231
232 if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
233 else {
234 unsigned int ma, mi, sz, fd;
235 char name[32], device[5+32];
236 FILE *fp = xfopen("/proc/partitions", "r");
237
238 while (fgets(toybuf, sizeof(toybuf), fp)) {
239 if (sscanf(toybuf, " %u %u %u %31s", &ma, &mi, &sz, name) != 4)
240 continue;
241
242 sprintf(device, "/dev/%.20s", name);
243 if (-1 == (fd = open(device, O_RDONLY))) {
244 if (errno != ENOMEDIUM) perror_msg_raw(device);
245 } else {
246 do_blkid(fd, device);
247 close(fd);
248 }
249 }
250 if (CFG_TOYBOX_FREE) fclose(fp);
251 }
252
253 if (FLAG(L) || FLAG(U)) toys.exitval = 2;
254 }
255
fstype_main(void)256 void fstype_main(void)
257 {
258 loopfiles(toys.optargs, do_blkid);
259 }
260