Browse Source

rots-utils: implemented usage for subcommands

S.J.R. van Schaik 4 years ago
parent
commit
33d6268295
11 changed files with 148 additions and 17 deletions
  1. 2 0
      include/info.h
  2. 1 0
      include/pack.h
  3. 1 0
      include/sign.h
  4. 1 0
      include/unpack.h
  5. 1 0
      include/verify.h
  6. 23 2
      source/info.c
  7. 36 8
      source/main.c
  8. 19 2
      source/pack.c
  9. 22 1
      source/sign.c
  10. 22 2
      source/unpack.c
  11. 20 2
      source/verify.c

+ 2 - 0
include/info.h

@@ -1,4 +1,6 @@
1 1
 #pragma once
2 2
 
3
+void show_get_offset_usage(const char *prog_name, const char *cmd);
4
+void show_get_size_usage(const char *prog_name, const char *cmd);
3 5
 int do_get_offset(int argc, char *argv[]);
4 6
 int do_get_size(int argc, char *argv[]);

+ 1 - 0
include/pack.h

@@ -1,3 +1,4 @@
1 1
 #pragma once
2 2
 
3
+void show_pack_usage(const char *prog_name, const char *cmd);
3 4
 int do_pack(int argc, char *argv[]);

+ 1 - 0
include/sign.h

@@ -1,3 +1,4 @@
1 1
 #pragma once
2 2
 
3
+void show_sign_usage(const char *prog_name, const char *cmd);
3 4
 int do_sign(int argc, char *argv[]);

+ 1 - 0
include/unpack.h

@@ -1,3 +1,4 @@
1 1
 #pragma once
2 2
 
3
+void show_unpack_usage(const char *prog_name, const char *cmd);
3 4
 int do_unpack(int argc, char *argv[]);

+ 1 - 0
include/verify.h

@@ -1,3 +1,4 @@
1 1
 #pragma once
2 2
 
3
+void show_verify_usage(const char *prog_name, const char *cmd);
3 4
 int do_verify(int argc, char *argv[]);

+ 23 - 2
source/info.c

@@ -8,6 +8,7 @@
8 8
 #include <image.h>
9 9
 #include <info.h>
10 10
 #include <macros.h>
11
+#include <option.h>
11 12
 #include <sign.h>
12 13
 
13 14
 enum {
@@ -19,6 +20,12 @@ struct args {
19 20
 	const char *image;
20 21
 };
21 22
 
23
+static struct opt_desc opt_descs[] = {
24
+	{ "-i", "--image=PATH", "the image in ROTS format" },
25
+	{ "-h", "--help", "display this help and exit" },
26
+	{ NULL, NULL, NULL },
27
+};
28
+
22 29
 static int parse_args(struct args *args, int argc, char *argv[])
23 30
 {
24 31
 	struct option options[] = {
@@ -40,6 +47,20 @@ static int parse_args(struct args *args, int argc, char *argv[])
40 47
 	return 0;
41 48
 }
42 49
 
50
+void show_get_offset_usage(const char *prog_name, const char *cmd)
51
+{
52
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
53
+		"print the offset of the payload within an image\n\n", prog_name, cmd);
54
+	format_options(opt_descs);
55
+}
56
+
57
+void show_get_size_usage(const char *prog_name, const char *cmd)
58
+{
59
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
60
+		"print the size of the payload within an image\n\n", prog_name, cmd);
61
+	format_options(opt_descs);
62
+}
63
+
43 64
 int do_get_offset(int argc, char *argv[])
