ReadFirmware1.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed ReadFirmware1 (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * Read the firmware image from the device using as many VS_RD_MOD
  48. * messages as needed; Write image blocks to file as as they are
  49. * read;
  50. *
  51. * the object here here is to read NVM from RAM in 1024 byte blocks
  52. * until MOFFSET exceeds the image length;
  53. *
  54. *
  55. * Contributor(s):
  56. * Charles Maier
  57. * Nathaniel Houghton
  58. *
  59. *--------------------------------------------------------------------*/
  60. #ifndef READFIRMWARE1_SOURCE
  61. #define READFIRMWARE1_SOURCE
  62. #include <stdint.h>
  63. #include <unistd.h>
  64. #include <memory.h>
  65. #include "../plc/plc.h"
  66. #include "../tools/error.h"
  67. #include "../tools/files.h"
  68. #include "../tools/endian.h"
  69. #include "../tools/memory.h"
  70. #include "../nvm/nvm.h"
  71. signed ReadFirmware1 (struct plc * plc)
  72. {
  73. struct channel * channel = (struct channel *)(plc->channel);
  74. struct message * message = (struct message *)(plc->message);
  75. #ifndef __GNUC__
  76. #pragma pack (push,1)
  77. #endif
  78. struct __packed vs_rd_mod_request
  79. {
  80. struct ethernet_hdr ethernet;
  81. struct qualcomm_hdr qualcomm;
  82. uint8_t MODULEID;
  83. uint8_t MACCESS;
  84. uint16_t MLENGTH;
  85. uint32_t MOFFSET;
  86. uint8_t MSECRET [16];
  87. }
  88. * request = (struct vs_rd_mod_request *) (message);
  89. struct __packed vs_rd_mod_confirm
  90. {
  91. struct ethernet_hdr ethernet;
  92. struct qualcomm_hdr qualcomm;
  93. uint8_t MSTATUS;
  94. uint8_t RES [3];
  95. uint8_t MODULEID;
  96. uint8_t RESERVED;
  97. uint16_t MLENGTH;
  98. uint32_t MOFFSET;
  99. uint32_t CHKSUM;
  100. uint8_t BUFFER [PLC_RECORD_SIZE];
  101. }
  102. * confirm = (struct vs_rd_mod_confirm *) (message);
  103. #ifndef __GNUC__
  104. #pragma pack (pop)
  105. #endif
  106. uint32_t header = 0;
  107. uint32_t extent = 0;
  108. uint32_t offset = 0;
  109. uint16_t length = PLC_RECORD_SIZE;
  110. Request (plc, "Read Firmware from Device");
  111. if (lseek (plc->nvm.file, 0, SEEK_SET))
  112. {
  113. error (PLC_EXIT (plc), errno, FILE_CANTHOME, plc->nvm.name);
  114. return (1);
  115. }
  116. do
  117. {
  118. memset (message, 0, sizeof (* message));
  119. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  120. QualcommHeader (&request->qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  121. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  122. request->MODULEID = VS_MODULE_MAC;
  123. request->MLENGTH = HTOLE16 (length);
  124. request->MOFFSET = HTOLE32 (offset);
  125. if (SendMME (plc) <= 0)
  126. {
  127. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  128. return (-1);
  129. }
  130. if (ReadMME (plc, 0, (VS_RD_MOD | MMTYPE_CNF)) <= 0)
  131. {
  132. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  133. return (-1);
  134. }
  135. if (confirm->MSTATUS)
  136. {
  137. Failure (plc, PLC_WONTDOIT);
  138. return (-1);
  139. }
  140. if (LE16TOH (confirm->MLENGTH) != length)
  141. {
  142. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  143. return (-1);
  144. }
  145. if (LE32TOH (confirm->MOFFSET) != offset)
  146. {
  147. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  148. return (-1);
  149. }
  150. length = LE16TOH (confirm->MLENGTH);
  151. offset = LE32TOH (confirm->MOFFSET);
  152. if (checksum32 (confirm->BUFFER, length, confirm->CHKSUM))
  153. {
  154. error (PLC_EXIT (plc), ECANCELED, "Bad Packet Checksum");
  155. return (-1);
  156. }
  157. if (offset == extent)
  158. {
  159. struct nvm_header1 * nvm_header = (struct nvm_header1 *)(confirm->BUFFER);
  160. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  161. {
  162. error (PLC_EXIT (plc), ECANCELED, "Bad Header Checksum");
  163. return (-1);
  164. }
  165. if (LE32TOH (nvm_header->HEADERVERSION) != 0x60000000)
  166. {
  167. error (PLC_EXIT (plc), ECANCELED, "Bad Header Version");
  168. return (-1);
  169. }
  170. extent += sizeof (* nvm_header);
  171. extent += LE32TOH (nvm_header->IMAGELENGTH);
  172. header = LE32TOH (nvm_header->NEXTHEADER);
  173. }
  174. if ((offset + length) > extent)
  175. {
  176. length = extent - offset;
  177. }
  178. if (lseek (plc->nvm.file, offset, SEEK_SET) != (off_t)(offset))
  179. {
  180. error (PLC_EXIT (plc), errno, FILE_CANTSEEK, plc->nvm.name);
  181. return (-1);
  182. }
  183. if (write (plc->nvm.file, confirm->BUFFER, length) != (signed)(length))
  184. {
  185. error (PLC_EXIT (plc), errno, FILE_CANTSEEK, plc->nvm.name);
  186. return (-1);
  187. }
  188. offset += length;
  189. length = 1024;
  190. }
  191. while ((header) || (offset < extent));
  192. Confirm (plc, "Read %s", plc->nvm.name);
  193. return (0);
  194. }
  195. #endif