scsi2sd-util.cc 20 KB

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