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;
45 unsigned long volume_serial;
49 if (!setlocale(LC_CTYPE, ""))
50 exfat_err("failed to init locale/codeset\n");
53 flags = EXFAT_GET_VOLUME_LABEL;
55 flags = EXFAT_SET_VOLUME_LABEL;
58 while ((c = getopt_long(argc, argv, "iVh", opts, NULL)) != EOF)
63 flags = EXFAT_GET_VOLUME_SERIAL;
65 flags = EXFAT_SET_VOLUME_SERIAL;
84 memset(ui.dev_name, 0, sizeof(ui.dev_name));
85 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[serial_mode + 1]);
87 ret = exfat_get_blk_dev_info(&ui, &bd);
92 /* Mode to change or display volume serial */
93 if (flags == EXFAT_GET_VOLUME_SERIAL) {
94 ret = exfat_show_volume_serial(bd.dev_fd);
95 } else if (flags == EXFAT_SET_VOLUME_SERIAL) {
96 ret = exfat_parse_ulong(argv[3], &volume_serial);
97 if (volume_serial > UINT_MAX)
102 exfat_err("invalid serial number(%s)\n", argv[3]);
106 ui.volume_serial = volume_serial;
107 ret = exfat_set_volume_serial(&bd, &ui);
113 ret = read_boot_sect(&bd, &bs);
117 exfat = exfat_alloc_exfat(&bd, bs);
123 exfat->root = exfat_alloc_inode(ATTR_SUBDIR);
129 exfat->root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
130 if (exfat_root_clus_count(exfat)) {
131 exfat_err("failed to follow the cluster chain of root\n");
132 exfat_free_inode(exfat->root);
137 /* Mode to change or display volume label */
138 if (flags == EXFAT_GET_VOLUME_LABEL)
139 ret = exfat_read_volume_label(exfat);
140 else if (flags == EXFAT_SET_VOLUME_LABEL)
141 ret = exfat_set_volume_label(exfat, argv[2]);
145 exfat_free_exfat(exfat);