Holger Hans Peter Freyther | 2281797 | 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 | eb9766f | 2009-12-29 06:01:36 +0100 | [diff] [blame] | 22 | #include <openbsc/debug.h> |
| 23 | |
Holger Hans Peter Freyther | 93e2976 | 2009-12-23 13:09:27 +0100 | [diff] [blame] | 24 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
Holger Hans Peter Freyther | 0a4deca | 2009-12-29 05:43:57 +0100 | [diff] [blame] | 33 | #define PART_LENGTH 138 |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 35 | struct sdp_firmware { |
| 36 | char magic[4]; |
| 37 | char more_magic[4]; |
Holger Hans Peter Freyther | 3614ac7 | 2009-12-29 08:08:47 +0100 | [diff] [blame] | 38 | u_int32_t header_length; |
| 39 | u_int32_t file_length; |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 40 | char sw_part[20]; |
| 41 | char text1[122]; |
Holger Hans Peter Freyther | 3614ac7 | 2009-12-29 08:08:47 +0100 | [diff] [blame] | 42 | u_int16_t part_length; |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 43 | /* stuff i don't know */ |
| 44 | } __attribute__((packed)); |
| 45 | |
Holger Hans Peter Freyther | eb9766f | 2009-12-29 06:01:36 +0100 | [diff] [blame] | 46 | struct sdp_header_entry { |
Holger Hans Peter Freyther | 21c87d1 | 2009-12-29 08:09:54 +0100 | [diff] [blame] | 47 | u_int16_t something1; |
| 48 | char text1[64]; |
| 49 | char time[12]; |
| 50 | char date[14]; |
| 51 | char text2[10]; |
| 52 | char text3[20]; |
| 53 | u_int32_t something2; |
| 54 | u_int32_t addr1; |
| 55 | u_int32_t addr2; |
| 56 | u_int32_t something3; |
Holger Hans Peter Freyther | eb9766f | 2009-12-29 06:01:36 +0100 | [diff] [blame] | 57 | } __attribute__((packed)); |
| 58 | |
| 59 | static_assert(sizeof(struct sdp_header_entry) == 138, right_entry); |
| 60 | |
Holger Hans Peter Freyther | e4cef6c | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 61 | /* more magic, the second "int" in the header */ |
| 62 | static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 }; |
| 63 | |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 64 | |
| 65 | static void analyze_file(int fd) |
| 66 | { |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 67 | struct sdp_firmware *firmware_header; |
Holger Hans Peter Freyther | b7f5313 | 2009-12-27 14:03:11 +0100 | [diff] [blame] | 68 | struct stat stat; |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 69 | char buf[4096]; |
Holger Hans Peter Freyther | 21c87d1 | 2009-12-29 08:09:54 +0100 | [diff] [blame] | 70 | int rc, i; |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 71 | |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 72 | rc = read(fd, buf, sizeof(*firmware_header)); |
| 73 | if (rc < 0) { |
| 74 | perror("can not read header"); |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 78 | firmware_header = (struct sdp_firmware *) &buf[0]; |
| 79 | if (strncmp(firmware_header->magic, " SDP", 4) != 0) { |
| 80 | fprintf(stderr, "Wrong magic.\n"); |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 84 | if (memcmp(firmware_header->more_magic, more_magic, 4) != 0) { |
| 85 | fprintf(stderr, "Wrong more magic.\n"); |
Holger Hans Peter Freyther | e4cef6c | 2009-12-23 13:03:12 +0100 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 89 | printf("Printing header information:\n"); |
Holger Hans Peter Freyther | 0306798 | 2009-12-24 10:19:51 +0100 | [diff] [blame] | 90 | printf("header_length: %u\n", ntohl(firmware_header->header_length)); |
| 91 | printf("file_length: %u\n", ntohl(firmware_header->file_length)); |
| 92 | printf("sw_part: %.20s\n", firmware_header->sw_part); |
Holger Hans Peter Freyther | 0a4deca | 2009-12-29 05:43:57 +0100 | [diff] [blame] | 93 | printf("text1: %.120s\n", firmware_header->text1); |
| 94 | printf("items: %u (rest %u)\n", ntohs(firmware_header->part_length) / PART_LENGTH, |
| 95 | ntohs(firmware_header->part_length) % PART_LENGTH); |
Holger Hans Peter Freyther | b7f5313 | 2009-12-27 14:03:11 +0100 | [diff] [blame] | 96 | |
| 97 | /* verify the file */ |
| 98 | if (fstat(fd, &stat) == -1) { |
| 99 | perror("Can not stat the file"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (ntohl(firmware_header->file_length) != stat.st_size) { |
| 104 | fprintf(stderr, "The filesize and the header do not match.\n"); |
| 105 | return; |
| 106 | } |
Holger Hans Peter Freyther | eb9766f | 2009-12-29 06:01:36 +0100 | [diff] [blame] | 107 | |
| 108 | if (ntohs(firmware_header->part_length) % PART_LENGTH != 0) { |
| 109 | fprintf(stderr, "The part length seems to be wrong.\n"); |
| 110 | return; |
| 111 | } |
Holger Hans Peter Freyther | 21c87d1 | 2009-12-29 08:09:54 +0100 | [diff] [blame] | 112 | |
| 113 | /* look into each firmware now */ |
Holger Hans Peter Freyther | 1bc845e | 2009-12-29 08:27:21 +0100 | [diff] [blame] | 114 | for (i = 0; i < ntohs(firmware_header->part_length) / PART_LENGTH; ++i) { |
| 115 | struct sdp_header_entry entry; |
Holger Hans Peter Freyther | 21c87d1 | 2009-12-29 08:09:54 +0100 | [diff] [blame] | 116 | unsigned int offset = sizeof(struct sdp_firmware); |
| 117 | offset += i * 138; |
Holger Hans Peter Freyther | 1bc845e | 2009-12-29 08:27:21 +0100 | [diff] [blame] | 118 | |
| 119 | if (lseek(fd, offset, SEEK_SET) != offset) { |
| 120 | fprintf(stderr, "Can not seek to the offset: %u.\n", offset); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | rc = read(fd, &entry, sizeof(entry)); |
| 125 | if (rc != sizeof(entry)) { |
| 126 | fprintf(stderr, "Can not read the header entry.\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | printf("Header Entry: %d\n", i); |
| 131 | printf("\tsomething1: %u\n", ntohs(entry.something1)); |
| 132 | printf("\ttext1: %.64s\n", entry.text1); |
| 133 | printf("\ttime: %.12s\n", entry.time); |
| 134 | printf("\tdate: %.14s\n", entry.date); |
| 135 | printf("\ttext2: %.10s\n", entry.text2); |
| 136 | printf("\ttext3: %.20s\n", entry.text3); |
| 137 | printf("\tsomething2: 0x%x\n", ntohl(entry.something2)); |
| 138 | printf("\taddr1: 0x%x\n", entry.addr1); |
| 139 | printf("\taddr2: 0x%x\n", entry.addr2); |
| 140 | printf("\tsomething3: 0x%x\n", ntohl(entry.something3)); |
Holger Hans Peter Freyther | 21c87d1 | 2009-12-29 08:09:54 +0100 | [diff] [blame] | 141 | } |
Holger Hans Peter Freyther | 2281797 | 2009-12-23 12:52:30 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int main(int argc, char** argv) |
| 145 | { |
| 146 | int i, fd; |
| 147 | |
| 148 | for (i = 1; i < argc; ++i) { |
| 149 | printf("Opening possible firmware '%s'\n", argv[i]); |
| 150 | fd = open(argv[i], O_RDONLY); |
| 151 | if (!fd) { |
| 152 | perror("nada"); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | analyze_file(fd); |
| 157 | } |
| 158 | |
| 159 | return EXIT_SUCCESS; |
| 160 | } |