OpenBSL  v.0.1.0
OpenBSL_Int.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Copyright (c) 2013, Leo Hendrawan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the copyright holder(s) nor the names of its
13 * contributor(s) may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR(S) BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ******************************************************************************/
27 
28 /**************************************************************************/
40 #ifndef _OPEN_BSL_INT_H_
41 #define _OPEN_BSL_INT_H_
42 
43 
44 //*****************************************************************************
45 // Include section
46 //*****************************************************************************
47 
48 #include "OpenBSL_Device.h"
49 
50 //*****************************************************************************
51 // Global variable declarations
52 //*****************************************************************************
53 
54 // checksums
55 extern uint16_t OpenBSL_InChecksum, OpenBSL_OutChecksum;
56 
57 
58 //*****************************************************************************
59 // Macros (defines) and data types
60 //*****************************************************************************
61 
62 // function pointer lists based on the configuration header file
63 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_ERASE_IMAGE == true)
64  #define OPEN_BSL_FUNC_PROC_ERASE_IMAGE OpenBSL_ProcEraseImg
65 #else
66  #define OPEN_BSL_FUNC_PROC_ERASE_IMAGE NULL
67 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_ERASE_IMAGE == true)
68 
69 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_DOWNLOAD_IMAGE == true)
70  #define OPEN_BSL_FUNC_PROC_DOWNLOAD_IMAGE OpenBSL_ProcDnldImg
71 #else
72  #define OPEN_BSL_FUNC_PROC_DOWNLOAD_IMAGE NULL
73 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_DOWNLOAD_IMAGE == true)
74 
75 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_UPLOAD_IMAGE == true)
76  #define OPEN_BSL_FUNC_PROC_UPLOAD_IMAGE OpenBSL_ProcUpldImg
77 #else
78  #define OPEN_BSL_FUNC_PROC_UPLOAD_IMAGE NULL
79 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_UPLOAD_IMAGE == true)
80 
81 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_CALCULATE_CHECKSUM == true)
82  #define OPEN_BSL_FUNC_PROC_CALCULATE_CHECKSUM OpenBSL_ProcCalcChksm
83 #else
84  #define OPEN_BSL_FUNC_PROC_CALCULATE_CHECKSUM NULL
85 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_CALCULATE_CHECKSUM == true)
86 
87 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_ERASE_SEGMENT == true)
88  #define OPEN_BSL_FUNC_PROC_ERASE_SEGMENT OpenBSL_ProcEraseSegment
89 #else
90  #define OPEN_BSL_FUNC_PROC_ERASE_SEGMENT NULL
91 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_ERASE_SEGMENT == true)
92 
93 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_DOWNLOAD_SEGMENT == true)
94  #define OPEN_BSL_FUNC_PROC_CMD_DOWNLOAD_SEGMENT OpenBSL_ProcDnldSegment
95 #else
96  #define OPEN_BSL_FUNC_PROC_DOWNLOAD_SEGMENT NULL
97 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_DOWNLOAD_SEGMENT == true)
98 
99 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_UPLOAD_SEGMENT == true)
100  #define OPEN_BSL_FUNC_PROC_UPLOAD_SEGMENT OpenBSL_ProcUpldSegment
101 #else
102  #define OPEN_BSL_FUNC_PROC_UPLOAD_SEGMENT NULL
103 #endif //(OPEN_BSL_CONFIG_SUPPORT_CMD_UPLOAD_SECTION == true)
104 
105 #if (OPEN_BSL_CONFIG_PASSWORD_PROTECTED == true)
106  #define OPEN_BSL_FUNC_PROC_PASSWORD OpenBSL_ProcPwdCmd
107 #else
108  #define OPEN_BSL_FUNC_PROC_PASSWORD NULL
109 #endif //(OPEN_BSL_CONFIG_PASSWORD_PROTECTED == true)
110 
111 #if (OPEN_BSL_CONFIG_SUPPORT_CMD_JUMP_TO_ADDR == true)
112  #define OPEN_BSL_FUNC_PROC_JUMP_TO_ADDR OpenBSL_ProcJumpToAddr
113 #else
114  #define OPEN_BSL_FUNC_PROC_JUMP_TO_ADDR NULL
115 #endif //(OPEN_BSL_CONFIG_PASSWORD_PROTECTED == true)
116 
120 #define OPEN_BSL_CMD_FUNC_TABLE \
121  ENTRY(OPEN_BSL_CMD_GET_MEM_INFO = 0x00, OpenBSL_ProcGetMemInfo) \
122  ENTRY(OPEN_BSL_CMD_ERASE_IMAGE = 0x01, OPEN_BSL_FUNC_PROC_ERASE_IMAGE) \
123  ENTRY(OPEN_BSL_CMD_DOWNLOAD_IMAGE = 0x02, OPEN_BSL_FUNC_PROC_DOWNLOAD_IMAGE) \
124  ENTRY(OPEN_BSL_CMD_UPLOAD_IMAGE = 0x03, OPEN_BSL_FUNC_PROC_UPLOAD_IMAGE) \
125  ENTRY(OPEN_BSL_CMD_RUN_APP = 0x04, OpenBSL_ProcRunApp) \
126  ENTRY(OPEN_BSL_CMD_CALCULATE_CHECKSUM = 0x05, OPEN_BSL_FUNC_PROC_CALCULATE_CHECKSUM) \
127  ENTRY(OPEN_BSL_CMD_ERASE_SEGMENT = 0x06, OPEN_BSL_FUNC_PROC_ERASE_SEGMENT) \
128  ENTRY(OPEN_BSL_CMD_DOWNLOAD_SEGMENT = 0x07, OPEN_BSL_FUNC_PROC_CMD_DOWNLOAD_SEGMENT) \
129  ENTRY(OPEN_BSL_CMD_UPLOAD_SEGMENT = 0x08, OPEN_BSL_FUNC_PROC_UPLOAD_SEGMENT) \
130  ENTRY(OPEN_BSL_CMD_PASSWD = 0x09, OPEN_BSL_FUNC_PROC_PASSWORD) \
131  ENTRY(OPEN_BSL_CMD_JUMP_TO_ADDR = 0x0A, OPEN_BSL_FUNC_PROC_JUMP_TO_ADDR)
132 
133 
137 typedef enum {
138 #define ENTRY(cmd, func) cmd,
140 #undef ENTRY
141 
142  /* sync command */
143  OPEN_BSL_CMD_SYNC = 0x80
144 } OpenBSL_Cmd_t;
145 
146 
147 
151 typedef enum {
152  OPEN_BSL_RESP_OK = 0x00,
153  OPEN_BSL_RESP_ERR_UNKNOWN_CMD = 0xE1,
154  OPEN_BSL_RESP_ERR_UNSUPPORTED_CMD = 0xE2,
155  OPEN_BSL_RESP_ERR_PASSWORD_PROTECTED = 0xE3,
156  OPEN_BSL_RESP_ERR_INVALID_PARAM = 0xE4,
157  OPEN_BSL_RESP_ERR_INVALID_FORMAT = 0xE5,
158  OPEN_BSL_RESP_ERR_INVALID_CHECKSUM = 0xE6
160 
164 #define OPEN_BSL_RESP_BIT_MASK (0x80)
165 
166 
167 /* OPEN_BSL_ASSERT
168  * assert function
169  */
170 #if (OPEN_BSL_CONFIG_DEBUG == true)
171 #define OPEN_BSL_ASSERT(x) if(!(x)) OpenBSL_AssertHdl();
172 #else
173 #define OPEN_BSL_ASSERT(x)
174 #endif // (OPEN_BSL_CONFIG_DEBUG == true)
175 
176 /* BV
177  * bit value macro function
178  */
179 #ifndef BV
180 #define BV(x) (1 << x)
181 #endif
182 
186 typedef struct {
187  memAddr_t start_addr;
188  memAddr_t end_addr;
190 
191 
192 // address data size
193 #define OPEN_BSL_ADDR_DATA_SIZE (sizeof(uint32_t))
194 
195 // memory segment transfer maximum size in bytes
196 #define OPEN_BSL_MAX_SEGMENT_READ_WRITE_DATA_SIZE (32)
197 
198 // OpenBSL password length in bytes
199 #define OPEN_BSL_PASSWORD_LENGTH (8)
200 
201 // packet length
202 #define OPEN_BSL_CHKSUM_LEN (2) // 2 bytes checksum
203 
204 
205 //*****************************************************************************
206 // External function declarations
207 //*****************************************************************************
208 
209 // core functions
210 extern void OpenBSL_CalcChksum(uint8_t byte, uint16_t *checksum);
211 extern void OpenBSL_DevInit(void);
212 
213 // device/application specific functions
214 extern bool OpenBSL_EntryCheck(void);
215 extern void OpenBSL_AssertHdl(void);
216 extern void OpenBSL_Jump(memAddr_t addr);
217 
218 // core command processing functions
219 extern void OpenBSL_ProcGetMemInfo(void);
220 extern void OpenBSL_ProcEraseImg(void);
221 extern void OpenBSL_ProcDnldImg(void);
222 extern void OpenBSL_ProcUpldImg(void);
223 extern void OpenBSL_ProcCalcChksm(void);
224 extern void OpenBSL_ProcEraseSegment(void);
225 extern void OpenBSL_ProcDnldSegment(void);
226 extern void OpenBSL_ProcUpldSegment(void);
227 extern void OpenBSL_ProcPwdCmd(void);
228 extern void OpenBSL_ProcJumpToAddr(void);
229 extern void OpenBSL_ProcRunApp(void);
230 
231 // communication module functions
232 extern void OpenBSL_CommInit(void);
233 extern uint8_t OpenBSL_CommRcvByte(void);
234 extern uint8_t OpenBSL_CommRcvByteChksum(void);
235 extern uint16_t OpenBSL_CommRcvShort(void);
236 extern uint16_t OpenBSL_CommRcvShortChksum(void);
237 extern uint32_t OpenBSL_CommRcvLong(void);
238 extern uint32_t OpenBSL_CommRcvLongChksum(void);
239 extern void OpenBSL_CommSendByte(uint8_t byte);
240 extern void OpenBSL_CommSendByteChksum(uint8_t byte);
241 extern void OpenBSL_CommSendShort(uint16_t val);
242 extern void OpenBSL_CommSendShortChksum(uint16_t val);
243 extern void OpenBSL_CommSendLong(uint32_t val);
244 extern void OpenBSL_CommSendLongChksum(uint32_t val);
245 
246 // memory module functions
247 extern void OpenBSL_MemInit(void);
248 extern uint8_t OpenBSL_MemReadByte(memAddr_t address);
249 extern void OpenBSL_MemOpenForErase(uint8_t section);
250 extern void OpenBSL_MemErase(memAddr_t address);
251 extern void OpenBSL_MemCloseForErase(uint8_t section);
252 extern void OpenBSL_MemOpenForWrite(uint8_t section);
253 extern void OpenBSL_MemWrite(uint8_t byte, memAddr_t address);
254 extern void OpenBSL_MemCloseForWrite(uint8_t section);
255 extern uint8_t OpenBSL_MemChkSection(memAddr_t address, memAddr_t end);
256 extern uint32_t OpenBSL_MemSgmntGetSize(uint8_t section);
257 
258 
259 #endif /* _OPEN_BSL_INT_H_ */
void OpenBSL_ProcGetMemInfo(void)
send memory info regarding on-chip non-volatile memory
Definition: OpenBSL_Core.c:309
OpenBSL device configuration header file.
void OpenBSL_ProcPwdCmd(void)
process incoming password to unlock other commands
Definition: OpenBSL_Core.c:1330
void OpenBSL_ProcDnldSegment(void)
download/write input bytes to certain part of application image/memory area
Definition: OpenBSL_Core.c:1057
void OpenBSL_AssertHdl(void)
assert handle function
Definition: OpenBSL_Custom.c:210
void OpenBSL_MemOpenForErase(uint8_t section)
open memory for erase operation
Definition: OpenBSL_Mem.c:123
uint8_t OpenBSL_MemReadByte(memAddr_t address)
read byte from device memory
Definition: OpenBSL_Mem.c:107
void OpenBSL_ProcEraseImg(void)
erase application image/memory area
Definition: OpenBSL_Core.c:410
void OpenBSL_MemCloseForWrite(uint8_t section)
close memory for write operation
Definition: OpenBSL_Mem.c:215
uint8_t OpenBSL_CommRcvByte(void)
receive a single byte from communication line - blocking function
Definition: OpenBSL_Comm.c:112
void OpenBSL_CommSendShort(uint16_t val)
send a two bytes (16 bit) value to communication line in little endian byte order (LSB first) - block...
Definition: OpenBSL_Comm.c:326
uint8_t OpenBSL_MemChkSection(memAddr_t address, memAddr_t end)
check for validity of a given memory section parameter
Definition: OpenBSL_Mem.c:236
void OpenBSL_ProcJumpToAddr(void)
process command to jump to certain given address
Definition: OpenBSL_Core.c:1471
bool OpenBSL_EntryCheck(void)
check whether it is necessary to run BSL or application
Definition: OpenBSL_Custom.c:149
uint32_t OpenBSL_CommRcvLongChksum(void)
receive a four bytes data (32 bit) from communication line and calculate/update the OpenBSL_InChecksu...
Definition: OpenBSL_Comm.c:250
void OpenBSL_DevInit(void)
device/HW initialization function for OpenBSL
Definition: OpenBSL_Custom.c:129
void OpenBSL_MemOpenForWrite(uint8_t section)
open memory for write operation
Definition: OpenBSL_Mem.c:177
void OpenBSL_ProcCalcChksm(void)
calculate 2 checksum bytes of given memory area
Definition: OpenBSL_Core.c:813
uint16_t OpenBSL_CommRcvShort(void)
receive a two bytes data (16 bit) from communication line - blocking function
Definition: OpenBSL_Comm.c:157
uint8_t OpenBSL_CommRcvByteChksum(void)
receive a single byte from communication line and update the OpenBSL_InChecksum checksum variable - b...
Definition: OpenBSL_Comm.c:132
void OpenBSL_ProcUpldImg(void)
upload/read application image/memory area as byte streams, ended with 2 checksum bytes ...
Definition: OpenBSL_Core.c:684
uint32_t OpenBSL_MemSgmntGetSize(uint8_t section)
get device memory segment size
Definition: OpenBSL_Mem.c:266
void OpenBSL_CommSendShortChksum(uint16_t val)
send a two bytes (16 bit) to communication line and update the OpenBSL_OutChecksum checksum variable ...
Definition: OpenBSL_Comm.c:352
void OpenBSL_CommSendLong(uint32_t val)
send a four bytes (32 bit) value to communication line in little endian byte order (LSB first) - bloc...
Definition: OpenBSL_Comm.c:378
void OpenBSL_Jump(memAddr_t addr)
run application, leave BSL mode
Definition: OpenBSL_Custom.c:189
void OpenBSL_CommInit(void)
device/HW initialization communication module function for OpenBSL
Definition: OpenBSL_Comm.c:79
void OpenBSL_ProcUpldSegment(void)
upload/read bytes from certain part of application image/memory area
Definition: OpenBSL_Core.c:1214
void OpenBSL_MemErase(memAddr_t address)
erase an address/segment from device memory
Definition: OpenBSL_Mem.c:140
void OpenBSL_CommSendByteChksum(uint8_t byte)
send a single byte to communication line and update the OpenBSL_OutChecksum checksum variable - block...
Definition: OpenBSL_Comm.c:304
void OpenBSL_CalcChksum(uint8_t byte, uint16_t *checksum)
OpenBSL checksum calcuation routine - based on the simple BSD checksum algorithm. ...
Definition: OpenBSL_Core.c:194
void OpenBSL_MemInit(void)
device/HW initialization memory module function for OpenBSL
Definition: OpenBSL_Mem.c:90
void OpenBSL_ProcRunApp(void)
process command to run application (jump to application reset vector at address APP_MEM_RESET_VECT_AD...
Definition: OpenBSL_Core.c:1575
void OpenBSL_MemWrite(uint8_t byte, memAddr_t address)
write byte to device memory
Definition: OpenBSL_Mem.c:198
void OpenBSL_ProcEraseSegment(void)
erase certain part of application image/memory area
Definition: OpenBSL_Core.c:927
void OpenBSL_CommSendByte(uint8_t byte)
send a single byte to communication line - blocking function
Definition: OpenBSL_Comm.c:283
void OpenBSL_ProcDnldImg(void)
download/write incoming byte streams to application image/memory area, ended with 2 checksum bytes ...
Definition: OpenBSL_Core.c:540
Definition: OpenBSL_Int.h:186
uint32_t OpenBSL_CommRcvLong(void)
receive a four bytes data (32 bit) from communication line - blocking function
Definition: OpenBSL_Comm.c:216
void OpenBSL_CommSendLongChksum(uint32_t val)
send a four bytes (32 bit) to communication line and update the OpenBSL_OutChecksum checksum variable...
Definition: OpenBSL_Comm.c:409
OpenBSL_Resp_t
Definition: OpenBSL_Int.h:151
#define OPEN_BSL_CMD_FUNC_TABLE
Definition: OpenBSL_Int.h:120
void OpenBSL_MemCloseForErase(uint8_t section)
close memory for erase operation
Definition: OpenBSL_Mem.c:160
OpenBSL_Cmd_t
Definition: OpenBSL_Int.h:137
uint16_t OpenBSL_CommRcvShortChksum(void)
receive a two bytes data (16 bit) from communication line and calculate/update the OpenBSL_InChecksum...
Definition: OpenBSL_Comm.c:187