aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/ipaccess-firmware/ipaccess-firmware.c
blob: e2e5008c077ec5cda9daa909cc556909b086cd35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Routines for parsing an ipacces SDP firmware file */

/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* more magic, the second "int" in the header */
static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 };


static void analyze_file(int fd)
{
	char buf[4096];
	int rc;
	unsigned int absolute_size;

	rc = read(fd, buf, 4);
	if (rc <= 0) {
		fprintf(stderr, "Not enough space for the header.\n");
		return;
	}

	if (strcmp(buf, " SDP") != 0) {
		fprintf(stderr, "Wrong magic number at the beginning of the file.\n");
		return;
	}

	rc = read(fd, buf, 4);
	if (rc <= 0) {
		fprintf(stderr, "Not enough space for the more_magic.\n");
		return;
	}

	if (strncmp(buf, more_magic, 4) != 0) {
		fprintf(stderr, "The more magic is not right.\n");
		return;
	}

	rc = read(fd, buf, 4);
	if (rc <= 0) {
		fprintf(stderr, "Trying to read the header length failed.\n");
		return;
	}

	memcpy(&absolute_size, &buf[0], 4);
	absolute_size = ntohl(absolute_size);

	printf("Printing header information:\n");
	printf("The header is %u bytes long\n", absolute_size);
}

int main(int argc, char** argv)
{
	int i, fd;

	for (i = 1; i < argc; ++i) {
		printf("Opening possible firmware '%s'\n", argv[i]);
		fd = open(argv[i], O_RDONLY);
		if (!fd) {
			perror("nada");
			continue;
		}

		analyze_file(fd);
	}

	return EXIT_SUCCESS;
}