WriteExecuteParameters1.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 WriteExecuteParameters1 (struct plc * plc, unsigned module, const struct nvm_header1 * nvm_header);
  44. *
  45. * plc.h
  46. *
  47. * write parameters to SDRAM using VS_WRITE_AND_EXECUTE_APPLET;
  48. *
  49. * we pass in the PIB image header because that is how the caller
  50. * located the PIB image in the file but we must write the entire
  51. * file into SDRAM at the address found in the PIB image header;
  52. * consequenctly, we have to rewind the file position and compute
  53. * the actual file size;
  54. *
  55. * this implementeation will probably never be used because we do
  56. * not distribute parameter files in old image file format; but if
  57. * we then this is it;
  58. *
  59. *
  60. * Contributor(s):
  61. * Charles Maier
  62. *
  63. *--------------------------------------------------------------------*/
  64. #ifndef WRITEEXECUTEPARAMETERS1_SOURCE
  65. #define WRITEEXECUTEPARAMETERS1_SOURCE
  66. #include "../tools/files.h"
  67. #include "../tools/error.h"
  68. #include "../plc/plc.h"
  69. signed WriteExecuteParameters1 (struct plc * plc, unsigned module, const struct nvm_header1 * nvm_header)
  70. {
  71. struct channel * channel = (struct channel *)(plc->channel);
  72. struct message * message = (struct message *)(plc->message);
  73. #ifndef __GNUC__
  74. #pragma pack (push,1)
  75. #endif
  76. struct nvm_header1 nvm_manifest;
  77. struct __packed vs_write_execute_request
  78. {
  79. struct ethernet_hdr ethernet;
  80. struct qualcomm_hdr qualcomm;
  81. uint32_t CLIENT_SESSION_ID;
  82. uint32_t SERVER_SESSION_ID;
  83. uint32_t FLAGS;
  84. uint8_t ALLOWED_MEM_TYPES [8];
  85. uint32_t TOTAL_LENGTH;
  86. uint32_t CURR_PART_LENGTH;
  87. uint32_t CURR_PART_OFFSET;
  88. uint32_t START_ADDR;
  89. uint32_t CHECKSUM;
  90. uint8_t RESERVED2 [8];
  91. uint8_t IMAGE [PLC_MODULE_SIZE];
  92. }
  93. * request = (struct vs_write_execute_request *) (message);
  94. struct __packed vs_write_execute_confirm
  95. {
  96. struct ethernet_hdr ethernet;
  97. struct qualcomm_hdr qualcomm;
  98. uint32_t MSTATUS;
  99. uint32_t CLIENT_SESSION_ID;
  100. uint32_t SERVER_SESSION_ID;
  101. uint32_t FLAGS;
  102. uint8_t ALLOWED_MEM_TYPES [8];
  103. uint32_t TOTAL_LENGTH;
  104. uint32_t CURR_PART_LENGTH;
  105. uint32_t CURR_PART_OFFSET;
  106. uint32_t START_ADDR;
  107. uint32_t CHECKSUM;
  108. uint8_t RESERVED2 [8];
  109. uint32_t CURR_PART_ABSOLUTE_ADDR;
  110. uint32_t ABSOLUTE_START_ADDR;
  111. }
  112. * confirm = (struct vs_write_execute_confirm *) (message);
  113. #ifndef __GNUC__
  114. #pragma pack (pop)
  115. #endif
  116. uint32_t length = PLC_MODULE_SIZE;
  117. uint32_t offset = LE32TOH (nvm_header->IMAGEADDRESS);
  118. uint32_t extent = LE32TOH (nvm_header->IMAGELENGTH);
  119. #if 0
  120. Request (plc, "Write %s (%d) (%08X:%d)", plc->PIB.name, module, offset, extent);
  121. #else
  122. /*
  123. * adjust the file extent to include the the manifest image header, the manifest image, the PIB
  124. * image header and the PIB image; this should in principle, equal the PIB filesize for Panther
  125. * and Lynx .pib files;
  126. */
  127. if (lseek (plc->PIB.file, 0, SEEK_SET))
  128. {
  129. error (1, errno, FILE_CANTHOME, plc->PIB.name);
  130. }
  131. if (read (plc->PIB.file, &nvm_manifest, sizeof (nvm_manifest)) != sizeof (nvm_manifest))
  132. {
  133. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  134. }
  135. extent += sizeof (* nvm_header);
  136. extent += LE32TOH (nvm_manifest.IMAGELENGTH);
  137. extent += sizeof (* nvm_header);
  138. Request (plc, "Write %s (%d) (%08X:%d)", plc->PIB.name, module, offset, extent);
  139. #endif
  140. while (extent)
  141. {
  142. memset (message, 0, sizeof (* message));
  143. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  144. QualcommHeader (&request->qualcomm, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_REQ));
  145. if (length > extent)
  146. {
  147. length = extent;
  148. }
  149. if (read (plc->PIB.file, request->IMAGE, length) != (signed)(length))
  150. {
  151. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  152. }
  153. request->CLIENT_SESSION_ID = HTOLE32 (plc->cookie);
  154. request->SERVER_SESSION_ID = HTOLE32 (0);
  155. request->FLAGS = HTOLE32 (PLC_MODULE_ABSOLUTE);
  156. request->ALLOWED_MEM_TYPES [0] = 1;
  157. request->TOTAL_LENGTH = nvm_header->IMAGELENGTH;
  158. request->CURR_PART_LENGTH = HTOLE32 (length);
  159. request->CURR_PART_OFFSET = HTOLE32 (offset);
  160. request->START_ADDR = nvm_header->ENTRYPOINT;
  161. request->CHECKSUM = nvm_header->IMAGECHECKSUM;
  162. plc->packetsize = sizeof (* request);
  163. if (SendMME (plc) <= 0)
  164. {
  165. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  166. return (-1);
  167. }
  168. do {
  169. if (ReadMME (plc, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_CNF)) <= 0)
  170. {
  171. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  172. return (-1);
  173. }
  174. } while (confirm->CLIENT_SESSION_ID != HTOLE32 (plc->cookie));
  175. if (confirm->MSTATUS)
  176. {
  177. Failure (plc, PLC_WONTDOIT);
  178. return (-1);
  179. }
  180. if (LE32TOH (confirm->CURR_PART_LENGTH) != length)
  181. {
  182. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  183. return (-1);
  184. }
  185. if (LE32TOH (confirm->CURR_PART_OFFSET) != offset)
  186. {
  187. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  188. return (-1);
  189. }
  190. offset += length;
  191. extent -= length;
  192. }
  193. return (0);
  194. }
  195. #endif