SCSI2SD_Bootloader.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. extern "C"
  22. {
  23. #include "hidapi.h"
  24. #include "cybtldr_api.h"
  25. #include "cybtldr_api2.h"
  26. }
  27. #include <string.h>
  28. using namespace SCSI2SD;
  29. hid_device* SCSI2SDHID_handle = NULL;
  30. // cybtldr interface.
  31. extern "C" int
  32. SCSI2SDHID_OpenConnection(void)
  33. {
  34. return 0;
  35. }
  36. extern "C" int
  37. SCSI2SDHID_CloseConnection(void)
  38. {
  39. return 0;
  40. }
  41. extern "C" int
  42. SCSI2SDHID_ReadData(unsigned char* data, int count)
  43. {
  44. uint8_t buf[65];
  45. buf[0] = 0; // Report ID
  46. int result = hid_read(SCSI2SDHID_handle, buf, count);
  47. if (result < 0)
  48. {
  49. std::cerr << "USB HID Read Failure: " <<
  50. hid_error(SCSI2SDHID_handle) << std::endl;
  51. }
  52. memcpy(data, buf, count);
  53. return (result >= 0) ? 0 : -1;
  54. }
  55. extern "C" int
  56. SCSI2SDHID_WriteData(unsigned char* data, int count)
  57. {
  58. uint8_t buf[65];
  59. buf[0] = 0; // report ID
  60. int i;
  61. for (i = 0; i < count; ++i)
  62. {
  63. buf[i+1] = data[i];
  64. }
  65. int result = hid_write(SCSI2SDHID_handle, buf, count + 1);
  66. if (result < 0)
  67. {
  68. std::cerr << "USB HID Write Failure: " <<
  69. hid_error(SCSI2SDHID_handle) << std::endl;
  70. }
  71. return (result >= 0) ? 0 : -1;
  72. }
  73. Bootloader::Bootloader(hid_device_info* hidInfo) :
  74. myHidInfo(hidInfo),
  75. myBootloaderHandle(NULL)
  76. {
  77. myBootloaderHandle = hid_open_path(hidInfo->path);
  78. if (!myBootloaderHandle)
  79. {
  80. hid_free_enumeration(myHidInfo);
  81. myHidInfo = NULL;
  82. std::stringstream msg;
  83. msg << "Error opening HID device " << hidInfo->path << std::endl;
  84. throw std::runtime_error(msg.str());
  85. }
  86. }
  87. Bootloader::~Bootloader()
  88. {
  89. if (myBootloaderHandle)
  90. {
  91. hid_close(myBootloaderHandle);
  92. }
  93. hid_free_enumeration(myHidInfo);
  94. }
  95. Bootloader*
  96. Bootloader::Open()
  97. {
  98. hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
  99. if (dev)
  100. {
  101. return new Bootloader(dev);
  102. }
  103. else
  104. {
  105. return NULL;
  106. }
  107. }
  108. Bootloader::HWInfo
  109. Bootloader::getHWInfo() const
  110. {
  111. HWInfo info = {"unknown", "unknown", "unknown"};
  112. switch (myHidInfo->release_number)
  113. {
  114. case 0x3001:
  115. info.desc = "3.5\" SCSI2SD (green)";
  116. info.version = "V3.0";
  117. info.firmwareName = "SCSI2SD-V3.cyacd";
  118. break;
  119. case 0x3002:
  120. info.desc = "3.5\" SCSI2SD (yellow) or 2.5\" SCSI2SD for Apple Powerbook";
  121. info.version = "V4.1/V4.2";
  122. info.firmwareName = "SCSI2SD-V4.cyacd";
  123. break;
  124. }
  125. return info;
  126. }
  127. bool
  128. Bootloader::isCorrectFirmware(const std::string& path) const
  129. {
  130. HWInfo info = getHWInfo();
  131. return path.rfind(info.firmwareName) != std::string::npos;
  132. }
  133. std::string
  134. Bootloader::getDevicePath() const
  135. {
  136. return myHidInfo->path;
  137. }
  138. std::wstring
  139. Bootloader::getManufacturer() const
  140. {
  141. return myHidInfo->manufacturer_string;
  142. }
  143. std::wstring
  144. Bootloader::getProductString() const
  145. {
  146. return myHidInfo->product_string;
  147. }
  148. void
  149. Bootloader::load(const std::string& path, void (*progress)(uint8_t, uint16_t))
  150. {
  151. CyBtldr_CommunicationsData cyComms =
  152. {
  153. &SCSI2SDHID_OpenConnection,
  154. &SCSI2SDHID_CloseConnection,
  155. &SCSI2SDHID_ReadData,
  156. &SCSI2SDHID_WriteData,
  157. HID_PACKET_SIZE
  158. };
  159. SCSI2SDHID_handle = myBootloaderHandle;
  160. int result = CyBtldr_Program(
  161. path.c_str(),
  162. &cyComms,
  163. progress);
  164. if (result)
  165. {
  166. throw std::runtime_error("Firmware update failed");
  167. }
  168. }