2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
18 #include <sys/types.h>
27 #include "LzmaWrapper.h"
29 #define FW_NAME "/tmp/Firmware_Speedport_W921V_1.20.000.bin"
32 #define MAGIC_SZ 0x3FFC00
33 #if __BYTE_ORDER == __LITTLE_ENDIAN
34 #define MAGIC_PART 0x12345678
35 #define MAGIC_LZMA 0x8000005D
36 #define MAGIC_ANNEX_B 0x3C
37 #define MAGIC_TAPI 0x5A
39 #define MAGIC_PART 0x78563412
40 #define MAGIC_LZMA 0x5D000080
41 #define MAGIC_ANNEX_B 0x3C000000
42 #define MAGIC_TAPI 0x5A000000
46 const char* part_type(u_int32_t id)
50 return "/tmp/vr9_dsl_fw_annex_b.bin";
52 return "/tmp/vr9_tapi_fw.bin";
54 printf("\tUnknown lzma type 0x%02X\n", id);
55 return "/tmp/unknown.lzma";
58 int main(int argc, char **argv)
67 int start = 0, end = 0;
69 printf("Arcadyan Firmware cutter v0.1\n");
70 printf("-----------------------------\n");
71 printf("This tool extracts the different parts of an arcadyan firmware update file\n");
72 printf("This tool is for private use only. The Firmware that gets extracted has a license that forbids redistribution\n");
73 printf("Please only run this if you understand the risks\n\n");
74 printf("I understand the risks ? (y/N)\n");
79 if (stat(FW_NAME, &s) != 0) {
80 printf("Failed to find %s\n", FW_NAME);
81 printf("Ask Google or try http://hilfe.telekom.de/dlp/eki/downloads/Speedport/Speedport%20W%20921V/Firmware_Speedport_W921V_1.20.000.bin\n");
85 buf_orig = malloc(s.st_size);
87 printf("Failed to alloc %d bytes\n", s.st_size);
91 fd = open(FW_NAME, O_RDONLY);
93 printf("Unable to open %s\n", FW_NAME);
97 buflen = read(fd, buf_orig, s.st_size);
99 if (buflen != s.st_size) {
100 printf("Loaded %d instead of %d bytes inside %s\n", buflen, s.st_size, FW_NAME);
107 for (i = 0; i < MAGIC_SZ; i++) {
109 buf_orig[i] = buf_orig[i + 16] ^ MAGIC;
111 buf_orig[i] = buf_orig[i] ^ MAGIC;
114 memmove(&buf_orig[MAGIC_SZ], &buf_orig[MAGIC_SZ + 3], buflen - MAGIC_SZ);
117 buf = (u_int32_t*) buf_orig;
120 if (buf[end] == MAGIC_PART) {
122 printf("Found partition at 0x%08X with size %d\n",
123 start * sizeof(u_int32_t),
124 (end - start) * sizeof(u_int32_t));
125 if (buf[start] == MAGIC_LZMA) {
126 int dest_len = 1024 * 1024;
127 int len = buf[end - 3];
128 u_int32_t id = buf[end - 6];
129 const char *type = part_type(id);
132 dest = malloc(dest_len);
134 printf("Failed to alloc dest buffer\n");
138 if (lzma_inflate((u_int8_t*)&buf[start], len, dest, &dest_len)) {
139 printf("Failed to decompress data\n");
143 fd = creat(type, S_IRUSR | S_IWUSR);
145 if (write(fd, dest, dest_len) != dest_len)
146 printf("\tFailed to write %d bytes\n", dest_len);
148 printf("\tWrote %d bytes to %s\n", dest_len, type);
151 printf("\tFailed to open %s\n", type);
155 printf("\tThis is not lzma\n");
161 } while(end < buflen / sizeof(u_int32_t));