-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcybtldr_api2.c
177 lines (154 loc) · 6.3 KB
/
cybtldr_api2.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
/*******************************************************************************
* 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"
#include "cybtldr_command.h"
#include "cybtldr_api.h"
#include "cybtldr_api2.h"
unsigned char g_abort;
int CyBtldr_RunAction(CyBtldr_Action action, const char* file, const unsigned char* securityKey, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update)
{
const unsigned long BL_VER_SUPPORT_VERIFY = 0x010214; /* Support for full flash verify added in v2.20 of cy_boot */
const unsigned char INVALID_APP = 0xFF;
unsigned long blVer = 0;
unsigned long siliconId = 0;
unsigned short rowNum = 0;
unsigned short bufSize = 0;
unsigned char siliconRev = 0;
unsigned char chksumtype = SUM_CHECKSUM;
unsigned char checksum = 0;
unsigned char checksum2 = 0;
unsigned char arrayId = 0;
unsigned char appId;
unsigned char isValid;
unsigned char isActive;
unsigned char buffer[MAX_BUFFER_SIZE];
char line[MAX_BUFFER_SIZE];
unsigned int lineLen;
char * app;
int err;
g_abort = 0;
err = CyBtldr_OpenDataFile(file);
if (CYRET_SUCCESS == err)
{
err = CyBtldr_ReadLine(&lineLen, line);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseHeader(lineLen, line, &siliconId, &siliconRev, &chksumtype);
if (CYRET_SUCCESS == err)
{
CyBtldr_SetCheckSumType(chksumtype);
err = CyBtldr_StartBootloadOperation(comm, siliconId, siliconRev, &blVer, securityKey);
}
app = strrchr(file, '_');
appId = (app != NULL && '.' == app[2])
? (CyBtldr_FromHex(app[1]) - 1)
: INVALID_APP;
if (CYRET_SUCCESS == err && INVALID_APP != appId)
{
/* NB: This block of code will still run for single app if file */
/* name format follows same as multi app (e.g. myfile_1.cyacd) */
/* This will return error if bootloader is for single app */
err = CyBtldr_GetApplicationStatus(appId, &isValid, &isActive);
/* Active app can be verified, but not programmed or erased */
if (CYRET_SUCCESS == err && VERIFY != action && isActive)
{
/* This is multi app */
err = CYRET_ERR_ACTIVE;
}
else if (CYBTLDR_STAT_ERR_CMD == (err ^ (int)CYRET_ERR_BTLDR_MASK))
{
/* Single app - restore previous CYRET_SUCCESS */
err = CYRET_SUCCESS;
}
}
if (CYRET_SUCCESS == err)
{
while (CYRET_SUCCESS == err)
{
if (g_abort)
{
err = CYRET_ABORT;
break;
}
err = CyBtldr_ReadLine(&lineLen, line);
if (CYRET_SUCCESS == err)
err = CyBtldr_ParseRowData(lineLen, line, &arrayId, &rowNum, buffer, &bufSize, &checksum);
if (CYRET_SUCCESS == err)
{
switch (action)
{
case ERASE:
err = CyBtldr_EraseRow(arrayId, rowNum);
break;
case PROGRAM:
err = CyBtldr_ProgramRow(arrayId, rowNum, buffer, bufSize);
if (CYRET_SUCCESS != err)
break;
/* Continue on to verify the row that was programmed */
case VERIFY:
checksum2 = (unsigned char)(checksum + arrayId + rowNum + (rowNum >> 8) + bufSize + (bufSize >> 8));
err = CyBtldr_VerifyRow(arrayId, rowNum, checksum2);
break;
}
if (CYRET_SUCCESS == err && NULL != update)
update(arrayId, rowNum);
}
else if (CYRET_ERR_EOF == err)
{
err = CYRET_SUCCESS;
break;
}
}
if (CYRET_SUCCESS == err)
{
/* Set the active application to what was just programmed */
if (PROGRAM == action && INVALID_APP != appId)
{
err = CyBtldr_GetApplicationStatus(appId, &isValid, &isActive);
if (CYRET_SUCCESS == err)
{
/* If valid set the active application to what was just programmed */
/* This is multi app */
err = (0 == isValid)
? CyBtldr_SetApplicationStatus(appId)
: CYRET_ERR_CHECKSUM;
}
else if (CYBTLDR_STAT_ERR_CMD == (err ^ (int)CYRET_ERR_BTLDR_MASK))
{
/* Single app - restore previous CYRET_SUCCESS */
err = CYRET_SUCCESS;
}
}
/* Verify that the entire application is valid */
else if ((PROGRAM == action || VERIFY == action) && (blVer >= BL_VER_SUPPORT_VERIFY))
err = CyBtldr_VerifyApplication();
}
CyBtldr_EndBootloadOperation();
}
else if (CYRET_ERR_COMM_MASK != (CYRET_ERR_COMM_MASK & err))
CyBtldr_EndBootloadOperation();
CyBtldr_CloseDataFile();
}
return err;
}
int CyBtldr_Program(const char* file, const unsigned char* securityKey, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update)
{
return CyBtldr_RunAction(PROGRAM, file, securityKey, comm, update);
}
int CyBtldr_Erase(const char* file, const unsigned char* securityKey, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update)
{
return CyBtldr_RunAction(ERASE, file, securityKey, comm, update);
}
int CyBtldr_Verify(const char* file, const unsigned char* securityKey, CyBtldr_CommunicationsData* comm, CyBtldr_ProgressUpdate* update)
{
return CyBtldr_RunAction(VERIFY, file, securityKey, comm, update);
}
int CyBtldr_Abort(void)
{
g_abort = 1;
return CYRET_SUCCESS;
}