make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -25,23 +25,26 @@ static struct {
uint8_t HR01[2];
uint8_t uid[7];
uint16_t size;
uint8_t data_blocks[TOPAZ_STATIC_MEMORY/8][8]; // this memory is always there
uint8_t data_blocks[TOPAZ_STATIC_MEMORY / 8][8]; // this memory is always there
uint8_t *dynamic_memory; // this memory can be there
dynamic_lock_area_t *dynamic_lock_areas; // lock area descriptors
} topaz_tag;
static void topaz_switch_on_field(void) {
static void topaz_switch_on_field(void)
{
UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_SELECT | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE | ISO14A_NO_RATS, 0, 0}};
SendCommand(&c);
}
static void topaz_switch_off_field(void) {
static void topaz_switch_off_field(void)
{
UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
SendCommand(&c);
}
// send a raw topaz command, returns the length of the response (0 in case of error)
static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response) {
static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response)
{
UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE | ISO14A_NO_RATS, len, 0}};
memcpy(c.d.asBytes, cmd, len);
SendCommand(&c);
@ -58,12 +61,13 @@ static int topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response) {
// calculate CRC bytes and send topaz command, returns the length of the response (0 in case of error)
static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response) {
static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response)
{
if (len > 1) {
uint8_t b1, b2;
compute_crc(CRC_14443_B, cmd, len-2, &b1, &b2);
cmd[len-2] = b1;
cmd[len-1] = b2;
compute_crc(CRC_14443_B, cmd, len - 2, &b1, &b2);
cmd[len - 2] = b1;
cmd[len - 1] = b2;
}
return topaz_send_cmd_raw(cmd, len, response);
@ -71,7 +75,8 @@ static int topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response) {
// select a topaz tag. Send WUPA and RID.
static int topaz_select(uint8_t *atqa, uint8_t *rid_response) {
static int topaz_select(uint8_t *atqa, uint8_t *rid_response)
{
// ToDo: implement anticollision
uint8_t wupa_cmd[] = {TOPAZ_WUPA};
@ -94,7 +99,8 @@ static int topaz_select(uint8_t *atqa, uint8_t *rid_response) {
// read all of the static memory of a selected Topaz tag.
static int topaz_rall(uint8_t *uid, uint8_t *response) {
static int topaz_rall(uint8_t *uid, uint8_t *response)
{
uint8_t rall_cmd[] = {TOPAZ_RALL, 0, 0, 0, 0, 0, 0, 0, 0};
memcpy(&rall_cmd[3], uid, 4);
@ -124,7 +130,8 @@ static int topaz_read_block(uint8_t *uid, uint8_t blockno, uint8_t *block_data)
}
// read a segment (16 blocks = 128 Bytes) of a selected Topaz tag. Works only for tags with dynamic memory.
static int topaz_read_segment(uint8_t *uid, uint8_t segno, uint8_t *segment_data) {
static int topaz_read_segment(uint8_t *uid, uint8_t segno, uint8_t *segment_data)
{
uint8_t rseg_cmd[] = {TOPAZ_RSEG, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t rseg_response[131];
@ -139,7 +146,8 @@ static int topaz_read_segment(uint8_t *uid, uint8_t segno, uint8_t *segment_data
}
// search for the lock area descriptor for the lockable area including byteno
static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno) {
static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno)
{
dynamic_lock_area_t *lock_area;
lock_area = topaz_tag.dynamic_lock_areas;
@ -154,7 +162,8 @@ static dynamic_lock_area_t *get_dynamic_lock_area(uint16_t byteno) {
}
// check if a memory byte is locked.
static bool topaz_byte_is_locked(uint16_t byteno) {
static bool topaz_byte_is_locked(uint16_t byteno)
{
uint8_t *lockbits;
uint16_t locked_bytes_per_bit;
dynamic_lock_area_t *lock_area;
@ -174,7 +183,7 @@ static bool topaz_byte_is_locked(uint16_t byteno) {
}
uint16_t blockno = byteno / locked_bytes_per_bit;
if(lockbits[blockno/8] & (0x01 << (blockno % 8))) {
if (lockbits[blockno / 8] & (0x01 << (blockno % 8))) {
return true;
} else {
return false;
@ -183,7 +192,8 @@ static bool topaz_byte_is_locked(uint16_t byteno) {
// read and print the Capability Container
static int topaz_print_CC(uint8_t *data) {
static int topaz_print_CC(uint8_t *data)
{
if (data[0] != 0xe1) {
topaz_tag.size = TOPAZ_STATIC_MEMORY;
return -1; // no NDEF message
@ -197,14 +207,15 @@ static int topaz_print_CC(uint8_t *data) {
topaz_tag.dynamic_memory = calloc(memsize - TOPAZ_STATIC_MEMORY, sizeof(uint8_t));
PrintAndLogEx(NORMAL, " %02x: Physical Memory Size of this tag: %d bytes", data[2], memsize);
PrintAndLogEx(NORMAL, " %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)");
(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) {
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;
@ -238,7 +249,8 @@ static void get_TLV(uint8_t **TLV_ptr, uint8_t *TLV_type, uint16_t *TLV_length,
// 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) {
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) {
@ -250,7 +262,8 @@ static void adjust_lock_areas(uint16_t block_start, uint16_t block_size) {
// read and print the lock area and reserved memory TLVs
static void topaz_print_control_TLVs(uint8_t *memory) {
static void topaz_print_control_TLVs(uint8_t *memory)
{
uint8_t *TLV_ptr = memory;
uint8_t TLV_type = 0;
uint16_t TLV_length;
@ -266,21 +279,21 @@ static void topaz_print_control_TLVs(uint8_t *memory) {
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 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;
PrintAndLogEx(NORMAL, "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);
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 *) calloc(sizeof(dynamic_lock_area_t) , sizeof(uint8_t));
new = topaz_tag.dynamic_lock_areas = (dynamic_lock_area_t *) calloc(sizeof(dynamic_lock_area_t), sizeof(uint8_t));
} else {
while(old->next != NULL) {
while (old->next != NULL) {
old = old->next;
}
new = old->next = (dynamic_lock_area_t *) calloc(sizeof(dynamic_lock_area_t), sizeof(uint8_t));
@ -303,8 +316,8 @@ static void topaz_print_control_TLVs(uint8_t *memory) {
uint8_t bytes_per_page = 1 << (TLV_value[2] & 0x0f);
uint16_t area_start = pages_addr * bytes_per_page + byte_offset;
PrintAndLogEx(NORMAL, "Reserved Memory of %d bytes at byte offset 0x%02x.",
size_in_bytes,
area_start);
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) {
@ -323,7 +336,8 @@ static void topaz_print_control_TLVs(uint8_t *memory) {
}
// read all of the dynamic memory
static int topaz_read_dynamic_data(void){
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) {
PrintAndLogEx(WARNING, "Error while reading dynamic memory block %02x. Aborting...", 0x0f);
@ -333,7 +347,7 @@ static int topaz_read_dynamic_data(void){
// 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) {
if (topaz_read_segment(topaz_tag.uid, segment, &topaz_tag.dynamic_memory[(segment - 1) * 128 + 8]) == -1) {
PrintAndLogEx(WARNING, "Error while reading dynamic memory block %02x. Aborting...", 0x0f);
return -1;
}
@ -343,36 +357,40 @@ static int topaz_read_dynamic_data(void){
// read and print the dynamic memory
static void topaz_print_dynamic_data(void) {
static void topaz_print_dynamic_data(void)
{
if (topaz_tag.size > TOPAZ_STATIC_MEMORY) {
PrintAndLogEx(NORMAL, "Dynamic Data blocks:");
if (topaz_read_dynamic_data() == 0) {
PrintAndLogEx(NORMAL, "block# | offset | Data | Locked(y/n)");
char line[80];
for (uint16_t blockno = 0x0f; blockno < topaz_tag.size/8; blockno++) {
uint8_t *block_data = &topaz_tag.dynamic_memory[(blockno-0x0f)*8];
for (uint16_t blockno = 0x0f; blockno < topaz_tag.size / 8; blockno++) {
uint8_t *block_data = &topaz_tag.dynamic_memory[(blockno - 0x0f) * 8];
char lockbits[9];
for (uint16_t j = 0; j < 8; j++) {
sprintf(&line[3*j], "%02x ", block_data[j]);
lockbits[j] = topaz_byte_is_locked(blockno*8+j) ? 'y' : 'n';
sprintf(&line[3 * j], "%02x ", block_data[j]);
lockbits[j] = topaz_byte_is_locked(blockno * 8 + j) ? 'y' : 'n';
}
lockbits[8] = '\0';
PrintAndLogEx(NORMAL, " 0x%02x | 0x%04x | %s| %-3s", blockno, blockno*8, line, lockbits);
PrintAndLogEx(NORMAL, " 0x%02x | 0x%04x | %s| %-3s", blockno, blockno * 8, line, lockbits);
}
}
}
}
static void topaz_print_lifecycle_state(uint8_t *data) {
static void topaz_print_lifecycle_state(uint8_t *data)
{
// to be done
}
static void topaz_print_NDEF(uint8_t *data) {
static void topaz_print_NDEF(uint8_t *data)
{
// to be done.
}
// read a Topaz tag and print some useful information
int CmdHFTopazReader(const char *Cmd) {
int CmdHFTopazReader(const char *Cmd)
{
int status;
uint8_t atqa[2];
uint8_t rid_response[8];
@ -381,7 +399,7 @@ int CmdHFTopazReader(const char *Cmd) {
bool verbose = true;
char ctmp = param_getchar(Cmd, 0);
if ( ctmp == 'S' || ctmp == 's') verbose = false;
if (ctmp == 'S' || ctmp == 's') verbose = false;
status = topaz_select(atqa, rid_response);
@ -408,9 +426,9 @@ int CmdHFTopazReader(const char *Cmd) {
// ToDo: CRC check
PrintAndLogEx(NORMAL, "HR0 : %02x (%sa Topaz tag (%scapable of carrying a NDEF message), %s memory map)", rid_response[0],
(rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
(rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
(rid_response[0] & 0x0F) == 0x01 ? "static" : "dynamic");
(rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
(rid_response[0] & 0xF0) == 0x10 ? "" : "not ",
(rid_response[0] & 0x0F) == 0x01 ? "static" : "dynamic");
PrintAndLogEx(NORMAL, "HR1 : %02x", rid_response[1]);
status = topaz_rall(uid_echo, rall_response);
@ -421,21 +439,21 @@ int CmdHFTopazReader(const char *Cmd) {
return -1;
}
memcpy(topaz_tag.uid, rall_response+2, 7);
memcpy(topaz_tag.uid, rall_response + 2, 7);
PrintAndLogEx(NORMAL, "UID : %02x %02x %02x %02x %02x %02x %02x",
topaz_tag.uid[6],
topaz_tag.uid[5],
topaz_tag.uid[4],
topaz_tag.uid[3],
topaz_tag.uid[2],
topaz_tag.uid[1],
topaz_tag.uid[0]);
topaz_tag.uid[6],
topaz_tag.uid[5],
topaz_tag.uid[4],
topaz_tag.uid[3],
topaz_tag.uid[2],
topaz_tag.uid[1],
topaz_tag.uid[0]);
PrintAndLogEx(NORMAL, " UID[6] (Manufacturer Byte) = %02x, Manufacturer: %s",
topaz_tag.uid[6],
getTagInfo(topaz_tag.uid[6]));
topaz_tag.uid[6],
getTagInfo(topaz_tag.uid[6]));
memcpy(topaz_tag.data_blocks, rall_response+2, 0x0f*8);
memcpy(topaz_tag.data_blocks, rall_response + 2, 0x0f * 8);
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Static Data blocks 00 to 0c:");
PrintAndLogEx(NORMAL, "block# | offset | Data | Locked");
@ -443,26 +461,26 @@ int CmdHFTopazReader(const char *Cmd) {
for (uint16_t i = 0; i <= 0x0c; i++) {
char lockbits[9];
for (uint16_t j = 0; j < 8; j++) {
sprintf(&line[3*j], "%02x ", topaz_tag.data_blocks[i][j] /*rall_response[2 + 8*i + j]*/);
lockbits[j] = topaz_byte_is_locked(i*8+j) ? 'y' : 'n';
sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[i][j] /*rall_response[2 + 8*i + j]*/);
lockbits[j] = topaz_byte_is_locked(i * 8 + j) ? 'y' : 'n';
}
lockbits[8] = '\0';
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", i, i*8, line, lockbits);
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", i, i * 8, line, lockbits);
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Static Reserved block 0d:");
for (uint16_t j = 0; j < 8; j++) {
sprintf(&line[3*j], "%02x ", topaz_tag.data_blocks[0x0d][j]);
sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[0x0d][j]);
}
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0d, 0x0d*8, line, "n/a");
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0d, 0x0d * 8, line, "n/a");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Static Lockbits and OTP Bytes:");
for (uint16_t j = 0; j < 8; j++) {
sprintf(&line[3*j], "%02x ", topaz_tag.data_blocks[0x0e][j]);
sprintf(&line[3 * j], "%02x ", topaz_tag.data_blocks[0x0e][j]);
}
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0e, 0x0e*8, line, "n/a");
PrintAndLogEx(NORMAL, " 0x%02x | 0x%02x | %s| %-3s", 0x0e, 0x0e * 8, line, "n/a");
PrintAndLogEx(NORMAL, "");
@ -488,17 +506,20 @@ int CmdHFTopazReader(const char *Cmd) {
return 0;
}
int CmdHFTopazSim(const char *Cmd) {
int CmdHFTopazSim(const char *Cmd)
{
PrintAndLogEx(NORMAL, "not yet implemented");
return 0;
}
int CmdHFTopazCmdRaw(const char *Cmd) {
int CmdHFTopazCmdRaw(const char *Cmd)
{
PrintAndLogEx(NORMAL, "not yet implemented. Use hf 14 raw with option -T.");
return 0;
}
int CmdHFTopazList(const char *Cmd) {
int CmdHFTopazList(const char *Cmd)
{
CmdTraceList("topaz");
return 0;
}
@ -515,13 +536,15 @@ static command_t CommandTable[] = {
{NULL, NULL, 0, NULL}
};
int CmdHFTopaz(const char *Cmd) {
int CmdHFTopaz(const char *Cmd)
{
clearCommandBuffer();
CmdsParse(CommandTable, Cmd);
return 0;
}
static int CmdHelp(const char *Cmd) {
static int CmdHelp(const char *Cmd)
{
CmdsHelp(CommandTable);
return 0;
}