-
Notifications
You must be signed in to change notification settings - Fork 32
/
nfchandler.cpp
117 lines (93 loc) · 2.69 KB
/
nfchandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "nfchandler.h"
#include "amiitool.h"
#include "amiibo.h"
#include <stdio.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#define PAGE_COUNT 135
#define WRITE_COMMAND 0XA2
const uint8_t dynamicLockBytes[4] = { 0x01, 0x00, 0x0f, 0xbd };
const uint8_t staticLockBytes[4] = { 0x00, 0x00, 0x0F, 0xE0 };
const nfc_modulation nmMifare = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
NFCHandler::NFCHandler() {
printf("Initializing NFC adapter\n");
nfc_init(&context);
if (!context) {
printf("Unable to init libnfc (malloc)\n");
exit(1);
}
device = nfc_open(context, NULL);
if (device == NULL) {
printf("ERROR: %s\n", "Unable to open NFC device.");
exit(1);
}
if (nfc_initiator_init(device) < 0) {
nfc_perror(device, "nfc_initiator_init");
exit(1);
}
printf("NFC reader: opened\n");
}
void NFCHandler::readTagUUID(uint8_t uuidBuffer[]) {
printf("***Scan tag***\n");
if (nfc_initiator_select_passive_target(device, nmMifare, NULL, 0, &target) > 0) {
printf("Read UID: ");
int uidSize = target.nti.nai.szUidLen;
Amiitool::shared()->printHex(target.nti.nai.abtUid, uidSize);
if (UUID_SIZE != uidSize) {
fprintf(stderr, "Read wrong size UID\n");
exit(1);
}
for (int i = 0; i < UUID_SIZE; i++) {
uuidBuffer[i] = target.nti.nai.abtUid[i];
}
}
}
void NFCHandler::writeAmiibo(Amiibo *amiibo) {
uint8_t uuid[UUID_SIZE];
readTagUUID(uuid);
amiibo->setUUID(uuid);
writeBuffer(amiibo->encryptedBuffer);
}
void NFCHandler::writeBuffer(const uint8_t *buffer) {
printf("Writing tag:\n");
writeDataPages(buffer);
writeDynamicLockBytes();
writeStaticLockBytes();
printf("Finished writing tag\n");
}
void NFCHandler::writeDataPages(const uint8_t *buffer) {
printf("Writing encrypted bin:\n");
for (uint8_t i = 3; i < PAGE_COUNT; i++) {
writePage(i, buffer + (i * 4));
}
printf("Done\n");
}
void NFCHandler::writeDynamicLockBytes() {
printf("Writing dynamic lock bytes\n");
writePage(130, dynamicLockBytes);
printf("Done\n");
}
void NFCHandler::writeStaticLockBytes() {
printf("Writing static lock bytes\n");
writePage(2, staticLockBytes);
printf("Done\n");
}
void NFCHandler::writePage(uint8_t page, const uint8_t *buffer) {
printf("Writing to %d: %02x %02x %02x %02x...",
page, buffer[0], buffer[1], buffer[2], buffer[3]);
uint8_t sendData[6] = {
WRITE_COMMAND, page, buffer[0], buffer[1], buffer[2], buffer[3]
};
int responseCode = nfc_initiator_transceive_bytes(device, sendData, 6, NULL, 0, 0);
if (responseCode == 0) {
printf("Done\n");
} else {
printf("Failed\n");
fprintf(stderr, "Failed to write to tag\n");
nfc_perror(device, "Write");
exit(1);
}
}