2022-09-05 20:46:43 +02:00
|
|
|
/*
|
|
|
|
* This file is part of patchelfcrc .
|
|
|
|
* Copyright (c) 2022 Mario Hüttel.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2 only.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <libelf.h>
|
|
|
|
#include <argp.h>
|
|
|
|
#include <stdbool.h>
|
2022-08-19 21:58:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <patchelfcrc/named_crcs.h>
|
2022-09-05 20:46:43 +02:00
|
|
|
#include <patchelfcrc/crc.h>
|
2022-08-16 22:27:37 +02:00
|
|
|
#include <patchelfcrc/version.h>
|
2022-08-20 11:26:04 +02:00
|
|
|
#include <linklist-lib/singly-linked-list.h>
|
2022-10-16 20:51:29 +02:00
|
|
|
#include <patchelfcrc/reporting.h>
|
|
|
|
#include <patchelfcrc/elfpatch.h>
|
2022-08-16 21:42:23 +02:00
|
|
|
|
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 {
|
2022-08-19 21:58:18 +02:00
|
|
|
GRANULARITY_BYTE = 8,
|
|
|
|
GRANULARITY_16BIT = 16,
|
|
|
|
GRANULARITY_32BIT = 32,
|
2022-08-16 21:42:23 +02:00
|
|
|
};
|
|
|
|
|
2022-08-19 21:58:18 +02:00
|
|
|
enum crc_format {
|
|
|
|
FORMAT_BARE = 0,
|
|
|
|
FORMAT_STRUCT,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARG_KEY_DRY_RUN (1)
|
|
|
|
#define ARG_KEY_START_MAGIC (2)
|
|
|
|
#define ARG_KEY_END_MAGIC (3)
|
2022-08-19 22:16:11 +02:00
|
|
|
#define ARG_KEY_LIST (4)
|
2022-08-19 21:58:18 +02:00
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
struct command_line_options {
|
|
|
|
bool little_endian;
|
2022-08-19 21:58:18 +02:00
|
|
|
bool dry_run;
|
|
|
|
bool verbose;
|
2022-08-16 21:42:23 +02:00
|
|
|
enum granularity granularity;
|
2022-08-19 21:58:18 +02:00
|
|
|
enum crc_format format;
|
|
|
|
struct crc_settings crc;
|
|
|
|
bool has_start_magic;
|
|
|
|
uint32_t start_magic;
|
|
|
|
bool has_end_magic;
|
|
|
|
uint32_t end_magic;
|
2022-08-19 22:16:11 +02:00
|
|
|
bool list;
|
2022-08-20 11:26:04 +02:00
|
|
|
SlList *section_list;
|
2022-10-16 20:51:29 +02:00
|
|
|
const char *elf_path;
|
2022-08-16 21:42:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static error_t parse_opt(int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
struct command_line_options *args = (struct command_line_options *)state->input;
|
2022-09-06 22:26:07 +02:00
|
|
|
const struct named_crc *looked_up_crc;
|
2022-09-05 20:46:43 +02:00
|
|
|
char *endptr;
|
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
switch (key) {
|
2022-08-19 21:58:18 +02:00
|
|
|
case ARG_KEY_DRY_RUN:
|
|
|
|
args->dry_run = true;
|
|
|
|
args->verbose = true;
|
|
|
|
break;
|
|
|
|
case ARG_KEY_START_MAGIC:
|
|
|
|
args->has_start_magic = true;
|
|
|
|
args->start_magic = strtoul(arg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case ARG_KEY_END_MAGIC:
|
|
|
|
args->has_end_magic = true;
|
|
|
|
args->end_magic = strtoul(arg, NULL, 0);
|
|
|
|
break;
|
2022-08-19 22:16:11 +02:00
|
|
|
case ARG_KEY_LIST:
|
|
|
|
args->list = true;
|
|
|
|
break;
|
2022-09-05 20:46:43 +02:00
|
|
|
case 'p':
|
|
|
|
/* Polyniomial */
|
|
|
|
args->crc.polynomial = strtoull(arg, &endptr, 0);
|
|
|
|
if (endptr == arg) {
|
2022-09-06 22:26:07 +02:00
|
|
|
if ((looked_up_crc = lookup_named_crc(arg))) {
|
|
|
|
memcpy(&args->crc, &looked_up_crc->settings, sizeof(struct crc_settings));
|
|
|
|
} else {
|
|
|
|
argp_error(state, "Error parsing polynomial: %s\n", arg);
|
|
|
|
}
|
2022-09-05 20:46:43 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-08-16 21:42:23 +02:00
|
|
|
case 'l':
|
|
|
|
args->little_endian = true;
|
|
|
|
break;
|
2022-08-19 21:58:18 +02:00
|
|
|
case 'v':
|
|
|
|
args->verbose = true;
|
|
|
|
break;
|
2022-08-20 11:26:04 +02:00
|
|
|
case 'S':
|
|
|
|
/* Section */
|
|
|
|
args->section_list = sl_list_append(args->section_list, strdup(arg));
|
|
|
|
break;
|
2022-08-19 21:58:18 +02:00
|
|
|
case 'g':
|
|
|
|
if (!strcmp(arg, "byte"))
|
|
|
|
args->granularity = GRANULARITY_BYTE;
|
|
|
|
else if (!strcmp(arg, "halfword"))
|
|
|
|
args->granularity = GRANULARITY_16BIT;
|
|
|
|
else if (!strcmp(arg, "word"))
|
|
|
|
args->granularity = GRANULARITY_32BIT;
|
|
|
|
break;
|
2022-10-16 20:51:29 +02:00
|
|
|
case ARGP_KEY_ARG:
|
|
|
|
if (state->arg_num >= 1)
|
|
|
|
argp_usage(state);
|
|
|
|
else
|
|
|
|
args->elf_path = arg;
|
|
|
|
break;
|
2022-08-16 21:42:23 +02:00
|
|
|
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-19 21:58:18 +02:00
|
|
|
const int crc_param_group = 1;
|
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-19 21:58:18 +02:00
|
|
|
{"granularity", 'g', "GRANULARITY", 0, "Granularity to calculate the CRC for", 0},
|
|
|
|
{"poly", 'p', "POLYNOMIAL", 0, "Polynomial to use", crc_param_group},
|
|
|
|
{"start-value", 's', "STARTVALUE", 0, "Start value for CRC calculation", crc_param_group},
|
|
|
|
{"reversed", 'r', 0, 0, "Bit reversed CRC", crc_param_group},
|
|
|
|
{"xor-out", 'x', "XORVAL", 0, "XOR the output with XORVAL. Default 0x0", crc_param_group},
|
|
|
|
{"dry-run", ARG_KEY_DRY_RUN, 0, 0, "Dry run. Caclualate CRCs but do not patch output file. Implicitly activates verbose mode.", 0},
|
|
|
|
{"verbose", 'v', 0, 0, "Verbose output", 0},
|
|
|
|
{"section", 'S', "SEC", 0, "Section to calculate CRC for", 2},
|
|
|
|
{"output-section", 'O', "OUTPUTSEC", 0, "Output section for generated CRCs", 2},
|
|
|
|
{"crc-format", 'F', "FORMAT", 0, "Output Format for CRCs.", 2},
|
|
|
|
{"start-magic", ARG_KEY_START_MAGIC, "STARTMAGIC", 0, "Check output section for start magic (uint32)", 2},
|
|
|
|
{"end-magic", ARG_KEY_END_MAGIC, "STARTMAGIC", 0, "Check output section for start magic (uint32)", 2},
|
2022-08-19 22:16:11 +02:00
|
|
|
{"list-crcs", ARG_KEY_LIST, 0, 0 , "List predefined CRCs", 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,
|
2022-10-16 20:51:29 +02:00
|
|
|
"ELF",
|
2022-08-16 21:42:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-19 21:58:18 +02:00
|
|
|
static void prepare_default_opts(struct command_line_options *opts)
|
|
|
|
{
|
|
|
|
opts->little_endian = false;
|
|
|
|
opts->verbose = false;
|
|
|
|
opts->granularity = GRANULARITY_BYTE;
|
|
|
|
opts->dry_run = false;
|
|
|
|
opts->crc.xor = 0UL;
|
2022-09-05 20:46:43 +02:00
|
|
|
opts->crc.polynomial = 0x104C11DB7UL;
|
2022-08-19 21:58:18 +02:00
|
|
|
opts->crc.start_value = 0xFFFFFFFFUL;
|
|
|
|
opts->crc.rev = false;
|
|
|
|
opts->format = FORMAT_BARE;
|
|
|
|
opts->has_end_magic = false;
|
|
|
|
opts->has_start_magic = false;
|
2022-08-19 22:16:11 +02:00
|
|
|
opts->list = false;
|
2022-08-20 11:26:04 +02:00
|
|
|
opts->section_list = NULL;
|
2022-10-16 20:51:29 +02:00
|
|
|
opts->elf_path = NULL;
|
2022-08-19 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_verbose_start_info(const struct command_line_options *cmd_opts)
|
|
|
|
{
|
2022-08-20 11:26:04 +02:00
|
|
|
int i;
|
|
|
|
SlList *list_iter;
|
2022-08-19 21:58:18 +02:00
|
|
|
const struct named_crc *predef_crc;
|
|
|
|
|
|
|
|
print_debug("Start CRC patching\n");
|
|
|
|
print_debug("Endianess: %s endian\n", (cmd_opts->little_endian ? "little" : "big"));
|
|
|
|
print_debug("Granularity: %u bits\n", (unsigned int)cmd_opts->granularity);
|
|
|
|
if (cmd_opts->has_start_magic)
|
|
|
|
print_debug("Checking for start magic: 0x%08x\n", (unsigned int)cmd_opts->start_magic);
|
|
|
|
if (cmd_opts->has_end_magic)
|
|
|
|
print_debug("Checking for end magic: 0x%08x\n", (unsigned int)cmd_opts->end_magic);
|
|
|
|
if (cmd_opts->dry_run)
|
|
|
|
print_debug("Dry run mode selected. Will not touch ELF file.\n");
|
|
|
|
predef_crc = reverse_lookup_named_crc(&cmd_opts->crc);
|
|
|
|
if (predef_crc) {
|
|
|
|
print_debug("Predefined CRC detected: %s\n", predef_crc->name);
|
2022-09-05 20:46:43 +02:00
|
|
|
} else {
|
|
|
|
print_debug("Generator polynomial: 0x%lx\n", cmd_opts->crc.polynomial);
|
|
|
|
print_debug("Start value: 0x%x\n", cmd_opts->crc.start_value);
|
|
|
|
print_debug("Output XOR: 0x%x\n", cmd_opts->crc.xor);
|
|
|
|
print_debug("Reversed: %s\n", cmd_opts->crc.rev ? "yes" : "no");
|
|
|
|
print_debug("CRC length: %d\n", crc_len_from_poly(cmd_opts->crc.polynomial));
|
2022-08-19 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 20:51:29 +02:00
|
|
|
if (cmd_opts->elf_path) {
|
|
|
|
print_debug("ELF file: %s\n", cmd_opts->elf_path);
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:26:04 +02:00
|
|
|
if (cmd_opts->section_list) {
|
|
|
|
for (list_iter = cmd_opts->section_list, i = 1; list_iter; list_iter = sl_list_next(list_iter), i++) {
|
|
|
|
print_debug("Input section [%d]: \"%s\"\n", i, (const char *)list_iter->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_cmd_args(struct command_line_options *opts)
|
|
|
|
{
|
|
|
|
SlList *list_iter;
|
|
|
|
|
|
|
|
/* Free the output section names */
|
|
|
|
for (list_iter = opts->section_list; list_iter; list_iter = sl_list_next(list_iter)) {
|
|
|
|
if (list_iter->data)
|
|
|
|
free(list_iter->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the section list */
|
|
|
|
sl_list_free(opts->section_list);
|
|
|
|
opts->section_list = NULL;
|
2022-08-19 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2022-09-05 20:46:43 +02:00
|
|
|
struct crc_calc crc;
|
2022-08-16 21:42:23 +02:00
|
|
|
struct command_line_options cmd_opts;
|
2022-10-16 20:51:29 +02:00
|
|
|
struct elfpatch ep;
|
2022-08-16 21:42:23 +02:00
|
|
|
|
2022-08-19 21:58:18 +02:00
|
|
|
prepare_default_opts(&cmd_opts);
|
2022-08-16 21:42:23 +02:00
|
|
|
parse_cmdline_options(&argc, &argv, &cmd_opts);
|
|
|
|
|
2022-10-16 20:51:29 +02:00
|
|
|
if (cmd_opts.verbose || cmd_opts.dry_run)
|
|
|
|
reporting_enable_verbose();
|
2022-08-19 21:58:18 +02:00
|
|
|
print_verbose_start_info(&cmd_opts);
|
2022-08-16 21:42:23 +02:00
|
|
|
|
2022-10-16 20:51:29 +02:00
|
|
|
/* Check if file has been supplied */
|
|
|
|
if (!cmd_opts.elf_path) {
|
|
|
|
print_err("No ELF file specified. Exiting...\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:16:11 +02:00
|
|
|
if (cmd_opts.list) {
|
|
|
|
list_predefined_crcs();
|
2022-08-20 11:26:04 +02:00
|
|
|
goto free_cmds;
|
2022-08-19 22:16:11 +02:00
|
|
|
}
|
2022-09-05 20:46:43 +02:00
|
|
|
|
2022-10-16 20:51:29 +02:00
|
|
|
/* Do error printing if using a reversed polynomial. It is not implemented yet! */
|
|
|
|
if (cmd_opts.crc.rev) {
|
|
|
|
print_err("Reversed polynomials are not supported yet\nExiting...\n");
|
|
|
|
goto free_cmds;
|
|
|
|
}
|
|
|
|
|
2022-09-05 20:46:43 +02:00
|
|
|
/* Build the CRC */
|
|
|
|
crc_init(&crc, &cmd_opts.crc);
|
2022-09-06 22:26:07 +02:00
|
|
|
|
2022-10-16 20:51:29 +02:00
|
|
|
/* Prepare libelf for use with the latest ELF version */
|
|
|
|
elf_version(EV_CURRENT);
|
|
|
|
|
|
|
|
/* Open the ELF file */
|
|
|
|
elf_patch_open(&ep, cmd_opts.elf_path);
|
|
|
|
|
|
|
|
elf_patch_print_stats(&ep);
|
|
|
|
|
|
|
|
elf_patch_close(&ep);
|
2022-09-06 22:26:07 +02:00
|
|
|
|
|
|
|
crc_destroy(&crc);
|
2022-08-20 11:26:04 +02:00
|
|
|
free_cmds:
|
|
|
|
|
|
|
|
free_cmd_args(&cmd_opts);
|
2022-08-19 22:16:11 +02:00
|
|
|
|
2022-08-16 21:42:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|