ModuleErase.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed ModuleErase (struct plc * plc, struct vs_module_spec * vs_module_spec);
  11. *
  12. * plc.h
  13. *
  14. * erase a module by writing 0xFF to all locations in flash memory;
  15. * in most cases, the module will be all of flash memory; allow up
  16. * to 60 seconds for each write to complete; extra time is needed
  17. * because memory must be erased;
  18. *
  19. * Contributor(s):
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. #ifndef MODULEERASE_SOURCE
  24. #define MODULEERASE_SOURCE
  25. #include <stdint.h>
  26. #include <limits.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <memory.h>
  30. #include "../tools/error.h"
  31. #include "../plc/plc.h"
  32. signed ModuleErase (struct plc * plc, struct vs_module_spec * vs_module_spec)
  33. {
  34. struct channel * channel = (struct channel *) (plc->channel);
  35. struct message * message = (struct message *) (plc->message);
  36. #ifndef __GNUC__
  37. #pragma pack (push,1)
  38. #endif
  39. struct __packed vs_module_operation_write_request
  40. {
  41. struct ethernet_hdr ethernet;
  42. struct qualcomm_hdr qualcomm;
  43. uint32_t RESERVED;
  44. uint8_t NUM_OP_DATA;
  45. struct __packed
  46. {
  47. uint16_t MOD_OP;
  48. uint16_t MOD_OP_DATA_LEN;
  49. uint32_t MOD_OP_RSVD;
  50. uint32_t MOD_OP_SESSION_ID;
  51. uint8_t MODULE_IDX;
  52. uint16_t MODULE_ID;
  53. uint16_t MODULE_SUB_ID;
  54. uint16_t DATA_LENGTH;
  55. uint32_t DATA_OFFSET;
  56. }
  57. MODULE_SPEC;
  58. uint8_t MODULE_DATA [PLC_MODULE_SIZE];
  59. }
  60. * request = (struct vs_module_operation_write_request *) (message);
  61. struct __packed vs_module_operation_write_confirm
  62. {
  63. struct ethernet_hdr ethernet;
  64. struct qualcomm_hdr qualcomm;
  65. uint16_t MSTATUS;
  66. uint16_t ERR_REC_CODE;
  67. uint32_t RESERVED;
  68. uint8_t NUM_OP_DATA;
  69. struct __packed
  70. {
  71. uint16_t MOD_OP;
  72. uint16_t MOD_OP_DATA_LEN;
  73. uint32_t MOD_OP_RSVD;
  74. uint32_t MOD_OP_SESSION_ID;
  75. uint8_t MODULE_IDX;
  76. uint16_t MODULE_ID;
  77. uint16_t MODULE_SUB_ID;
  78. uint16_t DATA_LENGTH;
  79. uint32_t DATA_OFFSET;
  80. }
  81. MODULE_SPEC;
  82. }
  83. * confirm = (struct vs_module_operation_write_confirm *) (message);
  84. #ifndef __GNUC__
  85. #pragma pack (pop)
  86. #endif
  87. unsigned timeout = channel->timeout;
  88. uint16_t length = PLC_MODULE_SIZE;
  89. uint32_t extent = vs_module_spec->MODULE_LENGTH;
  90. uint32_t offset = 0;
  91. Request (plc, "Erase Flash Memory");
  92. while (extent)
  93. {
  94. memset (message, 0, sizeof (* message));
  95. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  96. QualcommHeader (& request->qualcomm, 0, (VS_MODULE_OPERATION | MMTYPE_REQ));
  97. plc->packetsize = sizeof (struct vs_module_operation_write_request);
  98. if (length > extent)
  99. {
  100. length = extent;
  101. }
  102. request->NUM_OP_DATA = 1;
  103. request->MODULE_SPEC.MOD_OP = HTOLE16 (0x11);
  104. request->MODULE_SPEC.MOD_OP_DATA_LEN = HTOLE16 (sizeof (request->MODULE_SPEC) + sizeof (request->MODULE_DATA));
  105. request->MODULE_SPEC.MOD_OP_SESSION_ID = HTOLE32 (plc->cookie);
  106. request->MODULE_SPEC.MODULE_IDX = 0;
  107. request->MODULE_SPEC.MODULE_ID = HTOLE16 (vs_module_spec->MODULE_ID);
  108. request->MODULE_SPEC.MODULE_SUB_ID = HTOLE16 (vs_module_spec->MODULE_SUB_ID);
  109. request->MODULE_SPEC.DATA_LENGTH = HTOLE16 (length);
  110. request->MODULE_SPEC.DATA_OFFSET = HTOLE32 (offset);
  111. memset (request->MODULE_DATA, 0xFF, length);
  112. if (SendMME (plc) <= 0)
  113. {
  114. error (1, errno, CHANNEL_CANTSEND);
  115. }
  116. channel->timeout = PLC_MODULE_WRITE_TIMEOUT;
  117. if (ReadMME (plc, 0, (VS_MODULE_OPERATION | MMTYPE_CNF)) <= 0)
  118. {
  119. error (1, errno, CHANNEL_CANTREAD);
  120. }
  121. channel->timeout = timeout;
  122. if (confirm->MSTATUS)
  123. {
  124. Failure (plc, PLC_WONTDOIT);
  125. return (-1);
  126. }
  127. if (LE16TOH (confirm->MODULE_SPEC.DATA_LENGTH) != length)
  128. {
  129. error (1, 0, PLC_ERR_LENGTH);
  130. }
  131. if (LE32TOH (confirm->MODULE_SPEC.DATA_OFFSET) != offset)
  132. {
  133. error (1, 0, PLC_ERR_OFFSET);
  134. }
  135. extent -= length;
  136. offset += length;
  137. }
  138. return (0);
  139. }
  140. #endif