diff options
Diffstat (limited to 'utils/MTP/beastpatcher/mtp_win32.c')
-rw-r--r-- | utils/MTP/beastpatcher/mtp_win32.c | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/utils/MTP/beastpatcher/mtp_win32.c b/utils/MTP/beastpatcher/mtp_win32.c new file mode 100644 index 0000000000..f68faddbc2 --- /dev/null +++ b/utils/MTP/beastpatcher/mtp_win32.c | |||
@@ -0,0 +1,174 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (c) 2009, Dave Chapman | ||
12 | * All rights reserved. | ||
13 | * | ||
14 | * Redistribution and use in source and binary forms, with or without | ||
15 | * modification, are permitted provided that the following conditions are | ||
16 | * met: | ||
17 | * | ||
18 | * * Redistributions of source code must retain the above copyright | ||
19 | * notice, this list of conditions and the following disclaimer. | ||
20 | * | ||
21 | * * Redistributions in binary form must reproduce the above | ||
22 | * copyright notice, this list of conditions and the following | ||
23 | * disclaimer in the documentation and/or other materials provided | ||
24 | * with the distribution. | ||
25 | * | ||
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
29 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
30 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
31 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
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. | ||
37 | * | ||
38 | * | ||
39 | ****************************************************************************/ | ||
40 | |||
41 | #include <stdio.h> | ||
42 | #include <string.h> | ||
43 | #include <stddef.h> | ||
44 | #include <stdlib.h> | ||
45 | #include <wchar.h> | ||
46 | #include <stdbool.h> | ||
47 | #include <windows.h> | ||
48 | #include <tchar.h> | ||
49 | |||
50 | #include "mtp_common.h" | ||
51 | |||
52 | |||
53 | extern __declspec(dllimport) bool send_fw(LPWSTR file, int filesize, | ||
54 | void (*callback)(unsigned int progress, unsigned int max)); | ||
55 | |||
56 | int mtp_init(struct mtp_info_t* mtp_info) | ||
57 | { | ||
58 | /* Fill the info struct with zeros - mainly for the strings */ | ||
59 | memset(mtp_info, 0, sizeof(struct mtp_info_t)); | ||
60 | |||
61 | return 0; | ||
62 | |||
63 | } | ||
64 | |||
65 | int mtp_finished(struct mtp_info_t* mtp_info) | ||
66 | { | ||
67 | (void)mtp_info; | ||
68 | |||
69 | return 0; | ||
70 | } | ||
71 | |||
72 | int mtp_scan(struct mtp_info_t* mtp_info) | ||
73 | { | ||
74 | strcpy(mtp_info->manufacturer,"<unknown>"); | ||
75 | strcpy(mtp_info->modelname,"<unknown>"); | ||
76 | strcpy(mtp_info->version,"<unknown>"); | ||
77 | |||
78 | return 0; | ||
79 | |||
80 | } | ||
81 | |||
82 | static void callback(unsigned int progress, unsigned int max) | ||
83 | { | ||
84 | int percent = (progress * 100) / max; | ||
85 | |||
86 | printf("Progress: %u of %u (%d%%)\r", progress, max, percent); | ||
87 | fflush(stdout); | ||
88 | } | ||
89 | |||
90 | |||
91 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | ||
92 | int fwsize) | ||
93 | { | ||
94 | HANDLE hTempFile; | ||
95 | DWORD dwRetVal; | ||
96 | DWORD dwBytesWritten; | ||
97 | UINT uRetVal; | ||
98 | TCHAR szTempName[1024]; | ||
99 | TCHAR lpPathBuffer[1024]; | ||
100 | BOOL fSuccess; | ||
101 | wchar_t *tmp; | ||
102 | int ret; | ||
103 | |||
104 | (void)mtp_info; | ||
105 | |||
106 | /* Get the path for temporary files */ | ||
107 | dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); | ||
108 | if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) | ||
109 | { | ||
110 | fprintf(stderr, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError()); | ||
111 | return -1; | ||
112 | } | ||
113 | |||
114 | /* Create the temporary file */ | ||
115 | uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); | ||
116 | if (uRetVal == 0) | ||
117 | { | ||
118 | fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); | ||
119 | return -1; | ||
120 | } | ||
121 | |||
122 | /* Now create the file */ | ||
123 | hTempFile = CreateFile((LPTSTR) szTempName, // file name | ||
124 | GENERIC_READ | GENERIC_WRITE, // open r-w | ||
125 | 0, // do not share | ||
126 | NULL, // default security | ||
127 | CREATE_ALWAYS, // overwrite existing | ||
128 | FILE_ATTRIBUTE_NORMAL,// normal file | ||
129 | NULL); // no template | ||
130 | if (hTempFile == INVALID_HANDLE_VALUE) | ||
131 | { | ||
132 | fprintf(stderr, "[ERR] Could not create %s\n", szTempName); | ||
133 | return -1; | ||
134 | } | ||
135 | |||
136 | fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); | ||
137 | if (!fSuccess) | ||
138 | { | ||
139 | fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); | ||
140 | return -1; | ||
141 | } | ||
142 | |||
143 | fSuccess = CloseHandle (hTempFile); | ||
144 | if (!fSuccess) | ||
145 | { | ||
146 | fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); | ||
147 | return -1; | ||
148 | } | ||
149 | |||
150 | tmp = (LPWSTR)malloc(strlen(szTempName)*2+1); | ||
151 | mbstowcs(tmp, szTempName, strlen(szTempName)*2+1); | ||
152 | |||
153 | fprintf(stderr, "[INFO] Sending firmware...\n"); | ||
154 | if (send_fw(tmp, fwsize, &callback)) | ||
155 | { | ||
156 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); | ||
157 | ret = 0; | ||
158 | } | ||
159 | else | ||
160 | { | ||
161 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | ||
162 | ret = -1; | ||
163 | } | ||
164 | |||
165 | /* Keep the progress line onscreen */ | ||
166 | printf("\n"); | ||
167 | |||
168 | free(tmp); | ||
169 | |||
170 | if (!DeleteFile(szTempName)) | ||
171 | fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName); | ||
172 | |||
173 | return ret; | ||
174 | } | ||