2021-02-20 03:14:11 +08:00
|
|
|
local getopt = require('getopt')
|
|
|
|
local utils = require('utils')
|
|
|
|
local ac = require('ansicolors')
|
|
|
|
|
|
|
|
copyright = ''
|
|
|
|
author = "Christian Herrmann"
|
2021-02-23 04:58:09 +08:00
|
|
|
version = 'v1.0.2'
|
2021-02-20 03:14:11 +08:00
|
|
|
desc = [[
|
|
|
|
Perform bulk EM410x enrollment of T5577 RFID tags. It keeps track of last card id used.
|
|
|
|
If called with -s, this value resets "session".
|
|
|
|
|
|
|
|
if press <enter> it defaults to Y, which writes a ID.
|
|
|
|
Any other input char will exit the script.
|
|
|
|
|
2021-02-23 04:58:09 +08:00
|
|
|
You can supply a password, which will set the config block / block 7 on the T5577.
|
|
|
|
|
2021-04-08 16:44:31 +08:00
|
|
|
The verify option will issue a 'lf em 410x reader' command, so you can manually verify
|
2021-02-23 04:58:09 +08:00
|
|
|
that the write worked.
|
|
|
|
|
2021-02-20 03:14:11 +08:00
|
|
|
]]
|
|
|
|
example = [[
|
2021-04-08 16:44:31 +08:00
|
|
|
-- resets and start enrolling EM410x id 11CC334455
|
2021-02-20 03:14:11 +08:00
|
|
|
script run lf_em4100_bulk.lua -s 11CC334455
|
|
|
|
|
|
|
|
-- continue enrolling from where last iteration
|
|
|
|
script run lf_em4100_bulk.lua -c
|
2021-02-23 04:58:09 +08:00
|
|
|
|
|
|
|
-- reset and start enrolling from 11223344,
|
|
|
|
-- protecting the tag with password 010203
|
|
|
|
-- and verify the em id write.
|
2021-04-08 16:44:31 +08:00
|
|
|
script run lf_em4100_bulk.lua -s 1122334455 -p 01020304 -v
|
2021-02-20 03:14:11 +08:00
|
|
|
]]
|
|
|
|
usage = [[
|
2021-02-23 04:58:09 +08:00
|
|
|
script run lf_en4100_bulk.lua [-h] [-c] [-p password] [-s <start cn>] [-v]
|
2021-02-20 03:14:11 +08:00
|
|
|
]]
|
|
|
|
arguments = [[
|
|
|
|
-h : this help
|
|
|
|
-c : continue from last card number used
|
2021-02-23 04:58:09 +08:00
|
|
|
-p : Password protecting the T5577.
|
2021-02-20 03:14:11 +08:00
|
|
|
-s : starting card number
|
2021-02-23 04:58:09 +08:00
|
|
|
-v : verify write by executing a `lf em 410x reader`
|
2021-02-20 03:14:11 +08:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- Some globals
|
|
|
|
local DEBUG = false
|
|
|
|
local ENROLL_STATUS_FN = 'lf_em4100_status.txt'
|
|
|
|
---
|
|
|
|
-- A debug printout-function
|
|
|
|
local function dbg(args)
|
|
|
|
if not DEBUG then return end
|
|
|
|
if type(args) == 'table' then
|
|
|
|
local i = 1
|
|
|
|
while args[i] do
|
|
|
|
dbg(args[i])
|
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print('###', args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
---
|
|
|
|
-- This is only meant to be used when errors occur
|
|
|
|
local function oops(err)
|
|
|
|
print('ERROR:', err)
|
|
|
|
core.clearCommandBuffer()
|
|
|
|
return nil, errr
|
|
|
|
end
|
|
|
|
---
|
|
|
|
-- Usage help
|
|
|
|
local function help()
|
|
|
|
print(copyright)
|
|
|
|
print(author)
|
|
|
|
print(version)
|
|
|
|
print(desc)
|
|
|
|
print(ac.cyan..'Usage'..ac.reset)
|
|
|
|
print(usage)
|
|
|
|
print(ac.cyan..'Arguments'..ac.reset)
|
|
|
|
print(arguments)
|
|
|
|
print(ac.cyan..'Example usage'..ac.reset)
|
|
|
|
print(example)
|
|
|
|
end
|
|
|
|
---
|
|
|
|
-- Exit message
|
|
|
|
local function exitMsg(msg)
|
|
|
|
print( string.rep('--',20) )
|
|
|
|
print( string.rep('--',20) )
|
|
|
|
print(msg)
|
|
|
|
print()
|
|
|
|
end
|
|
|
|
---
|
|
|
|
--
|
|
|
|
local function readfile()
|
|
|
|
local f = io.open(ENROLL_STATUS_FN, "r")
|
|
|
|
if f == nil then
|
|
|
|
return nil, string.format("Could not read file %s", ENROLL_STATUS_FN)
|
|
|
|
end
|
|
|
|
local t = f:read("*all")
|
|
|
|
f:close()
|
2021-02-20 15:36:33 +08:00
|
|
|
local cn_hi = tonumber(t:sub(1, 2), 16)
|
|
|
|
local cn_low = tonumber(t:sub(3, 10), 16)
|
|
|
|
print(('Using EM4100 ID '..ac.green..'%02X%08X'..ac.reset..' from `'..ac.yellow..'%s'..ac.reset..'`'):format(cn_hi, cn_low, ENROLL_STATUS_FN))
|
|
|
|
return cn_hi, cn_low
|
2021-02-20 03:14:11 +08:00
|
|
|
end
|
|
|
|
---
|
|
|
|
--
|
2021-02-20 15:36:33 +08:00
|
|
|
local function writefile(cn_hi, cn_low)
|
2021-02-20 03:14:11 +08:00
|
|
|
local f = io.open(ENROLL_STATUS_FN, "w")
|
|
|
|
if f == nil then
|
|
|
|
return nil, string.format("Could not write to file %s", ENROLL_STATUS_FN)
|
|
|
|
end
|
2021-02-20 15:36:33 +08:00
|
|
|
f:write(("%02X%08X\n"):format(cn_hi, cn_low))
|
2021-02-20 03:14:11 +08:00
|
|
|
f:close()
|
2021-02-20 15:36:33 +08:00
|
|
|
print(('Wrote EM4100 ID '..ac.green..'%02X%08X'..ac.reset..' to `'..ac.yellow..'%s'..ac.reset..'`'):format(cn_hi, cn_low, ENROLL_STATUS_FN))
|
2021-02-20 03:14:11 +08:00
|
|
|
return true, 'Ok'
|
|
|
|
end
|
|
|
|
|
|
|
|
---
|
|
|
|
-- main
|
|
|
|
local function main(args)
|
|
|
|
|
|
|
|
print( string.rep('--',20) )
|
|
|
|
print( string.rep('--',20) )
|
|
|
|
print()
|
|
|
|
|
|
|
|
if #args == 0 then return help() end
|
|
|
|
|
2021-02-23 04:58:09 +08:00
|
|
|
local shall_verify = false
|
2021-02-20 03:14:11 +08:00
|
|
|
local shall_continue = false
|
2021-02-23 04:58:09 +08:00
|
|
|
local got_pwd = false
|
2021-02-20 03:14:11 +08:00
|
|
|
local startid = ''
|
2021-02-23 04:58:09 +08:00
|
|
|
local ipwd = ''
|
|
|
|
|
2021-02-20 03:14:11 +08:00
|
|
|
|
2021-02-23 04:58:09 +08:00
|
|
|
for o, a in getopt.getopt(args, 'cp:s:hv') do
|
2021-02-20 03:14:11 +08:00
|
|
|
if o == 'h' then return help() end
|
2021-02-23 04:58:09 +08:00
|
|
|
if o == 'c' then shall_continue = true end
|
2021-02-20 03:14:11 +08:00
|
|
|
if o == 's' then startid = a end
|
2021-02-23 04:58:09 +08:00
|
|
|
if o == 'p' then
|
|
|
|
ipwd = a
|
|
|
|
got_pwd = true
|
|
|
|
end
|
|
|
|
if o == 'v' then shall_verify = true end
|
2021-02-20 03:14:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- if reset/start over, check -s
|
2021-04-08 16:44:31 +08:00
|
|
|
if not shall_continue then
|
2021-02-20 03:14:11 +08:00
|
|
|
if startid == nil then return oops('empty card number string') end
|
|
|
|
if #startid == 0 then return oops('empty card number string') end
|
2021-02-23 04:58:09 +08:00
|
|
|
if #startid ~= 10 then return oops('card number wrong length. Must be 5 hex bytes') end
|
|
|
|
end
|
|
|
|
|
|
|
|
if got_pwd then
|
|
|
|
if ipwd == nil then return oops('empty password') end
|
|
|
|
if #ipwd == 0 then return oops('empty password') end
|
|
|
|
if #ipwd ~= 8 then return oops('password wrong length. Must be 4 hex bytes') end
|
2021-02-20 03:14:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
core.console('clear')
|
|
|
|
print(ac.red..'disable hints for less output'..ac.reset)
|
|
|
|
core.console('pref set hint --off')
|
|
|
|
print('')
|
2021-04-08 16:44:31 +08:00
|
|
|
|
2021-02-20 15:36:33 +08:00
|
|
|
local hi = tonumber(startid:sub(1, 2), 16)
|
|
|
|
local low = tonumber(startid:sub(3, 10), 16)
|
2021-02-23 04:58:09 +08:00
|
|
|
local pwd = tonumber(ipwd, 16)
|
|
|
|
|
|
|
|
if got_pwd then
|
|
|
|
print(('Will protect T5577 with password '..ac.green..'%08X'..ac.reset):format(pwd))
|
|
|
|
end
|
|
|
|
|
|
|
|
if shall_verify then
|
|
|
|
print('Will verify write afterwards')
|
|
|
|
end
|
|
|
|
|
2021-02-20 03:14:11 +08:00
|
|
|
if shall_continue then
|
|
|
|
print('Continue enrolling from last save')
|
2021-02-20 15:36:33 +08:00
|
|
|
hi, low = readfile()
|
2021-02-20 03:14:11 +08:00
|
|
|
else
|
|
|
|
print('reset & starting enrolling from refresh')
|
|
|
|
end
|
2021-04-08 16:44:31 +08:00
|
|
|
|
2021-02-20 15:36:33 +08:00
|
|
|
local template = 'EM4100 ID '..ac.green..'%02X%08X'..ac.reset
|
|
|
|
for i = low, low + 10000, 1 do
|
2021-02-20 03:14:11 +08:00
|
|
|
print('')
|
|
|
|
print( string.rep('--',20) )
|
2021-02-20 15:36:33 +08:00
|
|
|
local msg = (template):format(hi, i)
|
2021-02-20 03:14:11 +08:00
|
|
|
local ans = utils.input(msg, 'y'):lower()
|
|
|
|
if ans == 'y' then
|
2021-04-08 16:44:31 +08:00
|
|
|
core.console( ('lf em 410x clone --id %02X%08X'):format(hi, i) )
|
2021-02-20 15:36:33 +08:00
|
|
|
-- print ( ('lf em 410x clone --id %02X%08X'):format(hi, i) )
|
2021-02-23 04:58:09 +08:00
|
|
|
|
|
|
|
if got_pwd then
|
|
|
|
core.console('lf t55 detect')
|
|
|
|
core.console(('lf t55 protect -n %08x'):format(pwd))
|
|
|
|
end
|
|
|
|
|
|
|
|
if shall_verify then
|
|
|
|
core.console('lf em 410x reader')
|
|
|
|
end
|
2021-02-20 03:14:11 +08:00
|
|
|
else
|
|
|
|
print(ac.red..'User aborted'..ac.reset)
|
2021-02-20 15:36:33 +08:00
|
|
|
low = i
|
2021-02-20 03:14:11 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-02-20 15:36:33 +08:00
|
|
|
writefile(hi, low)
|
2021-02-20 03:14:11 +08:00
|
|
|
|
|
|
|
print('enabling hints again')
|
|
|
|
core.console('pref set hint --on')
|
|
|
|
end
|
|
|
|
|
|
|
|
main(args)
|