xref: /aosp_15_r20/external/coreboot/util/cbfstool/rmodtool.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* CLI utility for creating rmodules */
2 /* SPDX-License-Identifier: GPL-2.0-only */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <getopt.h>
9 #include "common.h"
10 #include "rmodule.h"
11 
12 static const char *optstring  = "i:o:vh?";
13 static struct option long_options[] = {
14 	{"inelf",        required_argument, 0, 'i' },
15 	{"outelf",       required_argument, 0, 'o' },
16 	{"verbose",      no_argument,       0, 'v' },
17 	{"help",         no_argument,       0, 'h' },
18 	{NULL,           0,                 0,  0  }
19 };
20 
usage(char * name)21 static void usage(char *name)
22 {
23 	printf(
24 		"rmodtool: utility for creating rmodules\n\n"
25 		"USAGE: %s [-h] [-v] <-i|--inelf name> <-o|--outelf name>\n",
26 		name
27 	);
28 }
29 
main(int argc,char * argv[])30 int main(int argc, char *argv[])
31 {
32 	int c;
33 	struct buffer elfin;
34 	struct buffer elfout;
35 	const char *input_file = NULL;
36 	const char *output_file = NULL;
37 
38 	if (argc < 3) {
39 		usage(argv[0]);
40 		return 1;
41 	}
42 
43 	while (1) {
44 		int optindex = 0;
45 
46 		c = getopt_long(argc, argv, optstring, long_options, &optindex);
47 
48 		if (c == -1)
49 			break;
50 
51 		switch (c) {
52 		case 'i':
53 			input_file = optarg;
54 			break;
55 		case 'h':
56 			usage(argv[0]);
57 			return 1;
58 		case 'o':
59 			output_file = optarg;
60 			break;
61 		case 'v':
62 			verbose++;
63 			break;
64 		default:
65 			break;
66 		}
67 	}
68 
69 	if (input_file == NULL || output_file == NULL) {
70 		usage(argv[0]);
71 		return 1;
72 	}
73 
74 	if (buffer_from_file(&elfin, input_file)) {
75 		ERROR("Couldn't read in file '%s'.\n", input_file);
76 		return 1;
77 	}
78 
79 	if (rmodule_create(&elfin, &elfout)) {
80 		ERROR("Unable to create rmodule from '%s'.\n", input_file);
81 		return 1;
82 	}
83 
84 	if (buffer_write_file(&elfout, output_file)) {
85 		ERROR("Unable to write rmodule elf '%s'.\n", output_file);
86 		return 1;
87 	}
88 
89 	return 0;
90 }
91