Skip to content

Instantly share code, notes, and snippets.

@socram8888
Created September 8, 2020 20:35
Show Gist options
  • Save socram8888/b3ad24d890342e0c2bfb0719f8636ff4 to your computer and use it in GitHub Desktop.
Save socram8888/b3ad24d890342e0c2bfb0719f8636ff4 to your computer and use it in GitHub Desktop.
// FM11RF005SH dump tool for libnfc
// Made by Marcos Del Sol Vives <marcos@orca.pet>
// License: WTFPL 2.0
#include <stdio.h>
#include <stdint.h>
#include <nfc/nfc.h>
int main(int argc, char ** argv) {
int ret = 1;
int rxSize;
if (argc != 1) {
fprintf(stderr, "This command expects no parameters\n");
return 1;
}
// Initialize context
nfc_context * ctx;
nfc_init(&ctx);
if (ctx == NULL) {
fprintf(stderr, "Error initializing libnfc\n");
return 1;
}
// Open default reader
nfc_device * dev = nfc_open(ctx, NULL);
if (dev == NULL) {
fprintf(stderr, "Error opening NFC reader\n");
goto fail_exit;
}
// Initialise NFC device as "initiator"
if (nfc_initiator_init(dev) < 0) {
nfc_perror(dev, "nfc_initiator_init");
goto fail_close;
}
// Configure the CRC
if (nfc_device_set_property_bool(dev, NP_HANDLE_CRC, false) < 0) {
nfc_perror(dev, "nfc_device_set_property_bool");
goto fail_close;
}
// Use raw send/receive methods
if (nfc_device_set_property_bool(dev, NP_EASY_FRAMING, false) < 0) {
nfc_perror(dev, "nfc_device_set_property_bool");
goto fail_close;
}
// Disable 14443-4 autoswitching
if (nfc_device_set_property_bool(dev, NP_AUTO_ISO14443_4, false) < 0) {
nfc_perror(dev, "nfc_device_set_property_bool");
goto fail_close;
}
fprintf(stderr, "NFC reader %s opened\n", nfc_device_get_name(dev));
// Send the REQA and expect ATQA
uint8_t reqa[1] = { 0x26 };
uint8_t atqa[2];
rxSize = nfc_initiator_transceive_bits(dev, reqa, 7, NULL, atqa, sizeof(atqa), NULL);
if (rxSize < 0) {
nfc_perror(dev, "nfc_initiator_transceive_bits");
goto fail_close;
}
if (rxSize != 16) {
fprintf(stderr, "Expected 16-bit ATQA, read %d bits\n", rxSize);
goto fail_close;
}
fprintf(stderr, "ATQA: %02x %02x\n", atqa[0], atqa[1]);
if (atqa[0] != 0x05 || atqa[1] != 0x00) {
fprintf(stderr, "Unexpected ATQA, aborting\n");
goto fail_close;
}
// Re-enable CRC
if (nfc_device_set_property_bool(dev, NP_HANDLE_CRC, true) < 0) {
nfc_perror(dev, "nfc_device_set_property_bool");
goto fail_close;
}
for (int sectorNum = 0; sectorNum < 8; sectorNum++) {
uint8_t readReq[2] = { 0x30, sectorNum };
uint8_t readValues[4];
rxSize = nfc_initiator_transceive_bytes(dev, readReq, sizeof(readReq), readValues, sizeof(readValues), 0);
if (rxSize < 0) {
nfc_perror(dev, "nfc_initiator_transceive_bytes");
goto fail_close;
}
printf("Sector %d: ", sectorNum);
if (rxSize != 4) {
printf("read error\n");
} else {
printf("%02x %02x %02x %02x\n", readValues[0], readValues[1], readValues[2], readValues[3]);
}
}
fail_close:
nfc_close(dev);
fail_exit:
nfc_exit(ctx);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment