• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* blkid.c - Prints type, label and UUID of filesystem(s).
2  *
3  * Copyright 2013 Brad Conroy <bconroy@uis.edu>
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))
9 USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
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 > 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') show_tag("SEC_TYPE", "msdos");
172       while (len && s[len-1]==' ') len--;
173       if (strstart(&s, "NO NAME")) len=0;
174     }
175     // TODO: special case NTFS $VOLUME_NAME here...
176     if (len) {
177       if (!strcmp(type, "f2fs")) {
178         // Convert UTF16LE to ASCII by replacing non-ASCII with '?'.
179         // TODO: support non-ASCII.
180         for (j=0; j<len; j++) {
181           buf[j] = s[2*j];
182           if (s[2*j+1]) buf[j]='?';
183           if (!buf[j]) break;
184         }
185       } else sprintf(buf, "%.*s", len, s);
186       if (FLAG(L)) return flagshow(buf, name);
187       show_tag("LABEL", buf);
188     }
189   }
190 
191   len = fstypes[i].uuid_off;
192   if (!FLAG(L) && len) {
193     int uoff = len-off;
194 
195     // Assemble UUID with whatever size and set of dashes this filesystem uses
196     s = buf;
197     if (!strcmp(type, "ntfs")) {
198       for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
199     } else if (!strcmp(type, "vfat")) {
200         s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
201                      toybuf[uoff+1], toybuf[uoff]);
202     } else if (!strcmp(type, "iso9660")) {
203         s += sprintf(s, "%c%c%c%c-", toybuf[uoff], toybuf[uoff+1],
204                      toybuf[uoff+2], toybuf[uoff+3]);
205         s += sprintf(s, "%c%c-", toybuf[uoff+4], toybuf[uoff+5]);
206         s += sprintf(s, "%c%c-", toybuf[uoff+6], toybuf[uoff+7]);
207         s += sprintf(s, "%c%c-", toybuf[uoff+8], toybuf[uoff+9]);
208         s += sprintf(s, "%c%c-", toybuf[uoff+10], toybuf[uoff+11]);
209         s += sprintf(s, "%c%c-", toybuf[uoff+12], toybuf[uoff+13]);
210         s += sprintf(s, "%c%c", toybuf[uoff+14], toybuf[uoff+15]);
211     } else {
212       for (j = 0; j < 16; j++)
213         s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
214     }
215 
216     if (FLAG(U)) return flagshow(buf, name);
217     show_tag("UUID", buf);
218   }
219 
220   if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
221     show_tag("SEC_TYPE", "ext2");
222 
223   if (FLAG(U) || FLAG(L)) return;
224 
225   show_tag("TYPE", type);
226   if (!strcasecmp(TT.o, "full")) xputc('\n');
227 }
228 
blkid_main(void)229 void blkid_main(void)
230 {
231   if (!TT.o) TT.o = "full";
232 
233   if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
234   else {
235     unsigned int ma, mi, sz, fd;
236     char name[32], device[5+32];
237     FILE *fp = xfopen("/proc/partitions", "r");
238 
239     while (fgets(toybuf, sizeof(toybuf), fp)) {
240       if (sscanf(toybuf, " %u %u %u %31s", &ma, &mi, &sz, name) != 4)
241         continue;
242 
243       sprintf(device, "/dev/%.20s", name);
244       if (-1 == (fd = open(device, O_RDONLY))) {
245         if (errno != ENOMEDIUM) perror_msg_raw(device);
246       } else {
247         do_blkid(fd, device);
248         close(fd);
249       }
250     }
251     if (CFG_TOYBOX_FREE) fclose(fp);
252   }
253 
254   if (FLAG(L) || FLAG(U)) toys.exitval = 2;
255 }
256 
fstype_main(void)257 void fstype_main(void)
258 {
259   loopfiles(toys.optargs, do_blkid);
260 }
261