-
Notifications
You must be signed in to change notification settings - Fork 0
/
srix.c
218 lines (179 loc) · 6.35 KB
/
srix.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <string.h>
#include "reader.h"
#include "srix.h"
#include "srixflag.h"
/**
* Generic SRIX4K tag
*/
struct Srix {
union {
struct {
uint32_t otp[5]; /* 0-4 Resettable OTP bits */
uint32_t counter[2]; /* 5-6 Count down counter */
uint32_t lockable[9]; /* 7-15 Lockable EEPROM */
uint32_t generic[112]; /* 16-127 EEPROM */
};
uint32_t eeprom[SRIX4K_BLOCKS]; /* SRIX4K EEPROM */
};
uint64_t uid; /* SRIX UID */
SrixFlag blockFlags; /* Modified block flags */
NfcReader *reader; /* NFC Reader */
SrixError error; /* Error */
};
/**
* Get UID from a SRIX4K.
* @param target pointer to Srix instance where save UID
* @return SrixError result
*/
static SrixError getUid(Srix *target) {
/* Get UID as byte array */
uint8_t uidBytes[SRIX_UID_LENGTH];
SrixError error = NfcGetUid(target->reader, uidBytes);
if (SRIX_IS_ERROR(error)) {
return error;
}
/* Check manufacturer code (datasheet of SRIX4K and ST25TB04K) */
if (uidBytes[7] != 0xD0 || uidBytes[6] != 0x02) {
return SRIX_ERROR(NFC_ERROR, "invalid tag manufacturer code");
}
/* Convert UID to uint64 */
target->uid = (uint64_t) uidBytes[7] << 56U | (uint64_t) uidBytes[6] << 48U | (uint64_t) uidBytes[5] << 40U |
(uint64_t) uidBytes[4] << 32U | (uint64_t) uidBytes[3] << 24U | (uint64_t) uidBytes[2] << 16U |
(uint64_t) uidBytes[1] << 8U | (uint64_t) uidBytes[0];
return SRIX_NO_ERROR;
}
/**
* Read all blocks from a SRIX4K.
* @param target pointer to Srix instance where save EEPROM content
* @return SrixError result
*/
static SrixError readBlocks(Srix *target) {
if (!target->reader) {
return SRIX_ERROR(SRIX_ERROR, "nfc reader hasn't been initialized");
}
for (uint8_t i = 0; i < SRIX4K_BLOCKS; i++) {
uint8_t readBlock[SRIX_BLOCK_LENGTH];
SrixError error = NfcReadBlock(target->reader, (SrixBlock *) readBlock, i);
if (SRIX_IS_ERROR(error)) {
return error;
}
target->eeprom[i] = readBlock[0] << 24 | readBlock[1] << 16 | readBlock[2] << 8 | readBlock[3];
}
return SRIX_NO_ERROR;
}
/**
* Write a selected group of blocks on SRIX4K.
* @param target pointer to Srix instance to take the blocks to write
* @param groupPointer pointer to array to write
* @param groupSize size of array to write
* @return SrixError result
*/
static SrixError srixWriteGroup(Srix *target, uint32_t *groupPointer, uint8_t groupSize) {
for (uint64_t i = 0; i < groupSize; i++) {
if (srixFlagGet(&target->blockFlags, groupPointer + i - target->eeprom)) {
const uint8_t writeBlock[] = {
groupPointer[i] >> 24,
groupPointer[i] >> 16,
groupPointer[i] >> 8,
groupPointer[i]
};
SrixError error = NfcWriteBlock(target->reader, (SrixBlock *) writeBlock,
groupPointer + i - target->eeprom);
if (SRIX_IS_ERROR(error)) {
return error;
}
}
}
return SRIX_NO_ERROR;
}
Srix *SrixNew() {
Srix *created = malloc(sizeof(Srix));
if (!created) {
return (void *) 0;
}
created->reader = NfcReaderNew();
created->error = SRIX_NO_ERROR;
created->error.message = "";
return created;
}
void SrixDelete(Srix target[static 1]) {
NfcCloseReader(target->reader);
free(target->reader);
free(target);
}
size_t NfcGetReadersCount(Srix target[static 1]) {
return NfcUpdateReaders(target->reader);
}
char *NfcGetDescription(Srix *target, int reader) {
return NfcGetReaderDescription(target->reader, reader);
}
const char *SrixGetLatestError(Srix target[static 1]) {
const char *message = target->error.message;
/* Reset error */
target->error = SRIX_NO_ERROR;
target->error.message = "";
return message;
}
const char *SrixNfcInit(Srix target[static 1], int reader) {
target->blockFlags = SRIX_FLAG_INIT;
NfcCloseReader(target->reader);
NfcInitReader(target->reader, reader);
/* Get SRIX4K UID & EEPROM */
SrixError error = getUid(target);
if (SRIX_IS_ERROR(error)) {
SrixDelete(target);
return error.message;
}
error = readBlocks(target);
if (SRIX_IS_ERROR(error)) {
SrixDelete(target);
}
return error.message;
}
void SrixMemoryInit(Srix target[static 1], uint32_t eeprom[const static SRIX4K_BLOCKS], uint64_t uid) {
/* Copy all blocks */
memcpy(target->eeprom, eeprom, SRIX4K_BLOCKS * SRIX_BLOCK_LENGTH);
/* Flag all generic blocks */
for (uint8_t i = 16; i < 128; i++) {
srixFlagAdd(&target->blockFlags, i);
}
target->uid = uid;
}
uint64_t SrixGetUid(Srix target[static 1]) {
return target->uid;
}
uint32_t *SrixGetBlock(Srix target[static 1], uint8_t blockNum) {
return (blockNum < SRIX4K_BLOCKS) ? (target->eeprom + blockNum) : 0;
}
void SrixModifyBlock(Srix target[static 1], const uint32_t block, const uint8_t blockNum) {
target->eeprom[blockNum] = block;
srixFlagAdd(&target->blockFlags, blockNum);
}
int SrixWriteBlocks(Srix target[static 1]) {
if (!target->reader) {
target->error = SRIX_ERROR(SRIX_ERROR, "NFC reader hasn't been initialized");
return target->error.errorType;
}
/* Counter blocks */
target->error = srixWriteGroup(target, target->counter, sizeof(target->counter) / sizeof(uint32_t));
if (SRIX_IS_ERROR(target->error)) {
return target->error.errorType;
}
/* OTP blocks */
target->error = srixWriteGroup(target, target->otp, sizeof(target->otp) / sizeof(uint32_t));
if (SRIX_IS_ERROR(target->error)) {
return target->error.errorType;
}
/* Lockable blocks */
target->error = srixWriteGroup(target, target->lockable, sizeof(target->lockable) / sizeof(uint32_t));
if (SRIX_IS_ERROR(target->error)) {
return target->error.errorType;
}
/* Generic blocks */
target->error = srixWriteGroup(target, target->generic, sizeof(target->generic) / sizeof(uint32_t));
if (SRIX_IS_ERROR(target->error)) {
return target->error.errorType;
}
target->blockFlags = SRIX_FLAG_INIT;
return SRIX_NO_ERROR.errorType;
}