InitDevice2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * int InitDevice2 (struct plc * plc);
  11. *
  12. * plc.h
  13. *
  14. * configure volatile memory by finding the memctl applet in a
  15. * newer .nvm file then downloading it and executing it;
  16. *
  17. * this implementation detects chipsets prior to INT6400 and uses
  18. * VS_WR_MEM/VS_ST_MAC messages instead of VS_WRITE_AND_EXECUTE
  19. * messages;
  20. *
  21. * Contributor(s):
  22. * Charles Maier <cmaier@qca.qualcomm.com>
  23. *
  24. *--------------------------------------------------------------------*/
  25. #ifndef INITDEVICE2_SOURCE
  26. #define INITDEVICE2_SOURCE
  27. #include <stdint.h>
  28. #include <unistd.h>
  29. #include <memory.h>
  30. #include <errno.h>
  31. #include "../tools/memory.h"
  32. #include "../tools/files.h"
  33. #include "../tools/flags.h"
  34. #include "../tools/error.h"
  35. #include "../plc/plc.h"
  36. #include "../nvm/nvm.h"
  37. int InitDevice2 (struct plc * plc)
  38. {
  39. unsigned module = 0;
  40. struct panther_nvm_header nvm_header;
  41. if (lseek (plc->NVM.file, 0, SEEK_SET))
  42. {
  43. error (1, errno, FILE_CANTHOME, plc->NVM.name);
  44. }
  45. do
  46. {
  47. if (read (plc->NVM.file, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  48. {
  49. error (1, errno, NVM_HDR_CANTREAD, plc->NVM.name, module);
  50. }
  51. if (LE16TOH (nvm_header.MajorVersion) != 1)
  52. {
  53. error (1, 0, NVM_HDR_VERSION, plc->NVM.name, module);
  54. }
  55. if (LE16TOH (nvm_header.MinorVersion) != 1)
  56. {
  57. error (1, 0, NVM_HDR_VERSION, plc->NVM.name, module);
  58. }
  59. if (checksum32 (& nvm_header, sizeof (nvm_header), 0))
  60. {
  61. error (1, 0, NVM_HDR_CHECKSUM, plc->NVM.name, module);
  62. }
  63. #if 0
  64. if (_allclr (LE16TOH (nvm_header.ExecuteMask), (1 << (plc->hardwareID -1))))
  65. {
  66. if (lseek (plc->NVM.file, LE32TOH (nvm_header.NextHeader), SEEK_SET) == -1)
  67. {
  68. error (1, errno, "Can't skip module: %s (%d)", plc->NVM.name, module);
  69. }
  70. }
  71. else
  72. #endif
  73. if (LE32TOH (nvm_header.ImageType) == NVM_IMAGE_MEMCTL)
  74. {
  75. if (WriteExecuteFirmware2 (plc, module, & nvm_header))
  76. {
  77. return (-1);
  78. }
  79. break;
  80. }
  81. if (lseek (plc->NVM.file, LE32TOH (nvm_header.NextHeader), SEEK_SET) == -1)
  82. {
  83. error (1, errno, "Can't skip module: %s (%d)", plc->NVM.name, module);
  84. }
  85. module++;
  86. }
  87. while (~ nvm_header.NextHeader);
  88. return (0);
  89. }
  90. #endif