mirror of
https://github.com/openwrt/openwrt.git
synced 2024-12-21 06:33:41 +00:00
firmware-utils: add hex pattern mode for xorimage
This commit adds "hex pattern mode" to xorimage. This mode allows xor with a hexadecimal pattern that cannot be expressed with ASCII charactors. usage (example): xorimage -i firmware.bin -o firmware.bin.new -p 6A57190601121E4C -x Signed-off-by: INAGAKI Hiroshi <musashino.open@gmail.com> Signed-off-by: Christian Lamparter <chunkeey@gmail.com> (fix checkpatch)
This commit is contained in:
parent
10a54e1442
commit
9145f5209f
@ -20,11 +20,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
static char default_pattern[] = "12345678";
|
static char default_pattern[] = "12345678";
|
||||||
|
static int is_hex_pattern;
|
||||||
|
|
||||||
|
|
||||||
int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
|
int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
|
||||||
@ -43,7 +45,7 @@ void usage(void) __attribute__ (( __noreturn__ ));
|
|||||||
|
|
||||||
void usage(void)
|
void usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>]\n");
|
fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>] [-x]\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,12 +58,14 @@ int main(int argc, char **argv)
|
|||||||
char *ifn = NULL;
|
char *ifn = NULL;
|
||||||
char *ofn = NULL;
|
char *ofn = NULL;
|
||||||
const char *pattern = default_pattern;
|
const char *pattern = default_pattern;
|
||||||
|
char hex_pattern[128];
|
||||||
|
unsigned int hex_buf;
|
||||||
int c;
|
int c;
|
||||||
int v0, v1, v2;
|
int v0, v1, v2;
|
||||||
size_t n;
|
size_t n;
|
||||||
int p_len, p_off = 0;
|
int p_len, p_off = 0;
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "i:o:p:h")) != -1) {
|
while ((c = getopt(argc, argv, "i:o:p:xh")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'i':
|
case 'i':
|
||||||
ifn = optarg;
|
ifn = optarg;
|
||||||
@ -72,6 +76,9 @@ int main(int argc, char **argv)
|
|||||||
case 'p':
|
case 'p':
|
||||||
pattern = optarg;
|
pattern = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'x':
|
||||||
|
is_hex_pattern = true;
|
||||||
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
@ -100,6 +107,27 @@ int main(int argc, char **argv)
|
|||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_hex_pattern) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if ((p_len / 2) > sizeof(hex_pattern)) {
|
||||||
|
fprintf(stderr, "provided hex pattern is too long\n");
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_len % 2 != 0) {
|
||||||
|
fprintf(stderr, "the number of characters (hex) is incorrect\n");
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < (p_len / 2); i++) {
|
||||||
|
if (sscanf(pattern + (i * 2), "%2x", &hex_buf) < 0) {
|
||||||
|
fprintf(stderr, "invalid hex digit around %d\n", i * 2);
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
hex_pattern[i] = (char)hex_buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
|
while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
|
||||||
if (n < sizeof(buf)) {
|
if (n < sizeof(buf)) {
|
||||||
@ -110,7 +138,12 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p_off = xor_data(buf, n, pattern, p_len, p_off);
|
if (is_hex_pattern) {
|
||||||
|
p_off = xor_data(buf, n, hex_pattern, (p_len / 2),
|
||||||
|
p_off);
|
||||||
|
} else {
|
||||||
|
p_off = xor_data(buf, n, pattern, p_len, p_off);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fwrite(buf, n, 1, out)) {
|
if (!fwrite(buf, n, 1, out)) {
|
||||||
FWRITE_ERROR:
|
FWRITE_ERROR:
|
||||||
|
Loading…
Reference in New Issue
Block a user