main.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (C) 2013 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_HID.hh"
  18. #include "SCSI2SD_Bootloader.hh"
  19. #include "Firmware.hh"
  20. #if __cplusplus >= 201103L
  21. #include <cstdint>
  22. #include <memory>
  23. using std::shared_ptr;
  24. #else
  25. #include <stdint.h>
  26. #include <tr1/memory>
  27. using std::tr1::shared_ptr;
  28. #endif
  29. #include <iomanip>
  30. #include <iostream>
  31. #include <sstream>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. using namespace SCSI2SD;
  36. extern "C"
  37. void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
  38. {
  39. //std::cerr <<
  40. //"Programmed flash array " << static_cast<int>(arrayId) <<
  41. // ", row " << rowNum << std::endl;
  42. std::cout << "." << std::flush;
  43. }
  44. static void usage()
  45. {
  46. std::cout << "Usage: bootloaderhost [-f] "
  47. "/path/to/firmware.cyacd\n" <<
  48. "\t-f\tForce, even if the firmware doesn't match the target board.\n\n" <<
  49. std::endl;
  50. }
  51. int main(int argc, char* argv[])
  52. {
  53. std::cout <<
  54. "PSoC 3/5LP USB HID Bootloader Host\n" <<
  55. "Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n" <<
  56. std::endl;
  57. int force = 0;
  58. opterr = 0;
  59. int c;
  60. while ((c = getopt(argc, argv, "v:p:f")) != -1)
  61. {
  62. switch (c)
  63. {
  64. case 'f':
  65. force = 1;
  66. break;
  67. case '?':
  68. usage();
  69. exit(1);
  70. }
  71. }
  72. std::string filename;
  73. if (optind < argc)
  74. {
  75. filename = argv[optind];
  76. }
  77. else
  78. {
  79. usage();
  80. exit(1);
  81. }
  82. // Enumerate and print the HID devices on the system
  83. shared_ptr<Bootloader> bootloader(Bootloader::Open());
  84. shared_ptr<HID> hid(HID::Open());
  85. if (hid)
  86. {
  87. try
  88. {
  89. hid->enterBootloader();
  90. }
  91. catch (std::exception& e)
  92. {
  93. std::cerr << e.what() << std::endl;
  94. hid.reset();
  95. }
  96. }
  97. if (!hid)
  98. {
  99. std::cout <<
  100. "Waiting for device connection" << std::endl <<
  101. "Connect USB cable to the bus-powered device now, or otherwise "
  102. "reset the device." << std::endl;
  103. }
  104. while (!bootloader)
  105. {
  106. bootloader.reset(Bootloader::Open());
  107. if (!bootloader && !hid)
  108. {
  109. hid.reset(HID::Open());
  110. if (hid)
  111. {
  112. try
  113. {
  114. hid->enterBootloader();
  115. }
  116. catch (std::exception& e)
  117. {
  118. std::cerr << e.what();
  119. }
  120. }
  121. }
  122. if (!bootloader)
  123. {
  124. usleep(100000); // 100ms
  125. }
  126. }
  127. std::stringstream foundMsg;
  128. foundMsg <<
  129. "Device Found\n" <<
  130. " type:\t\t\t" << std::setw(4) << std::hex <<
  131. Bootloader::VENDOR_ID << " " <<
  132. Bootloader::PRODUCT_ID <<
  133. "\n" <<
  134. " path:\t\t\t" << bootloader->getDevicePath() << "\n";
  135. std::cout << foundMsg.str() << std::endl;
  136. Bootloader::HWInfo hwInfo(bootloader->getHWInfo());
  137. std::cout <<
  138. " Board:\t\t" << hwInfo.desc << "\n" <<
  139. " Revision:\t\t" << hwInfo.version << std::endl;
  140. if (hid)
  141. {
  142. std::cout <<
  143. " Existing firmware:\t" <<
  144. hid->getFirmwareVersionStr() << std::endl;
  145. }
  146. if (!bootloader->isCorrectFirmware(filename) && !force)
  147. {
  148. std::cerr <<
  149. "ERROR: Unexpected firmware file. Expected: \""
  150. << hwInfo.firmwareName << "\"\n\n" <<
  151. "Using firmware design for a different board will destroy your " <<
  152. "hardware.\n" <<
  153. "If you still wish to proceed, try again with the \"-f\" flag.\n" <<
  154. std::endl;
  155. exit(1);
  156. }
  157. Firmware firmware(filename);
  158. std::stringstream firmMsg;
  159. firmMsg <<
  160. " Firmware Silicon ID:\t" << std::hex << firmware.siliconId() <<
  161. "\n";
  162. std::cout << firmMsg.str() << std::endl;
  163. std::cout << "Starting firmware upload: " << filename << std::endl;
  164. try
  165. {
  166. bootloader->load(filename, &ProgressUpdate);
  167. std::cout << "Firmware upload complete." << std::endl;
  168. }
  169. catch (std::exception& e)
  170. {
  171. std::cerr << "ERROR: Firmware update failed.\n" << e.what() << std::endl;
  172. exit(1);
  173. }
  174. return 0;
  175. }