Add print_warn() macro and the --use-vma option

This commit is contained in:
Mario Hüttel 2023-01-06 16:25:59 +01:00
parent 1528700d31
commit 1d5219cc18
4 changed files with 22 additions and 7 deletions

View File

@ -21,7 +21,8 @@
#include <stdbool.h> #include <stdbool.h>
#define print_err(fmt, ...) fprintf(stderr, "[ERR] " fmt, ## __VA_ARGS__); #define print_err(fmt, ...) fprintf(stderr, "[ERR] " fmt, ## __VA_ARGS__)
#define print_warn(fmt, ...) fprintf(stderr, "[WARN] " fmt, ## __VAR__ARGS__)
void print_debug(const char *fmt, ...); void print_debug(const char *fmt, ...);

View File

@ -11,7 +11,7 @@
[**\--granularity**=*GRANULARITY*] [**\--little-endian**] [**\--dry-run**] [**\--xsd**] [**\--granularity**=*GRANULARITY*] [**\--little-endian**] [**\--dry-run**] [**\--xsd**]
[**\--poly**=*POLYNOMIAL*] [**\--reversed**] [**\--start-value**=*STARTVALUE*] [**\--poly**=*POLYNOMIAL*] [**\--reversed**] [**\--start-value**=*STARTVALUE*]
[**--verbose**] [**\--xor-out**=*XORVAL*] [**\--end-magic**=*MAGIC*] [**--verbose**] [**\--xor-out**=*XORVAL*] [**\--end-magic**=*MAGIC*]
[**\--crc-format**=*FORMAT*] [**\--list-crcs**] [**\--output-section**=*OUTPUTSECTION*] [**\--crc-format**=*FORMAT*] [**\--use-vma**] [**\--list-crcs**] [**\--output-section**=*OUTPUTSECTION*]
[**\--start-magic**=*MAGIC*] [**\--section**=*SECTION*] [**\--help**] [**\--usage**] [**\--start-magic**=*MAGIC*] [**\--section**=*SECTION*] [**\--help**] [**\--usage**]
[**\--version**] *ELF* [**\--version**] *ELF*
@ -50,6 +50,9 @@
**-F** *FORMAT*, **\--crc-format**=*FORMAT* **-F** *FORMAT*, **\--crc-format**=*FORMAT*
: Output format to place in output section. Options for *FORMAT* are *bare* or *struct* : Output format to place in output section. Options for *FORMAT* are *bare* or *struct*
**--use_vma**
: Use the virtual memory address (VMA) instead of the load memory address (LMA) for the address fields in the struct output. This option is only considered if the format is *struct*
**--start-magic**=*MAGIC*, **--endmagic**=*MAGIC* **--start-magic**=*MAGIC*, **--endmagic**=*MAGIC*
: *MAGIC* numbers (32 bit unsigned) that are expected to be found at the start and the end of the given output section. This serves as safety guard against accidental corruption of the output file. *It is highly recommended to use these options*. : *MAGIC* numbers (32 bit unsigned) that are expected to be found at the start and the end of the given output section. This serves as safety guard against accidental corruption of the output file. *It is highly recommended to use these options*.

View File

