fcd0106e4595865a1c4b44536ac768403d73aa06
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.21.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(unsigned int 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
)
61 unsigned char *buf_orig
;
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.21.000.bin\n");
85 buf_orig
= malloc(s
.st_size
);
86 buf
= malloc(s
.st_size
);
87 if (!buf_orig
|| !buf
) {
88 printf("Failed to alloc %d bytes\n", s
.st_size
);
92 fd
= open(FW_NAME
, O_RDONLY
);
94 printf("Unable to open %s\n", FW_NAME
);
99 buflen
= read(fd
, buf_orig
, s
.st_size
);
101 if (buflen
!= s
.st_size
) {
102 printf("Loaded %d instead of %d bytes inside %s\n", buflen
, s
.st_size
, FW_NAME
);
109 for (i
= 0; i
< MAGIC_SZ
; i
++) {
111 buf_orig
[i
] = buf_orig
[i
+ 16] ^ MAGIC
;
113 buf_orig
[i
] = buf_orig
[i
] ^ MAGIC
;
116 memmove(&buf_orig
[MAGIC_SZ
], &buf_orig
[MAGIC_SZ
+ 3], buflen
- MAGIC_SZ
);
117 memcpy(buf
, buf_orig
, s
.st_size
);
121 if (buf
[end
] == MAGIC_PART
) {
123 printf("Found partition at 0x%08X with size %d\n",
124 start
* sizeof(unsigned int),
125 (end
- start
) * sizeof(unsigned int));
126 if (buf
[start
] == MAGIC_LZMA
) {
127 int dest_len
= 1024 * 1024;
128 int len
= buf
[end
- 3];
129 unsigned int id
= buf
[end
- 6];
130 const char *type
= part_type(id
);
133 dest
= malloc(dest_len
);
135 printf("Failed to alloc dest buffer\n");
139 if (lzma_inflate((unsigned char*)&buf
[start
], len
, dest
, &dest_len
)) {
140 printf("Failed to decompress data\n");
144 fd
= creat(type
, S_IRUSR
| S_IWUSR
);
146 if (write(fd
, dest
, dest_len
) != dest_len
)
147 printf("\tFailed to write %d bytes\n", dest_len
);
149 printf("\tWrote %d bytes to %s\n", dest_len
, type
);
152 printf("\tFailed to open %s\n", type
);
156 printf("\tThis is not lzma\n");
162 } while(end
< buflen
/ sizeof(unsigned int));