libfreefare/examples/felica-lite-dump.c

87 lines
2 KiB
C
Raw Permalink Normal View History

#include <err.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <freefare.h>
int
2017-06-27 13:58:31 +02:00
main(void)
{
nfc_device *device = NULL;
FreefareTag *tags = NULL;
nfc_connstring devices[8];
nfc_context *context;
2017-06-27 13:58:31 +02:00
nfc_init(&context);
if (context == NULL)
2017-06-27 13:58:31 +02:00
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
2017-06-27 13:58:31 +02:00
size_t device_count = nfc_list_devices(context, devices, 8);
if (device_count <= 0)
2017-06-27 13:58:31 +02:00
errx(EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) {
2017-06-27 13:58:31 +02:00
device = nfc_open(context, devices[d]);
if (!device) {
2017-06-27 13:58:31 +02:00
errx(EXIT_FAILURE, "nfc_open() failed.");
}
2017-06-27 13:58:31 +02:00
tags = freefare_get_tags(device);
if (!tags) {
2017-06-27 13:58:31 +02:00
nfc_close(device);
errx(EXIT_FAILURE, "Error listing FeliCa tag.");
}
for (int i = 0; tags[i]; i++) {
2017-06-27 13:58:31 +02:00
if (FELICA != freefare_get_tag_type(tags[i]))
continue;
2017-06-27 13:58:31 +02:00
char *uid = freefare_get_tag_uid(tags[i]);
printf("Dumping %s tag %s\n", freefare_get_tag_friendly_name(tags[i]), uid);
free(uid);
printf("Number\tName\tData\n");
for (int block = 0x00; block < 0x0f; block++) {
uint8_t buffer[16];
2017-06-27 13:58:31 +02:00
if (felica_read(tags[i], FELICA_SC_RO, block, buffer, sizeof(buffer)) < 0)
errx(EXIT_FAILURE, "Error reading block %d", block);
if (block < 0x0e)
2017-06-27 13:58:31 +02:00
printf("0x%02x\tS_PAD%d\t", block, block);
else
2017-06-27 13:58:31 +02:00
printf("0x%02x\tREG\t", block);
for (int j = 0; j < 16; j++) {
2017-06-27 13:58:31 +02:00
printf("%02x ", buffer[j]);
}
2017-06-27 13:58:31 +02:00
printf("\n");
}
char *block_names[] = {
"RC", "MAC", "ID", "D_ID", "SER_C", "SYS_C", "CKV", "CK", "MC",
};
int valid_bytes[] = {
16, 8, 16, 16, 2, 2, 2, 16, 5
};
for (int block = 0x80; block < 0x89; block++) {
uint8_t buffer[16];
2017-06-27 13:58:31 +02:00
if (felica_read(tags[i], FELICA_SC_RO, block, buffer, sizeof(buffer)) < 0)
errx(EXIT_FAILURE, "Error reading block %d", block);
2017-06-27 13:58:31 +02:00
printf("0x%02x\t%s\t", block, block_names[block - 0x80]);
for (int j = 0; j < valid_bytes[block - 0x80]; j++) {
2017-06-27 13:58:31 +02:00
printf("%02x ", buffer[j]);
}
2017-06-27 13:58:31 +02:00
printf("\n");
}
}
2017-06-27 13:58:31 +02:00
freefare_free_tags(tags);
nfc_close(device);
}
exit(EXIT_SUCCESS);
}