+static bool topaz_block_is_locked(uint8_t blockno, uint8_t *lockbits)
+{
+ if(lockbits[blockno/8] >> (blockno % 8) & 0x01) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+
+static int topaz_print_CC(uint8_t *data)
+{
+ if(data[0] != 0xe1) {
+ return -1; // no NDEF message
+ }
+
+ PrintAndLog("Capability Container: %02x %02x %02x %02x", data[0], data[1], data[2], data[3]);
+ PrintAndLog(" %02x: NDEF Magic Number", data[0]);
+ PrintAndLog(" %02x: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
+ PrintAndLog(" %02x: Physical Memory Size of this tag: %d bytes", data[2], (data[2] + 1) * 8);
+ PrintAndLog(" %02x: %s / %s", data[3],
+ (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
+ (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
+ return 0;
+}
+
+
+static void get_TLV(uint8_t **TLV_ptr, uint8_t *tag, uint16_t *length, uint8_t **value)
+{
+ *length = 0;
+ *value = NULL;
+
+ *tag = **TLV_ptr;
+ *TLV_ptr += 1;
+ switch (*tag) {
+ case 0x00: // NULL TLV.
+ case 0xFE: // Terminator TLV.
+ break;
+ case 0x01: // Lock Control TLV
+ case 0x02: // Reserved Memory TLV
+ case 0x03: // NDEF message TLV
+ case 0xFD: // proprietary TLV
+ *length = **TLV_ptr;
+ *TLV_ptr += 1;
+ if (*length == 0xff) {
+ *length = **TLV_ptr << 8;
+ *TLV_ptr += 1;
+ *length |= **TLV_ptr;
+ *TLV_ptr += 1;
+ }
+ *value = *TLV_ptr;
+ *TLV_ptr += *length;
+ break;
+ default: // RFU
+ break;
+ }
+}
+
+
+static bool topaz_print_lock_control_TLVs(uint8_t *memory)
+{
+ uint8_t *TLV_ptr = memory;
+ uint8_t tag = 0;
+ uint16_t length;
+ uint8_t *value;
+ bool lock_TLV_present = false;
+
+ while(*TLV_ptr != 0x03 && *TLV_ptr != 0xFD && *TLV_ptr != 0xFE) {
+ // all Lock Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
+ get_TLV(&TLV_ptr, &tag, &length, &value);
+ if (tag == 0x01) { // the Lock Control TLV
+ uint8_t pages_addr = value[0] >> 4;
+ uint8_t byte_offset = value[0] & 0x0f;
+ uint8_t size_in_bits = value[1] ? value[1] : 256;
+ uint8_t bytes_per_page = 1 << (value[2] & 0x0f);
+ uint8_t bytes_locked_per_bit = 1 << (value[2] >> 4);
+ PrintAndLog("Lock Area of %d bits at byte offset 0x%02x. Each Lock Bit locks %d bytes.",
+ size_in_bits,
+ pages_addr * bytes_per_page + byte_offset,
+ bytes_locked_per_bit);
+ lock_TLV_present = true;
+ }
+ }
+
+ if (!lock_TLV_present) {
+ PrintAndLog("(No Lock Control TLV present)");
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+
+static int topaz_print_reserved_memory_control_TLVs(uint8_t *memory)
+{
+ uint8_t *TLV_ptr = memory;
+ uint8_t tag = 0;
+ uint16_t length;
+ uint8_t *value;
+ bool reserved_memory_control_TLV_present = false;
+
+ while(*TLV_ptr != 0x03 && *TLV_ptr != 0xFD && *TLV_ptr != 0xFE) {
+ // all Reserved Memory Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
+ get_TLV(&TLV_ptr, &tag, &length, &value);
+ if (tag == 0x02) { // the Reserved Memory Control TLV
+ uint8_t pages_addr = value[0] >> 4;
+ uint8_t byte_offset = value[0] & 0x0f;
+ uint8_t size_in_bytes = value[1] ? value[1] : 256;
+ uint8_t bytes_per_page = 1 << (value[2] & 0x0f);
+ PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.",
+ size_in_bytes,
+ pages_addr * bytes_per_page + byte_offset);
+ reserved_memory_control_TLV_present = true;
+ }
+ }
+
+ if (!reserved_memory_control_TLV_present) {
+ PrintAndLog("(No Reserved Memory Control TLV present)");
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+
+static void topaz_print_lifecycle_state(uint8_t *data)
+{
+
+}
+
+
+static void topaz_print_NDEF(uint8_t *data)
+{
+
+}
+
+