ReadParameters1.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 ReadParameters1 (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * Read the PIB Image from an INT6x00 or AR7x00 using as many
  48. * VS_RD_MOD messages as needed; Write image blocks to file as
  49. * they are 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 length; we do
  53. * not know the PIB length until we have read the header contained
  54. * in the first block at 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. *
  63. * Contributor(s):
  64. * Charles Maier
  65. * Nathaniel Houghton
  66. * Lluis Paulet Gimbert <dimat.com>
  67. *
  68. *--------------------------------------------------------------------*/
  69. #ifndef READPARAMETERS1_SOURCE
  70. #define READPARAMETERS1_SOURCE
  71. #include <stdint.h>
  72. #include <unistd.h>
  73. #include <memory.h>
  74. #include "../tools/error.h"
  75. #include "../tools/files.h"
  76. #include "../plc/plc.h"
  77. #include "../pib/pib.h"
  78. signed ReadParameters1 (struct plc * plc)
  79. {
  80. struct channel * channel = (struct channel *)(plc->channel);
  81. struct message * message = (struct message *)(plc->message);
  82. #ifndef __GNUC__
  83. #pragma pack (push,1)
  84. #endif
  85. struct __packed vs_rd_mod_request
  86. {
  87. struct ethernet_hdr ethernet;
  88. struct qualcomm_hdr qualcomm;
  89. uint8_t MODULEID;
  90. uint8_t MACCESS;
  91. uint16_t MLENGTH;
  92. uint32_t MOFFSET;
  93. uint8_t MSECRET [16];
  94. }
  95. * request = (struct vs_rd_mod_request *) (message);
  96. struct __packed vs_rd_mod_confirm
  97. {
  98. struct ethernet_hdr ethernet;
  99. struct qualcomm_hdr qualcomm;
  100. uint8_t MSTATUS;
  101. uint8_t RESERVED1 [3];
  102. uint8_t MODULEID;
  103. uint8_t RESERVED;
  104. uint16_t MLENGTH;
  105. uint32_t MOFFSET;
  106. uint32_t CHKSUM;
  107. uint8_t BUFFER [PLC_RECORD_SIZE];
  108. }
  109. * confirm = (struct vs_rd_mod_confirm *) (message);
  110. #ifndef __GNUC__
  111. #pragma pack (pop)
  112. #endif
  113. uint32_t extent = 0;
  114. uint32_t offset = 0;
  115. uint16_t length = PLC_RECORD_SIZE;
  116. Request (plc, "Read Parameters from Device");
  117. if (lseek (plc->pib.file, 0, SEEK_SET))
  118. {
  119. error (PLC_EXIT (plc), errno, FILE_CANTHOME, plc->pib.name);
  120. return (1);
  121. }
  122. do
  123. {
  124. memset (message, 0, sizeof (* message));
  125. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  126. QualcommHeader (&request->qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  127. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  128. request->MODULEID = VS_MODULE_PIB;
  129. request->MLENGTH = HTOLE16 (length);
  130. request->MOFFSET = HTOLE32 (offset);
  131. if (SendMME (plc) <= 0)
  132. {
  133. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  134. return (-1);
  135. }
  136. if (ReadMME (plc, 0, (VS_RD_MOD | MMTYPE_CNF)) <= 0)
  137. {
  138. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  139. return (-1);
  140. }
  141. if (confirm->MSTATUS)
  142. {
  143. Failure (plc, PLC_WONTDOIT);
  144. return (-1);
  145. }
  146. if (LE16TOH (confirm->MLENGTH) != length)
  147. {
  148. error (PLC_EXIT (plc), 0, PLC_ERR_LENGTH);
  149. return (-1);
  150. }
  151. if (LE32TOH (confirm->MOFFSET) != offset)
  152. {
  153. error (PLC_EXIT (plc), 0, PLC_ERR_OFFSET);
  154. return (-1);
  155. }
  156. length = LE16TOH (confirm->MLENGTH);
  157. offset = LE32TOH (confirm->MOFFSET);
  158. if (checksum32 (confirm->BUFFER, length, confirm->CHKSUM))
  159. {
  160. error (PLC_EXIT (plc), ECANCELED, "Bad Packet Checksum");
  161. return (-1);
  162. }
  163. if (offset == extent)
  164. {
  165. struct pib_header * pib_header = (struct pib_header *) (confirm->BUFFER);
  166. extent = LE16TOH (pib_header->PIBLENGTH);
  167. }
  168. if ((offset + length) > extent)
  169. {
  170. length = extent - offset;
  171. }
  172. if (lseek (plc->pib.file, offset, SEEK_SET) != (signed)(offset))
  173. {
  174. error (PLC_EXIT (plc), errno, FILE_CANTSEEK, plc->pib.name);
  175. return (-1);
  176. }
  177. if (write (plc->pib.file, confirm->BUFFER, length) != (signed)(length))
  178. {
  179. error (PLC_EXIT (plc), errno, FILE_CANTSAVE, plc->pib.name);
  180. return (-1);
  181. }
  182. offset += length;
  183. }
  184. while (offset < extent);
  185. Confirm (plc, "Read %s", plc->pib.name);
  186. return (0);
  187. }
  188. #endif