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, "ULs*[!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 [-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 -s TAG Only show matching tags (default all) 22 23 config FSTYPE 24 bool "fstype" 25 default y 26 help 27 usage: fstype DEV... 28 29 Print type of filesystem on a block device or image. 30 */ 31 32 #define FOR_blkid 33 #include "toys.h" 34 35 GLOBALS( 36 struct arg_list *s; 37 ) 38 39 struct 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}, // keep this first for ext3/4 check 45 {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052}, 46 // NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via 47 // codepage, something called a uuid that's only 8 bytes long... 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}, // fat32 64 {"vfat", 0x31544146, 4, 54, 39, 11, 43} // fat1 65 }; 66 67 static 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) printf(" %s=\"%s\"", key, value); 77 } 78 79 static 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 86 static 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 // Read next block of data 97 len = readall(fd, toybuf, sizeof(toybuf)); 98 if (len != sizeof(toybuf)) return; 99 100 // Iterate through types in range 101 for (i=0; i<ARRAY_LEN(fstypes); i++) { 102 uint64_t test; 103 104 // Skip tests not in this 4k block 105 if (fstypes[i].magic_offset > off+sizeof(toybuf)) { 106 pass++; 107 continue; 108 } 109 if (fstypes[i].magic_offset < off) continue; 110 111 // Populate 64 bit little endian magic value 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 // distinguish ext2/3/4 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 // Output for fstype 134 if (*toys.which->name == 'f') { 135 puts(type); 136 return; 137 } 138 139 // output for blkid 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 // TODO: special case NTFS $VOLUME_NAME here... 151 if (len) { 152 if (!strcmp(type, "f2fs")) { 153 // Convert UTF16LE to ASCII by replacing non-ASCII with '?'. 154 // TODO: support non-ASCII. 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 // Assemble UUID with whatever size and set of dashes this filesystem uses 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 195 void 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 = toybuf, *buffer = toybuf+1024, device[32]; 201 FILE *fp = xfopen("/proc/partitions", "r"); 202 203 while (fgets(buffer, 1024, fp)) { 204 *name = 0; 205 if (sscanf(buffer, " %u %u %u %[^\n ]", &ma, &mi, &sz, name) != 4) 206 continue; 207 208 sprintf(device, "/dev/%.20s", name); 209 if (-1 == (fd = open(device, O_RDONLY))) { 210 if (errno != ENOMEDIUM) perror_msg_raw(device); 211 } else { 212 do_blkid(fd, device); 213 close(fd); 214 } 215 } 216 if (CFG_TOYBOX_FREE) fclose(fp); 217 } 218 219 if (FLAG(L) || FLAG(U)) toys.exitval = 2; 220 } 221 222 void fstype_main(void) 223 { 224 loopfiles(toys.optargs, do_blkid); 225 } 226