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#define FOR_blkid
31#include "toys.h"
32
33struct 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},
39 {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
40
41
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},
58 {"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43}
59};
60
61static 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
68static 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
79 len = readall(fd, toybuf, sizeof(toybuf));
80 if (len != sizeof(toybuf)) return;
81
82
83 for (i=0; i<ARRAY_LEN(fstypes); i++) {
84 uint64_t test;
85
86
87 if (fstypes[i].magic_offset > off+sizeof(toybuf)) {
88 pass++;
89 continue;
90 }
91 if (fstypes[i].magic_offset < off) continue;
92
93
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
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
116
117
118 if (*toys.which->name == 'f') {
119 puts(type);
120 return;
121 }
122
123
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
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
159void 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
186void fstype_main(void)
187{
188 loopfiles(toys.optargs, do_blkid);
189}
190