fix broken merge
[osmocom-bb.git] / src / target / firmware / apps / simtest / main.c
index f3fa89f..44c7adf 100755 (executable)
@@ -1,4 +1,4 @@
-/* main program of Free Software for Calypso Phone */
+/* SIM test application */
 
 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
  *
@@ -26,6 +26,7 @@
 
 #include <debug.h>
 #include <memory.h>
+#include <delay.h>
 #include <rffe.h>
 #include <keypad.h>
 #include <board.h>
 #include <comm/sercomm.h>
 #include <comm/timer.h>
 
-
 #include <calypso/sim.h>
 
-
 #define DEBUG
 
 /* Dump bytes in hex on the console */
@@ -53,14 +52,14 @@ static void myHexdump(uint8_t *data, int len)
        int i;
 
        for(i=0;i<len;i++)
-               printf("%x ",data[i]);
+               printf("%02x ",data[i]);
 
        printf("(%i bytes)\n", len);
 
        return;
 }
 
-/* SIM instructions 
+/* SIM instructions
    All instructions a standard sim card must feature: */
 #define SIM_CLASS 0xA0                 /* Class that contains the following instructions */
 #define SIM_SELECT 0xA4                        /* Select a file on the card */
@@ -71,7 +70,7 @@ static void myHexdump(uint8_t *data, int len)
 #define SIM_UPDATE_RECORD 0xDC         /* Write record of a record based file */
 #define SIM_SEEK 0xA2                  /* Seek in a record based file */
 #define SIM_INCREASE 0x32              /* Increase a record in a record based file */
-#define SIM_VERIFY_CHV 0x20            /* Authenicate with card (enter pin) */
+#define SIM_VERIFY_CHV 0x20            /* Authenticate with card (enter pin) */
 #define SIM_CHANGE_CHV 0x24            /* Change pin */
 #define SIM_DISABLE_CHV 0x26           /* Disable pin so that no authentication is needed anymore */
 #define SIM_ENABLE_CHV 0x28            /* Enable pin, authentication is now needed again */
@@ -83,7 +82,7 @@ static void myHexdump(uint8_t *data, int len)
 #define SIM_GET_RESPONSE 0xC0          /* Get the response of a command from the card */
 
 /* File identifiers (filenames)
-   The file identifiers are the standartized file identfiers mentiond in the
+   The file identifiers are the standardized file identifiers mentioned in the
    GSM-11-11 specification. */
 #define SIM_MF         0x3F00
 #define SIM_EF_ICCID   0x2FE2
@@ -128,18 +127,20 @@ uint16_t sim_select(uint16_t fid)
        txBuffer[1] = (uint8_t) fid;
        txBuffer[0] = (uint8_t) (fid >> 8);
 
-       if(calypso_sim_transceive(SIM_CLASS, SIM_SELECT, 0x00, 0x00, 0x02,txBuffer,status_word, SIM_APDU_PUT) != 0)
+       if(calypso_sim_transceive(SIM_CLASS, SIM_SELECT, 0x00, 0x00, 0x02,
+                                 txBuffer, status_word, SIM_APDU_PUT) != 0)
                return 0xFFFF;
 
        return (status_word[0] << 8) | status_word[1];
 }
 
 /* Get the status of the currently selected file */
