Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 1 | /* 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 Freyther | bf2bdc6 | 2009-12-23 13:09:27 +0100 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 23 | #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 Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 32 | struct 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 Freyther | e0d2ff4 | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 48 | /* more magic, the second "int" in the header */ |
| 49 | static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 }; |
| 50 | |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 51 | |
| 52 | static void analyze_file(int fd) |
| 53 | { |
Holger Hans Peter Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 54 | struct sdp_firmware *firmware_header; |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 55 | char buf[4096]; |
| 56 | int rc; |
| 57 | |
Holger Hans Peter Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 58 | rc = read(fd, buf, sizeof(*firmware_header)); |
| 59 | if (rc < 0) { |
| 60 | perror("can not read header"); |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
Holger Hans Peter Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 64 | firmware_header = (struct sdp_firmware *) &buf[0]; |
| 65 | if (strncmp(firmware_header->magic, " SDP", 4) != 0) { |
| 66 | fprintf(stderr, "Wrong magic.\n"); |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
Holger Hans Peter Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 70 | if (memcmp(firmware_header->more_magic, more_magic, 4) != 0) { |
| 71 | fprintf(stderr, "Wrong more magic.\n"); |
Holger Hans Peter Freyther | e0d2ff4 | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 75 | printf("Printing header information:\n"); |
Holger Hans Peter Freyther | 9094cba | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 76 | printf("header_length: %u\n", ntohl(firmware_header->header_length)); |
| 77 | printf("file_length: %u\n", ntohl(firmware_header->file_length)); |
| 78 | printf("sw_part: %.20s\n", firmware_header->sw_part); |
| 79 | printf("text1: %.122s\n", firmware_header->text1); |
| 80 | printf("text2: %.64s\n", firmware_header->text2); |
| 81 | printf("time: %.8s\n", firmware_header->time); |
| 82 | printf("date: %.8s\n", firmware_header->date); |
Holger Hans Peter Freyther | 65d67dc | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int main(int argc, char** argv) |
| 86 | { |
| 87 | int i, fd; |
| 88 | |
| 89 | for (i = 1; i < argc; ++i) { |
| 90 | printf("Opening possible firmware '%s'\n", argv[i]); |
| 91 | fd = open(argv[i], O_RDONLY); |
| 92 | if (!fd) { |
| 93 | perror("nada"); |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | analyze_file(fd); |
| 98 | } |
| 99 | |
| 100 | return EXIT_SUCCESS; |
| 101 | } |