WriteExecutePIB.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 WriteExecutePIB (struct plc * plc, uint32_t offset, struct pib_header * header);
  44. *
  45. * plc.h
  46. *
  47. * write parameters to SDRAM using VS_WRITE_AND_EXECUTE_APPLET; the memory
  48. * offset depends on the firmware release;
  49. *
  50. * plc->PIB must be positioned to the start of the PIB file or this
  51. * function will not work;
  52. *
  53. * pass in the PIB header since it contains the PIB file length and
  54. * checksum needed by the VS_WRITE_AND_EXECUTE_APPLET request;
  55. *
  56. *
  57. * Contributor(s):
  58. * Charles Maier
  59. *
  60. *--------------------------------------------------------------------*/
  61. #ifndef WRITEEXECUTEPIB_SOURCE
  62. #define WRITEEXECUTEPIB_SOURCE
  63. #include "../tools/files.h"
  64. #include "../tools/error.h"
  65. #include "../plc/plc.h"
  66. signed WriteExecutePIB (struct plc * plc, uint32_t offset, struct pib_header * header)
  67. {
  68. struct channel * channel = (struct channel *)(plc->channel);
  69. struct message * message = (struct message *)(plc->message);
  70. #ifndef __GNUC__
  71. #pragma pack (push,1)
  72. #endif
  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 extent = LE16TOH (header->PIBLENGTH);
  114. Request (plc, "Write %s (0) (%08X:%d)", plc->PIB.name, offset, extent);
  115. while (extent)
  116. {
  117. memset (message, 0, sizeof (* message));
  118. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  119. QualcommHeader (&request->qualcomm, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_REQ));
  120. if (length > extent)
  121. {
  122. length = extent;
  123. }
  124. if (read (plc->PIB.file, request->IMAGE, length) != (signed)(length))
  125. {
  126. error (1, errno, FILE_CANTREAD, plc->PIB.name);
  127. }
  128. request->CLIENT_SESSION_ID = HTOLE32 (plc->cookie);
  129. request->SERVER_SESSION_ID = HTOLE32 (0);
  130. request->FLAGS = HTOLE32 (PLC_MODULE_ABSOLUTE);
  131. request->ALLOWED_MEM_TYPES [0] = 1;
  132. request->TOTAL_LENGTH = header->PIBLENGTH;
  133. request->CURR_PART_LENGTH = HTOLE32 (length);
  134. request->CURR_PART_OFFSET = HTOLE32 (offset);
  135. request->START_ADDR = HTOLE32 (0);
  136. request->CHECKSUM = header->CHECKSUM;
  137. plc->packetsize = sizeof (* request);
  138. if (SendMME (plc) <= 0)
  139. {
  140. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  141. return (-1);
  142. }
  143. do {
  144. if (ReadMME (plc, 0, (VS_WRITE_AND_EXECUTE_APPLET | MMTYPE_CNF)) <= 0)
  145. {
  146. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  147. return (-1);
  148. }
  149. } while (confirm->CLIENT_SESSION_ID != HTOLE32 (plc->cookie));
  150. if (confirm->MSTATUS)
  151. {
  152. Failure (plc, PLC_WONTDOIT);
  153. return (-1);
  154. }
  155. if (LE32TOH (confirm->CURR_PART_LENGTH) != length)
  156. {
  157. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  158. return (-1);
  159. }
  160. if (LE32TOH (confirm->CURR_PART_OFFSET) != offset)
  161. {
  162. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  163. return (-1);
  164. }
  165. offset += length;
  166. extent -= length;
  167. }
  168. return (0);
  169. }
  170. #endif