WriteExecuteParameters2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 WriteExecuteParameters2 (struct plc * plc, unsigned module, const struct nvm_header2 * 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. *
  56. * Contributor(s):
  57. * Charles Maier
  58. *
  59. *--------------------------------------------------------------------*/
  60. #ifndef WRITEEXECUTEPARAMETERS2_SOURCE
  61. #define WRITEEXECUTEPARAMETERS2_SOURCE
  62. #include "../tools/files.h"
  63. #include "../tools/error.h"
  64. #include "../plc/plc.h"
  65. signed WriteExecuteParameters2 (struct plc * plc, unsigned module, const struct nvm_header2 * nvm_header)
  66. {
  67. struct channel * channel = (struct channel *)(plc->channel);
  68. struct message * message = (struct message *)(plc->message);
  69. #ifndef __GNUC__
  70. #pragma pack (push,1)
  71. #endif
  72. struct nvm_header2 tmp_header;
  73. struct __packed vs_write_execute_request
  74. {
  75. struct ethernet_hdr ethernet;
  76. struct qualcomm_hdr qualcomm;
  77. uint32_t CLIENT_SESSION_ID;
  78. uint32_t SERVER_SESSION_ID;
  79. uint32_t FLAGS;
  80. uint8_t ALLOWED_MEM_TYPES [8];
  81. uint32_t TOTAL_LENGTH;
  82. uint32_t CURR_PART_LENGTH;
  83. uint32_t CURR_PART_OFFSET;
  84. uint32_t START_ADDR;
  85. uint32_t CHECKSUM;
  86. uint8_t RESERVED2 [8];
  87. uint8_t IMAGE [PLC_MODULE_SIZE];
  88. }
  89. * request = (struct vs_write_execute_request *) (message);
  90. struct __packed vs_write_execute_confirm
  91. {
  92. struct ethernet_hdr ethernet;
  93. struct qualcomm_hdr qualcomm;
  94. uint32_t MSTATUS;
  95. uint32_t CLIENT_SESSION_ID;
  96. uint32_t SERVER_SESSION_ID;
  97. uint32_t FLAGS;
  98. uint8_t ALLOWED_MEM_TYPES [8];
  99. uint32_t TOTAL_LENGTH;
  100. uint32_t CURR_PART_LENGTH;
  101. uint32_t CURR_PART_OFFSET;
  102. uint32_t START_ADDR;
  103. uint32_t CHECKSUM;
  104. uint8_t RESERVED2 [8];
  105. uint32_t CURR_PART_ABSOLUTE_ADDR;
  106. uint32_t ABSOLUTE_START_ADDR;
  107. }
  108. * confirm = (struct vs_write_execute_confirm *) (message);
  109. #ifndef __GNUC__
  110. #pragma pack (pop)
  111. #endif
  112. uint32_t length = PLC_MODULE_SIZE;
  113. uint32_t offset = LE32TOH (nvm_header->ImageAddress);
  114. uint32_t extent = LE32TOH (nvm_header->ImageLength);
  115. #if 1
  116. /*
  117. * adjust the file extent to include the the manifest image header, the manifest image, the PIB
  118. * image header and the PIB image; this should in principle, equal the PIB filesize for Panther
  119. * and Lynx .pib files;
  120. */
  121. if (lseek (plc->PIB.file, 0, SEEK_SET))
  122. {
  123. error (1, errno, FILE_CANTHOME, plc->PIB.name);
  124. }
  125. if (read (plc->PIB.file, &tmp_header, sizeof (tmp_header)) != sizeof (tmp_header))
  126. {
  127. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  128. }
  129. extent += sizeof (tmp_header);
  130. extent += LE32TOH (tmp_header.ImageLength);
  131. extent += sizeof (* nvm_header);
  132. if (lseek (plc->PIB.file, 0, SEEK_SET))
  133. {
  134. error (1, errno, FILE_CANTHOME, plc->PIB.name);
  135. }
  136. #endif
  137. Request (plc, "Write %s (%d) (%08X:%d)", plc->PIB.name, module, offset, extent);
  138. while (extent)
  139. {
  140. memset (message, 0, sizeof (* message));
  141. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  142. QualcommHeader (&request->qualcomm, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_REQ));
  143. if (length > extent)
  144. {
  145. length = extent;
  146. }
  147. if (read (plc->PIB.file, request->IMAGE, length) != (signed)(length))
  148. {
  149. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  150. }
  151. request->CLIENT_SESSION_ID = HTOLE32 (plc->cookie);
  152. request->SERVER_SESSION_ID = HTOLE32 (0);
  153. request->FLAGS = HTOLE32 (PLC_MODULE_ABSOLUTE);
  154. request->ALLOWED_MEM_TYPES [0] = 0;
  155. request->TOTAL_LENGTH = nvm_header->ImageLength;
  156. request->CURR_PART_LENGTH = HTOLE32 (length);
  157. request->CURR_PART_OFFSET = HTOLE32 (offset);
  158. request->START_ADDR = nvm_header->EntryPoint;
  159. request->CHECKSUM = nvm_header->ImageChecksum;
  160. plc->packetsize = sizeof (* request);
  161. if (SendMME (plc) <= 0)
  162. {
  163. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  164. return (-1);
  165. }
  166. do {
  167. if (ReadMME (plc, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_CNF)) <= 0)
  168. {
  169. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  170. return (-1);
  171. }
  172. } while (confirm->CLIENT_SESSION_ID != HTOLE32 (plc->cookie));
  173. if (confirm->MSTATUS)
  174. {
  175. Failure (plc, PLC_WONTDOIT);
  176. return (-1);
  177. }
  178. if (LE32TOH (confirm->CURR_PART_LENGTH) != length)
  179. {
  180. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  181. return (-1);
  182. }
  183. if (LE32TOH (confirm->CURR_PART_OFFSET) != offset)
  184. {
  185. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  186. return (-1);
  187. }
  188. offset += length;
  189. extent -= length;
  190. }
  191. return (0);
  192. }
  193. #endif