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 
7 USE_BLKID(NEWTOY(blkid, "UL[!LU]", TOYFLAG_BIN))
8 USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
9 
10 config BLKID
11   bool "blkid"
12   default y
13   help
14     usage: blkid [-UL] DEV...
15 
16     Print type, label and UUID of filesystem on a block device or image.
17 
18     -U	Show UUID only (or device with that UUID)
19     -L	Show LABEL only (or device with that LABEL)
20 
21 config FSTYPE
22   bool "fstype"
23   default y
24   help
25     usage: fstype DEV...
26 
27     Print type of filesystem on a block device or image.
28 */
29 
30 #define FOR_blkid
31 #include "toys.h"
32 
33 struct fstype {
34   char *name;
35   uint64_t magic;
36   int magic_len, magic_offset, uuid_off, label_len, label_off;
37 } static const fstypes[] = {
38   {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144}, // keep this first for ext3/4 check
39   {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
40   // NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via
41   // codepage, something called a uuid that's only 8 bytes long...
42   {"ntfs", 0x5346544e, 4, 3, 0x48+(8<<24), 0, 0},
43 
44   {"adfs", 0xadf5, 2, 0xc00, 0,0,0},
45   {"bfs", 0x1badface, 4, 0, 0,0,0},
46   {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
47   {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
48   {"f2fs", 0xF2F52010, 4, 1024, 1132, 16, 1110},
49   {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
50   {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
51   {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
52   {"reiserfs", 0x724573496552ULL, 6, 65588, 65620, 16, 65636},
53   {"romfs", 0x2d6d6f72, 4, 0, 0,0,0},
54   {"squashfs", 0x73717368, 4, 0, 0,0,0},
55   {"xiafs", 0x012fd16d, 4, 572, 0,0,0},
56   {"xfs", 0x42534658, 4, 0, 32, 12, 108},
57   {"vfat", 0x3233544146ULL, 5, 82, 67+(4<<24), 11, 71},  // fat32
58   {"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43}     // fat1
59 };
60 
flagshow(char * s,char * name)61 static void flagshow(char *s, char *name)
62 {
63   if (*toys.optargs && strcmp(s, *toys.optargs)) return;
64   printf("%s\n", *toys.optargs ? name : s);
65   if (*toys.optargs) xexit();
66 }
67 
do_blkid(int fd,char * name)68 static void do_blkid(int fd, char *name)
69 {
70   int off, i, j, len;
71   char buf[128], *type, *s;
72 
73   off = i = 0;
74 
75   for (;;) {
76     int pass = 0;
77 
78     // Read next block of data
79     len = readall(fd, toybuf, sizeof(toybuf));
80     if (len != sizeof(toybuf)) return;
81 
82     // Iterate through types in range
83     for (i=0; i<ARRAY_LEN(fstypes); i++) {
84       uint64_t test;
85 
86       // Skip tests not in this 4k block
87       if (fstypes[i].magic_offset > off+sizeof(toybuf)) {
88         pass++;
89         continue;
90       }
91       if (fstypes[i].magic_offset < off) continue;
92 
93       // Populate 64 bit little endian magic value
94       test = 0;
95       for (j = 0; j < fstypes[i].magic_len; j++)
96         test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j);
97       if (test == fstypes[i].magic) break;
98     }
99 
100     if (i == ARRAY_LEN(fstypes)) {
101       off += len;
102       if (pass) continue;
103       return;
104     }
105     break;
106   }
107 
108   // distinguish ext2/3/4
109   type = fstypes[i].name;
110   if (!i) {
111     if (toybuf[1116]&4) type = "ext3";
112     if (toybuf[1120]&64) type = "ext4";
113   }
114 
115   // Could special case NTFS here...
116 
117   // Output for fstype
118   if (*toys.which->name == 'f') {
119     puts(type);
120     return;
121   }
122 
123   // output for blkid
124   if (!toys.optflags) printf("%s:",name);
125 
126   len = fstypes[i].label_len;
127   if (!FLAG(U) && len) {
128     s = toybuf+fstypes[i].label_off-off;
129     if (!strcmp(type, "vfat")) {
130       while (len && s[len-1]==' ') len--;
131       if (strstart(&s, "NO NAME")) len=0;
132     }
133     if (len && *s) {
134       sprintf(buf, "%.*s", len, s);
135       if (FLAG(L)) return flagshow(buf, name);
136       printf(" LABEL=\"%s\"", buf);
137     }
138   }
139 
140   len = fstypes[i].uuid_off;
141   if (!FLAG(L) && len) {
142     int bits = 0x550, size = len >> 24, uoff = (len&((1<<24)-1))-off;
143 
144     // Assemble UUID with whatever size and set of dashes this filesystem uses
145     if (size) bits = 4*(size == 4);
146     else size = 16;
147     for (j = 0, s = buf; j < size; j++)
148       s += sprintf(s, "-%02x"+!(bits & (1<<j)), toybuf[uoff+j]);
149 
150     if (FLAG(U)) return flagshow(buf, name);
151     printf(" UUID=\"%s\"", buf);
152   }
153 
154   if (toys.optflags) return;
155 
156   printf(" TYPE=\"%s\"\n", type);
157 }
158 
blkid_main(void)159 void blkid_main(void)
160 {
161   if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
162   else {
163     unsigned int ma, mi, sz, fd;
164     char *name = toybuf, *buffer = toybuf+1024, device[32];
165     FILE *fp = xfopen("/proc/partitions", "r");
166 
167     while (fgets(buffer, 1024, fp)) {
168       *name = 0;
169       if (sscanf(buffer, " %u %u %u %[^\n ]", &ma, &mi, &sz, name) != 4)
170         continue;
171 
172       sprintf(device, "/dev/%.20s", name);
173       if (-1 == (fd = open(device, O_RDONLY))) {
174         if (errno != ENOMEDIUM) perror_msg_raw(device);
175       } else {
176         do_blkid(fd, device);
177         close(fd);
178       }
179     }
180     if (CFG_TOYBOX_FREE) fclose(fp);
181   }
182 
183   if (FLAG(L) || FLAG(U)) toys.exitval = 2;
184 }
185 
fstype_main(void)186 void fstype_main(void)
187 {
188   loopfiles(toys.optargs, do_blkid);
189 }
190