SCSI2SD_Bootloader.cc 3.8 KB

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