PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/cc3000/src/nvmem.c

https://gitlab.com/caesar-lab/micropython
C | 334 lines | 98 code | 49 blank | 187 comment | 6 complexity | 4e55e2d05e05dcb7bc6350c5bf38e425 MD5 | raw file
  1. /*****************************************************************************
  2. *
  3. * nvmem.c - CC3000 Host Driver Implementation.
  4. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * Neither the name of Texas Instruments Incorporated nor the names of
  19. * its contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *****************************************************************************/
  35. //*****************************************************************************
  36. //
  37. //! \addtogroup nvmem_api
  38. //! @{
  39. //
  40. //*****************************************************************************
  41. #include <string.h>
  42. #include "nvmem.h"
  43. #include "hci.h"
  44. #include "socket.h"
  45. #include "evnt_handler.h"
  46. //*****************************************************************************
  47. //
  48. // Prototypes for the structures for APIs.
  49. //
  50. //*****************************************************************************
  51. #define NVMEM_READ_PARAMS_LEN (12)
  52. #define NVMEM_CREATE_PARAMS_LEN (8)
  53. #define NVMEM_WRITE_PARAMS_LEN (16)
  54. //*****************************************************************************
  55. //
  56. //! nvmem_read
  57. //!
  58. //! @param ulFileId nvmem file id:\n
  59. //! NVMEM_NVS_FILEID, NVMEM_NVS_SHADOW_FILEID,
  60. //! NVMEM_WLAN_CONFIG_FILEID, NVMEM_WLAN_CONFIG_SHADOW_FILEID,
  61. //! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
  62. //! NVMEM_MAC_FILEID, NVMEM_FRONTEND_VARS_FILEID,
  63. //! NVMEM_IP_CONFIG_FILEID, NVMEM_IP_CONFIG_SHADOW_FILEID,
  64. //! NVMEM_BOOTLOADER_SP_FILEID, NVMEM_RM_FILEID,
  65. //! and user files 12-15.
  66. //! @param ulLength number of bytes to read
  67. //! @param ulOffset ulOffset in file from where to read
  68. //! @param buff output buffer pointer
  69. //!
  70. //! @return on success 0, error otherwise.
  71. //!
  72. //! @brief Reads data from the file referred by the ulFileId parameter.
  73. //! Reads data from file ulOffset till length. Err if the file can't
  74. //! be used, is invalid, or if the read is out of bounds.
  75. //!
  76. //*****************************************************************************
  77. INT32 nvmem_read(UINT32 ulFileId, UINT32 ulLength, UINT32 ulOffset, UINT8 *buff)
  78. {
  79. UINT8 ucStatus = 0xFF;
  80. UINT8 *ptr;
  81. UINT8 *args;
  82. ptr = tSLInformation.pucTxCommandBuffer;
  83. args = (ptr + HEADERS_SIZE_CMD);
  84. // Fill in HCI packet structure
  85. args = UINT32_TO_STREAM(args, ulFileId);
  86. args = UINT32_TO_STREAM(args, ulLength);
  87. args = UINT32_TO_STREAM(args, ulOffset);
  88. // Initiate a HCI command
  89. hci_command_send(HCI_CMND_NVMEM_READ, ptr, NVMEM_READ_PARAMS_LEN);
  90. SimpleLinkWaitEvent(HCI_CMND_NVMEM_READ, &ucStatus);
  91. // In case there is data - read it - even if an error code is returned
  92. // Note: It is the user responsibility to ignore the data in case of an error code
  93. // Wait for the data in a synchronous way. Here we assume that the buffer is
  94. // big enough to store also parameters of nvmem
  95. SimpleLinkWaitData(buff, 0, 0);
  96. return(ucStatus);
  97. }
  98. //*****************************************************************************
  99. //
  100. //! nvmem_write
  101. //!
  102. //! @param ulFileId nvmem file id:\n
  103. //! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
  104. //! NVMEM_MAC_FILEID, NVMEM_BOOTLOADER_SP_FILEID,
  105. //! and user files 12-15.
  106. //! @param ulLength number of bytes to write
  107. //! @param ulEntryOffset offset in file to start write operation from
  108. //! @param buff data to write
  109. //!
  110. //! @return on success 0, error otherwise.
  111. //!
  112. //! @brief Write data to nvmem.
  113. //! writes data to file referred by the ulFileId parameter.
  114. //! Writes data to file ulOffset till ulLength.The file id will be
  115. //! marked invalid till the write is done. The file entry doesn't
  116. //! need to be valid - only allocated.
  117. //!
  118. //*****************************************************************************
  119. INT32 nvmem_write(UINT32 ulFileId, UINT32 ulLength, UINT32 ulEntryOffset, UINT8 *buff)
  120. {
  121. INT32 iRes;
  122. UINT8 *ptr;
  123. UINT8 *args;
  124. iRes = EFAIL;
  125. ptr = tSLInformation.pucTxCommandBuffer;
  126. args = (ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE);
  127. // Fill in HCI packet structure
  128. args = UINT32_TO_STREAM(args, ulFileId);
  129. args = UINT32_TO_STREAM(args, 12);
  130. args = UINT32_TO_STREAM(args, ulLength);
  131. args = UINT32_TO_STREAM(args, ulEntryOffset);
  132. memcpy((ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE +
  133. NVMEM_WRITE_PARAMS_LEN),buff,ulLength);
  134. // Initiate a HCI command but it will come on data channel
  135. hci_data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN,
  136. ulLength);
  137. SimpleLinkWaitEvent(HCI_EVNT_NVMEM_WRITE, &iRes);
  138. return(iRes);
  139. }
  140. //*****************************************************************************
  141. //
  142. //! nvmem_set_mac_address
  143. //!
  144. //! @param mac mac address to be set
  145. //!
  146. //! @return on success 0, error otherwise.
  147. //!
  148. //! @brief Write MAC address to EEPROM.
  149. //! mac address as appears over the air (OUI first)
  150. //!
  151. //*****************************************************************************
  152. UINT8 nvmem_set_mac_address(UINT8 *mac)
  153. {
  154. return nvmem_write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
  155. }
  156. //*****************************************************************************
  157. //
  158. //! nvmem_get_mac_address
  159. //!
  160. //! @param[out] mac mac address
  161. //!
  162. //! @return on success 0, error otherwise.
  163. //!
  164. //! @brief Read MAC address from EEPROM.
  165. //! mac address as appears over the air (OUI first)
  166. //!
  167. //*****************************************************************************
  168. UINT8 nvmem_get_mac_address(UINT8 *mac)
  169. {
  170. return nvmem_read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
  171. }
  172. //*****************************************************************************
  173. //
  174. //! nvmem_write_patch
  175. //!
  176. //! @param ulFileId nvmem file id:\n
  177. //! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
  178. //! @param spLength number of bytes to write
  179. //! @param spData SP data to write
  180. //!
  181. //! @return on success 0, error otherwise.
  182. //!
  183. //! @brief program a patch to a specific file ID.
  184. //! The SP data is assumed to be organized in 2-dimensional.
  185. //! Each line is SP_PORTION_SIZE bytes long. Actual programming is
  186. //! applied in SP_PORTION_SIZE bytes portions.
  187. //!
  188. //*****************************************************************************
  189. UINT8 nvmem_write_patch(UINT32 ulFileId, UINT32 spLength, const UINT8 *spData)
  190. {
  191. UINT8 status = 0;
  192. UINT16 offset = 0;
  193. UINT8* spDataPtr = (UINT8*)spData;
  194. while ((status == 0) && (spLength >= SP_PORTION_SIZE))
  195. {
  196. status = nvmem_write(ulFileId, SP_PORTION_SIZE, offset, spDataPtr);
  197. offset += SP_PORTION_SIZE;
  198. spLength -= SP_PORTION_SIZE;
  199. spDataPtr += SP_PORTION_SIZE;
  200. }
  201. if (status !=0)
  202. {
  203. // NVMEM error occurred
  204. return status;
  205. }
  206. if (spLength != 0)
  207. {
  208. // if reached here, a reminder is left
  209. status = nvmem_write(ulFileId, spLength, offset, spDataPtr);
  210. }
  211. return status;
  212. }
  213. //*****************************************************************************
  214. //
  215. //! nvmem_read_sp_version
  216. //!
  217. //! @param[out] patchVer first number indicates package ID and the second
  218. //! number indicates package build number
  219. //!
  220. //! @return on success 0, error otherwise.
  221. //!
  222. //! @brief Read patch version. read package version (WiFi FW patch,
  223. //! driver-supplicant-NS patch, bootloader patch)
  224. //!
  225. //*****************************************************************************
  226. #ifndef CC3000_TINY_DRIVER
  227. UINT8 nvmem_read_sp_version(UINT8* patchVer)
  228. {
  229. UINT8 *ptr;
  230. // 1st byte is the status and the rest is the SP version
  231. UINT8 retBuf[5];
  232. ptr = tSLInformation.pucTxCommandBuffer;
  233. // Initiate a HCI command, no args are required
  234. hci_command_send(HCI_CMND_READ_SP_VERSION, ptr, 0);
  235. SimpleLinkWaitEvent(HCI_CMND_READ_SP_VERSION, retBuf);
  236. // package ID
  237. *patchVer = retBuf[3];
  238. // package build number
  239. *(patchVer+1) = retBuf[4];
  240. return(retBuf[0]);
  241. }
  242. #endif
  243. //*****************************************************************************
  244. //
  245. //! nvmem_create_entry
  246. //!
  247. //! @param ulFileId nvmem file Id:\n
  248. //! * NVMEM_AES128_KEY_FILEID: 12
  249. //! * NVMEM_SHARED_MEM_FILEID: 13
  250. //! * and fileIDs 14 and 15
  251. //! @param ulNewLen entry ulLength
  252. //!
  253. //! @return on success 0, error otherwise.
  254. //!
  255. //! @brief Create new file entry and allocate space on the NVMEM.
  256. //! Applies only to user files.
  257. //! Modify the size of file.
  258. //! If the entry is unallocated - allocate it to size
  259. //! ulNewLen (marked invalid).
  260. //! If it is allocated then deallocate it first.
  261. //! To just mark the file as invalid without resizing -
  262. //! set ulNewLen=0.
  263. //!
  264. //*****************************************************************************
  265. INT32 nvmem_create_entry(UINT32 ulFileId, UINT32 ulNewLen)
  266. {
  267. UINT8 *ptr;
  268. UINT8 *args;
  269. UINT8 retval;
  270. ptr = tSLInformation.pucTxCommandBuffer;
  271. args = (ptr + HEADERS_SIZE_CMD);
  272. // Fill in HCI packet structure
  273. args = UINT32_TO_STREAM(args, ulFileId);
  274. args = UINT32_TO_STREAM(args, ulNewLen);
  275. // Initiate a HCI command
  276. hci_command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN);
  277. SimpleLinkWaitEvent(HCI_CMND_NVMEM_CREATE_ENTRY, &retval);
  278. return(retval);
  279. }
  280. //*****************************************************************************
  281. //
  282. // Close the Doxygen group.
  283. //! @}
  284. //
  285. //*****************************************************************************