-uint16_t sim_status(void) 
+uint16_t sim_status(void)
 {
        uint8_t status_word[2];
 
-       if(calypso_sim_transceive(SIM_CLASS, SIM_STATUS, 0x00, 0x00, 0x00,0,status_word, SIM_APDU_PUT) != 0)
+       if(calypso_sim_transceive(SIM_CLASS, SIM_STATUS, 0x00, 0x00, 0x00, 0,
+                                 status_word, SIM_APDU_PUT) != 0)
                return 0xFFFF;
 
        return (status_word[0] << 8) | status_word[1];
@@ -149,14 +150,47 @@ uint16_t sim_status(void)
 uint16_t sim_readbinary(uint8_t offset_high, uint8_t offset_low, uint8_t length, uint8_t *data)
 {
        uint8_t status_word[2];
-       if(calypso_sim_transceive(SIM_CLASS, SIM_READ_BINARY, offset_high, offset_low, length, data ,status_word, SIM_APDU_GET) != 0)
+       if(calypso_sim_transceive(SIM_CLASS, SIM_READ_BINARY, offset_high,
+                                 offset_low, length, data ,status_word,
+                                 SIM_APDU_GET) != 0)
+               return 0xFFFF;
+
+       return (status_word[0] << 8) | status_word[1];
+}
+
+uint16_t sim_verify(char *pin)
+{
+       uint8_t txBuffer[8];
+       uint8_t status_word[2];
+
+       memset(txBuffer, 0xFF, 8);
+       memcpy(txBuffer, pin, strlen(pin));
+
+       if(calypso_sim_transceive(SIM_CLASS, SIM_VERIFY_CHV, 0x00, 0x01, 0x08, txBuffer,status_word, SIM_APDU_PUT) != 0)
                return 0xFFFF;
 
        return (status_word[0] << 8) | status_word[1];
 }
 
+uint16_t sim_run_gsm_algorith(uint8_t *data)
+{
+       uint8_t status_word[2];
+
+       if(calypso_sim_transceive(SIM_CLASS, SIM_RUN_GSM_ALGORITHM, 0x00, 0x00, 0x10, data, status_word, SIM_APDU_PUT) != 0)
+               return 0xFFFF;
+
+       printf("   ==> Status word: %x\n", (status_word[0] << 8) | status_word[1]);
 
+       if(status_word[0] != 0x9F || status_word[1] != 0x0C)
+               return (status_word[0] << 8) | status_word[1];
 
+       /* GET RESPONSE */
+
+       if(calypso_sim_transceive(SIM_CLASS, SIM_GET_RESPONSE, 0, 0, 0x0C, data ,status_word, SIM_APDU_GET) != 0)
+               return 0xFFFF;
+
+       return (status_word[0] << 8) | status_word[1];
+}
 
 
 /* FIXME: We need proper calibrated delay loops at some point! */
@@ -189,7 +223,7 @@ void do_sim_test(void)
 
        uint8_t atr[20];
        uint8_t atrLength = 0;
-       
+
        memset(atr,0,sizeof(atr));
 
 
@@ -201,8 +235,8 @@ void do_sim_test(void)
 
        puts("----------------SIMTEST----8<-----------------\n");
 
-       /* Initalize Sim-Controller driver */
-       puts("Initalizing driver:\n");
+       /* Initialize Sim-Controller driver */
+       puts("Initializing driver:\n");
        calypso_sim_init();
 
        /* Power up sim and display ATR */
@@ -233,6 +267,9 @@ void do_sim_test(void)
        puts(" * Testing SELECT: Selecting DF_GSM\n");
        printf("   ==> Status word: %x\n", sim_select(SIM_DF_GSM));
 
+       puts(" * Testing PIN VERIFY\n");
+       printf("   ==> Status word: %x\n", sim_verify("1234"));
+
        puts(" * Testing SELECT: Selecting EF_IMSI\n");
        printf("   ==> Status word: %x\n", sim_select(SIM_EF_IMSI));
 
@@ -240,11 +277,18 @@ void do_sim_test(void)
        printf("   ==> Status word: %x\n", sim_status());
 
        memset(buffer,0,sizeof(buffer));
-       puts(" * Testing READ BINARY:\n");      
+       puts(" * Testing READ BINARY:\n");
        printf("   ==> Status word: %x\n", sim_readbinary(0,0,9,buffer));
        printf("       Data: ");
        myHexdump(buffer,9);
 
+       memset(buffer,0,sizeof(buffer));
+       memcpy(buffer,"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff",16);
+       puts(" * Testing RUN GSM ALGORITHM\n");
+       printf("   ==> Status word: %x\n", sim_run_gsm_algorith(buffer));
+       printf("       Result: ");
+       myHexdump(buffer,12);
+
        delay_ms(5000);
 
        calypso_sim_powerdown();
@@ -252,9 +296,6 @@ void do_sim_test(void)
        puts("------------END SIMTEST----8<-----------------\n");
 }
 
-
-
-
 /* Main Program */
 const char *hr = "======================================================================\n";
 
@@ -288,7 +329,7 @@ int main(void)
 
        keypad_set_handler(&key_handler);
 
-       /* Dump clock config aftee PLL set */
+       /* Dump clock config after PLL set */
        calypso_clk_dump();
        puts(hr);