ReadFlashMemory1.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed ReadFlashMemory1 (struct plc * plc);
  11. *
  12. * plc.h
  13. *
  14. * read all of flash memory and write to file;
  15. *
  16. * Contributor(s):
  17. * Charles Maier <cmaier@qca.qualcomm.com>
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef READFLASHMEMORY1_SOURCE
  21. #define READFLASHMEMORY1_SOURCE
  22. #include <string.h>
  23. #include "../tools/error.h"
  24. #include "../tools/files.h"
  25. #include "../tools/memory.h"
  26. #include "../tools/endian.h"
  27. #include "../plc/plc.h"
  28. #include "../nda/nda.h"
  29. signed ReadFlashMemory1 (struct plc * plc)
  30. {
  31. struct channel * channel = (struct channel *) (plc->channel);
  32. struct message * message = (struct message *) (plc->message);
  33. #ifndef __GNUC__
  34. #pragma pack (push,1)
  35. #endif
  36. struct __packed vs_rd_blk_nvm_request
  37. {
  38. struct ethernet_hdr ethernet;
  39. struct qualcomm_hdr qualcomm;
  40. uint16_t MODULEID;
  41. uint32_t MOFFSET;
  42. uint32_t MLENGTH;
  43. }
  44. * request = (struct vs_rd_blk_nvm_request *) (message);
  45. struct __packed vs_rd_blk_nvm_confirm
  46. {
  47. struct ethernet_hdr ethernet;
  48. struct qualcomm_hdr qualcomm;
  49. uint8_t MSTATUS;
  50. uint16_t MODULEID;
  51. uint32_t MOFFSET;
  52. uint32_t MLENGTH;
  53. uint8_t BUFFER [PLC_RECORD_SIZE];
  54. }
  55. * confirm = (struct vs_rd_blk_nvm_confirm *) (message);
  56. #ifndef __GNUC__
  57. #pragma pack (pop)
  58. #endif
  59. unsigned offset = 0;
  60. unsigned length = PLC_MODULE_SIZE;
  61. Request (plc, "Read Flash Memory");
  62. while (length == PLC_MODULE_SIZE)
  63. {
  64. memset (message, 0, sizeof (* message));
  65. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  66. QualcommHeader (& request->qualcomm, 0, (VS_RD_BLK_NVM | MMTYPE_REQ));
  67. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  68. request->MODULEID = HTOLE16 (MID_USERPIB);
  69. request->MOFFSET = HTOLE32 (offset);
  70. request->MLENGTH = HTOLE32 (length);
  71. if (SendMME (plc) <= 0)
  72. {
  73. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  74. return (-1);
  75. }
  76. if (ReadMME (plc, 0, (VS_RD_BLK_NVM | MMTYPE_CNF)) <= 0)
  77. {
  78. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  79. return (-1);
  80. }
  81. if (confirm->MSTATUS)
  82. {
  83. Failure (plc, PLC_WONTDOIT);
  84. return (-1);
  85. }
  86. length = LE16TOH (confirm->MLENGTH);
  87. offset = LE32TOH (confirm->MOFFSET);
  88. if (write (plc->nvm.file, confirm->BUFFER, length) != (signed) (length))
  89. {
  90. error (PLC_EXIT (plc), errno, FILE_CANTSAVE, plc->nvm.name);
  91. return (-1);
  92. }
  93. offset += length;
  94. }
  95. return (0);
  96. }
  97. #endif