1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507 |
- // 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;
- }
|