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