/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved. * Description: Test the file of the root key interface. * Author: Hisilicon * Create: 2019-09-20 */ #include "sample_otp_base.h" static td_s32 get_root_key_lock_stat(td_s32 argc, td_char *argv[]); static td_s32 set_root_key(td_s32 argc, td_char *argv[]); static otp_sample g_otp_sample_data[] = { { 0, "help", NULL, { "Display this help and exit.", "example: ./sample_otp_rootkey help" } }, { 1, "set", set_root_key, { "Set root key --> UAPI_OTP_BOOT_ROOTKEY.", "example: ./sample_otp_rootkey set boot" } }, { 2, "set", set_root_key, { "Set root key --> UAPI_OTP_OEM_ROOTKEY.", "example: ./sample_otp_rootkey set OEM" } }, { 3, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY0.", "example: ./sample_otp_rootkey set cas0" } }, { 4, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY1.", "example: ./sample_otp_rootkey set cas1" } }, { 5, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY2.", "example: ./sample_otp_rootkey set cas2" } }, { 6, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY3.", "example: ./sample_otp_rootkey set cas3" } }, { 7, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY4.", "example: ./sample_otp_rootkey set cas4" } }, { 8, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY5.", "example: ./sample_otp_rootkey set cas5" } }, { 9, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY6.", "example: ./sample_otp_rootkey set cas6" } }, { 10, "set", set_root_key, { "Set root key --> UAPI_OTP_CAS_ROOTKEY7.", "example: ./sample_otp_rootkey set cas7" } }, { 11, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_BOOT_ROOTKEY.", "example: ./sample_otp_rootkey get boot" } }, { 12, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_OEM_ROOTKEY.", "example: ./sample_otp_rootkey get OEM" } }, { 13, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY0.", "example: ./sample_otp_rootkey get cas0" } }, { 14, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY1.", "example: ./sample_otp_rootkey get cas1" } }, { 15, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY2.", "example: ./sample_otp_rootkey get cas2" } }, { 16, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY3.", "example: ./sample_otp_rootkey get cas3" } }, { 17, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY4.", "example: ./sample_otp_rootkey get cas4" } }, { 18, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY5.", "example: ./sample_otp_rootkey get cas5" } }, { 19, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY6.", "example: ./sample_otp_rootkey get cas6" } }, { 20, "get", get_root_key_lock_stat, { "Get root key --> UAPI_OTP_CAS_ROOTKEY7.", "example: ./sample_otp_rootkey get cas7" } }, }; static td_void get_type(td_s32 argc, td_char *argv[], uapi_otp_rootkey *key_type) { if (argv[0x2] == TD_NULL) { sample_printf("argv[0x2] is NULL\n"); goto out; } if (key_type == TD_NULL) { sample_printf("key_type is NULL\n"); goto out; } if (case_strcmp("boot", argv[0x2])) { *key_type = UAPI_OTP_BOOT_ROOTKEY; } else if (case_strcmp("OEM", argv[0x2])) { *key_type = UAPI_OTP_OEM_ROOTKEY; } else if (case_strcmp("cas0", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY0; } else if (case_strcmp("cas1", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY1; } else if (case_strcmp("cas2", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY2; } else if (case_strcmp("cas3", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY3; } else if (case_strcmp("cas4", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY4; } else if (case_strcmp("cas5", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY5; } else if (case_strcmp("cas6", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY6; } else if (case_strcmp("cas7", argv[0x2])) { *key_type = UAPI_OTP_CAS_ROOTKEY7; } else { *key_type = UAPI_OTP_ROOTKEY_MAX; } unused(argc); out: return; } static td_void set_key_printf(uapi_otp_rootkey key_type, const td_u8 *key, td_u32 len) { td_char *string = TD_NULL; if (key == TD_NULL) { goto out; } switch (key_type) { case UAPI_OTP_BOOT_ROOTKEY: string = "Set root key(UAPI_OTP_BOOT_ROOTKEY)"; break; case UAPI_OTP_OEM_ROOTKEY: string = "Set root key(UAPI_OTP_OEM_ROOTKEY)"; break; case UAPI_OTP_CAS_ROOTKEY0: string = "Set root key(UAPI_OTP_CAS_ROOTKEY0)"; break; case UAPI_OTP_CAS_ROOTKEY1: string = "Set root key(UAPI_OTP_CAS_ROOTKEY1)"; break; case UAPI_OTP_CAS_ROOTKEY2: string = "Set root key(UAPI_OTP_CAS_ROOTKEY2)"; break; case UAPI_OTP_CAS_ROOTKEY3: string = "Set root key(UAPI_OTP_CAS_ROOTKEY3)"; break; case UAPI_OTP_CAS_ROOTKEY4: string = "Set root key(UAPI_OTP_CAS_ROOTKEY4)"; break; case UAPI_OTP_CAS_ROOTKEY5: string = "Set root key(UAPI_OTP_CAS_ROOTKEY5)"; break; case UAPI_OTP_CAS_ROOTKEY6: string = "Set root key(UAPI_OTP_CAS_ROOTKEY6)"; break; case UAPI_OTP_CAS_ROOTKEY7: string = "Set root key(UAPI_OTP_CAS_ROOTKEY7)"; break; default: goto out; } print_buffer(string, key, len); out: return; } static td_void get_key_lock_stat_printf(uapi_otp_rootkey key_type, td_bool status) { switch (key_type) { case UAPI_OTP_BOOT_ROOTKEY: sample_printf("Get root key(UAPI_OTP_BOOT_ROOTKEY) lock stat: %d\n", status); break; case UAPI_OTP_OEM_ROOTKEY: sample_printf("Get root key(UAPI_OTP_OEM_ROOTKEY) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY0: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY0) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY1: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY1) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY2: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY2) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY3: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY3) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY4: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY4) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY5: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY5) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY6: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY6) lock stat: %d\n", status); break; case UAPI_OTP_CAS_ROOTKEY7: sample_printf("Get root key(UAPI_OTP_CAS_ROOTKEY7) lock stat: %d\n", status); break; default: break; } return; } static td_s32 get_root_key_lock_stat(td_s32 argc, td_char *argv[]) { td_s32 ret; td_bool status = TD_FALSE; uapi_otp_rootkey key_type = UAPI_OTP_ROOTKEY_MAX; get_type(argc, argv, &key_type); if (key_type == UAPI_OTP_ROOTKEY_MAX) { sample_printf("Don't have root key type\n"); ret = TD_FAILURE; goto out; } ret = uapi_otp_get_root_key_lock_stat(key_type, &status); if (ret != TD_SUCCESS) { sample_printf("Failed to get root key lock stat, ret = 0x%x \n", ret); goto out; } get_key_lock_stat_printf(key_type, status); out: return ret; } static td_s32 set_root_key(td_s32 argc, td_char *argv[]) { td_s32 ret; td_u8 key[KEY_LEN] = { 0x10, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; uapi_otp_rootkey key_type = UAPI_OTP_ROOTKEY_MAX; get_type(argc, argv, &key_type); if (key_type == UAPI_OTP_ROOTKEY_MAX) { sample_printf("Don't have root key type\n"); ret = TD_FAILURE; goto out; } ret = uapi_otp_set_root_key(key_type, key, sizeof(key)); if (ret != TD_SUCCESS) { sample_printf("Failed to set root key, ret = 0x%x \n", ret); goto out; } set_key_printf(key_type, key, sizeof(key)); out: return ret; } td_s32 main(int argc, char *argv[]) { td_s32 ret; if (argc < 0x3) { sample_printf("sample parameter error.\n"); ret = SOC_ERR_OTP_INVALID_PARA; goto out1; } if (case_strcmp("help", argv[1])) { show_usage(g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0])); ret = TD_SUCCESS; goto out0; } ret = uapi_otp_init(); if (ret != TD_SUCCESS) { sample_printf("OTP init failed, ret = 0x%x \n", ret); goto out1; } ret = run_cmdline(argc, argv, g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0])); (td_void)uapi_otp_deinit(); out1: show_returne_msg(g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0]), ret); out0: return ret; }