123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- * Copyright (C) 2021 Alibaba Group Holding Limited.
- *
- */
- #include <assert.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include "efuse-api.h"
- //#define DEBUG_INFO
- #ifndef ARRAY_SIZE
- #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
- #endif
- #define EFUSE_LIT_BLOCK_BIT_WIDTH 128
- #define EFUSE_BIG_BLOCK_BIT_WIDTH 256
- #define EFUSE_BYTES_PER_LIT_BLOCK (EFUSE_LIT_BLOCK_BIT_WIDTH >> 3)
- #define EFUSE_BYTES_PER_BIG_BLOCK (EFUSE_BIG_BLOCK_BIT_WIDTH >> 3)
- struct func_efuse_info {
- const char *func_name;
- unsigned int block_id;
- unsigned int addr; /* in byte */
- size_t len; /* in byte */
- unsigned int shift; /* left-shift bit within a byte*/
- unsigned int mask; /* mask within a byte */
- };
- struct func_efuse_info efuse_func_array[] = {
- {"UID", 5, 0x50, 20, 0, 0xff},
- {"USR_DSP0_JTAG_MODE", 8, 0x86, 1, 0, 0xff},
- {"USR_DSP1_JTAG_MODE", 8, 0x87, 1, 0, 0xff},
- {"USR_C910T_JTAG_MODE", 8, 0x88, 1, 0, 0xff},
- {"USR_C910R_JTAG_MODE", 8, 0x89, 1, 0, 0xff},
- {"USR_C906_JTAG_MODE", 8, 0x8a, 1, 0, 0xff},
- {"USR_E902_JTAG_MODE", 8, 0x8b, 1, 0, 0xff},
- {"USR_CHIP_DBG_MODE", 8, 0x8c, 1, 0, 0xff},
- {"USR_DFT_MODE", 8, 0x8d, 1, 0, 0xff},
- {"BOOT_OFFSET", 9, 0x90, 4, 0, 0xff},
- {"BOOT_INDEX", 9, 0x94, 1, 0, 0xff},
- {"BOOT_OFFSET_BAK", 9, 0x95, 4, 0, 0xff},
- {"BOOT_INDEX_BAK", 9, 0x99, 1, 0, 0xff},
- {"USR_USB_FASTBOOT_DIS", 9, 0x9a, 1, 4, 0x0f},
- {"USR_BROM_CCT_DIS", 9, 0x9a, 1, 0, 0x0f},
- {"IMAGE_BL2_ENC", 9, 0x9b, 1, 0, 0xff},
- {"IMAGE_BL3_ENC", 9, 0x9c, 1, 0, 0xff},
- {"IMAGE_BL4_ENC", 9, 0x9d, 1, 0, 0xff},
- {"BL1VERSION", 10, 0xa0, 8, 0, 0xff},
- {"BL2VERSION", 10, 0xa8, 8, 0, 0xff},
- {"SECURE_BOOT", 1, 0x10, 1, 0, 0xff},
- {"HASH_DEBUGPK", 25, 0x190, 32, 0, 0xff},
- {"BROM_DCACHE_EN", 1, 0x12, 1, 2, 0x3},
- {"GMAC0_MAC", 11, 0xb0, 6, 0, 0xff},
- {"GMAC1_MAC", 11, 0xb8, 6, 0, 0xff},
- {},
- };
- static const char *efuse_file = "/sys/bus/nvmem/devices/light-efuse0/nvmem";
- size_t efuse_read(int fd, void *buf, const char *func_name)
- {
- unsigned int offset, mask, shift;
- size_t len, ret;
- int i, block;
- const char *name;
- off_t pos;
- for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
- if (!strcmp(func_name, efuse_func_array[i].func_name))
- break;
- }
- if (i >= ARRAY_SIZE(efuse_func_array)) {
- printf("invalid efuse function name(%s)\n", name);
- return -EINVAL;
- }
- block = efuse_func_array[i].block_id;
- name = efuse_func_array[i].func_name;
- offset = efuse_func_array[i].addr;
- len = efuse_func_array[i].len;
- shift = efuse_func_array[i].shift;
- mask = efuse_func_array[i].mask;
- #ifdef DEBUG_INFO
- printf("[efuse info]: block: %d, name: %s, addr: 0x%x, len: %d mask: 0x%x\n",
- block, name, offset, (int)len, mask);
- #endif
- pos = lseek(fd, offset, SEEK_SET);
- if (pos < 0) {
- perror("failed to lseek offset to read");
- return -errno;
- }
- #ifdef DEBUG_INFO
- printf("efuse pos = %p\n", pos);
- #endif
- if (mask != 0xff) {
- unsigned char data;
- unsigned char rd_buf[len];
- ret = read(fd, rd_buf, len);
- if (ret != len) {
- #ifdef DEBUG_INFO
- printf("real read len: %d, expected read len: %d\n", ret, len);
- #endif
- perror("failed to read");
- ret = -errno;
- goto out;
- }
- data = (rd_buf[0] >> shift) & mask;
- #ifdef DEBUG_INFO
- printf("data = 0x%x\n", data);
- #endif
- memcpy(buf, &data, 1);
- if (len > 1) {
- len = len - 1;
- buf = (unsigned char *)buf + 1;
- memcpy(buf, &rd_buf[1], len);
- }
- } else { /* mask == 0xff */
- ret = read(fd, buf, len);
- if (ret != len) {
- #ifdef DEBUG_INFO
- printf("real read len: %d, expected read len: %d\n", ret, len);
- #endif
- perror("failed to read");
- ret = -errno;
- }
- }
- out:
- return ret;
- }
- size_t efuse_write(int fd, const void *buf, const char *func_name)
- {
- unsigned int offset, mask, shift;
- size_t len, ret;
- int i, block;
- const char *name;
- off_t pos;
- for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
- if (!strcmp(func_name, efuse_func_array[i].func_name))
- break;
- }
- if (i >= ARRAY_SIZE(efuse_func_array)) {
- printf("invalid efuse function name(%s)\n", name);
- return -EINVAL;
- }
- block = efuse_func_array[i].block_id;
- name = efuse_func_array[i].func_name;
- offset = efuse_func_array[i].addr;
- len = efuse_func_array[i].len;
- shift = efuse_func_array[i].shift;
- mask = efuse_func_array[i].mask;
- #ifdef DEBUG_INFO
- printf("efuse info: block: %d, name: %s, addr: 0x%x, len: %d, mask: 0x%x\n",
- block, name, offset, (int)len, mask);
- #endif
- pos = lseek(fd, offset, SEEK_SET);
- if (pos < 0) {
- perror("failed to lseek offset to read");
- return -errno;
- }
- #ifdef DEBUG_INFO
- printf("efuse pos = %p\n", pos);
- #endif
- if (mask != 0xff) {
- unsigned char data;
- unsigned char wr_buf[len];
- memcpy(wr_buf, buf, len);
- ret = read(fd, &data, 1);
- if (ret < 0) {
- perror("failed to read");
- ret = -errno;
- goto out;
- }
- data &= ~(mask << shift);
- data |= (wr_buf[0] & mask) << shift;
- memcpy(&wr_buf[0], &data, 1);
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- perror("failed to lseek offset to write");
- ret = -errno;
- goto out;
- }
- ret = write(fd, wr_buf, len);
- if (ret != len) {
- perror("failed to write");
- ret = -errno;
- }
- } else { /* mask == 0xff */
- ret = write(fd, buf, len);
- if (ret != len) {
- perror("failed to write");
- ret = -errno;
- }
- }
- out:
- return ret;
- }
- /* according to FuseMap_v1.2.1.xlsx */
- size_t efuse_block_read(int fd, unsigned char *buf, unsigned int block_num)
- {
- unsigned int width, offset, bytes;
- size_t ret;
- if (block_num >= 42 && block_num <= 47)
- width = EFUSE_BIG_BLOCK_BIT_WIDTH;
- else
- width = EFUSE_LIT_BLOCK_BIT_WIDTH;
- if (block_num <= 42)
- offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
- else if (block_num > 42 && block_num <= 48)
- offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
- else
- offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
- (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
- bytes = width / 8;
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- perror("failed to lseek offset to read");
- return -errno;
- }
- ret = read(fd, buf, bytes);
- if (ret != bytes) {
- perror("failed to read");
- ret = -errno;
- }
- return ret;
- }
- /* according to FuseMap_v1.2.1.xlsx */
- size_t efuse_block_write(int fd, unsigned char *buf, unsigned int block_num)
- {
- unsigned int width, offset, bytes;
- size_t ret;
- if (block_num >= 42 && block_num <= 47)
- width = EFUSE_BIG_BLOCK_BIT_WIDTH;
- else
- width = EFUSE_LIT_BLOCK_BIT_WIDTH;
- if (block_num <= 42)
- offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
- else if (block_num > 42 && block_num <= 48)
- offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
- else
- offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
- (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
- bytes = width / 8;
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- perror("failed to lseek offset to write");
- return -errno;
- }
- ret = write(fd, buf, bytes);
- if (ret != bytes) {
- perror("failed to write");
- ret = -errno;
- }
- return ret;
- }
- /**
- * csi_efuse_get_chipid() - Get chip id in eFuse
- *
- * @chip_id: pointer to the buffer to store chip id
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_chipid(void *chip_id)
- {
- int ret, fd;
- assert(chip_id);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- perror("open");
- return -errno;
- }
- ret = efuse_read(fd, chip_id, "UID");
- if (ret < 0)
- printf("failed to get 'UID' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_user_dbg_mode() - Get debug mode in user area
- *
- * @type: Debug type
- *
- * @dbg_mode: pointer to the buffer store debug mode
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t *dbg_mode)
- {
- int ret, fd;
- char *mode_name;
- assert(dbg_mode);
- if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
- (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
- (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
- (type != USR_CHIP_DBG) && (type != USR_DFT))
- return -EINVAL;
- switch (type) {
- case USR_DSP0_JTAG:
- mode_name = "USR_DSP0_JTAG_MODE";
- break;
- case USR_DSP1_JTAG:
- mode_name = "USR_DSP1_JTAG_MODE";
- break;
- case USR_C910T_JTAG:
- mode_name = "USR_C910T_JTAG_MODE";
- break;
- case USR_C910R_JTAG:
- mode_name = "USR_C910R_JTAG_MODE";
- break;
- case USR_C906_JTAG:
- mode_name = "USR_C906_JTAG_MODE";
- break;
- case USR_E902_JTAG:
- mode_name = "USR_E902_JTAG_MODE";
- break;
- case USR_CHIP_DBG:
- mode_name = "USR_CHIP_DBG_MODE";
- break;
- case USR_DFT:
- mode_name = "USR_DFT_MODE";
- break;
- }
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, dbg_mode, mode_name);
- if (ret < 0)
- printf("failed to get %s from efuse\n", mode_name);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_user_dbg_mode() - Set debug mode in user area
- *
- * @type: Debug type
- *
- * @dbg_mode: debug mode
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t dbg_mode)
- {
- int ret, fd;
- char *mode_name;
- if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
- (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
- (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
- (type != USR_CHIP_DBG) && (type != USR_DFT))
- return -EINVAL;
- if ((dbg_mode != DBG_MODE_ENABLE) && (dbg_mode != DBG_MODE_PWD_PROTECT) &&
- (dbg_mode != DBG_MODE_DISABLE))
- return -EINVAL;
- switch (type) {
- case USR_DSP0_JTAG:
- mode_name = "USR_DSP0_JTAG_MODE";
- break;
- case USR_DSP1_JTAG:
- mode_name = "USR_DSP1_JTAG_MODE";
- break;
- case USR_C910T_JTAG:
- mode_name = "USR_C910T_JTAG_MODE";
- break;
- case USR_C910R_JTAG:
- mode_name = "USR_C910R_JTAG_MODE";
- break;
- case USR_C906_JTAG:
- mode_name = "USR_C906_JTAG_MODE";
- break;
- case USR_E902_JTAG:
- mode_name = "USR_E902_JTAG_MODE";
- break;
- case USR_CHIP_DBG:
- mode_name = "USR_CHIP_DBG_MODE";
- break;
- case USR_DFT:
- mode_name = "USR_DFT_MODE";
- break;
- }
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, (unsigned char *)&dbg_mode, mode_name);
- if (ret < 0)
- printf("failed to set %s into efuse\n", mode_name);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_boot_offset() - Get BL1's offset in boot media
- *
- * @offset: pointer to the buffer to store BL1's offset
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_boot_offset(unsigned int *offset)
- {
- int ret, fd;
- assert(offset);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, offset, "BOOT_OFFSET");
- if (ret < 0)
- printf("failed to get 'BOOT_OFFSET' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
- *
- * @offset: Offset value to be set
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_boot_offset(unsigned int offset)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &offset, "BOOT_OFFSET");
- if (ret < 0)
- printf("failed to set 'BOOT_OFFSET' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_boot_index() - Get BL1's index in boot media
- *
- * @index: pointer to the buffer to store BL1's index
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_boot_index(unsigned char *index)
- {
- int ret, fd;
- assert(index);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, index, "BOOT_INDEX");
- if (ret < 0)
- printf("failed to get 'BOOT_INDEX' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_boot_index() - Set BL1's index in boot media
- *
- * @index: index value to be set
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_boot_index(const unsigned char index)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &index, "BOOT_INDEX");
- if (ret < 0)
- printf("failed to set 'BOOT_INDEX' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bak_boot_offset() - Get BL1's offset in boot media
- *
- * @offset: pointer to the buffer to store BL1's offset
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bak_boot_offset(unsigned int *offset)
- {
- int ret, fd;
- assert(offset);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, offset, "BOOT_OFFSET_BAK");
- if (ret < 0)
- printf("failed to get 'BOOT_OFFSET_BAK' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
- *
- * @offset: Offset value to be set
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bak_boot_offset(unsigned int offset)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &offset, "BOOT_OFFSET_BAK");
- if (ret < 0)
- printf("failed to set 'BOOT_OFFSET_BAK' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bak_boot_index() - Get BL1's index in boot media
- *
- * @index: pointer to the buffer to store BL1's index
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bak_boot_index(unsigned char *index)
- {
- int ret, fd;
- assert(index);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, index, "BOOT_INDEX_BAK");
- if (ret < 0)
- printf("failed to get 'BOOT_INDEX_BAK' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bak_boot_index() - Set BL1's index in boot media
- *
- * @index: index value to be set
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bak_boot_index(unsigned char index)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &index, "BOOT_INDEX_BAK");
- if (ret < 0)
- printf("failed to set 'BOOT_INDEX_BAK' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_usr_brom_usb_fastboot_st() - Get bootrom USB fastboots tatus in user area
- *
- * @status: pointer to the buffer to store USB fastboot status
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_usr_brom_usb_fastboot_st(brom_usbboot_st_t *status)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(status);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "USR_USB_FASTBOOT_DIS");
- if (ret < 0)
- printf("failed to get 'USR_USB_FASTBOOT_DIS' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0xa)
- *status = BROM_USBBOOT_DIS;
- else if (tempdata != BROM_USBBOOT_EN)
- ret = -EINVAL;
- else
- *status = BROM_USBBOOT_EN;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_dis_usr_brom_usb_fastboot() - Disable bootrom USB fastboot status in user area
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_dis_usr_brom_usb_fastboot(void)
- {
- int ret, fd;
- unsigned char status = 0xA; /* disable value */
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &status, "USR_USB_FASTBOOT_DIS");
- if (ret < 0)
- printf("failed to set 'USR_USB_FASTBOOT_DIS' status into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_usr_brom_cct_st() - Get bootrom CCT status in user area
- *
- * @status: pointer to the buffer to store CCT
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_usr_brom_cct_st(brom_cct_st_t *status)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(status);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "USR_BROM_CCT_DIS");
- if (ret < 0)
- printf("failed to get 'USR_BROM_CCT_DIS' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0xa)
- *status = BROM_CCT_DIS;
- else if(tempdata != BROM_CCT_EN)
- ret = -EINVAL;
- else
- *status = BROM_CCT_EN;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_dis_usr_brom_cct() - Disable bootrom CCT status in user area
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_dis_usr_brom_cct(void)
- {
- int ret, fd;
- unsigned char status = 0xA; /* disable value */
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &status, "USR_BROM_CCT_DIS");
- if (ret < 0)
- printf("failed to set 'USR_BROM_CCT_DIS' status into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bl2_img_encrypt_st() - Get BL2 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bl2_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(encrypt_flag);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "IMAGE_BL2_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0x5a)
- *encrypt_flag = IMAGE_ENCRYPT_EN;
- else if (tempdata != IMAGE_ENCRYPT_DIS)
- ret = -EINVAL;
- else
- *encrypt_flag = IMAGE_ENCRYPT_DIS;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bl2_img_encrypt_st() - Set BL2 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bl2_img_encrypt_st(img_encrypt_st_t encrypt_flag)
- {
- int ret, fd;
- if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
- return -EINVAL;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- if (encrypt_flag == IMAGE_ENCRYPT_EN)
- encrypt_flag = 0x5a;
- ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL2_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bl3_img_encrypt_st() - Get BL3 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bl3_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(encrypt_flag);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "IMAGE_BL3_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0x5a)
- *encrypt_flag = IMAGE_ENCRYPT_EN;
- else if (tempdata != IMAGE_ENCRYPT_DIS)
- ret = -EINVAL;
- else
- *encrypt_flag = IMAGE_ENCRYPT_DIS;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bl3_img_encrypt_st() - Set BL3 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL3 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bl3_img_encrypt_st(img_encrypt_st_t encrypt_flag)
- {
- int ret, fd;
- if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
- return -EINVAL;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- if (encrypt_flag == IMAGE_ENCRYPT_EN)
- encrypt_flag = 0x5a;
- ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL3_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bl4_img_encrypt_st() - Get BL4 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bl4_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(encrypt_flag);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "IMAGE_BL4_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0x5a)
- *encrypt_flag = IMAGE_ENCRYPT_EN;
- else if (tempdata != IMAGE_ENCRYPT_DIS)
- ret = -EINVAL;
- else
- *encrypt_flag = IMAGE_ENCRYPT_DIS;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bl4_img_encrypt_st() - Set BL4 image encryption flag
- *
- * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bl4_img_encrypt_st(img_encrypt_st_t encrypt_flag)
- {
- int ret, fd;
- if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
- return -EINVAL;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- if (encrypt_flag == IMAGE_ENCRYPT_EN)
- encrypt_flag = 0x5a;
- ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL4_ENC");
- if (ret < 0)
- printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bl1_version() - Get BL1 version
- *
- * @version: pointer to the buffer to store BL1's version in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bl1_version(unsigned long long *version)
- {
- int ret, fd;
- assert(version);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, version, "BL1VERSION");
- if (ret < 0)
- printf("failed to get 'BL1VERSION' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bl1_version() - Set BL1 version
- *
- * @version: pointer to the buffer to store BL1's version in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bl1_version(unsigned long long version)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &version, "BL1VERSION");
- if (ret < 0)
- printf("failed to set 'BL1VERSION' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_bl2_version() - Get BL2 version
- *
- * @version: pointer to the buffer to store BL2's version in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_bl2_version(unsigned long long *version)
- {
- int ret, fd;
- assert(version);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, version, "BL2VERSION");
- if (ret < 0)
- printf("failed to get 'BL2VERSION' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_bl2_version() - Set BL2 version
- *
- * @version: pointer to the buffer to store BL2's version in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_bl2_version(unsigned long long version)
- {
- int ret, fd;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_write(fd, &version, "BL2VERSION");
- if (ret < 0)
- printf("failed to set 'BL2VERSION' into efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_secure_boot_st() - Get seucre boot flag
- *
- * @sboot_flag: A pointer to the buffer to store secure boot flag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_secure_boot_st(sboot_st_t *sboot_flag)
- {
- int ret, fd;
- unsigned char tempdata;
- assert(sboot_flag);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, &tempdata, "SECURE_BOOT");
- if (ret < 0)
- printf("failed to get 'SECURE_BOOT' from efuse\n");
- else
- ret = 0;
- if (tempdata == 0x5a)
- *sboot_flag = SECURE_BOOT_EN;
- else if (tempdata != SECURE_BOOT_DIS)
- ret = -EINVAL;
- else
- *sboot_flag = SECURE_BOOT_DIS;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_hash_challenge() - Get hash challenge in eFuse
- *
- * @hash_resp: pointer to the buffer to store hash response
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_hash_challenge(void * hash_resp)
- {
- int ret, fd;
- assert(hash_resp);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_read(fd, hash_resp, "HASH_DEBUGPK");
- if (ret < 0)
- printf("failed to get 'HASH_DEBUGPK' from efuse\n");
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_get_userdata_group() - Get user data in corresponding eFuse block
- *
- * @key: pointer to the buffer to store user data
- * @block_num: the block number in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_get_userdata_group(unsigned char *key, unsigned char block_num)
- {
- int ret, fd;
- assert(key);
- if (block_num >= 58)
- return -EINVAL;
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_block_read(fd, key, block_num);
- if (ret < 0)
- printf("failed to get block%d data from efuse\n", block_num);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_userdata_group() - Set user data corresponding eFuse block
- *
- * @key: pointer to the buffer to store user data
- * @block_num: the block number in eFuse
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_efuse_set_userdata_group(unsigned char *key, unsigned char block_num)
- {
- int ret, fd;
- assert(key);
- if (block_num > 58) {
- printf("the block number is out of the scop \n");
- return -EINVAL;
- }
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- ret = efuse_block_write(fd, key, block_num);
- if (ret < 0)
- printf("failed to set block%d data into efuse\n", block_num);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_read() - Read data from eFuse
- *
- * @offset: offset address
- * @data: Pointer to a buffer storing the data read from eFuse
- * @cnt: Number of bytes need to be read
- *
- * Return: number of data items read or error code
- */
- int csi_efuse_read(unsigned int offset, void *data, unsigned int cnt)
- {
- int ret, fd;
- assert(data);
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- perror("failed to lseek offset to read");
- close(fd);
- return -errno;
- }
- ret = read(fd, data, cnt);
- if (ret < 0) {
- perror("failed to read data from efuse");
- ret = -errno;
- }
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_write() - Write data to eFuse
- *
- * @offset: offset address
- * @data: Pointer to a buffer storing the data write to eFuse
- * @cnt: Number of bytes need to be write
- *
- * Return: number of data items write or error code
- */
- int csi_efuse_write(unsigned int offset, void *data, unsigned int cnt)
- {
- int ret, fd;
- assert(data);
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- perror("failed to lseek offset to write");
- close(fd);
- return -errno;
- }
- ret = write(fd, data, cnt);
- if (ret < 0) {
- perror("failed to write data to efuse");
- ret = -errno;
- }
- close(fd);
- return ret;
- }
- /**
- * csi_dbg_enable_c910t_jtag() - Enable C910 TEE core jtag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_dbg_enable_c910t_jtag(void)
- {
- }
- /**
- * csi_dbg_disable_c910t_jtag() - Disable C910 TEE core jtag
- *
- * Return: 0 on success or negative code on failure
- */
- int csi_dbg_disable_c910t_jtag(void)
- {
- }
- /**
- * csi_efuse_get_gmac_macaddr() - Get gmac0/gmac1 mac address in eFuse
- * @dev_id: '0' means gmac0, '1' means gmac1
- * @mac: the mac address string
- *
- * Return: 0: Success others: Failed
- */
- int csi_efuse_get_gmac_macaddr(int dev_id, unsigned char *mac)
- {
- int ret, fd;
- char gmac_name[12] = {};
- assert(mac);
- if ((dev_id != 0) && (dev_id != 1))
- return -EINVAL;
- fd = open(efuse_file, O_RDONLY);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- sprintf(gmac_name, "GMAC%d_MAC", dev_id);
- ret = efuse_read(fd, mac, gmac_name);
- if (ret < 0)
- printf("failed to get %s\n", gmac_name);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_set_gmac_macaddr() - Set gmac0/gmac1 mac address in eFuse
- * @dev_id: '0' means gmac0, '1' means gmac1
- * @mac: the mac address string
- *
- * Return: 0: Success others: Failed
- */
- int csi_efuse_set_gmac_macaddr(int dev_id, unsigned char *mac)
- {
- int ret, fd;
- char gmac_name[12] = {};
- assert(mac);
- if ((dev_id != 0) && (dev_id != 1))
- return -EINVAL;
- fd = open(efuse_file, O_RDWR);
- if (fd < 0) {
- printf("failed to open efuse device: %s\n", efuse_file);
- return -errno;
- }
- sprintf(gmac_name, "GMAC%d_MAC", dev_id);
- ret = efuse_write(fd, mac, gmac_name);
- if (ret < 0)
- printf("failed to set '%s' into efuse\n", gmac_name);
- else
- ret = 0;
- close(fd);
- return ret;
- }
- /**
- * csi_efuse_update_lc_rma() - Upate efuse life cycle RMA
- *
- * Return: 0: Success others: Failed
- */
- int csi_efuse_update_lc_rma()
- {
- int fd, ret;
- char d = '1';
- const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rma_lc";
- fd = open(dev_path, O_WRONLY);
- if (fd < 0) {
- printf("failed to open device '%s'\n", dev_path);
- return -errno;
- }
- ret = write(fd, &d, 1);
- if (ret < 0){
- printf("failed to update rma lifecycle\n");
- return -errno;
- } else {
- ret = 0;
- }
- close(fd);
- return 0;
- }
- /**
- * csi_efuse_update_lc_rma() - Upate efuse life cycle RIP
- *
- * Return: 0: Success others: Failed
- */
- int csi_efuse_update_lc_rip()
- {
- int fd, ret;
- char d = '1';
- const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rip_lc";
- fd = open(dev_path, O_WRONLY);
- if (fd < 0) {
- printf("failed to open device '%s'\n", dev_path);
- return -errno;
- }
- ret = write(fd, &d, 1);
- if (ret < 0){
- printf("failed to update rip lifecycle\n");
- return -errno;
- } else {
- ret = 0;
- }
- close(fd);
- return 0;
- }
- int csi_efuse_get_lc_preld(char *lc_name)
- {
- int fd, ret;
- char data[30] = {0};
- unsigned int lf = 0;
- const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/lc_preld";
- char *str;
- assert(lc_name);
- fd = open(dev_path, O_RDONLY);
- if (fd < 0) {
- printf("failed to open device '%s' (%d)\n", dev_path, -errno);
- return -errno;
- }
- ret = read(fd, data, 30);
- if (ret < 0){
- printf("failed to read lifecycle from preld area\n");
- return -errno;
- }
- lf = strtoul(data, NULL, 16);
- switch (lf) {
- case 0xC44ACFCF:
- str = "LC_INIT";
- break;
- case 0xCA410C33:
- str = "LC_DEV";
- break;
- case 0x548411A6:
- str = "LC_OEM";
- break;
- case 0xABB00F15:
- str = "LC_PRO";
- break;
- case 0x67E93416:
- str = "LC_RMA";
- break;
- case 0x9fCAE0EA:
- str = "LC_RIP";
- break;
- default:
- str = "LC_MAX";
- return -EINVAL;
- }
- strcpy(lc_name, str);
- close(fd);
- return 0;
- }
- /*
- * csi_efuse_update_lc(enum life_cycle_e life_cycle)
- * @life_cycle: the life cycle to set
- * Return: 0: Success others: Failed
- */
- int csi_efuse_update_lc(enum life_cycle_e life_cycle)
- {
- int fd, ret = 0;
- char *lf;
- const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/update_lc";
- fd = open(dev_path, O_WRONLY);
- if (fd < 0) {
- printf("failed to open device '%s' (%d)\n", dev_path, -errno);
- return -errno;
- }
- switch (life_cycle) {
- case LC_DEV:
- lf = "LC_DEV";
- break;
- case LC_OEM:
- lf = "LC_OEM";
- break;
- case LC_PRO:
- lf = "LC_PRO";
- break;
- case LC_RMA:
- lf = "LC_RMA";
- break;
- case LC_RIP:
- lf = "LC_RIP";
- break;
- case LC_KILL_KEY1:
- lf = "LC_KILL_KEY1";
- break;
- case LC_KILL_KEY0:
- lf = "LC_KILL_KEY0";
- break;
- default:
- ret = -EINVAL;
- goto exit;
- }
- ret = write(fd, lf, strlen(lf));
- if (ret < 0)
- printf("failed to update efuse life cycle(%d)\n", ret);
- else
- ret = 0;
- exit:
- close(fd);
- return ret;
- }
|