blob: 5cf99d428e452c04cbcf10a5bd2b4da01e4390c6 [file] [log] [blame]
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +01001/* Routines for parsing an ipacces SDP firmware file */
2
3/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Holger Hans Peter Freytherbf2bdc62009-12-23 13:09:27 +010022#include <arpa/inet.h>
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010023#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
Holger Hans Peter Freyther46a920c2009-12-29 05:43:57 +010031#define PART_LENGTH 138
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010032
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010033struct sdp_firmware {
34 char magic[4];
35 char more_magic[4];
36 unsigned int header_length;
37 unsigned int file_length;
38 char sw_part[20];
39 char text1[122];
Holger Hans Peter Freyther46a920c2009-12-29 05:43:57 +010040 unsigned int short part_length;
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010041 /* stuff i don't know */
42} __attribute__((packed));
43
Holger Hans Peter Freythere0d2ff42009-12-23 13:03:12 +010044/* more magic, the second "int" in the header */
45static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 };
46
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010047
48static void analyze_file(int fd)
49{
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010050 struct sdp_firmware *firmware_header;
Holger Hans Peter Freyther30b9ecd2009-12-27 14:03:11 +010051 struct stat stat;
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010052 char buf[4096];
53 int rc;
54
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010055 rc = read(fd, buf, sizeof(*firmware_header));
56 if (rc < 0) {
57 perror("can not read header");
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010058 return;
59 }
60
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010061 firmware_header = (struct sdp_firmware *) &buf[0];
62 if (strncmp(firmware_header->magic, " SDP", 4) != 0) {
63 fprintf(stderr, "Wrong magic.\n");
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010064 return;
65 }
66
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010067 if (memcmp(firmware_header->more_magic, more_magic, 4) != 0) {
68 fprintf(stderr, "Wrong more magic.\n");
Holger Hans Peter Freythere0d2ff42009-12-23 13:03:12 +010069 return;
70 }
71
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010072 printf("Printing header information:\n");
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010073 printf("header_length: %u\n", ntohl(firmware_header->header_length));
74 printf("file_length: %u\n", ntohl(firmware_header->file_length));
75 printf("sw_part: %.20s\n", firmware_header->sw_part);
Holger Hans Peter Freyther46a920c2009-12-29 05:43:57 +010076 printf("text1: %.120s\n", firmware_header->text1);
77 printf("items: %u (rest %u)\n", ntohs(firmware_header->part_length) / PART_LENGTH,
78 ntohs(firmware_header->part_length) % PART_LENGTH);
Holger Hans Peter Freyther30b9ecd2009-12-27 14:03:11 +010079
80 /* verify the file */
81 if (fstat(fd, &stat) == -1) {
82 perror("Can not stat the file");
83 return;
84 }
85
86 if (ntohl(firmware_header->file_length) != stat.st_size) {
87 fprintf(stderr, "The filesize and the header do not match.\n");
88 return;
89 }
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010090}
91
92int main(int argc, char** argv)
93{
94 int i, fd;
95
96 for (i = 1; i < argc; ++i) {
97 printf("Opening possible firmware '%s'\n", argv[i]);
98 fd = open(argv[i], O_RDONLY);
99 if (!fd) {
100 perror("nada");
101 continue;
102 }
103
104 analyze_file(fd);
105 }
106
107 return EXIT_SUCCESS;
108}