view miscprog/memwrite-grep.c @ 215:d69f7512e3c1

Pirelli: documented and verified the checksum scheme used for the factory block
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 25 Dec 2016 23:48:16 +0000
parents 8bdc87c0fc03
children
line wrap: on
line source

/*
 * This utility performs a memmem() binary "grep", checking to see if a given
 * binary file (firmware image) contains the $l1tm_mem_write implementation
 * code found in our TCS211 objects and in that one C11x fw version for which
 * we have the map file.
 */

#define	_GNU_SOURCE
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

u_char needle[50] = {
	0x30, 0xb5,
	0x45, 0x68,
	0x42, 0x78,
	0x04, 0x3a,
	0x12, 0x06,
	0x13, 0x0e,
	0x00, 0x2b,
	0x09, 0xd0,
	0x00, 0x22,
	0x14, 0x18,
	0x24, 0x7a,
	0x54, 0x55,
	0x52, 0x1c,
	0x12, 0x04,
	0x12, 0x0c,
	0x01, 0x3b,
	0x00, 0x2b,
	0xf6, 0xd1,
	0x00, 0x20,
	0x88, 0x80,
	0x00, 0x06,
	0x00, 0x0e,
	0x48, 0x70,
	0x88, 0x70,
	0x30, 0xbd
};

u_char *haystack;
size_t haystack_size;

read_file(filename)
	char *filename;
{
	int fd;
	struct stat st;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		perror(filename);
		exit(1);
	}
	fstat(fd, &st);
	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr, "error: %s is not a regular file\n", filename);
		exit(1);
	}
	haystack_size = st.st_size;
	haystack = malloc(haystack_size);
	if (!haystack) {
		fprintf(stderr, "unable to malloc buffer for %s\n", filename);
		exit(1);
	}
	read(fd, haystack, haystack_size);
	close(fd);
}

main(argc, argv)
	char **argv;
{
	u_char *result;

	if (argc != 2) {
		fprintf(stderr, "usage: %s firmware.bin\n", argv[0]);
		exit(1);
	}
	read_file(argv[1]);
	result = memmem(haystack, haystack_size, needle, sizeof needle);
	if (result)
		printf("Found the needle bytes at offset 0x%x\n",
			result - haystack);
	else
		printf("Needle bytes not found in this image\n");
	exit(0);
}