📄 mu_sci.c
字号:
/*----------------------------------------------------------------------------+| This source code has been made available to you by IBM on an AS-IS| basis. Anyone receiving this source is licensed under IBM| copyrights to use it in any way he or she deems fit, including| copying it, modifying it, compiling it, and redistributing it either| with or without modifications. No license under IBM patents or| patent applications is to be implied by the copyright license.|| Any user of this software should understand that IBM cannot provide| technical support for this software and will not be responsible for| any consequences resulting from the use of this software.|| Any person who transfers this source code or any derivative work| must include the IBM copyright notice, this paragraph, and the| preceding two paragraphs in the transferred software.|| COPYRIGHT I B M CORPORATION 2001| LICENSED MATERIAL - PROGRAM PROPERTY OF I B M+----------------------------------------------------------------------------*//*----------------------------------------------------------------------------+| Author: Mike Lepore| Component: sci| File: mu_sci.c| Purpose: Menu application functions for the Smart Card Interface driver.| Changes:|| Date: Author Comment:| ---------- ---------------- -----------------------------------------------| 03/22/2001 MAL Initial check-in.| 03/26/2001 Zongwei Liu Port to Linux| 09/26/2001 zongwei Liu Port to pallas| 10/10/2001 Zongwei Liu Port to OS-Adaption layer+----------------------------------------------------------------------------*/#include <fcntl.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#include <linux/ioctl.h>#include "sci/sci_inf.h"#include "sc.h"#include "sci_prot.h"#include "mu_sci.h"static unsigned long loop_test = 0;static unsigned long ss_test = 0;int fd[2];/****************************************************************************** Function: main**** Purpose: get & perform a smart card command**** Parameters: ** ****************************************************************************/int main(int argc, char *argv[]){ char cmd[256]; char *dev_name[] = { "/dev/sci0\0", "/dev/sci1\0" }; unsigned long sci_id = 2; int i; SCI_ERROR rc; printf("sc_test program\n\n"); while (1) { printf("\nPls. select the card:\n\n"); printf(" sc0 = card 0\n"); printf(" sc1 = card 1\n"); printf(" exit = quit\n\n"); scanf("%s", cmd); printf("cmd = %s\n", (char *) cmd); if (strncmp(cmd, "sc0", 3) == 0) { printf("You selected card 0\n"); if ((fd[0] = open(dev_name[0], O_RDWR)) > 0) { sci_id = 0; printf("open /dev/sci0 success\n"); rc = 0; } else { printf("can't open /dev/sci0\n"); } } else if (strncmp(cmd, "sc1", 3) == 0) { printf("You selected card 1\n"); if ((fd[1] = open(dev_name[1], O_RDWR)) > 0) { sci_id = 1; printf("open /dev/sci1 success\n"); rc = 0; } else { printf("can't open /dev/sci1\n"); } } else if (strncmp(cmd, "exit", 4) == 0) { return (0); } else { printf("Error card num\n"); } while (rc == 0) { printf("input command (h=help):\n"); scanf("%s", cmd); printf("cmd = %s\n", (char *) cmd); rc = sci_test(sci_id, cmd); } }}/****************************************************************************** Function: sci_test**** Purpose: execute a smart card command**** Parameters: sci_id: 0-based number to identify smart card controller** cmd: pointer to the command string****************************************************************************/SCI_ERROR sci_test(unsigned long sci_id, char *cmd){ SCI_ERROR rc = SCI_ERROR_OK; if (strncmp(cmd, "h", 1) == 0) { printf("\n\r"); printf("-Smart Card Menu-------------------"); printf("-----------------------------------\n\r"); printf("rt - reset "); printf(" | "); printf("ga - get ATR parameters "); printf("\n\r"); printf("gp - get communication parameters"); printf(" | "); printf(" "); printf("\n\r"); printf("gi - get IFSD "); printf(" | "); printf("si - set IFSD "); printf("\n\r"); printf("sf - SELECT FILE command "); printf(" | "); printf("ur - UPDATE RECORD command "); printf("\n\r"); printf("rr - READ RECORD command "); printf(" | "); printf("lp - loop sf/ur/rr commands "); printf("\n\r"); printf("pp - protocol/parameter selection"); printf(" | "); printf("dr - display SCI register values "); printf("\n\r"); printf("gm - get SCI driver modes "); printf(" | "); printf("sm - set SCI driver modes "); printf("\n\r"); printf("dc - deactivate "); printf(" | "); printf("end - stop this driver "); printf("\n\r"); printf("-----------------------------------"); printf("-----------------------------------\n\r"); printf("\n\r"); } else if (strncmp(cmd, "end", 3) == 0) { close(fd[sci_id]); return (-1); } else { if (strncmp(cmd, "dc", 2) == 0) { ioctl(fd[sci_id], IOCTL_SET_DEACTIVATE); printf("Deactivation complete.\n\r"); } else if (strncmp(cmd, "lp", 2) == 0) { loop(sci_id); } else if (strncmp(cmd, "rt", 2) == 0) { if ((rc = sc_reset(sci_id)) == SCI_ERROR_OK) { printf("Reset successful.\n\r"); } else { print_error("sc_reset", rc); } } else if (strncmp(cmd, "sf", 2) == 0) { /* intended for the 8K Schlumberger Smart Card */ /* supplied with the evaluation kit */ loop_test = 0; ss_test = 0; if ((rc = select_file(sci_id)) != SCI_ERROR_OK) { print_error("select_file", rc); } } else if (strncmp(cmd, "rr", 2) == 0) { /* intended for the 8K Schlumberger Smart Card */ /* supplied with the evaluation kit */ loop_test = 0; if ((rc = read_record(sci_id)) != SCI_ERROR_OK) { print_error("read_record", rc); } } else if (strncmp(cmd, "ur", 2) == 0) { /* intended for the 8K Schlumberger Smart Card */ /* supplied with the evaluation kit */ loop_test = 0; if ((rc = update_record(sci_id)) != SCI_ERROR_OK) { print_error("update_record", rc); } } else if (strncmp(cmd, "pp", 2) == 0) { pps(sci_id); } else if (strncmp(cmd, "ga", 2) == 0) { get_ATR_parms(sci_id); } else if (strncmp(cmd, "gp", 2) == 0) { get_parms(sci_id); }#ifdef SET_PARAMETERS else if (strncmp(cmd, "sp", 2) == 0) { set_parms(sci_id); }#endif else if (strncmp(cmd, "gi", 2) == 0) { get_ifsd(sci_id); } else if (strncmp(cmd, "si", 2) == 0) { set_ifsd(sci_id); } else if (strncmp(cmd, "gm", 2) == 0) { get_modes(sci_id); } else if (strncmp(cmd, "sm", 2) == 0) { set_modes(sci_id); } else if (strncmp(cmd, "dr", 2) == 0) { ioctl(fd[sci_id], IOCTL_DUMP_REGS); } else if (strncmp(cmd, "ss", 2) == 0) { /* used for Schlumberger case 0101_1.BIN (T=1) */ loop_test = 0; ss_test = 1; if ((rc = select_file(sci_id)) != SCI_ERROR_OK) { print_error("select_file", rc); } } else { printf("Error command!\n"); } } return (0);}/****************************************************************************** Name loop**** Purpose: perform select file, update record,** and read record commands in a loop**** Parameters: sci_id: 0-based number to identify smart card controller**** Returns: Error status****************************************************************************/SCI_ERROR loop(unsigned long sci_id){ SCI_ERROR rc = SCI_ERROR_OK; unsigned long i, rep, fail, msg; char input; unsigned char prompt[255]; unsigned long ioctl_param; ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param); if (ioctl_param == 1) { printf("Enter number of iterations:\r\n"); scanf("%s", prompt); rep = atol(prompt); input = '0'; while ((input != 'Y') && (input != 'y') && (input != 'N') && (input != 'n')) { printf("Verbose message output (Y or N)?\r\n"); scanf("%s", prompt); input = (char) (prompt[0]); } msg = 0; if ((input == 'Y') || (input == 'y')) { msg = 1; } fail = 0; loop_test = 1; printf("Loop test running...\r\n"); for (i = 1; i <= rep; i++) { if (msg == 1) { printf("%d\r\n", i); printf("SELECT FILE\r\n"); } else if ((i % 100) == 0) { printf("%d\r\n", i); } rc = select_file(sci_id); if (rc == SCI_ERROR_OK) { if (msg == 1) { printf("UPDATE RECORD\r\n"); } if ((rc = update_record(sci_id)) == SCI_ERROR_OK) { if (msg == 1) { printf("READ RECORD\r\n"); } if ((rc = read_record(sci_id)) != SCI_ERROR_OK) { print_error("read_record", rc); fail++; } } else { print_error("update_record", rc); fail++; } } else { print_error("select_file", rc); fail++; } if (rc != SCI_ERROR_OK) { printf("reset\r\n"); if ((rc = sc_reset(sci_id)) != SCI_ERROR_OK) { print_error("sc_reset", rc); printf("Cancelling loop.\r\n"); rep = i; i = rep + 1; } } } if (rep > 0) { if (fail) { printf("%d out of %d iterations failed.\n", fail, rep); } else { printf("No failures occurred.\n"); } } } else { rc = SCI_ERROR_CARD_NOT_ACTIVATED; print_error("t0_loop", rc); } return (rc);}/****************************************************************************** Name select_file**** Purpose: issue SELECT FILE command to the Smart Card.**** Parameters: sci_id: 0-based number to identify smart card controller**** Returns: Error status****************************************************************************/SCI_ERROR select_file(unsigned long sci_id){ SCI_ERROR rc = SCI_ERROR_OK; unsigned long i = 0; unsigned char capdu[20]; unsigned long length = 0; unsigned char rapdu[258]; unsigned long ioctl_param; ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param); if (ioctl_param == 1) { if (ss_test == 0) { length = 8; capdu[0] = 0xc0; /* CLA byte */ capdu[1] = 0xa4; /* INS byte- SELECT FILE */ capdu[2] = 0x00; /* P1 */ capdu[3] = 0x00; /* P2 */ capdu[4] = 0x02; /* Lc */ capdu[5] = 0x10; /* Data field */ capdu[6] = 0x05; capdu[7] = 0x00; /* Le set to max.- causes GET RESPONSE */ } else { length = 20; capdu[0] = 0xc0; /* CLA byte */ capdu[1] = 0xa4; /* INS byte- SELECT FILE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -