main.cc 4.0 KB

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