1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 // LTO Cartridge memory
18 //-----------------------------------------------------------------------------
24 #include "cliparser.h"
25 #include "cmdparser.h" // command_t
31 #include "protocols.h"
32 #include "fileutils.h" // saveFile
33 #include "commonutil.h" // ARRAYLEN
36 We can't dump LTO 5 or 6 tags yet since we don't have a datasheet.
37 If you have access to datasheet, le me know!
39 CM size xx field indicate size in units of 1024b
41 LTO w Type info 00 01 has 101 blocks.
42 LTO w Type info 00 02 has 95 blocks.
43 LTO w Type info 00 03 has 255 blocks.
44 LTO w Type info 00 xx has NN blocks.
46 #define CM_MEM_MAX_SIZE 0x1FE0 // (32byte/block * 255block = 8160byte)
48 // todo: vendor mapping table..
50 // structure and database for uid -> tagtype lookups
51 typedef struct cm_page_s
{
58 static const cm_page_t cm_page_map
[] = {
59 { 0x001, 64, "Cartridge Manufacture's information", "" },
60 { 0x002, 64, "Media Manufacture's information", "" },
61 { 0x101, 64, "Initialisation Data", "" },
62 { 0x102, 48, "Tape Write Data", "" },
63 { 0x103, 1552, "Tape Directory", "" },
64 { 0x104, 64, "EOD Information", "" },
65 { 0x105, 32, "Cartidge Status and Tape Alert Flags", "" },
66 { 0x106, 384, "Mechanism Related", "" },
67 { 0x107, 128, "Suspended Append Writes", "" },
68 { 0x108, 64, "Usage Information 0", "" },
69 { 0x109, 64, "Usage Information 1", "" },
70 { 0x10A, 64, "Usage Information 2", "" },
71 { 0x10B, 64, "Usage Information 3", "" },
72 { 0x200, 1056, "Application Specific", "" },
73 { 0xFFC, 0, "Pad", "Used to reserve space for future Pages, and to align some Pages to 16-byte / 32-byte boundaries" },
74 { 0xFFD, 0, "Defect", "Used to indicate that the LTO CM contains defective memory locations in that area" },
75 { 0xFFE, 0, "Empty", "Indicates an empty table" },
76 { 0xFFF, 0, "EOPT", "End Of Page Table" },
77 { 0x000, 0, "no page info available", "" } // must be the last entry
81 static uint16_t get_page_len(uint16_t pageid) {
82 for (uint8_t i = 0; i < ARRAYLEN(cm_page_map ); ++i) {
83 if (pageid == cm_page_map[i].pageid) {
84 return cm_page_map[i].len;
87 //No match, return default
92 static const char *get_page_name(uint16_t pageid
) {
93 for (uint8_t i
= 0; i
< ARRAYLEN(cm_page_map
); ++i
) {
94 if (pageid
== cm_page_map
[i
].pageid
) {
95 return cm_page_map
[i
].name
;
98 //No match, return default
99 return cm_page_map
[ARRAYLEN(cm_page_map
) - 1].name
;
102 static int CmdHelp(const char *Cmd
);
104 static void lto_switch_off_field(void) {
105 SetISODEPState(ISODEP_INACTIVE
);
106 SendCommandMIX(CMD_HF_ISO14443A_READER
, 0, 0, 0, NULL
, 0);
109 static void lto_switch_on_field(void) {
110 SendCommandMIX(CMD_HF_ISO14443A_READER
, ISO14A_CONNECT
| ISO14A_NO_SELECT
| ISO14A_NO_DISCONNECT
| ISO14A_NO_RATS
, 0, 0, NULL
, 0);
113 // send a raw LTO-CM command, returns the length of the response (0 in case of error)
114 static int lto_send_cmd_raw(uint8_t *cmd
, uint8_t len
, uint8_t *response
, uint16_t *response_len
, bool addcrc
, bool is7bits
, bool verbose
) {
116 uint64_t arg0
= ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_NO_RATS
;
120 arg0
|= ISO14A_APPEND_CRC
;
131 SendCommandMIX(CMD_HF_ISO14443A_READER
, arg0
, arg1
, 0, cmd
, len
);
132 PacketResponseNG resp
;
134 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
135 if (verbose
) PrintAndLogEx(WARNING
, "timeout while waiting for reply.");
139 if (resp
.oldarg
[0] == *response_len
) {
140 *response_len
= resp
.oldarg
[0];
141 if (*response_len
> 0) {
142 memcpy(response
, resp
.data
.asBytes
, *response_len
);
145 if (verbose
) PrintAndLogEx(WARNING
, "Wrong response length (%d != %" PRIu64
")", *response_len
, resp
.oldarg
[0]);
152 // select a LTO-CM tag. Send WUPA and RID.
153 static int lto_select(uint8_t *id_response
, uint8_t id_len
, uint8_t *type_response
, bool verbose
) {
154 // Todo: implement anticollision
156 uint8_t resp
[] = {0, 0};
158 uint8_t wupa_cmd
[] = {LTO_REQ_STANDARD
};
159 uint8_t select_sn_cmd
[] = {LTO_SELECT
, 0x20};
160 uint8_t select_cmd
[] = {LTO_SELECT
, 0x70, 0, 0, 0, 0, 0};
163 int status
= lto_send_cmd_raw(wupa_cmd
, sizeof(wupa_cmd
), type_response
, &resp_len
, false, true, verbose
);
164 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
) {
165 return PM3_ESOFT
; // WUPA failed
169 status
= lto_send_cmd_raw(select_sn_cmd
, sizeof(select_sn_cmd
), id_response
, &resp_len
, false, false, verbose
);
170 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
) {
171 return PM3_EWRONGANSWER
; // REQUEST SERIAL NUMBER failed
174 memcpy(select_cmd
+ 2, id_response
, sizeof(select_cmd
) - 2);
176 status
= lto_send_cmd_raw(select_cmd
, sizeof(select_cmd
), resp
, &resp_len
, true, false, verbose
);
177 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
|| resp
[0] != 0x0A) {
178 return PM3_EWRONGANSWER
; // SELECT failed
181 // tag is now INIT and SELECTED.
185 static int lto_rdbl(uint8_t blk
, uint8_t *block_response
, uint8_t *block_cnt_response
, bool verbose
) {
187 uint16_t resp_len
= 18;
188 uint8_t rdbl_cmd
[] = {0x30, blk
};
189 uint8_t rdbl_cnt_cmd
[] = {0x80};
191 int status
= lto_send_cmd_raw(rdbl_cmd
, sizeof(rdbl_cmd
), block_response
, &resp_len
, true, false, verbose
);
192 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
) {
193 return PM3_EWRONGANSWER
; // READ BLOCK failed
196 status
= lto_send_cmd_raw(rdbl_cnt_cmd
, sizeof(rdbl_cnt_cmd
), block_cnt_response
, &resp_len
, false, false, verbose
);
197 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
) {
198 return PM3_EWRONGANSWER
; // READ BLOCK CONTINUE failed
205 static int lto_rdbl_ext(uint16_t blk, uint8_t *block_response, uint8_t *block_cnt_response, bool verbose) {
211 uint16_t resp_len = 18;
212 uint8_t rdbl_ext_cmd[] = {0x21 , blk & 0xFF, (blk >> 8) & 0xFF};
213 uint8_t rdbl_cnt_cmd[] = {0x80};
215 int status = lto_send_cmd_raw(rdbl_ext_cmd, sizeof(rdbl_ext_cmd), block_response, &resp_len, true, false, verbose);
216 if (status == PM3_ETIMEOUT || status == PM3_ESOFT) {
217 return PM3_EWRONGANSWER; // READ BLOCK failed
220 status = lto_send_cmd_raw(rdbl_cnt_cmd, sizeof(rdbl_cnt_cmd), block_cnt_response, &resp_len, false, false, verbose);
221 if (status == PM3_ETIMEOUT || status == PM3_ESOFT) {
222 return PM3_EWRONGANSWER; // READ BLOCK CONTINUE failed
229 static int CmdHfLTOInfo(const char *Cmd
) {
230 CLIParserContext
*ctx
;
231 CLIParserInit(&ctx
, "hf lto info",
232 "Get info from LTO tags",
239 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
241 return infoLTO(true);
244 static const char *lto_print_size(uint8_t ti
) {
247 return "101 blocks / 3232 bytes";
249 return "95 blocks / 3040 bytes";
251 return "255 blocks / 8160 bytes";
257 static void lto_print_ci(uint8_t *d
) {
258 uint32_t sn
= (bytes_to_num(d
, 4) & 0x0FFFFFFF);
259 PrintAndLogEx(INFO
, "CM Serial number... " _YELLOW_("%u"), sn
);
260 PrintAndLogEx(INFO
, "Manufacture Id..... " _YELLOW_("%u"), (d
[3] >> 4));
261 PrintAndLogEx(INFO
, "CM Size............ " _YELLOW_("%u") " ( 1024 x %u bytes )", d
[5], d
[5]);
262 PrintAndLogEx(INFO
, "Type............... " _YELLOW_("%s"), sprint_hex_inrow(d
+ 6, 2));
263 PrintAndLogEx(INFO
, "Manufacture info... " _YELLOW_("%s"), sprint_hex_inrow(d
+ 8, 24));
266 static void lto_print_cmwi(uint8_t *d
) {
268 PrintAndLogEx(NORMAL
, "");
269 PrintAndLogEx(INFO
, "--- " _CYAN_("LTO CM Write-Inhibit") " --------------------------");
270 PrintAndLogEx(INFO
, "Raw");
271 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
, 4));
272 PrintAndLogEx(INFO
, "Last write-inhibited block#... " _YELLOW_("%u"), d
[0]);
273 PrintAndLogEx(INFO
, "Block 1 protected flag........ %s", (d
[1] == 0) ? _GREEN_("uninitialised cartridge") : (d
[1] == 1) ? "initialised cartridge" : "n/a");
274 PrintAndLogEx(INFO
, "Reserved for future use....... " _YELLOW_("%s"), sprint_hex_inrow(d
+ 2, 2));
277 static void lto_print_cmpt(uint8_t *d
) {
278 // Block Address: B0 = integer part of [ (start address + offset) ÷ 32 ]
279 // Word Address: w = mod(start address + offset, 32 ) ÷ 2
281 PrintAndLogEx(NORMAL
, "");
282 PrintAndLogEx(INFO
, "--- " _CYAN_("Page Descriptor Table") " ----------------------------------------");
283 PrintAndLogEx(INFO
, "Raw");
284 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
, 28));
285 PrintAndLogEx(INFO
, " ^^^^^^^^ CRC-32");
286 PrintAndLogEx(INFO
, "------------------------------------------------------------------");
287 PrintAndLogEx(INFO
, " start ");
288 PrintAndLogEx(INFO
, " # | ver | id | address | name ");
289 PrintAndLogEx(INFO
, "---+-----+-----+---------+----------------------------------------");
292 for (uint8_t i
= 0; i
< 24; i
+= 4) {
294 uint8_t page_vs
= d
[i
] >> 4;
295 uint16_t page_id
= ((d
[i
] & 0x0F) << 8) | d
[i
+ 1];
296 uint16_t sa
= (d
[i
+ 2] << 8 | d
[i
+ 3]);
297 PrintAndLogEx(INFO
, " %u | %u | %03x | 0x%04X | %s", p
, page_vs
, page_id
, sa
, get_page_name(page_id
));
300 PrintAndLogEx(INFO
, "---+-----+-----+---------+----------------------------------------");
301 PrintAndLogEx(INFO
, "# Pages found... %u", p
);
304 static void lto_print_cmi(uint8_t *d
) {
306 uint16_t page_id
= (d
[0] << 8) | d
[1];
307 uint16_t page_len
= (d
[2] << 8) | d
[3];
310 memcpy(man
, (char *)d
+ 4, 8);
313 memcpy(serial
, (char *)d
+ 12, 10);
315 uint16_t cart_type
= (d
[22] << 8) | d
[23];
317 memcpy(dom
, (char *)d
+ 24, 8);
319 uint16_t tape_len
= (d
[32] << 8) | d
[33];
320 uint16_t tape_thick
= (d
[34] << 8) | d
[35];
321 uint16_t empty_reel
= (d
[36] << 8) | d
[37];
322 uint16_t hub_radius
= (d
[38] << 8) | d
[39];
323 uint16_t full_reel
= (d
[40] << 8) | d
[41];
324 uint16_t max_media_speed
= (d
[42] << 8) | d
[43];
326 memcpy(lic
, (char *)d
+ 44, 4);
329 memcpy(cmuse
, (char *)d
+ 48, 12);
331 // uint32_t crc = bytes_to_num(d+60, 4);
333 PrintAndLogEx(NORMAL
, "");
334 PrintAndLogEx(INFO
, "--- " _CYAN_("Cartridge Manufacturer's information") " --------------------------");
335 PrintAndLogEx(INFO
, "Raw");
336 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
, 32));
337 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
+ 32, 32));
338 PrintAndLogEx(INFO
, " ^^^^^^^^ CRC-32");
339 PrintAndLogEx(INFO
, "Page id.................. ..." _YELLOW_("0x%04x"), page_id
);
340 PrintAndLogEx(INFO
, "Page len.................... " _YELLOW_("%u"), page_len
);
341 PrintAndLogEx(INFO
, "Cartridge Manufacturer...... " _YELLOW_("%s"), man
);
342 PrintAndLogEx(INFO
, "Serial number............... " _YELLOW_("%s"), serial
);
343 PrintAndLogEx(INFO
, "Cartridge type.............. " _YELLOW_("0x%02x"), cart_type
);
344 PrintAndLogEx(INFO
, "Date of manufacture......... " _YELLOW_("%s"), dom
);
345 PrintAndLogEx(INFO
, "Tape len.................... " _YELLOW_("%u"), tape_len
);
346 PrintAndLogEx(INFO
, "Tape thickness.............. " _YELLOW_("%u"), tape_thick
);
347 PrintAndLogEx(INFO
, "Empty reel inertia.......... " _YELLOW_("%u") " %s", empty_reel
, (empty_reel
== 7270) ? "( def )" : "");
348 PrintAndLogEx(INFO
, "Hub radius.................. " _YELLOW_("%u") " %s", hub_radius
, (hub_radius
== 22528) ? "( def )" : "");
349 PrintAndLogEx(INFO
, "Full reel pack radius....... " _YELLOW_("%u") " / 0x%04x", full_reel
, full_reel
);
350 PrintAndLogEx(INFO
, "Maximum media speed......... " _YELLOW_("%u"), max_media_speed
);
351 PrintAndLogEx(INFO
, "License code................ " _YELLOW_("%s"), lic
);
352 PrintAndLogEx(INFO
, "Cartridge manufacture use... " _YELLOW_("%s"), cmuse
);
355 static void lto_print_mmi(uint8_t *d
) {
356 PrintAndLogEx(NORMAL
, "");
357 PrintAndLogEx(INFO
, "--- " _CYAN_("Media Manufacturer's information") " --------------------------");
358 PrintAndLogEx(INFO
, "Raw");
359 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
, 32));
360 PrintAndLogEx(INFO
, " " _YELLOW_("%s"), sprint_hex_inrow(d
+ 32, 32));
361 PrintAndLogEx(INFO
, " ^^^^^^^^ CRC-32");
364 uint16_t page_id
= (d
[0] << 8) | d
[1];
365 uint16_t page_len
= (d
[2] << 8) | d
[3];
368 memcpy(man
, (char *)d
+ 4, 48);
369 PrintAndLogEx(INFO
, "Page id.................... " _YELLOW_("0x%04x"), page_id
);
370 PrintAndLogEx(INFO
, "Page len................... " _YELLOW_("%u"), page_len
);
371 PrintAndLogEx(INFO
, "Servowriter Manufacturer... " _YELLOW_("%s"), man
);
378 // - initialisation data (64b)
379 // - Cartridge Status and Tape Alert Flags (64b)
380 // - Usage Information (4*64b , 256b)
381 // - Tape Write Pass (48b)
382 // - Tape Directory (16*xx) (max 1536b)
383 // - EOD Information (64b)
384 // - Mechanism Related (384b)
385 // - Application Specific Data (1056b)
386 // - Suspended Append Writes (128b)
388 static int CmdHFLTOReader(const char *Cmd
) {
390 CLIParserContext
*ctx
;
391 CLIParserInit(&ctx
, "hf lto reader",
392 "Act as a LTO-CM reader. Look for LTO-CM tags until Enter or the pm3 button is pressed",
393 "hf lto reader -@ -> continuous reader mode"
398 arg_lit0("@", NULL
, "optional - continuous reader mode"),
401 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
402 bool cm
= arg_get_lit(ctx
, 1);
406 PrintAndLogEx(INFO
, "Press " _GREEN_("<Enter>") " to exit");
409 return reader_lto(cm
, true);
412 int reader_lto(bool loop
, bool verbose
) {
414 int ret
= PM3_SUCCESS
;
417 uint8_t serial
[5] = {0};
418 uint8_t serial_len
= sizeof(serial
);
419 uint8_t type_info
[2] = {0};
421 lto_switch_off_field();
422 lto_switch_on_field();
423 clearCommandBuffer();
425 ret
= lto_select(serial
, serial_len
, type_info
, verbose
);
427 if (ret
!= PM3_SUCCESS
) {
432 if (ret
== PM3_SUCCESS
) {
435 PrintAndLogEx(NORMAL
, "");
438 PrintAndLogEx(INFO
, "UID......... " _GREEN_("%s"), sprint_hex_inrow(serial
, sizeof(serial
)));
441 } while (loop
&& kbd_enter_pressed() == false);
443 lto_switch_off_field();
447 int infoLTO(bool verbose
) {
449 clearCommandBuffer();
450 lto_switch_on_field();
452 uint8_t serial_number
[5];
453 uint8_t serial_len
= sizeof(serial_number
);
454 uint8_t type_info
[2];
456 int ret_val
= lto_select(serial_number
, serial_len
, type_info
, verbose
);
457 if (verbose
== false) {
458 if (ret_val
== PM3_SUCCESS
) {
459 PrintAndLogEx(NORMAL
, "");
460 PrintAndLogEx(INFO
, "UID......... " _YELLOW_("%s"), sprint_hex_inrow(serial_number
, sizeof(serial_number
)));
462 lto_switch_off_field();
466 if (ret_val
== PM3_SUCCESS
) {
467 PrintAndLogEx(NORMAL
, "");
468 PrintAndLogEx(INFO
, "--- " _CYAN_("Tag Information") " ---------------------------");
469 PrintAndLogEx(INFO
, "UID......... " _YELLOW_("%s"), sprint_hex_inrow(serial_number
, sizeof(serial_number
)));
470 PrintAndLogEx(INFO
, "Type info... " _YELLOW_("%s"), sprint_hex_inrow(type_info
, sizeof(type_info
)));
471 PrintAndLogEx(INFO
, "Memory...... " _YELLOW_("%s"), lto_print_size(type_info
[1]));
472 if (type_info
[1] > 3) {
473 PrintAndLogEx(INFO
, "Unknown LTO tag, report to @iceman!");
476 PrintAndLogEx(NORMAL
, "");
477 PrintAndLogEx(INFO
, "--- " _CYAN_("LTO Cartridge Information") " -----------------");
482 if (lto_rdbl(0, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
484 memcpy(b0
, d00_d15
, 16);
485 memcpy(b0
+ 16, d16_d31
, 16);
489 if (lto_rdbl(1, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
491 memcpy(b1
, d00_d15
, 16);
492 memcpy(b1
+ 16, d16_d31
, 16);
494 lto_print_cmpt(b1
+ 4);
496 // read block 2 - cartidge manufacture information
497 if (lto_rdbl(2, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
499 memcpy(b2_3
, d00_d15
, 16);
500 memcpy(b2_3
+ 16, d16_d31
, 16);
502 if (lto_rdbl(3, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
503 memcpy(b2_3
+ 32, d00_d15
, 16);
504 memcpy(b2_3
+ 48, d16_d31
, 16);
509 if (lto_rdbl(4, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
511 memcpy(b2_3
, d00_d15
, 16);
512 memcpy(b2_3
+ 16, d16_d31
, 16);
514 if (lto_rdbl(5, d00_d15
, d16_d31
, verbose
) == PM3_SUCCESS
) {
515 memcpy(b2_3
+ 32, d00_d15
, 16);
516 memcpy(b2_3
+ 48, d16_d31
, 16);
523 PrintAndLogEx(NORMAL
, "");
524 lto_switch_off_field();
528 static int CmdHfLTOList(const char *Cmd
) {
529 return CmdTraceListAlias(Cmd
, "hf lto", "lto -c");
532 int rdblLTO(uint8_t st_blk
, uint8_t end_blk
, bool verbose
) {
534 clearCommandBuffer();
535 lto_switch_on_field();
537 uint8_t serial_number
[5];
538 uint8_t serial_len
= sizeof(serial_number
);
539 uint8_t type_info
[2];
540 int ret_val
= lto_select(serial_number
, serial_len
, type_info
, verbose
);
542 if (ret_val
!= PM3_SUCCESS
) {
543 lto_switch_off_field();
547 uint8_t block_data_d00_d15
[18];
548 uint8_t block_data_d16_d31
[18];
549 uint8_t block_data
[32];
551 for (uint8_t i
= st_blk
; i
< end_blk
+ 1; i
++) {
553 ret_val
= lto_rdbl(i
, block_data_d00_d15
, block_data_d16_d31
, verbose
);
555 if (ret_val
== PM3_SUCCESS
) {
557 memcpy(block_data
, block_data_d00_d15
, 16);
558 memcpy(block_data
+ 16, block_data_d16_d31
, 16);
559 PrintAndLogEx(SUCCESS
, "BLK %03d: " _YELLOW_("%s"), i
, sprint_hex_inrow(block_data
, sizeof(block_data
)));
561 lto_switch_off_field();
566 lto_switch_off_field();
570 static int CmdHfLTOReadBlock(const char *Cmd
) {
571 CLIParserContext
*ctx
;
572 CLIParserInit(&ctx
, "hf lto rdbl",
573 "Reead blocks from LTO tag",
574 "hf lto rdbl --first 0 --last 254");
578 arg_int0(NULL
, "first", "<dec>", "The first block number to read as an integer"),
579 arg_int0(NULL
, "last", "<dec>", "The last block number to read as an integer"),
582 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
584 int startblock
= arg_get_int_def(ctx
, 1, 0);
585 int endblock
= arg_get_int_def(ctx
, 2, 254);
590 if (endblock
< startblock
) {
591 PrintAndLogEx(ERR
, "First block must be less than last block");
595 return rdblLTO(startblock
, endblock
, true);
598 static int lto_wrbl(uint8_t blk
, uint8_t *data
, bool verbose
) {
600 uint8_t resp
[] = {0, 0};
601 uint16_t resp_len
= 1;
602 uint8_t wrbl_cmd
[] = {0xA0, blk
};
603 uint8_t wrbl_d00_d15
[16];
604 uint8_t wrbl_d16_d31
[16];
606 memcpy(wrbl_d00_d15
, data
, 16);
607 memcpy(wrbl_d16_d31
, data
+ 16, 16);
609 int status
= lto_send_cmd_raw(wrbl_cmd
, sizeof(wrbl_cmd
), resp
, &resp_len
, true, false, verbose
);
610 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
|| resp
[0] != 0x0A) {
611 return PM3_EWRONGANSWER
; // WRITE BLOCK failed
614 status
= lto_send_cmd_raw(wrbl_d00_d15
, sizeof(wrbl_d00_d15
), resp
, &resp_len
, true, false, verbose
);
615 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
|| resp
[0] != 0x0A) {
616 return PM3_EWRONGANSWER
; // WRITE BLOCK failed
619 status
= lto_send_cmd_raw(wrbl_d16_d31
, sizeof(wrbl_d16_d31
), resp
, &resp_len
, true, false, verbose
);
620 if (status
== PM3_ETIMEOUT
|| status
== PM3_ESOFT
|| resp
[0] != 0x0A) {
621 return PM3_EWRONGANSWER
; // WRITE BLOCK failed
627 int wrblLTO(uint8_t blk
, uint8_t *data
, bool verbose
) {
629 clearCommandBuffer();
630 lto_switch_on_field();
632 uint8_t serial_number
[5];
633 uint8_t serial_len
= sizeof(serial_number
);
634 uint8_t type_info
[2];
635 int ret_val
= lto_select(serial_number
, serial_len
, type_info
, verbose
);
637 if (ret_val
!= PM3_SUCCESS
) {
638 lto_switch_off_field();
642 ret_val
= lto_wrbl(blk
, data
, verbose
);
643 lto_switch_off_field();
645 if (ret_val
== PM3_SUCCESS
) {
646 PrintAndLogEx(SUCCESS
, "BLK %03d: " _YELLOW_("write success"), blk
);
648 PrintAndLogEx(WARNING
, "BLK %03d: write error. Maybe this is a read-only block address.", blk
);
654 static int CmdHfLTOWriteBlock(const char *Cmd
) {
655 CLIParserContext
*ctx
;
656 CLIParserInit(&ctx
, "hf lto wrbl",
657 "Write data to block on LTO tag",
658 "hf lto wrbl --blk 128 -d 0001020304050607080910111213141516171819202122232425262728293031");
662 arg_str1("d", "data", "<hex>", "32 bytes of data to write (64 hex symbols, no spaces)"),
663 arg_int1(NULL
, "blk", "<dec>", "The block number to write to as an integer"),
666 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
668 int block_data_len
= 0;
669 uint8_t block_data
[32] = {0};
671 CLIGetHexWithReturn(ctx
, 1, block_data
, &block_data_len
);
673 if (block_data_len
!= 32) {
674 PrintAndLogEx(ERR
, "Block data is incorrect length");
679 int blk
= arg_get_int_def(ctx
, 2, 0);
683 int res
= wrblLTO(blk
, block_data
, true);
684 if (res
== PM3_SUCCESS
)
685 PrintAndLogEx(HINT
, "Try use 'hf lto rdbl' for verification");
690 int dumpLTO(uint8_t *dump
, bool verbose
) {
692 clearCommandBuffer();
693 lto_switch_on_field();
695 uint8_t serial_number
[5];
696 uint8_t serial_len
= sizeof(serial_number
);
697 uint8_t type_info
[2];
698 int ret_val
= lto_select(serial_number
, serial_len
, type_info
, verbose
);
700 if (ret_val
!= PM3_SUCCESS
) {
701 lto_switch_off_field();
704 // 0003 == 255 blocks x 32 = 8160 bytes
705 // 0002 == 95 blocks x 32 = 3040 bytes
706 // 0001 == 101 blocks x 32 = 3232 bytes
707 uint8_t blocks
= 0xFF;
708 if (type_info
[1] == 0x01) {
710 } else if (type_info
[1] == 0x02) {
713 PrintAndLogEx(SUCCESS
, "Found LTO tag w " _YELLOW_("%s") " memory", lto_print_size(type_info
[1]));
715 uint8_t block_data_d00_d15
[18];
716 uint8_t block_data_d16_d31
[18];
718 for (uint8_t i
= 0; i
< blocks
; i
++) {
720 ret_val
= lto_rdbl(i
, block_data_d00_d15
, block_data_d16_d31
, verbose
);
722 if (ret_val
== PM3_SUCCESS
) {
724 memcpy(dump
+ i
* 32, block_data_d00_d15
, 16);
725 memcpy(dump
+ (i
* 32) + 16, block_data_d16_d31
, 16);
727 lto_switch_off_field();
730 PrintAndLogEx(INPLACE
, "...reading block %d", i
);
734 lto_switch_off_field();
738 static int CmdHfLTODump(const char *Cmd
) {
739 CLIParserContext
*ctx
;
740 CLIParserInit(&ctx
, "hf lto dump",
741 "Dump data from LTO tag",
742 "hf lto dump -f myfile");
746 arg_str0("f", "file", "<fn>", "specify a filename for dumpfile"),
749 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
752 char filename
[FILE_PATH_SIZE
] = {0};
753 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
756 uint32_t dump_len
= CM_MEM_MAX_SIZE
;
757 uint8_t *dump
= calloc(dump_len
, sizeof(uint8_t));
759 PrintAndLogEx(ERR
, "error, cannot allocate memory");
763 int ret_val
= dumpLTO(dump
, true);
764 PrintAndLogEx(NORMAL
, "");
765 if (ret_val
!= PM3_SUCCESS
) {
770 if (strlen(filename
) == 0) {
771 char *fptr
= filename
+ snprintf(filename
, sizeof(filename
), "hf-lto-");
772 FillFileNameByUID(fptr
, dump
, "-dump", 5);
774 pm3_save_dump(filename
, dump
, dump_len
, jsfLto
);
779 int restoreLTO(uint8_t *dump
, bool verbose
) {
781 clearCommandBuffer();
782 lto_switch_on_field();
784 uint8_t type_info
[2];
785 uint8_t serial_number
[5];
786 uint8_t serial_len
= sizeof(serial_number
);
787 int ret_val
= lto_select(serial_number
, serial_len
, type_info
, verbose
);
789 if (ret_val
!= PM3_SUCCESS
) {
790 lto_switch_off_field();
794 uint8_t block_data
[32] = {0};
796 //Block address 0 and 1 are read-only
797 for (uint8_t blk
= 2; blk
< 255; blk
++) {
799 memcpy(block_data
, dump
+ (blk
* 32), 32);
801 ret_val
= lto_wrbl(blk
, block_data
, verbose
);
803 if (ret_val
== PM3_SUCCESS
) {
804 PrintAndLogEx(SUCCESS
, "Block %03d - " _YELLOW_("write success"), blk
);
806 lto_switch_off_field();
811 lto_switch_off_field();
815 static int CmdHfLTRestore(const char *Cmd
) {
816 CLIParserContext
*ctx
;
817 CLIParserInit(&ctx
, "hf lto restore",
818 "Restore data from dumpfile to LTO tag",
819 "hf lto restore -f hf-lto-92C7842CFF.bin|.eml");
823 arg_str1("f", "file", "<fn>", "specify a filename for dumpfile"),
826 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
829 char filename
[FILE_PATH_SIZE
] = {0};
830 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
835 char *lowstr
= str_dup(filename
);
838 if (str_endswith(lowstr
, ".bin")) {
840 uint8_t *dump
= NULL
;
841 if (loadFile_safe(filename
, "", (void **)&dump
, &dump_len
) == PM3_SUCCESS
) {
842 restoreLTO(dump
, true);
846 } else if (str_endswith(lowstr
, ".eml")) {
848 uint8_t *dump
= NULL
;
849 if (loadFileEML_safe(filename
, (void **)&dump
, &dump_len
) == PM3_SUCCESS
) {
850 restoreLTO(dump
, true);
855 PrintAndLogEx(WARNING
, "Warning: invalid dump filename " _YELLOW_("%s") " to restore", filename
);
861 static command_t CommandTable
[] = {
862 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
863 {"dump", CmdHfLTODump
, IfPm3Iso14443a
, "Dump LTO-CM tag to file"},
864 {"info", CmdHfLTOInfo
, IfPm3Iso14443a
, "Tag information"},
865 {"list", CmdHfLTOList
, AlwaysAvailable
, "List LTO-CM history"},
866 {"rdbl", CmdHfLTOReadBlock
, IfPm3Iso14443a
, "Read block"},
867 {"reader", CmdHFLTOReader
, IfPm3Iso14443a
, "Act like a LTO-CM reader"},
868 {"restore", CmdHfLTRestore
, IfPm3Iso14443a
, "Restore dump file to LTO-CM tag"},
869 {"wrbl", CmdHfLTOWriteBlock
, IfPm3Iso14443a
, "Write block"},
870 {NULL
, NULL
, NULL
, NULL
}
873 static int CmdHelp(const char *Cmd
) {
874 (void)Cmd
; // Cmd is not used so far
875 CmdsHelp(CommandTable
);
879 int CmdHFLTO(const char *Cmd
) {
880 clearCommandBuffer();
881 return CmdsParse(CommandTable
, Cmd
);