1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#define FOR_blkid
33#include "toys.h"
34
35GLOBALS(
36 struct arg_list *s;
37)
38
39struct fstype {
40 char *name;
41 uint64_t magic;
42 int magic_len, magic_offset, uuid_off, label_len, label_off;
43} static const fstypes[] = {
44 {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144},
45 {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
46
47
48 {"ntfs", 0x5346544e, 4, 3, 0x48, 0, 0},
49
50 {"adfs", 0xadf5, 2, 0xc00, 0,0,0},
51 {"bfs", 0x1badface, 4, 0, 0,0,0},
52 {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
53 {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
54 {"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c},
55 {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
56 {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
57 {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
58 {"reiserfs", 0x724573496552ULL, 6, 65588, 65620, 16, 65636},
59 {"romfs", 0x2d6d6f72, 4, 0, 0,0,0},
60 {"squashfs", 0x73717368, 4, 0, 0,0,0},
61 {"xiafs", 0x012fd16d, 4, 572, 0,0,0},
62 {"xfs", 0x42534658, 4, 0, 32, 12, 108},
63 {"vfat", 0x3233544146ULL, 5, 82, 67, 11, 71},
64 {"vfat", 0x31544146, 4, 54, 39, 11, 43}
65};
66
67static void show_tag(char *key, char *value)
68{
69 int show = 0;
70 struct arg_list *al;
71
72 if (TT.s) {
73 for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1;
74 } else show = 1;
75
76 if (show && *value) printf(" %s=\"%s\"", key, value);
77}
78
79static void flagshow(char *s, char *name)
80{
81 if (*toys.optargs && strcmp(s, *toys.optargs)) return;
82 printf("%s\n", *toys.optargs ? name : s);
83 if (*toys.optargs) xexit();
84}
85
86static void do_blkid(int fd, char *name)
87{
88 int off, i, j, len;
89 char buf[128], *type, *s;
90
91 off = i = 0;
92
93 for (;;) {
94 int pass = 0;
95
96
97 len = readall(fd, toybuf, sizeof(toybuf));
98 if (len != sizeof(toybuf)) return;
99
100
101 for (i=0; i<ARRAY_LEN(fstypes); i++) {
102 uint64_t test;
103
104
105 if (fstypes[i].magic_offset > off+sizeof(toybuf)) {
106 pass++;
107 continue;
108 }
109 if (fstypes[i].magic_offset < off) continue;
110
111
112 test = 0;
113 for (j = 0; j < fstypes[i].magic_len; j++)
114 test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j);
115 if (test == fstypes[i].magic) break;
116 }
117
118 if (i == ARRAY_LEN(fstypes)) {
119 off += len;
120 if (pass) continue;
121 return;
122 }
123 break;
124 }
125
126
127 type = fstypes[i].name;
128 if (!i) {
129 if (toybuf[1116]&4) type = "ext3";
130 if (toybuf[1120]&64) type = "ext4";
131 }
132
133
134 if (*toys.which->name == 'f') {
135 puts(type);
136 return;
137 }
138
139
140 if (!FLAG(L) && !FLAG(U)) printf("%s:",name);
141
142 len = fstypes[i].label_len;
143 if (!FLAG(U) && len) {
144 s = toybuf+fstypes[i].label_off-off;
145 if (!strcmp(type, "vfat")) {
146 show_tag("SEC_TYPE", "msdos");
147 while (len && s[len-1]==' ') len--;
148 if (strstart(&s, "NO NAME")) len=0;
149 }
150
151 if (len) {
152 if (!strcmp(type, "f2fs")) {
153
154
155 for (j=0; j<len; j++) {
156 buf[j] = s[2*j];
157 if (s[2*j+1]) buf[j]='?';
158 if (!buf[j]) break;
159 }
160 } else sprintf(buf, "%.*s", len, s);
161 if (FLAG(L)) return flagshow(buf, name);
162 show_tag("LABEL", buf);
163 }
164 }
165
166 len = fstypes[i].uuid_off;
167 if (!FLAG(L) && len) {
168 int uoff = len-off;
169
170
171 s = buf;
172 if (!strcmp(type, "ntfs")) {
173 for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
174 } else if (!strcmp(type, "vfat")) {
175 s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
176 toybuf[uoff+1], toybuf[uoff]);
177 } else {
178 for (j = 0; j < 16; j++)
179 s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
180 }
181
182 if (FLAG(U)) return flagshow(buf, name);
183 show_tag("UUID", buf);
184 }
185
186 if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
187 show_tag("SEC_TYPE", "ext2");
188
189 if (FLAG(U) || FLAG(L)) return;
190
191 show_tag("TYPE", type);
192 xputc('\n');
193}
194
195void blkid_main(void)
196{
197 if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
198 else {
199 unsigned int ma, mi, sz, fd;
200 char name[32], device[5+32];
201 FILE *fp = xfopen("/proc/partitions", "r");
202
203 while (fgets(toybuf, sizeof(toybuf), fp)) {
204 if (sscanf(toybuf, " %u %u %u %31s", &ma, &mi, &sz, name) != 4)
205 continue;
206
207 sprintf(device, "/dev/%.20s", name);
208 if (-1 == (fd = open(device, O_RDONLY))) {
209 if (errno != ENOMEDIUM) perror_msg_raw(device);
210 } else {
211 do_blkid(fd, device);
212 close(fd);
213 }
214 }
215 if (CFG_TOYBOX_FREE) fclose(fp);
216 }
217
218 if (FLAG(L) || FLAG(U)) toys.exitval = 2;
219}
220
221void fstype_main(void)
222{
223 loopfiles(toys.optargs, do_blkid);
224}
225