ModuleRead.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 ModuleRead (struct plc * plc, struct _file_ * file, uint16_t source, uint16_t module, uint16_t submodule);
  44. *
  45. * plc.h
  46. *
  47. * Read a module from volatile or non-volatile memory and write it
  48. * to file;
  49. *
  50. *
  51. * Contributor(s):
  52. * Charles Maier
  53. *
  54. *--------------------------------------------------------------------*/
  55. #ifndef MODULEREAD_SOURCE
  56. #define MODULEREAD_SOURCE
  57. #include <stdint.h>
  58. #include <unistd.h>
  59. #include <memory.h>
  60. #include "../tools/error.h"
  61. #include "../tools/files.h"
  62. #include "../tools/endian.h"
  63. #include "../tools/memory.h"
  64. #include "../plc/plc.h"
  65. signed ModuleRead (struct plc * plc, struct _file_ * file, uint16_t source, uint16_t module, uint16_t submodule)
  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 __packed vs_module_operation_read_request
  73. {
  74. struct ethernet_hdr ethernet;
  75. struct qualcomm_hdr qualcomm;
  76. uint32_t RESERVED;
  77. uint8_t NUM_OP_DATA;
  78. struct __packed
  79. {
  80. uint16_t MOD_OP;
  81. uint16_t MOD_OP_DATA_LEN;
  82. uint32_t MOD_OP_RSVD;
  83. uint16_t MODULE_ID;
  84. uint16_t MODULE_SUB_ID;
  85. uint16_t MODULE_LENGTH;
  86. uint32_t MODULE_OFFSET;
  87. }
  88. MODULE_SPEC;
  89. }
  90. * request = (struct vs_module_operation_read_request *)(message);
  91. struct __packed vs_module_operation_read_confirm
  92. {
  93. struct ethernet_hdr ethernet;
  94. struct qualcomm_hdr qualcomm;
  95. uint16_t MSTATUS;
  96. uint16_t ERR_REC_CODE;
  97. uint32_t RESERVED;
  98. uint8_t NUM_OP_DATA;
  99. struct __packed
  100. {
  101. uint16_t MOD_OP;
  102. uint16_t MOD_OP_DATA_LEN;
  103. uint32_t MOD_OP_RSVD;
  104. uint16_t MODULE_ID;
  105. uint16_t MODULE_SUB_ID;
  106. uint16_t MODULE_LENGTH;
  107. uint32_t MODULE_OFFSET;
  108. }
  109. MODULE_SPEC;
  110. uint8_t MODULE_DATA [PLC_MODULE_SIZE];
  111. }
  112. * confirm = (struct vs_module_operation_read_confirm *)(message);
  113. #ifndef __GNUC__
  114. #pragma pack (pop)
  115. #endif
  116. unsigned timer = channel->timeout;
  117. uint16_t length = PLC_MODULE_SIZE;
  118. uint32_t offset = 0;
  119. Request (plc, "Read Module from %s", source? "Flash": "Memory");
  120. if (lseek (file->file, 0, SEEK_SET) == -1)
  121. {
  122. error (PLC_EXIT (plc), errno, FILE_CANTHOME, file->name);
  123. return (-1);
  124. }
  125. while (length == PLC_MODULE_SIZE)
  126. {
  127. memset (message, 0, sizeof (* message));
  128. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  129. QualcommHeader (&request->qualcomm, 0, (VS_MODULE_OPERATION | MMTYPE_REQ));
  130. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  131. request->NUM_OP_DATA = 1;
  132. request->MODULE_SPEC.MOD_OP = HTOLE16 (source);
  133. request->MODULE_SPEC.MOD_OP_DATA_LEN = HTOLE16 (sizeof (request->MODULE_SPEC));
  134. request->MODULE_SPEC.MOD_OP_RSVD = HTOLE32 (0);
  135. request->MODULE_SPEC.MODULE_ID = HTOLE16 (module);
  136. request->MODULE_SPEC.MODULE_SUB_ID = HTOLE16 (submodule);
  137. request->MODULE_SPEC.MODULE_LENGTH = HTOLE16 (length);
  138. request->MODULE_SPEC.MODULE_OFFSET = HTOLE32 (offset);
  139. #if 0
  140. #if defined (__GNUC__)
  141. #warning "Debug code active in module ModuleRead"
  142. #endif
  143. fprintf (stderr, "----- \n");
  144. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (request->RESERVED));
  145. fprintf (stderr, "NUM_OP_DATA %d\n", request->NUM_OP_DATA);
  146. fprintf (stderr, "MOD_OP 0x%02X\n", LE16TOH (request->MODULE_SPEC.MOD_OP));
  147. fprintf (stderr, "MOD_OP_DATA_LEN %d\n", LE16TOH (request->MODULE_SPEC.MOD_OP_DATA_LEN));
  148. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (request->MODULE_SPEC.MOD_OP_RSVD));
  149. fprintf (stderr, "MODULE_ID 0x%04X\n", LE16TOH (request->MODULE_SPEC.MODULE_ID));
  150. fprintf (stderr, "MODULE_SUB_ID 0x%04X\n", LE16TOH (request->MODULE_SPEC.MODULE_SUB_ID));
  151. fprintf (stderr, "MODULE_LENGTH %d\n", LE16TOH (request->MODULE_SPEC.MODULE_LENGTH));
  152. fprintf (stderr, "MODULE_OFFSET 0x%08X\n", LE32TOH (request->MODULE_SPEC.MODULE_OFFSET));
  153. fprintf (stderr, "\n");
  154. #endif
  155. if (SendMME (plc) <= 0)
  156. {
  157. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  158. return (-1);
  159. }
  160. channel->timeout = PLC_MODULE_READ_TIMEOUT;
  161. do {
  162. if (ReadMME (plc, 0, (VS_MODULE_OPERATION | MMTYPE_CNF)) <= 0)
  163. {
  164. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  165. return (-1);
  166. }
  167. } while (confirm->MODULE_SPEC.MOD_OP != HTOLE16 (source) ||
  168. confirm->MODULE_SPEC.MODULE_ID != HTOLE16 (module) ||
  169. confirm->MODULE_SPEC.MODULE_SUB_ID != HTOLE16 (submodule) ||
  170. confirm->MODULE_SPEC.MODULE_OFFSET != HTOLE32 (offset));
  171. channel->timeout = timer;
  172. #if 0
  173. #if defined (__GNUC__)
  174. #warning "Debug code active in module ModuleRead"
  175. #endif
  176. fprintf (stderr, "MSTATUS 0x%04X\n", LE16TOH (confirm->MSTATUS));
  177. fprintf (stderr, "ERROR_REC_CODE %d\n", LE16TOH (confirm->ERR_REC_CODE));
  178. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (confirm->RESERVED));
  179. fprintf (stderr, "NUM_OP_DATA %d\n", confirm->NUM_OP_DATA);
  180. fprintf (stderr, "MOD_OP 0x%02X\n", LE16TOH (confirm->MODULE_SPEC.MOD_OP));
  181. fprintf (stderr, "MOD_OP_DATA_LEN %d\n", LE16TOH (confirm->MODULE_SPEC.MOD_OP_DATA_LEN));
  182. fprintf (stderr, "RESERVED 0x%08X\n", LE32TOH (confirm->MODULE_SPEC.MOD_OP_RSVD));
  183. fprintf (stderr, "MODULE_ID 0x%04X\n", LE16TOH (confirm->MODULE_SPEC.MODULE_ID));
  184. fprintf (stderr, "MODULE_SUB_ID 0x%04X\n", LE16TOH (confirm->MODULE_SPEC.MODULE_SUB_ID));
  185. fprintf (stderr, "MODULE_LENGTH %d\n", LE16TOH (confirm->MODULE_SPEC.MODULE_LENGTH));
  186. fprintf (stderr, "MODULE_OFFSET 0x%08X\n", LE32TOH (confirm->MODULE_SPEC.MODULE_OFFSET));
  187. fprintf (stderr, "\n");
  188. #endif
  189. if (LE16TOH (confirm->MSTATUS))
  190. {
  191. Failure (plc, PLC_WONTDOIT);
  192. return (-1);
  193. }
  194. length = LE16TOH (confirm->MODULE_SPEC.MODULE_LENGTH);
  195. offset = LE32TOH (confirm->MODULE_SPEC.MODULE_OFFSET);
  196. if (write (file->file, confirm->MODULE_DATA, length) != (signed)(length))
  197. {
  198. error (PLC_EXIT (plc), errno, FILE_CANTSAVE, file->name);
  199. return (-1);
  200. }
  201. offset += length;
  202. }
  203. return (0);
  204. }
  205. #endif