Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 1 | /* |
| 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. |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | #include <getopt.h> |
| 21 | #include "common.h" |
| 22 | #include "rmodule.h" |
| 23 | |
| 24 | static const char *optstring = "i:o:vh?"; |
| 25 | static struct option long_options[] = { |
| 26 | {"inelf", required_argument, 0, 'i' }, |
| 27 | {"outelf", required_argument, 0, 'o' }, |
| 28 | {"verbose", no_argument, 0, 'v' }, |
| 29 | {"help", no_argument, 0, 'h' }, |
| 30 | {NULL, 0, 0, 0 } |
| 31 | }; |
| 32 | |
| 33 | static void usage(char *name) |
| 34 | { |
| 35 | printf( |
| 36 | "rmodtool: utility for creating rmodules\n\n" |
| 37 | "USAGE: %s [-h] [-v] <-i|--inelf name> <-o|--outelf name>\n", |
| 38 | name |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | int main(int argc, char *argv[]) |
| 43 | { |
| 44 | int c; |
| 45 | struct buffer elfin; |
| 46 | struct buffer elfout; |
| 47 | const char *input_file = NULL; |
| 48 | const char *output_file = NULL; |
| 49 | |
| 50 | if (argc < 3) { |
| 51 | usage(argv[0]); |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | while (1) { |
| 56 | int optindex = 0; |
| 57 | |
| 58 | c = getopt_long(argc, argv, optstring, long_options, &optindex); |
| 59 | |
| 60 | if (c == -1) |
| 61 | break; |
| 62 | |
| 63 | switch (c) { |
| 64 | case 'i': |
| 65 | input_file = optarg; |
| 66 | break; |
| 67 | case 'h': |
| 68 | usage(argv[0]); |
| 69 | return 1; |
| 70 | case 'o': |
| 71 | output_file = optarg; |
| 72 | break; |
| 73 | case 'v': |
| 74 | verbose++; |
| 75 | break; |
| 76 | default: |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (input_file == NULL || output_file == NULL) { |
| 82 | usage(argv[0]); |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | if (buffer_from_file(&elfin, input_file)) { |
| 87 | ERROR("Couldn't read in file '%s'.\n", input_file); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (rmodule_create(&elfin, &elfout)) { |
| 92 | ERROR("Unable to create rmodule from '%s'.\n", input_file); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | if (buffer_write_file(&elfout, output_file)) { |
| 97 | ERROR("Unable to write rmodule elf '%s'.\n", output_file); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |