PLCReadParameterBlock.c 5.6 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 PLCReadParameterBlock (struct channel * channel, struct message * message, void * memory, size_t extent);
  44. *
  45. * plc.h
  46. *
  47. * Read the PIB Image from an INT6000 using as many VS_RD_MOD
  48. * messages as needed; Write image blocks to file as they are
  49. * read;
  50. *
  51. * the objective here is to read the PIB module in 1024 byte blocks
  52. * until the module offset equals or exceeds the PIB actual; we do
  53. * not know the PIB actual until we have read the header contained
  54. * in the first block read from OFFSET 0;
  55. *
  56. * MOFFSET and MLENGTH fields occupy different offsets in REQ and CNF
  57. * messages; we exploit that by initializing them using CNF message
  58. * offsets then copying values into REQ message offsets before each
  59. * read; this works because confirmation messages always return all
  60. * the data we request or nothing at all;
  61. *
  62. * Contributor(s):
  63. * Charles Maier
  64. * Nathaniel Houghton
  65. *
  66. *--------------------------------------------------------------------*/
  67. #ifndef READPARAMETERBLOCK_SOURCE
  68. #define READPARAMETERBLOCK_SOURCE
  69. #include <stdint.h>
  70. #include <unistd.h>
  71. #include <memory.h>
  72. #include "../tools/error.h"
  73. #include "../tools/files.h"
  74. #include "../pib/pib.h"
  75. #include "../plc/plc.h"
  76. signed PLCReadParameterBlock (struct channel * channel, struct message * message, void * memory, size_t extent)
  77. {
  78. uint8_t * buffer = (uint8_t *)(memory);
  79. #ifndef __GNUC__
  80. #pragma pack (push,1)
  81. #endif
  82. struct __packed vs_rd_mod_request
  83. {
  84. struct ethernet_hdr ethernet;
  85. struct qualcomm_hdr qualcomm;
  86. uint8_t MODULEID;
  87. uint8_t MACCESS;
  88. uint16_t MLENGTH;
  89. uint32_t MOFFSET;
  90. uint8_t MSECRET [16];
  91. }
  92. * request = (struct vs_rd_mod_request *) (message);
  93. struct __packed vs_rd_mod_confirm
  94. {
  95. struct ethernet_hdr ethernet;
  96. struct qualcomm_hdr qualcomm;
  97. uint8_t MSTATUS;
  98. uint8_t RESERVED1 [3];
  99. uint8_t MODULEID;
  100. uint8_t RESERVED;
  101. uint16_t MLENGTH;
  102. uint32_t MOFFSET;
  103. uint32_t CHKSUM;
  104. uint8_t BUFFER [PLC_RECORD_SIZE];
  105. }
  106. * confirm = (struct vs_rd_mod_confirm *) (message);
  107. #ifndef __GNUC__
  108. #pragma pack (pop)
  109. #endif
  110. uint16_t length = 0;
  111. uint32_t offset = 0;
  112. signed actual = PLC_RECORD_SIZE;
  113. do
  114. {
  115. memset (message, 0, sizeof (* message));
  116. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  117. QualcommHeader (&request->qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  118. request->MODULEID = VS_MODULE_PIB;
  119. request->MLENGTH = HTOLE16 (actual);
  120. request->MOFFSET = HTOLE32 (offset);
  121. if (sendpacket (channel, message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
  122. {
  123. error (1, errno, CHANNEL_CANTSEND);
  124. }
  125. if (readpacket (channel, message, sizeof (* message)) <= 0)
  126. {
  127. error (1, errno, CHANNEL_CANTREAD);
  128. }
  129. if (UnwantedMessage (message, sizeof (* confirm), 0, (VS_RD_MOD | MMTYPE_CNF)))
  130. {
  131. actual = PLC_RECORD_SIZE;
  132. offset = 0;
  133. continue;
  134. }
  135. if (confirm->MSTATUS)
  136. {
  137. error (1, ECANCELED, PLC_WONTDOIT);
  138. }
  139. if (LE16TOH (confirm->MLENGTH) != actual)
  140. {
  141. error (1, ECANCELED, PLC_ERR_LENGTH);
  142. }
  143. if (LE32TOH (confirm->MOFFSET) != offset)
  144. {
  145. error (1, ECANCELED, PLC_ERR_OFFSET);
  146. }
  147. actual = LE16TOH (confirm->MLENGTH);
  148. offset = LE32TOH (confirm->MOFFSET);
  149. if (checksum32 (confirm->BUFFER, actual, confirm->CHKSUM))
  150. {
  151. error (1, ECANCELED, "Bad Packet Checksum");
  152. }
  153. if (offset == length)
  154. {
  155. struct pib_header * pib_header = (struct pib_header *) (confirm->BUFFER);
  156. length = LE16TOH (pib_header->PIBLENGTH);
  157. }
  158. if ((offset + actual) > length)
  159. {
  160. actual = length - offset;
  161. }
  162. memcpy (buffer + offset, confirm->BUFFER, actual);
  163. offset += actual;
  164. extent -= actual;
  165. }
  166. while (offset < length);
  167. return (offset);
  168. }
  169. #endif