@ -38,13 +38,15 @@ const char *argp_program_bug_address = "<mario [dot] huettel [at] linux [dot] co
#define ARG_KEY_LIST (4) #define ARG_KEY_LIST (4)
#define ARG_KEY_EXPORT (5) #define ARG_KEY_EXPORT (5)
#define ARG_KEY_IMPORT (6) #define ARG_KEY_IMPORT (6)
#define ARG_KEY_XSD (7) #define ARG_KEY_USE_VMA (7)
#define ARG_KEY_XSD (8)
struct command_line_options { struct command_line_options {
bool little_endian; bool little_endian;
bool dry_run; bool dry_run;
bool verbose; bool verbose;
bool print_xsd; bool print_xsd;
bool use_vma;
enum granularity granularity; enum granularity granularity;
enum crc_format format; enum crc_format format;
struct crc_settings crc; struct crc_settings crc;
@ -99,6 +101,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case ARG_KEY_XSD: case ARG_KEY_XSD:
args->print_xsd = true; args->print_xsd = true;
break; break;
case ARG_KEY_USE_VMA:
args->use_vma = true;
break;
case 'p': case 'p':
/* Polyniomial */ /* Polyniomial */
args->crc.polynomial = strtoull(arg, &endptr, 0); args->crc.polynomial = strtoull(arg, &endptr, 0);
@ -189,6 +194,7 @@ static int parse_cmdline_options(int *argc, char ***argv, struct command_line_op
{"export", ARG_KEY_EXPORT, "XML", 0, "Export CRCs to XML file", 3}, {"export", ARG_KEY_EXPORT, "XML", 0, "Export CRCs to XML file", 3},
{"import", ARG_KEY_IMPORT, "XML", 0, "Do not caclulate CRCs but import them from file", 3}, {"import", ARG_KEY_IMPORT, "XML", 0, "Do not caclulate CRCs but import them from file", 3},
{"xsd", ARG_KEY_XSD, 0, 0, "Print XSD to stdout", 0}, {"xsd", ARG_KEY_XSD, 0, 0, "Print XSD to stdout", 0},
{"use-vma", ARG_KEY_USE_VMA, 0, 0, "Use the VMA instead of the LMA for struct output", 2},
/* Sentinel */ /* Sentinel */
{NULL, 0, 0, 0, NULL, 0} {NULL, 0, 0, 0, NULL, 0}
}; };
@ -214,6 +220,7 @@ static void prepare_default_opts(struct command_line_options *opts)
opts->granularity = GRANULARITY_BYTE; opts->granularity = GRANULARITY_BYTE;
opts->dry_run = false; opts->dry_run = false;
opts->crc.xor = 0UL; opts->crc.xor = 0UL;
opts->use_vma = false;
opts->crc.polynomial = 0x104C11DB7UL; opts->crc.polynomial = 0x104C11DB7UL;
opts->crc.start_value = 0xFFFFFFFFUL; opts->crc.start_value = 0xFFFFFFFFUL;
opts->crc.rev = false; opts->crc.rev = false;
@ -308,7 +315,7 @@ static int check_all_sections_present(elfpatch_handle_t *ep, SlList *list)
if (!ep) if (!ep)
return -1001; return -1001;
if (!list) { if (!list) {
print_err("No input sections specified.\n") print_err("No input sections specified.\n");
return -1; return -1;
} }
for (iter = list; iter; iter = sl_list_next(iter)) { for (iter = list; iter; iter = sl_list_next(iter)) {
@ -426,6 +433,10 @@ int main(int argc, char **argv)
return -2; return -2;
} }
if (cmd_opts.use_vma && cmd_opts.format != FORMAT_STRUCT) {
print_warn("--use-vma option only has an effect when exporting as struct output.");
}
if (!cmd_opts.output_section && cmd_opts.export_xml == NULL) { if (!cmd_opts.output_section && cmd_opts.export_xml == NULL) {
print_err("No output section / XML export specified. Will continue but not create any output\n"); print_err("No output section / XML export specified. Will continue but not create any output\n");
} }

View File

@ -39,7 +39,7 @@ int xml_write_crcs_to_file(const char *path, const uint32_t *crcs, SlList *secti
writer = xmlNewTextWriterFilename(path, 0); writer = xmlNewTextWriterFilename(path, 0);
if (!writer) { if (!writer) {
print_err("Cannot create XML file %s\n", path) print_err("Cannot create XML file %s\n", path);
ret = -1; ret = -1;
goto ret_none; goto ret_none;
} }
@ -393,7 +393,7 @@ struct xml_crc_import *xml_import_from_file(const char *path)
/* Get all CRCs */ /* Get all CRCs */
xpath_obj = xmlXPathEvalExpression(BAD_CAST "/patchelfcrc/sections/crc", xpath_ctx); xpath_obj = xmlXPathEvalExpression(BAD_CAST "/patchelfcrc/sections/crc", xpath_ctx);
if (xmlXPathNodeSetIsEmpty(xpath_obj->nodesetval)) { if (xmlXPathNodeSetIsEmpty(xpath_obj->nodesetval)) {
print_err("Internal error during read\n") print_err("Internal error during read\n");
xml_crc_import_free(ret); xml_crc_import_free(ret);
ret = NULL; ret = NULL;
goto ret_close_doc; goto ret_close_doc;