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;
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 */
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;
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();
printf("\n");
*/
- if(gemtag_pick_picc(gh)) {
+ if(gemtag_select_picc(gh)) {
printf("no card found!\n");
return -NO_PICC;
}
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);
#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 */
#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