ReadNVM.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ReadNVM (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * Read MAC Software 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. * MOFFSET and MLENGTH fields occupy different offsets in REQ and
  55. * CNF messages; we exploit that by initializing CNF message and
  56. * copying them into the REQ message before each read;
  57. *
  58. *
  59. * Contributor(s):
  60. * Charles Maier
  61. * Nathaniel Houghton
  62. *
  63. *--------------------------------------------------------------------*/
  64. #ifndef READNVM_SOURCE
  65. #define READNVM_SOURCE
  66. #include <stdint.h>
  67. #include <unistd.h>
  68. #include <memory.h>
  69. #include "../plc/plc.h"
  70. #include "../tools/error.h"
  71. #include "../tools/files.h"
  72. #include "../tools/endian.h"
  73. #include "../tools/memory.h"
  74. #include "../nvm/nvm.h"
  75. signed ReadNVM (struct plc * plc)
  76. {
  77. struct channel * channel = (struct channel *)(plc->channel);
  78. struct message * message = (struct message *)(plc->message);
  79. #ifndef __GNUC__
  80. #pragma pack (push,1)
  81. #endif
  82. struct __packed vs_rd_mod_request
  83. {
  84. struct ethernet_hdr ethernet;
  85. struct qualcomm_hdr qualcomm;
  86. uint8_t MODULEID;
  87. uint8_t MACCESS;
  88. uint16_t MLENGTH;
  89. uint32_t MOFFSET;
  90. uint8_t MSECRET [16];
  91. }
  92. * request = (struct vs_rd_mod_request *) (message);
  93. struct __packed vs_rd_mod_confirm
  94. {
  95. struct ethernet_hdr ethernet;
  96. struct qualcomm_hdr qualcomm;
  97. uint8_t MSTATUS;
  98. uint8_t RES [3];
  99. uint8_t MODULEID;
  100. uint8_t RESERVED;
  101. uint16_t MLENGTH;
  102. uint32_t MOFFSET;
  103. uint32_t CHKSUM;
  104. uint8_t BUFFER [PLC_RECORD_SIZE];
  105. }
  106. * confirm = (struct vs_rd_mod_confirm *) (message);
  107. #ifndef __GNUC__
  108. #pragma pack (pop)
  109. #endif
  110. uint32_t header = 0;
  111. uint32_t extent = 0;
  112. uint32_t offset = 0;
  113. uint16_t length = PLC_RECORD_SIZE;
  114. Request (plc, "Read Firmware from Device");
  115. if (lseek (plc->nvm.file, 0, SEEK_SET))
  116. {
  117. error (PLC_EXIT (plc), errno, FILE_CANTHOME, plc->nvm.name);
  118. return (1);
  119. }
  120. do
  121. {
  122. memset (message, 0, sizeof (* message));
  123. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  124. QualcommHeader (&request->qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  125. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  126. request->MODULEID = VS_MODULE_MAC;
  127. request->MLENGTH = HTOLE16 (length);
  128. request->MOFFSET = HTOLE32 (offset);
  129. if (SendMME (plc) <= 0)
  130. {
  131. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  132. return (-1);
  133. }
  134. if (ReadMME (plc, 0, (VS_RD_MOD | MMTYPE_CNF)) <= 0)
  135. {
  136. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  137. return (-1);
  138. }
  139. if (confirm->MSTATUS)
  140. {
  141. Failure (plc, PLC_WONTDOIT);
  142. return (-1);
  143. }
  144. if (LE16TOH (confirm->MLENGTH) != length)
  145. {
  146. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  147. return (-1);
  148. }
  149. if (LE32TOH (confirm->MOFFSET) != offset)
  150. {
  151. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  152. return (-1);
  153. }
  154. length = LE16TOH (confirm->MLENGTH);
  155. offset = LE32TOH (confirm->MOFFSET);
  156. if (checksum32 (confirm->BUFFER, length, confirm->CHKSUM))
  157. {
  158. error (PLC_EXIT (plc), ECANCELED, "Bad Packet Checksum");
  159. return (-1);
  160. }
  161. if (offset == extent)
  162. {
  163. struct nvm_header1 * nvm_header = (struct nvm_header1 *)(confirm->BUFFER);
  164. if (checksum32 (nvm_header, sizeof (* nvm_header), 0))
  165. {
  166. error (PLC_EXIT (plc), ECANCELED, "Bad Header Checksum");
  167. return (-1);
  168. }
  169. if (LE32TOH (nvm_header->HEADERVERSION) != 0x60000000)
  170. {
  171. error (PLC_EXIT (plc), ECANCELED, "Bad Header Version");
  172. return (-1);
  173. }
  174. extent += sizeof (* nvm_header);
  175. extent += LE32TOH (nvm_header->IMAGELENGTH);
  176. header = LE32TOH (nvm_header->NEXTHEADER);
  177. }
  178. if ((offset + length) > extent)
  179. {
  180. length = extent - offset;
  181. }
  182. if (lseek (plc->nvm.file, offset, SEEK_SET) != (off_t)(offset))
  183. {
  184. error (PLC_EXIT (plc), errno, FILE_CANTSEEK, plc->nvm.name);
  185. return (-1);
  186. }
  187. if (write (plc->nvm.file, confirm->BUFFER, length) != (signed)(length))
  188. {
  189. error (PLC_EXIT (plc), errno, FILE_CANTSAVE, plc->nvm.name);
  190. return (-1);
  191. }
  192. offset += length;
  193. length = 1024;
  194. }
  195. while ((header) || (offset < extent));
  196. Confirm (plc, "Read %s", plc->nvm.name);
  197. return (0);
  198. }
  199. #endif