blob: 8fdc2e9a4776fad88a711d2b33a874fdcfb82428 [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
31
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010032struct sdp_firmware {
33 char magic[4];
34 char more_magic[4];
35 unsigned int header_length;
36 unsigned int file_length;
37 char sw_part[20];
38 char text1[122];
39 u_int8_t no_idea_1[4];
40 char text2[64];
41 char time[8];
42 u_int8_t no_idea_2[4];
43 char date[8];
44 u_int8_t no_idea_3[6];
45 /* stuff i don't know */
46} __attribute__((packed));
47
Holger Hans Peter Freythere0d2ff42009-12-23 13:03:12 +010048/* more magic, the second "int" in the header */
49static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 };
50
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010051
52static void analyze_file(int fd)
53{
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010054 struct sdp_firmware *firmware_header;
Holger Hans Peter Freyther30b9ecd2009-12-27 14:03:11 +010055 struct stat stat;
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010056 char buf[4096];
57 int rc;
58
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010059 rc = read(fd, buf, sizeof(*firmware_header));
60 if (rc < 0) {
61 perror("can not read header");
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010062 return;
63 }
64
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010065 firmware_header = (struct sdp_firmware *) &buf[0];
66 if (strncmp(firmware_header->magic, " SDP", 4) != 0) {
67 fprintf(stderr, "Wrong magic.\n");
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010068 return;
69 }
70
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010071 if (memcmp(firmware_header->more_magic, more_magic, 4) != 0) {
72 fprintf(stderr, "Wrong more magic.\n");
Holger Hans Peter Freythere0d2ff42009-12-23 13:03:12 +010073 return;
74 }
75
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010076 printf("Printing header information:\n");
Holger Hans Peter Freyther9094cba2009-12-24 10:19:51 +010077 printf("header_length: %u\n", ntohl(firmware_header->header_length));
78 printf("file_length: %u\n", ntohl(firmware_header->file_length));
79 printf("sw_part: %.20s\n", firmware_header->sw_part);
80 printf("text1: %.122s\n", firmware_header->text1);
81 printf("text2: %.64s\n", firmware_header->text2);
82 printf("time: %.8s\n", firmware_header->time);
83 printf("date: %.8s\n", firmware_header->date);
Holger Hans Peter Freyther30b9ecd2009-12-27 14:03:11 +010084
85 /* verify the file */
86 if (fstat(fd, &stat) == -1) {
87 perror("Can not stat the file");
88 return;
89 }
90
91 if (ntohl(firmware_header->file_length) != stat.st_size) {
92 fprintf(stderr, "The filesize and the header do not match.\n");
93 return;
94 }
Holger Hans Peter Freyther65d67dc2009-12-23 12:52:30 +010095}
96
97int main(int argc, char** argv)
98{
99 int i, fd;
100
101 for (i = 1; i < argc; ++i) {
102 printf("Opening possible firmware '%s'\n", argv[i]);
103 fd = open(argv[i], O_RDONLY);
104 if (!fd) {
105 perror("nada");
106 continue;
107 }
108
109 analyze_file(fd);
110 }
111
112 return EXIT_SUCCESS;
113}