summaryrefslogtreecommitdiff
path: root/utils/MTP/MTP_DLL/sendfirm_win.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/MTP/MTP_DLL/sendfirm_win.cpp')
-rw-r--r--utils/MTP/MTP_DLL/sendfirm_win.cpp260
1 files changed, 0 insertions, 260 deletions
diff --git a/utils/MTP/MTP_DLL/sendfirm_win.cpp b/utils/MTP/MTP_DLL/sendfirm_win.cpp
deleted file mode 100644
index 6f9cf24e93..0000000000
--- a/utils/MTP/MTP_DLL/sendfirm_win.cpp
+++ /dev/null
@@ -1,260 +0,0 @@
1/*
2 * Windows MTP Firmware Uploading Implementation
3 *
4 * Based on http://opensource.creative.com/mtp_xfer.html
5 * Edited by Maurus Cuelenaere for Rockbox
6 *
7 * Copyright (c) 2009, Maurus Cuelenaere
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the <organization> nor the
18 * names of its contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY MAURUS CUELENAERE ''AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL MAURUS CUELENAERE BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <windows.h>
34#include "mswmdm_i.c"
35#include "mswmdm.h"
36#include "sac.h"
37#include "scclient.h"
38
39class CProgressHelper :
40 public IWMDMProgress
41{
42 void (*m_callback)(unsigned int progress, unsigned int max);
43 DWORD m_max_ticks;
44 DWORD m_cur_ticks;
45 DWORD m_counter;
46
47public:
48 CProgressHelper( void (*callback)(unsigned int progress, unsigned int max) );
49 ~CProgressHelper();
50 STDMETHOD(Begin)( DWORD dwEstimatedTicks );
51 STDMETHOD(Progress)( DWORD dwTranspiredTicks );
52 STDMETHOD(End)();
53
54 STDMETHOD(QueryInterface) ( REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject );
55 STDMETHOD_(ULONG, AddRef)( void );
56 STDMETHOD_(ULONG, Release)( void );
57};
58
59/*
60 * Compilation requirements:
61 *
62 * Download the Windows Media Format 9.5 SDK
63 * Add "c:\wmsdk\wmfsdk95\include,c:\wmsdk\wmfsdk95\wmdm\inc" to your inclusion path
64 * Add "c:\wmsdk\wmfsdk95\lib,c:\wmsdk\wmfsdk95\wmdm\lib" to your library inclusion path
65 * Link to "mssachlp.lib"
66 *
67 */
68extern "C" {
69__declspec(dllexport) bool send_fw(LPWSTR file, int filesize, void (*callback)(unsigned int progress, unsigned int max))
70{
71 bool return_value = false;
72 HRESULT hr;
73 IComponentAuthenticate* pICompAuth;
74 CSecureChannelClient *m_pSacClient = new CSecureChannelClient;
75 IWMDeviceManager3* m_pIdvMgr = NULL;
76
77 /* these are generic keys */
78 BYTE abPVK[] = {0x00};
79 BYTE abCert[] = {0x00};
80
81 CoInitialize(NULL);
82
83 /* get an authentication interface */
84 hr = CoCreateInstance(CLSID_MediaDevMgr, NULL, CLSCTX_ALL ,IID_IComponentAuthenticate, (void **)&pICompAuth);
85 if SUCCEEDED(hr)
86 {
87 /* create a secure channel client certificate */
88 hr = m_pSacClient->SetCertificate(SAC_CERT_V1, (BYTE*) abCert, sizeof(abCert), (BYTE*) abPVK, sizeof(abPVK));
89 if SUCCEEDED(hr)
90 {
91 /* bind the authentication interface to the secure channel client */
92 m_pSacClient->SetInterface(pICompAuth);
93
94 /* trigger communication */
95 hr = m_pSacClient->Authenticate(SAC_PROTOCOL_V1);
96 if SUCCEEDED(hr)
97 {
98 /* get main interface to media device manager */
99 hr = pICompAuth->QueryInterface(IID_IWMDeviceManager2, (void**)&m_pIdvMgr);
100 if SUCCEEDED(hr)
101 {
102 /* enumerate devices... */
103 IWMDMEnumDevice *pIEnumDev;
104 hr = m_pIdvMgr->EnumDevices2(&pIEnumDev);
105 if SUCCEEDED(hr)
106 {
107 hr = pIEnumDev->Reset(); /* Next will now return the first device */
108 if SUCCEEDED(hr)
109 {
110 IWMDMDevice3* pIDevice;
111 unsigned long ulNumFetched;
112 hr = pIEnumDev->Next(1, (IWMDMDevice **)&pIDevice, &ulNumFetched);
113 while (SUCCEEDED(hr) && (hr != S_FALSE))
114 {
115 /* get storage info */
116 DWORD tempDW;
117 pIDevice->GetType(&tempDW);
118 if (tempDW & WMDM_DEVICE_TYPE_STORAGE)
119 {
120 IWMDMEnumStorage *pIEnumStorage = NULL;
121 IWMDMStorage *pIStorage = NULL;
122 IWMDMStorage3 *pIFileStorage = NULL;
123 hr = pIDevice->EnumStorage(&pIEnumStorage);
124 if SUCCEEDED(hr)
125 {
126 pIEnumStorage->Reset();
127 hr = pIEnumStorage->Next(1, (IWMDMStorage **)&pIStorage, &ulNumFetched);
128 while (SUCCEEDED(hr) && (hr != S_FALSE))
129 {
130 IWMDMStorage3 *pNewStorage;
131 hr = pIStorage->QueryInterface(IID_IWMDMStorage3, (void **)&pNewStorage);
132 if SUCCEEDED(hr)
133 {
134 IWMDMStorageControl3 *pIWMDMStorageControl;
135 hr = pNewStorage->QueryInterface(IID_IWMDMStorageControl3,
136 (void**)&pIWMDMStorageControl);
137 if SUCCEEDED(hr)
138 {
139 IWMDMMetaData *pIWMDMMetaData = NULL;
140 hr = pNewStorage->CreateEmptyMetadataObject(&pIWMDMMetaData);
141 if (SUCCEEDED(hr))
142 {
143 DWORD dw = WMDM_FORMATCODE_UNDEFINEDFIRMWARE;
144 hr = pIWMDMMetaData->AddItem(WMDM_TYPE_DWORD, g_wszWMDMFormatCode, (BYTE *)&dw, sizeof(dw));
145 hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMFileName, (BYTE *)L"nk.bin", 32);
146 DWORD ow[2];
147 ow[0] = filesize;
148 ow[1] = 0;
149 hr = pIWMDMMetaData->AddItem(WMDM_TYPE_QWORD, g_wszWMDMFileSize, (BYTE *)ow, 2 * sizeof(dw));
150 if (SUCCEEDED(hr))
151 {
152 IWMDMStorage *pNewObject = NULL;
153 CProgressHelper *progress = new CProgressHelper(callback);
154
155 hr = pIWMDMStorageControl->Insert3(
156 WMDM_MODE_BLOCK | WMDM_CONTENT_FILE | WMDM_MODE_PROGRESS,
157 0,
158 file,
159 NULL,
160 NULL,
161 (callback == NULL ? NULL : (IWMDMProgress*)progress),
162 pIWMDMMetaData,
163 NULL,
164 (IWMDMStorage **)&pNewObject);
165
166 if(SUCCEEDED(hr) || hr == WMDM_S_NOT_ALL_PROPERTIES_APPLIED
167 || hr == WMDM_S_NOT_ALL_PROPERTIES_RETRIEVED)
168 {
169 return_value = true;
170 hr = S_FALSE;
171 }
172 }
173 }
174 }
175 }
176 }
177 }
178 pIEnumStorage->Release();
179 }
180
181 /* move to next device */
182 if(!return_value)
183 hr = pIEnumDev->Next(1, (IWMDMDevice **)&pIDevice, &ulNumFetched);
184 }
185 pIEnumDev->Release();
186 }
187 m_pIdvMgr->Release();
188 }
189 pICompAuth->Release();
190 }
191 }
192 }
193 }
194
195 CoUninitialize();
196
197 return return_value;
198}
199}
200
201
202CProgressHelper::CProgressHelper( void (*callback)(unsigned int progress, unsigned int max) )
203{
204 m_cur_ticks = 0;
205 m_max_ticks = 0;
206 m_counter = 0;
207
208 m_callback = callback;
209}
210
211CProgressHelper::~CProgressHelper()
212{
213}
214
215HRESULT CProgressHelper::Begin( DWORD dwEstimatedTicks )
216{
217 m_max_ticks = dwEstimatedTicks;
218
219 return S_OK;
220}
221
222HRESULT CProgressHelper::Progress( DWORD dwTranspiredTicks )
223{
224 m_cur_ticks = dwTranspiredTicks;
225
226 if(m_callback != NULL)
227 m_callback(m_cur_ticks, max(m_max_ticks, m_cur_ticks));
228
229 return S_OK;
230}
231
232HRESULT CProgressHelper::End()
233{
234 m_cur_ticks = m_max_ticks;
235
236 return S_OK;
237}
238
239HRESULT CProgressHelper::QueryInterface( REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject )
240{
241 if(riid == IID_IWMDMProgress || riid == IID_IUnknown)
242 {
243 *ppvObject = this;
244 return S_OK;
245 }
246 else
247 {
248 *ppvObject = NULL;
249 return E_NOINTERFACE;
250 }
251}
252
253ULONG CProgressHelper::AddRef()
254{
255 return m_counter++;
256}
257ULONG CProgressHelper::Release()
258{
259 return m_counter--;
260}