panther_nvm_seek.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /****************************************************************************
  2. Copyright (c) 2013-2021, Qualcomm Technologies, Inc.
  3. All Rights Reserved.
  4. Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. **********************************************************************
  6. 2013 Qualcomm Atheros, Inc.
  7. ****************************************************************************/
  8. /*====================================================================*
  9. *
  10. * signed panther_nvm_seek (signd fd, char const * filename, struct panther_nvm_header * nvm_header, uint32_t imagetype);
  11. *
  12. * nvm.h
  13. *
  14. * search a panther/lynx PIB file for the next image of a given
  15. * type; return 0 on success or -1 on failure to find another
  16. * image of the given type;
  17. *
  18. * the call must provide an image header strucuture for use while
  19. * searching; on success, that structure will contain information
  20. * about the image and file will be positioned to the start of
  21. * the image;
  22. *
  23. *
  24. *--------------------------------------------------------------------*/
  25. #ifndef PANTHER_NVM_SEEK_SOURCE
  26. #define PANTHER_NVM_SEEK_SOURCE
  27. #include <unistd.h>
  28. #include "../tools/endian.h"
  29. #include "../tools/files.h"
  30. #include "../tools/error.h"
  31. #include "../pib/pib.h"
  32. #include "../nvm/nvm.h"
  33. signed panther_nvm_seek (signed fd, char const * filename, struct panther_nvm_header * header, uint32_t imagetype)
  34. {
  35. unsigned module = 0;
  36. uint32_t origin = ~ 0;
  37. uint32_t offset = 0;
  38. do
  39. {
  40. if (read (fd, header, sizeof (* header)) != sizeof (* header))
  41. {
  42. error (1, errno, NVM_HDR_CANTREAD, filename, module);
  43. }
  44. if (LE16TOH (header->MajorVersion) != 1)
  45. {
  46. error (1, 0, "(0) " NVM_HDR_VERSION, filename, module);
  47. }
  48. if (LE16TOH (header->MinorVersion) != 1)
  49. {
  50. error (1, 0, "(1) " NVM_HDR_VERSION, filename, module);
  51. }
  52. if (checksum32 (header, sizeof (* header), 0))
  53. {
  54. error (1, 0, NVM_HDR_CHECKSUM, filename, module);
  55. }
  56. if (LE32TOH (header->PrevHeader) != origin)
  57. {
  58. error (1, 0, NVM_HDR_LINK, filename, module);
  59. }
  60. if (fdchecksum32 (fd, LE32TOH (header->ImageLength), header->ImageChecksum))
  61. {
  62. error (1, ECANCELED, NVM_IMG_CHECKSUM, filename, module);
  63. }
  64. if (LE32TOH (header->ImageType) == imagetype)
  65. {
  66. if (lseek (fd,(int)(0- LE32TOH(header->ImageLength) ),SEEK_CUR) == - 1)
  67. {
  68. error (1, ECANCELED, FILE_CANTHOME, filename);
  69. }
  70. return (0);
  71. }
  72. origin = offset;
  73. offset = LE32TOH (header->NextHeader);
  74. module++;
  75. }
  76. while (~ header->NextHeader);
  77. if (lseek (fd, 0, SEEK_CUR) != lseek (fd, 0, SEEK_END))
  78. {
  79. error (1, errno, NVM_HDR_LINK, filename, module);
  80. }
  81. if (lseek (fd, 0, SEEK_SET))
  82. {
  83. error (1, errno, FILE_CANTHOME, filename);
  84. }
  85. return (- 1);
  86. }
  87. /*====================================================================*
  88. *
  89. * signed panther_nvm_seek2 (signd fd, char const * filename, struct panther_nvm_header * nvm_header, uint32_t imagetype);
  90. *
  91. * nvm.h
  92. *
  93. * this function is duplicate of panther_nvm_seek and additionally created
  94. * to handle the repair checksum case;
  95. * without impacting other executables invoking panther_nvm_seek.
  96. *--------------------------------------------------------------------*/
  97. signed panther_nvm_seek2(signed fd, char const* filename, struct panther_nvm_header* header, uint32_t imagetype, bool repairchksum)
  98. {
  99. unsigned module = 0;
  100. uint32_t origin = ~0;
  101. uint32_t offset = 0;
  102. do
  103. {
  104. if (read(fd, header, sizeof(*header)) != sizeof(*header))
  105. {
  106. error(1, errno, NVM_HDR_CANTREAD, filename, module);
  107. }
  108. if (LE16TOH(header->MajorVersion) != 1)
  109. {
  110. error(1, 0, "(0) " NVM_HDR_VERSION, filename, module);
  111. }
  112. if (LE16TOH(header->MinorVersion) != 1)
  113. {
  114. error(1, 0, "(1) " NVM_HDR_VERSION, filename, module);
  115. }
  116. if (checksum32(header, sizeof(*header), 0))
  117. {
  118. error(1, 0, NVM_HDR_CHECKSUM, filename, module);
  119. }
  120. if (LE32TOH(header->PrevHeader) != origin)
  121. {
  122. error(1, 0, NVM_HDR_LINK, filename, module);
  123. }
  124. if (fdchecksum32(fd, LE32TOH(header->ImageLength), header->ImageChecksum) && !repairchksum)
  125. {
  126. error(1, ECANCELED, NVM_IMG_CHECKSUM, filename, module);
  127. }
  128. if (LE32TOH(header->ImageType) == imagetype)
  129. {
  130. if (lseek(fd, (int)(0 - LE32TOH(header->ImageLength)), SEEK_CUR) == -1)
  131. {
  132. error(1, ECANCELED, FILE_CANTHOME, filename);
  133. }
  134. return (0);
  135. }
  136. origin = offset;
  137. offset = LE32TOH(header->NextHeader);
  138. module++;
  139. } while (~header->NextHeader);
  140. if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END))
  141. {
  142. error(1, errno, NVM_HDR_LINK, filename, module);
  143. }
  144. if (lseek(fd, 0, SEEK_SET))
  145. {
  146. error(1, errno, FILE_CANTHOME, filename);
  147. }
  148. return (-1);
  149. }
  150. #endif