summaryrefslogtreecommitdiff
path: root/utils/MTP/beastpatcher/mtp_win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/MTP/beastpatcher/mtp_win32.c')
-rw-r--r--utils/MTP/beastpatcher/mtp_win32.c464
1 files changed, 232 insertions, 232 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