65 static jewel_tag ttDump;
66 static uint32_t uiBlocks = 0x0E;
67 static uint32_t uiBytesPerBlock = 0x08;
75 print_success_or_failure(
bool bFailure, uint32_t *uiCounter)
77 printf(
"%c", (bFailure) ?
'x' :
'.');
79 *uiCounter += (bFailure) ? 0 : 1;
87 bool bFailure =
false;
88 uint32_t uiReadBlocks = 0;
90 printf(
"Reading %d blocks |", uiBlocks + 1);
92 for (block = 0; block <= uiBlocks; block++) {
93 for (
byte = 0;
byte < uiBytesPerBlock;
byte++) {
96 req.read.btCmd = TC_READ;
97 req.read.btAdd = (block << 3) +
byte;
99 ttDump.ttd.abtData[(block << 3) +
byte] = res.read.btDat;
106 print_success_or_failure(bFailure, &uiReadBlocks);
110 printf(
"Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks + 1);
120 bool bFailure =
false;
121 uint32_t uiWrittenBlocks = 0;
122 uint32_t uiSkippedBlocks = 0;
123 uint32_t uiPartialBlocks = 0;
129 printf(
"Write Lock bytes ? [yN] ");
130 if (!fgets(buffer, BUFSIZ, stdin)) {
131 ERR(
"Unable to read standard input.");
133 write_lock = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
135 printf(
"Write OTP bytes ? [yN] ");
136 if (!fgets(buffer, BUFSIZ, stdin)) {
137 ERR(
"Unable to read standard input.");
139 write_otp = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
141 printf(
"Writing %d pages |", uiBlocks + 1);
147 for (block = uiSkippedBlocks; block <= uiBlocks; block++) {
155 if ((block == 0x0E) && (!write_lock) && (!write_otp)) {
161 if ((block == 0x0E) && (!write_lock || !write_otp)) {
166 for (
byte = 0;
byte < uiBytesPerBlock;
byte++) {
167 if ((block == 0x0E) && (
byte == 0 ||
byte == 1) && (!write_lock)) {
170 if ((block == 0x0E) && (
byte > 1) && (!write_otp)) {
178 ERR(
"tag was removed");
184 req.writee.btCmd = TC_WRITEE;
185 req.writee.btAdd = (block << 3) +
byte;
186 req.writee.btDat = ttDump.ttd.abtData[(block << 3) +
byte];
191 print_success_or_failure(bFailure, &uiWrittenBlocks);
195 printf(
"Done, %d of %d blocks written (%d blocks partial, %d blocks skipped).\n", uiWrittenBlocks, uiBlocks + 1, uiPartialBlocks, uiSkippedBlocks);
201 main(
int argc,
const char *argv[])
208 printf(
"%s r|w <dump.jwd>\n", argv[0]);
210 printf(
"r|w - Perform read from or write to card\n");
211 printf(
"<dump.jwd> - JeWel Dump (JWD) used to write (card to JWD) or (JWD to card)\n");
216 DBG(
"\nChecking arguments and settings\n");
218 bReadAction = tolower((
int)((
unsigned char) * (argv[1])) ==
'r');
221 memset(&ttDump, 0x00,
sizeof(ttDump));
223 pfDump = fopen(argv[2],
"rb");
225 if (pfDump == NULL) {
226 ERR(
"Could not open dump file: %s\n", argv[2]);
230 if (fread(&ttDump, 1,
sizeof(ttDump), pfDump) !=
sizeof(ttDump)) {
231 ERR(
"Could not read from dump file: %s\n", argv[2]);
237 DBG(
"Successfully opened the dump file\n");
241 if (context == NULL) {
242 ERR(
"Unable to init libnfc (malloc)");
249 ERR(
"Error opening NFC device");
263 nfc_perror(pnd,
"nfc_device_set_property_bool");
273 ERR(
"no tag was found\n");
280 printf(
"Found Jewel card with UID: ");
282 for (szPos = 0; szPos < 4; szPos++) {
283 printf(
"%02x", nt.nti.nji.btId[szPos]);
289 printf(
"Writing data to file: %s ... ", argv[2]);
291 pfDump = fopen(argv[2],
"wb");
292 if (pfDump == NULL) {
293 printf(
"Could not open file: %s\n", argv[2]);
298 if (fwrite(&ttDump, 1,
sizeof(ttDump), pfDump) !=
sizeof(ttDump)) {
299 printf(
"Could not write to file: %s\n", argv[2]);
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
void nfc_close(nfc_device *pnd)
Close from a NFC device.
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
int nfc_initiator_select_passive_target(nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt)
Select a passive or emulated tag.
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device's boolean-property value.
bool nfc_initiator_jewel_cmd(nfc_device *pnd, const jewel_req req, jewel_res *pres)
Execute a Jewel Topaz Command.
provide samples structs and functions to manipulate Jewel Topaz tags using libnfc
Provide some examples shared functions like print, parity calculation, options parsing.
#define ERR(...)
Print a error message.
#define DBG(...)
Print a message of standard output only in DEBUG mode.
NFC library context Struct which contains internal options, references, pointers, etc....
NFC modulation structure.