diff options
Diffstat (limited to 'utils/MTP/MTP_DLL/sendfirm_win.cpp')
-rw-r--r-- | utils/MTP/MTP_DLL/sendfirm_win.cpp | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/utils/MTP/MTP_DLL/sendfirm_win.cpp b/utils/MTP/MTP_DLL/sendfirm_win.cpp new file mode 100644 index 0000000000..12c3e3e88b --- /dev/null +++ b/utils/MTP/MTP_DLL/sendfirm_win.cpp | |||
@@ -0,0 +1,189 @@ | |||
1 | /* Windows MTP Firmware Uploading Implementation | ||
2 | * | ||
3 | * Based on http://opensource.creative.com/mtp_xfer.html | ||
4 | * | ||
5 | * Edited by Maurus Cuelenaere for Rockbox | ||
6 | */ | ||
7 | |||
8 | #include <windows.h> | ||
9 | #include "mswmdm_i.c" | ||
10 | #include "mswmdm.h" | ||
11 | #include "sac.h" | ||
12 | #include "scclient.h" | ||
13 | |||
14 | /* | ||
15 | * Compilation requirements: | ||
16 | * | ||
17 | * Download the Windows Media Format 9.5 SDK | ||
18 | * Add "c:\wmsdk\wmfsdk95\include,c:\wmsdk\wmfsdk95\wmdm\inc" to your inclusion path | ||
19 | * Add "c:\wmsdk\wmfsdk95\lib,c:\wmsdk\wmfsdk95\wmdm\lib" to your library inclusion path | ||
20 | * Link to "mssachlp.lib" | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | __declspec(dllexport) bool send_fw(LPWSTR file, int filesize) | ||
25 | { | ||
26 | bool return_value = false; | ||
27 | HRESULT hr; | ||
28 | IComponentAuthenticate* pICompAuth; | ||
29 | CSecureChannelClient *m_pSacClient = new CSecureChannelClient; | ||
30 | IWMDeviceManager3* m_pIdvMgr = NULL; | ||
31 | |||
32 | /* these are generic keys */ | ||
33 | BYTE abPVK[] = {0x00}; | ||
34 | BYTE abCert[] = {0x00}; | ||
35 | |||
36 | CoInitialize(NULL); | ||
37 | |||
38 | /* get an authentication interface */ | ||
39 | hr = CoCreateInstance(CLSID_MediaDevMgr, NULL, CLSCTX_ALL ,IID_IComponentAuthenticate, (void **)&pICompAuth); | ||
40 | if SUCCEEDED(hr) | ||
41 | { | ||
42 | /* create a secure channel client certificate */ | ||
43 | hr = m_pSacClient->SetCertificate(SAC_CERT_V1, (BYTE*) abCert, sizeof(abCert), (BYTE*) abPVK, sizeof(abPVK)); | ||
44 | if SUCCEEDED(hr) | ||
45 | { | ||
46 | /* bind the authentication interface to the secure channel client */ | ||
47 | m_pSacClient->SetInterface(pICompAuth); | ||
48 | |||
49 | /* trigger communication */ | ||
50 | hr = m_pSacClient->Authenticate(SAC_PROTOCOL_V1); | ||
51 | if SUCCEEDED(hr) | ||
52 | { | ||
53 | /* get main interface to media device manager */ | ||
54 | hr = pICompAuth->QueryInterface(IID_IWMDeviceManager2, (void**)&m_pIdvMgr); | ||
55 | if SUCCEEDED(hr) | ||
56 | { | ||
57 | /* enumerate devices... */ | ||
58 | IWMDMEnumDevice *pIEnumDev; | ||
59 | hr = m_pIdvMgr->EnumDevices2(&pIEnumDev); | ||
60 | if SUCCEEDED(hr) | ||
61 | { | ||
62 | hr = pIEnumDev->Reset(); /* Next will now return the first device */ | ||
63 | if SUCCEEDED(hr) | ||
64 | { | ||
65 | IWMDMDevice3* pIDevice; | ||
66 | unsigned long ulNumFetched; | ||
67 | hr = pIEnumDev->Next(1, (IWMDMDevice **)&pIDevice, &ulNumFetched); | ||
68 | while (SUCCEEDED(hr) && (hr != S_FALSE)) | ||
69 | { | ||
70 | #if 0 | ||
71 | /* output device name */ | ||
72 | wchar_t pwsString[256]; | ||
73 | hr = pIDevice->GetName(pwsString, 256); | ||
74 | if SUCCEEDED(hr) | ||
75 | wprintf(L"Found device %s\n", pwsString); | ||
76 | #endif | ||
77 | |||
78 | /* get storage info */ | ||
79 | DWORD tempDW; | ||
80 | pIDevice->GetType(&tempDW); | ||
81 | if (tempDW & WMDM_DEVICE_TYPE_STORAGE) | ||
82 | { | ||
83 | IWMDMEnumStorage *pIEnumStorage = NULL; | ||
84 | IWMDMStorage *pIStorage = NULL; | ||
85 | IWMDMStorage3 *pIFileStorage = NULL; | ||
86 | hr = pIDevice->EnumStorage(&pIEnumStorage); | ||
87 | if SUCCEEDED(hr) | ||
88 | { | ||
89 | pIEnumStorage->Reset(); | ||
90 | hr = pIEnumStorage->Next(1, (IWMDMStorage **)&pIStorage, &ulNumFetched); | ||
91 | while (SUCCEEDED(hr) && (hr != S_FALSE)) | ||
92 | { | ||
93 | IWMDMStorage3 *pNewStorage; | ||
94 | hr = pIStorage->QueryInterface(IID_IWMDMStorage3, (void **)&pNewStorage); | ||
95 | if SUCCEEDED(hr) | ||
96 | { | ||
97 | IWMDMStorageControl3 *pIWMDMStorageControl; | ||
98 | hr = pNewStorage->QueryInterface(IID_IWMDMStorageControl3, | ||
99 | (void**)&pIWMDMStorageControl); | ||
100 | if SUCCEEDED(hr) | ||
101 | { | ||
102 | IWMDMMetaData *pIWMDMMetaData = NULL; | ||
103 | hr = pNewStorage->CreateEmptyMetadataObject(&pIWMDMMetaData); | ||
104 | if (SUCCEEDED(hr)) | ||
105 | { | ||
106 | DWORD dw = WMDM_FORMATCODE_UNDEFINEDFIRMWARE; | ||
107 | hr = pIWMDMMetaData->AddItem(WMDM_TYPE_DWORD, g_wszWMDMFormatCode, (BYTE *)&dw, sizeof(dw)); | ||
108 | hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMFileName, (BYTE *)L"nk.bin", 32); | ||
109 | DWORD ow[2]; | ||
110 | ow[0] = filesize; | ||
111 | ow[1] = 0; | ||
112 | hr = pIWMDMMetaData->AddItem(WMDM_TYPE_QWORD, g_wszWMDMFileSize, (BYTE *)ow, 2 * sizeof(dw)); | ||
113 | if (SUCCEEDED(hr)) | ||
114 | { | ||
115 | IWMDMStorage *pNewObject = NULL; | ||
116 | |||
117 | hr = pIWMDMStorageControl->Insert3( | ||
118 | WMDM_MODE_BLOCK | WMDM_CONTENT_FILE, | ||
119 | 0, | ||
120 | file, | ||
121 | NULL, | ||
122 | NULL, | ||
123 | NULL, | ||
124 | pIWMDMMetaData, | ||
125 | NULL, | ||
126 | (IWMDMStorage **)&pNewObject); | ||
127 | |||
128 | if(SUCCEEDED(hr) || hr == WMDM_S_NOT_ALL_PROPERTIES_APPLIED | ||
129 | || hr == WMDM_S_NOT_ALL_PROPERTIES_RETRIEVED) | ||
130 | return_value = true; | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | /* move to next Storage */ | ||
137 | if(!return_value) | ||
138 | hr = pIEnumStorage->Next(1, (IWMDMStorage **)&pIStorage, &ulNumFetched); | ||
139 | } | ||
140 | pIEnumStorage->Release(); | ||
141 | } | ||
142 | |||
143 | /* move to next device */ | ||
144 | if(!return_value) | ||
145 | hr = pIEnumDev->Next(1, (IWMDMDevice **)&pIDevice, &ulNumFetched); | ||
146 | } | ||
147 | pIEnumDev->Release(); | ||
148 | } | ||
149 | m_pIdvMgr->Release(); | ||
150 | } | ||
151 | pICompAuth->Release(); | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | CoUninitialize(); | ||
158 | |||
159 | return return_value; | ||
160 | } | ||
161 | |||
162 | /* | ||
163 | * Example code: | ||
164 | |||
165 | #include <windows.h> | ||
166 | int filesize(char* filename) | ||
167 | { | ||
168 | FILE* fd; | ||
169 | int tmp; | ||
170 | fopen_s(&fd, filename, "r"); | ||
171 | fseek(fd, 0, SEEK_END); | ||
172 | tmp = ftell(fd); | ||
173 | fclose(fd); | ||
174 | return tmp; | ||
175 | } | ||
176 | |||
177 | __declspec(dllimport) bool send_fw(LPWSTR file, int filesize); | ||
178 | |||
179 | int _tmain(int argc, char* argv[]) | ||
180 | { | ||
181 | printf("Sending %s [%d]...\n", "Zlant.bin", filesize("Zlant.bin")); | ||
182 | if(send_fw(L"Zlant.bin", filesize("Zlant.bin"))) | ||
183 | printf("Transmitting succesfull!\n"); | ||
184 | else | ||
185 | printf("Transmitting failed!\n"); | ||
186 | return 0; | ||
187 | } | ||
188 | |||
189 | */ | ||