blkid.c 5.9 KB

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