SCSI2SD_Bootloader.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = hid_write(SCSI2SDHID_handle, buf, count + 1);
  60. if (result < 0)
  61. {
  62. std::cerr << "USB HID Write Failure: " <<
  63. hid_error(SCSI2SDHID_handle) << std::endl;
  64. }
  65. return (result >= 0) ? 0 : -1;
  66. }
  67. static CyBtldr_CommunicationsData g_cyComms =
  68. {
  69. &SCSI2SDHID_OpenConnection,
  70. &SCSI2SDHID_CloseConnection,
  71. &SCSI2SDHID_ReadData,
  72. &SCSI2SDHID_WriteData,
  73. Bootloader::HID_PACKET_SIZE
  74. };
  75. Bootloader::OperationScope::OperationScope()
  76. {
  77. unsigned long blVer;
  78. if (CyBtldr_StartBootloadOperation(&g_cyComms, 0x2e133069, 0, &blVer)
  79. != CYRET_SUCCESS)
  80. {
  81. throw std::runtime_error("Could not start bootloader operation");
  82. }
  83. }
  84. Bootloader::OperationScope::~OperationScope()
  85. {
  86. CyBtldr_EndBootloadOperation();
  87. }
  88. Bootloader::Bootloader(hid_device_info* hidInfo) :
  89. myHidInfo(hidInfo),
  90. myBootloaderHandle(NULL)
  91. {
  92. myBootloaderHandle = hid_open_path(hidInfo->path);
  93. if (!myBootloaderHandle)
  94. {
  95. std::stringstream msg;
  96. msg << "Error opening HID device " << hidInfo->path << std::endl;
  97. hid_free_enumeration(myHidInfo);
  98. myHidInfo = NULL;
  99. throw std::runtime_error(msg.str());
  100. }
  101. else
  102. {
  103. SCSI2SDHID_handle = myBootloaderHandle;
  104. }
  105. }
  106. Bootloader::~Bootloader()
  107. {
  108. if (myBootloaderHandle)
  109. {
  110. hid_close(myBootloaderHandle);
  111. }
  112. SCSI2SDHID_handle = NULL;
  113. hid_free_enumeration(myHidInfo);
  114. }
  115. Bootloader*
  116. Bootloader::Open()
  117. {
  118. hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
  119. if (dev)
  120. {
  121. return new Bootloader(dev);
  122. }
  123. else
  124. {
  125. return NULL;
  126. }
  127. }
  128. Bootloader::HWInfo
  129. Bootloader::getHWInfo() const
  130. {
  131. HWInfo info = {"unknown", "unknown", "unknown"};
  132. switch (myHidInfo->release_number)
  133. {
  134. case 0x3001:
  135. info.desc = "3.5\" SCSI2SD (green)";
  136. info.version = "V3.0";
  137. info.firmwareName = "SCSI2SD-V3.cyacd";
  138. break;
  139. case 0x3002:
  140. info.desc = "3.5\" SCSI2SD (yellow) or 2.5\" SCSI2SD for Apple Powerbook";
  141. info.version = "V4.1/V4.2";
  142. info.firmwareName = "SCSI2SD-V4.cyacd";
  143. break;
  144. }
  145. return info;
  146. }
  147. bool
  148. Bootloader::isCorrectFirmware(const std::string& path) const
  149. {
  150. HWInfo info = getHWInfo();
  151. return path.rfind(info.firmwareName) != std::string::npos;
  152. }
  153. std::string
  154. Bootloader::getDevicePath() const
  155. {
  156. return myHidInfo->path;
  157. }
  158. std::wstring
  159. Bootloader::getManufacturer() const
  160. {
  161. return myHidInfo->manufacturer_string;
  162. }
  163. std::wstring
  164. Bootloader::getProductString() const
  165. {
  166. return myHidInfo->product_string;
  167. }
  168. void
  169. Bootloader::load(const std::string& path, void (*progress)(uint8_t, uint16_t))
  170. {
  171. int result = CyBtldr_Program(
  172. path.c_str(),
  173. &g_cyComms,
  174. progress);
  175. if (result)
  176. {
  177. throw std::runtime_error("Firmware update failed");
  178. }
  179. }
  180. bool
  181. Bootloader::ping() const
  182. {
  183. try
  184. {
  185. Bootloader::OperationScope operationGuard;
  186. int result = CyBtldr_VerifyRow(0, 0, 0);
  187. switch (result)
  188. {
  189. case CYRET_SUCCESS:
  190. case CYRET_ERR_CHECKSUM: // We supplied a dummy value of 0.
  191. return true;
  192. default: return false;
  193. }
  194. }
  195. catch (std::exception& e)
  196. {
  197. return false;
  198. }
  199. }