cybtldr_parse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*******************************************************************************
  2. * Copyright 2011-2012, Cypress Semiconductor Corporation. All rights reserved.
  3. * You may use this file only in accordance with the license, terms, conditions,
  4. * disclaimers, and limitations in the end user license agreement accompanying
  5. * the software package with which this file was provided.
  6. ********************************************************************************/
  7. #include <string.h>
  8. #include "cybtldr_parse.h"
  9. /* Pointer to the *.cyacd file containing the data that is to be read */
  10. static FILE* dataFile;
  11. unsigned char CyBtldr_FromHex(char value)
  12. {
  13. if ('0' <= value && value <= '9')
  14. return (unsigned char)(value - '0');
  15. if ('a' <= value && value <= 'f')
  16. return (unsigned char)(10 + value - 'a');
  17. if ('A' <= value && value <= 'F')
  18. return (unsigned char)(10 + value - 'A');
  19. return 0;
  20. }
  21. int CyBtldr_FromAscii(unsigned int bufSize, unsigned char* buffer, unsigned short* rowSize, unsigned char* rowData)
  22. {
  23. unsigned short i;
  24. int err = CYRET_SUCCESS;
  25. if (bufSize & 1) // Make sure even number of bytes
  26. err = CYRET_ERR_LENGTH;
  27. else
  28. {
  29. for (i = 0; i < bufSize / 2; i++)
  30. {
  31. rowData[i] = (CyBtldr_FromHex(buffer[i * 2]) << 4) | CyBtldr_FromHex(buffer[i * 2 + 1]);
  32. }
  33. *rowSize = i;
  34. }
  35. return err;
  36. }
  37. int CyBtldr_ReadLine(unsigned int* size, char* buffer)
  38. {
  39. int err = CYRET_SUCCESS;
  40. unsigned int len = 0;
  41. if (NULL != dataFile && !feof(dataFile))
  42. {
  43. if (NULL != fgets(buffer, MAX_BUFFER_SIZE, dataFile))
  44. {
  45. len = strlen(buffer);
  46. while (len > 0 && ('\n' == buffer[len - 1] || '\r' == buffer[len - 1]))
  47. --len;
  48. }
  49. else
  50. err = CYRET_ERR_EOF;
  51. }
  52. else
  53. err = CYRET_ERR_FILE;
  54. *size = len;
  55. return err;
  56. }
  57. int CyBtldr_OpenDataFile(const char* file)
  58. {
  59. dataFile = fopen(file, "r");
  60. return (NULL == dataFile)
  61. ? CYRET_ERR_FILE
  62. : CYRET_SUCCESS;
  63. }
  64. int CyBtldr_ParseHeader(unsigned int bufSize, unsigned char* buffer, unsigned long* siliconId, unsigned char* siliconRev, unsigned char* chksum)
  65. {
  66. const unsigned int LENGTH_ID = 5; //4-silicon id, 1-silicon rev
  67. const unsigned int LENGTH_CHKSUM = LENGTH_ID + 1; //1-checksum type
  68. unsigned short rowSize;
  69. unsigned char rowData[MAX_BUFFER_SIZE];
  70. int err = CyBtldr_FromAscii(bufSize, buffer, &rowSize, rowData);
  71. if (CYRET_SUCCESS == err)
  72. {
  73. if (rowSize >= LENGTH_CHKSUM)
  74. *chksum = rowData[5];
  75. if (rowSize >= LENGTH_ID)
  76. {
  77. *siliconId = (rowData[0] << 24) | (rowData[1] << 16) | (rowData[2] << 8) | (rowData[3]);
  78. *siliconRev = rowData[4];
  79. }
  80. else
  81. err = CYRET_ERR_LENGTH;
  82. }
  83. return err;
  84. }
  85. int CyBtldr_ParseRowData(unsigned int bufSize, unsigned char* buffer, unsigned char* arrayId, unsigned short* rowNum, unsigned char* rowData, unsigned short* size, unsigned char* checksum)
  86. {
  87. const unsigned short MIN_SIZE = 6; //1-array, 2-addr, 2-size, 1-checksum
  88. const int DATA_OFFSET = 5;
  89. unsigned int i;
  90. unsigned short hexSize;
  91. unsigned char hexData[MAX_BUFFER_SIZE];
  92. int err = CYRET_SUCCESS;
  93. if (bufSize <= MIN_SIZE)
  94. err = CYRET_ERR_LENGTH;
  95. else if (buffer[0] == ':')
  96. {
  97. err = CyBtldr_FromAscii(bufSize - 1, &buffer[1], &hexSize, hexData);
  98. *arrayId = hexData[0];
  99. *rowNum = (hexData[1] << 8) | (hexData[2]);
  100. *size = (hexData[3] << 8) | (hexData[4]);
  101. *checksum = (hexData[hexSize - 1]);
  102. if ((*size + MIN_SIZE) == hexSize)
  103. {
  104. for (i = 0; i < *size; i++)
  105. {
  106. rowData[i] = (hexData[DATA_OFFSET + i]);
  107. }
  108. }
  109. else
  110. err = CYRET_ERR_DATA;
  111. }
  112. else
  113. err = CYRET_ERR_CMD;
  114. return err;
  115. }
  116. int CyBtldr_CloseDataFile(void)
  117. {
  118. int err = 0;
  119. if (NULL != dataFile)
  120. {
  121. err = fclose(dataFile);
  122. dataFile = NULL;
  123. }
  124. return (0 == err)
  125. ? CYRET_SUCCESS
  126. : CYRET_ERR_FILE;
  127. }