📄 xpurtc_timer.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include <sys/time.h>#include "rmdef/rmdef.h"#define ALLOW_OS_CODE 1#include "llad/include/gbus.h"#include "rua/include/rua.h"#include "dynamic_drmmanager.h"struct runtime_args *create_runtime_args(void);void destroy_runtime_args(struct runtime_args *args);int usage(void);int parse_args(int argc, char **argv, struct runtime_args *args);int args_sanity(struct runtime_args *args);#define GET 0x01#define SET 0x02#define QUERY 0x04#define RESTORE 0x08struct runtime_args { RMuint32 state; RMint32 time_val;};#define XTASK_BUF_SIZE 10 * 1024#define XRPC_OFFSET 1024int verbose_output;int main(int argc, char **argv){ RMuint8 *xrpc_linux = NULL; RMuint8 *xrpc_gbus = NULL; RMuint32 *cpu_time = NULL; RMuint32 *xpu_time = NULL; struct RUA *pInstance = NULL; RMstatus status = RM_OK; struct runtime_args args; struct timeval tv; args.state = 0x00000000; args.time_val = 0x00000000; status = RUACreateInstance(&pInstance, 0); if (status != RM_OK) { RMDBGLOG((ENABLE, "Unable to talk to RUA instance\n")); return -1; } if ( (parse_args(argc, argv, &args) != 0) || (args_sanity(&args) != 0)) { usage(); status = RM_ERROR; goto cleanup; } RMDBGLOG((ENABLE, "Created RUA instance...\n")); xrpc_gbus = (RMuint8*)RUAMalloc(pInstance, 0, RUA_DRAM_UNPROTECTED, XTASK_BUF_SIZE); RUALock(pInstance, (RMuint32)xrpc_gbus, XTASK_BUF_SIZE); xrpc_linux = (RMuint8*)RUAMap(pInstance, (RMuint32)xrpc_gbus, XTASK_BUF_SIZE); if ((xrpc_gbus == NULL) || (xrpc_linux == NULL)) { RMDBGLOG((ENABLE,"Unable to alloc memory on gbus\n")); goto cleanup; } if ( RMFAILED(status = xpurtc_initialize((RMuint32*)xrpc_gbus, XTASK_BUF_SIZE, 0, FALSE)) ) { RMDBGLOG((ENABLE, "Unable to initialize xpurtc xtask\n")); goto cleanup; } cpu_time = (RMuint32*)xrpc_linux + XRPC_OFFSET; xpu_time = (RMuint32*)xrpc_gbus + XRPC_OFFSET; switch(args.state) { case GET: status = xpurtc_gettime(xpu_time); if( RMFAILED(status) ) { fprintf(stderr, "xpurtc_gettime() failed!"); break; } fprintf(stderr, "XPU RTC time is: %ld\n", *cpu_time); break; case RESTORE: status = xpurtc_restore(); if( RMFAILED(status) ) { fprintf(stderr, "xpurtc_restore() failed!"); break; } fprintf(stderr, "attempted to restore battery time.\n"); fprintf(stderr, "If there was no backed up time, then clock will still read -1.\n"); break; case SET: if( args.time_val == -1 ) { if( gettimeofday(&tv, NULL) != -1 ) *cpu_time = (RMuint32)tv.tv_sec; else { fprintf(stderr, "Unable to get system time!\n"); status = RM_ERROR; break; } } else { *cpu_time = args.time_val; } printf("setting XPU clock time: %ld\n", *cpu_time); status = xpurtc_settime(xpu_time); if( RMFAILED(status) ) { fprintf(stderr, "xpurtc_settime() failed!"); break; } break; case QUERY: status = xpurtc_isvalid(); fprintf(stderr, "xpurtc state is: %s\n", RMFAILED(status) ? "INVALID" : "VALID"); break; default: fprintf(stderr, "Processing error!\n"); fprintf(stderr,"Only one of the options get, set, query may be passed at one time.\n"); usage(); break; }cleanup: xpurtc_terminate(); if( (pInstance != NULL) && (xrpc_gbus != NULL) ) { RUAUnLock(pInstance, (RMuint32)xrpc_gbus, XTASK_BUF_SIZE); RUAUnMap(pInstance, xrpc_gbus, XTASK_BUF_SIZE); RUAFree(pInstance, (RMuint32)xrpc_gbus); } if( pInstance != NULL ) RUADestroyInstance(pInstance); return RMFAILED(status) ? -1 : 0;}int usage(void) { fprintf(stdout,"xpurtc_timer [options]\n""\t--help, -h print out this help message\n""\t--get, -g gets time from XPU and prints it to the console\n""\t--restore, -r restores time from a battery powered register\n""\t--query, -q querys the clock to see if the current clock value is valid\n""\t--set <time value>, -s <time value> sets the time from a unix time stamp. Passing '-1' indicates that the program should read the system time, and write it to the xpu clock\n""\t To set the date string, use the GNU date output from:\n""\t \t # date +%%2m%%2d%%2H%%2M%%Y\n""\t Then use the following line on the Sigma board:\n""\t \t # date -s <time string>\n""\t Only one of the options get, set, query may be passed at one time.\n" ); return 0;}int parse_args(int argc, char** argv, struct runtime_args *args) { int c; if ( argc == 1 ) { fprintf(stderr,"You must specify a file to authenticate.\n"); return -1; } while (1) { int option_index = 0; static struct option long_options[] = { {"help", 0,0,'h'}, {"get", 0,0,'g'}, {"restore", 0,0,'r'}, {"query", 0,0,'q'}, {"set", 1, NULL, 's'}, {"verbose", 0,0,'v'} }; c = getopt_long( argc, argv, "hgqvrs:", long_options, &option_index); if ( c == -1 ) break; switch (c) { case 'h': return -1; break; case 'g': args->state |= GET; break; case 'r': args->state |= RESTORE; break; case 'q': args->state |= QUERY; break; case 's': args->state |= SET; args->time_val = (RMint32)atoi(optarg); break; case 'v': verbose_output = 1; break; default: return -1; break; }; } return 0;}int args_sanity(struct runtime_args *args) { int ret_val = 0; RMDBGLOG((ENABLE, "checking argument sanity...\n")); if ( args->state == 0x00 ) { fprintf(stderr, "You MUST pass at least one argument to xpurtc_time.\n"); ret_val = -1; } if ( args->state == SET && args->time_val == 0x00 ) { fprintf(stderr, "You MUST specify a value with the set option.\n"); ret_val = -1; } return ret_val;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -