Initial scsi2sd-util GUI tool.
[SCSI2SD-V6.git] / software / scsi2sd-util / scsi2sd-util.cc
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
2 //
3 //      This file is part of SCSI2SD.
4 //
5 //      SCSI2SD is free software: you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation, either version 3 of the License, or
8 //      (at your option) any later version.
9 //
10 //      SCSI2SD is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include <wx/wxprec.h>
21 #ifndef WX_PRECOMP
22 #include <wx/wx.h>
23 #endif
24
25 #include <wx/filedlg.h>
26 #include <wx/notebook.h>
27 #include <wx/progdlg.h>
28 #include <wx/utils.h>
29 #include <wx/windowptr.h>
30 #include <wx/thread.h>
31
32 #include "ConfigUtil.hh"
33 #include "TargetPanel.hh"
34 #include "SCSI2SD_Bootloader.hh"
35 #include "SCSI2SD_HID.hh"
36 #include "Firmware.hh"
37
38 #include <algorithm>
39 #include <vector>
40 #include <set>
41 #include <sstream>
42
43 #if __cplusplus >= 201103L
44 #include <cstdint>
45 #include <memory>
46 using std::shared_ptr;
47 #else
48 #include <stdint.h>
49 #include <tr1/memory>
50 using std::tr1::shared_ptr;
51 #endif
52
53
54 using namespace SCSI2SD;
55
56 class ProgressWrapper
57 {
58 public:
59         void setProgressDialog(
60                 const wxWindowPtr<wxGenericProgressDialog>& dlg,
61                 size_t maxRows)
62         {
63                 myProgressDialog = dlg;
64                 myMaxRows = maxRows;
65                 myNumRows = 0;
66         }
67
68         void clearProgressDialog()
69         {
70                 myProgressDialog.reset();
71         }
72
73         void update(unsigned char arrayId, unsigned short rowNum)
74         {
75                 if (!myProgressDialog) return;
76
77                 myNumRows++;
78
79                 std::stringstream ss;
80                 ss << "Writing flash array " <<
81                         static_cast<int>(arrayId) << " row " <<
82                         static_cast<int>(rowNum);
83                 std::clog << ss.str() << std::endl;
84                 myProgressDialog->Update(myNumRows, ss.str());
85         }
86
87 private:
88         wxWindowPtr<wxGenericProgressDialog> myProgressDialog;
89         size_t myMaxRows;
90         size_t myNumRows;
91 };
92 static ProgressWrapper TheProgressWrapper;
93
94 extern "C"
95 void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
96 {
97         TheProgressWrapper.update(arrayId, rowNum);
98 }
99
100 namespace
101 {
102
103 class TimerLock
104 {
105 public:
106         TimerLock(wxTimer* timer) :
107                 myTimer(timer),
108                 myInterval(myTimer->GetInterval())
109         {
110                 myTimer->Stop();
111         };
112
113         virtual ~TimerLock()
114         {
115                 if (myTimer && myInterval > 0)
116                 {
117                         myTimer->Start(myInterval);
118                 }
119         }
120 private:
121         wxTimer* myTimer;
122         int myInterval;
123 };
124
125 class AppFrame : public wxFrame
126 {
127 public:
128         AppFrame() :
129                 wxFrame(NULL, wxID_ANY, "scsi2sd-util", wxPoint(50, 50), wxSize(600, 650)),
130                 myInitialConfig(false),
131                 myTickCounter(0)
132         {
133                 wxMenu *menuFile = new wxMenu();
134                 menuFile->Append(
135                         ID_ConfigDefaults,
136                         "Load &Defaults",
137                         "Load default configuration options.");
138                 menuFile->Append(
139                         ID_Firmware,
140                         "&Upgrade Firmware...",
141                         "Upgrade or inspect device firmware version.");
142                 menuFile->AppendSeparator();
143                 menuFile->Append(wxID_EXIT);
144                 wxMenu *menuHelp = new wxMenu();
145                 menuHelp->Append(wxID_ABOUT);
146                 wxMenuBar *menuBar = new wxMenuBar();
147                 menuBar->Append( menuFile, "&File" );
148                 menuBar->Append( menuHelp, "&Help" );
149                 SetMenuBar( menuBar );
150
151                 CreateStatusBar();
152                 SetStatusText( "Searching for SCSI2SD" );
153
154                 {
155                         wxPanel* cfgPanel = new wxPanel(this);
156                         wxFlexGridSizer *fgs = new wxFlexGridSizer(2, 1, 5, 5);
157                         wxNotebook* tabs = new wxNotebook(cfgPanel, ID_Notebook);
158
159                         for (int i = 0; i < MAX_SCSI_TARGETS; ++i)
160                         {
161                                 TargetPanel* target =
162                                         new TargetPanel(tabs, ConfigUtil::Default(i));
163                                 myTargets.push_back(target);
164                                 std::stringstream ss;
165                                 ss << "Device " << (i + 1);
166                                 tabs->AddPage(target, ss.str());
167                         }
168                         fgs->Add(tabs);
169
170
171                         wxPanel* btnPanel = new wxPanel(cfgPanel);
172                         wxFlexGridSizer *btnFgs = new wxFlexGridSizer(1, 2, 5, 5);
173                         btnFgs->Add(new wxButton(btnPanel, ID_BtnLoad, wxT("Load from device")));
174                         mySaveButton =
175                                 new wxButton(btnPanel, ID_BtnSave, wxT("Save to device"));
176                         btnFgs->Add(mySaveButton);
177                         {
178                                 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
179                                 hbox->Add(btnFgs, 1, wxALL | wxEXPAND, 15);
180                                 btnPanel->SetSizer(hbox);
181                                 fgs->Add(btnPanel);
182                         }
183
184                         {
185                                 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
186                                 hbox->Add(fgs, 1, wxALL | wxEXPAND, 15);
187                                 cfgPanel->SetSizer(hbox);
188                         }
189                 }
190
191                 myTimer = new wxTimer(this, ID_Timer);
192                 myTimer->Start(1000); //ms
193
194         }
195 private:
196         std::vector<TargetPanel*> myTargets;
197         wxButton* mySaveButton;
198         wxTimer* myTimer;
199         shared_ptr<HID> myHID;
200         shared_ptr<Bootloader> myBootloader;
201         bool myInitialConfig;
202
203         uint8_t myTickCounter;
204
205         void onConfigChanged(wxCommandEvent& event)
206         {
207                 evaluate();
208         }
209
210         void evaluate()
211         {
212                 bool valid = true;
213
214                 // Check for duplicate SCSI IDs
215                 std::set<uint8_t> enabledID;
216
217                 // Check for overlapping SD sectors.
218                 std::vector<std::pair<uint32_t, uint64_t> > sdSectors;
219
220                 bool isTargetEnabled = false; // Need at least one enabled
221
222                 for (size_t i = 0; i < myTargets.size(); ++i)
223                 {
224                         valid = myTargets[i]->evaluate() && valid;
225
226                         if (myTargets[i]->isEnabled())
227                         {
228                                 isTargetEnabled = true;
229                                 uint8_t scsiID = myTargets[i]->getSCSIId();
230                                 if (enabledID.find(scsiID) != enabledID.end())
231                                 {
232                                         myTargets[i]->setDuplicateID(true);
233                                         valid = false;
234                                 }
235                                 else
236                                 {
237                                         enabledID.insert(scsiID);
238                                         myTargets[i]->setDuplicateID(false);
239                                 }
240
241                                 auto sdSectorRange = myTargets[i]->getSDSectorRange();
242                                 for (auto it(sdSectors.begin()); it != sdSectors.end(); ++it)
243                                 {
244                                         if (sdSectorRange.first < it->second &&
245                                                 sdSectorRange.second > it->first)
246                                         {
247                                                 valid = false;
248                                                 myTargets[i]->setSDSectorOverlap(true);
249                                         }
250                                         else
251                                         {
252                                                 myTargets[i]->setSDSectorOverlap(false);
253                                         }
254                                 }
255                                 sdSectors.push_back(sdSectorRange);
256                         }
257                         else
258                         {
259                                 myTargets[i]->setDuplicateID(false);
260                                 myTargets[i]->setSDSectorOverlap(false);
261                         }
262                 }
263
264                 valid = valid && isTargetEnabled; // Need at least one.
265
266                 mySaveButton->Enable(valid && myHID);
267         }
268
269
270         enum
271         {
272                 ID_ConfigDefaults = wxID_HIGHEST + 1,
273                 ID_Firmware,
274                 ID_Timer,
275                 ID_Notebook,
276                 ID_BtnLoad,
277                 ID_BtnSave
278         };
279
280         void OnID_ConfigDefaults(wxCommandEvent& event)
281         {
282                 for (size_t i = 0; i < myTargets.size(); ++i)
283                 {
284                         myTargets[i]->setConfig(ConfigUtil::Default(i));
285                 }
286         }
287
288         void OnID_Firmware(wxCommandEvent& event)
289         {
290                 TimerLock lock(myTimer);
291                 doFirmwareUpdate();
292         }
293
294         void doFirmwareUpdate()
295         {
296                 wxFileDialog dlg(
297                         this,
298                         "Load firmware file",
299                         "",
300                         "",
301                         "SCSI2SD Firmware files (*.cyacd)|*.cyacd",
302                         wxFD_OPEN | wxFD_FILE_MUST_EXIST);
303                 if (dlg.ShowModal() == wxID_CANCEL) return;
304
305                 std::string filename(dlg.GetPath());
306
307                 try
308                 {
309                         if (!myHID) myHID.reset(HID::Open());
310                         if (myHID)
311                         {
312                                 std::clog << "Resetting SCSI2SD into bootloader" << std::endl;
313
314                                 myHID->enterBootloader();
315                                 myHID.reset();
316                         }
317
318                         if (myBootloader)
319                         {
320                                 // Verify the USB HID connection is valid
321                                 if (!myBootloader->ping())
322                                 {
323                                         myBootloader.reset();
324                                 }
325                         }
326
327                         for (int i = 0; !myBootloader && (i < 10); ++i)
328                         {
329                                 std::clog << "Searching for bootloader" << std::endl;
330                                 myBootloader.reset(Bootloader::Open());
331                                 wxMilliSleep(100);
332                         }
333                 }
334                 catch (std::exception& e)
335                 {
336                         std::clog << e.what() << std::endl;
337
338                         SetStatusText(e.what());
339                         myHID.reset();
340                         myBootloader.reset();
341                 }
342
343                 if (!myBootloader)
344                 {
345                         wxMessageBox(
346                                 "SCSI2SD not found",
347                                 "Device not ready",
348                                 wxOK | wxICON_ERROR);
349
350                         return;
351                 }
352
353                 if (!myBootloader->isCorrectFirmware(filename))
354                 {
355                         // TODO allow "force" option
356                         wxMessageBox(
357                                 "Wrong filename",
358                                 "Wrong filename",
359                                 wxOK | wxICON_ERROR);
360                         return;
361                 }
362
363                 int totalFlashRows = 0;
364                 try
365                 {
366                         Firmware firmware(filename);
367                         totalFlashRows = firmware.totalFlashRows();
368                 }
369                 catch (std::exception& e)
370                 {
371                         SetStatusText(e.what());
372                         std::stringstream msg;
373                         msg << "Could not open firmware file: " << e.what();
374                         wxMessageBox(
375                                 msg.str(),
376                                 "Bad file",
377                                 wxOK | wxICON_ERROR);
378                         return;
379                 }
380
381                 {
382                         wxWindowPtr<wxGenericProgressDialog> progress(
383                                 new wxGenericProgressDialog(
384                                         "Loading firmware",
385                                         "Loading firmware",
386                                         totalFlashRows,
387                                         this,
388                                         wxPD_APP_MODAL)
389                                         );
390                         TheProgressWrapper.setProgressDialog(progress, totalFlashRows);
391                 }
392
393                 std::clog << "Upgrading firmware from file: " << filename << std::endl;
394
395                 try
396                 {
397                         myBootloader->load(filename, &ProgressUpdate);
398                         TheProgressWrapper.clearProgressDialog();
399
400                         wxMessageBox(
401                                 "Firmware update successful",
402                                 "Firmware OK",
403                                 wxOK);
404                         SetStatusText("Firmware update successful");
405
406
407                         myHID.reset();
408                         myBootloader.reset();
409                 }
410                 catch (std::exception& e)
411                 {
412                         TheProgressWrapper.clearProgressDialog();
413                         SetStatusText(e.what());
414                         myHID.reset();
415                         myBootloader.reset();
416
417                         wxMessageBox(
418                                 "Firmware Update Failed",
419                                 e.what(),
420                                 wxOK | wxICON_ERROR);
421                 }
422         }
423
424         void OnID_Timer(wxTimerEvent& event)
425         {
426                 // Check if we are connected to the HID device.
427                 // AND/or bootloader device.
428                 try
429                 {
430                         if (myBootloader)
431                         {
432                                 // Verify the USB HID connection is valid
433                                 if (!myBootloader->ping())
434                                 {
435                                         myBootloader.reset();
436                                 }
437                         }
438
439                         if (!myBootloader)
440                         {
441                                 myBootloader.reset(Bootloader::Open());
442
443                                 if (myBootloader)
444                                 {
445                                         SetStatusText(wxT("SCSI2SD Bootloader Ready"));
446                                 }
447                         }
448
449                         if (myHID)
450                         {
451                                 // Verify the USB HID connection is valid
452                                 if (!myHID->ping())
453                                 {
454                                         myHID.reset();
455                                 }
456                         }
457
458                         if (!myHID)
459                         {
460                                 myHID.reset(HID::Open());
461                                 if (myHID)
462                                 {
463                                         uint16_t version = myHID->getFirmwareVersion();
464                                         if (version == 0)
465                                         {
466                                                 // Oh dear, old firmware
467                                                 SetStatusText(wxT("Firmware update required"));
468                                                 myHID.reset();
469                                         }
470                                         else
471                                         {
472                                                 SetStatusText(wxT("SCSI2SD Ready"));
473
474                                                 if (!myInitialConfig)
475                                                 {
476                                                         wxCommandEvent loadEvent(wxEVT_NULL, ID_BtnLoad);
477                                                         GetEventHandler()->AddPendingEvent(loadEvent);
478                                                 }
479                                         }
480                                 }
481                                 else
482                                 {
483                                         char ticks[] = {'/', '-', '\\', '|'};
484                                         std::stringstream ss;
485                                         ss << "Searching for SCSI2SD device " << ticks[myTickCounter % sizeof(ticks)];
486                                         myTickCounter++;
487                                         SetStatusText(ss.str());
488                                 }
489                         }
490                 }
491                 catch (std::runtime_error& e)
492                 {
493                         std::clog << e.what() << std::endl;
494                         SetStatusText(e.what());
495                 }
496
497                 evaluate();
498         }
499
500         void doLoad(wxCommandEvent& event)
501         {
502                 TimerLock lock(myTimer);
503                 if (!myHID) return;
504
505                 std::clog << "Loading configuration" << std::endl;
506
507                 wxWindowPtr<wxGenericProgressDialog> progress(
508                         new wxGenericProgressDialog(
509                                 "Load config settings",
510                                 "Loading config settings",
511                                 100,
512                                 this,
513                                 wxPD_APP_MODAL | wxPD_CAN_ABORT)
514                                 );
515
516                 int flashRow = SCSI_CONFIG_0_ROW;
517                 int currentProgress = 0;
518                 int totalProgress = myTargets.size() * SCSI_CONFIG_ROWS;
519                 for (size_t i = 0;
520                         i < myTargets.size();
521                         ++i, flashRow += SCSI_CONFIG_ROWS)
522                 {
523                         std::vector<uint8_t> raw(sizeof(TargetConfig));
524
525                         for (size_t j = 0; j < SCSI_CONFIG_ROWS; ++j)
526                         {
527                                 std::stringstream ss;
528                                 ss << "Reading flash array " << SCSI_CONFIG_ARRAY <<
529                                         " row " << (flashRow + j);
530                                 SetStatusText(ss.str());
531                                 std::clog << ss.str() << std::endl;
532                                 currentProgress += 1;
533                                 if (!progress->Update(
534                                                 (100 * currentProgress) / totalProgress,
535                                                 ss.str()
536                                                 )
537                                         )
538                                 {
539                                         goto abort;
540                                 }
541
542                                 std::vector<uint8_t> flashData;
543
544                                 try
545                                 {
546                                         myHID->readFlashRow(
547                                                 SCSI_CONFIG_ARRAY, flashRow + j, flashData);
548
549                                 }
550                                 catch (std::runtime_error& e)
551                                 {
552                                         SetStatusText(e.what());
553                                         goto err;
554                                 }
555
556                                 std::copy(
557                                         flashData.begin(),
558                                         flashData.end(),
559                                         &raw[j * SCSI_CONFIG_ROW_SIZE]);
560                         }
561                         myTargets[i]->setConfig(ConfigUtil::fromBytes(&raw[0]));
562                 }
563
564                 myInitialConfig = true;
565                 SetStatusText("Load Complete");
566                 std::clog << "Load Complete" << std::endl;
567                 while (progress->Update(100, "Load Complete"))
568                 {
569                         // Wait for the user to click "Close"
570                         wxMilliSleep(50);
571                 }
572                 goto out;
573
574         err:
575                 SetStatusText("Load failed");
576                 std::clog << "Load failed" << std::endl;
577                 while (progress->Update(100, "Load failed"))
578                 {
579                         // Wait for the user to click "Close"
580                         wxMilliSleep(50);
581                 }
582                 goto out;
583
584         abort:
585                 SetStatusText("Load Aborted");
586                 std::clog << "Load Aborted" << std::endl;
587
588         out:
589                 return;
590         }
591
592         void doSave(wxCommandEvent& event)
593         {
594                 TimerLock lock(myTimer);
595                 if (!myHID) return;
596
597                 std::clog << "Saving configuration" << std::endl;
598
599                 wxWindowPtr<wxGenericProgressDialog> progress(
600                         new wxGenericProgressDialog(
601                                 "Save config settings",
602                                 "Saving config settings",
603                                 100,
604                                 this,
605                                 wxPD_APP_MODAL | wxPD_CAN_ABORT)
606                                 );
607
608                 int flashRow = SCSI_CONFIG_0_ROW;
609                 int currentProgress = 0;
610                 int totalProgress = myTargets.size() * SCSI_CONFIG_ROWS;
611                 for (size_t i = 0;
612                         i < myTargets.size();
613                         ++i, flashRow += SCSI_CONFIG_ROWS)
614                 {
615                         TargetConfig config(myTargets[i]->getConfig());
616                         std::vector<uint8_t> raw(ConfigUtil::toBytes(config));
617
618                         for (size_t j = 0; j < SCSI_CONFIG_ROWS; ++j)
619                         {
620                                 std::stringstream ss;
621                                 ss << "Programming flash array " << SCSI_CONFIG_ARRAY <<
622                                         " row " << (flashRow + j);
623                                 SetStatusText(ss.str());
624                                 std::clog << ss.str() << std::endl;
625                                 currentProgress += 1;
626                                 if (!progress->Update(
627                                                 (100 * currentProgress) / totalProgress,
628                                                 ss.str()
629                                                 )
630                                         )
631                                 {
632                                         goto abort;
633                                 }
634
635                                 std::vector<uint8_t> flashData(SCSI_CONFIG_ROW_SIZE, 0);
636                                 std::copy(
637                                         &raw[j * SCSI_CONFIG_ROW_SIZE],
638                                         &raw[(1+j) * SCSI_CONFIG_ROW_SIZE],
639                                         flashData.begin());
640                                 try
641                                 {
642                                         myHID->writeFlashRow(
643                                                 SCSI_CONFIG_ARRAY, flashRow + j, flashData);
644                                 }
645                                 catch (std::runtime_error& e)
646                                 {
647                                         std::clog << e.what() << std::endl;
648                                         SetStatusText(e.what());
649                                         goto err;
650                                 }
651                         }
652                 }
653
654                 // Reboot so new settings take effect.
655                 myHID->enterBootloader();
656                 myHID.reset();
657
658                 SetStatusText("Save Complete");
659                 std::clog << "Save Complete" << std::endl;
660                 while (progress->Update(100, "Save Complete"))
661                 {
662                         // Wait for the user to click "Close"
663                         wxMilliSleep(50);
664                 }
665                 goto out;
666
667         err:
668                 SetStatusText("Save failed");
669                 std::clog << "Save failed" << std::endl;
670                 while (progress->Update(100, "Save failed"))
671                 {
672                         // Wait for the user to click "Close"
673                         wxMilliSleep(50);
674                 }
675                 goto out;
676
677         abort:
678                 SetStatusText("Save Aborted");
679                 std::clog << "Save Aborted" << std::endl;
680
681         out:
682                 (void) true; // empty statement.
683         }
684
685         void OnExit(wxCommandEvent& event)
686         {
687                 Close(true);
688         }
689         void OnAbout(wxCommandEvent& event)
690         {
691                 wxMessageBox(
692                         "SCSI2SD (scsi2sd-util)\n"
693                         "Copyright (C) 2014 Michael McMaster <michael@codesrc.com>\n"
694                         "\n"
695 "This program is free software: you can redistribute it and/or modify\n"
696 "it under the terms of the GNU General Public License as published by\n"
697 "the Free Software Foundation, either version 3 of the License, or\n"
698 "(at your option) any later version.\n"
699 "\n"
700 "This program is distributed in the hope that it will be useful,\n"
701 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
702 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
703 "GNU General Public License for more details.\n"
704 "\n"
705 "You should have received a copy of the GNU General Public License\n"
706 "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n",
707
708                         "About scsi2sd-util", wxOK | wxICON_INFORMATION );
709         }
710
711         wxDECLARE_EVENT_TABLE();
712 };
713
714 wxBEGIN_EVENT_TABLE(AppFrame, wxFrame)
715         EVT_MENU(AppFrame::ID_ConfigDefaults, AppFrame::OnID_ConfigDefaults)
716         EVT_MENU(AppFrame::ID_Firmware, AppFrame::OnID_Firmware)
717         EVT_MENU(wxID_EXIT, AppFrame::OnExit)
718         EVT_MENU(wxID_ABOUT, AppFrame::OnAbout)
719
720         EVT_TIMER(AppFrame::ID_Timer, AppFrame::OnID_Timer)
721
722         EVT_COMMAND(wxID_ANY, ConfigChangedEvent, AppFrame::onConfigChanged)
723
724         EVT_BUTTON(ID_BtnSave, AppFrame::doSave)
725         EVT_BUTTON(ID_BtnLoad, AppFrame::doLoad)
726
727
728 wxEND_EVENT_TABLE()
729
730
731
732 class App : public wxApp
733 {
734 public:
735         virtual bool OnInit()
736         {
737                 AppFrame* frame = new AppFrame();
738                 frame->Show(true);
739                 SetTopWindow(frame);
740                 return true;
741         }
742 };
743 } // namespace
744
745 // Main Method
746 wxIMPLEMENT_APP(App);
747
748