lightning_nvm_file.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed lightning_nvm_file (struct _file_ const * file);
  11. *
  12. * validate a Thunderbolt/Lightning firmware image file;
  13. *
  14. * Contributor(s):
  15. * Charles Maier <cmaier@qca.qualcomm.com>
  16. *
  17. *--------------------------------------------------------------------*/
  18. #ifndef NVMFILE1_SOURCE
  19. #define NVMFILE1_SOURCE
  20. #include <memory.h>
  21. #include <errno.h>
  22. #include "../tools/memory.h"
  23. #include "../tools/files.h"
  24. #include "../tools/error.h"
  25. #include "../nvm/nvm.h"
  26. signed lightning_nvm_file (struct _file_ const * file)
  27. {
  28. unsigned module = 0;
  29. struct lightning_nvm_header nvm_header;
  30. if (lseek (file->file, 0, SEEK_SET))
  31. {
  32. error (1, errno, FILE_CANTHOME, file->name);
  33. }
  34. do
  35. {
  36. if (read (file->file, & nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  37. {
  38. error (1, errno, NVM_HDR_CANTREAD, file->name, module);
  39. }
  40. if (LE32TOH (nvm_header.HEADERVERSION) != 0x60000000)
  41. {
  42. error (1, errno, NVM_HDR_VERSION, file->name, module);
  43. }
  44. if (checksum32 (& nvm_header, sizeof (nvm_header), 0))
  45. {
  46. error (1, errno, NVM_HDR_CHECKSUM, file->name, module);
  47. }
  48. if (fdchecksum32 (file->file, LE32TOH (nvm_header.IMAGELENGTH), nvm_header.IMAGECHECKSUM))
  49. {
  50. error (1, errno, NVM_IMG_CHECKSUM, file->name, module);
  51. }
  52. module++;
  53. }
  54. while (nvm_header.NEXTHEADER);
  55. if (lseek (file->file, 0, SEEK_CUR) != lseek (file->file, 0, SEEK_END))
  56. {
  57. error (1, errno, NVM_HDR_LINK, file->name, module);
  58. }
  59. return (0);
  60. }
  61. #endif