-
Notifications
You must be signed in to change notification settings - Fork 1
/
programmer.cpp
367 lines (349 loc) · 13.9 KB
/
programmer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* programmer.cpp
* PSG
*
* This file contains a program for automatically uploading a firmware update to
* an attached PSG device. It can take a .uf2 (Pico only), .hex (PIC only), or
* .bin (combined) firmware file, and sends it to the device.
*
* This code is licensed under the GPLv2 license.
* Copyright (c) 2022-2023 JackMacWindows.
*/
#include <portmidi.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <cstring>
#if defined(__linux__)
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
static std::vector<std::string> split(const std::string& strToSplit, const char * delims) {
std::vector<std::string> retval;
size_t pos = strToSplit.find_first_not_of(delims);
while (pos != std::string::npos) {
const size_t end = strToSplit.find_first_of(delims, pos);
retval.push_back(strToSplit.substr(pos, end - pos));
pos = strToSplit.find_first_not_of(delims, end);
}
return retval;
}
std::string getMountpoint() {
// wait for RPI-RP2 drive
struct stat st;
while (true) if (stat("/dev/disk/by-label/RPI-RP2", &st) == 0) break;
std::this_thread::sleep_for(std::chrono::seconds(2)); // wait for possible mount
// search for automount
std::string node = std::to_string(major(st.st_rdev)) + ":" + std::to_string(minor(st.st_rdev));
std::ifstream in("/proc/self/mountinfo");
while (!in.eof()) {
std::string line;
std::getline(in, line);
if (line.empty()) continue;
std::vector<std::string> parts = split(line, " ");
if (parts.empty()) continue;
if (parts[2] == node) return parts[4];
}
in.close();
// attempt to mount manually
mkdir(".picomount", 0777);
if (mount("/dev/disk/by-label/RPI-RP2", ".picomount", "vfat", MS_NOATIME, NULL) != 0) {
std::cout << "Cannot find mount, and cannot mount disk manually\nPlease mount the RPI-RP2 disk manually and type the path here: ";
std::string line;
std::getline(std::cin, line);
return line;
}
return ".picomount";
}
void mountCleanup() {
sync();
}
#elif defined(_WIN32)
#include <windows.h>
#include <stdio.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <winioctl.h>
#define BUFFER_SIZE 256
#define MAX_DRIVES 26
// Finds the device interface for the CDROM drive with the given interface number.
DEVINST GetDrivesDevInstByDeviceNumber(long DeviceNumber) {
const GUID *guid = &GUID_DEVINTERFACE_DISK;
// Get device interface info set handle
// for all devices attached to system
HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) return 0;
// Retrieve a context structure for a device interface of a device information set.
BYTE buf[1024];
PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buf;
SP_DEVICE_INTERFACE_DATA spdid;
SP_DEVINFO_DATA spdd;
DWORD dwSize;
spdid.cbSize = sizeof(spdid);
// Iterate through all the interfaces and try to match one based on
// the device number.
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, i, &spdid); i++) {
// Get the device path.
dwSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize, NULL);
if (dwSize == 0 || dwSize > sizeof(buf)) continue;
pspdidd->cbSize = sizeof(*pspdidd);
ZeroMemory((PVOID)&spdd, sizeof(spdd));
spdd.cbSize = sizeof(spdd);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, dwSize, &dwSize, &spdd)) continue;
// Open the device.
HANDLE hDrive = CreateFile(pspdidd->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDrive == INVALID_HANDLE_VALUE) continue;
// Get the device number.
STORAGE_DEVICE_NUMBER sdn;
dwSize = 0;
if (DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwSize, NULL)) {
// Does it match?
if (DeviceNumber == (long)sdn.DeviceNumber) {
CloseHandle(hDrive);
SetupDiDestroyDeviceInfoList(hDevInfo);
return spdd.DevInst;
}
}
CloseHandle(hDrive);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
// Returns true if the given device instance belongs to the USB device with the given VID and PID.
boolean matchDevInstToUsbDevice(DEVINST device, DWORD vid, DWORD pid) {
// This is the string we will be searching for in the device harware IDs.
TCHAR hwid[64];
sprintf(hwid, "VID_%04X&PID_%04X", vid, pid);
// Get a list of hardware IDs for all USB devices.
ULONG ulLen;
CM_Get_Device_ID_List_Size(&ulLen, NULL, CM_GETIDLIST_FILTER_NONE);
TCHAR *pszBuffer = malloc(sizeof(TCHAR) * ulLen);
CM_Get_Device_ID_List(NULL, pszBuffer, ulLen, CM_GETIDLIST_FILTER_NONE);
// Iterate through the list looking for our ID.
for (LPTSTR pszDeviceID = pszBuffer; *pszDeviceID; pszDeviceID += _tcslen(pszDeviceID) + 1) {
// Some versions of Windows have the string in upper case and other versions have it
// in lower case so just make it all upper.
for (int i = 0; pszDeviceID[i]; i++) pszDeviceID[i] = toupper(pszDeviceID[i]);
if (_tcsstr(pszDeviceID, hwid)) {
// Found the device, now we want the grandchild device, which is the "generic volume"
DEVINST MSDInst = 0;
if (CR_SUCCESS == CM_Locate_DevNode(&MSDInst, pszDeviceID, CM_LOCATE_DEVNODE_NORMAL)) {
DEVINST DiskDriveInst = 0;
if (CR_SUCCESS == CM_Get_Child(&DiskDriveInst, MSDInst, 0)) {
// Now compare the grandchild node against the given device instance.
if (device == DiskDriveInst) return TRUE;
}
}
}
}
return FALSE;
}
std::string getMountpoint() {
// TODO: wait for device
DWORD vid = 0x2e8a, pid = 0x0003;
TCHAR caDrive[4] = TEXT("A:\\");
TCHAR volume[BUFFER_SIZE];
TCHAR volume_path_name[BUFFER_SIZE];
DWORD dwDriveMask;
int count = 0;
// Get all drives in the system.
dwDriveMask = GetLogicalDrives();
if (dwDriveMask == 0) {
std::cerr << "Error - GetLogicalDrives failed\n";
return "";
}
// Loop for all drives.
for (int nLoopIndex = 0; nLoopIndex < MAX_DRIVES; nLoopIndex++, dwDriveMask >>= 1) {
// If a drive is present,
if (dwDriveMask & 1) {
caDrive[0] = TEXT('A') + nLoopIndex;
// If a drive is removable.
if (GetDriveType(caDrive) == DRIVE_REMOVABLE) {
//Get its volume info.
if (GetVolumeNameForVolumeMountPoint(caDrive, volume, BUFFER_SIZE)) {
DWORD lpcchReturnLength;
GetVolumePathNamesForVolumeName(volume, volume_path_name, BUFFER_SIZE, &lpcchReturnLength);
char szVolumeAccessPath[] = "\\\\.\\X:";
szVolumeAccessPath[4] = caDrive[0];
long DeviceNumber = -1;
HANDLE hVolume = CreateFile(szVolumeAccessPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hVolume == INVALID_HANDLE_VALUE) return 1;
STORAGE_DEVICE_NUMBER sdn;
DWORD dwBytesReturned = 0;
long res = DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
if (res) DeviceNumber = sdn.DeviceNumber;
CloseHandle(hVolume);
if (DeviceNumber != -1) {
DEVINST DevInst = GetDrivesDevInstByDeviceNumber(DeviceNumber);
boolean match = matchDevInstToUsbDevice(DevInst, vid, pid);
if (match) return volume_path_name;
}
count++;
}
}
}
}
std::cerr << "Could not find drive\n";
return "";
}
void mountCleanup() {}
#elif defined(__APPLE__)
#include <sys/stat.h>
std::string getMountpoint() {
// wait for RPI-RP2 drive
struct stat st;
while (true) {
if (stat("/Volumes/RPI-RP2", &st) == 0) break;
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
return "/Volumes/RPI-RP2";
}
void mountCleanup() {}
#else
#error Unsupported platform
#endif
static std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
static PmTimestamp milliseconds(void *time_info) {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - startTime).count();
}
int main(int argc, const char * argv[]) {
std::string hexdata;
uint8_t * uf2data;
size_t uf2size = 0;
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <firmware.bin|uf2|hex>\n";
return 1;
}
std::ifstream in(argv[1], std::ios::binary);
if (!in.is_open()) {
std::cerr << "Could not open input file\n";
return 2;
}
std::cout << "Reading firmware file\n";
std::string line;
int i = 1;
for (std::getline(in, line); !in.eof() && line != "UF2"; std::getline(in, line), i++) {
if (!line.empty()) {
if (line[0] != ':') {
std::cerr << "Invalid HEX data on line " << i << "\n";
return 3;
}
hexdata += line + "\n";
}
}
if (!in.eof()) {
uint8_t buf[508];
uint32_t * buf32 = (uint32_t*)buf;
in.read((char*)buf, 508);
if (in.gcount() < 508 || buf32[0] != 0x9E5D5157) {
std::cerr << "Invalid UF2 data\n";
return 4;
}
uf2size = buf32[5] * 512;
uf2data = new uint8_t[uf2size];
uf2data[0] = 'U';
uf2data[1] = 'F';
uf2data[2] = '2';
uf2data[3] = '\n';
memcpy(uf2data + 4, buf, 508);
in.read((char*)uf2data + 512, uf2size - 512);
}
in.close();
if (uf2size == 0 && hexdata.empty()) {
std::cerr << "No firmware data present\n";
return 5;
}
std::cout << "Opening MIDI device\n";
PortMidiStream * stream = NULL, * streamin = NULL;
PmError error;
if ((error = Pm_Initialize()) != pmNoError) throw std::runtime_error(std::string("Could not init: ") + Pm_GetErrorText(error));
for (int i = 0; i < Pm_CountDevices(); i++) {
const PmDeviceInfo * inf = Pm_GetDeviceInfo(i);
if (inf == NULL) {
std::cerr << "No PSG device found\n";
return 6;
}
if (inf->output && strstr(inf->name, "PSG")) {
if ((error = Pm_OpenOutput(&stream, i, NULL, 0, milliseconds, NULL, 0)) != pmNoError) {
std::cerr << "Could not open device: " << Pm_GetErrorText(error) << "\n";
return error;
}
std::cout << "Opened MIDI output device " << inf->name << "\n";
if (streamin != NULL) break;
}
if (inf->input && strstr(inf->name, "PSG")) {
if ((error = Pm_OpenInput(&streamin, i, NULL, 0, milliseconds, NULL)) != pmNoError) {
std::cerr << "Could not open device: " << Pm_GetErrorText(error) << "\n";
return error;
}
std::cout << "Opened MIDI input device " << inf->name << "\n";
if (stream != NULL) break;
}
}
if (stream == NULL || streamin == NULL) {
std::cerr << "No PSG device found\n";
return 6;
}
if (!hexdata.empty()) {
std::cout << "Uploading PIC firmware (" << hexdata.size() << " bytes)\n";
uint8_t * data = new uint8_t[hexdata.size()+7];
data[0] = 0xF0;
data[1] = 0x00;
data[2] = 0x46;
data[3] = 0x71;
data[4] = 0x00;
data[5] = 0;
memcpy(data + 6, hexdata.c_str(), hexdata.size());
data[hexdata.size() + 6] = 0xF7;
Pm_WriteSysEx(stream, 0, data);
std::cout << "Waiting for write to complete\n";
while (!Pm_Poll(streamin)) std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "Flash finished, reloading output\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // make sure the device has time to boot
Pm_Close(stream);
for (int i = 0; i < Pm_CountDevices(); i++) {
const PmDeviceInfo * inf = Pm_GetDeviceInfo(i);
if (inf == NULL) {
std::cerr << "No PSG device found\n";
return 6;
}
if (inf->output && strstr(inf->name, "PSG")) {
if ((error = Pm_OpenOutput(&stream, i, NULL, 0, milliseconds, NULL, 0)) != pmNoError) {
std::cerr << "Could not open device: " << Pm_GetErrorText(error) << "\n";
return error;
}
std::cout << "Opened MIDI output device " << inf->name << "\n";
break;
}
}
if (stream == NULL) {
std::cerr << "No PSG device found\n";
return 6;
}
}
if (uf2size) {
std::cout << "Flipping device into bootloader mode\n";
uint8_t msg[] = {0xF0, 0x00, 0x46, 0x71, 0x01, 0x00, 0xF7};
Pm_WriteSysEx(stream, 0, msg);
std::cout << "Waiting for USB device\n";
std::string mountpoint = getMountpoint();
if (mountpoint == "") return 6;
std::cout << "Found Pico at " << mountpoint << "\nUploading Pico firmware (" << uf2size << " bytes)\n";
std::ofstream out(mountpoint + "/firmware.uf2", std::ios::binary);
out.write((char*)uf2data, uf2size);
out.close();
mountCleanup();
delete[] uf2data;
std::cout << "Upload finished, Pico will reboot momentarily\n";
}
Pm_Close(stream);
Pm_Close(streamin);
std::cout << "Finished programming device\n";
return 0;
}