SCSI2SD_Bootloader.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include "SCSI2SD_Bootloader.hh"
  18. #include <iostream>
  19. #include <sstream>
  20. #include <stdexcept>
  21. #include <string.h>
  22. using namespace SCSI2SD;
  23. hid_device* SCSI2SDHID_handle = NULL;
  24. // cybtldr interface.
  25. extern "C" int
  26. SCSI2SDHID_OpenConnection(void)
  27. {
  28. return 0;
  29. }
  30. extern "C" int
  31. SCSI2SDHID_CloseConnection(void)
  32. {
  33. return 0;
  34. }
  35. extern "C" int
  36. SCSI2SDHID_ReadData(unsigned char* data, int count)
  37. {
  38. uint8_t buf[65];
  39. buf[0] = 0; // Report ID
  40. int result = hid_read(SCSI2SDHID_handle, buf, count);
  41. if (result < 0)
  42. {
  43. std::cerr << "USB HID Read Failure: " <<
  44. hid_error(SCSI2SDHID_handle) << std::endl;
  45. }
  46. memcpy(data, buf, count);
  47. return (result >= 0) ? 0 : -1;
  48. }
  49. extern "C" int
  50. SCSI2SDHID_WriteData(unsigned char* data, int count)
  51. {
  52. uint8_t buf[65];
  53. buf[0] = 0; // report ID
  54. int i;
  55. for (i = 0; i < count; ++i)
  56. {
  57. buf[i+1] = data[i];
  58. }
  59. int result = -1;
  60. for (int retry = 0; retry < 3 && result < 0; ++retry)
  61. {
  62. result = hid_write(SCSI2SDHID_handle, buf, count + 1);
  63. }
  64. if (result < 0)
  65. {
  66. std::cerr << "USB HID Write Failure: " <<
  67. hid_error(SCSI2SDHID_handle) << std::endl;
  68. }
  69. return (result >= 0) ? 0 : -1;
  70. }
  71. static CyBtldr_CommunicationsData g_cyComms =
  72. {
  73. &SCSI2SDHID_OpenConnection,
  74. &SCSI2SDHID_CloseConnection,
  75. &SCSI2SDHID_ReadData,
  76. &SCSI2SDHID_WriteData,
  77. Bootloader::HID_PACKET_SIZE
  78. };
  79. Bootloader::OperationScope::OperationScope()
  80. {
  81. unsigned long blVer;
  82. if (CyBtldr_StartBootloadOperation(&g_cyComms, 0x2e133069, 0, &blVer)
  83. != CYRET_SUCCESS)
  84. {
  85. throw std::runtime_error("Could not start bootloader operation");
  86. }
  87. }
  88. Bootloader::OperationScope::~OperationScope()
  89. {
  90. CyBtldr_EndBootloadOperation();
  91. }
  92. Bootloader::Bootloader(hid_device_info* hidInfo) :
  93. myHidInfo(hidInfo),
  94. myBootloaderHandle(NULL)
  95. {
  96. myBootloaderHandle = hid_open_path(hidInfo->path);
  97. if (!myBootloaderHandle)
  98. {
  99. std::stringstream msg;
  100. msg << "Error opening HID device " << hidInfo->path << std::endl;
  101. hid_free_enumeration(myHidInfo);
  102. myHidInfo = NULL;
  103. throw std::runtime_error(msg.str());
  104. }
  105. else
  106. {
  107. SCSI2SDHID_handle = myBootloaderHandle;
  108. }
  109. }
  110. Bootloader::~Bootloader()
  111. {
  112. if (myBootloaderHandle)
  113. {
  114. hid_close(myBootloaderHandle);
  115. }
  116. SCSI2SDHID_handle = NULL;
  117. hid_free_enumeration(myHidInfo);
  118. }
  119. Bootloader*
  120. Bootloader::Open()
  121. {
  122. hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
  123. if (dev)
  124. {
  125. return new Bootloader(dev);
  126. }
  127. else
  128. {
  129. return NULL;
  130. }
  131. }
  132. Bootloader::HWInfo
  133. Bootloader::getHWInfo() const
  134. {
  135. HWInfo info = {"unknown", "unknown", "unknown"};
  136. switch (myHidInfo->release_number)
  137. {
  138. case 0x3001:
  139. info.desc = "3.5\" SCSI2SD (green)";
  140. info.version = "V3.0";
  141. info.firmwareName = "SCSI2SD-V3.cyacd";
  142. break;
  143. case 0x3002:
  144. info.desc = "3.5\" SCSI2SD (yellow/red) or 2.5\" SCSI2SD for Apple Powerbook";
  145. info.version = "V4.1/V4.2/V5.0";
  146. info.firmwareName = "SCSI2SD-V4.cyacd";
  147. break;
  148. }
  149. return info;
  150. }
  151. bool
  152. Bootloader::isCorrectFirmware(const std::string& path) const
  153. {
  154. HWInfo info = getHWInfo();
  155. return path.rfind(info.firmwareName) != std::string::npos;
  156. }
  157. std::string
  158. Bootloader::getDevicePath() const
  159. {
  160. return myHidInfo->path;
  161. }
  162. std::wstring
  163. Bootloader::getManufacturer() const
  164. {
  165. return myHidInfo->manufacturer_string;
  166. }
  167. std::wstring
  168. Bootloader::getProductString() const
  169. {
  170. return myHidInfo->product_string;
  171. }
  172. void
  173. Bootloader::load(const std::string& path, void (*progress)(uint8_t, uint16_t))
  174. {
  175. int result = CyBtldr_Program(
  176. path.c_str(),
  177. &g_cyComms,
  178. progress);
  179. if (result)
  180. {
  181. throw std::runtime_error("Firmware update failed");
  182. }
  183. }
  184. bool
  185. Bootloader::ping() const
  186. {
  187. try
  188. {
  189. Bootloader::OperationScope operationGuard;
  190. int result = CyBtldr_VerifyRow(0, 0, 0);
  191. switch (result)
  192. {
  193. case CYRET_SUCCESS:
  194. case CYRET_ERR_CHECKSUM: // We supplied a dummy value of 0.
  195. return true;
  196. default: return false;
  197. }
  198. }
  199. catch (std::exception& e)
  200. {
  201. return false;
  202. }
  203. }