light-efuse-hal.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2021 Alibaba Group Holding Limited.
  4. *
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include "efuse-api.h"
  15. //#define DEBUG_INFO
  16. #ifndef ARRAY_SIZE
  17. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  18. #endif
  19. #define EFUSE_LIT_BLOCK_BIT_WIDTH 128
  20. #define EFUSE_BIG_BLOCK_BIT_WIDTH 256
  21. #define EFUSE_BYTES_PER_LIT_BLOCK (EFUSE_LIT_BLOCK_BIT_WIDTH >> 3)
  22. #define EFUSE_BYTES_PER_BIG_BLOCK (EFUSE_BIG_BLOCK_BIT_WIDTH >> 3)
  23. struct func_efuse_info {
  24. const char *func_name;
  25. unsigned int block_id;
  26. unsigned int addr; /* in byte */
  27. size_t len; /* in byte */
  28. unsigned int shift; /* left-shift bit within a byte*/
  29. unsigned int mask; /* mask within a byte */
  30. };
  31. struct func_efuse_info efuse_func_array[] = {
  32. {"UID", 5, 0x50, 20, 0, 0xff},
  33. {"USR_DSP0_JTAG_MODE", 8, 0x86, 1, 0, 0xff},
  34. {"USR_DSP1_JTAG_MODE", 8, 0x87, 1, 0, 0xff},
  35. {"USR_C910T_JTAG_MODE", 8, 0x88, 1, 0, 0xff},
  36. {"USR_C910R_JTAG_MODE", 8, 0x89, 1, 0, 0xff},
  37. {"USR_C906_JTAG_MODE", 8, 0x8a, 1, 0, 0xff},
  38. {"USR_E902_JTAG_MODE", 8, 0x8b, 1, 0, 0xff},
  39. {"USR_CHIP_DBG_MODE", 8, 0x8c, 1, 0, 0xff},
  40. {"USR_DFT_MODE", 8, 0x8d, 1, 0, 0xff},
  41. {"BOOT_OFFSET", 9, 0x90, 4, 0, 0xff},
  42. {"BOOT_INDEX", 9, 0x94, 1, 0, 0xff},
  43. {"BOOT_OFFSET_BAK", 9, 0x95, 4, 0, 0xff},
  44. {"BOOT_INDEX_BAK", 9, 0x99, 1, 0, 0xff},
  45. {"USR_USB_FASTBOOT_DIS", 9, 0x9a, 1, 4, 0x0f},
  46. {"USR_BROM_CCT_DIS", 9, 0x9a, 1, 0, 0x0f},
  47. {"IMAGE_BL2_ENC", 9, 0x9b, 1, 0, 0xff},
  48. {"IMAGE_BL3_ENC", 9, 0x9c, 1, 0, 0xff},
  49. {"IMAGE_BL4_ENC", 9, 0x9d, 1, 0, 0xff},
  50. {"BL1VERSION", 10, 0xa0, 8, 0, 0xff},
  51. {"BL2VERSION", 10, 0xa8, 8, 0, 0xff},
  52. {"SECURE_BOOT", 1, 0x10, 1, 0, 0xff},
  53. {"HASH_DEBUGPK", 25, 0x190, 32, 0, 0xff},
  54. {"BROM_DCACHE_EN", 1, 0x12, 1, 2, 0x3},
  55. {"GMAC0_MAC", 11, 0xb0, 6, 0, 0xff},
  56. {"GMAC1_MAC", 11, 0xb8, 6, 0, 0xff},
  57. {},
  58. };
  59. static const char *efuse_file = "/sys/bus/nvmem/devices/light-efuse0/nvmem";
  60. size_t efuse_read(int fd, void *buf, const char *func_name)
  61. {
  62. unsigned int offset, mask, shift;
  63. size_t len, ret;
  64. int i, block;
  65. const char *name;
  66. off_t pos;
  67. for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
  68. if (!strcmp(func_name, efuse_func_array[i].func_name))
  69. break;
  70. }
  71. if (i >= ARRAY_SIZE(efuse_func_array)) {
  72. printf("invalid efuse function name(%s)\n", name);
  73. return -EINVAL;
  74. }
  75. block = efuse_func_array[i].block_id;
  76. name = efuse_func_array[i].func_name;
  77. offset = efuse_func_array[i].addr;
  78. len = efuse_func_array[i].len;
  79. shift = efuse_func_array[i].shift;
  80. mask = efuse_func_array[i].mask;
  81. #ifdef DEBUG_INFO
  82. printf("[efuse info]: block: %d, name: %s, addr: 0x%x, len: %d mask: 0x%x\n",
  83. block, name, offset, (int)len, mask);
  84. #endif
  85. pos = lseek(fd, offset, SEEK_SET);
  86. if (pos < 0) {
  87. perror("failed to lseek offset to read");
  88. return -errno;
  89. }
  90. #ifdef DEBUG_INFO
  91. printf("efuse pos = %p\n", pos);
  92. #endif
  93. if (mask != 0xff) {
  94. unsigned char data;
  95. unsigned char rd_buf[len];
  96. ret = read(fd, rd_buf, len);
  97. if (ret != len) {
  98. #ifdef DEBUG_INFO
  99. printf("real read len: %d, expected read len: %d\n", ret, len);
  100. #endif
  101. perror("failed to read");
  102. ret = -errno;
  103. goto out;
  104. }
  105. data = (rd_buf[0] >> shift) & mask;
  106. #ifdef DEBUG_INFO
  107. printf("data = 0x%x\n", data);
  108. #endif
  109. memcpy(buf, &data, 1);
  110. if (len > 1) {
  111. len = len - 1;
  112. buf = (unsigned char *)buf + 1;
  113. memcpy(buf, &rd_buf[1], len);
  114. }
  115. } else { /* mask == 0xff */
  116. ret = read(fd, buf, len);
  117. if (ret != len) {
  118. #ifdef DEBUG_INFO
  119. printf("real read len: %d, expected read len: %d\n", ret, len);
  120. #endif
  121. perror("failed to read");
  122. ret = -errno;
  123. }
  124. }
  125. out:
  126. return ret;
  127. }
  128. size_t efuse_write(int fd, const void *buf, const char *func_name)
  129. {
  130. unsigned int offset, mask, shift;
  131. size_t len, ret;
  132. int i, block;
  133. const char *name;
  134. off_t pos;
  135. for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
  136. if (!strcmp(func_name, efuse_func_array[i].func_name))
  137. break;
  138. }
  139. if (i >= ARRAY_SIZE(efuse_func_array)) {
  140. printf("invalid efuse function name(%s)\n", name);
  141. return -EINVAL;
  142. }
  143. block = efuse_func_array[i].block_id;
  144. name = efuse_func_array[i].func_name;
  145. offset = efuse_func_array[i].addr;
  146. len = efuse_func_array[i].len;
  147. shift = efuse_func_array[i].shift;
  148. mask = efuse_func_array[i].mask;
  149. #ifdef DEBUG_INFO
  150. printf("efuse info: block: %d, name: %s, addr: 0x%x, len: %d, mask: 0x%x\n",
  151. block, name, offset, (int)len, mask);
  152. #endif
  153. pos = lseek(fd, offset, SEEK_SET);
  154. if (pos < 0) {
  155. perror("failed to lseek offset to read");
  156. return -errno;
  157. }
  158. #ifdef DEBUG_INFO
  159. printf("efuse pos = %p\n", pos);
  160. #endif
  161. if (mask != 0xff) {
  162. unsigned char data;
  163. unsigned char wr_buf[len];
  164. memcpy(wr_buf, buf, len);
  165. ret = read(fd, &data, 1);
  166. if (ret < 0) {
  167. perror("failed to read");
  168. ret = -errno;
  169. goto out;
  170. }
  171. data &= ~(mask << shift);
  172. data |= (wr_buf[0] & mask) << shift;
  173. memcpy(&wr_buf[0], &data, 1);
  174. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  175. perror("failed to lseek offset to write");
  176. ret = -errno;
  177. goto out;
  178. }
  179. ret = write(fd, wr_buf, len);
  180. if (ret != len) {
  181. perror("failed to write");
  182. ret = -errno;
  183. }
  184. } else { /* mask == 0xff */
  185. ret = write(fd, buf, len);
  186. if (ret != len) {
  187. perror("failed to write");
  188. ret = -errno;
  189. }
  190. }
  191. out:
  192. return ret;
  193. }
  194. /* according to FuseMap_v1.2.1.xlsx */
  195. size_t efuse_block_read(int fd, unsigned char *buf, unsigned int block_num)
  196. {
  197. unsigned int width, offset, bytes;
  198. size_t ret;
  199. if (block_num >= 42 && block_num <= 47)
  200. width = EFUSE_BIG_BLOCK_BIT_WIDTH;
  201. else
  202. width = EFUSE_LIT_BLOCK_BIT_WIDTH;
  203. if (block_num <= 42)
  204. offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
  205. else if (block_num > 42 && block_num <= 48)
  206. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
  207. else
  208. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
  209. (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
  210. bytes = width / 8;
  211. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  212. perror("failed to lseek offset to read");
  213. return -errno;
  214. }
  215. ret = read(fd, buf, bytes);
  216. if (ret != bytes) {
  217. perror("failed to read");
  218. ret = -errno;
  219. }
  220. return ret;
  221. }
  222. /* according to FuseMap_v1.2.1.xlsx */
  223. size_t efuse_block_write(int fd, unsigned char *buf, unsigned int block_num)
  224. {
  225. unsigned int width, offset, bytes;
  226. size_t ret;
  227. if (block_num >= 42 && block_num <= 47)
  228. width = EFUSE_BIG_BLOCK_BIT_WIDTH;
  229. else
  230. width = EFUSE_LIT_BLOCK_BIT_WIDTH;
  231. if (block_num <= 42)
  232. offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
  233. else if (block_num > 42 && block_num <= 48)
  234. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
  235. else
  236. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
  237. (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
  238. bytes = width / 8;
  239. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  240. perror("failed to lseek offset to write");
  241. return -errno;
  242. }
  243. ret = write(fd, buf, bytes);
  244. if (ret != bytes) {
  245. perror("failed to write");
  246. ret = -errno;
  247. }
  248. return ret;
  249. }
  250. /**
  251. * csi_efuse_get_chipid() - Get chip id in eFuse
  252. *
  253. * @chip_id: pointer to the buffer to store chip id
  254. *
  255. * Return: 0 on success or negative code on failure
  256. */
  257. int csi_efuse_get_chipid(void *chip_id)
  258. {
  259. int ret, fd;
  260. assert(chip_id);
  261. fd = open(efuse_file, O_RDONLY);
  262. if (fd < 0) {
  263. printf("failed to open efuse device: %s\n", efuse_file);
  264. perror("open");
  265. return -errno;
  266. }
  267. ret = efuse_read(fd, chip_id, "UID");
  268. if (ret < 0)
  269. printf("failed to get 'UID' from efuse\n");
  270. else
  271. ret = 0;
  272. close(fd);
  273. return ret;
  274. }
  275. /**
  276. * csi_efuse_get_user_dbg_mode() - Get debug mode in user area
  277. *
  278. * @type: Debug type
  279. *
  280. * @dbg_mode: pointer to the buffer store debug mode
  281. *
  282. * Return: 0 on success or negative code on failure
  283. */
  284. int csi_efuse_get_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t *dbg_mode)
  285. {
  286. int ret, fd;
  287. char *mode_name;
  288. assert(dbg_mode);
  289. if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
  290. (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
  291. (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
  292. (type != USR_CHIP_DBG) && (type != USR_DFT))
  293. return -EINVAL;
  294. switch (type) {
  295. case USR_DSP0_JTAG:
  296. mode_name = "USR_DSP0_JTAG_MODE";
  297. break;
  298. case USR_DSP1_JTAG:
  299. mode_name = "USR_DSP1_JTAG_MODE";
  300. break;
  301. case USR_C910T_JTAG:
  302. mode_name = "USR_C910T_JTAG_MODE";
  303. break;
  304. case USR_C910R_JTAG:
  305. mode_name = "USR_C910R_JTAG_MODE";
  306. break;
  307. case USR_C906_JTAG:
  308. mode_name = "USR_C906_JTAG_MODE";
  309. break;
  310. case USR_E902_JTAG:
  311. mode_name = "USR_E902_JTAG_MODE";
  312. break;
  313. case USR_CHIP_DBG:
  314. mode_name = "USR_CHIP_DBG_MODE";
  315. break;
  316. case USR_DFT:
  317. mode_name = "USR_DFT_MODE";
  318. break;
  319. }
  320. fd = open(efuse_file, O_RDONLY);
  321. if (fd < 0) {
  322. printf("failed to open efuse device: %s\n", efuse_file);
  323. return -errno;
  324. }
  325. ret = efuse_read(fd, dbg_mode, mode_name);
  326. if (ret < 0)
  327. printf("failed to get %s from efuse\n", mode_name);
  328. else
  329. ret = 0;
  330. close(fd);
  331. return ret;
  332. }
  333. /**
  334. * csi_efuse_set_user_dbg_mode() - Set debug mode in user area
  335. *
  336. * @type: Debug type
  337. *
  338. * @dbg_mode: debug mode
  339. *
  340. * Return: 0 on success or negative code on failure
  341. */
  342. int csi_efuse_set_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t dbg_mode)
  343. {
  344. int ret, fd;
  345. char *mode_name;
  346. if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
  347. (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
  348. (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
  349. (type != USR_CHIP_DBG) && (type != USR_DFT))
  350. return -EINVAL;
  351. if ((dbg_mode != DBG_MODE_ENABLE) && (dbg_mode != DBG_MODE_PWD_PROTECT) &&
  352. (dbg_mode != DBG_MODE_DISABLE))
  353. return -EINVAL;
  354. switch (type) {
  355. case USR_DSP0_JTAG:
  356. mode_name = "USR_DSP0_JTAG_MODE";
  357. break;
  358. case USR_DSP1_JTAG:
  359. mode_name = "USR_DSP1_JTAG_MODE";
  360. break;
  361. case USR_C910T_JTAG:
  362. mode_name = "USR_C910T_JTAG_MODE";
  363. break;
  364. case USR_C910R_JTAG:
  365. mode_name = "USR_C910R_JTAG_MODE";
  366. break;
  367. case USR_C906_JTAG:
  368. mode_name = "USR_C906_JTAG_MODE";
  369. break;
  370. case USR_E902_JTAG:
  371. mode_name = "USR_E902_JTAG_MODE";
  372. break;
  373. case USR_CHIP_DBG:
  374. mode_name = "USR_CHIP_DBG_MODE";
  375. break;
  376. case USR_DFT:
  377. mode_name = "USR_DFT_MODE";
  378. break;
  379. }
  380. fd = open(efuse_file, O_RDWR);
  381. if (fd < 0) {
  382. printf("failed to open efuse device: %s\n", efuse_file);
  383. return -errno;
  384. }
  385. ret = efuse_write(fd, (unsigned char *)&dbg_mode, mode_name);
  386. if (ret < 0)
  387. printf("failed to set %s into efuse\n", mode_name);
  388. else
  389. ret = 0;
  390. close(fd);
  391. return ret;
  392. }
  393. /**
  394. * csi_efuse_get_boot_offset() - Get BL1's offset in boot media
  395. *
  396. * @offset: pointer to the buffer to store BL1's offset
  397. *
  398. * Return: 0 on success or negative code on failure
  399. */
  400. int csi_efuse_get_boot_offset(unsigned int *offset)
  401. {
  402. int ret, fd;
  403. assert(offset);
  404. fd = open(efuse_file, O_RDONLY);
  405. if (fd < 0) {
  406. printf("failed to open efuse device: %s\n", efuse_file);
  407. return -errno;
  408. }
  409. ret = efuse_read(fd, offset, "BOOT_OFFSET");
  410. if (ret < 0)
  411. printf("failed to get 'BOOT_OFFSET' from efuse\n");
  412. else
  413. ret = 0;
  414. close(fd);
  415. return ret;
  416. }
  417. /**
  418. * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
  419. *
  420. * @offset: Offset value to be set
  421. *
  422. * Return: 0 on success or negative code on failure
  423. */
  424. int csi_efuse_set_boot_offset(unsigned int offset)
  425. {
  426. int ret, fd;
  427. fd = open(efuse_file, O_RDWR);
  428. if (fd < 0) {
  429. printf("failed to open efuse device: %s\n", efuse_file);
  430. return -errno;
  431. }
  432. ret = efuse_write(fd, &offset, "BOOT_OFFSET");
  433. if (ret < 0)
  434. printf("failed to set 'BOOT_OFFSET' into efuse\n");
  435. else
  436. ret = 0;
  437. close(fd);
  438. return ret;
  439. }
  440. /**
  441. * csi_efuse_get_boot_index() - Get BL1's index in boot media
  442. *
  443. * @index: pointer to the buffer to store BL1's index
  444. *
  445. * Return: 0 on success or negative code on failure
  446. */
  447. int csi_efuse_get_boot_index(unsigned char *index)
  448. {
  449. int ret, fd;
  450. assert(index);
  451. fd = open(efuse_file, O_RDONLY);
  452. if (fd < 0) {
  453. printf("failed to open efuse device: %s\n", efuse_file);
  454. return -errno;
  455. }
  456. ret = efuse_read(fd, index, "BOOT_INDEX");
  457. if (ret < 0)
  458. printf("failed to get 'BOOT_INDEX' from efuse\n");
  459. else
  460. ret = 0;
  461. close(fd);
  462. return ret;
  463. }
  464. /**
  465. * csi_efuse_set_boot_index() - Set BL1's index in boot media
  466. *
  467. * @index: index value to be set
  468. *
  469. * Return: 0 on success or negative code on failure
  470. */
  471. int csi_efuse_set_boot_index(const unsigned char index)
  472. {
  473. int ret, fd;
  474. fd = open(efuse_file, O_RDWR);
  475. if (fd < 0) {
  476. printf("failed to open efuse device: %s\n", efuse_file);
  477. return -errno;
  478. }
  479. ret = efuse_write(fd, &index, "BOOT_INDEX");
  480. if (ret < 0)
  481. printf("failed to set 'BOOT_INDEX' into efuse\n");
  482. else
  483. ret = 0;
  484. close(fd);
  485. return ret;
  486. }
  487. /**
  488. * csi_efuse_get_bak_boot_offset() - Get BL1's offset in boot media
  489. *
  490. * @offset: pointer to the buffer to store BL1's offset
  491. *
  492. * Return: 0 on success or negative code on failure
  493. */
  494. int csi_efuse_get_bak_boot_offset(unsigned int *offset)
  495. {
  496. int ret, fd;
  497. assert(offset);
  498. fd = open(efuse_file, O_RDONLY);
  499. if (fd < 0) {
  500. printf("failed to open efuse device: %s\n", efuse_file);
  501. return -errno;
  502. }
  503. ret = efuse_read(fd, offset, "BOOT_OFFSET_BAK");
  504. if (ret < 0)
  505. printf("failed to get 'BOOT_OFFSET_BAK' from efuse\n");
  506. else
  507. ret = 0;
  508. close(fd);
  509. return ret;
  510. }
  511. /**
  512. * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
  513. *
  514. * @offset: Offset value to be set
  515. *
  516. * Return: 0 on success or negative code on failure
  517. */
  518. int csi_efuse_set_bak_boot_offset(unsigned int offset)
  519. {
  520. int ret, fd;
  521. fd = open(efuse_file, O_RDWR);
  522. if (fd < 0) {
  523. printf("failed to open efuse device: %s\n", efuse_file);
  524. return -errno;
  525. }
  526. ret = efuse_write(fd, &offset, "BOOT_OFFSET_BAK");
  527. if (ret < 0)
  528. printf("failed to set 'BOOT_OFFSET_BAK' into efuse\n");
  529. else
  530. ret = 0;
  531. close(fd);
  532. return ret;
  533. }
  534. /**
  535. * csi_efuse_get_bak_boot_index() - Get BL1's index in boot media
  536. *
  537. * @index: pointer to the buffer to store BL1's index
  538. *
  539. * Return: 0 on success or negative code on failure
  540. */
  541. int csi_efuse_get_bak_boot_index(unsigned char *index)
  542. {
  543. int ret, fd;
  544. assert(index);
  545. fd = open(efuse_file, O_RDONLY);
  546. if (fd < 0) {
  547. printf("failed to open efuse device: %s\n", efuse_file);
  548. return -errno;
  549. }
  550. ret = efuse_read(fd, index, "BOOT_INDEX_BAK");
  551. if (ret < 0)
  552. printf("failed to get 'BOOT_INDEX_BAK' from efuse\n");
  553. else
  554. ret = 0;
  555. close(fd);
  556. return ret;
  557. }
  558. /**
  559. * csi_efuse_set_bak_boot_index() - Set BL1's index in boot media
  560. *
  561. * @index: index value to be set
  562. *
  563. * Return: 0 on success or negative code on failure
  564. */
  565. int csi_efuse_set_bak_boot_index(unsigned char index)
  566. {
  567. int ret, fd;
  568. fd = open(efuse_file, O_RDWR);
  569. if (fd < 0) {
  570. printf("failed to open efuse device: %s\n", efuse_file);
  571. return -errno;
  572. }
  573. ret = efuse_write(fd, &index, "BOOT_INDEX_BAK");
  574. if (ret < 0)
  575. printf("failed to set 'BOOT_INDEX_BAK' into efuse\n");
  576. else
  577. ret = 0;
  578. close(fd);
  579. return ret;
  580. }
  581. /**
  582. * csi_efuse_get_usr_brom_usb_fastboot_st() - Get bootrom USB fastboots tatus in user area
  583. *
  584. * @status: pointer to the buffer to store USB fastboot status
  585. *
  586. * Return: 0 on success or negative code on failure
  587. */
  588. int csi_efuse_get_usr_brom_usb_fastboot_st(brom_usbboot_st_t *status)
  589. {
  590. int ret, fd;
  591. unsigned char tempdata;
  592. assert(status);
  593. fd = open(efuse_file, O_RDONLY);
  594. if (fd < 0) {
  595. printf("failed to open efuse device: %s\n", efuse_file);
  596. return -errno;
  597. }
  598. ret = efuse_read(fd, &tempdata, "USR_USB_FASTBOOT_DIS");
  599. if (ret < 0)
  600. printf("failed to get 'USR_USB_FASTBOOT_DIS' from efuse\n");
  601. else
  602. ret = 0;
  603. if (tempdata == 0xa)
  604. *status = BROM_USBBOOT_DIS;
  605. else if (tempdata != BROM_USBBOOT_EN)
  606. ret = -EINVAL;
  607. else
  608. *status = BROM_USBBOOT_EN;
  609. close(fd);
  610. return ret;
  611. }
  612. /**
  613. * csi_efuse_dis_usr_brom_usb_fastboot() - Disable bootrom USB fastboot status in user area
  614. *
  615. * Return: 0 on success or negative code on failure
  616. */
  617. int csi_efuse_dis_usr_brom_usb_fastboot(void)
  618. {
  619. int ret, fd;
  620. unsigned char status = 0xA; /* disable value */
  621. fd = open(efuse_file, O_RDWR);
  622. if (fd < 0) {
  623. printf("failed to open efuse device: %s\n", efuse_file);
  624. return -errno;
  625. }
  626. ret = efuse_write(fd, &status, "USR_USB_FASTBOOT_DIS");
  627. if (ret < 0)
  628. printf("failed to set 'USR_USB_FASTBOOT_DIS' status into efuse\n");
  629. else
  630. ret = 0;
  631. close(fd);
  632. return ret;
  633. }
  634. /**
  635. * csi_efuse_get_usr_brom_cct_st() - Get bootrom CCT status in user area
  636. *
  637. * @status: pointer to the buffer to store CCT
  638. *
  639. * Return: 0 on success or negative code on failure
  640. */
  641. int csi_efuse_get_usr_brom_cct_st(brom_cct_st_t *status)
  642. {
  643. int ret, fd;
  644. unsigned char tempdata;
  645. assert(status);
  646. fd = open(efuse_file, O_RDONLY);
  647. if (fd < 0) {
  648. printf("failed to open efuse device: %s\n", efuse_file);
  649. return -errno;
  650. }
  651. ret = efuse_read(fd, &tempdata, "USR_BROM_CCT_DIS");
  652. if (ret < 0)
  653. printf("failed to get 'USR_BROM_CCT_DIS' from efuse\n");
  654. else
  655. ret = 0;
  656. if (tempdata == 0xa)
  657. *status = BROM_CCT_DIS;
  658. else if(tempdata != BROM_CCT_EN)
  659. ret = -EINVAL;
  660. else
  661. *status = BROM_CCT_EN;
  662. close(fd);
  663. return ret;
  664. }
  665. /**
  666. * csi_efuse_dis_usr_brom_cct() - Disable bootrom CCT status in user area
  667. *
  668. * Return: 0 on success or negative code on failure
  669. */
  670. int csi_efuse_dis_usr_brom_cct(void)
  671. {
  672. int ret, fd;
  673. unsigned char status = 0xA; /* disable value */
  674. fd = open(efuse_file, O_RDWR);
  675. if (fd < 0) {
  676. printf("failed to open efuse device: %s\n", efuse_file);
  677. return -errno;
  678. }
  679. ret = efuse_write(fd, &status, "USR_BROM_CCT_DIS");
  680. if (ret < 0)
  681. printf("failed to set 'USR_BROM_CCT_DIS' status into efuse\n");
  682. else
  683. ret = 0;
  684. close(fd);
  685. return ret;
  686. }
  687. /**
  688. * csi_efuse_get_bl2_img_encrypt_st() - Get BL2 image encryption flag
  689. *
  690. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  691. *
  692. * Return: 0 on success or negative code on failure
  693. */
  694. int csi_efuse_get_bl2_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  695. {
  696. int ret, fd;
  697. unsigned char tempdata;
  698. assert(encrypt_flag);
  699. fd = open(efuse_file, O_RDONLY);
  700. if (fd < 0) {
  701. printf("failed to open efuse device: %s\n", efuse_file);
  702. return -errno;
  703. }
  704. ret = efuse_read(fd, &tempdata, "IMAGE_BL2_ENC");
  705. if (ret < 0)
  706. printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
  707. else
  708. ret = 0;
  709. if (tempdata == 0x5a)
  710. *encrypt_flag = IMAGE_ENCRYPT_EN;
  711. else if (tempdata != IMAGE_ENCRYPT_DIS)
  712. ret = -EINVAL;
  713. else
  714. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  715. close(fd);
  716. return ret;
  717. }
  718. /**
  719. * csi_efuse_set_bl2_img_encrypt_st() - Set BL2 image encryption flag
  720. *
  721. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  722. *
  723. * Return: 0 on success or negative code on failure
  724. */
  725. int csi_efuse_set_bl2_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  726. {
  727. int ret, fd;
  728. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  729. return -EINVAL;
  730. fd = open(efuse_file, O_RDWR);
  731. if (fd < 0) {
  732. printf("failed to open efuse device: %s\n", efuse_file);
  733. return -errno;
  734. }
  735. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  736. encrypt_flag = 0x5a;
  737. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL2_ENC");
  738. if (ret < 0)
  739. printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
  740. else
  741. ret = 0;
  742. close(fd);
  743. return ret;
  744. }
  745. /**
  746. * csi_efuse_get_bl3_img_encrypt_st() - Get BL3 image encryption flag
  747. *
  748. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  749. *
  750. * Return: 0 on success or negative code on failure
  751. */
  752. int csi_efuse_get_bl3_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  753. {
  754. int ret, fd;
  755. unsigned char tempdata;
  756. assert(encrypt_flag);
  757. fd = open(efuse_file, O_RDONLY);
  758. if (fd < 0) {
  759. printf("failed to open efuse device: %s\n", efuse_file);
  760. return -errno;
  761. }
  762. ret = efuse_read(fd, &tempdata, "IMAGE_BL3_ENC");
  763. if (ret < 0)
  764. printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
  765. else
  766. ret = 0;
  767. if (tempdata == 0x5a)
  768. *encrypt_flag = IMAGE_ENCRYPT_EN;
  769. else if (tempdata != IMAGE_ENCRYPT_DIS)
  770. ret = -EINVAL;
  771. else
  772. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  773. close(fd);
  774. return ret;
  775. }
  776. /**
  777. * csi_efuse_set_bl3_img_encrypt_st() - Set BL3 image encryption flag
  778. *
  779. * @encrypt_flag: pointer to the buffer to store BL3 encryption flag
  780. *
  781. * Return: 0 on success or negative code on failure
  782. */
  783. int csi_efuse_set_bl3_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  784. {
  785. int ret, fd;
  786. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  787. return -EINVAL;
  788. fd = open(efuse_file, O_RDWR);
  789. if (fd < 0) {
  790. printf("failed to open efuse device: %s\n", efuse_file);
  791. return -errno;
  792. }
  793. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  794. encrypt_flag = 0x5a;
  795. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL3_ENC");
  796. if (ret < 0)
  797. printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
  798. else
  799. ret = 0;
  800. close(fd);
  801. return ret;
  802. }
  803. /**
  804. * csi_efuse_get_bl4_img_encrypt_st() - Get BL4 image encryption flag
  805. *
  806. * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
  807. *
  808. * Return: 0 on success or negative code on failure
  809. */
  810. int csi_efuse_get_bl4_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  811. {
  812. int ret, fd;
  813. unsigned char tempdata;
  814. assert(encrypt_flag);
  815. fd = open(efuse_file, O_RDONLY);
  816. if (fd < 0) {
  817. printf("failed to open efuse device: %s\n", efuse_file);
  818. return -errno;
  819. }
  820. ret = efuse_read(fd, &tempdata, "IMAGE_BL4_ENC");
  821. if (ret < 0)
  822. printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
  823. else
  824. ret = 0;
  825. if (tempdata == 0x5a)
  826. *encrypt_flag = IMAGE_ENCRYPT_EN;
  827. else if (tempdata != IMAGE_ENCRYPT_DIS)
  828. ret = -EINVAL;
  829. else
  830. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  831. close(fd);
  832. return ret;
  833. }
  834. /**
  835. * csi_efuse_set_bl4_img_encrypt_st() - Set BL4 image encryption flag
  836. *
  837. * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
  838. *
  839. * Return: 0 on success or negative code on failure
  840. */
  841. int csi_efuse_set_bl4_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  842. {
  843. int ret, fd;
  844. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  845. return -EINVAL;
  846. fd = open(efuse_file, O_RDWR);
  847. if (fd < 0) {
  848. printf("failed to open efuse device: %s\n", efuse_file);
  849. return -errno;
  850. }
  851. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  852. encrypt_flag = 0x5a;
  853. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL4_ENC");
  854. if (ret < 0)
  855. printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
  856. else
  857. ret = 0;
  858. close(fd);
  859. return ret;
  860. }
  861. /**
  862. * csi_efuse_get_bl1_version() - Get BL1 version
  863. *
  864. * @version: pointer to the buffer to store BL1's version in eFuse
  865. *
  866. * Return: 0 on success or negative code on failure
  867. */
  868. int csi_efuse_get_bl1_version(unsigned long long *version)
  869. {
  870. int ret, fd;
  871. assert(version);
  872. fd = open(efuse_file, O_RDONLY);
  873. if (fd < 0) {
  874. printf("failed to open efuse device: %s\n", efuse_file);
  875. return -errno;
  876. }
  877. ret = efuse_read(fd, version, "BL1VERSION");
  878. if (ret < 0)
  879. printf("failed to get 'BL1VERSION' from efuse\n");
  880. else
  881. ret = 0;
  882. close(fd);
  883. return ret;
  884. }
  885. /**
  886. * csi_efuse_set_bl1_version() - Set BL1 version
  887. *
  888. * @version: pointer to the buffer to store BL1's version in eFuse
  889. *
  890. * Return: 0 on success or negative code on failure
  891. */
  892. int csi_efuse_set_bl1_version(unsigned long long version)
  893. {
  894. int ret, fd;
  895. fd = open(efuse_file, O_RDWR);
  896. if (fd < 0) {
  897. printf("failed to open efuse device: %s\n", efuse_file);
  898. return -errno;
  899. }
  900. ret = efuse_write(fd, &version, "BL1VERSION");
  901. if (ret < 0)
  902. printf("failed to set 'BL1VERSION' into efuse\n");
  903. else
  904. ret = 0;
  905. close(fd);
  906. return ret;
  907. }
  908. /**
  909. * csi_efuse_get_bl2_version() - Get BL2 version
  910. *
  911. * @version: pointer to the buffer to store BL2's version in eFuse
  912. *
  913. * Return: 0 on success or negative code on failure
  914. */
  915. int csi_efuse_get_bl2_version(unsigned long long *version)
  916. {
  917. int ret, fd;
  918. assert(version);
  919. fd = open(efuse_file, O_RDONLY);
  920. if (fd < 0) {
  921. printf("failed to open efuse device: %s\n", efuse_file);
  922. return -errno;
  923. }
  924. ret = efuse_read(fd, version, "BL2VERSION");
  925. if (ret < 0)
  926. printf("failed to get 'BL2VERSION' from efuse\n");
  927. else
  928. ret = 0;
  929. close(fd);
  930. return ret;
  931. }
  932. /**
  933. * csi_efuse_set_bl2_version() - Set BL2 version
  934. *
  935. * @version: pointer to the buffer to store BL2's version in eFuse
  936. *
  937. * Return: 0 on success or negative code on failure
  938. */
  939. int csi_efuse_set_bl2_version(unsigned long long version)
  940. {
  941. int ret, fd;
  942. fd = open(efuse_file, O_RDWR);
  943. if (fd < 0) {
  944. printf("failed to open efuse device: %s\n", efuse_file);
  945. return -errno;
  946. }
  947. ret = efuse_write(fd, &version, "BL2VERSION");
  948. if (ret < 0)
  949. printf("failed to set 'BL2VERSION' into efuse\n");
  950. else
  951. ret = 0;
  952. close(fd);
  953. return ret;
  954. }
  955. /**
  956. * csi_efuse_get_secure_boot_st() - Get seucre boot flag
  957. *
  958. * @sboot_flag: A pointer to the buffer to store secure boot flag
  959. *
  960. * Return: 0 on success or negative code on failure
  961. */
  962. int csi_efuse_get_secure_boot_st(sboot_st_t *sboot_flag)
  963. {
  964. int ret, fd;
  965. unsigned char tempdata;
  966. assert(sboot_flag);
  967. fd = open(efuse_file, O_RDONLY);
  968. if (fd < 0) {
  969. printf("failed to open efuse device: %s\n", efuse_file);
  970. return -errno;
  971. }
  972. ret = efuse_read(fd, &tempdata, "SECURE_BOOT");
  973. if (ret < 0)
  974. printf("failed to get 'SECURE_BOOT' from efuse\n");
  975. else
  976. ret = 0;
  977. if (tempdata == 0x5a)
  978. *sboot_flag = SECURE_BOOT_EN;
  979. else if (tempdata != SECURE_BOOT_DIS)
  980. ret = -EINVAL;
  981. else
  982. *sboot_flag = SECURE_BOOT_DIS;
  983. close(fd);
  984. return ret;
  985. }
  986. /**
  987. * csi_efuse_get_hash_challenge() - Get hash challenge in eFuse
  988. *
  989. * @hash_resp: pointer to the buffer to store hash response
  990. *
  991. * Return: 0 on success or negative code on failure
  992. */
  993. int csi_efuse_get_hash_challenge(void * hash_resp)
  994. {
  995. int ret, fd;
  996. assert(hash_resp);
  997. fd = open(efuse_file, O_RDONLY);
  998. if (fd < 0) {
  999. printf("failed to open efuse device: %s\n", efuse_file);
  1000. return -errno;
  1001. }
  1002. ret = efuse_read(fd, hash_resp, "HASH_DEBUGPK");
  1003. if (ret < 0)
  1004. printf("failed to get 'HASH_DEBUGPK' from efuse\n");
  1005. else
  1006. ret = 0;
  1007. close(fd);
  1008. return ret;
  1009. }
  1010. /**
  1011. * csi_efuse_get_userdata_group() - Get user data in corresponding eFuse block
  1012. *
  1013. * @key: pointer to the buffer to store user data
  1014. * @block_num: the block number in eFuse
  1015. *
  1016. * Return: 0 on success or negative code on failure
  1017. */
  1018. int csi_efuse_get_userdata_group(unsigned char *key, unsigned char block_num)
  1019. {
  1020. int ret, fd;
  1021. assert(key);
  1022. if (block_num >= 58)
  1023. return -EINVAL;
  1024. fd = open(efuse_file, O_RDONLY);
  1025. if (fd < 0) {
  1026. printf("failed to open efuse device: %s\n", efuse_file);
  1027. return -errno;
  1028. }
  1029. ret = efuse_block_read(fd, key, block_num);
  1030. if (ret < 0)
  1031. printf("failed to get block%d data from efuse\n", block_num);
  1032. else
  1033. ret = 0;
  1034. close(fd);
  1035. return ret;
  1036. }
  1037. /**
  1038. * csi_efuse_set_userdata_group() - Set user data corresponding eFuse block
  1039. *
  1040. * @key: pointer to the buffer to store user data
  1041. * @block_num: the block number in eFuse
  1042. *
  1043. * Return: 0 on success or negative code on failure
  1044. */
  1045. int csi_efuse_set_userdata_group(unsigned char *key, unsigned char block_num)
  1046. {
  1047. int ret, fd;
  1048. assert(key);
  1049. if (block_num > 58) {
  1050. printf("the block number is out of the scop \n");
  1051. return -EINVAL;
  1052. }
  1053. fd = open(efuse_file, O_RDWR);
  1054. if (fd < 0) {
  1055. printf("failed to open efuse device: %s\n", efuse_file);
  1056. return -errno;
  1057. }
  1058. ret = efuse_block_write(fd, key, block_num);
  1059. if (ret < 0)
  1060. printf("failed to set block%d data into efuse\n", block_num);
  1061. else
  1062. ret = 0;
  1063. close(fd);
  1064. return ret;
  1065. }
  1066. /**
  1067. * csi_efuse_read() - Read data from eFuse
  1068. *
  1069. * @offset: offset address
  1070. * @data: Pointer to a buffer storing the data read from eFuse
  1071. * @cnt: Number of bytes need to be read
  1072. *
  1073. * Return: number of data items read or error code
  1074. */
  1075. int csi_efuse_read(unsigned int offset, void *data, unsigned int cnt)
  1076. {
  1077. int ret, fd;
  1078. assert(data);
  1079. fd = open(efuse_file, O_RDONLY);
  1080. if (fd < 0) {
  1081. printf("failed to open efuse device: %s\n", efuse_file);
  1082. return -errno;
  1083. }
  1084. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  1085. perror("failed to lseek offset to read");
  1086. close(fd);
  1087. return -errno;
  1088. }
  1089. ret = read(fd, data, cnt);
  1090. if (ret < 0) {
  1091. perror("failed to read data from efuse");
  1092. ret = -errno;
  1093. }
  1094. close(fd);
  1095. return ret;
  1096. }
  1097. /**
  1098. * csi_efuse_write() - Write data to eFuse
  1099. *
  1100. * @offset: offset address
  1101. * @data: Pointer to a buffer storing the data write to eFuse
  1102. * @cnt: Number of bytes need to be write
  1103. *
  1104. * Return: number of data items write or error code
  1105. */
  1106. int csi_efuse_write(unsigned int offset, void *data, unsigned int cnt)
  1107. {
  1108. int ret, fd;
  1109. assert(data);
  1110. fd = open(efuse_file, O_RDWR);
  1111. if (fd < 0) {
  1112. printf("failed to open efuse device: %s\n", efuse_file);
  1113. return -errno;
  1114. }
  1115. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  1116. perror("failed to lseek offset to write");
  1117. close(fd);
  1118. return -errno;
  1119. }
  1120. ret = write(fd, data, cnt);
  1121. if (ret < 0) {
  1122. perror("failed to write data to efuse");
  1123. ret = -errno;
  1124. }
  1125. close(fd);
  1126. return ret;
  1127. }
  1128. /**
  1129. * csi_dbg_enable_c910t_jtag() - Enable C910 TEE core jtag
  1130. *
  1131. * Return: 0 on success or negative code on failure
  1132. */
  1133. int csi_dbg_enable_c910t_jtag(void)
  1134. {
  1135. }
  1136. /**
  1137. * csi_dbg_disable_c910t_jtag() - Disable C910 TEE core jtag
  1138. *
  1139. * Return: 0 on success or negative code on failure
  1140. */
  1141. int csi_dbg_disable_c910t_jtag(void)
  1142. {
  1143. }
  1144. /**
  1145. * csi_efuse_get_gmac_macaddr() - Get gmac0/gmac1 mac address in eFuse
  1146. * @dev_id: '0' means gmac0, '1' means gmac1
  1147. * @mac: the mac address string
  1148. *
  1149. * Return: 0: Success others: Failed
  1150. */
  1151. int csi_efuse_get_gmac_macaddr(int dev_id, unsigned char *mac)
  1152. {
  1153. int ret, fd;
  1154. char gmac_name[12] = {};
  1155. assert(mac);
  1156. if ((dev_id != 0) && (dev_id != 1))
  1157. return -EINVAL;
  1158. fd = open(efuse_file, O_RDONLY);
  1159. if (fd < 0) {
  1160. printf("failed to open efuse device: %s\n", efuse_file);
  1161. return -errno;
  1162. }
  1163. sprintf(gmac_name, "GMAC%d_MAC", dev_id);
  1164. ret = efuse_read(fd, mac, gmac_name);
  1165. if (ret < 0)
  1166. printf("failed to get %s\n", gmac_name);
  1167. else
  1168. ret = 0;
  1169. close(fd);
  1170. return ret;
  1171. }
  1172. /**
  1173. * csi_efuse_set_gmac_macaddr() - Set gmac0/gmac1 mac address in eFuse
  1174. * @dev_id: '0' means gmac0, '1' means gmac1
  1175. * @mac: the mac address string
  1176. *
  1177. * Return: 0: Success others: Failed
  1178. */
  1179. int csi_efuse_set_gmac_macaddr(int dev_id, unsigned char *mac)
  1180. {
  1181. int ret, fd;
  1182. char gmac_name[12] = {};
  1183. assert(mac);
  1184. if ((dev_id != 0) && (dev_id != 1))
  1185. return -EINVAL;
  1186. fd = open(efuse_file, O_RDWR);
  1187. if (fd < 0) {
  1188. printf("failed to open efuse device: %s\n", efuse_file);
  1189. return -errno;
  1190. }
  1191. sprintf(gmac_name, "GMAC%d_MAC", dev_id);
  1192. ret = efuse_write(fd, mac, gmac_name);
  1193. if (ret < 0)
  1194. printf("failed to set '%s' into efuse\n", gmac_name);
  1195. else
  1196. ret = 0;
  1197. close(fd);
  1198. return ret;
  1199. }
  1200. /**
  1201. * csi_efuse_update_lc_rma() - Upate efuse life cycle RMA
  1202. *
  1203. * Return: 0: Success others: Failed
  1204. */
  1205. int csi_efuse_update_lc_rma()
  1206. {
  1207. int fd, ret;
  1208. char d = '1';
  1209. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rma_lc";
  1210. fd = open(dev_path, O_WRONLY);
  1211. if (fd < 0) {
  1212. printf("failed to open device '%s'\n", dev_path);
  1213. return -errno;
  1214. }
  1215. ret = write(fd, &d, 1);
  1216. if (ret < 0){
  1217. printf("failed to update rma lifecycle\n");
  1218. return -errno;
  1219. } else {
  1220. ret = 0;
  1221. }
  1222. close(fd);
  1223. return 0;
  1224. }
  1225. /**
  1226. * csi_efuse_update_lc_rma() - Upate efuse life cycle RIP
  1227. *
  1228. * Return: 0: Success others: Failed
  1229. */
  1230. int csi_efuse_update_lc_rip()
  1231. {
  1232. int fd, ret;
  1233. char d = '1';
  1234. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rip_lc";
  1235. fd = open(dev_path, O_WRONLY);
  1236. if (fd < 0) {
  1237. printf("failed to open device '%s'\n", dev_path);
  1238. return -errno;
  1239. }
  1240. ret = write(fd, &d, 1);
  1241. if (ret < 0){
  1242. printf("failed to update rip lifecycle\n");
  1243. return -errno;
  1244. } else {
  1245. ret = 0;
  1246. }
  1247. close(fd);
  1248. return 0;
  1249. }
  1250. int csi_efuse_get_lc_preld(char *lc_name)
  1251. {
  1252. int fd, ret;
  1253. char data[30] = {0};
  1254. unsigned int lf = 0;
  1255. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/lc_preld";
  1256. char *str;
  1257. assert(lc_name);
  1258. fd = open(dev_path, O_RDONLY);
  1259. if (fd < 0) {
  1260. printf("failed to open device '%s' (%d)\n", dev_path, -errno);
  1261. return -errno;
  1262. }
  1263. ret = read(fd, data, 30);
  1264. if (ret < 0){
  1265. printf("failed to read lifecycle from preld area\n");
  1266. return -errno;
  1267. }
  1268. lf = strtoul(data, NULL, 16);
  1269. switch (lf) {
  1270. case 0xC44ACFCF:
  1271. str = "LC_INIT";
  1272. break;
  1273. case 0xCA410C33:
  1274. str = "LC_DEV";
  1275. break;
  1276. case 0x548411A6:
  1277. str = "LC_OEM";
  1278. break;
  1279. case 0xABB00F15:
  1280. str = "LC_PRO";
  1281. break;
  1282. case 0x67E93416:
  1283. str = "LC_RMA";
  1284. break;
  1285. case 0x9fCAE0EA:
  1286. str = "LC_RIP";
  1287. break;
  1288. default:
  1289. str = "LC_MAX";
  1290. return -EINVAL;
  1291. }
  1292. strcpy(lc_name, str);
  1293. close(fd);
  1294. return 0;
  1295. }
  1296. /*
  1297. * csi_efuse_update_lc(enum life_cycle_e life_cycle)
  1298. * @life_cycle: the life cycle to set
  1299. * Return: 0: Success others: Failed
  1300. */
  1301. int csi_efuse_update_lc(enum life_cycle_e life_cycle)
  1302. {
  1303. int fd, ret = 0;
  1304. char *lf;
  1305. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/update_lc";
  1306. fd = open(dev_path, O_WRONLY);
  1307. if (fd < 0) {
  1308. printf("failed to open device '%s' (%d)\n", dev_path, -errno);
  1309. return -errno;
  1310. }
  1311. switch (life_cycle) {
  1312. case LC_DEV:
  1313. lf = "LC_DEV";
  1314. break;
  1315. case LC_OEM:
  1316. lf = "LC_OEM";
  1317. break;
  1318. case LC_PRO:
  1319. lf = "LC_PRO";
  1320. break;
  1321. case LC_RMA:
  1322. lf = "LC_RMA";
  1323. break;
  1324. case LC_RIP:
  1325. lf = "LC_RIP";
  1326. break;
  1327. case LC_KILL_KEY1:
  1328. lf = "LC_KILL_KEY1";
  1329. break;
  1330. case LC_KILL_KEY0:
  1331. lf = "LC_KILL_KEY0";
  1332. break;
  1333. default:
  1334. ret = -EINVAL;
  1335. goto exit;
  1336. }
  1337. ret = write(fd, lf, strlen(lf));
  1338. if (ret < 0)
  1339. printf("failed to update efuse life cycle(%d)\n", ret);
  1340. else
  1341. ret = 0;
  1342. exit:
  1343. close(fd);
  1344. return ret;
  1345. }