ReadFlashFirmware.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed ReadFlashFirmware (struct plc *plc);
  11. *
  12. * nda.h
  13. *
  14. * Contributor(s):
  15. * Charles Maier <cmaier@qca.qualcomm.com>
  16. *
  17. *--------------------------------------------------------------------*/
  18. #ifndef READFLASHFIRMWARE_SOURCE
  19. #define READFLASHFIRMWARE_SOURCE
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <memory.h>
  23. #include "../tools/error.h"
  24. #include "../tools/files.h"
  25. #include "../nvm/nvm.h"
  26. #include "../plc/plc.h"
  27. #include "../nda/nda.h"
  28. signed ReadFlashFirmware (struct plc * plc)
  29. {
  30. struct channel * channel = (struct channel *) (plc->channel);
  31. struct message * message = (struct message *) (plc->message);
  32. #ifndef __GNUC__
  33. #pragma pack (push,1)
  34. #endif
  35. struct __packed vs_rd_blk_nvm_request
  36. {
  37. struct ethernet_hdr ethernet;
  38. struct qualcomm_hdr qualcomm;
  39. uint16_t MODULEID;
  40. uint32_t MOFFSET;
  41. uint32_t MLENGTH;
  42. }
  43. * request = (struct vs_rd_blk_nvm_request *) (message);
  44. struct __packed vs_rd_blk_nvm_confirm
  45. {
  46. struct ethernet_hdr ethernet;
  47. struct qualcomm_hdr qualcomm;
  48. uint8_t MSTATUS;
  49. uint16_t MODULEID;
  50. uint32_t MOFFSET;
  51. uint32_t MLENGTH;
  52. uint8_t BUFFER [PLC_RECORD_SIZE];
  53. }
  54. * confirm = (struct vs_rd_blk_nvm_confirm *) (message);
  55. #ifndef __GNUC__
  56. #pragma pack (pop)
  57. #endif
  58. uint32_t offset = 0;
  59. uint32_t length = PLC_RECORD_SIZE;
  60. Request (plc, "Reading Flash");
  61. if (lseek (plc->nvm.file, 0, SEEK_SET))
  62. {
  63. error (PLC_EXIT (plc), errno, FILE_CANTHOME, plc->nvm.name);
  64. return (1);
  65. }
  66. do
  67. {
  68. memset (message, 0, sizeof (* message));
  69. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  70. QualcommHeader (& request->qualcomm, 0, (VS_RD_BLK_NVM | MMTYPE_REQ));
  71. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  72. request->MODULEID = HTOLE16 (MID_FIRMWARE);
  73. request->MLENGTH = HTOLE32 (length);
  74. request->MOFFSET = HTOLE32 (offset);
  75. if (SendMME (plc) <= 0)
  76. {
  77. error (PLC_EXIT (plc), ECANCELED, CHANNEL_CANTSEND);
  78. return (-1);
  79. }
  80. if (ReadMME (plc, 0, (VS_RD_BLK_NVM | MMTYPE_CNF)) <= 0)
  81. {
  82. error (PLC_EXIT (plc), ECANCELED, CHANNEL_CANTREAD);
  83. return (-1);
  84. }
  85. if (confirm->MSTATUS)
  86. {
  87. Failure (plc, PLC_WONTDOIT);
  88. return (-1);
  89. }
  90. if (LE32TOH (confirm->MOFFSET) != offset)
  91. {
  92. Failure (plc, PLC_ERR_OFFSET);
  93. return (-1);
  94. }
  95. if (LE32TOH (confirm->MLENGTH) != length)
  96. {
  97. Failure (plc, PLC_ERR_LENGTH);
  98. return (-1);
  99. }
  100. if (lseek (plc->nvm.file, offset, SEEK_SET) != (signed) (offset))
  101. {
  102. error (PLC_EXIT (plc), errno, "Can't seek %s", plc->nvm.name);
  103. return (-1);
  104. }
  105. if (write (plc->nvm.file, confirm->BUFFER, length) < (signed) (length))
  106. {
  107. error (PLC_EXIT (plc), errno, "Can't save %s", plc->nvm.name);
  108. return (-1);
  109. }
  110. offset += length;// read another 1K block
  111. }
  112. // read the entire flash
  113. while ( offset < ( 2048 * 1024 ) );
  114. return (0);
  115. }
  116. #endif