+ dynamic_lock_area_t *lock_area;
+
+ lock_area = topaz_tag.dynamic_lock_areas;
+
+ while (lock_area != NULL) {
+ if (byteno < lock_area->first_locked_byte) {
+ lock_area = lock_area->next;
+ } else {
+ return lock_area;
+ }
+ }
+
+ return NULL;
+}
+
+
+// check if a memory byte is locked.
+static bool topaz_byte_is_locked(uint16_t byteno)
+{
+ uint8_t *lockbits;
+ uint16_t locked_bytes_per_bit;
+ dynamic_lock_area_t *lock_area;
+
+ if (byteno < TOPAZ_STATIC_MEMORY) {
+ lockbits = &topaz_tag.data_blocks[0x0e][0];
+ locked_bytes_per_bit = 8;
+ } else {
+ lock_area = get_dynamic_lock_area(byteno);
+ if (lock_area == NULL) {
+ return false;
+ } else {
+ lockbits = &topaz_tag.dynamic_memory[lock_area->byte_offset - TOPAZ_STATIC_MEMORY];
+ locked_bytes_per_bit = lock_area->bytes_locked_per_bit;
+ byteno = byteno - lock_area->first_locked_byte;
+ }
+ }
+
+ uint16_t blockno = byteno / locked_bytes_per_bit;
+ if(lockbits[blockno/8] & (0x01 << (blockno % 8))) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+
+// read and print the Capability Container
+static int topaz_print_CC(uint8_t *data)
+{
+ if(data[0] != 0xe1) {
+ topaz_tag.size = TOPAZ_STATIC_MEMORY;
+ 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);
+ uint16_t memsize = (data[2] + 1) * 8;
+ topaz_tag.size = memsize;
+ topaz_tag.dynamic_memory = malloc(memsize - TOPAZ_STATIC_MEMORY);
+ PrintAndLog(" %02x: Physical Memory Size of this tag: %d bytes", data[2], memsize);
+ 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;
+}
+
+
+// return type, length and value of a TLV, starting at memory position *TLV_ptr
+static void get_TLV(uint8_t **TLV_ptr, uint8_t *TLV_type, uint16_t *TLV_length, uint8_t **TLV_value)
+{
+ *TLV_length = 0;
+ *TLV_value = NULL;
+
+ *TLV_type = **TLV_ptr;
+ *TLV_ptr += 1;
+ switch (*TLV_type) {
+ 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
+ *TLV_length = **TLV_ptr;
+ *TLV_ptr += 1;
+ if (*TLV_length == 0xff) {
+ *TLV_length = **TLV_ptr << 8;
+ *TLV_ptr += 1;
+ *TLV_length |= **TLV_ptr;
+ *TLV_ptr += 1;
+ }
+ *TLV_value = *TLV_ptr;
+ *TLV_ptr += *TLV_length;
+ break;
+ default: // RFU
+ break;
+ }
+}
+
+
+// lock area TLVs contain no information on the start of the respective lockable area. Lockable areas
+// do not include the lock bits and reserved memory. We therefore need to adjust the start of the
+// respective lockable areas accordingly
+static void adjust_lock_areas(uint16_t block_start, uint16_t block_size)
+{
+ dynamic_lock_area_t *lock_area = topaz_tag.dynamic_lock_areas;
+ while (lock_area != NULL) {
+ if (lock_area->first_locked_byte <= block_start) {
+ lock_area->first_locked_byte += block_size;
+ }
+ lock_area = lock_area->next;
+ }
+}
+
+
+// read and print the lock area and reserved memory TLVs
+static void topaz_print_control_TLVs(uint8_t *memory)
+{
+ uint8_t *TLV_ptr = memory;
+ uint8_t TLV_type = 0;
+ uint16_t TLV_length;
+ uint8_t *TLV_value;
+ bool lock_TLV_present = false;
+ bool reserved_memory_control_TLV_present = false;
+ uint16_t next_lockable_byte = 0x0f * 8; // first byte after static memory area
+
+ 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, &TLV_type, &TLV_length, &TLV_value);
+ if (TLV_type == 0x01) { // a Lock Control TLV
+ uint8_t pages_addr = TLV_value[0] >> 4;
+ uint8_t byte_offset = TLV_value[0] & 0x0f;
+ uint16_t size_in_bits = TLV_value[1] ? TLV_value[1] : 256;
+ uint16_t size_in_bytes = (size_in_bits + 7)/8;
+ uint16_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
+ uint16_t bytes_locked_per_bit = 1 << (TLV_value[2] >> 4);
+ uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
+ PrintAndLog("Lock Area of %d bits at byte offset 0x%04x. Each Lock Bit locks %d bytes.",
+ size_in_bits,
+ area_start,
+ bytes_locked_per_bit);
+ lock_TLV_present = true;
+ dynamic_lock_area_t *old = topaz_tag.dynamic_lock_areas;
+ dynamic_lock_area_t *new = topaz_tag.dynamic_lock_areas;
+ if (old == NULL) {
+ new = topaz_tag.dynamic_lock_areas = (dynamic_lock_area_t *)malloc(sizeof(dynamic_lock_area_t));
+ } else {
+ while(old->next != NULL) {
+ old = old->next;
+ }
+ new = old->next = (dynamic_lock_area_t *)malloc(sizeof(dynamic_lock_area_t));
+ }
+ new->next = NULL;
+ if (area_start <= next_lockable_byte) {
+ // lock areas are not lockable
+ next_lockable_byte += size_in_bytes;
+ }
+ new->first_locked_byte = next_lockable_byte;
+ new->byte_offset = area_start;
+ new->size_in_bits = size_in_bits;
+ new->bytes_locked_per_bit = bytes_locked_per_bit;
+ next_lockable_byte += size_in_bits * bytes_locked_per_bit;
+ }
+ if (TLV_type == 0x02) { // a Reserved Memory Control TLV
+ uint8_t pages_addr = TLV_value[0] >> 4;
+ uint8_t byte_offset = TLV_value[0] & 0x0f;
+ uint16_t size_in_bytes = TLV_value[1] ? TLV_value[1] : 256;
+ uint8_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
+ uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
+ PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.",
+ size_in_bytes,
+ area_start);
+ reserved_memory_control_TLV_present = true;
+ adjust_lock_areas(area_start, size_in_bytes); // reserved memory areas are not lockable
+ if (area_start <= next_lockable_byte) {
+ next_lockable_byte += size_in_bytes;
+ }
+ }
+ }
+
+ if (!lock_TLV_present) {
+ PrintAndLog("(No Lock Control TLV present)");
+ }
+
+ if (!reserved_memory_control_TLV_present) {
+ PrintAndLog("(No Reserved Memory Control TLV present)");
+ }
+}
+
+
+// read all of the dynamic memory
+static int topaz_read_dynamic_data(void)
+{
+ // first read the remaining block of segment 0
+ if(topaz_read_block(topaz_tag.uid, 0x0f, &topaz_tag.dynamic_memory[0]) == -1) {
+ PrintAndLog("Error while reading dynamic memory block %02x. Aborting...", 0x0f);
+ return -1;
+ }
+
+ // read the remaining segments
+ uint8_t max_segment = topaz_tag.size / 128 - 1;
+ for(uint8_t segment = 1; segment <= max_segment; segment++) {
+ if(topaz_read_segment(topaz_tag.uid, segment, &topaz_tag.dynamic_memory[(segment-1)*128+8]) == -1) {
+ PrintAndLog("Error while reading dynamic memory block %02x. Aborting...", 0x0f);
+ return -1;
+ }
+ }
+