2022-08-16 21:42:23 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <libelf.h>
|
|
|
|
#include <argp.h>
|
|
|
|
#include <stdbool.h>
|
2022-08-16 22:27:37 +02:00
|
|
|
#include <patchelfcrc/version.h>
|
2022-08-16 21:42:23 +02:00
|
|
|
|
|
|
|
#define print_err(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__);
|
|
|
|
#define print_debug(fmt, ...) do { \
|
|
|
|
if (verbose) { \
|
|
|
|
printf("[DBG] "fmt, ## __VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-08-16 22:27:37 +02:00
|
|
|
const char *argp_program_bug_address = "<mario.huettel@linux.com>";
|
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
enum granularity {
|
|
|
|
GRANULARITY_BYTE = 1,
|
|
|
|
GRANULARITY_16BIT,
|
|
|
|
GRANULARITY_32BIT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct command_line_options {
|
|
|
|
bool little_endian;
|
|
|
|
enum granularity granularity;
|
|
|
|
};
|
|
|
|
|
|
|
|
static error_t parse_opt(int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
struct command_line_options *args = (struct command_line_options *)state->input;
|
|
|
|
switch (key) {
|
|
|
|
case 'l':
|
|
|
|
args->little_endian = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
2022-08-16 22:27:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
2022-08-16 21:42:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_cmdline_options(int *argc, char ***argv, struct command_line_options *cmd_opts)
|
|
|
|
{
|
2022-08-16 22:27:37 +02:00
|
|
|
error_t err;
|
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
if (!argc || !argv)
|
|
|
|
return -1000;
|
|
|
|
|
|
|
|
static struct argp_option options[] = {
|
2022-08-16 22:27:37 +02:00
|
|
|
{"little-endian", 'l', 0, 0, "Memory image is little endian. Only relevant if granularity is greater than a single byte", 0},
|
2022-08-16 21:42:23 +02:00
|
|
|
/* Sentinel */
|
2022-08-16 22:27:37 +02:00
|
|
|
{NULL, 0, 0, 0, NULL, 0}
|
2022-08-16 21:42:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct argp arg_parser = {
|
|
|
|
options,
|
|
|
|
parse_opt,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
0, 0, 0
|
|
|
|
};
|
|
|
|
|
2022-08-16 22:27:37 +02:00
|
|
|
err = argp_parse(&arg_parser, *argc, *argv, 0, 0, cmd_opts);
|
2022-08-16 21:42:23 +02:00
|
|
|
|
2022-08-16 22:27:37 +02:00
|
|
|
return err ? -1 : 0;
|
2022-08-16 21:42:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
bool verbose = true;
|
|
|
|
struct command_line_options cmd_opts;
|
|
|
|
|
|
|
|
cmd_opts.little_endian = false;
|
|
|
|
cmd_opts.granularity = GRANULARITY_BYTE;
|
|
|
|
|
|
|
|
parse_cmdline_options(&argc, &argv, &cmd_opts);
|
|
|
|
|
|
|
|
print_debug("Start CRC patching\n");
|
|
|
|
print_debug("Endianess: %s endian\n", (cmd_opts.little_endian ? "little" : "big"));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|