aboutsummaryrefslogtreecommitdiff
path: root/toys/other/blkid.c
blob: 40391de495fee8ed149e01b91b41f5d1bbda4ee4 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* blkid.c - Prints type, label and UUID of filesystem(s).
 *
 * Copyright 2013 Brad Conroy <bconroy@uis.edu>
 *
 * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html
 * TODO: -U and -L should require arguments

USE_BLKID(NEWTOY(blkid, "ULs*[!LU]", TOYFLAG_BIN))
USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))

config BLKID
  bool "blkid"
  default y
  help
    usage: blkid [-s TAG] [-UL] DEV...

    Print type, label and UUID of filesystem on a block device or image.

    -U	Show UUID only (or device with that UUID)
    -L	Show LABEL only (or device with that LABEL)
    -s TAG	Only show matching tags (default all)

config FSTYPE
  bool "fstype"
  default y
  help
    usage: fstype DEV...

    Print type of filesystem on a block device or image.
*/

#define FOR_blkid
#include "toys.h"

GLOBALS(
  struct arg_list *s;
)

struct fstype {
  char *name;
  uint64_t magic;
  int magic_len, magic_offset, uuid_off, label_len, label_off;
} static const fstypes[] = {
  {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144}, // keep this first for ext3/4 check
  {"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
  // NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via
  // codepage, something called a uuid that's only 8 bytes long...
  {"ntfs", 0x5346544e, 4, 3, 0x48, 0, 0},

  {"adfs", 0xadf5, 2, 0xc00, 0,0,0},
  {"bfs", 0x1badface, 4, 0, 0,0,0},
  {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
  {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
  {"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c},
  {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
  {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
  {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
  {"reiserfs", 0x724573496552ULL, 6, 65588, 65620, 16, 65636},
  {"romfs", 0x2d6d6f72, 4, 0, 0,0,0},
  {"squashfs", 0x73717368, 4, 0, 0,0,0},
  {"xiafs", 0x012fd16d, 4, 572, 0,0,0},
  {"xfs", 0x42534658, 4, 0, 32, 12, 108},
  {"vfat", 0x3233544146ULL, 5, 82, 67, 11, 71},  // fat32
  {"vfat", 0x31544146, 4, 54, 39, 11, 43}     // fat1
};

static void show_tag(char *key, char *value)
{
  int show = 0;
  struct arg_list *al;

  if (TT.s) {
    for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1;
  } else show = 1;

  if (show) printf(" %s=\"%s\"", key, value);
}

static void flagshow(char *s, char *name)
{
  if (*toys.optargs && strcmp(s, *toys.optargs)) return;
  printf("%s\n", *toys.optargs ? name : s);
  if (*toys.optargs) xexit();
}

static void do_blkid(int fd, char *name)
{
  int off, i, j, len;
  char buf[128], *type, *s;

  off = i = 0;

  for (;;) {
    int pass = 0;

    // Read next block of data
    len = readall(fd, toybuf, sizeof(toybuf));
    if (len != sizeof(toybuf)) return;

    // Iterate through types in range
    for (i=0; i<ARRAY_LEN(fstypes); i++) {
      uint64_t test;

      // Skip tests not in this 4k block
      if (fstypes[i].magic_offset > off+sizeof(toybuf)) {
        pass++;
        continue;
      }
      if (fstypes[i].magic_offset < off) continue;

      // Populate 64 bit little endian magic value
      test = 0;
      for (j = 0; j < fstypes[i].magic_len; j++)
        test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j);
      if (test == fstypes[i].magic) break;
    }

    if (i == ARRAY_LEN(fstypes)) {
      off += len;
      if (pass) continue;
      return;
    }
    break;
  }

  // distinguish ext2/3/4
  type = fstypes[i].name;
  if (!i) {
    if (toybuf[1116]&4) type = "ext3";
    if (toybuf[1120]&64) type = "ext4";
  }

  // Output for fstype
  if (*toys.which->name == 'f') {
    puts(type);
    return;
  }

  // output for blkid
  if (!FLAG(L) && !FLAG(U)) printf("%s:",name);

  len = fstypes[i].label_len;
  if (!FLAG(U) && len) {
    s = toybuf+fstypes[i].label_off-off;
    if (!strcmp(type, "vfat")) {
      show_tag("SEC_TYPE", "msdos");
      while (len && s[len-1]==' ') len--;
      if (strstart(&s, "NO NAME")) len=0;
    }
    // TODO: special case NTFS $VOLUME_NAME here...
    if (len) {
      if (!strcmp(type, "f2fs")) {
        // Convert UTF16LE to ASCII by replacing non-ASCII with '?'.
        // TODO: support non-ASCII.
        for (j=0; j<len; j++) {
          buf[j] = s[2*j];
          if (s[2*j+1]) buf[j]='?';
          if (!buf[j]) break;
        }
      } else sprintf(buf, "%.*s", len, s);
      if (FLAG(L)) return flagshow(buf, name);
      show_tag("LABEL", buf);
    }
  }

  len = fstypes[i].uuid_off;
  if (!FLAG(L) && len) {
    int uoff = len-off;

    // Assemble UUID with whatever size and set of dashes this filesystem uses
    s = buf;
    if (!strcmp(type, "ntfs")) {
      for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
    } else if (!strcmp(type, "vfat")) {
        s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
                     toybuf[uoff+1], toybuf[uoff]);
    } else {
      for (j = 0; j < 16; j++)
        s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
    }

    if (FLAG(U)) return flagshow(buf, name);
    show_tag("UUID", buf);
  }

  if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
    show_tag("SEC_TYPE", "ext2");

  if (FLAG(U) || FLAG(L)) return;

  show_tag("TYPE", type);
  xputc('\n');
}

void blkid_main(void)
{
  if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid);
  else {
    unsigned int ma, mi, sz, fd;
    char *name = toybuf, *buffer = toybuf+1024, device[32];
    FILE *fp = xfopen("/proc/partitions", "r");

    while (fgets(buffer, 1024, fp)) {
      *name = 0;
      if (sscanf(buffer, " %u %u %u %[^\n ]", &ma, &mi, &sz, name) != 4)
        continue;

      sprintf(device, "/dev/%.20s", name);
      if (-1 == (fd = open(device, O_RDONLY))) {
        if (errno != ENOMEDIUM) perror_msg_raw(device);
      } else {
        do_blkid(fd, device);
        close(fd);
      }
    }
    if (CFG_TOYBOX_FREE) fclose(fp);
  }

  if (FLAG(L) || FLAG(U)) toys.exitval = 2;
}

void fstype_main(void)
{
  loopfiles(toys.optargs, do_blkid);
}