forked from gv1/cybootload_linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcybtldr_api.c
312 lines (262 loc) · 9.84 KB
/
cybtldr_api.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*******************************************************************************
* 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 "cybtldr_command.h"
#include "cybtldr_api.h"
/* The highest number of memory arrays for any device. This includes flash and EEPROM arrays */
#define MAX_DEV_ARRAYS 0x80
/* The default value if a flash array has not yet received data */
#define NO_FLASH_ARRAY_DATA 0
/* The maximum number of flash arrays */
#define MAX_FLASH_ARRAYS 0x40
/* The minimum array id for EEPROM arrays. */
#define MIN_EEPROM_ARRAY 0x40
unsigned long g_validRows[MAX_FLASH_ARRAYS];
static CyBtldr_CommunicationsData* g_comm;
int CyBtldr_TransferData(unsigned char* inBuf, int inSize, unsigned char* outBuf, int outSize)
{
int err = g_comm->WriteData(inBuf, inSize);
if (CYRET_SUCCESS == err)
err = g_comm->ReadData(outBuf, outSize);
if (CYRET_SUCCESS != err)
err |= CYRET_ERR_COMM_MASK;
return err;
}
int CyBtldr_ValidateRow(unsigned char arrayId, unsigned short rowNum)
{
unsigned long inSize;
unsigned long outSize;
unsigned short minRow = 0;
unsigned short maxRow = 0;
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned char status = CYRET_SUCCESS;
int err = CYRET_SUCCESS;
if (arrayId < MAX_FLASH_ARRAYS)
{
if (NO_FLASH_ARRAY_DATA == g_validRows[arrayId])
{
err = CyBtldr_CreateGetFlashSizeCmd(arrayId, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseGetFlashSizeCmdResult(outBuf, outSize, &minRow, &maxRow, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
if (CYRET_SUCCESS == err)
{
if (CYRET_SUCCESS == status)
g_validRows[arrayId] = (minRow << 16) + maxRow;
else
err = status | CYRET_ERR_BTLDR_MASK;
}
}
if (CYRET_SUCCESS == err)
{
minRow = (unsigned short)(g_validRows[arrayId] >> 16);
maxRow = (unsigned short)g_validRows[arrayId];
if (rowNum < minRow || rowNum > maxRow)
err = CYRET_ERR_ROW;
}
}
else
err = CYRET_ERR_ARRAY;
return err;
}
int CyBtldr_StartBootloadOperation(CyBtldr_CommunicationsData* comm, unsigned long expSiId, unsigned char expSiRev, unsigned long* blVer)
{
const unsigned long SUPPORTED_BOOTLOADER = 0x010000;
const unsigned long BOOTLOADER_VERSION_MASK = 0xFF0000;
unsigned long i;
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned long siliconId = 0;
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned char siliconRev = 0;
unsigned char status = CYRET_SUCCESS;
int err;
g_comm = comm;
for (i = 0; i < MAX_FLASH_ARRAYS; i++)
g_validRows[i] = NO_FLASH_ARRAY_DATA;
err = g_comm->OpenConnection();
if (CYRET_SUCCESS != err)
err |= CYRET_ERR_COMM_MASK;
if (CYRET_SUCCESS == err)
err = CyBtldr_CreateEnterBootLoaderCmd(inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseEnterBootLoaderCmdResult(outBuf, outSize, &siliconId, &siliconRev, blVer, &status);
if (CYRET_SUCCESS == err)
{
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
if (expSiId != siliconId || expSiRev != siliconRev)
err = CYRET_ERR_DEVICE;
else if ((*blVer & BOOTLOADER_VERSION_MASK) != SUPPORTED_BOOTLOADER)
err = CYRET_ERR_VERSION;
}
return err;
}
int CyBtldr_GetApplicationStatus(unsigned char appID, unsigned char* isValid, unsigned char* isActive)
{
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned char status = CYRET_SUCCESS;
int err;
err = CyBtldr_CreateGetAppStatusCmd(appID, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseGetAppStatusCmdResult(outBuf, outSize, isValid, isActive, &status);
if (CYRET_SUCCESS == err)
{
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
}
return err;
}
int CyBtldr_SetApplicationStatus(unsigned char appID)
{
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned char status = CYRET_SUCCESS;
int err;
err = CyBtldr_CreateSetActiveAppCmd(appID, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseSetActiveAppCmdResult(outBuf, outSize, &status);
if (CYRET_SUCCESS == err)
{
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
}
return err;
}
int CyBtldr_EndBootloadOperation(void)
{
unsigned long inSize;
unsigned long outSize;
unsigned char inBuf[MAX_COMMAND_SIZE];
int err = CyBtldr_CreateExitBootLoaderCmd(inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
{
err = g_comm->WriteData(inBuf, inSize);
if (CYRET_SUCCESS == err)
err = g_comm->CloseConnection();
if (CYRET_SUCCESS != err)
err |= CYRET_ERR_COMM_MASK;
}
g_comm = NULL;
return err;
}
int CyBtldr_ProgramRow(unsigned char arrayID, unsigned short rowNum, unsigned char* buf, unsigned short size)
{
const int TRANSFER_HEADER_SIZE = 11;
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned long inSize;
unsigned long outSize;
unsigned long offset = 0;
unsigned short subBufSize;
unsigned char status = CYRET_SUCCESS;
int err = CYRET_SUCCESS;
if (arrayID < MAX_FLASH_ARRAYS)
err = CyBtldr_ValidateRow(arrayID, rowNum);
//Break row into pieces to ensure we don't send too much for the transfer protocol
while ((CYRET_SUCCESS == err) && ((size - offset + TRANSFER_HEADER_SIZE) > g_comm->MaxTransferSize))
{
subBufSize = (unsigned short)(g_comm->MaxTransferSize - TRANSFER_HEADER_SIZE);
err = CyBtldr_CreateSendDataCmd(&buf[offset], subBufSize, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseSendDataCmdResult(outBuf, outSize, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
offset += subBufSize;
}
if (CYRET_SUCCESS == err)
{
subBufSize = (unsigned short)(size - offset);
err = CyBtldr_CreateProgramRowCmd(arrayID, rowNum, &buf[offset], subBufSize, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseProgramRowCmdResult(outBuf, outSize, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
}
return err;
}
int CyBtldr_EraseRow(unsigned char arrayID, unsigned short rowNum)
{
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned char status = CYRET_SUCCESS;
int err = CYRET_SUCCESS;
if (arrayID < MAX_FLASH_ARRAYS)
err = CyBtldr_ValidateRow(arrayID, rowNum);
if (CYRET_SUCCESS == err)
err = CyBtldr_CreateEraseRowCmd(arrayID, rowNum, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseEraseRowCmdResult(outBuf, outSize, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
return err;
}
int CyBtldr_VerifyRow(unsigned char arrayID, unsigned short rowNum, unsigned char checksum)
{
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned char rowChecksum = 0;
unsigned char status = CYRET_SUCCESS;
int err = CYRET_SUCCESS;
if (arrayID < MAX_FLASH_ARRAYS)
err = CyBtldr_ValidateRow(arrayID, rowNum);
if (CYRET_SUCCESS == err)
err = CyBtldr_CreateVerifyRowCmd(arrayID, rowNum, inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseVerifyRowCmdResult(outBuf, outSize, &rowChecksum, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
if ((CYRET_SUCCESS == err) && (rowChecksum != checksum))
err = CYRET_ERR_CHECKSUM;
return err;
}
int CyBtldr_VerifyApplication(void)
{
unsigned char inBuf[MAX_COMMAND_SIZE];
unsigned char outBuf[MAX_COMMAND_SIZE];
unsigned long inSize = 0;
unsigned long outSize = 0;
unsigned char checksumValid = 0;
unsigned char status = CYRET_SUCCESS;
int err = CyBtldr_CreateVerifyChecksumCmd(inBuf, &inSize, &outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_TransferData(inBuf, inSize, outBuf, outSize);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseVerifyChecksumCmdResult(outBuf, outSize, &checksumValid, &status);
if (CYRET_SUCCESS != status)
err = status | CYRET_ERR_BTLDR_MASK;
if ((CYRET_SUCCESS == err) && (!checksumValid))
err = CYRET_ERR_CHECKSUM;
return err;
}