| 1 |
|
| 2 |
|
| 3 |
#include <errno.h> |
| 4 |
#include <getopt.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "rom.h" |
| 13 |
|
| 14 |
static const char *USAGE_FMT = "Usage: %s <rom-file> <command>\n" |
| 15 |
" -h|--help\n" |
| 16 |
" -f|--fmap_region_name\n\n" |
| 17 |
"Command:\n" |
| 18 |
" GEN_ECFW_PTR - Writes the ECFW PTR\n"; |
| 19 |
|
| 20 |
static const char *program_name; |
| 21 |
|
| 22 |
static void print_program_usage(void) |
| 23 |
{ |
| 24 |
fprintf(stderr, USAGE_FMT, program_name); |
| 25 |
exit(EXIT_FAILURE); |
| 26 |
} |
| 27 |
|
| 28 |
#define ERROR(...) do { fprintf(stderr, "ERROR: " __VA_ARGS__); \ |
| 29 |
print_program_usage(); } \ |
| 30 |
while (0) |
| 31 |
|
| 32 |
static void print_help(void) |
| 33 |
{ |
| 34 |
printf(USAGE_FMT, program_name); |
| 35 |
} |
| 36 |
|
| 37 |
static const struct option longopts[] = { |
| 38 |
{"help", 0, 0, 'h'}, |
| 39 |
{"fmap_region_name", 1, 0, 'f'}, |
| 40 |
{NULL, 0, 0, 0} |
| 41 |
}; |
| 42 |
|
| 43 |
int main(int argc, char *argv[]) |
| 44 |
{ |
| 45 |
char *fmap_region_name = NULL; |
| 46 |
const char *rom_file = NULL; |
| 47 |
const char *command = NULL; |
| 48 |
|
| 49 |
int opt, idx, ret = 0; |
| 50 |
program_name = argv[0]; |
| 51 |
|
| 52 |
if (argc < 3) |
| 53 |
ERROR("Not enough argument given\n\n"); |
| 54 |
|
| 55 |
while (!ret && |
| 56 |
(opt = getopt_long(argc, argv, "?hf:", |
| 57 |
longopts, &idx)) != -1) { |
| 58 |
switch (opt) { |
| 59 |
case 'h': |
| 60 |
case '?': |
| 61 |
print_help(); |
| 62 |
exit(EXIT_SUCCESS); |
| 63 |
break; |
| 64 |
case 'f': |
| 65 |
fmap_region_name = strdup(optarg); |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
if (optind < argc) { |
| 70 |
rom_file = argv[optind]; |
| 71 |
optind++; |
| 72 |
} |
| 73 |
if (optind < argc) { |
| 74 |
command = argv[optind]; |
| 75 |
optind++; |
| 76 |
} |
| 77 |
|
| 78 |
if (optind < argc) |
| 79 |
ERROR("Unknown argument: %s\n\n", argv[optind]); |
| 80 |
|
| 81 |
if (!rom_file) |
| 82 |
ERROR("No ROM specified\n\n"); |
| 83 |
|
| 84 |
if (!command) { |
| 85 |
ERROR("No command specified\n\n"); |
| 86 |
} else if (!strcmp(command, "GEN_ECFW_PTR")) { |
| 87 |
if (!fmap_region_name) |
| 88 |
ERROR("No fmap region specified\n\n"); |
| 89 |
if (!rom_set_ec_fw_ptr(rom_file, fmap_region_name)) |
| 90 |
ERROR("Failed to write EC FW PTR\n\n"); |
| 91 |
} else { |
| 92 |
ERROR("Unknown command specified: '%s'\n\n", command); |
| 93 |
} |
| 94 |
|
| 95 |
if (fmap_region_name) |
| 96 |
free(fmap_region_name); |
| 97 |
|
| 98 |
return ret; |
| 99 |
} |