1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <getopt.h>
11 #include <errno.h>
12 #include <locale.h>
13
14 #include "exfat_ondisk.h"
15 #include "libexfat.h"
16 #include "exfat_fs.h"
17
usage(void)18 static void usage(void)
19 {
20 fprintf(stderr, "Usage: tune.exfat\n");
21 fprintf(stderr, "\t-l | --print-label Print volume label\n");
22 fprintf(stderr, "\t-L | --set-label=label Set volume label\n");
23 fprintf(stderr, "\t-u | --print-guid Print volume GUID\n");
24 fprintf(stderr, "\t-U | --set-guid=guid Set volume GUID\n");
25 fprintf(stderr, "\t-i | --print-serial Print volume serial\n");
26 fprintf(stderr, "\t-I | --set-serial=value Set volume serial\n");
27 fprintf(stderr, "\t-V | --version Show version\n");
28 fprintf(stderr, "\t-v | --verbose Print debug\n");
29 fprintf(stderr, "\t-h | --help Show help\n");
30
31 exit(EXIT_FAILURE);
32 }
33
34 static struct option opts[] = {
35 {"print-label", no_argument, NULL, 'l' },
36 {"set-label", required_argument, NULL, 'L' },
37 {"print-guid", no_argument, NULL, 'u' },
38 {"set-guid", required_argument, NULL, 'U' },
39 {"print-serial", no_argument, NULL, 'i' },
40 {"set-serial", required_argument, NULL, 'I' },
41 {"version", no_argument, NULL, 'V' },
42 {"verbose", no_argument, NULL, 'v' },
43 {"help", no_argument, NULL, 'h' },
44 {"?", no_argument, NULL, '?' },
45 {NULL, 0, NULL, 0 }
46 };
47
main(int argc,char * argv[])48 int main(int argc, char *argv[])
49 {
50 int c;
51 int ret = EXIT_FAILURE;
52 unsigned long volume_serial;
53 struct exfat_blk_dev bd;
54 struct exfat_user_input ui;
55 bool version_only = false;
56 int flags = 0;
57 char label_input[VOLUME_LABEL_BUFFER_SIZE];
58 struct exfat *exfat = NULL;
59 struct pbr *bs;
60
61 init_user_input(&ui);
62
63 if (!setlocale(LC_CTYPE, ""))
64 exfat_err("failed to init locale/codeset\n");
65
66 opterr = 0;
67 while ((c = getopt_long(argc, argv, "I:iL:lU:uVvh", opts, NULL)) != EOF)
68 switch (c) {
69 case 'l':
70 flags = EXFAT_GET_VOLUME_LABEL;
71 break;
72 case 'L':
73 snprintf(label_input, sizeof(label_input), "%s",
74 optarg);
75 flags = EXFAT_SET_VOLUME_LABEL;
76 break;
77 case 'u':
78 flags = EXFAT_GET_VOLUME_GUID;
79 break;
80 case 'U':
81 if (*optarg != '\0' && *optarg != '\r')
82 ui.guid = optarg;
83 flags = EXFAT_SET_VOLUME_GUID;
84 break;
85 case 'i':
86 flags = EXFAT_GET_VOLUME_SERIAL;
87 break;
88 case 'I':
89 ret = exfat_parse_ulong(optarg, &volume_serial);
90 if (volume_serial > UINT_MAX)
91 ret = -ERANGE;
92
93
94 if (ret < 0) {
95 exfat_err("invalid serial number(%s)\n", optarg);
96 goto out;
97 }
98
99 ui.volume_serial = volume_serial;
100 flags = EXFAT_SET_VOLUME_SERIAL;
101 break;
102 case 'V':
103 version_only = true;
104 break;
105 case 'v':
106 print_level = EXFAT_DEBUG;
107 break;
108 case '?':
109 case 'h':
110 default:
111 usage();
112 }
113
114 show_version();
115 if (version_only)
116 exit(EXIT_FAILURE);
117
118 if (argc < 3 || argc - optind != 1)
119 usage();
120
121 ui.dev_name = argv[argc - 1];
122
123 ret = exfat_get_blk_dev_info(&ui, &bd);
124 if (ret < 0)
125 goto out;
126
127 /* Mode to change or display volume serial */
128 if (flags == EXFAT_GET_VOLUME_SERIAL) {
129 ret = exfat_show_volume_serial(bd.dev_fd);
130 goto close_fd_out;
131 } else if (flags == EXFAT_SET_VOLUME_SERIAL) {
132 ret = exfat_set_volume_serial(&bd, &ui);
133 goto close_fd_out;
134 }
135
136 ret = read_boot_sect(&bd, &bs);
137 if (ret)
138 goto close_fd_out;
139
140 exfat = exfat_alloc_exfat(&bd, bs);
141 if (!exfat) {
142 ret = -ENOMEM;
143 goto close_fd_out;
144 }
145
146 exfat->root = exfat_alloc_inode(ATTR_SUBDIR);
147 if (!exfat->root) {
148 ret = -ENOMEM;
149 goto close_fd_out;
150 }
151
152 exfat->root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
153 if (exfat_root_clus_count(exfat)) {
154 exfat_err("failed to follow the cluster chain of root\n");
155 exfat_free_inode(exfat->root);
156 ret = -EINVAL;
157 goto close_fd_out;
158 }
159
160 if (flags == EXFAT_GET_VOLUME_LABEL)
161 ret = exfat_read_volume_label(exfat);
162 else if (flags == EXFAT_SET_VOLUME_LABEL)
163 ret = exfat_set_volume_label(exfat, label_input);
164 else if (flags == EXFAT_GET_VOLUME_GUID)
165 ret = exfat_read_volume_guid(exfat);
166 else if (flags == EXFAT_SET_VOLUME_GUID)
167 ret = exfat_set_volume_guid(exfat, ui.guid);
168
169 close_fd_out:
170 close(bd.dev_fd);
171 if (exfat)
172 exfat_free_exfat(exfat);
173 out:
174 return ret;
175 }
176