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