44 65
 {
45 66
 	struct args args;
@@ -47,7 +68,7 @@ int do_get_offset(int argc, char *argv[])
47 68
 	FILE *fp;
48 69
 
49 70
 	if (parse_args(&args, argc, argv) < 0) {
50
-		fprintf(stderr, "invalid\n");
71
+		show_get_offset_usage(argv[0], argv[1]);
51 72
 		return -1;
52 73
 	}
53 74
 
@@ -78,7 +99,7 @@ int do_get_size(int argc, char *argv[])
78 99
 	FILE *fp;
79 100
 
80 101
 	if (parse_args(&args, argc, argv) < 0) {
81
-		fprintf(stderr, "invalid\n");
102
+		show_get_size_usage(argv[0], argv[1]);
82 103
 		return -1;
83 104
 	}
84 105
 

+ 36 - 8
source/main.c

@@ -11,23 +11,24 @@
11 11
 struct entry {
12 12
 	const char *cmd;
13 13
 	int (* main)(int argc, char *argv[]);
14
+	void (* show_usage)(const char *prog_name, const char *cmd);
14 15
 	char *desc;
15 16
 };
16 17
 
17 18
 struct entry entries[] = {
18
-	{ "get-offset", do_get_offset,
19
+	{ "get-offset", do_get_offset, show_get_offset_usage,
19 20
 		"print the offset of the payload within an image" },
20
-	{ "get-size", do_get_size,
21
+	{ "get-size", do_get_size, show_get_size_usage,
21 22
 		"print the size of the payload within an image" },
22
-	{ "pack", do_pack,
23
+	{ "pack", do_pack, show_pack_usage,
23 24
 		"pack the payload within an image" },
24
-	{ "sign", do_sign,
25
+	{ "sign", do_sign, show_sign_usage,
25 26
 		"sign an image" },
26
-	{ "unpack", do_unpack,
27
+	{ "unpack", do_unpack, show_unpack_usage,
27 28
 		"unpack the payload of an image" },
28
-	{ "verify", do_verify,
29
+	{ "verify", do_verify, show_verify_usage,
29 30
 		"verify the signatures of an image" },
30
-	{ NULL, NULL, NULL },
31
+	{ NULL, NULL, NULL, NULL },
31 32
 };
32 33
 
33 34
 static void show_usage(const char *prog_name)
@@ -39,10 +40,28 @@ static void show_usage(const char *prog_name)
39 40
 		"Available commands:\n", prog_name);
40 41
 
41 42
 	for (entry = entries; entry->cmd; ++entry) {
42
-		fprintf(stderr, "\t%-16s %-80s\n", entry->cmd, entry->desc);
43
+		fprintf(stderr, "\t%-16s %s\n", entry->cmd, entry->desc);
43 44
 	}
44 45
 }
45 46
 
47
+static void show_cmd_usage(char *prog_name, char *cmd)
48
+{
49
+	struct entry *entry;
50
+
51
+	for (entry = entries; entry->cmd; ++entry) {
52
+		if (strcmp(entry->cmd, cmd) != 0)
53
+			continue;
54
+
55
+		if (!entry->show_usage)
56
+			break;
57
+
58
+		entry->show_usage(prog_name, cmd);
59
+		return;
60
+	}
61
+
62
+	show_usage(prog_name);
63
+}
64
+
46 65
 int main(int argc, char *argv[])
47 66
 {
48 67
 	struct entry *entry;
@@ -52,6 +71,15 @@ int main(int argc, char *argv[])
52 71
 		return -1;
53 72
 	}
54 73
 
74
+	if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
75
+		if (argc < 3)
76
+			show_usage(argv[0]);
77
+		else
78
+			show_cmd_usage(argv[0], argv[2]);
79
+
80
+		return -1;
81
+	}
82
+
55 83
 	for (entry = entries; entry->cmd; ++entry) {
56 84
 		if (strcmp(entry->cmd, argv[1]) == 0)
57 85
 			return entry->main(argc, argv);

+ 19 - 2
source/pack.c

@@ -6,6 +6,7 @@
6 6
 #include <file.h>
7 7
 #include <image.h>
8 8
 #include <macros.h>
9
+#include <option.h>
9 10
 #include <pack.h>
10 11
 
11 12
 enum {
@@ -17,6 +18,12 @@ struct args {
17 18
 	const char *input, *output;
18 19
 };
19 20
 
21
+static struct opt_desc opt_descs[] = {
22
+	{ "-o", "--output", "the file to contain the image in ROTS format" },
23
+	{ "-h", "--help", "display this help and exit" },
24
+	{ NULL, NULL, NULL },
25
+};
26
+
20 27
 static int parse_args(struct args *args, int argc, char *argv[])
21 28
 {
22 29
 	struct option options[] = {
@@ -31,10 +38,13 @@ static int parse_args(struct args *args, int argc, char *argv[])
31 38
 		switch (ret) {
32 39
 		case OPTION_HELP: return -1;
33 40
 		case OPTION_OUTPUT: args->output = optarg; break;
34
-		default: break;
41
+		default: return -1;
35 42
 		}
36 43
 	}
37 44
 
45
+	if (!args->output)
46
+		return -1;
47
+
38 48
 	if (optind >= argc)
39 49
 		return -1;
40 50
 
@@ -48,6 +58,13 @@ static int parse_args(struct args *args, int argc, char *argv[])
48 58
 	return 0;
49 59
 }
50 60
 
61
+void show_pack_usage(const char *prog_name, const char *cmd)
62
+{
63
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
64
+		"pack the payload within an image\n\n", prog_name, cmd);
65
+	format_options(opt_descs);
66
+}
67
+
51 68
 int do_pack(int argc, char *argv[])
52 69
 {
53 70
 	char data[4096];
@@ -57,7 +74,7 @@ int do_pack(int argc, char *argv[])
57 74
 	size_t nbytes, size;
58 75
 
59 76
 	if (parse_args(&args, argc, argv) < 0) {
60
-		fprintf(stderr, "invalid\n");
77
+		show_pack_usage(argv[0], argv[1]);
61 78
 		return -1;
62 79
 	}
63 80
 

+ 22 - 1
source/sign.c

@@ -14,6 +14,7 @@
14 14
 
15 15
 #include <image.h>
16 16
 #include <macros.h>
17
+#include <option.h>
17 18
 #include <sign.h>
18 19
 #include <x509.h>
19 20
 
@@ -29,6 +30,16 @@ struct args {
29 30
 	const char *image, *digest, *key, *cert;
30 31
 };
31 32
 
33
+static struct opt_desc opt_descs[] = {
34
+	{ "-i", "--image=PATH","the image in ROTS format to sign" },
35
+	{ "-d", "--digest={ripemd160,sha224,sha256,sha384,sha512,whirlpool}\n",
36
+		"\t\tthe digest algorithm to use for signing" },
37
+	{ "-k", "--key=PATH", "the private key" },
38
+	{ "-c", "--cert=PATH", "the certificate containing the public key" },
39
+	{ "-h", "--help", "display this help and exit" },
40
+	{ NULL, NULL, NULL },
41
+};
42
+
32 43
 static int parse_args(struct args *args, int argc, char *argv[])
33 44
 {
34 45
 	struct option options[] = {
@@ -53,6 +64,9 @@ static int parse_args(struct args *args, int argc, char *argv[])
53 64
 		}
54 65
 	}
55 66
 
67
+	if (!args->image || !args->digest || !args->key || !args->cert)
68
+		return -1;
69
+
56 70
 	return 0;
57 71
 }
58 72
 
@@ -158,6 +172,13 @@ err_close_image:
158 172
 	return -1;
159 173
 }
160 174
 
175
+void show_sign_usage(const char *prog_name, const char *cmd)
176
+{
177
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
178
+		"sign an image\n\n", prog_name, cmd);
179
+	format_options(opt_descs);
180
+}
181
+
161 182
 int do_sign(int argc, char *argv[])
162 183
 {
163 184
 	struct args args;
@@ -166,7 +187,7 @@ int do_sign(int argc, char *argv[])
166 187
 	char *cn;
167 188
 
168 189
 	if (parse_args(&args, argc, argv) < 0) {
169
-		fprintf(stderr, "invalid\n");
190
+		show_sign_usage(argv[0], argv[1]);
170 191
 		return -1;
171 192
 	}
172 193
 

+ 22 - 2
source/unpack.c

@@ -6,6 +6,7 @@
6 6
 
7 7
 #include <image.h>
8 8
 #include <macros.h>
9
+#include <option.h>
9 10
 #include <unpack.h>
10 11
 
11 12
 enum {
@@ -18,6 +19,13 @@ struct args {
18 19
 	const char *image, *output;
19 20
 };
20 21
 
22
+static struct opt_desc opt_descs[] = {
23
+	{ "-i", "--image=PATH", "the image in ROTS format to unpack" },
24
+	{ "-o", "--output=PATH", "the file to contain the payload" },
25
+	{ "-h", "--help", "display this help and exit" },
26
+	{ NULL, NULL, NULL },
27
+};
28
+
21 29
 static int parse_args(struct args *args, int argc, char *argv[])
22 30
 {
23 31
 	struct option options[] = {
@@ -34,13 +42,23 @@ static int parse_args(struct args *args, int argc, char *argv[])
34 42
 		case OPTION_HELP: return -1;
35 43
 		case OPTION_IMAGE: args->image = optarg; break;
36 44
 		case OPTION_OUTPUT: args->output = optarg; break;
37
-		default: break;
45
+		default: return -1;
38 46
 		}
39 47
 	}
40 48
 
49
+	if (!args->image || !args->output)
50
+		return -1;
51
+
41 52
 	return 0;
42 53
 }
43 54
 
55
+void show_unpack_usage(const char *prog_name, const char *cmd)
56
+{
57
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
58
+		"unpack the payload of an image\n\n", prog_name, cmd);
59
+	format_options(opt_descs);
60
+}
61
+
44 62
 int do_unpack(int argc, char *argv[])
45 63
 {
46 64
 	char data[512];
@@ -49,8 +67,10 @@ int do_unpack(int argc, char *argv[])
49 67
 	struct args args;
50 68
 	size_t nbytes, size;
51 69
 
70
+	memset(&args, 0, sizeof args);
71
+
52 72
 	if (parse_args(&args, argc, argv) < 0) {
53
-		fprintf(stderr, "invalid\n");
73
+		show_unpack_usage(argv[0], argv[1]);
54 74
 		return -1;
55 75
 	}
56 76
 

+ 20 - 2
source/verify.c

@@ -15,6 +15,7 @@
15 15
 
16 16
 #include <image.h>
17 17
 #include <macros.h>
18
+#include <option.h>
18 19
 #include <verify.h>
19 20
 #include <x509.h>
20 21
 
@@ -28,6 +29,13 @@ struct args {
28 29
 	const char *image, *digest, *certs;
29 30
 };
30 31
 
32
+static struct opt_desc opt_descs[] = {
33
+	{ "-i", "--image=PATH", "the image in ROTS format to verify" },
34
+	{ "-c", "--certs=PATH", "the directory containing the certificates" },
35
+	{ "-h", "--help", "display this help and exit" },
36
+	{ NULL, NULL, NULL },
37
+};
38
+
31 39
 static int parse_args(struct args *args, int argc, char *argv[])
32 40
 {
33 41
 	struct option options[] = {
@@ -38,7 +46,7 @@ static int parse_args(struct args *args, int argc, char *argv[])
38 46
 	};
39 47
 	int ret;
40 48
 
41
-	while ((ret = getopt_long(argc, (char * const *)argv, "hi:d:c:", options,
49
+	while ((ret = getopt_long(argc, (char * const *)argv, "hi:c:", options,
42 50
 		NULL)) >= 0) {
43 51
 		switch (ret) {
44 52
 		case OPTION_HELP: return -1;
@@ -48,6 +56,9 @@ static int parse_args(struct args *args, int argc, char *argv[])
48 56
 		}
49 57
 	}
50 58
 
59
+	if (!args->image || !args->certs)
60
+		return -1;
61
+
51 62
 	return 0;
52 63
 }
53 64
 
@@ -220,13 +231,20 @@ err_close_image:
220 231
 	return -1;
221 232
 }
222 233
 
234
+void show_verify_usage(const char *prog_name, const char *cmd)
235
+{
236
+	fprintf(stderr, "usage: %s %s [option]...\n\n"
237
+		"verify the signature of an image\n\n", prog_name, cmd);
238
+	format_options(opt_descs);
239
+}
240
+
223 241
 int do_verify(int argc, char *argv[])
224 242
 {
225 243
 	struct args args;
226 244
 	size_t count, total;
227 245
 
228 246
 	if (parse_args(&args, argc, argv) < 0) {
229
-		fprintf(stderr, "invalid\n");
247
+		show_verify_usage(argv[0], argv[1]);
230 248
 		return -1;
231 249
 	}
232 250