aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

diskutil.c (15136B)


      1 /* see LICENSE file for copyright and license details */
      2 #include "../diskutil.h"
      3 #include "../util.h"
      4 
      5 #include <errno.h>
      6 #include <fcntl.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <sys/stat.h>
     11 #include <sys/types.h>
     12 #include <unistd.h>
     13 
     14 int
     15 blockdev_open(struct BlockDev *dev, const char *path, int writable)
     16 {
     17   struct stat st;
     18   int         fd, flags;
     19 
     20   flags = writable ? O_RDWR : O_RDONLY;
     21   fd    = open(path, flags);
     22   if (fd < 0)
     23     return -1;
     24 
     25   if (fstat(fd, &st) < 0) {
     26     close(fd);
     27     return -1;
     28   }
     29 
     30   dev->fd       = fd;
     31   dev->size     = st.st_size;
     32   dev->sec_size = 512;
     33   dev->dirty    = 0;
     34 
     35   if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
     36     blockdev_sys_size(dev);
     37 
     38   return 0;
     39 }
     40 
     41 int
     42 blockdev_read(struct BlockDev *dev, uint64_t sector, void *buf, size_t sector_count)
     43 {
     44   off_t   offset = (off_t)sector * dev->sec_size;
     45   size_t  size   = sector_count * dev->sec_size;
     46   ssize_t n;
     47 
     48   if (lseek(dev->fd, offset, SEEK_SET) == (off_t)-1)
     49     return -1;
     50 
     51   n = read(dev->fd, buf, size);
     52   if (n < 0 || (size_t)n != size)
     53     return -1;
     54 
     55   return 0;
     56 }
     57 
     58 int
     59 blockdev_write(struct BlockDev *dev, uint64_t sector, const void *buf, size_t sector_count)
     60 {
     61   off_t   offset = (off_t)sector * dev->sec_size;
     62   size_t  size   = sector_count * dev->sec_size;
     63   ssize_t n;
     64 
     65   if (lseek(dev->fd, offset, SEEK_SET) == (off_t)-1)
     66     return -1;
     67 
     68   n = write(dev->fd, buf, size);
     69   if (n < 0 || (size_t)n != size)
     70     return -1;
     71   dev->dirty = 1;
     72 
     73   return 0;
     74 }
     75 
     76 void
     77 blockdev_close(struct BlockDev *dev)
     78 {
     79   if (dev->dirty && dev->fd >= 0)
     80     blockdev_sys_reread(dev);
     81   if (dev->fd >= 0) {
     82     close(dev->fd);
     83     dev->fd = -1;
     84   }
     85 }
     86 
     87 struct BlockDevInfo *
     88 blockdev_probe_list(void)
     89 {
     90   struct BlockDevInfo *head = NULL;
     91   struct BlockDevInfo *tail = NULL;
     92   struct BlockDevInfo *info;
     93   struct BlockDev      dev;
     94   char                 devname[16];
     95   char                 devpath[128];
     96   char                 c;
     97 
     98   for (c = 'a'; c <= 'z'; c++) {
     99     snprintf(devname, sizeof(devname), "sd%c", c);
    100     snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, devname);
    101     if (blockdev_open(&dev, devpath, 0) == 0) {
    102       info = ecalloc(1, sizeof(*info));
    103       strlcpy(info->name, devname, sizeof(info->name));
    104       snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, devname);
    105       strlcpy(info->type, "disk", sizeof(info->type));
    106       info->size = dev.size;
    107       if (!head) {
    108         head = info;
    109       } else {
    110         tail->next = info;
    111       }
    112       tail = info;
    113       blockdev_close(&dev);
    114     }
    115   }
    116   return head;
    117 }
    118 
    119 void
    120 blockdev_free_list(struct BlockDevInfo *list)
    121 {
    122   struct BlockDevInfo *curr = list;
    123   while (curr) {
    124     struct BlockDevInfo *next = curr->next;
    125     struct BlockDevInfo *part = curr->parts;
    126     while (part) {
    127       struct BlockDevInfo *next_part = part->next;
    128       free(part);
    129       part = next_part;
    130     }
    131     free(curr);
    132     curr = next;
    133   }
    134 }
    135 
    136 struct FsType {
    137   const char *name;
    138   size_t      magic_offset;
    139   size_t      magic_len;
    140   uint64_t    magic1;
    141   uint64_t    magic2;
    142   size_t      uuid_offset;
    143   size_t      uuid_len;
    144   size_t      label_offset;
    145   size_t      label_len;
    146 };
    147 
    148 static const struct FsType fstypes[] = {
    149     /* ext2/3/4 */
    150     {"ext", 1080, 2, 0xEF53, 0x53EF, 1128, 16, 1144, 16},
    151     /* vfat / fat32 */
    152     {"vfat", 82, 5, 0x3233544146ULL, 0, 67, 4, 71, 11},
    153     /* vfat / fat12/16 */
    154     {"vfat", 54, 4, 0x31544146, 0, 39, 4, 43, 11},
    155     /* ntfs */
    156     {"ntfs", 3, 4, 0x5346544e, 0, 72, 8, 0, 0},
    157     /* exfat: its label is an entry of the root folder, not in the boot sector */
    158     {"exfat", 3, 8, 0x2020205441465845ULL, 0, 100, 4, 0, 0},
    159     /* iso9660: "CD001" at the primary volume descriptor, LBA 16 */
    160     {"iso9660", 32769, 5, 0x3130304443ULL, 0, 0, 0, 32808, 32},
    161     /* xfs */
    162     {"xfs", 0, 4, 0x42534658, 0x58465342, 32, 16, 108, 12},
    163     /* btrfs */
    164     {"btrfs", 65600, 8, 0x4D5F53665248425FULL, 0, 65803, 16, 65819, 256},
    165     /* f2fs */
    166     {"f2fs", 1024, 4, 0xF2F52010, 0x1020F5F2, 1132, 16, 1148, 512},
    167     /* ufs1 */
    168     {"ufs1", 9564, 4, 0x011954, 0x54190100, 8428, 8, 0, 0},
    169     /* ufs2 */
    170     {"ufs2", 66908, 4, 0x19540119, 0x19015419, 65772, 8, 66216, 32},
    171     /* fossil */
    172     {"fossil", 131200, 4, 0x2340A3B1, 0xB1A34023, 0, 0, 131072, 127},
    173 };
    174 
    175 static void
    176 format_uuid(const unsigned char *buf, size_t len, const char *type, char *out, size_t out_len)
    177 {
    178   size_t i;
    179   char  *p;
    180 
    181   p = out;
    182   if (strcmp(type, "vfat") == 0 || strcmp(type, "exfat") == 0) {
    183     snprintf(out, out_len, "%02X%02X-%02X%02X", buf[3], buf[2], buf[1], buf[0]);
    184     return;
    185   }
    186 
    187   if (strcmp(type, "ntfs") == 0) {
    188     for (i = 8; i > 0; i--) {
    189       snprintf(p, out_len - (p - out), "%02X", buf[i - 1]);
    190       p += 2;
    191     }
    192     return;
    193   }
    194 
    195   for (i = 0; i < len; i++) {
    196     if (i == 4 || i == 6 || i == 8 || i == 10) {
    197       *p++ = '-';
    198     }
    199     snprintf(p, out_len - (p - out), "%02x", buf[i]);
    200     p += 2;
    201   }
    202   *p = '\0';
    203 }
    204 
    205 static int
    206 zfs_detect(const unsigned char *buf, size_t len)
    207 {
    208   size_t   offset;
    209   uint64_t magic;
    210 
    211   for (offset = 131072; offset + 8 <= len; offset += 1024) {
    212     magic = ((uint64_t)buf[offset + 7] << 56) | ((uint64_t)buf[offset + 6] << 48)
    213             | ((uint64_t)buf[offset + 5] << 40) | ((uint64_t)buf[offset + 4] << 32)
    214             | ((uint64_t)buf[offset + 3] << 24) | ((uint64_t)buf[offset + 2] << 16)
    215             | ((uint64_t)buf[offset + 1] << 8) | (uint64_t)buf[offset];
    216 
    217     if (magic == 0x00bab10c00000000ULL || magic == 0x0cb1ba0000000000ULL || magic == 0x00bab10cULL
    218         || magic == 0x0cb1ba00ULL)
    219       return 1;
    220   }
    221   return 0;
    222 }
    223 
    224 struct Plan9FsDef {
    225   const char *name;
    226   int         blocksize;
    227   int         namelen;
    228   int         dirblks;
    229   int         indirblks;
    230   int         daddrbits;
    231 };
    232 
    233 static const struct Plan9FsDef p9_fs_defs[] = {
    234     {"cwfs64x", 16384, 144, 6, 4, 64},
    235     {"cwfs64", 16384, 56, 6, 4, 64},
    236     {"cwfs", 16384, 28, 6, 2, 32},
    237     {"fs64", 8192, 56, 6, 4, 64},
    238     {"fs", 4096, 28, 6, 2, 32},
    239 };
    240 
    241 static int
    242 plan9_magic_check(const unsigned char *p, const char **name)
    243 {
    244   if (memcmp(p, "\x01\x1c\xe5\x0d", 4) == 0 || memcmp(p, "\x0d\xe5\x1c\x01", 4) == 0) {
    245     *name = "hjfs";
    246     return 1;
    247   }
    248   if (memcmp(p, "gefs", 4) == 0 || memcmp(p, "sfeg", 4) == 0) {
    249     *name = "gefs";
    250     return 1;
    251   }
    252   return 0;
    253 }
    254 
    255 static int
    256 plan9_detect_other(const unsigned char *buf, size_t len, const char **name)
    257 {
    258   if (len >= 4 && plan9_magic_check(buf, name))
    259     return 1;
    260   if (len >= 516 && plan9_magic_check(buf + 512, name))
    261     return 1;
    262   return 0;
    263 }
    264 
    265 static int
    266 plan9_detect_text(const unsigned char *buf, size_t len, const char **name)
    267 {
    268   int    blocksize = 0, namelen = 0, dirblks = 0, indirblks = 0, daddrbits = 0;
    269   char   line[128];
    270   char   key[64];
    271   int    val;
    272   size_t i = 0, j;
    273 
    274   if (len >= 256 + 15) {
    275     if (memcmp(buf + 256, "kfs wren device", 15) == 0) {
    276       *name = "kfs";
    277       return 1;
    278     }
    279   }
    280 
    281   if (len < 512)
    282     return 0;
    283   i = 512;
    284   if (len > 1536)
    285     len = 1536;
    286 
    287   while (i < len) {
    288     size_t k = 0;
    289     while (i < len && buf[i] != '\n' && k < sizeof(line) - 1) {
    290       line[k++] = buf[i++];
    291     }
    292     if (i < len && buf[i] == '\n')
    293       i++;
    294     line[k] = '\0';
    295 
    296     if (sscanf(line, "%63s %d", key, &val) == 2) {
    297       if (strcmp(key, "blocksize") == 0)
    298         blocksize = val;
    299       else if (strcmp(key, "namelen") == 0)
    300         namelen = val;
    301       else if (strcmp(key, "dirblks") == 0)
    302         dirblks = val;
    303       else if (strcmp(key, "indirblks") == 0)
    304         indirblks = val;
    305       else if (strcmp(key, "daddrbits") == 0)
    306         daddrbits = val;
    307     }
    308   }
    309 
    310   for (j = 0; j < LEN(p9_fs_defs); j++) {
    311     if (blocksize == p9_fs_defs[j].blocksize && namelen == p9_fs_defs[j].namelen
    312         && dirblks == p9_fs_defs[j].dirblks && indirblks == p9_fs_defs[j].indirblks
    313         && daddrbits == p9_fs_defs[j].daddrbits) {
    314       *name = p9_fs_defs[j].name;
    315       return 1;
    316     }
    317   }
    318 
    319   return 0;
    320 }
    321 
    322 /* utf-16 little end first, n units, written as utf-8; returns its length */
    323 static size_t
    324 utf16_to_utf8(const unsigned char *p, size_t n, char *out, size_t sz)
    325 {
    326   size_t used = 0;
    327 
    328   for (size_t k = 0; k < n; k++) {
    329     unsigned c = p[2 * k] | p[2 * k + 1] << 8;
    330 
    331     if (c < 0x80 && used + 1 < sz) {
    332       out[used++] = (char)c;
    333     } else if (c >= 0x80 && c < 0x800 && used + 2 < sz) {
    334       out[used++] = (char)(0xC0 | c >> 6);
    335       out[used++] = (char)(0x80 | (c & 0x3F));
    336     } else if (c >= 0x800 && (c < 0xD800 || c > 0xDFFF) && used + 3 < sz) {
    337       out[used++] = (char)(0xE0 | c >> 12);
    338       out[used++] = (char)(0x80 | (c >> 6 & 0x3F));
    339       out[used++] = (char)(0x80 | (c & 0x3F));
    340     }
    341   }
    342   out[used] = '\0';
    343   return used;
    344 }
    345 
    346 /* exfat keeps its label as an entry of the root folder: the folder's
    347  * first cluster is read and its label entry, utf-16, written as utf-8 */
    348 #define EXFAT_ENTRY_LABEL 0x83
    349 #define EXFAT_LABEL_MAX   11
    350 
    351 static uint32_t
    352 le32(const unsigned char *p)
    353 {
    354   return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
    355 }
    356 
    357 static void
    358 exfat_label(struct BlockDev *dev, const unsigned char *boot, char *label, size_t label_sz)
    359 {
    360   unsigned char *dir, *e;
    361   uint32_t       heap   = le32(boot + 88);
    362   uint32_t       root   = le32(boot + 96);
    363   unsigned       sshift = boot[108], cshift = boot[109];
    364   size_t         csize, i, used = 0;
    365   uint64_t       sector;
    366 
    367   label[0] = '\0';
    368   if (sshift < 9 || sshift > 12 || cshift > 25 - sshift || root < 2
    369       || (1u << sshift) != dev->sec_size)
    370     return;
    371   csize  = (size_t)1 << (sshift + cshift);
    372   sector = (uint64_t)heap + ((uint64_t)(root - 2) << cshift);
    373   dir    = emalloc(csize);
    374   if (blockdev_read(dev, sector, dir, (size_t)1 << cshift) < 0) {
    375     free(dir);
    376     return;
    377   }
    378   for (i = 0; i + 32 <= csize; i += 32) {
    379     e = dir + i;
    380     if (e[0] == 0)
    381       break;
    382     if (e[0] != EXFAT_ENTRY_LABEL)
    383       continue;
    384     used = utf16_to_utf8(e + 2, e[1] < EXFAT_LABEL_MAX ? e[1] : EXFAT_LABEL_MAX, label, label_sz);
    385     break;
    386   }
    387   label[used] = '\0';
    388   free(dir);
    389 }
    390 
    391 /* ntfs keeps its label in the $VOLUME_NAME attribute of the master file
    392  * table's fourth record, $Volume: the record is read and its fixups
    393  * undone, then its attributes walked */
    394 #define NTFS_VOLUME_RECORD 3
    395 #define NTFS_ATTR_VOLNAME  0x60
    396 #define NTFS_ATTR_END      0xFFFFFFFFu
    397 #define NTFS_RECORD_MAX    4096
    398 
    399 static void
    400 ntfs_label(struct BlockDev *dev, const unsigned char *boot, char *label, size_t label_sz)
    401 {
    402   unsigned       bps = boot[11] | boot[12] << 8, spc = boot[13];
    403   signed char    cpr = (signed char)boot[64];
    404   uint64_t       mft = 0, rsize, at;
    405   unsigned char *rec;
    406   unsigned       usa, nusa, a;
    407 
    408   label[0] = '\0';
    409   for (int i = 7; i >= 0; i--)
    410     mft = mft << 8 | boot[48 + i];
    411   if (!bps || bps % 512 || !spc)
    412     return;
    413   rsize = cpr > 0 ? (uint64_t)cpr * spc * bps : (uint64_t)1 << -cpr;
    414   if (rsize < 512 || rsize > NTFS_RECORD_MAX || rsize % dev->sec_size)
    415     return;
    416   at  = (mft * spc * bps + NTFS_VOLUME_RECORD * rsize) / dev->sec_size;
    417   rec = emalloc(rsize);
    418   if (blockdev_read(dev, at, rec, rsize / dev->sec_size) < 0 || memcmp(rec, "FILE", 4))
    419     goto out;
    420   /* each 512 bytes ends in the update number; the array holds what was there */
    421   usa  = rec[4] | rec[5] << 8;
    422   nusa = rec[6] | rec[7] << 8;
    423   if (usa + 2 * (size_t)nusa > rsize || nusa < 1)
    424     goto out;
    425   for (unsigned k = 1; k < nusa && k * 512 <= rsize; k++) {
    426     rec[k * 512 - 2] = rec[usa + 2 * k];
    427     rec[k * 512 - 1] = rec[usa + 2 * k + 1];
    428   }
    429   for (a = rec[20] | rec[21] << 8; a + 24 <= rsize;) {
    430     uint32_t type =
    431         rec[a] | rec[a + 1] << 8 | (uint32_t)rec[a + 2] << 16 | (uint32_t)rec[a + 3] << 24;
    432     uint32_t len =
    433         rec[a + 4] | rec[a + 5] << 8 | (uint32_t)rec[a + 6] << 16 | (uint32_t)rec[a + 7] << 24;
    434 
    435     if (type == NTFS_ATTR_END || len < 24 || a + len > rsize)
    436       break;
    437     /* resident: its value's length and where it starts in the attribute */
    438     if (type == NTFS_ATTR_VOLNAME && !rec[a + 8]) {
    439       uint32_t vlen = rec[a + 16] | rec[a + 17] << 8 | (uint32_t)rec[a + 18] << 16
    440                       | (uint32_t)rec[a + 19] << 24;
    441       unsigned voff = rec[a + 20] | rec[a + 21] << 8;
    442 
    443       if (voff + vlen <= len)
    444         utf16_to_utf8(rec + a + voff, vlen / 2, label, label_sz);
    445       break;
    446     }
    447     a += len;
    448   }
    449 out:
    450   free(rec);
    451 }
    452 
    453 int
    454 blockdev_detect_fs(
    455     struct BlockDev *dev,
    456     char            *type,
    457     size_t           type_sz,
    458     char            *label,
    459     size_t           label_sz,
    460     char            *uuid,
    461     size_t           uuid_sz
    462 )
    463 {
    464   unsigned char *buf;
    465   size_t         read_size = 262144; /* 256kb */
    466   size_t         sectors_to_read;
    467   size_t         i, j;
    468   int            found = 0;
    469 
    470   if (dev->size < read_size)
    471     read_size = dev->size;
    472 
    473   buf             = emalloc(read_size);
    474   sectors_to_read = (read_size + dev->sec_size - 1) / dev->sec_size;
    475   if (blockdev_read(dev, 0, buf, sectors_to_read) < 0) {
    476     free(buf);
    477     return -2;
    478   }
    479 
    480   for (i = 0; i < LEN(fstypes); i++) {
    481     const struct FsType *fs = &fstypes[i];
    482     if (fs->magic_offset + fs->magic_len > read_size)
    483       continue;
    484 
    485     uint64_t val = 0;
    486     for (j = 0; j < fs->magic_len; j++) {
    487       val |= ((uint64_t)buf[fs->magic_offset + j]) << (8 * j);
    488     }
    489 
    490     if (val == fs->magic1 || (fs->magic2 && val == fs->magic2)) {
    491       const char *type_name = fs->name;
    492       if (strcmp(fs->name, "ext") == 0) {
    493         /* Extents, 64-bit descriptors, flex_bg and extra inode fields
    494          * identify ext4 even when the filesystem also has a journal. */
    495         if (read_size < 1128)
    496           continue;
    497         if ((buf[1120] & 0xc0) || (buf[1121] & 0x02) || (buf[1124] & 0x40))
    498           type_name = "ext4";
    499         else if (buf[1116] & 4)
    500           type_name = "ext3";
    501         else
    502           type_name = "ext2";
    503       }
    504 
    505       if (type && type_sz)
    506         strlcpy(type, type_name, type_sz);
    507 
    508       if (label && label_sz && fs->label_len && fs->label_offset + fs->label_len <= read_size) {
    509         snprintf(label, label_sz, "%.*s", (int)fs->label_len, buf + fs->label_offset);
    510         /* strip trailing spaces */
    511         char *end = label + strlen(label) - 1;
    512         while (end >= label && *end == ' ') {
    513           *end = '\0';
    514           end--;
    515         }
    516       }
    517 
    518       if (label && label_sz && strcmp(type_name, "exfat") == 0)
    519         exfat_label(dev, buf, label, label_sz);
    520       if (label && label_sz && strcmp(type_name, "ntfs") == 0)
    521         ntfs_label(dev, buf, label, label_sz);
    522 
    523       if (uuid && uuid_sz && fs->uuid_len && fs->uuid_offset + fs->uuid_len <= read_size) {
    524         format_uuid(buf + fs->uuid_offset, fs->uuid_len, fs->name, uuid, uuid_sz);
    525       }
    526 
    527       found = 1;
    528       break;
    529     }
    530   }
    531 
    532   if (!found && zfs_detect(buf, read_size)) {
    533     if (type && type_sz)
    534       strlcpy(type, "zfs_member", type_sz);
    535     found = 1;
    536   }
    537 
    538   if (!found) {
    539     const char *p9_name = NULL;
    540     if (plan9_detect_other(buf, read_size, &p9_name)
    541         || plan9_detect_text(buf, read_size, &p9_name)) {
    542       if (type && type_sz)
    543         strlcpy(type, p9_name, type_sz);
    544       found = 1;
    545     }
    546   }
    547 
    548   free(buf);
    549   return found ? 0 : -1;
    550 }