view bootutil/c139-patch-dmagic.c @ 30:2d60f9a3032b default tip

LICENSE: public domain, copied from freecalypso-tools
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 24 Jun 2023 04:24:38 +0000
parents 6b0d533046e5
children
line wrap: on
line source

/*
 * This program patches DD DD DD DD magic bytes into the given binary file
 * at offset 0x2060, thereby turning a locked C11x/12x or C139/140 flash image
 * into an unlocked one.
 */

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

static u_char magic[4] = {0xDD, 0xDD, 0xDD, 0xDD};

main(argc, argv)
	char **argv;
{
	int fd;
	struct stat st;

	if (argc != 2) {
		fprintf(stderr, "usage: %s flashimage.bin\n", argv[0]);
		exit(1);
	}
	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror(argv[1]);
		exit(1);
	}
	fstat(fd, &st);
	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr, "error: %s is not a regular file\n", argv[1]);
		exit(1);
	}
	if (st.st_size < 0x2064) {
		fprintf(stderr, "error: %s is too short\n", argv[1]);
		exit(1);
	}
	lseek(fd, 0x2060, SEEK_SET);
	if (write(fd, magic, 4) != 4) {
		perror("error writing to file");
		exit(1);
	}
	exit(0);
}