- int error = 0;
- switch(tag_type) {
- case 0x1d:
- DbpString("MIM 256 card found, reading card ...");
- command_size = 9;
- card_size = 256;
- response_obfuscation = 0x52;
- break;
- case 0x3d:
- DbpString("MIM 1024 card found, reading card ...");
- command_size = 11;
- card_size = 1024;
- response_obfuscation = 0xd4;
- break;
- default:
- DbpString("No or unknown card found, aborting");
- error = 1;
- break;
+ WDT_HIT();
+}
+/* calculate crc for a legic command */
+static int LegicCRC(int byte_index, int value, int cmd_sz) {
+ crc_clear(&legic_crc);
+ crc_update(&legic_crc, 1, 1); /* CMD_READ */
+ crc_update(&legic_crc, byte_index, cmd_sz-1);
+ crc_update(&legic_crc, value, 8);
+ return crc_finish(&legic_crc);
+}
+
+int legic_read_byte(int byte_index, int cmd_sz) {
+ int byte;
+
+ legic_prng_forward(4); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us + 100us*delay */
+
+ frame_send_rwd(1 | (byte_index << 1), cmd_sz);
+ frame_clean(¤t_frame);
+
+ frame_receive_rwd(¤t_frame, 12, 1);
+
+ byte = current_frame.data & 0xff;
+ if( LegicCRC(byte_index, byte, cmd_sz) != (current_frame.data >> 8) ) {
+ Dbprintf("!!! crc mismatch: expected %x but got %x !!!", LegicCRC(byte_index, current_frame.data & 0xff, cmd_sz), current_frame.data >> 8);
+ return -1;