cybtldr_command.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #ifndef __CYBTLDR_COMMAND_H__
  8. #define __CYBTLDR_COMMAND_H__
  9. #include "cybtldr_utils.h"
  10. /* Maximum number of bytes to allocate for a single command. */
  11. #define MAX_COMMAND_SIZE 512
  12. //STANDARD PACKET FORMAT:
  13. // Multi byte entries are encoded in LittleEndian.
  14. /*******************************************************************************
  15. * [1-byte] [1-byte ] [2-byte] [n-byte] [ 2-byte ] [1-byte]
  16. * [ SOP ] [Command] [ Size ] [ Data ] [Checksum] [ EOP ]
  17. *******************************************************************************/
  18. /* The first byte of any boot loader command. */
  19. #define CMD_START 0x01
  20. /* The last byte of any boot loader command. */
  21. #define CMD_STOP 0x17
  22. /* The minimum number of bytes in a bootloader command. */
  23. #define BASE_CMD_SIZE 0x07
  24. /* Command identifier for verifying the checksum value of the bootloadable project. */
  25. #define CMD_VERIFY_CHECKSUM 0x31
  26. /* Command identifier for getting the number of flash rows in the target device. */
  27. #define CMD_GET_FLASH_SIZE 0x32
  28. /* Command identifier for getting info about the app status. This is only supported on multi app bootloader. */
  29. #define CMD_GET_APP_STATUS 0x33
  30. /* Command identifier for reasing a row of flash data from the target device. */
  31. #define CMD_ERASE_ROW 0x34
  32. /* Command identifier for making sure the bootloader host and bootloader are in sync. */
  33. #define CMD_SYNC 0x35
  34. /* Command identifier for setting the active application. This is only supported on multi app bootloader. */
  35. #define CMD_SET_ACTIVE_APP 0x36
  36. /* Command identifier for sending a block of data to the bootloader without doing anything with it yet. */
  37. #define CMD_SEND_DATA 0x37
  38. /* Command identifier for starting the boot loader. All other commands ignored until this is sent. */
  39. #define CMD_ENTER_BOOTLOADER 0x38
  40. /* Command identifier for programming a single row of flash. */
  41. #define CMD_PROGRAM_ROW 0x39
  42. /* Command identifier for verifying the contents of a single row of flash. */
  43. #define CMD_VERIFY_ROW 0x3A
  44. /* Command identifier for exiting the bootloader and restarting the target program. */
  45. #define CMD_EXIT_BOOTLOADER 0x3B
  46. /*
  47. * This enum defines the different types of checksums that can be
  48. * used by the bootloader for ensuring data integrety.
  49. */
  50. typedef enum
  51. {
  52. /* Checksum type is a basic inverted summation of all bytes */
  53. SUM_CHECKSUM = 0x00,
  54. /* 16-bit CRC checksum using the CCITT implementation */
  55. CRC_CHECKSUM = 0x01,
  56. } CyBtldr_ChecksumType;
  57. /*******************************************************************************
  58. * Function Name: CyBtldr_ComputeChecksum
  59. ********************************************************************************
  60. * Summary:
  61. * Computes the 2byte checksum for the provided command data. The checksum is
  62. * the 2's complement of the 1-byte sum of all bytes.
  63. *
  64. * Parameters:
  65. * buf - The data to compute the checksum on
  66. * size - The number of bytes contained in buf.
  67. *
  68. * Returns:
  69. * The checksum for the provided data.
  70. *
  71. *******************************************************************************/
  72. unsigned short CyBtldr_ComputeChecksum(unsigned char* buf, unsigned long size);
  73. /*******************************************************************************
  74. * Function Name: CyBtldr_SetCheckSumType
  75. ********************************************************************************
  76. * Summary:
  77. * Updates what checksum algorithm is used when generating packets
  78. *
  79. * Parameters:
  80. * chksumType - The type of checksum to use when creating packets
  81. *
  82. * Returns:
  83. * NA
  84. *
  85. *******************************************************************************/
  86. void CyBtldr_SetCheckSumType(CyBtldr_ChecksumType chksumType);
  87. /*******************************************************************************
  88. * Function Name: CyBtldr_ParseDefaultCmdResult
  89. ********************************************************************************
  90. * Summary:
  91. * Parses the output from any command that returns the default result packet
  92. * data. The default result is just a status byte
  93. *
  94. * Parameters:
  95. * cmdBuf - The preallocated buffer to store command data in.
  96. * cmdSize - The number of bytes in the command.
  97. * status - The status code returned by the bootloader.
  98. *
  99. * Returns:
  100. * CYRET_SUCCESS - The command was constructed successfully
  101. * CYRET_ERR_LENGTH - The packet does not contain enough data
  102. * CYRET_ERR_DATA - The packet's contents are not correct
  103. *
  104. *******************************************************************************/
  105. int CyBtldr_ParseDefaultCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* status);
  106. /*******************************************************************************
  107. * Function Name: CyBtldr_CreateEnterBootLoaderCmd
  108. ********************************************************************************
  109. * Summary:
  110. * Creates the command used to startup the bootloader.
  111. * NB: This command must be sent before the bootloader will respond to any
  112. * other command.
  113. *
  114. * Parameters:
  115. * protect - The flash protection settings.
  116. * cmdBuf - The preallocated buffer to store command data in.
  117. * cmdSize - The number of bytes in the command.
  118. * resSize - The number of bytes expected in the bootloader's response packet.
  119. *
  120. * Returns:
  121. * CYRET_SUCCESS - The command was constructed successfully
  122. *
  123. *******************************************************************************/
  124. EXTERN int CyBtldr_CreateEnterBootLoaderCmd(unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  125. /*******************************************************************************
  126. * Function Name: CyBtldr_ParseEnterBootLoaderCmdResult
  127. ********************************************************************************
  128. * Summary:
  129. * Parses the output from the EnterBootLoader command to get the resultant
  130. * data.
  131. *
  132. * Parameters:
  133. * cmdBuf - The buffer containing the output from the bootloader.
  134. * cmdSize - The number of bytes in cmdBuf.
  135. * siliconId - The silicon ID of the device being communicated with.
  136. * siliconRev - The silicon Revision of the device being communicated with.
  137. * blVersion - The bootloader version being communicated with.
  138. * status - The status code returned by the bootloader.
  139. *
  140. * Returns:
  141. * CYRET_SUCCESS - The command was constructed successfully
  142. * CYRET_ERR_LENGTH - The packet does not contain enough data
  143. * CYRET_ERR_DATA - The packet's contents are not correct
  144. *
  145. *******************************************************************************/
  146. EXTERN int CyBtldr_ParseEnterBootLoaderCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned long* siliconId, unsigned char* siliconRev, unsigned long* blVersion, unsigned char* status);
  147. /*******************************************************************************
  148. * Function Name: CyBtldr_CreateExitBootLoaderCmd
  149. ********************************************************************************
  150. * Summary:
  151. * Creates the command used to stop communicating with the boot loader and to
  152. * trigger the target device to restart, running the new bootloadable
  153. * application.
  154. *
  155. * Parameters:
  156. * resetType - The type of reset to perform (0 = Reset, 1 = Direct Call).
  157. * cmdBuf - The preallocated buffer to store command data in.
  158. * cmdSize - The number of bytes in the command.
  159. * resSize - The number of bytes expected in the bootloader's response packet.
  160. *
  161. * Returns:
  162. * CYRET_SUCCESS - The command was constructed successfully
  163. *
  164. *******************************************************************************/
  165. EXTERN int CyBtldr_CreateExitBootLoaderCmd(unsigned char resetType, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  166. /*******************************************************************************
  167. * Function Name: CyBtldr_CreateProgramRowCmd
  168. ********************************************************************************
  169. * Summary:
  170. * Creates the command used to program a single flash row.
  171. *
  172. * Parameters:
  173. * arrayId - The array id to program.
  174. * rowNum - The row number to program.
  175. * buf - The buffer of data to program into the flash row.
  176. * size - The number of bytes in data for the row.
  177. * cmdBuf - The preallocated buffer to store command data in.
  178. * cmdSize - The number of bytes in the command.
  179. * resSize - The number of bytes expected in the bootloader's response packet.
  180. *
  181. * Returns:
  182. * CYRET_SUCCESS - The command was constructed successfully
  183. *
  184. *******************************************************************************/
  185. EXTERN int CyBtldr_CreateProgramRowCmd(unsigned char arrayId, unsigned short rowNum, unsigned char* buf, unsigned short size, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  186. /*******************************************************************************
  187. * Function Name: CyBtldr_ParseProgramRowCmdResult
  188. ********************************************************************************
  189. * Summary:
  190. * Parses the output from the ProgramRow command to get the resultant
  191. * data.
  192. *
  193. * Parameters:
  194. * cmdBuf - The preallocated buffer to store command data in.
  195. * cmdSize - The number of bytes in the command.
  196. * status - The status code returned by the bootloader.
  197. *
  198. * Returns:
  199. * CYRET_SUCCESS - The command was constructed successfully
  200. * CYRET_ERR_LENGTH - The packet does not contain enough data
  201. * CYRET_ERR_DATA - The packet's contents are not correct
  202. *
  203. *******************************************************************************/
  204. EXTERN int CyBtldr_ParseProgramRowCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* status);
  205. /*******************************************************************************
  206. * Function Name: CyBtldr_CreateVerifyRowCmd
  207. ********************************************************************************
  208. * Summary:
  209. * Creates the command used to verify that the contents of flash match the
  210. * provided row data.
  211. *
  212. * Parameters:
  213. * arrayId - The array id to verify.
  214. * rowNum - The row number to verify.
  215. * cmdBuf - The preallocated buffer to store command data in.
  216. * cmdSize - The number of bytes in the command.
  217. * resSize - The number of bytes expected in the bootloader's response packet.
  218. *
  219. * Returns:
  220. * CYRET_SUCCESS - The command was constructed successfully
  221. *
  222. *******************************************************************************/
  223. EXTERN int CyBtldr_CreateVerifyRowCmd(unsigned char arrayId, unsigned short rowNum, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  224. /*******************************************************************************
  225. * Function Name: CyBtldr_ParseVerifyRowCmdResult
  226. ********************************************************************************
  227. * Summary:
  228. * Parses the output from the VerifyRow command to get the resultant
  229. * data.
  230. *
  231. * Parameters:
  232. * cmdBuf - The preallocated buffer to store command data in.
  233. * cmdSize - The number of bytes in the command.
  234. * checksum - The checksum from the row to verify.
  235. * status - The status code returned by the bootloader.
  236. *
  237. * Returns:
  238. * CYRET_SUCCESS - The command was constructed successfully
  239. * CYRET_ERR_LENGTH - The packet does not contain enough data
  240. * CYRET_ERR_DATA - The packet's contents are not correct
  241. *
  242. *******************************************************************************/
  243. EXTERN int CyBtldr_ParseVerifyRowCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* checksum, unsigned char* status);
  244. /*******************************************************************************
  245. * Function Name: CyBtldr_CreateEraseRowCmd
  246. ********************************************************************************
  247. * Summary:
  248. * Creates the command used to erase a single flash row.
  249. *
  250. * Parameters:
  251. * arrayId - The array id to erase.
  252. * rowNum - The row number to erase.
  253. * cmdBuf - The preallocated buffer to store command data in.
  254. * cmdSize - The number of bytes in the command.
  255. * resSize - The number of bytes expected in the bootloader's response packet.
  256. *
  257. * Returns:
  258. * CYRET_SUCCESS - The command was constructed successfully
  259. *
  260. *******************************************************************************/
  261. EXTERN int CyBtldr_CreateEraseRowCmd(unsigned char arrayId, unsigned short rowNum, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  262. /*******************************************************************************
  263. * Function Name: CyBtldr_ParseEraseRowCmdResult
  264. ********************************************************************************
  265. * Summary:
  266. * Parses the output from the EraseRow command to get the resultant
  267. * data.
  268. *
  269. * Parameters:
  270. * cmdBuf - The preallocated buffer to store command data in.
  271. * cmdSize - The number of bytes in the command.
  272. * status - The status code returned by the bootloader.
  273. *
  274. * Returns:
  275. * CYRET_SUCCESS - The command was constructed successfully
  276. * CYRET_ERR_LENGTH - The packet does not contain enough data
  277. * CYRET_ERR_DATA - The packet's contents are not correct
  278. *
  279. *******************************************************************************/
  280. EXTERN int CyBtldr_ParseEraseRowCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* status);
  281. /*******************************************************************************
  282. * Function Name: CyBtldr_CreateVerifyChecksumCmd
  283. ********************************************************************************
  284. * Summary:
  285. * Creates the command used to verify that the checkusm value in flash matches
  286. * what is expected.
  287. *
  288. * Parameters:
  289. * cmdBuf - The preallocated buffer to store command data in.
  290. * cmdSize - The number of bytes in the command.
  291. * resSize - The number of bytes expected in the bootloader's response packet.
  292. *
  293. * Returns:
  294. * CYRET_SUCCESS - The command was constructed successfully
  295. *
  296. *******************************************************************************/
  297. EXTERN int CyBtldr_CreateVerifyChecksumCmd(unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  298. /*******************************************************************************
  299. * Function Name: CyBtldr_ParseVerifyChecksumCmdResult
  300. ********************************************************************************
  301. * Summary:
  302. * Parses the output from the VerifyChecksum command to get the resultant
  303. * data.
  304. *
  305. * Parameters:
  306. * cmdBuf - The preallocated buffer to store command data in.
  307. * cmdSize - The number of bytes in the command.
  308. * checksumValid - Whether or not the full checksums match (1 = valid, 0 = invalid)
  309. * status - The status code returned by the bootloader.
  310. *
  311. * Returns:
  312. * CYRET_SUCCESS - The command was constructed successfully
  313. * CYRET_ERR_LENGTH - The packet does not contain enough data
  314. * CYRET_ERR_DATA - The packet's contents are not correct
  315. *
  316. *******************************************************************************/
  317. EXTERN int CyBtldr_ParseVerifyChecksumCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* checksumValid, unsigned char* status);
  318. /*******************************************************************************
  319. * Function Name: CyBtldr_CreateGetFlashSizeCmd
  320. ********************************************************************************
  321. * Summary:
  322. * Creates the command used to retreive the number of flash rows in the device.
  323. *
  324. * Parameters:
  325. * arrayId - The array ID to get the flash size of.
  326. * cmdBuf - The preallocated buffer to store command data in.
  327. * cmdSize - The number of bytes in the command.
  328. * resSize - The number of bytes expected in the bootloader's response packet.
  329. *
  330. * Returns:
  331. * CYRET_SUCCESS - The command was constructed successfully
  332. *
  333. *******************************************************************************/
  334. EXTERN int CyBtldr_CreateGetFlashSizeCmd(unsigned char arrayId, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  335. /*******************************************************************************
  336. * Function Name: CyBtldr_ParseGetFlashSizeCmdResult
  337. ********************************************************************************
  338. * Summary:
  339. * Parses the output from the GetFlashSize command to get the resultant
  340. * data.
  341. *
  342. * Parameters:
  343. * cmdBuf - The preallocated buffer to store command data in.
  344. * cmdSize - The number of bytes in the command.
  345. * startRow - The first available row number in the flash array.
  346. * endRow - The last available row number in the flash array.
  347. * status - The status code returned by the bootloader.
  348. *
  349. * Returns:
  350. * CYRET_SUCCESS - The command was constructed successfully
  351. * CYRET_ERR_LENGTH - The packet does not contain enough data
  352. * CYRET_ERR_DATA - The packet's contents are not correct
  353. *
  354. *******************************************************************************/
  355. EXTERN int CyBtldr_ParseGetFlashSizeCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned short* startRow, unsigned short* endRow, unsigned char* status);
  356. /*******************************************************************************
  357. * Function Name: CyBtldr_CreateSendDataCmd
  358. ********************************************************************************
  359. * Summary:
  360. * Creates the command used to send a block of data to the target.
  361. *
  362. * Parameters:
  363. * buf - The buffer of data data to program into the flash row.
  364. * size - The number of bytes in data for the row.
  365. * cmdBuf - The preallocated buffer to store command data in.
  366. * cmdSize - The number of bytes in the command.
  367. * resSize - The number of bytes expected in the bootloader's response packet.
  368. *
  369. * Returns:
  370. * CYRET_SUCCESS - The command was constructed successfully
  371. *
  372. *******************************************************************************/
  373. EXTERN int CyBtldr_CreateSendDataCmd(unsigned char* buf, unsigned short size, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  374. /*******************************************************************************
  375. * Function Name: CyBtldr_ParseSendDataCmdResult
  376. ********************************************************************************
  377. * Summary:
  378. * Parses the output from the SendData command to get the resultant
  379. * data.
  380. *
  381. * Parameters:
  382. * cmdBuf - The preallocated buffer to store command data in.
  383. * cmdSize - The number of bytes in the command.
  384. * status - The status code returned by the bootloader.
  385. *
  386. * Returns:
  387. * CYRET_SUCCESS - The command was constructed successfully
  388. * CYRET_ERR_LENGTH - The packet does not contain enough data
  389. * CYRET_ERR_DATA - The packet's contents are not correct
  390. *
  391. *******************************************************************************/
  392. EXTERN int CyBtldr_ParseSendDataCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* status);
  393. /*******************************************************************************
  394. * Function Name: CyBtldr_CreateSyncBootLoaderCmd
  395. ********************************************************************************
  396. * Summary:
  397. * Creates the command used to ensure that the host application is in sync
  398. * with the bootloader application.
  399. *
  400. * Parameters:
  401. * cmdBuf - The preallocated buffer to store command data in.
  402. * cmdSize - The number of bytes in the command.
  403. * resSize - The number of bytes expected in the bootloader's response packet.
  404. *
  405. * Returns:
  406. * CYRET_SUCCESS - The command was constructed successfully
  407. *
  408. *******************************************************************************/
  409. EXTERN int CyBtldr_CreateSyncBootLoaderCmd(unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  410. /*******************************************************************************
  411. * Function Name: CyBtldr_CreateGetAppStatusCmd
  412. ********************************************************************************
  413. * Summary:
  414. * Creates the command used to get information about the application. This
  415. * command is only supported by the multi application bootloaader.
  416. *
  417. * Parameters:
  418. * appId - The id for the application to get status for
  419. * cmdBuf - The preallocated buffer to store command data in.
  420. * cmdSize - The number of bytes in the command.
  421. * resSize - The number of bytes expected in the bootloader's response packet.
  422. *
  423. * Returns:
  424. * CYRET_SUCCESS - The command was constructed successfully
  425. *
  426. *******************************************************************************/
  427. EXTERN int CyBtldr_CreateGetAppStatusCmd(unsigned char appId, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  428. /*******************************************************************************
  429. * Function Name: CyBtldr_ParseGetAppStatusCmdResult
  430. ********************************************************************************
  431. * Summary:
  432. * Parses the output from the GetAppStatus command to get the resultant
  433. * data.
  434. *
  435. * Parameters:
  436. * cmdBuf - The preallocated buffer to store command data in.
  437. * cmdSize - The number of bytes in the command.
  438. * isValid - Is the application valid.
  439. * isActive- Is the application currently marked as active.
  440. * status - The status code returned by the bootloader.
  441. *
  442. * Returns:
  443. * CYRET_SUCCESS - The command was constructed successfully
  444. * CYRET_ERR_LENGTH - The packet does not contain enough data
  445. * CYRET_ERR_DATA - The packet's contents are not correct
  446. *
  447. *******************************************************************************/
  448. EXTERN int CyBtldr_ParseGetAppStatusCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* isValid, unsigned char* isActive, unsigned char* status);
  449. /*******************************************************************************
  450. * Function Name: CyBtldr_CreateSetActiveAppCmd
  451. ********************************************************************************
  452. * Summary:
  453. * Creates the command used to set the active application for the bootloader
  454. * to run. This command is only supported by the multi application
  455. * bootloaader.
  456. *
  457. * Parameters:
  458. * appId - The id for the application to get status for
  459. * cmdBuf - The preallocated buffer to store command data in.
  460. * cmdSize - The number of bytes in the command.
  461. * resSize - The number of bytes expected in the bootloader's response packet.
  462. *
  463. * Returns:
  464. * CYRET_SUCCESS - The command was constructed successfully
  465. *
  466. *******************************************************************************/
  467. EXTERN int CyBtldr_CreateSetActiveAppCmd(unsigned char appId, unsigned char* cmdBuf, unsigned long* cmdSize, unsigned long* resSize);
  468. /*******************************************************************************
  469. * Function Name: CyBtldr_ParseSetActiveAppCmdResult
  470. ********************************************************************************
  471. * Summary:
  472. * Parses the output from the SetActiveApp command to get the resultant
  473. * data.
  474. *
  475. * Parameters:
  476. * cmdBuf - The preallocated buffer to store command data in.
  477. * cmdSize - The number of bytes in the command.
  478. * status - The status code returned by the bootloader.
  479. *
  480. * Returns:
  481. * CYRET_SUCCESS - The command was constructed successfully
  482. * CYRET_ERR_LENGTH - The packet does not contain enough data
  483. * CYRET_ERR_DATA - The packet's contents are not correct
  484. *
  485. *******************************************************************************/
  486. EXTERN int CyBtldr_ParseSetActiveAppCmdResult(unsigned char* cmdBuf, unsigned long cmdSize, unsigned char* status);
  487. #endif