2009-12-23 11:52:30 +00:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-12-29 05:01:36 +00:00
|
|
|
#include <openbsc/debug.h>
|
|
|
|
|
2009-12-23 12:09:27 +00:00
|
|
|
#include <arpa/inet.h>
|
2009-12-23 11:52:30 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-12-29 04:43:57 +00:00
|
|
|
#define PART_LENGTH 138
|
2009-12-23 11:52:30 +00:00
|
|
|
|
2009-12-29 13:27:10 +00:00
|
|
|
struct sdp_firmware_start {
|
2009-12-24 09:19:51 +00:00
|
|
|
char magic[4];
|
|
|
|
char more_magic[4];
|
2009-12-29 13:27:10 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct sdp_firmware {
|
2009-12-29 07:08:47 +00:00
|
|
|
u_int32_t header_length;
|
|
|
|
u_int32_t file_length;
|
2009-12-24 09:19:51 +00:00
|
|
|
char sw_part[20];
|
|
|
|
char text1[122];
|
2009-12-29 07:08:47 +00:00
|
|
|
u_int16_t part_length;
|
2009-12-24 09:19:51 +00:00
|
|
|
/* stuff i don't know */
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2009-12-29 05:01:36 +00:00
|
|
|
struct sdp_header_entry {
|
2009-12-29 07:09:54 +00:00
|
|
|
u_int16_t something1;
|
|
|
|
char text1[64];
|
|
|
|
char time[12];
|
|
|
|
char date[14];
|
|
|
|
char text2[10];
|
|
|
|
char text3[20];
|
2009-12-29 12:02:19 +00:00
|
|
|
u_int32_t length;
|
2009-12-29 07:09:54 +00:00
|
|
|
u_int32_t addr1;
|
|
|
|
u_int32_t addr2;
|
2009-12-29 12:02:19 +00:00
|
|
|
u_int32_t start;
|
2009-12-29 05:01:36 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
static_assert(sizeof(struct sdp_header_entry) == 138, right_entry);
|
2009-12-29 13:27:10 +00:00
|
|
|
static_assert(sizeof(struct sdp_firmware_start) + sizeof(struct sdp_firmware) == 160, _right_header_length);
|
2009-12-29 05:01:36 +00:00
|
|
|
|
2009-12-23 12:03:12 +00:00
|
|
|
/* more magic, the second "int" in the header */
|
|
|
|
static char more_magic[] = { 0x10, 0x02, 0x00, 0x0 };
|
|
|
|
|
2009-12-23 11:52:30 +00:00
|
|
|
|
2009-12-29 12:02:19 +00:00
|
|
|
static void analyze_file(int fd, const unsigned int st_size, const unsigned int base_offset)
|
2009-12-23 11:52:30 +00:00
|
|
|
{
|
2009-12-29 13:27:10 +00:00
|
|
|
struct sdp_firmware_start *firmware_start;
|
2009-12-24 09:19:51 +00:00
|
|
|
struct sdp_firmware *firmware_header;
|
2009-12-23 11:52:30 +00:00
|
|
|
char buf[4096];
|
2009-12-29 07:09:54 +00:00
|
|
|
int rc, i;
|
2009-12-29 13:27:10 +00:00
|
|
|
unsigned int start_offset = 0;
|
2009-12-23 11:52:30 +00:00
|
|
|
|
2009-12-29 13:27:10 +00:00
|
|
|
rc = read(fd, buf, sizeof(*firmware_start));
|
2009-12-24 09:19:51 +00:00
|
|
|
if (rc < 0) {
|
2009-12-29 13:27:10 +00:00
|
|
|
perror("Can not read header start.");
|
2009-12-23 11:52:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-29 13:27:10 +00:00
|
|
|
firmware_start = (struct sdp_firmware_start *) &buf[0];
|
|
|
|
if (strncmp(firmware_start->magic, " SDP", 4) != 0) {
|
2009-12-24 09:19:51 +00:00
|
|
|
fprintf(stderr, "Wrong magic.\n");
|
2009-12-23 12:03:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-29 13:27:10 +00:00
|
|
|
if (memcmp(firmware_start->more_magic, more_magic, 4) != 0) {
|
2009-12-24 09:19:51 +00:00
|
|
|
fprintf(stderr, "Wrong more magic.\n");
|
2009-12-23 12:03:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-29 13:27:10 +00:00
|
|
|
|
|
|
|
start_offset = sizeof(*firmware_start);
|
|
|
|
rc = read(fd, &buf[start_offset], sizeof(*firmware_header));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Can not read header.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
firmware_header = (struct sdp_firmware *) &buf[start_offset];
|
|
|
|
start_offset += sizeof(*firmware_header);
|
|
|
|
|
2009-12-23 11:52:30 +00:00
|
|
|
printf("Printing header information:\n");
|
2009-12-24 09:19:51 +00:00
|
|
|
printf("header_length: %u\n", ntohl(firmware_header->header_length));
|
|
|
|
printf("file_length: %u\n", ntohl(firmware_header->file_length));
|
|
|
|
printf("sw_part: %.20s\n", firmware_header->sw_part);
|
2009-12-29 04:43:57 +00:00
|
|
|
printf("text1: %.120s\n", firmware_header->text1);
|
|
|
|
printf("items: %u (rest %u)\n", ntohs(firmware_header->part_length) / PART_LENGTH,
|
|
|
|
ntohs(firmware_header->part_length) % PART_LENGTH);
|
2009-12-27 13:03:11 +00:00
|
|
|
|
2009-12-29 12:02:19 +00:00
|
|
|
if (ntohl(firmware_header->file_length) != st_size) {
|
2009-12-27 13:03:11 +00:00
|
|
|
fprintf(stderr, "The filesize and the header do not match.\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-12-29 05:01:36 +00:00
|
|
|
|
|
|
|
if (ntohs(firmware_header->part_length) % PART_LENGTH != 0) {
|
|
|
|
fprintf(stderr, "The part length seems to be wrong.\n");
|
|
|
|
return;
|
|
|
|
}
|
2009-12-29 07:09:54 +00:00
|
|
|
|
|
|
|
/* look into each firmware now */
|
2009-12-29 07:27:21 +00:00
|
|
|
for (i = 0; i < ntohs(firmware_header->part_length) / PART_LENGTH; ++i) {
|
|
|
|
struct sdp_header_entry entry;
|
2009-12-29 13:27:10 +00:00
|
|
|
unsigned int offset = start_offset + base_offset;
|
2009-12-29 07:09:54 +00:00
|
|
|
offset += i * 138;
|
2009-12-29 07:27:21 +00:00
|
|
|
|
|
|
|
if (lseek(fd, offset, SEEK_SET) != offset) {
|
|
|
|
fprintf(stderr, "Can not seek to the offset: %u.\n", offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = read(fd, &entry, sizeof(entry));
|
|
|
|
if (rc != sizeof(entry)) {
|
|
|
|
fprintf(stderr, "Can not read the header entry.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Header Entry: %d\n", i);
|
|
|
|
printf("\tsomething1: %u\n", ntohs(entry.something1));
|
|
|
|
printf("\ttext1: %.64s\n", entry.text1);
|
|
|
|
printf("\ttime: %.12s\n", entry.time);
|
|
|
|
printf("\tdate: %.14s\n", entry.date);
|
|
|
|
printf("\ttext2: %.10s\n", entry.text2);
|
|
|
|
printf("\ttext3: %.20s\n", entry.text3);
|
|
|
|
printf("\taddr1: 0x%x\n", entry.addr1);
|
|
|
|
printf("\taddr2: 0x%x\n", entry.addr2);
|
2009-12-29 12:02:19 +00:00
|
|
|
printf("\tstart: 0x%x\n", ntohl(entry.start));
|
|
|
|
printf("\tlength: 0x%x\n", ntohl(entry.length));
|
|
|
|
|
|
|
|
/* now we need to find the SDP file... */
|
|
|
|
offset = ntohl(entry.start) + 4 + base_offset;
|
|
|
|
if (lseek(fd, offset, SEEK_SET) != offset) {
|
|
|
|
perror("can't seek to sdp");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-29 17:22:29 +00:00
|
|
|
printf("------> parsing\n");
|
|
|
|
analyze_file(fd, ntohl(entry.length), offset);
|
|
|
|
printf("<------ parsing\n");
|
2009-12-29 07:09:54 +00:00
|
|
|
}
|
2009-12-23 11:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int i, fd;
|
2009-12-29 12:02:19 +00:00
|
|
|
struct stat stat;
|
2009-12-23 11:52:30 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-12-29 12:02:19 +00:00
|
|
|
/* verify the file */
|
|
|
|
if (fstat(fd, &stat) == -1) {
|
|
|
|
perror("Can not stat the file");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
analyze_file(fd, stat.st_size, 0);
|
2009-12-23 11:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|