Firmware.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "Firmware.hh"
  18. extern "C"
  19. {
  20. #include "cybtldr_parse.h"
  21. }
  22. #include <functional>
  23. #include <sstream>
  24. #include <stdexcept>
  25. #include <string.h>
  26. using namespace SCSI2SD;
  27. namespace
  28. {
  29. struct FirmwareFile
  30. {
  31. ~FirmwareFile()
  32. {
  33. CyBtldr_CloseDataFile();
  34. }
  35. };
  36. }
  37. Firmware::Firmware(const std::string& path)
  38. {
  39. if (CyBtldr_OpenDataFile(path.c_str()) != CYRET_SUCCESS)
  40. {
  41. std::stringstream msg;
  42. msg << "Could not open file: " << path;
  43. throw std::runtime_error(msg.str());
  44. }
  45. FirmwareFile closeOnReturn;
  46. uint8_t buffer[MAX_BUFFER_SIZE];
  47. unsigned int lineSize;
  48. if (
  49. CyBtldr_ReadLine(&lineSize, reinterpret_cast<char*>(buffer))
  50. != CYRET_SUCCESS
  51. )
  52. {
  53. std::stringstream msg;
  54. msg << "Could not read file: " << path;
  55. throw std::runtime_error(msg.str());
  56. }
  57. {
  58. unsigned long silId;
  59. unsigned char silRev;
  60. unsigned char chksum[MAX_BUFFER_SIZE];
  61. if (
  62. CyBtldr_ParseHeader(
  63. lineSize,
  64. buffer,
  65. &silId,
  66. &silRev,
  67. chksum)
  68. != CYRET_SUCCESS)
  69. {
  70. std::stringstream msg;
  71. msg << "Premature end of file: " << path;
  72. throw std::runtime_error(msg.str());
  73. }
  74. mySiliconId = silId;
  75. mySiliconRev = static_cast<int>(silRev);
  76. }
  77. // Count the number of flash rows. This is used to show "percentage
  78. // complete" when uploading the firmware.
  79. myTotalFlashRows = 0;
  80. while (
  81. CyBtldr_ReadLine(&lineSize, reinterpret_cast<char*>(buffer))
  82. == CYRET_SUCCESS
  83. )
  84. {
  85. myTotalFlashRows++;
  86. }
  87. }
  88. Firmware::~Firmware()
  89. {
  90. }