diff options
Diffstat (limited to 'utils/MTP/beastpatcher/mtp_win32.c')
-rw-r--r-- | utils/MTP/beastpatcher/mtp_win32.c | 410 |
1 files changed, 231 insertions, 179 deletions
diff --git a/utils/MTP/beastpatcher/mtp_win32.c b/utils/MTP/beastpatcher/mtp_win32.c index ced930ab80..1d6105f4fa 100644 --- a/utils/MTP/beastpatcher/mtp_win32.c +++ b/utils/MTP/beastpatcher/mtp_win32.c | |||
@@ -1,179 +1,231 @@ | |||
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 <tchar.h> | 47 | #include <sys/types.h> |
48 | 48 | #include <sys/stat.h> | |
49 | #include "mtp_common.h" | 49 | #include <tchar.h> |
50 | 50 | ||
51 | #include "../MTP_DLL/MTP_DLL.h" | 51 | #include "mtp_common.h" |
52 | 52 | ||
53 | int mtp_init(struct mtp_info_t* mtp_info) | 53 | #include "../MTP_DLL/MTP_DLL.h" |
54 | { | 54 | |
55 | /* Fill the info struct with zeros - mainly for the strings */ | 55 | |
56 | memset(mtp_info, 0, sizeof(struct mtp_info_t)); | 56 | static int filesize(const char* filename); |
57 | 57 | ||
58 | return 0; | 58 | |
59 | 59 | int mtp_init(struct mtp_info_t* mtp_info) | |
60 | } | 60 | { |
61 | 61 | /* Fill the info struct with zeros - mainly for the strings */ | |
62 | int mtp_finished(struct mtp_info_t* mtp_info) | 62 | memset(mtp_info, 0, sizeof(struct mtp_info_t)); |
63 | { | 63 | |
64 | (void)mtp_info; | 64 | return 0; |
65 | 65 | ||
66 | return 0; | 66 | } |
67 | } | 67 | |
68 | 68 | int mtp_finished(struct mtp_info_t* mtp_info) | |
69 | int mtp_scan(struct mtp_info_t* mtp_info) | 69 | { |
70 | { | 70 | (void)mtp_info; |
71 | wchar_t name[256]; | 71 | |
72 | wchar_t manufacturer[256]; | 72 | return 0; |
73 | DWORD version; | 73 | } |
74 | int num = 0; | 74 | |
75 | 75 | int mtp_scan(struct mtp_info_t* mtp_info) | |
76 | num = mtp_description(name, manufacturer, &version); | 76 | { |
77 | 77 | wchar_t name[256]; | |
78 | wcstombs(mtp_info->manufacturer, manufacturer, 200); | 78 | wchar_t manufacturer[256]; |
79 | wcstombs(mtp_info->modelname, name, 200); | 79 | DWORD version; |
80 | 80 | int num = 0; | |
81 | sprintf(mtp_info->version, "%x", (unsigned int)version); | 81 | |
82 | return (num > 0) ? num : -1; | 82 | num = mtp_description(name, manufacturer, &version); |
83 | 83 | ||
84 | } | 84 | wcstombs(mtp_info->manufacturer, manufacturer, 200); |
85 | 85 | wcstombs(mtp_info->modelname, name, 200); | |
86 | static void callback(unsigned int progress, unsigned int max) | 86 | |
87 | { | 87 | sprintf(mtp_info->version, "%x", (unsigned int)version); |
88 | int percent = (progress * 100) / max; | 88 | return (num > 0) ? num : -1; |
89 | 89 | ||
90 | printf("Progress: %u of %u (%d%%)\r", progress, max, percent); | 90 | } |
91 | fflush(stdout); | 91 | |
92 | } | 92 | static void callback(unsigned int progress, unsigned int max) |
93 | 93 | { | |
94 | 94 | int percent = (progress * 100) / max; | |
95 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | 95 | |
96 | int fwsize) | 96 | printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent); |
97 | { | 97 | fflush(stdout); |
98 | HANDLE hTempFile; | 98 | } |
99 | DWORD dwRetVal; | 99 | |
100 | DWORD dwBytesWritten; | 100 | |
101 | UINT uRetVal; | 101 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, |
102 | TCHAR szTempName[1024]; | 102 | int fwsize) |
103 | TCHAR lpPathBuffer[1024]; | 103 | { |
104 | BOOL fSuccess; | 104 | HANDLE hTempFile; |
105 | wchar_t *tmp; | 105 | DWORD dwRetVal; |
106 | int ret; | 106 | DWORD dwBytesWritten; |
107 | 107 | UINT uRetVal; | |
108 | (void)mtp_info; | 108 | TCHAR szTempName[1024]; |
109 | 109 | TCHAR lpPathBuffer[1024]; | |
110 | /* Get the path for temporary files */ | 110 | BOOL fSuccess; |
111 | dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); | 111 | wchar_t *tmp; |
112 | if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) | 112 | int ret; |
113 | { | 113 | |
114 | fprintf(stderr, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError()); | 114 | (void)mtp_info; |
115 | return -1; | 115 | |
116 | } | 116 | /* Get the path for temporary files */ |
117 | 117 | dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); | |
118 | /* Create the temporary file */ | 118 | if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) |
119 | uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); | 119 | { |
120 | if (uRetVal == 0) | 120 | fprintf(stderr, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError()); |
121 | { | 121 | return -1; |
122 | fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); | 122 | } |
123 | return -1; | 123 | |
124 | } | 124 | /* Create the temporary file */ |
125 | 125 | uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); | |
126 | /* Now create the file */ | 126 | if (uRetVal == 0) |
127 | hTempFile = CreateFile((LPTSTR) szTempName, // file name | 127 | { |
128 | GENERIC_READ | GENERIC_WRITE, // open r-w | 128 | fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); |
129 | 0, // do not share | 129 | return -1; |
130 | NULL, // default security | 130 | } |
131 | CREATE_ALWAYS, // overwrite existing | 131 | |
132 | FILE_ATTRIBUTE_NORMAL,// normal file | 132 | /* Now create the file */ |
133 | NULL); // no template | 133 | hTempFile = CreateFile((LPTSTR) szTempName, // file name |
134 | if (hTempFile == INVALID_HANDLE_VALUE) | 134 | GENERIC_READ | GENERIC_WRITE, // open r-w |
135 | { | 135 | 0, // do not share |
136 | fprintf(stderr, "[ERR] Could not create %s\n", szTempName); | 136 | NULL, // default security |
137 | return -1; | 137 | CREATE_ALWAYS, // overwrite existing |
138 | } | 138 | FILE_ATTRIBUTE_NORMAL,// normal file |
139 | 139 | NULL); // no template | |
140 | fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); | 140 | if (hTempFile == INVALID_HANDLE_VALUE) |
141 | if (!fSuccess) | 141 | { |
142 | { | 142 | fprintf(stderr, "[ERR] Could not create %s\n", szTempName); |
143 | fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); | 143 | return -1; |
144 | return -1; | 144 | } |
145 | } | 145 | |
146 | 146 | fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); | |
147 | fSuccess = CloseHandle (hTempFile); | 147 | if (!fSuccess) |
148 | if (!fSuccess) | 148 | { |
149 | { | 149 | fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); |
150 | fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); | 150 | return -1; |
151 | return -1; | 151 | } |
152 | } | 152 | |
153 | 153 | fSuccess = CloseHandle (hTempFile); | |
154 | tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1); | 154 | if (!fSuccess) |
155 | mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1); | 155 | { |
156 | 156 | fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); | |
157 | fprintf(stderr, "[INFO] Sending firmware...\n"); | 157 | return -1; |
158 | if (mtp_sendnk(tmp, fwsize, &callback)) | 158 | } |
159 | { | 159 | |
160 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); | 160 | tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1); |
161 | ret = 0; | 161 | mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1); |
162 | } | 162 | |
163 | else | 163 | fprintf(stderr, "[INFO] Sending firmware...\n"); |
164 | { | 164 | if (mtp_sendnk(tmp, fwsize, &callback)) |
165 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | 165 | { |
166 | ret = -1; | 166 | fprintf(stderr, "\n"); |
167 | } | 167 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); |
168 | 168 | ret = 0; | |
169 | /* Keep the progress line onscreen */ | 169 | } |
170 | printf("\n"); | 170 | else |
171 | 171 | { | |
172 | free(tmp); | 172 | fprintf(stderr, "\n"); |
173 | 173 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | |
174 | if (!DeleteFile(szTempName)) | 174 | ret = -1; |
175 | fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName); | 175 | } |
176 | 176 | free(tmp); | |
177 | return ret; | 177 | |
178 | } | 178 | if (!DeleteFile(szTempName)) |
179 | 179 | fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName); | |
180 | |||
181 | return ret; | ||
182 | } | ||
183 | |||
184 | |||
185 | int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) | ||
186 | { | ||
187 | wchar_t *fn; | ||
188 | |||
189 | fn = (LPWSTR)malloc(strlen(filename)*2+1); | ||
190 | mbstowcs(fn, filename, strlen(filename)*2+1); | ||
191 | |||
192 | if (mtp_init(mtp_info) < 0) { | ||
193 | fprintf(stderr,"[ERR] Can not init MTP\n"); | ||
194 | return 1; | ||
195 | } | ||
196 | /* Scan for attached MTP devices. */ | ||
197 | if (mtp_scan(mtp_info) < 0) | ||
198 | { | ||
199 | fprintf(stderr,"[ERR] No devices found\n"); | ||
200 | return 1; | ||
201 | } | ||
202 | |||
203 | fprintf(stderr, "[INFO] Sending firmware...\n"); | ||
204 | if (mtp_sendnk(fn, filesize(filename), &callback)) | ||
205 | { | ||
206 | /* keep progress on screen */ | ||
207 | printf("\n"); | ||
208 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); | ||
209 | return 0; | ||
210 | } | ||
211 | else | ||
212 | { | ||
213 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | ||
214 | return -1; | ||
215 | } | ||
216 | mtp_finished(mtp_info); | ||
217 | } | ||
218 | |||
219 | |||
220 | static int filesize(const char* filename) | ||
221 | { | ||
222 | struct _stat sb; | ||
223 | int res; | ||
224 | |||
225 | res = _stat(filename, &sb); | ||
226 | if(res == -1) { | ||
227 | fprintf(stderr, "Error getting filesize!\n"); | ||
228 | return -1; | ||
229 | } | ||
230 | return sb.st_size; | ||
231 | } | ||