1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Namjae Jeon <linkinjeon@kernel.org>
14 #include "exfat_ondisk.h"
18 static void usage(void)
20 fprintf(stderr, "Usage: exfatlabel\n");
21 fprintf(stderr, "\t-i | --volume-serial Switch to volume serial mode\n");
22 fprintf(stderr, "\t-V | --version Show version\n");
23 fprintf(stderr, "\t-h | --help Show help\n");
28 static struct option opts[] = {
29 {"volume-serial", no_argument, NULL, 'i' },
30 {"version", no_argument, NULL, 'V' },
31 {"help", no_argument, NULL, 'h' },
32 {"?", no_argument, NULL, '?' },
36 int main(int argc, char *argv[])
39 int ret = EXIT_FAILURE;
40 struct exfat_blk_dev bd;
41 struct exfat_user_input ui;
42 bool version_only = false;
48 if (!setlocale(LC_CTYPE, ""))
49 exfat_err("failed to init locale/codeset\n");
52 flags = EXFAT_GET_VOLUME_LABEL;
54 flags = EXFAT_SET_VOLUME_LABEL;
57 while ((c = getopt_long(argc, argv, "iVh", opts, NULL)) != EOF)
62 flags = EXFAT_GET_VOLUME_SERIAL;
64 flags = EXFAT_SET_VOLUME_SERIAL;
83 memset(ui.dev_name, 0, sizeof(ui.dev_name));
84 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[serial_mode + 1]);
86 ret = exfat_get_blk_dev_info(&ui, &bd);
91 /* Mode to change or display volume serial */
92 if (flags == EXFAT_GET_VOLUME_SERIAL) {
93 ret = exfat_show_volume_serial(bd.dev_fd);
94 } else if (flags == EXFAT_SET_VOLUME_SERIAL) {
95 ui.volume_serial = strtoul(argv[3], NULL, 0);
96 ret = exfat_set_volume_serial(&bd, &ui);
102 ret = read_boot_sect(&bd, &bs);
106 exfat = exfat_alloc_exfat(&bd, bs);
113 exfat->root = exfat_alloc_inode(ATTR_SUBDIR);
119 exfat->root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
120 if (exfat_root_clus_count(exfat)) {
121 exfat_err("failed to follow the cluster chain of root\n");
122 exfat_free_inode(exfat->root);
127 /* Mode to change or display volume label */
128 if (flags == EXFAT_GET_VOLUME_LABEL)
129 ret = exfat_read_volume_label(exfat);
130 else if (flags == EXFAT_SET_VOLUME_LABEL)
131 ret = exfat_set_volume_label(exfat, argv[2]);
135 exfat_free_exfat(exfat);