toybox/toys/other/blkid.c
<<
>>
Prefs
   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
   8USE_BLKID(NEWTOY(blkid, "ULo:s*[!LU]", TOYFLAG_BIN))
   9USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
  10
  11config BLKID
  12  bool "blkid"
  13  default y
  14  help
  15    usage: blkid [-o TYPE] [-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    -o TYPE     Output format (full, value, export)
  22    -s TAG      Only show matching tags (default all)
  23
  24config FSTYPE
  25  bool "fstype"
  26  default y
  27  help
  28    usage: fstype DEV...
  29
  30    Print type of filesystem on a block device or image.
  31*/
  32
  33#define FOR_blkid
  34#include "toys.h"
  35
  36GLOBALS(
  37  struct arg_list *s;
  38  char *o;
  39)
  40
  41struct fstype {
  42  char *name;
  43  uint64_t magic;
  44  int magic_len, magic_offset, uuid_off, label_len, label_off;
  45} static const fstypes[] = {
  46  {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144}, // keep this first for ext3/4 check
  47  {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
  48  // NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via
  49  // codepage, something called a uuid that's only 8 bytes long...
  50  {"ntfs", 0x5346544e, 4, 3, 0x48, 0, 0},
  51
  52  {"adfs", 0xadf5, 2, 0xc00, 0,0,0},
  53  {"bfs", 0x1badface, 4, 0, 0,0,0},
  54  {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
  55  {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
  56  {"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c},
  57  {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
  58  {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
  59  {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
  60  {"reiserfs", 0x724573496552ULL, 6, 65588, 65620, 16, 65636},
  61  {"romfs", 0x2d6d6f72, 4, 0, 0,0,0},
  62  {"squashfs", 0x73717368, 4, 0, 0,0,0},
  63  {"xiafs", 0x012fd16d, 4, 572, 0,0,0},
  64  {"xfs", 0x42534658, 4, 0, 32, 12, 108},
  65  {"vfat", 0x3233544146ULL, 5, 82, 67, 11, 71},  // fat32
  66  {"vfat", 0x31544146, 4, 54, 39, 11, 43}     // fat1
  67};
  68
  69static void escape(char *str, int force)
  70{
  71  if (!force && str[strcspn(str, "\" \\\n\t$<>|&;`'~()!#?")]) force++;
  72  if (!force) return xputsn(str);
  73
  74  putchar('"');
  75  while (*str) {
  76    if (strchr("\" \\", *str)) putchar('\\');
  77    putchar(*str++);
  78  }
  79  putchar('"');
  80}
  81
  82static void show_tag(char *key, char *value)
  83{
  84  int show = 0;
  85  struct arg_list *al;
  86
  87  if (TT.s) {
  88    for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1;
  89  } else show = 1;
  90
  91  if (!show || !*value) return;
  92  if (!strcasecmp(TT.o, "full") || !strcasecmp(TT.o, "export")) {
  93    printf(" %s="+!(*TT.o=='f'), key);
  94    escape(value, *TT.o=='f');
  95    if (*TT.o=='e') xputc('\n');
  96  } else if (!strcasecmp(TT.o, "value")) xputs(value);
  97  else error_exit("bad -o %s", TT.o);
  98}
  99
 100static void flagshow(char *s, char *name)
 101{
 102  if (*toys.optargs && strcmp(s, *toys.optargs)) return;
 103  printf("%s\n", *toys.optargs ? name : s);
 104  if (*toys.optargs) xexit();
 105}
 106
 107static void do_blkid(int fd, char *name)
 108{
 109  int off, i, j, len;
 110  char buf[128], *type, *s;
 111
 112  off = i = 0;
 113
 114  for (;;) {
 115    int pass = 0;
 116
 117    // Read next block of data
 118    len = readall(fd, toybuf, sizeof(toybuf));
 119    if (len != sizeof(toybuf)) return;
 120
 121    // Iterate through types in range
 122    for (i=0; i<ARRAY_LEN(fstypes); i++) {
 123      uint64_t test;
 124
 125      // Skip tests not in this 4k block
 126      if (fstypes[i].magic_offset > off+sizeof(toybuf)) {
 127        pass++;
 128        continue;
 129      }
 130      if (fstypes[i].magic_offset < off) continue;
 131
 132      // Populate 64 bit little endian magic value
 133      test = 0;
 134      for (j = 0; j < fstypes[i].magic_len; j++)
 135        test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j);
 136      if (test == fstypes[i].magic) break;
 137    }
 138
 139    if (i == ARRAY_LEN(fstypes)) {
 140      off += len;
 141      if (pass) continue;
 142      return;
 143    }
 144    break;
 145  }
 146
 147  // distinguish ext2/3/4
 148  type = fstypes[i].name;
 149  if (!i) {
 150    if (toybuf[1116]&4) type = "ext3";
 151    if (toybuf[1120]&64) type = "ext4";
 152  }
 153
 154  // Output for fstype
 155  if (*toys.which->name == 'f') {
 156    puts(type);
 157    return;
 158  }
 159
 160  // output for blkid
 161  if (!FLAG(L) && !FLAG(U)) {
 162    if (!TT.o || !strcasecmp(TT.o, "full")) printf("%s:", name);
 163    else if (!strcasecmp(TT.o, "export")) show_tag("DEVNAME", name);
 164  }
 165
 166  len = fstypes[i].label_len;
 167  if (!FLAG(U) && len) {
 168    s = toybuf+fstypes[i].label_off-off;
 169    if (!strcmp(type, "vfat")) {
 170      show_tag("SEC_TYPE", "msdos");
 171      while (len && s[len-1]==' ') len--;
 172      if (strstart(&s, "NO NAME")) len=0;
 173    }
 174    // TODO: special case NTFS $VOLUME_NAME here...
 175    if (len) {
 176      if (!strcmp(type, "f2fs")) {
 177        // Convert UTF16LE to ASCII by replacing non-ASCII with '?'.
 178        // TODO: support non-ASCII.
 179        for (j=0; j<len; j++) {
 180          buf[j] = s[2*j];
 181          if (s[2*j+1]) buf[j]='?';
 182          if (!buf[j]) break;
 183        }
 184      } else sprintf(buf, "%.*s", len, s);
 185      if (FLAG(L)) return flagshow(buf, name);
 186      show_tag("LABEL", buf);
 187    }
 188  }
 189
 190  len = fstypes[i].uuid_off;
 191  if (!FLAG(L) && len) {
 192    int uoff = len-off;
 193
 194    // Assemble UUID with whatever size and set of dashes this filesystem uses
 195    s = buf;
 196    if (!strcmp(type, "ntfs")) {
 197      for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
 198    } else if (!strcmp(type, "vfat")) {
 199        s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
 200                     toybuf[uoff+1], toybuf[uoff]);
 201    } else {
 202      for (j = 0; j < 16; j++)
 203        s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
 204    }
 205
 206    if (FLAG(U)) return flagshow(buf, name);
 207    show_tag("UUID", buf);
 208  }
 209
 210  if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
 211    show_tag("SEC_TYPE", "ext2");
 212
 213  if (FLAG(U) || FLAG(L)) return;
 214
 215  show_tag("TYPE", type);
 216  if (!strcasecmp(TT.o, "full")) xputc('\n');
 217}
 218
 219void blkid_main(void)
 220{
 221  if (!TT.o) TT.o = "full";
 222
 223  if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
 224  else {
 225    unsigned int ma, mi, sz, fd;
 226    char name[32], device[5+32];
 227    FILE *fp = xfopen("/proc/partitions", "r");
 228
 229    while (fgets(toybuf, sizeof(toybuf), fp)) {
 230      if (sscanf(toybuf, " %u %u %u %31s", &ma, &mi, &sz, name) != 4)
 231        continue;
 232
 233      sprintf(device, "/dev/%.20s", name);
 234      if (-1 == (fd = open(device, O_RDONLY))) {
 235        if (errno != ENOMEDIUM) perror_msg_raw(device);
 236      } else {
 237        do_blkid(fd, device);
 238        close(fd);
 239      }
 240    }
 241    if (CFG_TOYBOX_FREE) fclose(fp);
 242  }
 243
 244  if (FLAG(L) || FLAG(U)) toys.exitval = 2;
 245}
 246
 247void fstype_main(void)
 248{
 249  loopfiles(toys.optargs, do_blkid);
 250}
 251