added write16 function (not working by now)
authorhackbard <hackbard>
Tue, 21 Mar 2006 18:16:47 +0000 (18:16 +0000)
committerhackbard <hackbard>
Tue, 21 Mar 2006 18:16:47 +0000 (18:16 +0000)
gemtag/gemtag.c
gemtag/gemtag.h

index 48dc427..d6191ba 100644 (file)
@@ -29,7 +29,7 @@ int asciidump(unsigned char *data,int len) {
 
        for(i=0;i<len;i++)
                if((data[i]>0x19)&&(data[i]<0x7f)) printf("%c",data[i]);
-               else printf("_");
+               else printf(".");
        //printf("\n");
 
        return 0;
@@ -261,7 +261,7 @@ int gemtag_auth_mifare_key(struct gemtag_handle *gh,
 
        gemtag_transform_mifare_key(gh,key6,key12);
                             
-       buf[0]=0x60; /* auth mode */
+       buf[0]=GEMTAG_PICC_AUTHENT1A;   /* auth mode */
        memcpy(buf+1,gh->serial,4);     /* sreial */
        memcpy(buf+5,key12,12);         /* transformed key */
        buf[17]=sector;                 /* sector */
@@ -272,21 +272,32 @@ int gemtag_auth_mifare_key(struct gemtag_handle *gh,
        return 0;
 }
 
-int gemtag_read16(struct gemtag_handle *gh,int sector,
-                  unsigned char *data) {
+int gemtag_read16(struct gemtag_handle *gh,int sector,unsigned char *data) {
 
        unsigned char buf[32];
        int len,ret;
 
        buf[0]=sector;
-       ret=gemtag_transceive(gh,GEMTAG_CMD_PICC_READ,
-                             buf,1,data,&len);
+       ret=gemtag_transceive(gh,GEMTAG_CMD_PICC_READ,buf,1,data,&len);
        if(ret) return -READ_FAILED;
 
        return 0;
 }
 
-int gemtag_pick_picc(struct gemtag_handle *gh) {
+int gemtag_write16(struct gemtag_handle *gh,int sector,unsigned char *data) {
+
+       unsigned char buf[32];
+       int ret,len;
+
+       buf[0]=GEMTAG_PICC_WRITE16;
+       buf[1]=sector;
+       memcpy(buf+2,data,16);
+       ret=gemtag_transceive(gh,GEMTAG_CMD_COMMON_WRITE,buf,18,buf,&len);
+
+       return ret;
+}
+
+int gemtag_select_picc(struct gemtag_handle *gh) {
 
        unsigned char buf[16];
        unsigned int len;
@@ -309,13 +320,13 @@ int gemtag_pick_picc(struct gemtag_handle *gh) {
 
        return 0;
 }
-       
+
 int main(int argc, char **argv) {
 
        struct gemtag_handle *gh;
        unsigned char buf[256];
        unsigned char key6[6];
-       int i;
+       int i,ret;
 
        gh=gemtag_open();
 
@@ -330,7 +341,7 @@ int main(int argc, char **argv) {
        printf("\n");
        */
 
-       if(gemtag_pick_picc(gh)) {
+       if(gemtag_select_picc(gh)) {
                printf("no card found!\n");
                return -NO_PICC;
        }
@@ -338,17 +349,29 @@ int main(int argc, char **argv) {
        memset(key6,0xff,6);
        printf("\nreading sectors ... (serial: %02x %02x %02x %02x)\n\n",
               gh->serial[3],gh->serial[2],gh->serial[1],gh->serial[0]);
-       for(i=0;i<256;i++) {
-               gemtag_auth_mifare_key(gh,key6,i);
-               if(!gemtag_read16(gh,i,buf)) {
-                       printf("%02x: ",i++);
-                       hexdump(buf,16);
-                       printf(" | ");
-                       asciidump(buf,16);
-                       printf("\n");
-               }
-               else return 0;
+       i=0;
+       while(!gemtag_auth_mifare_key(gh,key6,i)) {
+               gemtag_read16(gh,i,buf);
+               printf("%02x: ",i);
+               hexdump(buf,16);
+               printf(" | ");
+               asciidump(buf,16);
+               printf("\n");
+               i+=1;
        }
+       printf("\n");
+
+       /* try to write some data
+       printf("write some data to the 5th sector:\n");
+       for(i=0;i<16;i++) buf[i]=i;
+       i=4;
+       ret=gemtag_write16(gh,i,buf);
+       printf("return = %d\n",ret);
+       gemtag_read16(gh,i,buf);
+       printf("%02x: ",i);
+       hexdump(buf,16);
+       printf("\n");
+       */
 
        gemtag_close(gh);
 
index fa716f8..dfdfa64 100644 (file)
@@ -44,8 +44,9 @@ struct gemtag_cmd_hdr {
 #define CMD_FAILED                     0x04
 #define AUTH_FAILED                    0x05
 #define READ_FAILED                    0x06
-#define NO_PICC                                0x07
-#define PICC_SELECT_ERROR              0x08
+#define WRITE_FAILED                   0x07
+#define NO_PICC                                0x08
+#define PICC_SELECT_ERROR              0x09
 
 /* gemtag commands */
 
@@ -119,4 +120,23 @@ struct gemtag_cmd_hdr {
 #define GEMTAG_CMD_PPS_REQUEST         0xa0
 #define GEMTAG_CMD_DESELECT            0xa1
 
+
+/* function prototypes */
+int asciidump(unsigned char *data,int len);
+int hexdump(unsigned char *data,int len);
+struct usb_device *find_device(unsigned short vendor,unsigned short device);
+u_int16_t gemtag_calc_crc(unsigned char *data,u_int16_t len);
+int gemtag_transceive(struct gemtag_handle *gh,unsigned char cmd,
+                      unsigned char *tx,unsigned int tx_len,
+                      unsigned char *rx,unsigned int *rx_len);
+struct gemtag_handle *gemtag_open(void);
+int gemtag_close(struct gemtag_handle *gh);
+int gemtag_transform_mifare_key(struct gemtag_handle *gh,
+                                unsigned char *key6,unsigned char *key12);
+int gemtag_auth_mifare_key(struct gemtag_handle *gh,
+                           unsigned char *key6,int sector);
+int gemtag_read16(struct gemtag_handle *gh,int sector,unsigned char *data);
+int gemtag_write16(struct gemtag_handle *gh,int sector,unsigned char *data);
+int gemtag_select_picc(struct gemtag_handle *gh);
+
 #endif