summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/MTP/beastpatcher/mtp_win32.c464
-rw-r--r--utils/MTP/sendfirm_win.c174
-rw-r--r--utils/disassembler/arm/disasm_arm.c846
-rw-r--r--utils/disassembler/arm/main.c264
-rw-r--r--utils/jz4740_tools/HXFmerge.c642
-rw-r--r--utils/jz4740_tools/HXFreplace.c484
-rw-r--r--utils/jz4740_tools/HXFsplit.c642
7 files changed, 1758 insertions, 1758 deletions
diff --git a/utils/MTP/beastpatcher/mtp_win32.c b/utils/MTP/beastpatcher/mtp_win32.c
index 7a7c6db769..15d0d705e2 100644
--- a/utils/MTP/beastpatcher/mtp_win32.c
+++ b/utils/MTP/beastpatcher/mtp_win32.c
@@ -1,232 +1,232 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * 8 *
9 * $Id$ 9 * $Id$
10 * 10 *
11 * Copyright (c) 2009, Dave Chapman 11 * Copyright (c) 2009, Dave Chapman
12 * All rights reserved. 12 * All rights reserved.
13 * 13 *
14 * Redistribution and use in source and binary forms, with or without 14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are 15 * modification, are permitted provided that the following conditions are
16 * met: 16 * met:
17 * 17 *
18 * * Redistributions of source code must retain the above copyright 18 * * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer. 19 * notice, this list of conditions and the following disclaimer.
20 * 20 *
21 * * Redistributions in binary form must reproduce the above 21 * * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following 22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided 23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution. 24 * with the distribution.
25 * 25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * 37 *
38 * 38 *
39 ****************************************************************************/ 39 ****************************************************************************/
40 40
41#include <stdio.h> 41#include <stdio.h>
42#include <string.h> 42#include <string.h>
43#include <stddef.h> 43#include <stddef.h>
44#include <stdlib.h> 44#include <stdlib.h>
45#include <wchar.h> 45#include <wchar.h>
46#include <windows.h> 46#include <windows.h>
47#include <sys/types.h> 47#include <sys/types.h>
48#include <sys/stat.h> 48#include <sys/stat.h>
49#include <tchar.h> 49#include <tchar.h>
50 50
51#include "mtp_common.h" 51#include "mtp_common.h"
52 52
53#include "../MTP_DLL/MTP_DLL.h" 53#include "../MTP_DLL/MTP_DLL.h"
54 54
55 55
56static int filesize(const char* filename); 56static int filesize(const char* filename);
57 57
58 58
59int mtp_init(struct mtp_info_t* mtp_info) 59int mtp_init(struct mtp_info_t* mtp_info)
60{ 60{
61 /* Fill the info struct with zeros - mainly for the strings */ 61 /* Fill the info struct with zeros - mainly for the strings */
62 memset(mtp_info, 0, sizeof(struct mtp_info_t)); 62 memset(mtp_info, 0, sizeof(struct mtp_info_t));
63 63
64 return 0; 64 return 0;
65 65
66} 66}
67 67
68int mtp_finished(struct mtp_info_t* mtp_info) 68int mtp_finished(struct mtp_info_t* mtp_info)
69{ 69{
70 (void)mtp_info; 70 (void)mtp_info;
71 71
72 return 0; 72 return 0;
73} 73}
74 74
75int mtp_scan(struct mtp_info_t* mtp_info) 75int mtp_scan(struct mtp_info_t* mtp_info)
76{ 76{
77 wchar_t name[256]; 77 wchar_t name[256];
78 wchar_t manufacturer[256]; 78 wchar_t manufacturer[256];
79 DWORD version; 79 DWORD version;
80 int num = 0; 80 int num = 0;
81 81
82 num = mtp_description(name, manufacturer, &version); 82 num = mtp_description(name, manufacturer, &version);
83 83
84 wcstombs(mtp_info->manufacturer, manufacturer, 200); 84 wcstombs(mtp_info->manufacturer, manufacturer, 200);
85 wcstombs(mtp_info->modelname, name, 200); 85 wcstombs(mtp_info->modelname, name, 200);
86 86
87 sprintf(mtp_info->version, "%x", (unsigned int)version); 87 sprintf(mtp_info->version, "%x", (unsigned int)version);
88 return (num > 0) ? num : -1; 88 return (num > 0) ? num : -1;
89 89
90} 90}
91 91
92static void callback(unsigned int progress, unsigned int max) 92static void callback(unsigned int progress, unsigned int max)
93{ 93{
94 int percent = (progress * 100) / max; 94 int percent = (progress * 100) / max;
95 95
96 printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent); 96 printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent);
97 fflush(stdout); 97 fflush(stdout);
98} 98}
99 99
100 100
101int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, 101int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf,
102 int fwsize) 102 int fwsize)
103{ 103{
104 HANDLE hTempFile; 104 HANDLE hTempFile;
105 DWORD dwRetVal; 105 DWORD dwRetVal;
106 DWORD dwBytesWritten; 106 DWORD dwBytesWritten;
107 UINT uRetVal; 107 UINT uRetVal;
108 TCHAR szTempName[1024]; 108 TCHAR szTempName[1024];
109 TCHAR lpPathBuffer[1024]; 109 TCHAR lpPathBuffer[1024];
110 BOOL fSuccess; 110 BOOL fSuccess;
111 wchar_t *tmp; 111 wchar_t *tmp;
112 int ret; 112 int ret;
113 113
114 (void)mtp_info; 114 (void)mtp_info;
115 115
116 /* Get the path for temporary files */ 116 /* Get the path for temporary files */
117 dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); 117 dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer);
118 if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) 118 if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0))
119 { 119 {
120 fprintf(stderr, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError()); 120 fprintf(stderr, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError());
121 return -1; 121 return -1;
122 } 122 }
123 123
124 /* Create the temporary file */ 124 /* Create the temporary file */
125 uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); 125 uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName);
126 if (uRetVal == 0) 126 if (uRetVal == 0)
127 { 127 {
128 fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); 128 fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError());
129 return -1; 129 return -1;
130 } 130 }
131 131
132 /* Now create the file */ 132 /* Now create the file */
133 hTempFile = CreateFile((LPTSTR) szTempName, // file name 133 hTempFile = CreateFile((LPTSTR) szTempName, // file name
134 GENERIC_READ | GENERIC_WRITE, // open r-w 134 GENERIC_READ | GENERIC_WRITE, // open r-w
135 0, // do not share 135 0, // do not share
136 NULL, // default security 136 NULL, // default security
137 CREATE_ALWAYS, // overwrite existing 137 CREATE_ALWAYS, // overwrite existing
138 FILE_ATTRIBUTE_NORMAL,// normal file 138 FILE_ATTRIBUTE_NORMAL,// normal file
139 NULL); // no template 139 NULL); // no template
140 if (hTempFile == INVALID_HANDLE_VALUE) 140 if (hTempFile == INVALID_HANDLE_VALUE)
141 { 141 {
142 fprintf(stderr, "[ERR] Could not create %s\n", szTempName); 142 fprintf(stderr, "[ERR] Could not create %s\n", szTempName);
143 return -1; 143 return -1;
144 } 144 }
145 145
146 fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); 146 fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL);
147 if (!fSuccess) 147 if (!fSuccess)
148 { 148 {
149 fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); 149 fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError());
150 return -1; 150 return -1;
151 } 151 }
152 152
153 fSuccess = CloseHandle (hTempFile); 153 fSuccess = CloseHandle (hTempFile);
154 if (!fSuccess) 154 if (!fSuccess)
155 { 155 {
156 fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); 156 fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError());
157 return -1; 157 return -1;
158 } 158 }
159 159
160 tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1); 160 tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1);
161 mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1); 161 mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1);
162 162
163 fprintf(stderr, "[INFO] Sending firmware...\n"); 163 fprintf(stderr, "[INFO] Sending firmware...\n");
164 if (mtp_sendnk(tmp, fwsize, &callback)) 164 if (mtp_sendnk(tmp, fwsize, &callback))
165 { 165 {
166 fprintf(stderr, "\n"); 166 fprintf(stderr, "\n");
167 fprintf(stderr, "[INFO] Firmware sent successfully\n"); 167 fprintf(stderr, "[INFO] Firmware sent successfully\n");
168 ret = 0; 168 ret = 0;
169 } 169 }
170 else 170 else
171 { 171 {
172 fprintf(stderr, "\n"); 172 fprintf(stderr, "\n");
173 fprintf(stderr, "[ERR] Error occured during sending.\n"); 173 fprintf(stderr, "[ERR] Error occured during sending.\n");
174 ret = -1; 174 ret = -1;
175 } 175 }
176 free(tmp); 176 free(tmp);
177 177
178 if (!DeleteFile(szTempName)) 178 if (!DeleteFile(szTempName))
179 fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName); 179 fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName);
180 180
181 return ret; 181 return ret;
182} 182}
183 183
184 184
185int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) 185int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename)
186{ 186{
187 wchar_t *fn; 187 wchar_t *fn;
188 188
189 fn = (LPWSTR)malloc(strlen(filename)*2+1); 189 fn = (LPWSTR)malloc(strlen(filename)*2+1);
190 mbstowcs(fn, filename, strlen(filename)*2+1); 190 mbstowcs(fn, filename, strlen(filename)*2+1);
191 191
192 if (mtp_init(mtp_info) < 0) { 192 if (mtp_init(mtp_info) < 0) {
193 fprintf(stderr,"[ERR] Can not init MTP\n"); 193 fprintf(stderr,"[ERR] Can not init MTP\n");
194 return 1; 194 return 1;
195 } 195 }
196 /* Scan for attached MTP devices. */ 196 /* Scan for attached MTP devices. */
197 if (mtp_scan(mtp_info) < 0) 197 if (mtp_scan(mtp_info) < 0)
198 { 198 {
199 fprintf(stderr,"[ERR] No devices found\n"); 199 fprintf(stderr,"[ERR] No devices found\n");
200 return 1; 200 return 1;
201 } 201 }
202 202
203 fprintf(stderr, "[INFO] Sending firmware...\n"); 203 fprintf(stderr, "[INFO] Sending firmware...\n");
204 if (mtp_sendnk(fn, filesize(filename), &callback)) 204 if (mtp_sendnk(fn, filesize(filename), &callback))
205 { 205 {
206 /* keep progress on screen */ 206 /* keep progress on screen */
207 printf("\n"); 207 printf("\n");
208 fprintf(stderr, "[INFO] Firmware sent successfully\n"); 208 fprintf(stderr, "[INFO] Firmware sent successfully\n");
209 return 0; 209 return 0;
210 } 210 }
211 else 211 else
212 { 212 {
213 fprintf(stderr, "[ERR] Error occured during sending.\n"); 213 fprintf(stderr, "[ERR] Error occured during sending.\n");
214 return -1; 214 return -1;
215 } 215 }
216 mtp_finished(mtp_info); 216 mtp_finished(mtp_info);
217} 217}
218 218
219 219
220static int filesize(const char* filename) 220static int filesize(const char* filename)
221{ 221{
222 struct _stat sb; 222 struct _stat sb;
223 int res; 223 int res;
224 224
225 res = _stat(filename, &sb); 225 res = _stat(filename, &sb);
226 if(res == -1) { 226 if(res == -1) {
227 fprintf(stderr, "Error getting filesize!\n"); 227 fprintf(stderr, "Error getting filesize!\n");
228 return -1; 228 return -1;
229 } 229 }
230 return sb.st_size; 230 return sb.st_size;
231} 231}
232 232
diff --git a/utils/MTP/sendfirm_win.c b/utils/MTP/sendfirm_win.c
index 3c45aa6ae7..c74b93bb37 100644
--- a/utils/MTP/sendfirm_win.c
+++ b/utils/MTP/sendfirm_win.c
@@ -1,87 +1,87 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 Maurus Cuelenaere 10 * Copyright (C) 2008 Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22 22
23#include <stdio.h> 23#include <stdio.h>
24#include <string.h> 24#include <string.h>
25#include <stddef.h> 25#include <stddef.h>
26#include <stdlib.h> 26#include <stdlib.h>
27#include <wchar.h> 27#include <wchar.h>
28#include <stdbool.h> 28#include <stdbool.h>
29#include <windows.h> 29#include <windows.h>
30 30
31#include "MTP_DLL/MTP_DLL.h" 31#include "MTP_DLL/MTP_DLL.h"
32 32
33void usage(void) 33void usage(void)
34{ 34{
35 fprintf(stderr, "usage: sendfirm <local filename>\n"); 35 fprintf(stderr, "usage: sendfirm <local filename>\n");
36} 36}
37 37
38int filesize(char* filename) 38int filesize(char* filename)
39{ 39{
40 FILE* fd; 40 FILE* fd;
41 int tmp; 41 int tmp;
42 fd = fopen(filename, "r"); 42 fd = fopen(filename, "r");
43 if(fd == NULL) 43 if(fd == NULL)
44 { 44 {
45 fprintf(stderr, "Error while opening %s!\n", filename); 45 fprintf(stderr, "Error while opening %s!\n", filename);
46 return -1; 46 return -1;
47 } 47 }
48 fseek(fd, 0, SEEK_END); 48 fseek(fd, 0, SEEK_END);
49 tmp = ftell(fd); 49 tmp = ftell(fd);
50 fclose(fd); 50 fclose(fd);
51 return tmp; 51 return tmp;
52} 52}
53 53
54void callback(unsigned int progress, unsigned int max) 54void callback(unsigned int progress, unsigned int max)
55{ 55{
56 unsigned int normalized = progress*1000/max; 56 unsigned int normalized = progress*1000/max;
57 printf("Progress: %d.%d%%\r", normalized/10, normalized%10); 57 printf("Progress: %d.%d%%\r", normalized/10, normalized%10);
58 fflush(stdout); 58 fflush(stdout);
59} 59}
60 60
61int main(int argc, char **argv) 61int main(int argc, char **argv)
62{ 62{
63 if (argc < 2) 63 if (argc < 2)
64 { 64 {
65 usage(); 65 usage();
66 return 1; 66 return 1;
67 } 67 }
68 68
69 wchar_t *tmp; 69 wchar_t *tmp;
70 70
71 tmp = (LPWSTR)malloc(strlen(argv[1])*2+1); 71 tmp = (LPWSTR)malloc(strlen(argv[1])*2+1);
72 mbstowcs(tmp, argv[1], strlen(argv[1])*2+1); 72 mbstowcs(tmp, argv[1], strlen(argv[1])*2+1);
73 73
74 wprintf(tmp); 74 wprintf(tmp);
75 printf("\n"); 75 printf("\n");
76 76
77 fprintf(stdout, "Sending firmware...\n"); 77 fprintf(stdout, "Sending firmware...\n");
78 78
79 if(mtp_sendnk(tmp, filesize(argv[1]), &callback)) 79 if(mtp_sendnk(tmp, filesize(argv[1]), &callback))
80 fprintf(stdout, "Firmware sent successfully!\n"); 80 fprintf(stdout, "Firmware sent successfully!\n");
81 else 81 else
82 fprintf(stdout, "Error occured during sending!\n"); 82 fprintf(stdout, "Error occured during sending!\n");
83 83
84 free(tmp); 84 free(tmp);
85 85
86 exit(0); 86 exit(0);
87} 87}
diff --git a/utils/disassembler/arm/disasm_arm.c b/utils/disassembler/arm/disasm_arm.c
index b23c6b0355..14e72d9583 100644
--- a/utils/disassembler/arm/disasm_arm.c
+++ b/utils/disassembler/arm/disasm_arm.c
@@ -1,423 +1,423 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3#include <stdint.h> 3#include <stdint.h>
4 4
5#define ULONG uint32_t 5#define ULONG uint32_t
6#define UCHAR uint8_t 6#define UCHAR uint8_t
7 7
8#define FRMT "0x%x" // "0x%x" 8#define FRMT "0x%x" // "0x%x"
9#define SHFTFRMC "%s %s #%d" // "%s %s %d" 9#define SHFTFRMC "%s %s #%d" // "%s %s %d"
10#define SHFTFRMR "%s %s %s" // "%s %s %s" 10#define SHFTFRMR "%s %s %s" // "%s %s %s"
11//#define FRMT "0x%x" 11//#define FRMT "0x%x"
12//#define SHFTFRMC "%s %s %d" 12//#define SHFTFRMC "%s %s %d"
13//#define SHFTFRMR "%s %s %s" 13//#define SHFTFRMR "%s %s %s"
14 14
15char *cond[16] = { "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le", "", "nv" }; 15char *cond[16] = { "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le", "", "nv" };
16char *cnd1[16] = { "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le", " ", "nv" }; 16char *cnd1[16] = { "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le", " ", "nv" };
17char *opcd[16] = {"and","eor","sub","rsb","add","adc","sbc","rsc","tst","teq","cmp","cmn","orr","mov","bic","mvn" }; 17char *opcd[16] = {"and","eor","sub","rsb","add","adc","sbc","rsc","tst","teq","cmp","cmn","orr","mov","bic","mvn" };
18char setc[32] = {0,115,0,115,0,115,0,115,0,115,0,115,0,115,0,115,0, 0 ,0, 0 ,0, 0 ,0, 0 ,0,115,0,115,0,115,0,115 }; 18char setc[32] = {0,115,0,115,0,115,0,115,0,115,0,115,0,115,0,115,0, 0 ,0, 0 ,0, 0 ,0, 0 ,0,115,0,115,0,115,0,115 };
19char *regs[16] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc" }; 19char *regs[16] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc" };
20 20
21char *shfts[4] = { "lsl", "lsr", "asr", "ror" }; 21char *shfts[4] = { "lsl", "lsr", "asr", "ror" };
22 22
23/* 23/*
2431-28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 2431-28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
25Cond 0 0 I ---Opcode--- S |----Rn----- ----Rd----- --------Operand 2-------- Data Processing /PSR Transfer 25Cond 0 0 I ---Opcode--- S |----Rn----- ----Rd----- --------Operand 2-------- Data Processing /PSR Transfer
26Cond 0 0 0 0 | 0 0 A S |----Rd----- ----Rn----- ---Rs---- 1 0 0 1 --Rm--- Multiply 26Cond 0 0 0 0 | 0 0 A S |----Rd----- ----Rn----- ---Rs---- 1 0 0 1 --Rm--- Multiply
27Cond 0 0 0 0 | 1 U A S |---RdHi---- ---RdLo---- ---Rn---- 1 0 0 1 --Rm--- Multiply Long 27Cond 0 0 0 0 | 1 U A S |---RdHi---- ---RdLo---- ---Rn---- 1 0 0 1 --Rm--- Multiply Long
28Cond 0 0 0 1 | 0 B 0 0 |----Rn----- ----Rd----- 0 0 0 0 1 0 0 1 --Rm--- Single Data Swap 28Cond 0 0 0 1 | 0 B 0 0 |----Rn----- ----Rd----- 0 0 0 0 1 0 0 1 --Rm--- Single Data Swap
29Cond 0 0 0 1 | 0 0 1 0 |1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 --Rn--- Branch and Exchange 29Cond 0 0 0 1 | 0 0 1 0 |1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 --Rn--- Branch and Exchange
30Cond 0 0 0 P | U 0 W L |----Rn----- ----Rd----- 0 0 0 0 1 S H 1 --Rm--- Halfword Data Transfer: register offset 30Cond 0 0 0 P | U 0 W L |----Rn----- ----Rd----- 0 0 0 0 1 S H 1 --Rm--- Halfword Data Transfer: register offset
31Cond 0 0 0 P | U 1 W L |----Rn----- ----Rd----- --Offset- 1 S H 1 -Offset Halfword Data Transfer: immediate offset 31Cond 0 0 0 P | U 1 W L |----Rn----- ----Rd----- --Offset- 1 S H 1 -Offset Halfword Data Transfer: immediate offset
32Cond 0 1 I P | U B W L |----Rn----- ----Rd----- --------Offset----------- Single Data Transfer 32Cond 0 1 I P | U B W L |----Rn----- ----Rd----- --------Offset----------- Single Data Transfer
33Cond 0 1 1 1 | x x x x |x x x x x x x x x x x x x x x 1 x x x x Undefined 33Cond 0 1 1 1 | x x x x |x x x x x x x x x x x x x x x 1 x x x x Undefined
34Cond 1 0 0 P | U S W L |----Rn----- -----------Register List------------- Block Data Transfer 34Cond 1 0 0 P | U S W L |----Rn----- -----------Register List------------- Block Data Transfer
35Cond 1 0 1 L | -------------------------Offset------------------------------ Branch 35Cond 1 0 1 L | -------------------------Offset------------------------------ Branch
36Cond 1 1 0 P | U N W L |----Rn----- ----CRd---- ---CP#--- -----Offset---- Coprocessor Data Transfer 36Cond 1 1 0 P | U N W L |----Rn----- ----CRd---- ---CP#--- -----Offset---- Coprocessor Data Transfer
37Cond 1 1 1 0 | --CP Opc---|----CRn---- ----CRd---- ---CP#--- -CP-- 0 --CRm-- Coprocessor Data Operation 37Cond 1 1 1 0 | --CP Opc---|----CRn---- ----CRd---- ---CP#--- -CP-- 0 --CRm-- Coprocessor Data Operation
38Cond 1 1 1 0 | CP Opc L |----CRn---- ----Rd----- ---CP#--- -CP-- 1 --CRm-- Coprocessor Register Transfer 38Cond 1 1 1 0 | CP Opc L |----CRn---- ----Rd----- ---CP#--- -CP-- 1 --CRm-- Coprocessor Register Transfer
39Cond 1 1 1 1 | x x x x |x x x x x x x x x x x x x x x x x x x x Software Interrupt 39Cond 1 1 1 1 | x x x x |x x x x x x x x x x x x x x x x x x x x Software Interrupt
40 40
410x04200000 410x04200000
420001 0 0 0 0 0 1 1 0 6 e 1 1 1 0 1 8 420001 0 0 0 0 0 1 1 0 6 e 1 1 1 0 1 8
43================================================================================ 43================================================================================
44Cond 0 1 I P | U B W L |----Rn----- ----Rd----- --------Offset----------- Single Data Transfer 44Cond 0 1 I P | U B W L |----Rn----- ----Rd----- --------Offset----------- Single Data Transfer
45 45
46 46
47EQ 0 Z set equal 47EQ 0 Z set equal
48NE 1 Z clear not equal 48NE 1 Z clear not equal
49CS 2 C set unsigned higher or same 49CS 2 C set unsigned higher or same
50CC 3 C clear unsigned lower 50CC 3 C clear unsigned lower
51MI 4 N set negative 51MI 4 N set negative
52PL 5 N clear positive or zero 52PL 5 N clear positive or zero
53VS 6 V set overflow 53VS 6 V set overflow
54VC 7 V clear no overflow 54VC 7 V clear no overflow
55HI 8 C set and Z clear unsigned higher 55HI 8 C set and Z clear unsigned higher
56LS 9 C clear or Z set unsigned lower or same 56LS 9 C clear or Z set unsigned lower or same
57GE A N equals V greater or equal 57GE A N equals V greater or equal
58LT B N not equal to V less than 58LT B N not equal to V less than
59GT C Z clear AND (N equals V) greater than 59GT C Z clear AND (N equals V) greater than
60LE D Z set OR (N not equal to V) less than or equal 60LE D Z set OR (N not equal to V) less than or equal
61AL E (ignored) always 61AL E (ignored) always
62 62
63AND 0 operand1 AND operand2 63AND 0 operand1 AND operand2
64EOR 1 operand1 EOR operand2 64EOR 1 operand1 EOR operand2
65SUB 2 operand1 - operand2 65SUB 2 operand1 - operand2
66RSB 3 operand2 - operand1 66RSB 3 operand2 - operand1
67ADD 4 operand1 + operand2 67ADD 4 operand1 + operand2
68ADC 5 operand1 + operand2 + carry 68ADC 5 operand1 + operand2 + carry
69SBC 6 operand1 - operand2 + carry - 1 69SBC 6 operand1 - operand2 + carry - 1
70RSC 7 operand2 - operand1 + carry - 1 70RSC 7 operand2 - operand1 + carry - 1
71TST 8 AND, but result is not written 71TST 8 AND, but result is not written
72TEQ 9 as EOR, but result is not written 72TEQ 9 as EOR, but result is not written
73CMP A as SUB, but result is not written 73CMP A as SUB, but result is not written
74CMN B as ADD, but result is not written 74CMN B as ADD, but result is not written
75ORR C operand1 OR operand2 75ORR C operand1 OR operand2
76MOV D operand2 (operand1 is ignored) 76MOV D operand2 (operand1 is ignored)
77BIC E operand1 AND NOT operand2 (Bit clear) 77BIC E operand1 AND NOT operand2 (Bit clear)
78MVN F NOT operand2 (operand1 is ignored) 78MVN F NOT operand2 (operand1 is ignored)
79*/ 79*/
80 80
81void multiply_stg(char *stg, ULONG val) 81void multiply_stg(char *stg, ULONG val)
82{ 82{
83 if((val&0xc00000) == 0) // simple mul 83 if((val&0xc00000) == 0) // simple mul
84 { 84 {
85 if(val & 0x100000) // set condition flags 85 if(val & 0x100000) // set condition flags
86 if(val & 0x200000) sprintf(stg+strlen(stg), "mla%ss ", cond[val>>28]); 86 if(val & 0x200000) sprintf(stg+strlen(stg), "mla%ss ", cond[val>>28]);
87 else sprintf(stg+strlen(stg), "mul%ss ", cond[val>>28]); 87 else sprintf(stg+strlen(stg), "mul%ss ", cond[val>>28]);
88 else 88 else
89 if(val & 0x200000) sprintf(stg+strlen(stg), "mla%s ", cnd1[val>>28]); 89 if(val & 0x200000) sprintf(stg+strlen(stg), "mla%s ", cnd1[val>>28]);
90 else sprintf(stg+strlen(stg), "mul%s ", cnd1[val>>28]); 90 else sprintf(stg+strlen(stg), "mul%s ", cnd1[val>>28]);
91 91
92 if(val & 0x200000) // accumulate 92 if(val & 0x200000) // accumulate
93 sprintf(stg+strlen(stg), "%s, %s, %s, %s", regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15], regs[(val>>12)&15]); 93 sprintf(stg+strlen(stg), "%s, %s, %s, %s", regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15], regs[(val>>12)&15]);
94 else 94 else
95 sprintf(stg+strlen(stg), "%s, %s, %s", regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15]); 95 sprintf(stg+strlen(stg), "%s, %s, %s", regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15]);
96 } 96 }
97 else 97 else
98 { 98 {
99 if(val & 0x100000) // set condition flags 99 if(val & 0x100000) // set condition flags
100 if(val & 0x200000) // accumulate 100 if(val & 0x200000) // accumulate
101 if(val & 0x400000) sprintf(stg+strlen(stg), "smlal%ss ", cond[val>>28]); 101 if(val & 0x400000) sprintf(stg+strlen(stg), "smlal%ss ", cond[val>>28]);
102 else sprintf(stg+strlen(stg), "umlal%ss ", cond[val>>28]); 102 else sprintf(stg+strlen(stg), "umlal%ss ", cond[val>>28]);
103 else 103 else
104 if(val & 0x400000) sprintf(stg+strlen(stg), "smull%ss ", cond[val>>28]); 104 if(val & 0x400000) sprintf(stg+strlen(stg), "smull%ss ", cond[val>>28]);
105 else sprintf(stg+strlen(stg), "umull%ss ", cond[val>>28]); 105 else sprintf(stg+strlen(stg), "umull%ss ", cond[val>>28]);
106 else 106 else
107 if(val & 0x200000) 107 if(val & 0x200000)
108 if(val & 0x400000) sprintf(stg+strlen(stg), "smlal%s ", cond[val>>28]); 108 if(val & 0x400000) sprintf(stg+strlen(stg), "smlal%s ", cond[val>>28]);
109 else sprintf(stg+strlen(stg), "umlal%s ", cond[val>>28]); 109 else sprintf(stg+strlen(stg), "umlal%s ", cond[val>>28]);
110 else 110 else
111 if(val & 0x400000) sprintf(stg+strlen(stg), "smull%s ", cond[val>>28]); 111 if(val & 0x400000) sprintf(stg+strlen(stg), "smull%s ", cond[val>>28]);
112 else sprintf(stg+strlen(stg), "umull%s ", cond[val>>28]); 112 else sprintf(stg+strlen(stg), "umull%s ", cond[val>>28]);
113 113
114 sprintf(stg+strlen(stg), "%s, %s, %s, %s", regs[(val>>12)&15], regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15]); 114 sprintf(stg+strlen(stg), "%s, %s, %s, %s", regs[(val>>12)&15], regs[(val>>16)&15], regs[(val>>0)&15], regs[(val>>8)&15]);
115 } 115 }
116} 116}
117 117
118void halfword_stg(char *stg, ULONG val) 118void halfword_stg(char *stg, ULONG val)
119{ 119{
120 ULONG off = ((val>>4) & 0xf0) + (val & 0x0f); 120 ULONG off = ((val>>4) & 0xf0) + (val & 0x0f);
121 121
122 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%s", cond[val>>28]); 122 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%s", cond[val>>28]);
123 else sprintf(stg+strlen(stg), "str%s", cond[val>>28]); 123 else sprintf(stg+strlen(stg), "str%s", cond[val>>28]);
124 124
125 switch((val>>5) & 3) // SWP, HW, SB, SH 125 switch((val>>5) & 3) // SWP, HW, SB, SH
126 { 126 {
127 case 0: sprintf(stg+strlen(stg), "error: SWP"); break; 127 case 0: sprintf(stg+strlen(stg), "error: SWP"); break;
128 case 1: sprintf(stg+strlen(stg), "h "); break; 128 case 1: sprintf(stg+strlen(stg), "h "); break;
129 case 2: sprintf(stg+strlen(stg), "sb "); break; 129 case 2: sprintf(stg+strlen(stg), "sb "); break;
130 case 3: sprintf(stg+strlen(stg), "sh "); break; 130 case 3: sprintf(stg+strlen(stg), "sh "); break;
131 } 131 }
132 132
133 if(val & 0x400000) // immidiate offset 133 if(val & 0x400000) // immidiate offset
134 if(val & 0x1000000) // pre index 134 if(val & 0x1000000) // pre index
135 if(val & 0x200000) // write back 135 if(val & 0x200000) // write back
136 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], off); 136 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], off);
137 else sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], off); 137 else sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], off);
138 else 138 else
139 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], off); 139 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], off);
140 else sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], off); 140 else sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], off);
141 else 141 else
142 if(val & 0x200000) // write back 142 if(val & 0x200000) // write back
143 sprintf(stg+strlen(stg), "error 'write back' on post indexed"); 143 sprintf(stg+strlen(stg), "error 'write back' on post indexed");
144 else 144 else
145 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s], "FRMT, regs[(val>>12)&15], regs[(val>>16)&15], off); 145 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s], "FRMT, regs[(val>>12)&15], regs[(val>>16)&15], off);
146 else sprintf(stg+strlen(stg), "%s, [%s], -"FRMT, regs[(val>>12)&15], regs[(val>>16)&15], off); 146 else sprintf(stg+strlen(stg), "%s, [%s], -"FRMT, regs[(val>>12)&15], regs[(val>>16)&15], off);
147 else 147 else
148 if(val & 0x1000000) // pre index 148 if(val & 0x1000000) // pre index
149 if(val & 0x200000) // write back 149 if(val & 0x200000) // write back
150 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, %s]!", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 150 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, %s]!", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
151 else sprintf(stg+strlen(stg), "%s, [%s, -%s]!", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 151 else sprintf(stg+strlen(stg), "%s, [%s, -%s]!", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
152 else 152 else
153 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, %s]", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 153 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s, %s]", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
154 else sprintf(stg+strlen(stg), "%s, [%s, -%s]", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 154 else sprintf(stg+strlen(stg), "%s, [%s, -%s]", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
155 else 155 else
156 if(val & 0x200000) // write back 156 if(val & 0x200000) // write back
157 sprintf(stg+strlen(stg), "error 'write back' on post indexed"); 157 sprintf(stg+strlen(stg), "error 'write back' on post indexed");
158 else 158 else
159 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s], %s", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 159 if(val & 0x800000) sprintf(stg+strlen(stg), "%s, [%s], %s", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
160 else sprintf(stg+strlen(stg), "%s, [%s], -%s", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]); 160 else sprintf(stg+strlen(stg), "%s, [%s], -%s", regs[(val>>12)&15], regs[(val>>16)&15], regs[val&15]);
161} 161}
162 162
163void branch_stg(char *stg, ULONG val, ULONG pos) 163void branch_stg(char *stg, ULONG val, ULONG pos)
164{ 164{
165 ULONG off = pos + (((int32_t)val << 8) >> 6) + 8; 165 ULONG off = pos + (((int32_t)val << 8) >> 6) + 8;
166 166
167 if((val & 0x0ffffff0) == 0x012fff10) // bx instruction 167 if((val & 0x0ffffff0) == 0x012fff10) // bx instruction
168 { sprintf(stg+strlen(stg), "bx%s %s", cond[val>>28], regs[val&15]); } 168 { sprintf(stg+strlen(stg), "bx%s %s", cond[val>>28], regs[val&15]); }
169 else 169 else
170 { 170 {
171 if(((val>>24)&15) == 10) sprintf(stg+strlen(stg), "b%s ", cond[val>>28]); 171 if(((val>>24)&15) == 10) sprintf(stg+strlen(stg), "b%s ", cond[val>>28]);
172 else sprintf(stg+strlen(stg), "bl%s ", cond[val>>28]); 172 else sprintf(stg+strlen(stg), "bl%s ", cond[val>>28]);
173 173
174 sprintf(stg+strlen(stg), "0x%x", off); 174 sprintf(stg+strlen(stg), "0x%x", off);
175 } 175 }
176} 176}
177 177
178void opcode_stg(char *stg, ULONG val, ULONG off) 178void opcode_stg(char *stg, ULONG val, ULONG off)
179{ 179{
180 ULONG des, op1; 180 ULONG des, op1;
181 char op2[80]; 181 char op2[80];
182 char *st = stg + strlen(stg); 182 char *st = stg + strlen(stg);
183 183
184 if(((val & 0x0ffffff0) == 0x012fff10) && (val & 16)) 184 if(((val & 0x0ffffff0) == 0x012fff10) && (val & 16))
185 { branch_stg(stg, val, off); return; } 185 { branch_stg(stg, val, off); return; }
186 else if(((val & 0x0f000000) == 0x00000000) && ((val & 0xf0) == 0x90)) 186 else if(((val & 0x0f000000) == 0x00000000) && ((val & 0xf0) == 0x90))
187 { multiply_stg(stg, val); return; } 187 { multiply_stg(stg, val); return; }
188 else if(((val & 0x0f000000) <= 0x01000000) && ((val & 0x90) == 0x90) && ((val & 0xf0) > 0x90) && ((val & 0x01200000) != 0x00200000)) 188 else if(((val & 0x0f000000) <= 0x01000000) && ((val & 0x90) == 0x90) && ((val & 0xf0) > 0x90) && ((val & 0x01200000) != 0x00200000))
189 { halfword_stg(stg, val); return; } 189 { halfword_stg(stg, val); return; }
190 190
191 sprintf(stg+strlen(stg), "%s%s%s ", opcd[(val>>21) & 15], cond[val>>28], setc[(val>>20) & 31]?"s":" "); 191 sprintf(stg+strlen(stg), "%s%s%s ", opcd[(val>>21) & 15], cond[val>>28], setc[(val>>20) & 31]?"s":" ");
192 192
193 des = (val>>12) & 15; 193 des = (val>>12) & 15;
194 op1 = (val>>16) & 15; 194 op1 = (val>>16) & 15;
195 195
196 if(val & 0x2000000) // immidiate 196 if(val & 0x2000000) // immidiate
197 { 197 {
198 off = (ULONG)((uint64_t)(val&0xff) << (32 - 2 * ((val >> 8) & 15))) | ((val&0xff) >> 2 * ((val >> 8) & 15)); 198 off = (ULONG)((uint64_t)(val&0xff) << (32 - 2 * ((val >> 8) & 15))) | ((val&0xff) >> 2 * ((val >> 8) & 15));
199 sprintf(op2, FRMT" ", off); 199 sprintf(op2, FRMT" ", off);
200 } 200 }
201 else 201 else
202 { 202 {
203 if(val & 16) // shift type 203 if(val & 16) // shift type
204 sprintf(op2, SHFTFRMR, regs[val&15], shfts[(val>>5)&3], regs[(val>>8)&15]); 204 sprintf(op2, SHFTFRMR, regs[val&15], shfts[(val>>5)&3], regs[(val>>8)&15]);
205 else 205 else
206 if((val>>7) & 31) 206 if((val>>7) & 31)
207 sprintf(op2, SHFTFRMC, regs[val&15], shfts[(val>>5)&3], (val>>7) & 31); 207 sprintf(op2, SHFTFRMC, regs[val&15], shfts[(val>>5)&3], (val>>7) & 31);
208 else 208 else
209 sprintf(op2, "%s ", regs[val&15]); 209 sprintf(op2, "%s ", regs[val&15]);
210 } 210 }
211 211
212 switch((val>>21) & 15) 212 switch((val>>21) & 15)
213 { 213 {
214 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 12: 214 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 12:
215 case 14: sprintf(stg+strlen(stg), "%s, %s, %s", regs[des], regs[op1], op2); break; 215 case 14: sprintf(stg+strlen(stg), "%s, %s, %s", regs[des], regs[op1], op2); break;
216 216
217 case 8: case 9: case 10: 217 case 8: case 9: case 10:
218 case 11: if(val & 0x100000) // set status 218 case 11: if(val & 0x100000) // set status
219 sprintf(stg+strlen(stg), "%s, %s", regs[op1], op2); // standard TEQ,TST,CMP,CMN 219 sprintf(stg+strlen(stg), "%s, %s", regs[op1], op2); // standard TEQ,TST,CMP,CMN
220 else 220 else
221 { //special MRS/MSR opcodes 221 { //special MRS/MSR opcodes
222 if((((val>>23) & 31) == 2) && ((val & 0x3f0fff) == 0x0f0000)) 222 if((((val>>23) & 31) == 2) && ((val & 0x3f0fff) == 0x0f0000))
223 { sprintf(st, "mrs%s %s, %s", cnd1[val>>28], regs[des], val&0x400000?"SPSR_xx":"CPSR"); } 223 { sprintf(st, "mrs%s %s, %s", cnd1[val>>28], regs[des], val&0x400000?"SPSR_xx":"CPSR"); }
224 else 224 else
225 if((((val>>23) & 31) == 2) && ((val & 0x30fff0) == 0x20f000)) 225 if((((val>>23) & 31) == 2) && ((val & 0x30fff0) == 0x20f000))
226 { sprintf(st, "msr%s %s, %s", cnd1[val>>28], val&0x400000?"SPSR_xx":"CPSR", regs[val&15]); } 226 { sprintf(st, "msr%s %s, %s", cnd1[val>>28], val&0x400000?"SPSR_xx":"CPSR", regs[val&15]); }
227 else 227 else
228 if((((val>>23) & 31) == 6) && ((val & 0x30f000) == 0x20f000)) 228 if((((val>>23) & 31) == 6) && ((val & 0x30f000) == 0x20f000))
229 { sprintf(st, "msr%s %s, %s", cnd1[val>>28], val&0x400000?"SPSR_xx":"CPSR_cf", op2); } 229 { sprintf(st, "msr%s %s, %s", cnd1[val>>28], val&0x400000?"SPSR_xx":"CPSR_cf", op2); }
230 else 230 else
231 if((((val>>23) & 31) == 2) && ((val & 0x300ff0) == 0x000090)) 231 if((((val>>23) & 31) == 2) && ((val & 0x300ff0) == 0x000090))
232 { sprintf(st, "swp%s%s %s, %s, [%s]", val&0x400000?"b":"", cnd1[val>>28], regs[(val>>12)&15], regs[val&15], regs[(val>>16)&15]); } 232 { sprintf(st, "swp%s%s %s, %s, [%s]", val&0x400000?"b":"", cnd1[val>>28], regs[(val>>12)&15], regs[val&15], regs[(val>>16)&15]); }
233 else 233 else
234 { sprintf(stg+strlen(stg), "??????????????"); } 234 { sprintf(stg+strlen(stg), "??????????????"); }
235 } break; 235 } break;
236 case 13: 236 case 13:
237 case 15: sprintf(stg+strlen(stg), "%s, %s", regs[des], op2); break; 237 case 15: sprintf(stg+strlen(stg), "%s, %s", regs[des], op2); break;
238 } 238 }
239} 239}
240 240
241void opcode_cop(char *stg, ULONG val, ULONG off) 241void opcode_cop(char *stg, ULONG val, ULONG off)
242{ 242{
243 char* op; 243 char* op;
244 int opcode1 = (val >> 21) & 0x7; 244 int opcode1 = (val >> 21) & 0x7;
245 int CRn = (val >> 16) & 0xf; 245 int CRn = (val >> 16) & 0xf;
246 int Rd = (val >> 12) & 0xf; 246 int Rd = (val >> 12) & 0xf;
247 int cp_num = (val >> 8) & 0xf; 247 int cp_num = (val >> 8) & 0xf;
248 int opcode2 = (val >> 5) & 0x7; 248 int opcode2 = (val >> 5) & 0x7;
249 int CRm = val & 0xf; 249 int CRm = val & 0xf;
250 250
251 251
252// ee073f5e mcr 15, 0, r3, cr7, cr14, {2} 252// ee073f5e mcr 15, 0, r3, cr7, cr14, {2}
253 253
254 if (val & (1<<4)) { 254 if (val & (1<<4)) {
255 if (val & (1<<20)) { 255 if (val & (1<<20)) {
256 op = "mrc"; 256 op = "mrc";
257 } else { 257 } else {
258 op = "mcr"; 258 op = "mcr";
259 } 259 }
260 opcode1 = (val >> 21) & 0x7; 260 opcode1 = (val >> 21) & 0x7;
261 CRn = (val >> 16) & 0xf; 261 CRn = (val >> 16) & 0xf;
262 Rd = (val >> 12) & 0xf; 262 Rd = (val >> 12) & 0xf;
263 cp_num = (val >> 8) & 0xf; 263 cp_num = (val >> 8) & 0xf;
264 opcode2 = (val >> 5) & 0x7; 264 opcode2 = (val >> 5) & 0x7;
265 CRm = val & 0xf; 265 CRm = val & 0xf;
266 266
267 sprintf(stg+strlen(stg), "%s%s %d, %d, r%d, cr%d, cr%d, {%d}", op, cnd1[val>>28], cp_num, opcode1, Rd, CRn, CRm, opcode2); 267 sprintf(stg+strlen(stg), "%s%s %d, %d, r%d, cr%d, cr%d, {%d}", op, cnd1[val>>28], cp_num, opcode1, Rd, CRn, CRm, opcode2);
268 } else { 268 } else {
269 op = "cdp"; 269 op = "cdp";
270 270
271 opcode1 = (val >> 20) & 0xf; 271 opcode1 = (val >> 20) & 0xf;
272 CRn = (val >> 16) & 0xf; 272 CRn = (val >> 16) & 0xf;
273 Rd = (val >> 12) & 0xf; 273 Rd = (val >> 12) & 0xf;
274 cp_num = (val >> 8) & 0xf; 274 cp_num = (val >> 8) & 0xf;
275 opcode2 = (val >> 5) & 0x7; 275 opcode2 = (val >> 5) & 0x7;
276 CRm = val & 0xf; 276 CRm = val & 0xf;
277 277
278 sprintf(stg+strlen(stg), "%s%s %d, %d, cr%d, cr%d, cr%d, {%d}", op, cnd1[val>>28], cp_num, opcode1, Rd, CRn, CRm, opcode2); 278 sprintf(stg+strlen(stg), "%s%s %d, %d, cr%d, cr%d, cr%d, {%d}", op, cnd1[val>>28], cp_num, opcode1, Rd, CRn, CRm, opcode2);
279 } 279 }
280 280
281} 281}
282 282
283 283
284void single_data(char *stg, ULONG val) 284void single_data(char *stg, ULONG val)
285{ 285{
286 char op2[80]; 286 char op2[80];
287 287
288 if(((val & 0x0e000000) == 0x06000000) && (val & 16)) 288 if(((val & 0x0e000000) == 0x06000000) && (val & 16))
289 { sprintf(stg+strlen(stg), "undef%s", cond[val>>28]); 289 { sprintf(stg+strlen(stg), "undef%s", cond[val>>28]);
290 return; 290 return;
291 } 291 }
292 292
293 if(val & 0x400000) 293 if(val & 0x400000)
294 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%sb ", cond[val>>28]); 294 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%sb ", cond[val>>28]);
295 else sprintf(stg+strlen(stg), "str%sb ", cond[val>>28]); 295 else sprintf(stg+strlen(stg), "str%sb ", cond[val>>28]);
296 else 296 else
297 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%s ", cnd1[val>>28]); 297 if(val & 0x100000) sprintf(stg+strlen(stg), "ldr%s ", cnd1[val>>28]);
298 else sprintf(stg+strlen(stg), "str%s ", cnd1[val>>28]); 298 else sprintf(stg+strlen(stg), "str%s ", cnd1[val>>28]);
299 299
300 if(val & 0x2000000) {// reg offset 300 if(val & 0x2000000) {// reg offset
301 if(val & 16) // shift type 301 if(val & 16) // shift type
302 sprintf(op2, "error: reg defined shift"); 302 sprintf(op2, "error: reg defined shift");
303 else 303 else
304 if((val>>7) & 31) 304 if((val>>7) & 31)
305 sprintf(op2, SHFTFRMC, regs[val&15], shfts[(val>>5)&3], (val>>7) & 31); 305 sprintf(op2, SHFTFRMC, regs[val&15], shfts[(val>>5)&3], (val>>7) & 31);
306 else 306 else
307 sprintf(op2, "%s", regs[val&15]); 307 sprintf(op2, "%s", regs[val&15]);
308 } 308 }
309 309
310 if(val & 0x2000000) // reg offset 310 if(val & 0x2000000) // reg offset
311 if(val & 0x1000000) // pre index 311 if(val & 0x1000000) // pre index
312 if(val & 0x800000) // up offset (+) 312 if(val & 0x800000) // up offset (+)
313 if(val & 0x200000) // write back 313 if(val & 0x200000) // write back
314 sprintf(stg+strlen(stg), "%s, [%s, %s]!", regs[(val>>12)&15], regs[(val>>16)&15], op2); 314 sprintf(stg+strlen(stg), "%s, [%s, %s]!", regs[(val>>12)&15], regs[(val>>16)&15], op2);
315 else 315 else
316 sprintf(stg+strlen(stg), "%s, [%s, %s]", regs[(val>>12)&15], regs[(val>>16)&15], op2); 316 sprintf(stg+strlen(stg), "%s, [%s, %s]", regs[(val>>12)&15], regs[(val>>16)&15], op2);
317 else 317 else
318 if(val & 0x200000) // write back 318 if(val & 0x200000) // write back
319 sprintf(stg+strlen(stg), "%s, [%s, -%s]!", regs[(val>>12)&15], regs[(val>>16)&15], op2); 319 sprintf(stg+strlen(stg), "%s, [%s, -%s]!", regs[(val>>12)&15], regs[(val>>16)&15], op2);
320 else 320 else
321 sprintf(stg+strlen(stg), "%s, [%s, -%s]", regs[(val>>12)&15], regs[(val>>16)&15], op2); 321 sprintf(stg+strlen(stg), "%s, [%s, -%s]", regs[(val>>12)&15], regs[(val>>16)&15], op2);
322 else 322 else
323 if(val & 0x200000) // write back 323 if(val & 0x200000) // write back
324 sprintf(stg+strlen(stg), "error 'write back' set"); 324 sprintf(stg+strlen(stg), "error 'write back' set");
325 else 325 else
326 if(val & 0x800000) // up offset (+) 326 if(val & 0x800000) // up offset (+)
327 sprintf(stg+strlen(stg), "%s, [%s], %s", regs[(val>>12)&15], regs[(val>>16)&15], op2); 327 sprintf(stg+strlen(stg), "%s, [%s], %s", regs[(val>>12)&15], regs[(val>>16)&15], op2);
328 else 328 else
329 sprintf(stg+strlen(stg), "%s, [%s], -%s", regs[(val>>12)&15], regs[(val>>16)&15], op2); 329 sprintf(stg+strlen(stg), "%s, [%s], -%s", regs[(val>>12)&15], regs[(val>>16)&15], op2);
330 else 330 else
331 if(val & 0x1000000) // pre index 331 if(val & 0x1000000) // pre index
332 if(val & 0x800000) // up offset (+) 332 if(val & 0x800000) // up offset (+)
333 if(val & 0x200000) // write back 333 if(val & 0x200000) // write back
334 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 334 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
335 else sprintf(stg+strlen(stg), "%s, [%s]!", regs[(val>>12)&15], regs[(val>>16)&15]); 335 else sprintf(stg+strlen(stg), "%s, [%s]!", regs[(val>>12)&15], regs[(val>>16)&15]);
336 else 336 else
337 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 337 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, "FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
338 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]); 338 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]);
339 else 339 else
340 if(val & 0x200000) // write back 340 if(val & 0x200000) // write back
341 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 341 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]!", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
342 else sprintf(stg+strlen(stg), "%s, [%s]!", regs[(val>>12)&15], regs[(val>>16)&15]); 342 else sprintf(stg+strlen(stg), "%s, [%s]!", regs[(val>>12)&15], regs[(val>>16)&15]);
343 else 343 else
344 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 344 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s, -"FRMT"]", regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
345 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]); 345 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]);
346 else 346 else
347 if(val & 0x200000) // write back 347 if(val & 0x200000) // write back
348 sprintf(stg+strlen(stg), "error 'write back' set"); 348 sprintf(stg+strlen(stg), "error 'write back' set");
349 else 349 else
350 if(val & 0x800000) // up offset (+) 350 if(val & 0x800000) // up offset (+)
351 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s], "FRMT, regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 351 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s], "FRMT, regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
352 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]); 352 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]);
353 else 353 else
354 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s], -"FRMT, regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff); 354 if(val & 0xfff) sprintf(stg+strlen(stg), "%s, [%s], -"FRMT, regs[(val>>12)&15], regs[(val>>16)&15], val & 0xfff);
355 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]); 355 else sprintf(stg+strlen(stg), "%s, [%s]", regs[(val>>12)&15], regs[(val>>16)&15]);
356} 356}
357 357
358void block_data(char *stg, ULONG val) 358void block_data(char *stg, ULONG val)
359{ 359{
360 char lst[80]; 360 char lst[80];
361 int i; 361 int i;
362 362
363 strcpy(lst, "{"); 363 strcpy(lst, "{");
364 for(i=0; i<16; i++) 364 for(i=0; i<16; i++)
365 if(val & (1<<i)) 365 if(val & (1<<i))
366 sprintf(lst+strlen(lst), "%s, ", regs[i]); 366 sprintf(lst+strlen(lst), "%s, ", regs[i]);
367 if(strlen(lst)>2) 367 if(strlen(lst)>2)
368 strcpy(lst+strlen(lst)-2, "}"); 368 strcpy(lst+strlen(lst)-2, "}");
369 else 369 else
370 strcpy(lst+strlen(lst), "}"); 370 strcpy(lst+strlen(lst), "}");
371 371
372 if(val & 0x400000) // load psr or force user mode 372 if(val & 0x400000) // load psr or force user mode
373 strcpy(lst+strlen(lst), "^"); 373 strcpy(lst+strlen(lst), "^");
374 374
375 375
376 if(val & 0x100000) // load 376 if(val & 0x100000) // load
377 if(val & 0x1000000) // pre offset 377 if(val & 0x1000000) // pre offset
378 if(val & 0x800000) sprintf(stg+strlen(stg), "ldm%sib ", cond[val>>28]); 378 if(val & 0x800000) sprintf(stg+strlen(stg), "ldm%sib ", cond[val>>28]);
379 else sprintf(stg+strlen(stg), "ldm%sdb ", cond[val>>28]); 379 else sprintf(stg+strlen(stg), "ldm%sdb ", cond[val>>28]);
380 else 380 else
381 if(val & 0x800000) sprintf(stg+strlen(stg), "ldm%sia ", cond[val>>28]); 381 if(val & 0x800000) sprintf(stg+strlen(stg), "ldm%sia ", cond[val>>28]);
382 else sprintf(stg+strlen(stg), "ldm%sda ", cond[val>>28]); 382 else sprintf(stg+strlen(stg), "ldm%sda ", cond[val>>28]);
383 else 383 else
384 if(val & 0x1000000) 384 if(val & 0x1000000)
385 if(val & 0x800000) sprintf(stg+strlen(stg), "stm%sib ", cond[val>>28]); 385 if(val & 0x800000) sprintf(stg+strlen(stg), "stm%sib ", cond[val>>28]);
386 else sprintf(stg+strlen(stg), "stm%sdb ", cond[val>>28]); 386 else sprintf(stg+strlen(stg), "stm%sdb ", cond[val>>28]);
387 else 387 else
388 if(val & 0x800000) sprintf(stg+strlen(stg), "stm%sia ", cond[val>>28]); 388 if(val & 0x800000) sprintf(stg+strlen(stg), "stm%sia ", cond[val>>28]);
389 else sprintf(stg+strlen(stg), "stm%sda ", cond[val>>28]); 389 else sprintf(stg+strlen(stg), "stm%sda ", cond[val>>28]);
390 390
391 switch((val>>21)&3) 391 switch((val>>21)&3)
392 { 392 {
393 case 0: sprintf(stg+strlen(stg), "%s, %s", regs[(val>>16)&15], lst); break; 393 case 0: sprintf(stg+strlen(stg), "%s, %s", regs[(val>>16)&15], lst); break;
394 case 1: sprintf(stg+strlen(stg), "%s!, %s", regs[(val>>16)&15], lst); break; 394 case 1: sprintf(stg+strlen(stg), "%s!, %s", regs[(val>>16)&15], lst); break;
395 case 2: sprintf(stg+strlen(stg), "%s, %s", regs[(val>>16)&15], lst); break; 395 case 2: sprintf(stg+strlen(stg), "%s, %s", regs[(val>>16)&15], lst); break;
396 case 3: sprintf(stg+strlen(stg), "%s!, %s", regs[(val>>16)&15], lst); break; 396 case 3: sprintf(stg+strlen(stg), "%s!, %s", regs[(val>>16)&15], lst); break;
397 } 397 }
398} 398}
399 399
400void dis_asm(ULONG off, ULONG val, char *stg) 400void dis_asm(ULONG off, ULONG val, char *stg)
401{ 401{
402 sprintf(stg, "%6x: %08x ", off, val); 402 sprintf(stg, "%6x: %08x ", off, val);
403 403
404 switch((val >> 24) & 15) 404 switch((val >> 24) & 15)
405 { 405 {
406 case 0: 406 case 0:
407 case 1: 407 case 1:
408 case 2: 408 case 2:
409 case 3: opcode_stg(stg, val, off); break; 409 case 3: opcode_stg(stg, val, off); break;
410 case 4: 410 case 4:
411 case 5: 411 case 5:
412 case 6: 412 case 6:
413 case 7: single_data(stg, val); break; 413 case 7: single_data(stg, val); break;
414 case 8: 414 case 8:
415 case 9: block_data(stg, val); break; 415 case 9: block_data(stg, val); break;
416 case 10: 416 case 10:
417 case 11: branch_stg(stg, val, off); break; 417 case 11: branch_stg(stg, val, off); break;
418 case 12: 418 case 12:
419 case 13: sprintf(stg+strlen(stg), "cop%s", cnd1[val>>28]); break; 419 case 13: sprintf(stg+strlen(stg), "cop%s", cnd1[val>>28]); break;
420 case 14: opcode_cop(stg, val, off); break; 420 case 14: opcode_cop(stg, val, off); break;
421 case 15: sprintf(stg+strlen(stg), "swi%s", cnd1[val>>28]); break; 421 case 15: sprintf(stg+strlen(stg), "swi%s", cnd1[val>>28]); break;
422 } 422 }
423} 423}
diff --git a/utils/disassembler/arm/main.c b/utils/disassembler/arm/main.c
index 0edc633785..674696405c 100644
--- a/utils/disassembler/arm/main.c
+++ b/utils/disassembler/arm/main.c
@@ -1,132 +1,132 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <stdint.h> 4#include <stdint.h>
5 5
6#define ULONG uint32_t 6#define ULONG uint32_t
7#define USHORT uint16_t 7#define USHORT uint16_t
8#define UCHAR uint8_t 8#define UCHAR uint8_t
9 9
10ULONG isdata[1000000]; /* each bit defines one byte as: code=0, data=1 */ 10ULONG isdata[1000000]; /* each bit defines one byte as: code=0, data=1 */
11 11
12extern void dis_asm(ULONG off, ULONG val, char *stg); 12extern void dis_asm(ULONG off, ULONG val, char *stg);
13 13
14int static inline le2int(unsigned char* buf) 14int static inline le2int(unsigned char* buf)
15{ 15{
16 int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 16 int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
17 17
18 return res; 18 return res;
19} 19}
20 20
21int main(int argc, char **argv) 21int main(int argc, char **argv)
22{ 22{
23 FILE *in, *out; 23 FILE *in, *out;
24 char *ptr, stg[256]; 24 char *ptr, stg[256];
25 unsigned char buf[4]; 25 unsigned char buf[4];
26 ULONG pos, sz, val, loop; 26 ULONG pos, sz, val, loop;
27 int offset, offset1; 27 int offset, offset1;
28 USHORT regid; 28 USHORT regid;
29 29
30 if(argc == 1 || strcmp(argv[1], "--help") == 0) 30 if(argc == 1 || strcmp(argv[1], "--help") == 0)
31 { printf("Usage: arm_disass [input file]\n"); 31 { printf("Usage: arm_disass [input file]\n");
32 printf(" disassembles input file to 'disasm.txt'\n"); 32 printf(" disassembles input file to 'disasm.txt'\n");
33 exit(-1); 33 exit(-1);
34 } 34 }
35 35
36 in = fopen(argv[1], "rb"); 36 in = fopen(argv[1], "rb");
37 if(in == NULL) 37 if(in == NULL)
38 { printf("Cannot open %s", argv[1]); 38 { printf("Cannot open %s", argv[1]);
39 exit(-1); 39 exit(-1);
40 } 40 }
41 41
42 out = fopen("disasm.txt", "w"); 42 out = fopen("disasm.txt", "w");
43 if(out == NULL) exit(-1); 43 if(out == NULL) exit(-1);
44 44
45 fseek(in, 0, SEEK_END); 45 fseek(in, 0, SEEK_END);
46 sz = ftell(in); 46 sz = ftell(in);
47 47
48 /* first loop only sets data/code tags */ 48 /* first loop only sets data/code tags */
49 for(loop=0; loop<2; loop++) 49 for(loop=0; loop<2; loop++)
50 { 50 {
51 for(pos=0; pos<sz; pos+=4) 51 for(pos=0; pos<sz; pos+=4)
52 { 52 {
53 /* clear disassembler string start */ 53 /* clear disassembler string start */
54 memset(stg, 0, 40); 54 memset(stg, 0, 40);
55 /* read next code dword */ 55 /* read next code dword */
56 fseek(in, pos, SEEK_SET); 56 fseek(in, pos, SEEK_SET);
57 fread(buf, 1, 4, in); 57 fread(buf, 1, 4, in);
58 58
59 val = le2int(buf); 59 val = le2int(buf);
60 60
61 /* check for data tag set: if 1 byte out of 4 is marked => assume data */ 61 /* check for data tag set: if 1 byte out of 4 is marked => assume data */
62 if((isdata[pos>>5] & (0xf << (pos & 31))) || (val & 0xffff0000) == 0) 62 if((isdata[pos>>5] & (0xf << (pos & 31))) || (val & 0xffff0000) == 0)
63 { 63 {
64 sprintf(stg, "%6x: %08x", pos, val); 64 sprintf(stg, "%6x: %08x", pos, val);
65 } 65 }
66 else 66 else
67 { 67 {
68 dis_asm(pos, val, stg); 68 dis_asm(pos, val, stg);
69 69
70 /* check for instant mov operation */ 70 /* check for instant mov operation */
71 if(memcmp(stg+17, "mov ", 4) == 0 && (ptr=strstr(stg, "0x")) != NULL) 71 if(memcmp(stg+17, "mov ", 4) == 0 && (ptr=strstr(stg, "0x")) != NULL)
72 { 72 {
73 regid = *(USHORT*)(stg+22); 73 regid = *(USHORT*)(stg+22);
74 74
75 sscanf(ptr+2, "%x", &offset); 75 sscanf(ptr+2, "%x", &offset);
76 if(ptr[-1] == '-') 76 if(ptr[-1] == '-')
77 offset = -offset; 77 offset = -offset;
78 } 78 }
79 else 79 else
80 /* check for add/sub operation */ 80 /* check for add/sub operation */
81 if((ptr=strstr(stg, "0x")) != NULL 81 if((ptr=strstr(stg, "0x")) != NULL
82 && (memcmp(stg+17, "add ", 4) == 0 || memcmp(stg+17, "sub ", 4) == 0)) 82 && (memcmp(stg+17, "add ", 4) == 0 || memcmp(stg+17, "sub ", 4) == 0))
83 { 83 {
84 if(regid == *(USHORT*)(stg+22) && regid == *(USHORT*)(stg+26)) 84 if(regid == *(USHORT*)(stg+22) && regid == *(USHORT*)(stg+26))
85 { 85 {
86 sscanf(ptr+2, "%x", &offset1); 86 sscanf(ptr+2, "%x", &offset1);
87 if(ptr[-1] == '-') 87 if(ptr[-1] == '-')
88 offset1 = -offset1; 88 offset1 = -offset1;
89 89
90 if(memcmp(stg+17, "add ", 4) == 0) offset += offset1; 90 if(memcmp(stg+17, "add ", 4) == 0) offset += offset1;
91 else offset -= offset1; 91 else offset -= offset1;
92 92
93 /* add result to disassembler string */ 93 /* add result to disassembler string */
94 sprintf(stg+strlen(stg), " <- 0x%x", offset); 94 sprintf(stg+strlen(stg), " <- 0x%x", offset);
95 } 95 }
96 else 96 else
97 regid = 0; 97 regid = 0;
98 } 98 }
99 else 99 else
100 regid = 0; 100 regid = 0;
101 101
102 /* check for const data */ 102 /* check for const data */
103 if(memcmp(stg+26, "[pc, ", 5) == 0 && (ptr=strstr(stg, "0x")) != NULL) 103 if(memcmp(stg+26, "[pc, ", 5) == 0 && (ptr=strstr(stg, "0x")) != NULL)
104 { 104 {
105 sscanf(ptr+2, "%x", &offset); 105 sscanf(ptr+2, "%x", &offset);
106 if(ptr[-1] == '-') 106 if(ptr[-1] == '-')
107 offset = -offset; 107 offset = -offset;
108 108
109 /* add data tag */ 109 /* add data tag */
110 isdata[(pos+offset+8)>>5] |= 1 << ((pos+offset+8) & 31); 110 isdata[(pos+offset+8)>>5] |= 1 << ((pos+offset+8) & 31);
111 111
112 /* add const data to disassembler string */ 112 /* add const data to disassembler string */
113 fseek(in, pos+offset+8, SEEK_SET); 113 fseek(in, pos+offset+8, SEEK_SET);
114 fread(&buf, 1, 4, in); 114 fread(&buf, 1, 4, in);
115 offset = le2int(buf); 115 offset = le2int(buf);
116 116
117 sprintf(stg+strlen(stg), " <- 0x%x", offset); 117 sprintf(stg+strlen(stg), " <- 0x%x", offset);
118 } 118 }
119 } 119 }
120 120
121 /* remove trailing spaces */ 121 /* remove trailing spaces */
122 while(stg[strlen(stg)-1] == 32) 122 while(stg[strlen(stg)-1] == 32)
123 stg[strlen(stg)-1] = 0; 123 stg[strlen(stg)-1] = 0;
124 124
125 if(loop == 1) 125 if(loop == 1)
126 fprintf(out, "%s\n", stg); 126 fprintf(out, "%s\n", stg);
127 } 127 }
128 } 128 }
129 129
130 fclose(in); 130 fclose(in);
131 return 0; 131 return 0;
132} 132}
diff --git a/utils/jz4740_tools/HXFmerge.c b/utils/jz4740_tools/HXFmerge.c
index 6b6c62505e..fdd81b8445 100644
--- a/utils/jz4740_tools/HXFmerge.c
+++ b/utils/jz4740_tools/HXFmerge.c
@@ -1,321 +1,321 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30#include <dirent.h> 30#include <dirent.h>
31 31
32#define VERSION "0.2" 32#define VERSION "0.2"
33 33
34static unsigned char* int2le(unsigned int val) 34static unsigned char* int2le(unsigned int val)
35{ 35{
36 static unsigned char addr[4]; 36 static unsigned char addr[4];
37 addr[0] = val & 0xff; 37 addr[0] = val & 0xff;
38 addr[1] = (val >> 8) & 0xff; 38 addr[1] = (val >> 8) & 0xff;
39 addr[2] = (val >> 16) & 0xff; 39 addr[2] = (val >> 16) & 0xff;
40 addr[3] = (val >> 24) & 0xff; 40 addr[3] = (val >> 24) & 0xff;
41 return addr; 41 return addr;
42} 42}
43 43
44static unsigned int le2int(unsigned char* buf) 44static unsigned int le2int(unsigned char* buf)
45{ 45{
46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
47 47
48 return res; 48 return res;
49} 49}
50 50
51#ifdef _WIN32 51#ifdef _WIN32
52 #define PATH_SEPARATOR "\\" 52 #define PATH_SEPARATOR "\\"
53#else 53#else
54 #define PATH_SEPARATOR "/" 54 #define PATH_SEPARATOR "/"
55#endif 55#endif
56 56
57#ifndef _WIN32 57#ifndef _WIN32
58 58
59#define MIN(a, b) (a > b ? b : a) 59#define MIN(a, b) (a > b ? b : a)
60static char* replace(char* str) 60static char* replace(char* str)
61{ 61{
62 static char tmp[255]; 62 static char tmp[255];
63 memcpy(tmp, str, MIN(strlen(str), 255)); 63 memcpy(tmp, str, MIN(strlen(str), 255));
64 char *ptr = tmp; 64 char *ptr = tmp;
65 while(*ptr != 0) 65 while(*ptr != 0)
66 { 66 {
67 if(*ptr == 0x2F) /* /*/ 67 if(*ptr == 0x2F) /* /*/
68 *ptr = 0x5C; /* \ */ 68 *ptr = 0x5C; /* \ */
69 ptr++; 69 ptr++;
70 } 70 }
71 return tmp; 71 return tmp;
72} 72}
73#endif 73#endif
74 74
75static bool is_dir(const char* name1, const char* name2) 75static bool is_dir(const char* name1, const char* name2)
76{ 76{
77 char *name; 77 char *name;
78 DIR *directory; 78 DIR *directory;
79 name = (char*)malloc(strlen(name1)+strlen(name2)+1); 79 name = (char*)malloc(strlen(name1)+strlen(name2)+1);
80 strcpy(name, name1); 80 strcpy(name, name1);
81 strcat(name, name2); 81 strcat(name, name2);
82 directory = opendir(name); 82 directory = opendir(name);
83 free(name); 83 free(name);
84 if(directory) 84 if(directory)
85 { 85 {
86 closedir(directory); 86 closedir(directory);
87 return true; 87 return true;
88 } 88 }
89 else 89 else
90 return false; 90 return false;
91} 91}
92 92
93unsigned int _filesize(FILE* fd) 93unsigned int _filesize(FILE* fd)
94{ 94{
95 unsigned int tmp, oldpos; 95 unsigned int tmp, oldpos;
96 oldpos = ftell(fd); 96 oldpos = ftell(fd);
97 fseek(fd, 0, SEEK_END); 97 fseek(fd, 0, SEEK_END);
98 tmp = ftell(fd); 98 tmp = ftell(fd);
99 fseek(fd, oldpos, SEEK_SET); 99 fseek(fd, oldpos, SEEK_SET);
100 return tmp; 100 return tmp;
101} 101}
102#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 102#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
103 { \ 103 { \
104 closedir(indir_handle); \ 104 closedir(indir_handle); \
105 if(filesize > 0) \ 105 if(filesize > 0) \
106 free(buffer); \ 106 free(buffer); \
107 fprintf(stderr, "[ERR] Error writing to file\n"); \ 107 fprintf(stderr, "[ERR] Error writing to file\n"); \
108 return; \ 108 return; \
109 } 109 }
110static void merge_hxf(const char* indir, FILE* outfile, const char* add) 110static void merge_hxf(const char* indir, FILE* outfile, const char* add)
111{ 111{
112 DIR *indir_handle; 112 DIR *indir_handle;
113 struct dirent *dirs; 113 struct dirent *dirs;
114 char dir[255]; 114 char dir[255];
115 strcpy(dir, indir); 115 strcpy(dir, indir);
116 strcat(dir, add); 116 strcat(dir, add);
117 117
118 if((indir_handle = opendir(dir)) == NULL) 118 if((indir_handle = opendir(dir)) == NULL)
119 { 119 {
120 fprintf(stderr, "[ERR] Error opening dir %s\n", indir); 120 fprintf(stderr, "[ERR] Error opening dir %s\n", indir);
121 return; 121 return;
122 } 122 }
123 123
124 while((dirs = readdir(indir_handle)) != NULL) 124 while((dirs = readdir(indir_handle)) != NULL)
125 { 125 {
126 if(strcmp(dirs->d_name, "..") != 0 && 126 if(strcmp(dirs->d_name, "..") != 0 &&
127 strcmp(dirs->d_name, ".") != 0) 127 strcmp(dirs->d_name, ".") != 0)
128 { 128 {
129 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name); 129 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name);
130 if(is_dir(dir, dirs->d_name)) 130 if(is_dir(dir, dirs->d_name))
131 { 131 {
132 char dir2[255]; 132 char dir2[255];
133 strcpy(dir2, add); 133 strcpy(dir2, add);
134 strcat(dir2, dirs->d_name); 134 strcat(dir2, dirs->d_name);
135 strcat(dir2, PATH_SEPARATOR); 135 strcat(dir2, PATH_SEPARATOR);
136 merge_hxf(indir, outfile, dir2); 136 merge_hxf(indir, outfile, dir2);
137 } 137 }
138 else 138 else
139 { 139 {
140 FILE *filehandle; 140 FILE *filehandle;
141 unsigned char *buffer; 141 unsigned char *buffer;
142 char file[255]; 142 char file[255];
143 unsigned int filesize; 143 unsigned int filesize;
144 strcpy(file, dir); 144 strcpy(file, dir);
145 strcat(file, dirs->d_name); 145 strcat(file, dirs->d_name);
146 if((filehandle = fopen(file, "rb")) == NULL) 146 if((filehandle = fopen(file, "rb")) == NULL)
147 { 147 {
148 fprintf(stderr, "[ERR] Cannot open %s\n", file); 148 fprintf(stderr, "[ERR] Cannot open %s\n", file);
149 closedir(indir_handle); 149 closedir(indir_handle);
150 return; 150 return;
151 } 151 }
152 filesize = _filesize(filehandle); 152 filesize = _filesize(filehandle);
153 if(filesize > 0) 153 if(filesize > 0)
154 { 154 {
155 buffer = (unsigned char*)malloc(filesize); 155 buffer = (unsigned char*)malloc(filesize);
156 if(buffer == NULL) 156 if(buffer == NULL)
157 { 157 {
158 fclose(filehandle); 158 fclose(filehandle);
159 closedir(indir_handle); 159 closedir(indir_handle);
160 fprintf(stderr, "[ERR] Cannot allocate memory\n"); 160 fprintf(stderr, "[ERR] Cannot allocate memory\n");
161 return; 161 return;
162 } 162 }
163 if(fread(buffer, filesize, 1, filehandle) != 1) 163 if(fread(buffer, filesize, 1, filehandle) != 1)
164 { 164 {
165 fclose(filehandle); 165 fclose(filehandle);
166 closedir(indir_handle); 166 closedir(indir_handle);
167 free(buffer); 167 free(buffer);
168 fprintf(stderr, "[ERR] Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name); 168 fprintf(stderr, "[ERR] Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name);
169 return; 169 return;
170 } 170 }
171 } 171 }
172 fclose(filehandle); 172 fclose(filehandle);
173 173
174 if(strlen(add)>0) 174 if(strlen(add)>0)
175 { 175 {
176#ifdef _DIRENT_HAVE_D_NAMLEN 176#ifdef _DIRENT_HAVE_D_NAMLEN
177 WRITE(int2le(dirs->d_namlen+strlen(add)), 4); 177 WRITE(int2le(dirs->d_namlen+strlen(add)), 4);
178#else 178#else
179 WRITE(int2le(strlen(dirs->d_name)+strlen(add)), 4); 179 WRITE(int2le(strlen(dirs->d_name)+strlen(add)), 4);
180#endif 180#endif
181#ifndef _WIN32 181#ifndef _WIN32
182 WRITE(replace((char*)add), strlen(add)-1); 182 WRITE(replace((char*)add), strlen(add)-1);
183#else 183#else
184 WRITE(add, strlen(add)-1); 184 WRITE(add, strlen(add)-1);
185#endif 185#endif
186 WRITE(PATH_SEPARATOR, 1); 186 WRITE(PATH_SEPARATOR, 1);
187#ifdef _DIRENT_HAVE_D_NAMLEN 187#ifdef _DIRENT_HAVE_D_NAMLEN
188 WRITE(dirs->d_name, dirs->d_namlen); 188 WRITE(dirs->d_name, dirs->d_namlen);
189#else 189#else
190 WRITE(dirs->d_name, strlen(dirs->d_name)); 190 WRITE(dirs->d_name, strlen(dirs->d_name));
191#endif 191#endif
192 } 192 }
193 else 193 else
194 { 194 {
195#ifdef _DIRENT_HAVE_D_NAMLEN 195#ifdef _DIRENT_HAVE_D_NAMLEN
196 WRITE(int2le(dirs->d_namlen), 4); 196 WRITE(int2le(dirs->d_namlen), 4);
197 WRITE(dirs->d_name, dirs->d_namlen); 197 WRITE(dirs->d_name, dirs->d_namlen);
198#else 198#else
199 WRITE(int2le(strlen(dirs->d_name)), 4); 199 WRITE(int2le(strlen(dirs->d_name)), 4);
200 WRITE(dirs->d_name, strlen(dirs->d_name)); 200 WRITE(dirs->d_name, strlen(dirs->d_name));
201#endif 201#endif
202 } 202 }
203 WRITE(int2le(filesize), 4); 203 WRITE(int2le(filesize), 4);
204 if(filesize>0) 204 if(filesize>0)
205 { 205 {
206 WRITE(buffer, filesize); 206 WRITE(buffer, filesize);
207 free(buffer); 207 free(buffer);
208 } 208 }
209 } 209 }
210 } 210 }
211 } 211 }
212 closedir(indir_handle); 212 closedir(indir_handle);
213} 213}
214 214
215static void print_usage(void) 215static void print_usage(void)
216{ 216{
217#ifdef _WIN32 217#ifdef _WIN32
218 fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n"); 218 fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n");
219 fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n"); 219 fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n");
220#else 220#else
221 fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n"); 221 fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n");
222 fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n"); 222 fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n");
223#endif 223#endif
224} 224}
225 225
226static int checksum(FILE *file) 226static int checksum(FILE *file)
227{ 227{
228 int oldpos = ftell(file); 228 int oldpos = ftell(file);
229 int ret=0, i, filesize = _filesize(file)-0x40; 229 int ret=0, i, filesize = _filesize(file)-0x40;
230 unsigned char *buf; 230 unsigned char *buf;
231 231
232 buf = (unsigned char*)malloc(filesize); 232 buf = (unsigned char*)malloc(filesize);
233 233
234 if(buf == NULL) 234 if(buf == NULL)
235 { 235 {
236 fseek(file, oldpos, SEEK_SET); 236 fseek(file, oldpos, SEEK_SET);
237 fprintf(stderr, "[ERR] Error while allocating memory\n"); 237 fprintf(stderr, "[ERR] Error while allocating memory\n");
238 return 0; 238 return 0;
239 } 239 }
240 240
241 fseek(file, 0x40, SEEK_SET); 241 fseek(file, 0x40, SEEK_SET);
242 if(fread(buf, filesize, 1, file) != 1) 242 if(fread(buf, filesize, 1, file) != 1)
243 { 243 {
244 free(buf); 244 free(buf);
245 fseek(file, oldpos, SEEK_SET); 245 fseek(file, oldpos, SEEK_SET);
246 fprintf(stderr, "[ERR] Error while reading from file\n"); 246 fprintf(stderr, "[ERR] Error while reading from file\n");
247 return 0; 247 return 0;
248 } 248 }
249 249
250 fprintf(stderr, "[INFO] Computing checksum..."); 250 fprintf(stderr, "[INFO] Computing checksum...");
251 251
252 for(i = 0; i < filesize; i+=4) 252 for(i = 0; i < filesize; i+=4)
253 ret += le2int(&buf[i]); 253 ret += le2int(&buf[i]);
254 254
255 free(buf); 255 free(buf);
256 fseek(file, oldpos, SEEK_SET); 256 fseek(file, oldpos, SEEK_SET);
257 257
258 fprintf(stderr, " Done!\n"); 258 fprintf(stderr, " Done!\n");
259 return ret; 259 return ret;
260} 260}
261 261
262int main(int argc, char *argv[]) 262int main(int argc, char *argv[])
263{ 263{
264 FILE *outfile; 264 FILE *outfile;
265 265
266 fprintf(stderr, "HXFmerge v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 266 fprintf(stderr, "HXFmerge v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
267 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n"); 267 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
268 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); 268 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
269 269
270 if(argc != 3) 270 if(argc != 3)
271 { 271 {
272 print_usage(); 272 print_usage();
273 return 1; 273 return 1;
274 } 274 }
275 275
276#ifdef _WIN32 276#ifdef _WIN32
277 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0) 277 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0)
278 { 278 {
279 fprintf(stderr, "[ERR] Input path must end with a \\\n"); 279 fprintf(stderr, "[ERR] Input path must end with a \\\n");
280#else 280#else
281 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0) 281 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0)
282 { 282 {
283 fprintf(stderr, "[ERR] Input path must end with a /\n"); 283 fprintf(stderr, "[ERR] Input path must end with a /\n");
284#endif 284#endif
285 return 2; 285 return 2;
286 } 286 }
287 287
288 if((outfile = fopen(argv[2], "wb+")) == NULL) 288 if((outfile = fopen(argv[2], "wb+")) == NULL)
289 { 289 {
290 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); 290 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
291 return 3; 291 return 3;
292 } 292 }
293 293
294 fseek(outfile, 0x40, SEEK_SET); 294 fseek(outfile, 0x40, SEEK_SET);
295 295
296 merge_hxf(argv[1], outfile, ""); 296 merge_hxf(argv[1], outfile, "");
297 297
298 fflush(outfile); 298 fflush(outfile);
299 299
300 fprintf(stderr, "[INFO] Filling header...\n"); 300 fprintf(stderr, "[INFO] Filling header...\n");
301 301
302#undef WRITE 302#undef WRITE
303#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 303#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
304 { \ 304 { \
305 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \ 305 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \
306 fclose(outfile); \ 306 fclose(outfile); \
307 return 4; \ 307 return 4; \
308 } 308 }
309 fflush(outfile); 309 fflush(outfile);
310 fseek(outfile, 0, SEEK_SET); 310 fseek(outfile, 0, SEEK_SET);
311 WRITE("WADF0100200804111437", 20); 311 WRITE("WADF0100200804111437", 20);
312 WRITE(int2le(_filesize(outfile)), 4); 312 WRITE(int2le(_filesize(outfile)), 4);
313 WRITE(int2le(checksum(outfile)), 4); 313 WRITE(int2le(checksum(outfile)), 4);
314 WRITE(int2le(0), 4); 314 WRITE(int2le(0), 4);
315 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32); 315 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32);
316 fclose(outfile); 316 fclose(outfile);
317 317
318 fprintf(stderr, "[INFO] Done!\n"); 318 fprintf(stderr, "[INFO] Done!\n");
319 319
320 return 0; 320 return 0;
321} 321}
diff --git a/utils/jz4740_tools/HXFreplace.c b/utils/jz4740_tools/HXFreplace.c
index 989a59dd61..bcfff82540 100644
--- a/utils/jz4740_tools/HXFreplace.c
+++ b/utils/jz4740_tools/HXFreplace.c
@@ -1,242 +1,242 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30#include <dirent.h> 30#include <dirent.h>
31 31
32#define VERSION "0.1" 32#define VERSION "0.1"
33 33
34static unsigned char* int2le(unsigned int val) 34static unsigned char* int2le(unsigned int val)
35{ 35{
36 static unsigned char addr[4]; 36 static unsigned char addr[4];
37 addr[0] = val & 0xff; 37 addr[0] = val & 0xff;
38 addr[1] = (val >> 8) & 0xff; 38 addr[1] = (val >> 8) & 0xff;
39 addr[2] = (val >> 16) & 0xff; 39 addr[2] = (val >> 16) & 0xff;
40 addr[3] = (val >> 24) & 0xff; 40 addr[3] = (val >> 24) & 0xff;
41 return addr; 41 return addr;
42} 42}
43 43
44static unsigned int le2int(unsigned char* buf) 44static unsigned int le2int(unsigned char* buf)
45{ 45{
46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
47 47
48 return res; 48 return res;
49} 49}
50 50
51unsigned int _filesize(FILE* fd) 51unsigned int _filesize(FILE* fd)
52{ 52{
53 unsigned int tmp, oldpos; 53 unsigned int tmp, oldpos;
54 oldpos = ftell(fd); 54 oldpos = ftell(fd);
55 fseek(fd, 0, SEEK_END); 55 fseek(fd, 0, SEEK_END);
56 tmp = ftell(fd); 56 tmp = ftell(fd);
57 fseek(fd, oldpos, SEEK_SET); 57 fseek(fd, oldpos, SEEK_SET);
58 return tmp; 58 return tmp;
59} 59}
60 60
61static void print_usage(void) 61static void print_usage(void)
62{ 62{
63#ifdef _WIN32 63#ifdef _WIN32
64 fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); 64 fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
65 fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n"); 65 fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
66#else 66#else
67 fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); 67 fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
68 fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n"); 68 fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
69#endif 69#endif
70} 70}
71 71
72static int checksum(FILE *file) 72static int checksum(FILE *file)
73{ 73{
74 int oldpos = ftell(file); 74 int oldpos = ftell(file);
75 int ret=0, i, filesize = _filesize(file)-0x40; 75 int ret=0, i, filesize = _filesize(file)-0x40;
76 unsigned char *buf; 76 unsigned char *buf;
77 77
78 buf = (unsigned char*)malloc(filesize); 78 buf = (unsigned char*)malloc(filesize);
79 79
80 if(buf == NULL) 80 if(buf == NULL)
81 { 81 {
82 fseek(file, oldpos, SEEK_SET); 82 fseek(file, oldpos, SEEK_SET);
83 fprintf(stderr, "[ERR] Error while allocating memory\n"); 83 fprintf(stderr, "[ERR] Error while allocating memory\n");
84 return 0; 84 return 0;
85 } 85 }
86 86
87 fseek(file, 0x40, SEEK_SET); 87 fseek(file, 0x40, SEEK_SET);
88 if(fread(buf, filesize, 1, file) != 1) 88 if(fread(buf, filesize, 1, file) != 1)
89 { 89 {
90 free(buf); 90 free(buf);
91 fseek(file, oldpos, SEEK_SET); 91 fseek(file, oldpos, SEEK_SET);
92 fprintf(stderr, "[ERR] Error while reading from file\n"); 92 fprintf(stderr, "[ERR] Error while reading from file\n");
93 return 0; 93 return 0;
94 } 94 }
95 95
96 fprintf(stderr, "[INFO] Computing checksum..."); 96 fprintf(stderr, "[INFO] Computing checksum...");
97 97
98 for(i = 0; i < filesize; i+=4) 98 for(i = 0; i < filesize; i+=4)
99 ret += le2int(&buf[i]); 99 ret += le2int(&buf[i]);
100 100
101 free(buf); 101 free(buf);
102 fseek(file, oldpos, SEEK_SET); 102 fseek(file, oldpos, SEEK_SET);
103 103
104 fprintf(stderr, " Done!\n"); 104 fprintf(stderr, " Done!\n");
105 return ret; 105 return ret;
106} 106}
107 107
108int main(int argc, char *argv[]) 108int main(int argc, char *argv[])
109{ 109{
110 FILE *infile, *outfile, *fw; 110 FILE *infile, *outfile, *fw;
111 111
112 fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 112 fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
113 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n"); 113 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
114 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); 114 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
115 115
116 if(argc != 4) 116 if(argc != 4)
117 { 117 {
118 print_usage(); 118 print_usage();
119 return 1; 119 return 1;
120 } 120 }
121 121
122 if((infile = fopen(argv[1], "rb")) == NULL) 122 if((infile = fopen(argv[1], "rb")) == NULL)
123 { 123 {
124 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); 124 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
125 return 2; 125 return 2;
126 } 126 }
127 127
128 if(fseek(infile, 0x40, SEEK_SET) != 0) 128 if(fseek(infile, 0x40, SEEK_SET) != 0)
129 { 129 {
130 fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); 130 fprintf(stderr, "[ERR] Cannot seek to 0x40\n");
131 fclose(infile); 131 fclose(infile);
132 return 3; 132 return 3;
133 } 133 }
134 134
135 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); 135 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n");
136 136
137 int found = -1; 137 int found = -1;
138 int filenamesize; 138 int filenamesize;
139 char *filename; 139 char *filename;
140 unsigned char tmp[4]; 140 unsigned char tmp[4];
141 141
142#define READ(x, len) if(fread(x, len, 1, infile) != 1) \ 142#define READ(x, len) if(fread(x, len, 1, infile) != 1) \
143 { \ 143 { \
144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ 144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
145 fclose(infile); \ 145 fclose(infile); \
146 return 4; \ 146 return 4; \
147 } 147 }
148 while(found < 0) 148 while(found < 0)
149 { 149 {
150 READ(&tmp[0], 4); 150 READ(&tmp[0], 4);
151 filenamesize = le2int(tmp); 151 filenamesize = le2int(tmp);
152 filename = (char*)malloc(filenamesize); 152 filename = (char*)malloc(filenamesize);
153 READ(filename, filenamesize); 153 READ(filename, filenamesize);
154 if(strcmp(filename, "ccpmp.bin") == 0) 154 if(strcmp(filename, "ccpmp.bin") == 0)
155 found = ftell(infile); 155 found = ftell(infile);
156 else 156 else
157 { 157 {
158 READ(&tmp[0], 4); 158 READ(&tmp[0], 4);
159 fseek(infile, le2int(tmp), SEEK_CUR); 159 fseek(infile, le2int(tmp), SEEK_CUR);
160 } 160 }
161 free(filename); 161 free(filename);
162 } 162 }
163 163
164 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); 164 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found);
165 165
166 if((outfile = fopen(argv[2], "wb+")) == NULL) 166 if((outfile = fopen(argv[2], "wb+")) == NULL)
167 { 167 {
168 fclose(infile); 168 fclose(infile);
169 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); 169 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
170 return 5; 170 return 5;
171 } 171 }
172 172
173#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 173#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
174 { \ 174 { \
175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ 175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
176 fclose(outfile); \ 176 fclose(outfile); \
177 if(fw != NULL) \ 177 if(fw != NULL) \
178 fclose(fw); \ 178 fclose(fw); \
179 return 5; \ 179 return 5; \
180 } 180 }
181 181
182 unsigned char* buffer; 182 unsigned char* buffer;
183 183
184 buffer = (unsigned char*)malloc(found); 184 buffer = (unsigned char*)malloc(found);
185 fseek(infile, 0, SEEK_SET); 185 fseek(infile, 0, SEEK_SET);
186 READ(buffer, found); 186 READ(buffer, found);
187 WRITE(buffer, found); 187 WRITE(buffer, found);
188 free(buffer); 188 free(buffer);
189 189
190 if((fw = fopen(argv[3], "rb")) == NULL) 190 if((fw = fopen(argv[3], "rb")) == NULL)
191 { 191 {
192 fclose(infile); 192 fclose(infile);
193 fclose(outfile); 193 fclose(outfile);
194 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); 194 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]);
195 } 195 }
196 196
197 int fw_filesize = _filesize(fw); 197 int fw_filesize = _filesize(fw);
198 198
199#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ 199#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
200 { \ 200 { \
201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ 201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
202 fclose(infile); \ 202 fclose(infile); \
203 fclose(outfile); \ 203 fclose(outfile); \
204 return 6; \ 204 return 6; \
205 } 205 }
206 buffer = (unsigned char*)malloc(fw_filesize); 206 buffer = (unsigned char*)malloc(fw_filesize);
207 READ2(buffer, fw_filesize); 207 READ2(buffer, fw_filesize);
208 fputc(0x20, outfile); /* Padding */ 208 fputc(0x20, outfile); /* Padding */
209 WRITE(int2le(fw_filesize), 4); 209 WRITE(int2le(fw_filesize), 4);
210 WRITE(buffer, fw_filesize); 210 WRITE(buffer, fw_filesize);
211 free(buffer); 211 free(buffer);
212 fclose(fw); 212 fclose(fw);
213 fw = NULL; 213 fw = NULL;
214 214
215 fseek(infile, found+1, SEEK_SET); 215 fseek(infile, found+1, SEEK_SET);
216 READ(&tmp, 4); 216 READ(&tmp, 4);
217 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) 217 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0)
218 { 218 {
219 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); 219 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]);
220 fclose(infile); 220 fclose(infile);
221 fclose(outfile); 221 fclose(outfile);
222 return 7; 222 return 7;
223 } 223 }
224 found = ftell(infile); 224 found = ftell(infile);
225 225
226 int other_size = _filesize(infile) - found; 226 int other_size = _filesize(infile) - found;
227 buffer = (unsigned char*)malloc(other_size); 227 buffer = (unsigned char*)malloc(other_size);
228 READ(buffer, other_size); 228 READ(buffer, other_size);
229 WRITE(buffer, other_size); 229 WRITE(buffer, other_size);
230 free(buffer); 230 free(buffer);
231 fclose(infile); 231 fclose(infile);
232 232
233 fflush(outfile); 233 fflush(outfile);
234 fseek(outfile, 0x14, SEEK_SET); 234 fseek(outfile, 0x14, SEEK_SET);
235 WRITE(int2le(_filesize(outfile)), 4); 235 WRITE(int2le(_filesize(outfile)), 4);
236 WRITE(int2le(checksum(outfile)), 4); 236 WRITE(int2le(checksum(outfile)), 4);
237 fclose(outfile); 237 fclose(outfile);
238 238
239 fprintf(stderr, "[INFO] Done!\n"); 239 fprintf(stderr, "[INFO] Done!\n");
240 240
241 return 0; 241 return 0;
242} 242}
diff --git a/utils/jz4740_tools/HXFsplit.c b/utils/jz4740_tools/HXFsplit.c
index 6e945b067a..dbeace8a20 100644
--- a/utils/jz4740_tools/HXFsplit.c
+++ b/utils/jz4740_tools/HXFsplit.c
@@ -1,321 +1,321 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30 30
31#define VERSION "0.2" 31#define VERSION "0.2"
32 32
33struct header{ 33struct header{
34 char main_header[20]; 34 char main_header[20];
35 unsigned int size; 35 unsigned int size;
36 unsigned int checksum; 36 unsigned int checksum;
37 unsigned int unknown; 37 unsigned int unknown;
38 char other_header[32]; 38 char other_header[32];
39}; 39};
40 40
41static char* basepath(char* path) 41static char* basepath(char* path)
42{ 42{
43 static char tmp[255]; 43 static char tmp[255];
44 char *ptr, *ptr2, *ptr3; 44 char *ptr, *ptr2, *ptr3;
45 ptr = path; 45 ptr = path;
46 ptr2 = (char*)tmp; 46 ptr2 = (char*)tmp;
47#ifdef _WIN32 47#ifdef _WIN32
48 ptr3 = strrchr(path, 0x5C); 48 ptr3 = strrchr(path, 0x5C);
49#else 49#else
50 ptr3 = strrchr(path, 0x2F); 50 ptr3 = strrchr(path, 0x2F);
51#endif 51#endif
52 while((int)ptr < (int)ptr3) 52 while((int)ptr < (int)ptr3)
53 { 53 {
54 *ptr2 = *ptr; 54 *ptr2 = *ptr;
55 ptr++; 55 ptr++;
56 ptr2++; 56 ptr2++;
57 } 57 }
58#ifdef _WIN32 58#ifdef _WIN32
59 *ptr2 = 0x5C; 59 *ptr2 = 0x5C;
60#else 60#else
61 *ptr2 = 0x2F; 61 *ptr2 = 0x2F;
62#endif 62#endif
63 ptr2++; 63 ptr2++;
64 *ptr2 = 0; 64 *ptr2 = 0;
65 return (char*)tmp; 65 return (char*)tmp;
66} 66}
67 67
68#ifndef _WIN32 68#ifndef _WIN32
69static void replace(char* str) 69static void replace(char* str)
70{ 70{
71 char *ptr = str; 71 char *ptr = str;
72 while(*ptr != 0) 72 while(*ptr != 0)
73 { 73 {
74 if(*ptr == 0x5C) /* \ */ 74 if(*ptr == 0x5C) /* \ */
75 *ptr = 0x2F; /* / */ 75 *ptr = 0x2F; /* / */
76 ptr++; 76 ptr++;
77 } 77 }
78} 78}
79#endif 79#endif
80 80
81static unsigned int le2int(unsigned char* buf) 81static unsigned int le2int(unsigned char* buf)
82{ 82{
83 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 83 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
84 84
85 return res; 85 return res;
86} 86}
87 87
88#ifdef _WIN32 88#ifdef _WIN32
89 #define PATH_SEPARATOR '\\' 89 #define PATH_SEPARATOR '\\'
90#else 90#else
91 #define PATH_SEPARATOR '/' 91 #define PATH_SEPARATOR '/'
92#endif 92#endif
93 93
94static unsigned int __mkdir(const char *path) 94static unsigned int __mkdir(const char *path)
95{ 95{
96 char opath[256]; 96 char opath[256];
97 char *p; 97 char *p;
98 size_t len; 98 size_t len;
99 99
100 strncpy(opath, path, sizeof(opath)); 100 strncpy(opath, path, sizeof(opath));
101 len = strlen(opath); 101 len = strlen(opath);
102 if(opath[len - 1] == PATH_SEPARATOR) 102 if(opath[len - 1] == PATH_SEPARATOR)
103 opath[len - 1] = '\0'; 103 opath[len - 1] = '\0';
104 for(p = opath; *p; p++) 104 for(p = opath; *p; p++)
105 if(*p == PATH_SEPARATOR) 105 if(*p == PATH_SEPARATOR)
106 { 106 {
107 *p = '\0'; 107 *p = '\0';
108 if(access(opath, F_OK)) 108 if(access(opath, F_OK))
109#ifdef _WIN32 109#ifdef _WIN32
110 mkdir(opath); 110 mkdir(opath);
111#else 111#else
112 mkdir(opath, S_IRWXU); 112 mkdir(opath, S_IRWXU);
113#endif 113#endif
114 *p = PATH_SEPARATOR; 114 *p = PATH_SEPARATOR;
115 } 115 }
116 if(access(opath, F_OK)) 116 if(access(opath, F_OK))
117#ifdef _WIN32 117#ifdef _WIN32
118 return mkdir(opath); 118 return mkdir(opath);
119#else 119#else
120 return mkdir(opath, S_IRWXU); 120 return mkdir(opath, S_IRWXU);
121#endif 121#endif
122 else 122 else
123 return -1; 123 return -1;
124} 124}
125 125
126#if 0 126#if 0
127static bool dir_exists(const char *dir) 127static bool dir_exists(const char *dir)
128{ 128{
129 struct stat buf; 129 struct stat buf;
130 memset(&buf, 0, sizeof(struct stat)); 130 memset(&buf, 0, sizeof(struct stat));
131 printf("start: %s\n", dir); 131 printf("start: %s\n", dir);
132 char *dir_cpy = (char*)malloc(strlen(dir)); 132 char *dir_cpy = (char*)malloc(strlen(dir));
133 strcpy(dir_cpy, dir); 133 strcpy(dir_cpy, dir);
134 printf("%s\n", dir_cpy); 134 printf("%s\n", dir_cpy);
135 int tmp = (int)dir_cpy; 135 int tmp = (int)dir_cpy;
136 while(*dir_cpy != 0) 136 while(*dir_cpy != 0)
137 { 137 {
138 dir_cpy++; 138 dir_cpy++;
139 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0) 139 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
140 *dir_cpy = 0; 140 *dir_cpy = 0;
141 } 141 }
142 printf("while_done\n"); 142 printf("while_done\n");
143 dir_cpy = (char*)tmp; 143 dir_cpy = (char*)tmp;
144 printf("statting %s...\n", dir_cpy); 144 printf("statting %s...\n", dir_cpy);
145 tmp = stat(dir_cpy, &buf); 145 tmp = stat(dir_cpy, &buf);
146 printf("chk_dir(%s) = %d\n", dir_cpy, tmp); 146 printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
147 free(dir_cpy); 147 free(dir_cpy);
148 return tmp == 0; 148 return tmp == 0;
149} 149}
150#endif 150#endif
151 151
152static bool file_exists(const char *file) 152static bool file_exists(const char *file)
153{ 153{
154 struct stat buf; 154 struct stat buf;
155 return stat(file, &buf) == 0; 155 return stat(file, &buf) == 0;
156} 156}
157 157
158 158
159static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath) 159static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
160{ 160{
161 FILE *outfile; 161 FILE *outfile;
162 char *filename; 162 char *filename;
163 unsigned int filenamesize, filesize; 163 unsigned int filenamesize, filesize;
164 while(size > 0) 164 while(size > 0)
165 { 165 {
166 filenamesize = le2int((unsigned char*)infile); 166 filenamesize = le2int((unsigned char*)infile);
167 infile += 4; 167 infile += 4;
168 size -= 4; 168 size -= 4;
169 if(size > 0) 169 if(size > 0)
170 { 170 {
171 filename = (char*)calloc(1, filenamesize+1+strlen(outpath)); 171 filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
172 memcpy(filename, outpath, strlen(outpath)); 172 memcpy(filename, outpath, strlen(outpath));
173 memcpy(&filename[strlen(outpath)], infile, filenamesize); 173 memcpy(&filename[strlen(outpath)], infile, filenamesize);
174#ifndef _WIN32 174#ifndef _WIN32
175 replace(filename); 175 replace(filename);
176#endif 176#endif
177 infile += filenamesize + 1; /* + padding */ 177 infile += filenamesize + 1; /* + padding */
178 size -= filenamesize + 1; 178 size -= filenamesize + 1;
179 179
180 filesize = le2int((unsigned char*)infile); 180 filesize = le2int((unsigned char*)infile);
181 infile += 4; 181 infile += 4;
182 size -= 4; 182 size -= 4;
183#if 0 183#if 0
184 if(!dir_exists(basepath(filename))) 184 if(!dir_exists(basepath(filename)))
185#endif 185#endif
186 { 186 {
187 printf("[INFO] %s\n", basepath(filename)); 187 printf("[INFO] %s\n", basepath(filename));
188 if(__mkdir(basepath(filename)) != 0) 188 if(__mkdir(basepath(filename)) != 0)
189 { 189 {
190#if 0 190#if 0
191 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename)); 191 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
192 return -3; 192 return -3;
193#endif 193#endif
194 } 194 }
195 } 195 }
196 196
197 if(!file_exists(filename)) 197 if(!file_exists(filename))
198 { 198 {
199 printf("[INFO] %s: %d bytes\n", filename, filesize); 199 printf("[INFO] %s: %d bytes\n", filename, filesize);
200 if((outfile = fopen(filename, "wb")) == NULL) 200 if((outfile = fopen(filename, "wb")) == NULL)
201 { 201 {
202 fprintf(stderr, "[ERR] Error opening file %s\n", filename); 202 fprintf(stderr, "[ERR] Error opening file %s\n", filename);
203 return -1; 203 return -1;
204 } 204 }
205 if(filesize>0) 205 if(filesize>0)
206 { 206 {
207 if(fwrite(infile, filesize, 1, outfile) != 1) 207 if(fwrite(infile, filesize, 1, outfile) != 1)
208 { 208 {
209 fclose(outfile); 209 fclose(outfile);
210 fprintf(stderr, "[ERR] Error writing to file %s\n", filename); 210 fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
211 return -2; 211 return -2;
212 } 212 }
213 } 213 }
214 fclose(outfile); 214 fclose(outfile);
215 } 215 }
216 216
217 infile += filesize; 217 infile += filesize;
218 size -= filesize; 218 size -= filesize;
219 } 219 }
220 } 220 }
221 return 0; 221 return 0;
222} 222}
223 223
224static void print_usage(void) 224static void print_usage(void)
225{ 225{
226#ifdef _WIN32 226#ifdef _WIN32
227 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n"); 227 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
228 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n"); 228 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
229#else 229#else
230 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n"); 230 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
231 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n"); 231 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
232#endif 232#endif
233} 233}
234 234
235int main(int argc, char *argv[]) 235int main(int argc, char *argv[])
236{ 236{
237 FILE *infile; 237 FILE *infile;
238 struct header hdr; 238 struct header hdr;
239 unsigned char *inbuffer; 239 unsigned char *inbuffer;
240 240
241 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 241 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
242 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n"); 242 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
243 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); 243 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
244 244
245 if(argc != 3) 245 if(argc != 3)
246 { 246 {
247 print_usage(); 247 print_usage();
248 return 1; 248 return 1;
249 } 249 }
250 250
251#ifdef _WIN32 251#ifdef _WIN32
252 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0) 252 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
253 { 253 {
254 fprintf(stderr, "[ERR] Output path must end with a \\\n"); 254 fprintf(stderr, "[ERR] Output path must end with a \\\n");
255#else 255#else
256 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0) 256 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
257 { 257 {
258 fprintf(stderr, "[ERR] Output path must end with a /\n"); 258 fprintf(stderr, "[ERR] Output path must end with a /\n");
259#endif 259#endif
260 return 2; 260 return 2;
261 } 261 }
262 262
263 if((infile = fopen(argv[1], "rb")) == NULL) 263 if((infile = fopen(argv[1], "rb")) == NULL)
264 { 264 {
265 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); 265 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
266 return 3; 266 return 3;
267 } 267 }
268 268
269 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL) 269 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
270 { 270 {
271 fclose(infile); 271 fclose(infile);
272 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header)); 272 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
273 return 4; 273 return 4;
274 } 274 }
275 275
276 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1) 276 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
277 { 277 {
278 fclose(infile); 278 fclose(infile);
279 fprintf(stderr, "Cannot read header of %s\n", argv[1]); 279 fprintf(stderr, "Cannot read header of %s\n", argv[1]);
280 return 5; 280 return 5;
281 } 281 }
282 282
283 memcpy(hdr.main_header, inbuffer, 20); 283 memcpy(hdr.main_header, inbuffer, 20);
284 hdr.size = le2int(&inbuffer[20]); 284 hdr.size = le2int(&inbuffer[20]);
285 hdr.checksum = le2int(&inbuffer[24]); 285 hdr.checksum = le2int(&inbuffer[24]);
286 hdr.unknown = le2int(&inbuffer[28]); 286 hdr.unknown = le2int(&inbuffer[28]);
287 memcpy(hdr.other_header, &inbuffer[32], 32); 287 memcpy(hdr.other_header, &inbuffer[32], 32);
288 free(inbuffer); 288 free(inbuffer);
289 289
290 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0) 290 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
291 { 291 {
292 fclose(infile); 292 fclose(infile);
293 fprintf(stderr, "[ERR] Header doesn't match\n"); 293 fprintf(stderr, "[ERR] Header doesn't match\n");
294 return 6; 294 return 6;
295 } 295 }
296 296
297 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL) 297 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
298 { 298 {
299 fclose(infile); 299 fclose(infile);
300 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size); 300 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
301 return 7; 301 return 7;
302 } 302 }
303 303
304 fseek(infile, sizeof(struct header), SEEK_SET); 304 fseek(infile, sizeof(struct header), SEEK_SET);
305 305
306 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1) 306 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
307 { 307 {
308 fclose(infile); 308 fclose(infile);
309 free(inbuffer); 309 free(inbuffer);
310 fprintf(stderr, "[ERR] Cannot read file in buffer\n"); 310 fprintf(stderr, "[ERR] Cannot read file in buffer\n");
311 return 8; 311 return 8;
312 } 312 }
313 313
314 fclose(infile); 314 fclose(infile);
315 315
316 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]); 316 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
317 317
318 free(inbuffer); 318 free(inbuffer);
319 319
320 return 0; 320 return 0;
321} 321}