blob: 1455cd9a3b12d9321d119fca6fa8a91a78d329f7 [file] [log] [blame]
Aaron Durbin4fde5a62014-03-07 15:11:53 -06001/*
2 * cbfstool, CLI utility for creating rmodules
3 *
4 * Copyright (C) 2014 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include <getopt.h>
25#include "common.h"
26#include "rmodule.h"
27
28static const char *optstring = "i:o:vh?";
29static struct option long_options[] = {
30 {"inelf", required_argument, 0, 'i' },
31 {"outelf", required_argument, 0, 'o' },
32 {"verbose", no_argument, 0, 'v' },
33 {"help", no_argument, 0, 'h' },
34 {NULL, 0, 0, 0 }
35};
36
37static void usage(char *name)
38{
39 printf(
40 "rmodtool: utility for creating rmodules\n\n"
41 "USAGE: %s [-h] [-v] <-i|--inelf name> <-o|--outelf name>\n",
42 name
43 );
44}
45
46int main(int argc, char *argv[])
47{
48 int c;
49 struct buffer elfin;
50 struct buffer elfout;
51 const char *input_file = NULL;
52 const char *output_file = NULL;
53
54 if (argc < 3) {
55 usage(argv[0]);
56 return 1;
57 }
58
59 while (1) {
60 int optindex = 0;
61
62 c = getopt_long(argc, argv, optstring, long_options, &optindex);
63
64 if (c == -1)
65 break;
66
67 switch (c) {
68 case 'i':
69 input_file = optarg;
70 break;
71 case 'h':
72 usage(argv[0]);
73 return 1;
74 case 'o':
75 output_file = optarg;
76 break;
77 case 'v':
78 verbose++;
79 break;
80 default:
81 break;
82 }
83 }
84
85 if (input_file == NULL || output_file == NULL) {
86 usage(argv[0]);
87 return 1;
88 }
89
90 if (buffer_from_file(&elfin, input_file)) {
91 ERROR("Couldn't read in file '%s'.\n", input_file);
92 return 1;
93 }
94
95 if (rmodule_create(&elfin, &elfout)) {
96 ERROR("Unable to create rmodule from '%s'.\n", input_file);
97 return 1;
98 }
99
100 if (buffer_write_file(&elfout, output_file)) {
101 ERROR("Unable to write rmodule elf '%s'.\n", output_file);
102 return 1;
103 }
104
105 return 0;
106}