#!/usr/bin/python2
#  readmifareultra.py - read all sectors from a Ultralight tag
#
#  Keith Howell <kch@kch.net>
#    built on the code by:
#  Adam Laurie <adam@algroup.co.uk>
#  http://rfidiot.org/
#
#  This code is copyright (c) Adam Laurie, 2006, All rights reserved.
#  For non-commercial use only, the following terms apply - for all other
#  uses, please contact the author:
#
#    This code is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This code is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#


import sys
# import os

try:
    import rfidiot
    card = rfidiot.card
except Exception as e:
    print("Couldn't open reader! (%s)" % e)
    sys.exit(True)

rf_help = rfidiot.help

if rf_help:
    print(sys.argv[0] + " - read mifare ultralight tags")
    print("Usage: " + sys.argv[0])
    print()
    sys.exit(True)

card.info("readmifareultra v3.0c")
card.waitfortag("Waiting for Mifare Ultralight...")

blocks = 16

print("\n  ID: " + card.uid)
print("Type: " + card.tagtype)

card.select()
# pull header block information from the tag. A read failure here (e.g. the tag
# is not a Type 2 / Ultralight, or the field dropped) leaves the header values
# unset, so bail out cleanly rather than crashing later (NameError on lock0).
if card.readblock(0):
    sn0 = card.data[0:2]
    sn1 = card.data[2:4]
    sn2 = card.data[4:6]
    bcc0 = card.data[6:8]
else:
    print("read error: %s" % card.errorcode)
    sys.exit(True)

if card.readblock(1):
    sn3 = card.data[0:2]
    sn4 = card.data[2:4]
    sn5 = card.data[4:6]
    sn6 = card.data[6:8]
else:
    print("read error: %s" % card.errorcode)
    sys.exit(True)

if card.readblock(2):
    bcc1 = card.data[0:2]
    internal = card.data[2:4]
    lock0 = card.data[4:6]
    lock1 = card.data[6:8]
else:
    print("read error: %s" % card.errorcode)
    sys.exit(True)

if card.readblock(3):
    otp0 = card.data[0:2]
    otp1 = card.data[2:4]
    otp2 = card.data[4:6]
    otp3 = card.data[6:8]
else:
    print("read error: %s" % card.errorcode)
    sys.exit(True)

# convert lock bytes to binary for later use
lbits0 = card.ToBinaryString(card.ToBinary(lock0))
lbits1 = card.ToBinaryString(card.ToBinary(lock1))
lbits = lbits1 + lbits0

y = 0
plock = ""
for x in range(15, -1, -1):
    plock = lbits[y : y + 1] + plock
    y += 1

# show status of the OTP area on the tag
print("OTP area is ", end="")
if int(plock[3:4]) == 1:
    print("locked and ", end="")
else:
    print("unlocked and ", end="")
if int(plock[0:1]) == 1:
    print("cannot be changed")
else:
    print("can be changed")

print("If locked, blocks 4 through 9 ", end="")
if int(plock[1:2]) == 1:
    print("cannot be unlocked")
else:
    print("can be unlocked")

print("If locked, blocks 0a through 0f ", end="")
if int(plock[2:3]) == 1:
    print("cannot be unlocked")
else:
    print("can be unlocked")

print("\nTag Data:")
# DATA0 byte starts on page/block 4
for x in range(blocks):
    print("    Block %02x:" % x, end="")
    if card.readblock(x):
        print(card.data[:8], end="")
        print(card.ReadablePrint(card.ToBinary(card.data[:8])), end="")
        if x > 2:
            if int(plock[x : x + 1]) == 1:
                print("  locked")
            else:
                print("  unlocked")
        else:
            print("  -")
    else:
        print("read error: %s" % card.errorcode)
print()

if x > 0:
    sys.exit(False)
else:
    sys.exit(True)
