ModuleWrite.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 ModuleWrite (struct plc * plc, struct _file_ * file, unsigned index, struct vs_module_spec * vs_module_spec);
  44. *
  45. * plc.h
  46. *
  47. * write an entire file to flash memory as a single module; write
  48. * the file in 1400 byte records; allow up to 80 seconds for each
  49. * write to complete; extra time is needed because memory must be
  50. * erased;
  51. *
  52. *
  53. * Contributor(s):
  54. * Charles Maier
  55. *
  56. *--------------------------------------------------------------------*/
  57. #ifndef MODULEWRITE_SOURCE
  58. #define MODULEWRITE_SOURCE
  59. #include <stdint.h>
  60. #include <limits.h>
  61. #include <unistd.h>
  62. #include <string.h>
  63. #include <memory.h>
  64. #include "../tools/error.h"
  65. #include "../tools/files.h"
  66. #include "../plc/plc.h"
  67. signed ModuleWrite (struct plc * plc, struct _file_ * file, unsigned index, struct vs_module_spec * vs_module_spec)
  68. {
  69. struct channel * channel = (struct channel *)(plc->channel);
  70. struct message * message = (struct message *)(plc->message);
  71. #ifndef __GNUC__
  72. #pragma pack (push,1)
  73. #endif
  74. struct __packed vs_module_operation_write_request
  75. {
  76. struct ethernet_hdr ethernet;
  77. struct qualcomm_hdr qualcomm;
  78. uint32_t RESERVED;
  79. uint8_t NUM_OP_DATA;
  80. struct __packed
  81. {
  82. uint16_t MOD_OP;
  83. uint16_t MOD_OP_DATA_LEN;
  84. uint32_t MOD_OP_RSVD;
  85. uint32_t MOD_OP_SESSION_ID;
  86. uint8_t MODULE_IDX;
  87. uint16_t MODULE_ID;
  88. uint16_t MODULE_SUB_ID;
  89. uint16_t MODULE_LENGTH;
  90. uint32_t MODULE_OFFSET;
  91. }
  92. MODULE_SPEC;
  93. uint8_t MODULE_DATA [PLC_MODULE_SIZE];
  94. }
  95. * request = (struct vs_module_operation_write_request *)(message);
  96. struct __packed vs_module_operation_write_confirm
  97. {
  98. struct ethernet_hdr ethernet;
  99. struct qualcomm_hdr qualcomm;
  100. uint16_t MSTATUS;
  101. uint16_t ERR_REC_CODE;
  102. uint32_t RESERVED;
  103. uint8_t NUM_OP_DATA;
  104. struct __packed
  105. {
  106. uint16_t MOD_OP;
  107. uint16_t MOD_OP_DATA_LEN;
  108. uint32_t MOD_OP_RSVD;
  109. uint32_t MOD_OP_SESSION_ID;
  110. uint8_t MODULE_IDX;
  111. uint16_t MODULE_ID;
  112. uint16_t MODULE_SUB_ID;
  113. uint16_t MODULE_LENGTH;
  114. uint32_t MODULE_OFFSET;
  115. }
  116. MODULE_SPEC;
  117. }
  118. * confirm = (struct vs_module_operation_write_confirm *)(message);
  119. #ifndef __GNUC__
  120. #pragma pack (pop)
  121. #endif
  122. unsigned timer = channel->timeout;
  123. uint16_t length = PLC_MODULE_SIZE;
  124. uint32_t extent = vs_module_spec->MODULE_LENGTH;
  125. uint32_t offset = 0;
  126. Request (plc, "Flash %s", file->name);
  127. while (extent)
  128. {
  129. memset (message, 0, sizeof (* message));
  130. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  131. QualcommHeader (&request->qualcomm, 0, (VS_MODULE_OPERATION | MMTYPE_REQ));
  132. plc->packetsize = sizeof (struct vs_module_operation_write_request);
  133. if (length > extent)
  134. {
  135. length = extent;
  136. }
  137. if (read (file->file, request->MODULE_DATA, length) != length)
  138. {
  139. error (1, errno, FILE_CANTREAD, file->name);
  140. }
  141. request->NUM_OP_DATA = 1;
  142. request->MODULE_SPEC.MOD_OP = HTOLE16 (PLC_MOD_OP_WRITE_MODULE);
  143. request->MODULE_SPEC.MOD_OP_DATA_LEN = HTOLE16 (sizeof (request->MODULE_SPEC) + sizeof (request->MODULE_DATA));
  144. request->MODULE_SPEC.MOD_OP_SESSION_ID = HTOLE32 (plc->cookie);
  145. request->MODULE_SPEC.MODULE_IDX = index;
  146. request->MODULE_SPEC.MODULE_ID = HTOLE16 (vs_module_spec->MODULE_ID);
  147. request->MODULE_SPEC.MODULE_SUB_ID = HTOLE16 (vs_module_spec->MODULE_SUB_ID);
  148. request->MODULE_SPEC.MODULE_LENGTH = HTOLE16 (length);
  149. request->MODULE_SPEC.MODULE_OFFSET = HTOLE32 (offset);
  150. #if 1
  151. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (request->RESERVED));
  152. fprintf (stderr, "NUM_OP_DATA %d\n", request->NUM_OP_DATA);
  153. fprintf (stderr, "MOD_OP 0x%02X\n", LE16TOH (request->MODULE_SPEC.MOD_OP));
  154. fprintf (stderr, "MOD_OP_DATA_LEN %d\n", LE16TOH (request->MODULE_SPEC.MOD_OP_DATA_LEN));
  155. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (request->MODULE_SPEC.MOD_OP_RSVD));
  156. fprintf (stderr, "MODULE_ID 0x%04X\n", LE16TOH (request->MODULE_SPEC.MODULE_ID));
  157. fprintf (stderr, "MODULE_SUB_ID 0x%04X\n", LE16TOH (request->MODULE_SPEC.MODULE_SUB_ID));
  158. fprintf (stderr, "MODULE_LENGTH %d\n", LE16TOH (request->MODULE_SPEC.MODULE_LENGTH));
  159. fprintf (stderr, "MODULE_OFFSET 0x%08X\n", LE32TOH (request->MODULE_SPEC.MODULE_OFFSET));
  160. #endif
  161. if (SendMME (plc) <= 0)
  162. {
  163. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  164. return (-1);
  165. }
  166. channel->timeout = PLC_MODULE_WRITE_TIMEOUT;
  167. do {
  168. if (ReadMME (plc, 0, (VS_MODULE_OPERATION | MMTYPE_CNF)) <= 0)
  169. {
  170. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  171. channel->timeout = timer;
  172. return (-1);
  173. }
  174. } while (confirm->MODULE_SPEC.MOD_OP != HTOLE16 (PLC_MOD_OP_WRITE_MODULE) ||
  175. confirm->MODULE_SPEC.MOD_OP_SESSION_ID != HTOLE32 (plc->cookie));
  176. channel->timeout = timer;
  177. #if 1
  178. fprintf (stderr, "MSTATUS 0x%04X\n", LE16TOH (confirm->MSTATUS));
  179. fprintf (stderr, "ERROR_REC_CODE %d\n", LE16TOH (confirm->ERR_REC_CODE));
  180. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (confirm->RESERVED));
  181. fprintf (stderr, "NUM_OP_DATA %d\n", confirm->NUM_OP_DATA);
  182. fprintf (stderr, "MOD_OP 0x%02X\n", LE16TOH (request->MODULE_SPEC.MOD_OP));
  183. fprintf (stderr, "MOD_OP_DATA_LEN %d\n", LE16TOH (confirm->MODULE_SPEC.MOD_OP_DATA_LEN));
  184. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (confirm->MODULE_SPEC.MOD_OP_RSVD));
  185. fprintf (stderr, "MODULE_ID 0x%04X\n", LE16TOH (confirm->MODULE_SPEC.MODULE_ID));
  186. fprintf (stderr, "MODULE_SUB_ID 0x%04X\n", LE16TOH (confirm->MODULE_SPEC.MODULE_SUB_ID));
  187. fprintf (stderr, "MODULE_LENGTH %d\n", LE16TOH (confirm->MODULE_SPEC.MODULE_LENGTH));
  188. fprintf (stderr, "MODULE_OFFSET 0x%08X\n", LE32TOH (request->MODULE_SPEC.MODULE_OFFSET));
  189. #endif
  190. if (confirm->MSTATUS)
  191. {
  192. Failure (plc, PLC_WONTDOIT);
  193. return (-1);
  194. }
  195. if (LE16TOH (confirm->MODULE_SPEC.MODULE_LENGTH) != length)
  196. {
  197. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  198. return (-1);
  199. }
  200. if (LE32TOH (confirm->MODULE_SPEC.MODULE_OFFSET) != offset)
  201. {
  202. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  203. return (-1);
  204. }
  205. extent -= length;
  206. offset += length;
  207. }
  208. return (0);
  209. }
  210. #endif