nvm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* Copyright (C) 2019 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _NVM_H_
  13. #define _NVM_H_
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include "quec_cust_patch.h"
  21. #define MAX_SN_LEN (24)
  22. #define MAX_STATION_NUM (15)
  23. #define MAX_LAST_DESCRIPTION_LEN (32)
  24. #define MAX_STATION_NAME_LEN (10)
  25. #define IMEI_STR_LEN (15)
  26. #define IMEI_BIN_LEN (8)
  27. #define IMEISV_STR_LEN (16)
  28. #define IMEISV_BIN_LEN (9)
  29. /**
  30. * \brief text format of IMEI
  31. *
  32. * IMEI is 15 decimal characters. When it is used as string, pay attention
  33. * that the NUL character is not included.
  34. */
  35. typedef struct
  36. {
  37. char ch[IMEI_STR_LEN];
  38. } nvmImeiStr_t;
  39. /**
  40. * \brief binary format of IMEI
  41. *
  42. * The format follows 3GPP 24.008, 10.5.1.4.
  43. */
  44. typedef struct
  45. {
  46. uint8_t ch[IMEI_BIN_LEN];
  47. } nvmImeiBin_t;
  48. /**
  49. * \brief binary format of IMEISV
  50. *
  51. * The format follows 3GPP 24.008, 10.5.1.4.
  52. */
  53. typedef struct
  54. {
  55. uint8_t ch[IMEISV_BIN_LEN];
  56. } nvmImeisvBin_t;
  57. /**
  58. * \brief binary format of IMEISV
  59. *
  60. * The format follows 3GPP 24.008, 10.5.1.4.
  61. */
  62. typedef struct
  63. {
  64. uint8_t ch[IMEISV_STR_LEN];
  65. } nvmImeisvStr_t;
  66. /**
  67. * \brief NVID may be refered by other modules
  68. */
  69. enum
  70. {
  71. NVID_CALIB_PARAM = 0x002,
  72. NVID_IMEI1 = 0x005,
  73. NVID_IMEI2 = 0x179,
  74. NVID_IMEI3 = 0x186,
  75. NVID_IMEI4 = 0x1e4,
  76. NVID_SIM_LOCK_CUSTOMIZE_DATA = 0x1f0,
  77. NVID_SIM_LOCK_USER_DATA = 0x1f1,
  78. NVID_SIM_LOCK_CONTROL_KEY_ID = 0x1f2,
  79. // ims nv
  80. NVID_IMS_CSM_NV = 0x800,
  81. NVID_IMS_SAPP_NV = 0x801,
  82. NVID_IMS_ISIM_NV = 0x813,
  83. NVID_SIM_DELTA_NV = 0x870,
  84. NVID_SIM_DELTA_CARD2_NV = 0x871,
  85. NVID_PLMN_DELTA_NV = 0x872,
  86. NVID_PLMN_DELTA_CARD2_NV = 0x873,
  87. };
  88. /**
  89. * \brief phaseCheck struct
  90. */
  91. typedef struct
  92. {
  93. uint32_t Magic;
  94. uint8_t SN[MAX_SN_LEN];
  95. uint8_t SN2[MAX_SN_LEN];
  96. uint32_t StationNum; // the test station number of the testing
  97. uint8_t StationName[MAX_STATION_NUM][MAX_STATION_NAME_LEN];
  98. // uint8_t Reserved[48]; /*reserved*/
  99. // uint16_t iItem; /*part1: Bit0~ Bit_14 indicate test Station reserved*/
  100. uint8_t Reserved[13];
  101. uint8_t SignFlag; // internal flag
  102. uint8_t szLastFailDescription[MAX_LAST_DESCRIPTION_LEN];
  103. uint16_t iTestSign; // Bit0~Bit14 ---> station0~station 14 if tested. 0: tested, 1: not tested
  104. uint16_t iItem; // part1: Bit0~ Bit_14 indicate test Station, 0: Pass, 1: fail
  105. } phaseCheckHead_t;
  106. typedef struct
  107. {
  108. uint8_t addr[6];
  109. } btAddress_t;
  110. typedef struct
  111. {
  112. uint8_t engine_sn[24];
  113. uint8_t map_version[4];
  114. uint8_t activate_code[16];
  115. } gpsNVInfo_t;
  116. typedef struct
  117. {
  118. uint8_t imei1[8]; /*imei1 number 8byte*/
  119. uint8_t imei2[8]; /*imei2 number 8byte*/
  120. btAddress_t bt_add;
  121. gpsNVInfo_t gps_info;
  122. uint8_t wifi_add[6];
  123. uint8_t reserve1[2];
  124. uint8_t imei3[8]; /*imei3 number 8byte*/
  125. uint8_t imei4[8]; /*imei4 number 8byte*/
  126. uint8_t reserved[16];
  127. } refNVDirect_t;
  128. typedef struct
  129. {
  130. uint32_t ims_csm_addr;
  131. uint32_t ims_csm_len;
  132. uint32_t ims_sapp_addr;
  133. uint32_t ims_sapp_len;
  134. uint32_t ims_isim_addr;
  135. uint32_t ims_isim_len;
  136. uint32_t ims_sim1_delta_addr;
  137. uint32_t ims_sim1_delta_len;
  138. uint32_t ims_sim2_delta_addr;
  139. uint32_t ims_sim2_delta_len;
  140. uint32_t ims_plmn1_delta_addr;
  141. uint32_t ims_plmn1_delta_len;
  142. uint32_t ims_plmn2_delta_addr;
  143. uint32_t ims_plmn2_delta_len;
  144. } imsNVInfo_t;
  145. /**
  146. * \brief initialize nvm
  147. *
  148. * It should be called before any nv access.
  149. */
  150. void nvmInit(void);
  151. /**
  152. * \brief read nv item to buffer
  153. *
  154. * This will read the running data (rather than the fixed data)
  155. * of nv items.
  156. *
  157. * When \p buf is NULL or \p size is 0, it will return the size of
  158. * nv item if the nv item exists.
  159. *
  160. * \p buf should be large enough to hold the whole nv item. Otherwise,
  161. * it will return -1. That is, partial read of nv item is not supported.
  162. *
  163. * \param nvid nv ID
  164. * \param buf buffer for nv item read
  165. * \param size buffer size
  166. * \return
  167. * - nv item size on success
  168. * - -1 on fail
  169. * - invalid nvid, or not exist
  170. * - \p size is less than nv item size
  171. */
  172. int nvmReadItem(uint16_t nvid, void *buf, unsigned size);
  173. /**
  174. * \brief write nv item from buffer
  175. *
  176. * This can be used to write both cp nv and factory nv, but the behavior
  177. * is different.
  178. *
  179. * For cp nv, the nv data will be written to the file for running data.
  180. *
  181. * For factory nv, the nv data will be written to the factory nv file
  182. * directly. However, /factory is mounted as read only by default, and
  183. * ythe write will fail. If the system is in calibration mode, in which
  184. * mode /factory will be mounted, or /factory is remounted to read write,
  185. * the factory nv data is changed.
  186. *
  187. * The content will be checked before write. When the exist contents is
  188. * the same, no operation will be taken and is regarded as success.
  189. *
  190. * \param nvid nv ID
  191. * \param buf buffer for nv item write
  192. * \param size buffer size
  193. * \return
  194. * - 0 when the existed content is the same
  195. * - nv item size on changed
  196. * - -1 on fail
  197. */
  198. int nvmWriteItem(uint16_t nvid, const void *buf, unsigned size);
  199. /**
  200. * \brief read nv item to buffer, not considering running data
  201. *
  202. * Comparing to \p nvmReadItem, the difference is this won't check running
  203. * data file.
  204. *
  205. * This is only for nv maintenance. It should be initiated by PC tool, and
  206. * normal application shouldn't call this.
  207. *
  208. * \param nvid nv ID
  209. * \param buf buffer for nv item read
  210. * \param size buffer size
  211. * \return
  212. * - nv item size on success
  213. * - -1 on fail
  214. * - invalid nvid, or not exist
  215. * - \p size is less than nv item size
  216. */
  217. int nvmReadFixedItem(uint16_t nvid, void *buf, unsigned size);
  218. /**
  219. * \brief write nv item from buffer, not considering running data
  220. *
  221. * Comparing to \p nvmWriteItem, the difference is this will write to the
  222. * fixed nv file forcedly.
  223. *
  224. * This is only for nv maintenance. It should be initiated by PC tool, and
  225. * normal application shouldn't call this.
  226. *
  227. * \param nvid nv ID
  228. * \param buf buffer for nv item write
  229. * \param size buffer size
  230. * \return
  231. * - 0 when the existed content is the same
  232. * - nv item size on changed
  233. * - -1 on fail
  234. */
  235. int nvmWriteFixedItem(uint16_t nvid, const void *buf, unsigned size);
  236. /**
  237. * \brief clear running nv data
  238. *
  239. * Remove the file of nvid running nv data
  240. *
  241. * \param nvid nv ID
  242. * \return
  243. * - true when remove the nvid running nv
  244. * - false the nvid nv desc is inexistence
  245. */
  246. bool nvmClearRunningItem(uint16_t nvid);
  247. /**
  248. * \brief write nv item from buffer and update crc on nv head, only considering running data
  249. *
  250. * Comparing to \p nvmWriteItem, the difference is this will calculate data crc and write
  251. * to the nv file head, and only write running nv.
  252. *
  253. * \param nvid nv ID
  254. * \param buf buffer for nv item write
  255. * \param size buffer size
  256. * \return
  257. * - nv item size on changed
  258. * - -1 on fail
  259. */
  260. int nvmWriteHeadCrcItem(uint16_t nvid, const void *buf, unsigned size);
  261. /**
  262. * \brief write nv item from buffer and update crc on nv tail, only considering running data
  263. *
  264. * Comparing to \p nvmWriteItem, the difference is this will calculate data crc and write
  265. * to the nv file tail, and only write running nv.
  266. *
  267. * \param nvid nv ID
  268. * \param buf buffer for nv item write
  269. * \param size buffer size
  270. * \return
  271. * - nv item size on changed
  272. * - -1 on fail
  273. */
  274. int nvmWriteTailCrcItem(uint16_t nvid, const void *buf, unsigned size);
  275. /**
  276. * \brief clear all running nv data
  277. *
  278. * It is assumed that running nv data files are located under dedicated
  279. * directory. So, just remove all files under the dedicated directory
  280. * for running data to clean all running nv data.
  281. */
  282. void nvmClearRunning(void);
  283. /**
  284. * \brief read all fixed nv data and packed to bin
  285. *
  286. * The bin format follows the data format received in ResearchDownload.
  287. *
  288. * When \p size is not enough for all nv data, the buffer will be modified,
  289. * but the return value is -1.
  290. *
  291. * When \p size is larger than read nv size, the remaining will be filled
  292. * with 0xff, and the CRC at head will be the CRC of the total size.
  293. *
  294. * When \p buf is NULL or \p size is 0, it will return the size needed
  295. * for all nv data.
  296. *
  297. * \param buf buffer for packed bin
  298. * \param size buffer size
  299. * \return
  300. * - size of packed bin
  301. * - -1 on fail
  302. */
  303. int nvmReadFixedBin(void *buf, unsigned size);
  304. /**
  305. * \brief update fixednv bin by read fixednv
  306. *
  307. * This will read fixednv, and keep the fixednv bin struct. In case the
  308. * download tool will compare fixednv bin with the data of writenv, by
  309. * just compare memory, this will be useful.
  310. *
  311. * \param buf buffer for packed bin
  312. * \param size buffer size
  313. * \return
  314. * - size of packed bin
  315. * - -1 on fail
  316. */
  317. int nvmReadUpdataFixedBin(void *buf, unsigned size);
  318. #ifdef QUEC_PATCH_FACTORY_UPDATE
  319. int nvmWriteFixedBin_v2(const void *buf, unsigned size, bool parsecheck);
  320. #endif
  321. /**
  322. * \brief write fixed nv data from packed bin
  323. *
  324. * The bin format follows the data format received in ResearchDownload.
  325. *
  326. * The content will be checked before write. When the exist contents is
  327. * the same, no operation will be taken and is regarded as success.
  328. *
  329. * Also, when the nvid is production nv, and the production nv exists,
  330. * no operation will be taken and is regarded as success.
  331. *
  332. * When one item write fails, this will return -1 immediately. The
  333. * remaining data are ignored, and it is possible that some nv items
  334. * are already changed.
  335. *
  336. * \param buf buffer for packed bin
  337. * \param size buffer size
  338. * \return
  339. * - sizeof packed bin
  340. * - -1 on fail
  341. */
  342. int nvmWriteFixedBin(const void *buf, unsigned size);
  343. /**
  344. * \brief read phasecheck data
  345. *
  346. * It is not recommended to call this to access phasecheck data directly.
  347. * Rather, there are several APIs to access concrete fields of phasecheck.
  348. *
  349. * \param chk buffer packet pointer
  350. * \return
  351. * - true if read success
  352. * - false if read fail
  353. */
  354. bool nvmReadPhasecheck(phaseCheckHead_t *chk);
  355. /**
  356. * \brief write phasecheck data
  357. *
  358. * \param chk buffer packet pointer
  359. * \return
  360. * - true if read success
  361. * - false if read fail
  362. */
  363. bool nvmWritePhaseCheck(const phaseCheckHead_t *chk);
  364. /**
  365. * \brief clear PhaseCheck data
  366. *
  367. * It is dangeruous!! It will be delete PhaseCheck data.
  368. * It will unlink the file when erase PhaseCheck Flash in FDL2.
  369. */
  370. void nvmClearPhaseCheck(void);
  371. /**
  372. * \brief get nv id from file name
  373. *
  374. * This is for 8910 only. In shared memory list, each item is assigned
  375. * a file name. This file name will be used as the nv file name, not
  376. * including directory name.
  377. *
  378. * It just search the map, not check whether the nv file exists.
  379. *
  380. * \param fname nv file name
  381. * \return
  382. * - nv id
  383. * - -1 if \p fname is invalid
  384. */
  385. int nvmGetIdFromFileName(const char *fname);
  386. /**
  387. * \brief read IMEI in text format
  388. *
  389. * \param index IMEI index, 0/1
  390. * \param imei output IMEI in text format
  391. * \return
  392. * - read bytes on success, IMEI_STR_LEN
  393. * - -1 on error
  394. */
  395. int nvmReadImeiStr(int index, nvmImeiStr_t *imei);
  396. /**
  397. * \brief read IMEI in binary format
  398. *
  399. * \param index IMEI index, 0/1
  400. * \param imei output IMEI in binary format
  401. * \return
  402. * - read bytes on success, IMEI_BIN_LEN
  403. * - -1 on error
  404. */
  405. int nvmReadImeiBin(int index, nvmImeiBin_t *imei);
  406. /**
  407. * \brief read IMEISV in binary format
  408. *
  409. * \param index IMEI index, 0/1
  410. * \param imeisv output IMEISV in binary format
  411. * \return
  412. * - read bytes on success, IMEISV_BIN_LEN
  413. * - -1 on error
  414. */
  415. int nvmReadImeisvBin(int index, nvmImeisvBin_t *imei);
  416. /**
  417. * \brief read IMEISV in text format
  418. *
  419. * \param index IMEISV index, 0/1
  420. * \param imei output IMEISV in text format
  421. * \return
  422. * - read bytes on success, IMEISV_STR_LEN
  423. * - -1 on error
  424. */
  425. int nvmReadImeisvStr(int index, nvmImeisvStr_t *str);
  426. /**
  427. * \brief write IMEI in text format
  428. *
  429. * \param index IMEI index, 0/1
  430. * \param imei input IMEI in text format
  431. * \return
  432. * - IMEI_STR_LEN on success,
  433. * - -1 on error
  434. */
  435. int nvmWriteImeiStr(int index, const nvmImeiStr_t *imei);
  436. /**
  437. * \brief write IMEI in binary format
  438. *
  439. * \param index IMEI index, 0/1
  440. * \param imei input IMEI in binary format
  441. * \return
  442. * - IMEI_BIN_LEN on success,
  443. * - -1 on error
  444. */
  445. int nvmWriteImeiBin(int index, const nvmImeiBin_t *imei);
  446. /**
  447. * \brief write IMEISV in binary format
  448. *
  449. * \param index IMEI index, 0/1
  450. * \param imeisv input IMEISV in binary format
  451. * \return
  452. * - IMEISV_BIN_LEN on success,
  453. * - -1 on error
  454. */
  455. int nvmWriteImeisvBin(int index, const nvmImeisvBin_t *imei);
  456. /**
  457. * \brief default text format of IMEI
  458. *
  459. * \param index IMEI index, 0/1
  460. * \param imei output IMEI in text format
  461. */
  462. void nvmDefaultImeiStr(int index, nvmImeiStr_t *imei);
  463. /**
  464. * \brief default binary format of IMEI
  465. *
  466. * \param index IMEI index, 0/1
  467. * \param imei output IMEI in binary format
  468. */
  469. void nvmDefaultImeiBin(int index, nvmImeiBin_t *bin);
  470. /**
  471. * \brief default binary format of IMEISV
  472. *
  473. * \param index IMEI index, 0/1
  474. * \param imeisv output IMEISV in binary format
  475. */
  476. void nvmDefaultImeisvBin(int index, nvmImeisvBin_t *bin);
  477. /**
  478. * \brief read SN from phasecheck.bin
  479. *
  480. * \param index for SN1/SN2
  481. * \param nvmSn buffer packet pointer, ASCII format
  482. * \return read size ,if error return -1
  483. */
  484. int nvmReadSN(int index, void *nvmSn, int size);
  485. /**
  486. * \brief write SN to phasecheck.bin
  487. *
  488. * \param index for SN1/SN2
  489. * \param nvmSn buffer packet pointer, ASCII format
  490. * \return write size, if error return -1
  491. */
  492. int nvmWriteSN(int index, const void *nvmSn, int len);
  493. #ifdef CONFIG_SOC_8910
  494. /**
  495. * \brief read audio codec config information
  496. *
  497. * \param nvmSn audio codec struct
  498. * \return read size, if error return -1
  499. */
  500. int nvmReadAudioCodec(void *nvmSn, int size);
  501. /**
  502. * \brief write audio codec config information
  503. *
  504. * \param nvmSn audio codec struct
  505. * \return write size, if error return -1
  506. */
  507. int nvmWriteAudioCodec(void *nvmSn, int size);
  508. #endif
  509. #ifdef CONFIG_SOC_8850
  510. /**
  511. * \brief write ap tsx data information
  512. *
  513. * \param data tsx data struct
  514. * \param size tsx data size
  515. * \return write size, if error return -1
  516. */
  517. int nvmWriteTsxData(void *data, int size);
  518. /**
  519. * \brief read ap tsx data information
  520. *
  521. * \param data tsx data struct
  522. * \param size tsx data size
  523. * \return read size, if error return -1
  524. */
  525. int nvmReadTsxData(void *data, int size);
  526. #endif
  527. /**
  528. * \brief merge deltanv params to running nv
  529. * \param fname deltanv bin file name pointer
  530. * \return
  531. * - true if merge success
  532. * - false if merge fail
  533. */
  534. bool nvmMergeDeltanv(const char *fname);
  535. /**
  536. * \brief set nv data to deltanv param
  537. * \param buf deltanv bin file name pointer
  538. * \param data set nv data pointer
  539. * \return
  540. * - bytes if set data success
  541. * - -1 if set data fail
  542. */
  543. int nvmSetDeltanvParam(const char *fname, const uint8_t *data);
  544. /**
  545. * \brief get nv data from deltanv param
  546. * \param buf deltanv bin file name pointer
  547. * \param data get nv data pointer
  548. * \param len data len
  549. * \return
  550. * - bytes if get data success
  551. * - -1 if get data fail
  552. */
  553. int nvmGetDeltanvParam(const char *fname, uint8_t *data, unsigned len);
  554. #ifdef CONFIG_SOC_8850
  555. /**
  556. * \brief merge indeltanv to running nv
  557. *
  558. * it will failed when the nvid have no running nv
  559. *
  560. * \return
  561. * - true if read success
  562. * - false if read fail
  563. */
  564. bool nvmMergeIndeltaNV(void);
  565. /**
  566. * \brief get merge indeltanv flag
  567. *
  568. * \return read data, if error return -1
  569. */
  570. int nvmReadIndeltanvFlag(void);
  571. /**
  572. * \brief set merge indeltanv flag
  573. *
  574. * \param flag flag
  575. */
  576. void nvmWriteIndeltanvFlag(uint32_t flag);
  577. #endif
  578. // #ifdef CONFIG_IDENTIFY_CLOCK //quectel update
  579. /**
  580. * \brief get 26M & 32k clock config
  581. *
  582. * \return clock config
  583. */
  584. uint32_t identifyClockId(void);
  585. // #endif //quectel update
  586. #ifdef __cplusplus
  587. }
  588. #endif
  589. #endif