/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved. * Description: This is a head file for otp test * Author: Hisilicon * Create: 2019-09-19 */ #ifndef __SAMPLE_OTP_BASE_H__ #define __SAMPLE_OTP_BASE_H__ #include #include #include "td_type.h" #include "soc_errno.h" #include "uapi_otp.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define sample_printf(fmt...) printf(fmt) #define KEY_LEN 16 #define NAME_MAX_SIZE 32 #define STRING_MAX_SIZE 64 #define TYPE_INDEX 2 #define unused(x) (x) = (x) typedef struct otp_sample { td_u32 index; td_char name[NAME_MAX_SIZE]; td_s32(*run_sample)(td_s32, td_char **); td_u8 help[0x3][0x80]; } otp_sample; typedef struct otp_vendorid_string { uapi_otp_vendorid index; td_char string[STRING_MAX_SIZE]; } otp_vendorid_string; td_bool case_strcmp(const td_char *s1, const td_char *s2) { td_u32 s1_len = strlen(s1); td_u32 s2_len = strlen(s2); if (s1_len == s2_len && s1_len > 0) { return (strncasecmp(s1, s2, s1_len) == 0) ? TD_TRUE : TD_FALSE; } return TD_FALSE; } td_void print_buffer(const td_char *string, const td_u8 *buf, td_u32 len) { td_u32 i; if (buf == NULL || string == NULL) { sample_printf("null pointer input in function print_buf!\n"); return; } sample_printf("%s:\n", string); for (i = 0; i < len; i++) { if ((i != 0) && ((i % 0x10) == 0)) { sample_printf("\n"); } sample_printf("0x%02x ", buf[i]); } sample_printf("\n"); return; } td_void show_usage(otp_sample sample_table[], td_u32 len) { td_u32 i, k; sample_printf("Usage:\n"); sample_printf("----------------------------------------------------\n"); for (i = 0; i < len; i++) { sample_printf(" %10s %-16s %s\n", "name:", sample_table[i].name, sample_table[i].help[0]); for (k = 1; k < sizeof(sample_table[i].help) / sizeof(sample_table[i].help[0]); k++) { if (sample_table[i].help[k][0] == 0) { break; } sample_printf(" %10s %-16s %s\n", "", "", sample_table[i].help[k]); } } return; } void show_returne_msg(otp_sample *sample_table, td_u32 len, td_s32 ret) { if (sample_table == TD_NULL) { return; } if (ret == TD_SUCCESS) { sample_printf("Sample executed successfully. \n"); return; } if (ret == SOC_ERR_OTP_NOT_SUPPORT_INTERFACE) { sample_printf("This chipset not support this interface. ret:0X%X\n", ret); } else { sample_printf("Sample executed failed. ret:0X%X\n", ret); } if (ret == SOC_ERR_OTP_INVALID_PARA) { show_usage(sample_table, len); } return; } static otp_sample *get_opt(td_char *argv, otp_sample *otp_table, td_u32 len) { td_u32 i; for (i = 0; i < len; i++) { if (case_strcmp(otp_table[i].name, argv)) { return &otp_table[i]; } } return NULL; } td_s32 run_cmdline(td_s32 argc, td_char **argv, otp_sample *otp_table, td_u32 len) { td_s32 ret = TD_FAILURE; otp_sample *sample = NULL; if (argv == TD_NULL || argv[1] == TD_NULL) { sample_printf("argv[1] is NULL\n"); goto out; } if (otp_table == TD_NULL || len == 0) { sample_printf("otp_sample table data is NULL or len == 0\n"); goto out; } sample = get_opt(argv[1], otp_table, len); if (sample == TD_NULL) { sample_printf("failed to get_opt func\n"); goto out; } if (sample->run_sample != TD_NULL) { ret = (*sample->run_sample)(argc, argv); } else { sample_printf(" otp sample error.\n"); } out: return ret; } #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* End of #ifndef __SAMPLE_OTP_BASE_H__ */