fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / cmdhftopaz.c
blob45724ac5d4f121ca875c1ce5fb946c2beeb1b043
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015 Piwi
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // High frequency Topaz (NFC Type 1) commands
9 //-----------------------------------------------------------------------------
10 #include "cmdhftopaz.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
17 #include "cliparser.h"
18 #include "cmdparser.h" // command_t
19 #include "comms.h"
20 #include "cmdtrace.h"
21 #include "cmdhf14a.h"
22 #include "ui.h"
23 #include "crc16.h"
24 #include "protocols.h"
25 #include "nfc/ndef.h"
27 #define TOPAZ_STATIC_MEMORY (0x0f * 8) // 15 blocks with 8 Bytes each
29 // a struct to describe a memory area which contains lock bits and the corresponding lockable memory area
30 typedef struct dynamic_lock_area {
31 struct dynamic_lock_area *next;
32 uint16_t byte_offset; // the address of the lock bits
33 uint16_t size_in_bits;
34 uint16_t first_locked_byte; // the address of the lockable area
35 uint16_t bytes_locked_per_bit;
36 } dynamic_lock_area_t;
38 static struct {
39 uint8_t HR01[2];
40 uint8_t uid[7];
41 uint16_t size;
42 uint8_t data_blocks[TOPAZ_STATIC_MEMORY / 8][8]; // this memory is always there
43 uint8_t *dynamic_memory; // this memory can be there
44 dynamic_lock_area_t *dynamic_lock_areas; // lock area descriptors
45 } topaz_tag;
47 static void topaz_switch_on_field(void) {
48 SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_CONNECT | ISO14A_NO_SELECT | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE | ISO14A_NO_RATS, 0, 0, NULL, 0);
51 static void topaz_switch_off_field(void) {
52 SetISODEPState(ISODEP_INACTIVE);
53 SendCommandMIX(CMD_HF_ISO14443A_READER, 0, 0, 0, NULL, 0);
56 // send a raw topaz command, returns the length of the response (0 in case of error)
57 static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response, uint16_t *response_len, bool verbose) {
58 SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE | ISO14A_NO_RATS, len, 0, cmd, len);
59 PacketResponseNG resp;
60 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) {
61 if (verbose) PrintAndLogEx(WARNING, "timeout while waiting for reply.");
62 return PM3_ETIMEOUT;
65 if (resp.oldarg[0] == *response_len) {
66 *response_len = resp.oldarg[0];
68 PrintAndLogEx(DEBUG, "%s", sprint_hex(resp.data.asBytes, *response_len));
69 if (*response_len > 0) {
70 memcpy(response, resp.data.asBytes, *response_len);
72 } else {
73 if (verbose) PrintAndLogEx(WARNING, "Wrong response length (%d != %" PRIu64 ")", *response_len, resp.oldarg[0]);
74 return PM3_ESOFT;
76 return PM3_SUCCESS;
79 // calculate CRC bytes and send topaz command, returns the length of the response (0 in case of error)
80 static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response, uint16_t *response_len, bool verbose) {
81 if (len > 1) {
82 uint8_t b1, b2;
83 compute_crc(CRC_14443_B, cmd, len - 2, &b1, &b2);
84 cmd[len - 2] = b1;
85 cmd[len - 1] = b2;
88 return topaz_send_cmd_raw(cmd, len, response, response_len, verbose);
91 // select a topaz tag. Send WUPA and RID.
92 static int topaz_select(uint8_t *atqa, uint8_t atqa_len, uint8_t *rid_response, uint8_t rid_len, bool verbose) {
93 // ToDo: implement anticollision
95 uint16_t resp_len;
96 uint8_t wupa_cmd[] = {TOPAZ_WUPA};
97 uint8_t rid_cmd[] = {TOPAZ_RID, 0, 0, 0, 0, 0, 0, 0, 0};
99 topaz_switch_on_field();
101 resp_len = atqa_len;
102 int status = topaz_send_cmd(wupa_cmd, sizeof(wupa_cmd), atqa, &resp_len, verbose);
103 if (status == PM3_ETIMEOUT || status == PM3_ESOFT) {
104 topaz_switch_off_field();
105 return PM3_ESOFT; // WUPA failed
108 resp_len = rid_len;
109 status = topaz_send_cmd(rid_cmd, sizeof(rid_cmd), rid_response, &resp_len, verbose);
110 if (status == PM3_ETIMEOUT || status == PM3_ESOFT) {
111 topaz_switch_off_field();
112 return PM3_EWRONGANSWER; // RID failed
115 return PM3_SUCCESS;
118 // read all of the static memory of a selected Topaz tag.
119 static int topaz_rall(uint8_t *uid, uint8_t *response) {
120 uint16_t resp_len = 0;
121 uint8_t rall_cmd[] = {TOPAZ_RALL, 0, 0, 0, 0, 0, 0, 0, 0};
122 memcpy(&rall_cmd[3], uid, 4);
124 if (topaz_send_cmd(rall_cmd, sizeof(rall_cmd), response, &resp_len, true) == PM3_ETIMEOUT) {
125 topaz_switch_off_field();
126 return PM3_ESOFT; // RALL failed
129 return PM3_SUCCESS;
132 // read a block (8 Bytes) of a selected Topaz tag.
133 static int topaz_read_block(uint8_t *uid, uint8_t blockno, uint8_t *block_data) {
134 uint16_t resp_len = 0;
135 uint8_t read8_cmd[] = {TOPAZ_READ8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
136 uint8_t read8_response[11];
138 read8_cmd[1] = blockno;
139 memcpy(&read8_cmd[10], uid, 4);
141 if (topaz_send_cmd(read8_cmd, sizeof(read8_cmd), read8_response, &resp_len, true) == PM3_ETIMEOUT) {
142 topaz_switch_off_field();
143 return PM3_ESOFT; // READ8 failed
145 memcpy(block_data, &read8_response[1], 8);
146 return PM3_SUCCESS;
149 // read a segment (16 blocks = 128 Bytes) of a selected Topaz tag. Works only for tags with dynamic memory.
150 static int topaz_read_segment(uint8_t *uid, uint8_t segno, uint8_t *segment_data) {
151 uint16_t resp_len = 0;
152 uint8_t rseg_cmd[] = {TOPAZ_RSEG, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
153 uint8_t rseg_response[131];
155 rseg_cmd[1] = segno << 4;
156 memcpy(&rseg_cmd[10], uid, 4);
158 if (topaz_send_cmd(rseg_cmd, sizeof(rseg_cmd), rseg_response, &resp_len, true) == PM3_ETIMEOUT) {
159 topaz_switch_off_field();
160 return PM3_ESOFT; // RSEG failed
162 memcpy(segment_data, &rseg_response[1], 128);
163 return PM3_SUCCESS;
166 // search for the lock area descriptor for the lockable area including byteno
167 static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno) {
168 dynamic_lock_area_t *lock_area;
169 lock_area = topaz_tag.dynamic_lock_areas;
171 while (lock_area != NULL) {
172 if (byteno < lock_area->first_locked_byte) {
173 lock_area = lock_area->next;
174 } else {
175 return lock_area;
178 return NULL;
181 // check if a memory byte is locked.
182 static bool topaz_byte_is_locked(uint16_t byteno) {
183 uint8_t *lockbits;
184 uint16_t locked_bytes_per_bit;
185 dynamic_lock_area_t *lock_area;
187 if (byteno < TOPAZ_STATIC_MEMORY) {
188 lockbits = &topaz_tag.data_blocks[0x0e][0];
189 locked_bytes_per_bit = 8;
190 } else {
191 lock_area = get_dynamic_lock_area(byteno);
192 if (lock_area == NULL) {
193 return false;
194 } else {
195 lockbits = &topaz_tag.dynamic_memory[lock_area->byte_offset - TOPAZ_STATIC_MEMORY];
196 locked_bytes_per_bit = lock_area->bytes_locked_per_bit;
197 byteno = byteno - lock_area->first_locked_byte;
201 uint16_t blockno = byteno / locked_bytes_per_bit;
202 if (lockbits[blockno / 8] & (0x01 << (blockno % 8))) {
203 return true;
204 } else {
205 return false;
209 // read and print the Capability Container
210 static int topaz_print_CC(uint8_t *data) {
211 if (data[0] != 0xe1) {
212 topaz_tag.size = TOPAZ_STATIC_MEMORY;
213 return PM3_ESOFT; // no NDEF message
216 PrintAndLogEx(SUCCESS, "Capability Container: %02x %02x %02x %02x", data[0], data[1], data[2], data[3]);
217 PrintAndLogEx(SUCCESS, " %02x: NDEF Magic Number", data[0]);
218 PrintAndLogEx(SUCCESS, " %02x: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
219 uint16_t memsize = (data[2] + 1) * 8;
220 topaz_tag.size = memsize;
221 topaz_tag.dynamic_memory = calloc(memsize - TOPAZ_STATIC_MEMORY, sizeof(uint8_t));
222 PrintAndLogEx(SUCCESS, " %02x: Physical Memory Size of this tag: %d bytes", data[2], memsize);
223 PrintAndLogEx(SUCCESS, " %02x: %s / %s", data[3],
224 (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
225 (data[3] & 0x0F) == 0 ? "Write access granted without any security" : (data[3] & 0x0F) == 0x0F ? "No write access granted at all" : "(RFU)");
226 return PM3_SUCCESS;
229 // return type, length and value of a TLV, starting at memory position *TLV_ptr
230 static void get_TLV(uint8_t **TLV_ptr, uint8_t *TLV_type, uint16_t *TLV_length, uint8_t **TLV_value) {
231 *TLV_length = 0;
232 *TLV_value = NULL;
234 *TLV_type = **TLV_ptr;
235 *TLV_ptr += 1;
236 switch (*TLV_type) {
237 case 0x00: // NULL TLV.
238 case 0xFE: // Terminator TLV.
239 break;
240 case 0x01: // Lock Control TLV
241 case 0x02: // Reserved Memory TLV
242 case 0x03: // NDEF message TLV
243 case 0xFD: // proprietary TLV
244 *TLV_length = **TLV_ptr;
245 *TLV_ptr += 1;
246 if (*TLV_length == 0xff) {
247 *TLV_length = **TLV_ptr << 8;
248 *TLV_ptr += 1;
249 *TLV_length |= **TLV_ptr;
250 *TLV_ptr += 1;
252 *TLV_value = *TLV_ptr;
253 *TLV_ptr += *TLV_length;
254 break;
255 default: // RFU
256 break;
260 // lock area TLVs contain no information on the start of the respective lockable area. Lockable areas
261 // do not include the lock bits and reserved memory. We therefore need to adjust the start of the
262 // respective lockable areas accordingly
263 static void adjust_lock_areas(uint16_t block_start, uint16_t block_size) {
264 dynamic_lock_area_t *lock_area = topaz_tag.dynamic_lock_areas;
265 while (lock_area != NULL) {
266 if (lock_area->first_locked_byte <= block_start) {
267 lock_area->first_locked_byte += block_size;
269 lock_area = lock_area->next;
273 // read and print the lock area and reserved memory TLVs
274 static void topaz_print_control_TLVs(uint8_t *memory) {
275 uint8_t *TLV_ptr = memory;
276 uint8_t TLV_type = 0;
277 uint16_t TLV_length;
278 uint8_t *TLV_value;
279 bool lock_TLV_present = false;
280 bool reserved_memory_control_TLV_present = false;
281 uint16_t next_lockable_byte = 0x0f * 8; // first byte after static memory area
283 while (*TLV_ptr != 0x03 && *TLV_ptr != 0xFD && *TLV_ptr != 0xFE) {
284 // all Lock Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
285 get_TLV(&TLV_ptr, &TLV_type, &TLV_length, &TLV_value);
286 if (TLV_type == 0x01) { // a Lock Control TLV
287 uint8_t pages_addr = TLV_value[0] >> 4;
288 uint8_t byte_offset = TLV_value[0] & 0x0f;
289 uint16_t size_in_bits = TLV_value[1] ? TLV_value[1] : 256;
290 uint16_t size_in_bytes = (size_in_bits + 7) / 8;
291 uint16_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
292 uint16_t bytes_locked_per_bit = 1 << (TLV_value[2] >> 4);
293 uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
294 PrintAndLogEx(SUCCESS, "Lock Area of %d bits at byte offset 0x%04x. Each Lock Bit locks %d bytes.",
295 size_in_bits,
296 area_start,
297 bytes_locked_per_bit);
298 lock_TLV_present = true;
299 dynamic_lock_area_t *old = topaz_tag.dynamic_lock_areas;
300 dynamic_lock_area_t *new;
301 if (old == NULL) {
302 new = topaz_tag.dynamic_lock_areas = (dynamic_lock_area_t *) calloc(sizeof(dynamic_lock_area_t), sizeof(uint8_t));
303 } else {
304 while (old->next != NULL) {
305 old = old->next;
307 new = old->next = (dynamic_lock_area_t *) calloc(sizeof(dynamic_lock_area_t), sizeof(uint8_t));
309 new->next = NULL;
310 if (area_start <= next_lockable_byte) {
311 // lock areas are not lockable
312 next_lockable_byte += size_in_bytes;
314 new->first_locked_byte = next_lockable_byte;
315 new->byte_offset = area_start;
316 new->size_in_bits = size_in_bits;
317 new->bytes_locked_per_bit = bytes_locked_per_bit;
318 next_lockable_byte += size_in_bits * bytes_locked_per_bit;
320 if (TLV_type == 0x02) { // a Reserved Memory Control TLV
321 uint8_t pages_addr = TLV_value[0] >> 4;
322 uint8_t byte_offset = TLV_value[0] & 0x0f;
323 uint16_t size_in_bytes = TLV_value[1] ? TLV_value[1] : 256;
324 uint8_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
325 uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
326 PrintAndLogEx(SUCCESS, "Reserved Memory of %d bytes at byte offset 0x%02x.",
327 size_in_bytes,
328 area_start);
329 reserved_memory_control_TLV_present = true;
330 adjust_lock_areas(area_start, size_in_bytes); // reserved memory areas are not lockable
331 if (area_start <= next_lockable_byte) {
332 next_lockable_byte += size_in_bytes;
337 if (!lock_TLV_present) {
338 PrintAndLogEx(SUCCESS, "(No Lock Control TLV present)");
341 if (!reserved_memory_control_TLV_present) {
342 PrintAndLogEx(SUCCESS, "(No Reserved Memory Control TLV present)");
346 // read all of the dynamic memory
347 static int topaz_read_dynamic_data(void) {
348 // first read the remaining block of segment 0
349 if (topaz_read_block(topaz_tag.uid, 0x0F, &topaz_tag.dynamic_memory[0]) == PM3_ESOFT) {
350 PrintAndLogEx(ERR, "Error while reading dynamic memory block " _YELLOW_("%02x") ". Aborting...", 0x0F);
351 return PM3_ESOFT;
354 // read the remaining segments
355 uint8_t max_segment = topaz_tag.size / 128 - 1;
356 for (uint8_t segment = 1; segment <= max_segment; segment++) {
357 if (topaz_read_segment(topaz_tag.uid, segment, &topaz_tag.dynamic_memory[(segment - 1) * 128 + 8]) == PM3_ESOFT) {
358 PrintAndLogEx(ERR, "Error while reading dynamic memory block " _YELLOW_("%02x") ". Aborting...", segment);
359 return PM3_ESOFT;
362 return PM3_SUCCESS;
365 // read and print the dynamic memory
366 static void topaz_print_dynamic_data(void) {
367 if (topaz_tag.size > TOPAZ_STATIC_MEMORY) {
368 PrintAndLogEx(SUCCESS, "Dynamic Data blocks:");
369 if (topaz_read_dynamic_data() == 0) {
370 PrintAndLogEx(NORMAL, "block# | offset | Data | Locked(y/n)");
371 PrintAndLogEx(NORMAL, "-------+--------+-------------------------+------------");
372 char line[80];
373 for (uint16_t blockno = 0x0F; blockno < topaz_tag.size / 8; blockno++) {
374 uint8_t *block_data = &topaz_tag.dynamic_memory[(blockno - 0x0F) * 8];
375 char lockbits[9];
376 for (uint16_t j = 0; j < 8; j++) {
377 sprintf(&line[3 * j], "%02x ", block_data[j]);
378 lockbits[j] = topaz_byte_is_locked(blockno * 8 + j) ? 'y' : 'n';
380 lockbits[8] = '\0';
381 PrintAndLogEx(NORMAL, " 0x%02x | 0x%04x | %s| %-3s", blockno, blockno * 8, line, lockbits);
387 static void topaz_print_lifecycle_state(uint8_t *data) {
388 // to be done
391 static int topaz_print_NDEF(uint8_t *data, size_t maxsize) {
392 return NDEFDecodeAndPrint(data, maxsize, true);
395 static int CmdHFTopazReader(const char *Cmd) {
396 CLIParserContext *ctx;
397 CLIParserInit(&ctx, "hf topaz reader",
398 "Read UID from Topaz tags",
399 "hf topaz reader");
401 void *argtable[] = {
402 arg_param_begin,
403 arg_lit0("v", "verbose", "verbose output"),
404 arg_param_end
406 CLIExecWithReturn(ctx, Cmd, argtable, true);
408 bool verbose = arg_get_lit(ctx, 1);
410 CLIParserFree(ctx);
412 return readTopazUid(verbose);
415 // read a Topaz tag and print some useful information
416 int CmdHFTopazInfo(const char *Cmd) {
417 CLIParserContext *ctx;
418 CLIParserInit(&ctx, "hf topaz info",
419 "Get info from Topaz tags",
420 "hf topaz info");
422 void *argtable[] = {
423 arg_param_begin,
424 arg_lit0("v", "verbose", "verbose output"),
425 arg_param_end
427 CLIExecWithReturn(ctx, Cmd, argtable, true);
429 bool verbose = arg_get_lit(ctx, 1);
431 CLIParserFree(ctx);
433 int status = readTopazUid(verbose);
434 if (status != PM3_SUCCESS)
435 return status;
437 PrintAndLogEx(NORMAL, "");
438 PrintAndLogEx(SUCCESS, "Static Data blocks " _YELLOW_("0x00") " to " _YELLOW_("0x0C")":");
439 PrintAndLogEx(NORMAL, "block# | offset | Data | Locked");
440 PrintAndLogEx(NORMAL, "-------+--------+-------------------------+------------");
441 char line[80];
442 for (uint16_t i = 0; i <= 0x0c; i++) {
443 char lockbits[9];
444 for (uint16_t j = 0; j < 8; j++) {
445 sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[i][j] /*rall_response[2 + 8*i + j]*/);
446 lockbits[j] = topaz_byte_is_locked(i * 8 + j) ? 'y' : 'n';
448 lockbits[8] = '\0';
449 PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", i, i * 8, line, lockbits);
452 PrintAndLogEx(NORMAL, "");
453 PrintAndLogEx(SUCCESS, "Static Reserved block " _YELLOW_("0x0D")":");
454 for (uint16_t j = 0; j < 8; j++) {
455 sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[0x0d][j]);
457 PrintAndLogEx(NORMAL, "-------+--------+-------------------------+------------");
458 PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0d, 0x0d * 8, line, "n/a");
459 PrintAndLogEx(NORMAL, "");
461 PrintAndLogEx(SUCCESS, "Static Lockbits and OTP Bytes:");
462 for (uint16_t j = 0; j < 8; j++) {
463 sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[0x0e][j]);
465 PrintAndLogEx(NORMAL, "-------+--------+-------------------------+------------");
466 PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0e, 0x0e * 8, line, "n/a");
467 PrintAndLogEx(NORMAL, "");
469 status = topaz_print_CC(&topaz_tag.data_blocks[1][0]);
471 if (status == PM3_ESOFT) {
472 PrintAndLogEx(SUCCESS, "No NDEF message data present");
473 topaz_switch_off_field();
474 return PM3_SUCCESS;
477 PrintAndLogEx(NORMAL, "");
478 topaz_print_control_TLVs(&topaz_tag.data_blocks[1][4]);
480 PrintAndLogEx(NORMAL, "");
481 topaz_print_dynamic_data();
483 topaz_print_lifecycle_state(&topaz_tag.data_blocks[1][0]);
485 topaz_print_NDEF(&topaz_tag.data_blocks[1][0], TOPAZ_STATIC_MEMORY);
487 PrintAndLogEx(INFO, "-------------------------------------------------------------");
488 topaz_switch_off_field();
489 return PM3_SUCCESS;
492 static int CmdHFTopazSim(const char *Cmd) {
493 CLIParserContext *ctx;
494 CLIParserInit(&ctx, "hf topaz sim",
495 "Simulate a Topaz tag",
496 "hf topaz sim -> Not yet implemented");
498 void *argtable[] = {
499 arg_param_begin,
500 arg_param_end
502 CLIExecWithReturn(ctx, Cmd, argtable, true);
503 CLIParserFree(ctx);
504 PrintAndLogEx(INFO, "not yet implemented");
505 return PM3_SUCCESS;
508 static int CmdHFTopazCmdRaw(const char *Cmd) {
509 CLIParserContext *ctx;
510 CLIParserInit(&ctx, "hf topaz raw",
511 "Send raw hex data to Topaz tags",
512 "hf topaz raw -> Not yet implemented");
514 void *argtable[] = {
515 arg_param_begin,
516 arg_param_end
518 CLIExecWithReturn(ctx, Cmd, argtable, true);
519 CLIParserFree(ctx);
521 PrintAndLogEx(INFO, "not yet implemented. Use hf 14 raw with option -T.");
522 return PM3_SUCCESS;
525 static int CmdHFTopazList(const char *Cmd) {
526 return CmdTraceListAlias(Cmd, "hf topaz", "topaz");
529 static int CmdHFTopazSniff(const char *Cmd) {
530 CLIParserContext *ctx;
531 CLIParserInit(&ctx, "hf topaz sniff",
532 "Sniff Topaz reader-tag communication",
533 "hf topaz sniff");
535 void *argtable[] = {
536 arg_param_begin,
537 arg_param_end
539 CLIExecWithReturn(ctx, Cmd, argtable, true);
540 CLIParserFree(ctx);
542 uint8_t param = 0;
543 SendCommandNG(CMD_HF_ISO14443A_SNIFF, (uint8_t *)&param, sizeof(uint8_t));
545 return PM3_SUCCESS;
548 static int CmdHelp(const char *Cmd);
550 static command_t CommandTable[] = {
551 {"help", CmdHelp, AlwaysAvailable, "This help"},
552 {"list", CmdHFTopazList, AlwaysAvailable, "List Topaz history"},
553 {"info", CmdHFTopazInfo, IfPm3Iso14443a, "Tag information"},
554 {"reader", CmdHFTopazReader, IfPm3Iso14443a, "Act like a Topaz reader"},
555 {"sim", CmdHFTopazSim, IfPm3Iso14443a, "<UID> -- Simulate Topaz tag"},
556 {"sniff", CmdHFTopazSniff, IfPm3Iso14443a, "Sniff Topaz reader-tag communication"},
557 {"raw", CmdHFTopazCmdRaw, IfPm3Iso14443a, "Send raw hex data to tag"},
558 {NULL, NULL, 0, NULL}
561 static int CmdHelp(const char *Cmd) {
562 (void)Cmd; // Cmd is not used so far
563 CmdsHelp(CommandTable);
564 return PM3_SUCCESS;
567 int CmdHFTopaz(const char *Cmd) {
568 clearCommandBuffer();
569 return CmdsParse(CommandTable, Cmd);
572 int readTopazUid(bool verbose) {
574 uint8_t atqa[2];
575 uint8_t rid_response[8];
576 uint8_t *uid_echo = &rid_response[2];
577 uint8_t rall_response[124];
579 int status = topaz_select(atqa, sizeof(atqa), rid_response, sizeof(rid_response), verbose);
580 if (status == PM3_ESOFT) {
581 if (verbose) PrintAndLogEx(ERR, "Error: couldn't receive ATQA");
582 return PM3_ESOFT;
585 if (atqa[1] != 0x0c && atqa[0] != 0x00) {
586 if (verbose) PrintAndLogEx(ERR, "Tag doesn't support the Topaz protocol.");
587 topaz_switch_off_field();
588 return PM3_ESOFT;
591 if (status == PM3_EWRONGANSWER) {
592 if (verbose) PrintAndLogEx(ERR, "Error: tag didn't answer to RID");
593 topaz_switch_off_field();
594 return PM3_ESOFT;
597 status = topaz_rall(uid_echo, rall_response);
598 if (status == PM3_ESOFT) {
599 PrintAndLogEx(ERR, "Error: tag didn't answer to RALL");
600 topaz_switch_off_field();
601 return PM3_ESOFT;
604 memcpy(topaz_tag.uid, rall_response + 2, 7);
605 memcpy(topaz_tag.data_blocks, rall_response + 2, 0x0f * 8);
607 // printing
608 PrintAndLogEx(NORMAL, "");
609 PrintAndLogEx(INFO, "--- " _CYAN_("Tag Information") " ---------------------------");
610 PrintAndLogEx(SUCCESS, " UID: %02x %02x %02x %02x %02x %02x %02x",
611 topaz_tag.uid[6],
612 topaz_tag.uid[5],
613 topaz_tag.uid[4],
614 topaz_tag.uid[3],
615 topaz_tag.uid[2],
616 topaz_tag.uid[1],
617 topaz_tag.uid[0]);
619 PrintAndLogEx(SUCCESS, " UID[6] (Manufacturer Byte) = " _YELLOW_("%02x")", Manufacturer: " _YELLOW_("%s"),
620 topaz_tag.uid[6],
621 getTagInfo(topaz_tag.uid[6])
624 PrintAndLogEx(SUCCESS, " ATQA: %02x %02x", atqa[1], atqa[0]);
626 topaz_tag.HR01[0] = rid_response[0];
627 topaz_tag.HR01[1] = rid_response[1];
629 // ToDo: CRC check
630 PrintAndLogEx(SUCCESS, " HR0: %02x (%sa Topaz tag (%scapable of carrying a NDEF message), %s memory map)",
631 rid_response[0],
632 (rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
633 (rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
634 (rid_response[0] & 0x0F) == 0x01 ? "static" : "dynamic");
636 PrintAndLogEx(SUCCESS, " HR1: %02x", rid_response[1]);
638 topaz_switch_off_field();
639 return PM3_SUCCESS;