1/*
2 * patcher.c - ADAM2 patcher for Netgear DG834 (and compatible)
3 *
4 * Copyright (C) 2006 Felix Fietkau
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <stddef.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <stdint.h>
23#include <sys/mman.h>
24#include <sys/stat.h>
25#include <string.h>
26
27#include <sys/ioctl.h>
28
29int main(int argc, char **argv)
30{
31	int fd;
32	char *ptr;
33	uint32_t *i;
34
35	if (argc != 2) {
36		fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
37		exit(1);
38	}
39
40	if (((fd = open(argv[1], O_RDWR)) < 0)
41			|| ((ptr = mmap(0, 128 * 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1))) {
42		fprintf(stderr, "Can't open file\n");
43		exit(1);
44	}
45
46	i = (uint32_t *) &ptr[0x3944];
47	if (*i == 0x0c000944) {
48		fprintf(stderr, "Unpatched ADAM2 detected. Patching... ");
49		*i = 0x00000000;
50		msync(i, sizeof(*i), MS_SYNC|MS_INVALIDATE);
51		fprintf(stderr, "done!\n");
52	} else if (*i == 0x00000000) {
53		fprintf(stderr, "Patched ADAM2 detected.\n");
54	} else {
55		fprintf(stderr, "Unknown ADAM2 detected. Can't patch!\n");
56	}
57
58	close(fd);
59}
60