Merge branch 'master' into fix/iclass-unhash-preimage-enumeration
Signed-off-by: Iceman <iceman@iuse.se>
This commit is contained in:
355 files changed
+142273
-3638
No files matched your search
@@ -2902,8 +2902,8 @@ static int CmdDiff(const char *Cmd) {
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// sanity check
|
||||
if (IfPm3Rdv4Fw() == false && (splenA > 0 || splenB > 0)) {
|
||||
PrintAndLogEx(WARNING, "No RDV4 Flashmemory available");
|
||||
if (IfPm3Flash() == false && (splenA > 0 || splenB > 0)) {
|
||||
PrintAndLogEx(WARNING, "No Flashmemory available");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
#include "fileutils.h"
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "cliparser.h"
|
||||
#include "comms.h"
|
||||
#include "cmdfpga.h"
|
||||
#include "ui.h"
|
||||
|
||||
static int CmdHelp(const char *Cmd);
|
||||
|
||||
static int CmdConfigFpga(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hw fpga config ",
|
||||
"Read the bin file and write it to the fpga [sram|flash]",
|
||||
"hw fpga config -s -f myfile -> config fpga sram by bin file\n"
|
||||
"hw fpga config -f myfile -> config fpga flash by bin file\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_lit0("s", "sram", "config to sram, or config to flash"),
|
||||
arg_str0("f", "file", "<fn>", "save to file, not suffix include"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
||||
|
||||
// config to sram or flash by -s option
|
||||
bool sram_mode = arg_get_lit(ctx, 1);
|
||||
|
||||
// check for -file option first to determine the output mode
|
||||
int fnlen = 0;
|
||||
char filename[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
|
||||
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if ((! sram_mode) && (! IfFpgaFlash())) {
|
||||
PrintAndLogEx(WARNING, "This device does not support FPGA flash configuration");
|
||||
return PM3_ENOTIMPL;
|
||||
}
|
||||
|
||||
// Check if file name is provided
|
||||
if (fnlen == 0) {
|
||||
PrintAndLogEx(WARNING, "No file specified");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
// Load the file content into data buffer
|
||||
uint8_t *data = NULL;
|
||||
size_t datalen = 0;
|
||||
int res = loadFile_safe(filename, ".bin", (void **)&data, &datalen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
free(data);
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
// TODO DXL Maybe length check for device model is required.
|
||||
// coming soon
|
||||
|
||||
// TODO DXL check response result is required
|
||||
|
||||
struct {
|
||||
uint8_t sram_mode;
|
||||
uint32_t file_length;
|
||||
} PACKED params;
|
||||
params.sram_mode = sram_mode;
|
||||
params.file_length = datalen;
|
||||
|
||||
PacketResponseNG resp;
|
||||
PrintAndLogEx(INFO, "Starting FPGA configuration in " _YELLOW_("%s"), sram_mode ? "SRAM" : "Flash");
|
||||
|
||||
// Start fpga config by mode
|
||||
SendCommandNG(CMD_FPGA_BITSTREAM_CONFIG_START, (uint8_t *)¶ms, sizeof(params));
|
||||
// Wait for response before sending data
|
||||
if (WaitForResponseTimeout(CMD_FPGA_BITSTREAM_CONFIG_START, &resp, 15000) == false) {
|
||||
PrintAndLogEx(WARNING, "start config for fpga timeout while waiting for reply");
|
||||
free(data);
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status == PM3_ENOTIMPL) {
|
||||
PrintAndLogEx(ERR, "Feature not implemented");
|
||||
free(data);
|
||||
return resp.status;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to start FPGA configuration: %d", resp.status);
|
||||
free(data);
|
||||
return resp.status;
|
||||
}
|
||||
|
||||
// Start to send data in chunks
|
||||
size_t offset = 0;
|
||||
while (offset < datalen) {
|
||||
size_t chunk_size = (datalen - offset) > PM3_CMD_DATA_SIZE ? PM3_CMD_DATA_SIZE : (datalen - offset);
|
||||
SendCommandNG(CMD_FPGA_BITSTREAM_CONFIG_WRITE, data + offset, chunk_size);
|
||||
// Wait for response before sending next chunk
|
||||
if (WaitForResponseTimeout(CMD_FPGA_BITSTREAM_CONFIG_WRITE, &resp, 5000) == false) {
|
||||
PrintAndLogEx(WARNING, "transfer data for fpga timeout while waiting for reply");
|
||||
free(data);
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to transfer FPGA configuration data: %d", resp.status);
|
||||
free(data);
|
||||
return resp.status;
|
||||
}
|
||||
offset += chunk_size;
|
||||
// Print progress
|
||||
PrintAndLogEx(INFO, "Sent "_YELLOW_("%zu")" bytes, progress: "_YELLOW_("%.2f%%"), chunk_size, (offset / (float)datalen) * 100);
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "FPGA configuration transfer complete!");
|
||||
|
||||
// Stop fpga config
|
||||
SendCommandNG(CMD_FPGA_BITSTREAM_CONFIG_FINISH, NULL, 0);
|
||||
if (WaitForResponseTimeout(CMD_FPGA_BITSTREAM_CONFIG_FINISH, &resp, 5000) == false) {
|
||||
PrintAndLogEx(WARNING, "stop config for fpga timeout while waiting for reply");
|
||||
free(data);
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to finish FPGA configuration: %d", resp.status);
|
||||
free(data);
|
||||
return resp.status;
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "FPGA configuration successfully!");
|
||||
free(data);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdFpgaSetPowerPWM(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hw fpga pwrpwm ",
|
||||
"Set the proportion of low levels of PWM output from FPGA for controlling driver voltage.",
|
||||
"hw fpga pwrpwm -c 1000 -> Set pwm low-level count to 1000 for HF driver power.\n"
|
||||
"hw fpga pwrpwm -c 1000 -l -> Set pwm low-level count to 1000 for LF driver power.\n");
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_lit0("l", "lf", "Set the driver voltage for LF. When not specified as LF, default setting for HF."),
|
||||
arg_int0("c", "count", "<int>", "PWM output low level counting used to control the voltage of the driver. "
|
||||
"The larger the value, the higher the voltage; "
|
||||
"the smaller the value, the lower the voltage."),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
||||
|
||||
// Is setting for lf?
|
||||
bool is_lf = arg_get_lit(ctx, 1);
|
||||
// the count of low levels of pwm output
|
||||
int count = arg_get_int_def(ctx, 2, 1000);
|
||||
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// TODO DXL check response result is required
|
||||
|
||||
if (count < 0 || count > 4095) {
|
||||
PrintAndLogEx(ERR, "Invalid count value: %d. It should be between 0 and 4095.", count);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
struct {
|
||||
uint8_t is_lf;
|
||||
uint16_t count;
|
||||
} PACKED params = {
|
||||
.is_lf = is_lf,
|
||||
.count = count,
|
||||
};
|
||||
|
||||
// Start fpga config by mode
|
||||
PacketResponseNG resp;
|
||||
SendCommandNG(CMD_PM5_FPGA_SET_PWR_PWM_LOW_COUNT, (uint8_t *)¶ms, sizeof(params));
|
||||
PrintAndLogEx(INFO, "PWM value: " _YELLOW_("%d") ", for %s", count, is_lf ? "LF driver" : "HF driver");
|
||||
|
||||
// Wait for response before sending data
|
||||
if (WaitForResponseTimeout(CMD_PM5_FPGA_SET_PWR_PWM_LOW_COUNT, &resp, 15000) == false) {
|
||||
PrintAndLogEx(WARNING, "set power pwm for fpga timeout while waiting for reply");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Set PWM to FPGA for driver power control complete!");
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static command_t CommandTable[] = {
|
||||
{"help", CmdHelp, AlwaysAvailable, "This help"},
|
||||
{"-------------", CmdHelp, AlwaysAvailable, "----------------------- " _CYAN_("Operation") " -----------------------"},
|
||||
{"config", CmdConfigFpga, IfPm3Present, "Configure FPGA for sram or flash"},
|
||||
{"pwrpwm", CmdFpgaSetPowerPWM, IfPm5, "Adjust the input voltage of the driver through FPGA."},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
static int CmdHelp(const char *Cmd) {
|
||||
(void)Cmd; // Cmd is not used so far
|
||||
CmdsHelp(CommandTable);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int CmdFPGA(const char *Cmd) {
|
||||
clearCommandBuffer();
|
||||
return CmdsParse(CommandTable, Cmd);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef CMD_FPGA_H
|
||||
#define CMD_FPGA_H
|
||||
|
||||
int CmdFPGA(const char *Cmd);
|
||||
|
||||
#endif // CMD_FPGA_H
|
||||
+38
-2
@@ -21,6 +21,7 @@
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "cliparser.h" // parse
|
||||
#include "comms.h" // clearCommandBuffer
|
||||
#include "util.h" // set_rgb
|
||||
#include "lfdemod.h" // computeSignalProperties
|
||||
#include "cmdhf14a.h" // ISO14443-A
|
||||
#include "cmdhf14b.h" // ISO14443-B
|
||||
@@ -298,6 +299,22 @@ int CmdHFSearch(const char *Cmd) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Mirror an antenna tuning level on the PM5 antenna RGB LED. `volt` is scaled
|
||||
// relative to the running peak (`v_max`), so the colour tracks the on-screen bar:
|
||||
// blue = low, green = mid, red = high.
|
||||
static void tune_rgb_update(uint32_t volt, uint32_t v_max) {
|
||||
uint32_t t = (v_max > 0) ? (volt * 510 / v_max) : 0;
|
||||
if (t > 510) {
|
||||
t = 510;
|
||||
}
|
||||
if (t < 255) { // blue -> green
|
||||
set_rgb(0, (uint8_t)t, (uint8_t)(255 - t));
|
||||
} else { // green -> red
|
||||
t -= 255;
|
||||
set_rgb((uint8_t)t, (uint8_t)(255 - t), 0);
|
||||
}
|
||||
}
|
||||
|
||||
int CmdHFTune(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
@@ -315,6 +332,7 @@ int CmdHFTune(const char *Cmd) {
|
||||
arg_lit0(NULL, "mix", "mixed style"),
|
||||
arg_lit0(NULL, "value", "values style"),
|
||||
arg_lit0("v", "verbose", "verbose output"),
|
||||
arg_lit0(NULL, "rgb", "(PM5) mirror the tuning level on the antenna RGB LED"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
@@ -323,8 +341,14 @@ int CmdHFTune(const char *Cmd) {
|
||||
bool is_mix = arg_get_lit(ctx, 3);
|
||||
bool is_value = arg_get_lit(ctx, 4);
|
||||
bool verbose = arg_get_lit(ctx, 5);
|
||||
bool use_rgb = arg_get_lit(ctx, 6);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if (use_rgb && (IfPm5() == false)) {
|
||||
PrintAndLogEx(WARNING, "`--rgb` is only supported on Proxmark5; ignoring");
|
||||
use_rgb = false;
|
||||
}
|
||||
|
||||
if ((is_bar + is_mix + is_value) > 1) {
|
||||
PrintAndLogEx(ERR, "Select only one output style");
|
||||
return PM3_EINVARG;
|
||||
@@ -373,12 +397,18 @@ int CmdHFTune(const char *Cmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ((resp.status == PM3_EOPABORTED) || (resp.length != sizeof(uint16_t))) {
|
||||
if ((resp.status == PM3_EOPABORTED) || (resp.length != sizeof(uint16_t) && resp.length != sizeof(uint32_t))) {
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t volt = resp.data.asDwords[0] & 0xFFFF;
|
||||
uint32_t volt;
|
||||
if (resp.length == sizeof(uint16_t)) {
|
||||
volt = resp.data.asDwords[0] & 0xFFFF;
|
||||
} else {
|
||||
volt = resp.data.asDwords[0]; // U32. It can exceed 65.535V.
|
||||
}
|
||||
|
||||
if (first) {
|
||||
v_max = volt;
|
||||
v_min = volt;
|
||||
@@ -391,6 +421,12 @@ int CmdHFTune(const char *Cmd) {
|
||||
v_count++;
|
||||
}
|
||||
print_progress(volt, v_max, style);
|
||||
if (use_rgb) {
|
||||
tune_rgb_update(volt, v_max);
|
||||
}
|
||||
}
|
||||
if (use_rgb) {
|
||||
set_rgb(0, 0, 0); // turn the LED off on exit
|
||||
}
|
||||
mode[0] = 3;
|
||||
|
||||
|
||||
+181
-13
@@ -3309,6 +3309,79 @@ static int CmdHF15Restore(const char *Cmd) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
// ISO15693 magic "V3" tag support
|
||||
#define ISO15_MAGIC_V3_BLK_UID_LO 0x10
|
||||
#define ISO15_MAGIC_V3_BLK_UID_HI 0x11
|
||||
#define ISO15_MAGIC_V3_BLK_SIG_A 0x14
|
||||
#define ISO15_MAGIC_V3_BLK_SIG_B 0x15
|
||||
|
||||
// signature check in block 0x14/0x15
|
||||
static const uint8_t iso15_magic_v3_sig_a[4] = {0xA5, 0x2B, 0x44, 0x2C};
|
||||
static const uint8_t iso15_magic_v3_sig_b[4] = {0x21, 0xAE, 0x93, 0x00};
|
||||
|
||||
// finalize command write in block 0x14/0x15
|
||||
// hf 15 raw -wac -d 022114A52B442C
|
||||
// hf 15 raw -wac -d 02211569E25D00
|
||||
static const uint8_t iso15_magic_v3_fin_a[4] = {0xA5, 0x2B, 0x44, 0x2C};
|
||||
static const uint8_t iso15_magic_v3_fin_b[4] = {0x69, 0xE2, 0x5D, 0x00};
|
||||
|
||||
// Read one 4-byte block from a magic V3 tag in unaddressed mode.
|
||||
static int hf15_magic_v3_read_blk(uint8_t blockno, uint8_t out[4]) {
|
||||
|
||||
uint16_t approxlen = 2 + 1 + 2;
|
||||
iso15_raw_cmd_t *packet = (iso15_raw_cmd_t *)calloc(1, sizeof(iso15_raw_cmd_t) + approxlen);
|
||||
if (packet == NULL) {
|
||||
PrintAndLogEx(WARNING, "Failed to allocate memory");
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
|
||||
// enforce OPTION flag so we get the lock byte, keeping data at offset 2
|
||||
packet->raw[packet->rawlen++] = arg_get_raw_flag(0, true, false, true);
|
||||
packet->raw[packet->rawlen++] = ISO15693_READBLOCK;
|
||||
packet->raw[packet->rawlen++] = blockno;
|
||||
AddCrc15(packet->raw, packet->rawlen);
|
||||
packet->rawlen += 2;
|
||||
packet->flags = (ISO15_CONNECT | ISO15_READ_RESPONSE);
|
||||
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_HF_ISO15693_COMMAND, (uint8_t *)packet, ISO15_RAW_LEN(packet->rawlen));
|
||||
free(packet);
|
||||
|
||||
PacketResponseNG resp;
|
||||
if (WaitForResponseTimeout(CMD_HF_ISO15693_COMMAND, &resp, 2000) == false) {
|
||||
PrintAndLogEx(DEBUG, "iso15693 timeout");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
|
||||
ISO15_ERROR_HANDLING_RESPONSE
|
||||
|
||||
uint8_t *d = resp.data.asBytes;
|
||||
|
||||
ISO15_ERROR_HANDLING_CARD_RESPONSE(d, resp.length)
|
||||
|
||||
memcpy(out, d + 2, 4);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
// Write one 4-byte block to a magic V3 tag in unaddressed mode.
|
||||
static int hf15_magic_v3_write_blk(uint8_t blockno, const uint8_t *data) {
|
||||
uint16_t flags = arg_get_raw_flag(0, true, false, false);
|
||||
return hf_15_write_blk(NULL, flags, NULL, true, blockno, data, 4);
|
||||
}
|
||||
|
||||
// Detect an un-finalized magic V3 tag by its configuration-mode signature.
|
||||
static bool hf15_magic_v3_is_config_mode(void) {
|
||||
uint8_t a[4] = {0};
|
||||
uint8_t b[4] = {0};
|
||||
if (hf15_magic_v3_read_blk(ISO15_MAGIC_V3_BLK_SIG_A, a) != PM3_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
if (hf15_magic_v3_read_blk(ISO15_MAGIC_V3_BLK_SIG_B, b) != PM3_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
return (memcmp(a, iso15_magic_v3_sig_a, 4) == 0) && (memcmp(b, iso15_magic_v3_sig_b, 4) == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Commandline handling: HF15 CMD CSETUID
|
||||
* Set UID for magic Chinese card
|
||||
@@ -3317,15 +3390,19 @@ static int CmdHF15CSetUID(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hf 15 csetuid",
|
||||
"Set UID for magic Chinese card (only works with such cards)\n",
|
||||
"Set UID for magic Chinese card (only works with such cards)\n"
|
||||
"For magic 'V3' tags this writes the UID configuration only and is repeatable;\n"
|
||||
"run `" _YELLOW_("hf 15 cfinalize") "` afterwards to lock the UID permanently.",
|
||||
"hf 15 csetuid -u E011223344556677 -> use gen1 command\n"
|
||||
"hf 15 csetuid -u E011223344556677 --v2 -> use gen2 command"
|
||||
"hf 15 csetuid -u E011223344556677 --v2 -> use gen2 command\n"
|
||||
"hf 15 csetuid -u E011223344556677 --v3 -> use gen3 (V3) magic tag"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str1("u", "uid", "<hex>", "UID, 8 hex bytes"),
|
||||
arg_lit0("2", "v2", "Use gen2 magic command"),
|
||||
arg_lit0("3", "v3", "Use gen3 (V3) magic tag (repeatable, needs cfinalize)"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
||||
@@ -3337,8 +3414,14 @@ static int CmdHF15CSetUID(const char *Cmd) {
|
||||
int uidlen = 0;
|
||||
CLIGetHexWithReturn(ctx, 1, payload.uid, &uidlen);
|
||||
bool use_v2 = arg_get_lit(ctx, 2);
|
||||
bool use_v3 = arg_get_lit(ctx, 3);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if (use_v2 && use_v3) {
|
||||
PrintAndLogEx(WARNING, "Select only one of " _YELLOW_("--v2") " / " _YELLOW_("--v3"));
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (uidlen != ISO15693_UID_LENGTH) {
|
||||
PrintAndLogEx(WARNING, "UID must include 8 hex bytes, got " _RED_("%i"), uidlen);
|
||||
return PM3_EINVARG;
|
||||
@@ -3360,19 +3443,40 @@ static int CmdHF15CSetUID(const char *Cmd) {
|
||||
}
|
||||
|
||||
PrintAndLogEx(INFO, "Writing...");
|
||||
PacketResponseNG resp;
|
||||
clearCommandBuffer();
|
||||
|
||||
uint16_t cmd = CMD_HF_ISO15693_CSETUID;
|
||||
if (use_v2) {
|
||||
cmd = CMD_HF_ISO15693_CSETUID_V2;
|
||||
}
|
||||
if (use_v3) {
|
||||
/*
|
||||
for example id: E011223344556677
|
||||
[=] 16 | 77 66 55 44 | 0 | wfUD
|
||||
[=] 17 | 33 22 11 E0 | 0 | 3"..
|
||||
*/
|
||||
uint8_t blk_lo[4] = {0};
|
||||
uint8_t blk_hi[4] = {0};
|
||||
reverse_array_copy(payload.uid + 4, 4, blk_lo);
|
||||
reverse_array_copy(payload.uid, 4, blk_hi);
|
||||
|
||||
SendCommandNG(cmd, (uint8_t *)&payload, sizeof(payload));
|
||||
if (WaitForResponseTimeout(cmd, &resp, 2000) == false) {
|
||||
PrintAndLogEx(WARNING, "timeout while waiting for reply");
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
if (hf15_magic_v3_write_blk(ISO15_MAGIC_V3_BLK_UID_LO, blk_lo) != PM3_SUCCESS ||
|
||||
hf15_magic_v3_write_blk(ISO15_MAGIC_V3_BLK_UID_HI, blk_hi) != PM3_SUCCESS) {
|
||||
PrintAndLogEx(FAILED, "Setting new UID ( " _RED_("fail") " )");
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
} else {
|
||||
PacketResponseNG resp;
|
||||
clearCommandBuffer();
|
||||
|
||||
uint16_t cmd = CMD_HF_ISO15693_CSETUID;
|
||||
if (use_v2) {
|
||||
cmd = CMD_HF_ISO15693_CSETUID_V2;
|
||||
}
|
||||
|
||||
SendCommandNG(cmd, (uint8_t *)&payload, sizeof(payload));
|
||||
if (WaitForResponseTimeout(cmd, &resp, 2000) == false) {
|
||||
PrintAndLogEx(WARNING, "timeout while waiting for reply");
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(INFO, "Verifying...");
|
||||
@@ -3397,6 +3501,69 @@ static int CmdHF15CSetUID(const char *Cmd) {
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commandline handling: HF15 CMD CFINALIZE
|
||||
* Finalize a magic 'V3' tag - irreversible
|
||||
*/
|
||||
static int CmdHF15CFinalize(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hf 15 cfinalize",
|
||||
"Finalize a magic ISO15693 'V3' tag.\n"
|
||||
_RED_("This operation is irreversible.") " After finalize the configuration\n"
|
||||
"area is erased and the UID can no longer be changed. Set the UID with\n"
|
||||
"`" _YELLOW_("hf 15 csetuid --v3") "` first, then lock it in with this command.",
|
||||
"hf 15 cfinalize -y"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_lit0("y", "yes", "Confirm the irreversible finalize operation"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
bool confirmed = arg_get_lit(ctx, 1);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
PrintAndLogEx(INFO, "Get current tag");
|
||||
|
||||
uint8_t carduid[ISO15693_UID_LENGTH] = {0x00};
|
||||
if (getUID(true, false, carduid) != PM3_SUCCESS) {
|
||||
PrintAndLogEx(FAILED, "no tag found");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
// Safety: only proceed on a tag that is actually an un-finalized magic V3.
|
||||
// Writing the finalize values to any other tag may permanently brick it.
|
||||
if (hf15_magic_v3_is_config_mode() == false) {
|
||||
PrintAndLogEx(FAILED, "tag is not an un-finalized magic " _YELLOW_("V3") " tag");
|
||||
PrintAndLogEx(HINT, "Hint: signature in blocks 0x14/0x15 not found - already finalized or not a V3 tag");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Magic " _GREEN_("V3") " tag in configuration mode ( " _GREEN_("ok") " )");
|
||||
|
||||
if (confirmed == false) {
|
||||
PrintAndLogEx(WARNING, _RED_("This operation is irreversible!") " The UID will be locked permanently.");
|
||||
PrintAndLogEx(WARNING, "Add " _YELLOW_("-y") " to confirm and proceed.");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
PrintAndLogEx(INFO, "Finalizing...");
|
||||
|
||||
if (hf15_magic_v3_write_blk(ISO15_MAGIC_V3_BLK_SIG_A, iso15_magic_v3_fin_a) != PM3_SUCCESS ||
|
||||
hf15_magic_v3_write_blk(ISO15_MAGIC_V3_BLK_SIG_B, iso15_magic_v3_fin_b) != PM3_SUCCESS) {
|
||||
PrintAndLogEx(FAILED, "Finalize ( " _RED_("fail") " )");
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Finalize ( " _GREEN_("ok") " )");
|
||||
PrintAndLogEx(HINT, "Hint: UID is now locked; the tag behaves like a normal ISO15693 tag");
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdHF15SlixEASEnable(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
@@ -4146,6 +4313,7 @@ static command_t CommandTable[] = {
|
||||
{"writedsfid", CmdHF15WriteDsfid, IfPm3Iso15693, "Writes the DSFID on an ISO-15693 tag"},
|
||||
{"-----------", CmdHF15Help, IfPm3Iso15693, "------------------------- " _CYAN_("Magic") " -----------------------"},
|
||||
{"csetuid", CmdHF15CSetUID, IfPm3Iso15693, "Set UID for magic card"},
|
||||
{"cfinalize", CmdHF15CFinalize, IfPm3Iso15693, "Finalize a magic V3 tag (irreversible)"},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
+115
-4
@@ -21,6 +21,7 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "comms.h"
|
||||
#include "cmdtrace.h"
|
||||
@@ -82,6 +83,7 @@
|
||||
#define FELICA_POLLING_REQUEST_SYSTEM_CODE 0x01U
|
||||
#define FELICA_SYSTEM_LIST_JSON "felica/felica_system_code_list"
|
||||
#define FELICA_IC_CODE_LIST_JSON "felica/felica_ic_code_list"
|
||||
#define FELICA_CONTAINER_ISSUE_LIST_JSON "felica/felica_container_issue_information_list"
|
||||
|
||||
#define FELICA_REQUEST_SERVICE_DISCOVERY_BATCH_SIZE 32U
|
||||
#define FELICA_MAX_NODE_NUMBER 0x03FFU
|
||||
@@ -457,6 +459,8 @@ static json_t *felica_system_list = NULL;
|
||||
static bool felica_system_list_loaded = false;
|
||||
static json_t *felica_ic_code_list = NULL;
|
||||
static bool felica_ic_code_list_loaded = false;
|
||||
static json_t *felica_container_issue_list = NULL;
|
||||
static bool felica_container_issue_list_loaded = false;
|
||||
|
||||
static void set_last_known_card(felica_card_select_t card) {
|
||||
last_known_card = card;
|
||||
@@ -771,6 +775,100 @@ static json_t *felica_get_ic_code_list(void) {
|
||||
return felica_ic_code_list;
|
||||
}
|
||||
|
||||
static json_t *felica_get_container_issue_list(void) {
|
||||
if (felica_container_issue_list_loaded) {
|
||||
return felica_container_issue_list;
|
||||
}
|
||||
|
||||
felica_container_issue_list_loaded = true;
|
||||
|
||||
char *path = NULL;
|
||||
if (searchFile(&path, RESOURCES_SUBDIR, FELICA_CONTAINER_ISSUE_LIST_JSON, ".json", true) != PM3_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_error_t error;
|
||||
json_t *root = json_load_file(path, 0, &error);
|
||||
if (root == NULL) {
|
||||
PrintAndLogEx(WARNING, "Failed to parse `%s` line %d: %s", path, error.line, error.text);
|
||||
free(path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (json_is_array(root) == false) {
|
||||
PrintAndLogEx(WARNING, "Invalid `%s` format, expected array root", path);
|
||||
json_decref(root);
|
||||
free(path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
felica_container_issue_list = root;
|
||||
free(path);
|
||||
return felica_container_issue_list;
|
||||
}
|
||||
|
||||
static bool felica_container_issue_matches(const felica_get_container_issue_info_response_t *response,
|
||||
const json_t *entry) {
|
||||
if (response == NULL || json_is_object(entry) == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *format_hex = felica_get_json_string(entry, "format_version_carrier_info");
|
||||
const char *model = felica_get_json_string(entry, "mobile_phone_model_info");
|
||||
if (format_hex == NULL || model == NULL ||
|
||||
strlen(format_hex) != (sizeof(response->format_version_carrier_information) * 2U)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t expected_format[sizeof(response->format_version_carrier_information)] = {0};
|
||||
size_t expected_format_len = 0;
|
||||
if (hexstr_to_byte_array(format_hex, expected_format, &expected_format_len) == false ||
|
||||
expected_format_len != sizeof(expected_format) ||
|
||||
memcmp(response->format_version_carrier_information, expected_format, sizeof(expected_format)) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t model_len = strlen(model);
|
||||
if (model_len == 0 || model_len > sizeof(response->mobile_phone_model_information) ||
|
||||
memcmp(response->mobile_phone_model_information, model, model_len) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = model_len; i < sizeof(response->mobile_phone_model_information); i++) {
|
||||
if (response->mobile_phone_model_information[i] != 0x00) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void felica_print_container_issue_annotations(const felica_get_container_issue_info_response_t *response) {
|
||||
json_t *list = felica_get_container_issue_list();
|
||||
if (response == NULL || list == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
json_t *entry = NULL;
|
||||
json_array_foreach(list, index, entry) {
|
||||
if (felica_container_issue_matches(response, entry) == false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *name = felica_get_json_string(entry, "name");
|
||||
if (name == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *carrier = felica_get_json_string(entry, "carrier");
|
||||
PrintAndLogEx(INFO, " Device Model.......... " _GREEN_("%s"), name);
|
||||
if (carrier) {
|
||||
PrintAndLogEx(INFO, " Carrier............... " _GREEN_("%s"), carrier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const json_t *felica_find_ic_annotation(uint8_t rom_type, uint8_t ic_type) {
|
||||
json_t *ic_code_list = felica_get_ic_code_list();
|
||||
if (ic_code_list == NULL) {
|
||||
@@ -2397,6 +2495,18 @@ static int info_felica(bool verbose) {
|
||||
PrintAndLogEx(INFO, " Code......... " _GREEN_("%s"), sprint_hex_inrow(card.code, sizeof(card.code)));
|
||||
}
|
||||
PrintAndLogEx(INFO, " NFCID2....... " _GREEN_("%s"), sprint_hex_inrow(card.uid, sizeof(card.uid)));
|
||||
const int16_t days_since_2000 = (int16_t)(((uint16_t)card.IDm[5] << 8) | card.IDm[4]);
|
||||
const time_t felica_epoch = (time_t)946684800; // 2000-01-01 UTC
|
||||
const time_t now = time(NULL);
|
||||
if (days_since_2000 >= 0 && now != (time_t)-1 && now >= felica_epoch &&
|
||||
(time_t)days_since_2000 <= (now - felica_epoch) / 86400) {
|
||||
const time_t manufacturing_time = felica_epoch + ((time_t)days_since_2000 * 86400);
|
||||
const struct tm *date = gmtime(&manufacturing_time);
|
||||
char date_string[11] = {0};
|
||||
if (date != NULL && strftime(date_string, sizeof(date_string), "%Y-%m-%d", date) != 0) {
|
||||
PrintAndLogEx(INFO, " Mfg date..... " _GREEN_("%s"), date_string);
|
||||
}
|
||||
}
|
||||
PrintAndLogEx(INFO, "PMM............ " _YELLOW_("%s"), sprint_hex_inrow(card.PMm, sizeof(card.PMm)));
|
||||
PrintAndLogEx(INFO, " IC code...... " _GREEN_("%s") " ( %s )",
|
||||
sprint_hex_inrow(card.iccode, sizeof(card.iccode)),
|
||||
@@ -2469,18 +2579,19 @@ static int info_felica(bool verbose) {
|
||||
sizeof(container_issue_info_response.mobile_phone_model_information),
|
||||
model_ascii,
|
||||
sizeof(model_ascii)
|
||||
);
|
||||
);
|
||||
PrintAndLogEx(INFO, "Container issue info:");
|
||||
PrintAndLogEx(INFO, " Format/Carrier... " _YELLOW_("%s"),
|
||||
PrintAndLogEx(INFO, " Format/Carrier info... " _YELLOW_("%s"),
|
||||
sprint_hex_inrow(container_issue_info_response.format_version_carrier_information,
|
||||
sizeof(container_issue_info_response.format_version_carrier_information)));
|
||||
if (model_is_ascii) {
|
||||
PrintAndLogEx(INFO, " Model............ " _GREEN_("%s") " (ASCII)", model_ascii);
|
||||
PrintAndLogEx(INFO, " Model info............ " _GREEN_("%s") " (ASCII)", model_ascii);
|
||||
} else {
|
||||
PrintAndLogEx(INFO, " Model............ " _YELLOW_("%s") " (HEX)",
|
||||
PrintAndLogEx(INFO, " Model info............ " _YELLOW_("%s") " (HEX)",
|
||||
sprint_hex_inrow(container_issue_info_response.mobile_phone_model_information,
|
||||
sizeof(container_issue_info_response.mobile_phone_model_information)));
|
||||
}
|
||||
felica_print_container_issue_annotations(&container_issue_info_response);
|
||||
}
|
||||
|
||||
const uint16_t container_properties[] = {0x0000, 0x0001};
|
||||
|
||||
@@ -1937,8 +1937,12 @@ static int CmdHFiClassELoad(const char *Cmd) {
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
print_picopass_header((picopass_hdr_t *) dump);
|
||||
print_picopass_info((picopass_hdr_t *) dump);
|
||||
if (bytes_read < sizeof(picopass_hdr_t)) {
|
||||
PrintAndLogEx(FAILED, "Error, dump file is too small to be a valid iCLASS dump - bytes: %zu, expected at least: %zu", bytes_read, sizeof(picopass_hdr_t));
|
||||
} else {
|
||||
print_picopass_header((picopass_hdr_t *) dump);
|
||||
print_picopass_info((picopass_hdr_t *) dump);
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
@@ -2059,8 +2063,12 @@ static int CmdHFiClassEView(const char *Cmd) {
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
print_picopass_header((picopass_hdr_t *) dump);
|
||||
print_picopass_info((picopass_hdr_t *) dump);
|
||||
if (bytes < sizeof(picopass_hdr_t)) {
|
||||
PrintAndLogEx(FAILED, "Error, only %u bytes downloaded, too small to be a valid iCLASS dump - expected at least: %zu", bytes, sizeof(picopass_hdr_t));
|
||||
} else {
|
||||
print_picopass_header((picopass_hdr_t *) dump);
|
||||
print_picopass_info((picopass_hdr_t *) dump);
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
@@ -2326,6 +2334,18 @@ static int CmdHFiClassDecrypt(const char *Cmd) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Below (and in iclass_decode_credentials()) we unconditionally probe
|
||||
// fixed offsets up through block 9 -- app_issuer_area (block 5), the
|
||||
// aa1_encryption flag and block 7 in iclass_decode_credentials(), and
|
||||
// the block 9 PACS/PIN check here -- regardless of what applimit or
|
||||
// decryptedlen/8 say the "real" block count is. All 10 blocks (0-9)
|
||||
// must actually be present in the loaded file before any of that runs.
|
||||
if (decryptedlen < 10 * PICOPASS_BLOCK_SIZE) {
|
||||
PrintAndLogEx(FAILED, "Error, dump file is too small - bytes: %zu, expected at least: %d", decryptedlen, 10 * PICOPASS_BLOCK_SIZE);
|
||||
free(decrypted);
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
have_file = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -2337,7 +2337,7 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is
|
||||
return ;
|
||||
}
|
||||
|
||||
// CLA 0x90 Proprietary class - DESFire's "ISO 7816 wrap of native commands" indicator.
|
||||
// CLA 0x90 Proprietary class - DESFire's "ISO 7816 wrap of native commands" indicator.
|
||||
// Tells the card "the next byte is a DESFire native opcode, not an ISO 7816 standard INS."
|
||||
// INS 0x5A DESFire native opcode SelectApplication (3-byte AID).
|
||||
|
||||
|
||||
@@ -1085,9 +1085,12 @@ static int mfc_read_tag(iso14a_card_select_t *card, uint8_t *carddata, uint8_t n
|
||||
uint8_t *data = resp.data.asBytes;
|
||||
|
||||
if (mfIsSectorTrailerBasedOnBlocks(sectorNo, blockNo)) {
|
||||
// sector trailer. Fill in the keys.
|
||||
// sector trailer. Key A is never readable; preserve Key B when ACL allows it.
|
||||
memcpy(data, keyA + (sectorNo * MIFARE_KEY_SIZE), MIFARE_KEY_SIZE);
|
||||
memcpy(data + 10, keyB + (sectorNo * MIFARE_KEY_SIZE), MIFARE_KEY_SIZE);
|
||||
uint8_t trailer_acl = mf_get_accesscondition(3, &data[6]);
|
||||
if (trailer_acl != 0 && trailer_acl != 1 && trailer_acl != 2) {
|
||||
memcpy(data + 10, keyB + (sectorNo * MIFARE_KEY_SIZE), MIFARE_KEY_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(carddata + (MFBLOCK_SIZE * (mfFirstBlockOfSector(sectorNo) + blockNo)), data, MFBLOCK_SIZE);
|
||||
|
||||
+21
-3
@@ -639,7 +639,7 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int DesfirePCRun(DesfireContext_t *dctx, const uint8_t proximity_key[MFDES_PC_KEY_LEN], uint8_t rounds, bool verbose) {
|
||||
static int DesfirePCRun(DesfireContext_t *dctx, const uint8_t proximity_key[MFDES_PC_KEY_LEN], uint8_t rounds, bool activate_field, bool verbose) {
|
||||
if (dctx == NULL || proximity_key == NULL || rounds < 1 || rounds > MFDES_PC_MAX_ROUNDS) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
@@ -655,7 +655,7 @@ static int DesfirePCRun(DesfireContext_t *dctx, const uint8_t proximity_key[MFDE
|
||||
uint8_t prepare_resp[APDU_RES_LEN] = {0};
|
||||
size_t prepare_resp_len = 0;
|
||||
uint8_t respcode = 0xFF;
|
||||
res = DesfireExchangeEx(true, dctx, MFDES_PREPARE_PC, NULL, 0, &respcode, prepare_resp, &prepare_resp_len, true, 0);
|
||||
res = DesfireExchangeEx(activate_field, dctx, MFDES_PREPARE_PC, NULL, 0, &respcode, prepare_resp, &prepare_resp_len, true, 0);
|
||||
if (res != PM3_SUCCESS) {
|
||||
uint16_t sw = status(respcode);
|
||||
PrintAndLogEx(ERR, "Prepare proximity check command failed. Result: %d %s", res, DesfireGetErrorString(res, &sw));
|
||||
@@ -4591,6 +4591,7 @@ static int CmdHF14ADesPC(const char *Cmd) {
|
||||
"This command uses plain communication with a dedicated 16-byte AES proximity key.",
|
||||
"hf mfdes pc --key 00000000000000000000000000000000\n"
|
||||
"hf mfdes pc --key 00112233445566778899aabbccddeeff --rounds 4\n"
|
||||
"hf mfdes pc --aid 123456 --key 00112233445566778899aabbccddeeff\n"
|
||||
"hf mfdes pc --key 00112233445566778899aabbccddeeff -c native -a");
|
||||
|
||||
void *argtable[] = {
|
||||
@@ -4600,6 +4601,7 @@ static int CmdHF14ADesPC(const char *Cmd) {
|
||||
arg_str1("k", "key", "<hex>", "Key (AES-128, exactly 16 bytes)"),
|
||||
arg_int0("r", "rounds", "<dec>", "Number of rounds (1..8), default 8"),
|
||||
arg_str0("c", "ccset", "<native|niso>", "Communication command set (default from `hf mfdes default`)"),
|
||||
arg_str0(NULL, "aid", "<hex>", "Application ID (3 hex bytes, big endian)"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
@@ -4635,6 +4637,13 @@ static int CmdHF14ADesPC(const char *Cmd) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
uint32_t appid = 0;
|
||||
bool appid_present = false;
|
||||
if (CLIGetUint32Hex(ctx, 6, 0, &appid, &appid_present, 3, "AID must have 3 bytes length")) {
|
||||
CLIParserFree(ctx);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
SetAPDULogging(APDULogging);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
@@ -4643,7 +4652,16 @@ static int CmdHF14ADesPC(const char *Cmd) {
|
||||
DesfireSetCommMode(&dctx, DCMPlain);
|
||||
DesfireSetSecureChannel(&dctx, DACNone);
|
||||
|
||||
int res = DesfirePCRun(&dctx, proximity_key, rounds, verbose);
|
||||
int res = PM3_SUCCESS;
|
||||
if (appid_present) {
|
||||
res = DesfireSelectAndAuthenticateAppW(&dctx, DACNone, ISW6bAID, appid, true, verbose);
|
||||
if (res != PM3_SUCCESS) {
|
||||
DropField();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
res = DesfirePCRun(&dctx, proximity_key, rounds, !appid_present, verbose);
|
||||
DropField();
|
||||
return res;
|
||||
}
|
||||
|
||||
+8
-28
@@ -943,44 +943,26 @@ static int fm11_generate_1nt_candidates(uint32_t uid, uint32_t nt, uint32_t nt_e
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static uint16_t fm11_i_lfsr16[1 << 16] = {0};
|
||||
static uint16_t fm11_s_lfsr16[1 << 16] = {0};
|
||||
static uint16_t fm11_prev8_lfsr16[1 << 16] = {0};
|
||||
static uint16_t fm11_prev14_lfsr16[1 << 16] = {0};
|
||||
static uint16_t fm11_prev8_lfsr16[1 << 16];
|
||||
static uint16_t fm11_prev14_lfsr16[1 << 16];
|
||||
static bool fm11_lfsr16_ready = false;
|
||||
|
||||
static uint16_t fm11_prev_lfsr16(uint16_t nonce);
|
||||
|
||||
static void fm11_init_lfsr16_table(void) {
|
||||
if (fm11_lfsr16_ready) {
|
||||
return;
|
||||
}
|
||||
uint16_t buffer[16] = { 0 };
|
||||
uint16_t x = 1;
|
||||
for (uint16_t i = 1; i; ++i) {
|
||||
fm11_i_lfsr16[(x & 0xff) << 8 | x >> 8] = i;
|
||||
fm11_s_lfsr16[i] = (x & 0xff) << 8 | x >> 8;
|
||||
for (uint32_t i = 0; i < 65536 + 16; ++i) {
|
||||
fm11_prev8_lfsr16[buffer[(i + 8) % 16]] = buffer[i % 16];
|
||||
fm11_prev14_lfsr16[buffer[(i + 14) % 16]] = buffer[i % 16];
|
||||
|
||||
x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;
|
||||
}
|
||||
for (uint32_t nonce = 0; nonce <= UINT16_MAX; nonce++) {
|
||||
uint16_t p = nonce;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
p = fm11_prev_lfsr16(p);
|
||||
}
|
||||
fm11_prev8_lfsr16[nonce] = p;
|
||||
for (uint8_t i = 8; i < 14; i++) {
|
||||
p = fm11_prev_lfsr16(p);
|
||||
}
|
||||
fm11_prev14_lfsr16[nonce] = p;
|
||||
buffer[i % 16] = (x & 0xFF) << 8 | x >> 8;
|
||||
}
|
||||
fm11_lfsr16_ready = true;
|
||||
}
|
||||
|
||||
static uint16_t fm11_prev_lfsr16(uint16_t nonce) {
|
||||
uint16_t i = fm11_i_lfsr16[nonce];
|
||||
i = (i == 1) ? 0xffff : i - 1;
|
||||
return fm11_s_lfsr16[i];
|
||||
}
|
||||
|
||||
static uint16_t fm11_compute_seednt16_nt32(uint32_t nt32, uint64_t key) {
|
||||
static const uint8_t a[] = {0, 8, 9, 4, 6, 11, 1, 15, 12, 5, 2, 13, 10, 14, 3, 7};
|
||||
static const uint8_t b[] = {0, 13, 1, 14, 4, 10, 15, 7, 5, 3, 8, 6, 9, 2, 12, 11};
|
||||
@@ -3071,8 +3053,6 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t total_candidates = 0;
|
||||
for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) {
|
||||
|
||||
+279
-7
@@ -36,6 +36,7 @@
|
||||
#include "ui.h"
|
||||
#include "fpga.h"
|
||||
#include "cmdhw.h"
|
||||
#include "cmdfpga.h"
|
||||
#include "cmddata.h"
|
||||
#include "commonutil.h"
|
||||
#include "preferences.h"
|
||||
@@ -52,7 +53,20 @@
|
||||
|
||||
static int CmdHelp(const char *Cmd);
|
||||
|
||||
static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used) {
|
||||
static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used, uint32_t flash_size) {
|
||||
// AT32 (PM5): the chip id is an ARM DBGMCU IDCODE, not an Atmel CIDR, so the
|
||||
// AT91 decode below does not apply (it would print "Unknown" and a bogus flash
|
||||
// size). Report the MCU and use the real flash size the device reported.
|
||||
if (IfPm5()) {
|
||||
PrintAndLogEx(NORMAL, " MCU....... " _YELLOW_("%s"), "AT32F437");
|
||||
uint32_t mem_kb = flash_size / 1024;
|
||||
PrintAndLogEx(NORMAL, " Memory.... " _YELLOW_("%u") " KB ( " _YELLOW_("%2.0f%%") " used )"
|
||||
, mem_kb
|
||||
, mem_kb == 0 ? 0.0f : (float)mem_used / (mem_kb * 1024) * 100
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const char *asBuff;
|
||||
switch (iChipID) {
|
||||
case 0x270B0A40:
|
||||
@@ -156,11 +170,24 @@ static void lookup_chipid_short(uint32_t iChipID, uint32_t mem_used) {
|
||||
);
|
||||
}
|
||||
|
||||
static void lookupChipID(uint32_t iChipID, uint32_t mem_used) {
|
||||
static void lookupChipID(uint32_t iChipID, uint32_t mem_used, uint32_t flash_size) {
|
||||
const char *asBuff;
|
||||
uint32_t mem_avail = 0;
|
||||
PrintAndLogEx(NORMAL, "\n [ " _YELLOW_("Hardware") " ]");
|
||||
|
||||
// AT32 (PM5): the chip id is an ARM DBGMCU IDCODE, not an Atmel CIDR, so the
|
||||
// verbose AT91 decode below does not apply. Print a short AT32 summary instead.
|
||||
if (IfPm5()) {
|
||||
PrintAndLogEx(NORMAL, " --= uC: AT32F437");
|
||||
uint32_t mem_kb = flash_size / 1024;
|
||||
PrintAndLogEx(NORMAL, " --= Nonvolatile Program Memory Size: %u KB, Used: %u bytes (%2.0f%%)"
|
||||
, mem_kb
|
||||
, mem_used
|
||||
, mem_kb == 0 ? 0.0f : (float)mem_used / (mem_kb * 1024) * 100
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (iChipID) {
|
||||
case 0x270B0A40:
|
||||
asBuff = "AT91SAM7S512 Rev A";
|
||||
@@ -1701,6 +1728,215 @@ int set_fpga_mode(uint8_t mode) {
|
||||
return resp.status;
|
||||
}
|
||||
|
||||
static int CmdPM5Ant(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hw ant_pm5",
|
||||
"Control the antennal of pm5",
|
||||
"hw ant_pm5 --set <u8_data> -> Write the data of IO data register\n"
|
||||
"hw ant_pm5 -m --set <u8_data> -> Write the data of IO map register\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_lit0("m", "map", "Write the IO map register"),
|
||||
arg_u64_0("s", "set", "<u8_data>", "Set PM5 antenna"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
bool rw_map = arg_get_lit(ctx, 1);
|
||||
uint64_t io = arg_get_u64_def(ctx, 2, -1);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
PacketResponseNG resp;
|
||||
|
||||
// Read the current value of the register before writing
|
||||
struct {
|
||||
uint8_t reg_type; // 0 is io reg, 1 is map reg.
|
||||
} PACKED payload_read = {
|
||||
.reg_type = rw_map ? 1 : 0,
|
||||
};
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_ANT_CONTROL_READ, (uint8_t*)&payload_read, sizeof(payload_read));
|
||||
if (WaitForResponseTimeout(CMD_ANT_CONTROL_READ, &resp, 1000) == false) {
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to read PM5 antenna register");
|
||||
return resp.status;
|
||||
}
|
||||
PrintAndLogEx(INFO, "PM5 antenna register read: 0x%02X", resp.data.asBytes[0]);
|
||||
|
||||
// Write the new value to the register(If need)
|
||||
if (io != (uint64_t)-1) {
|
||||
struct {
|
||||
uint8_t data;
|
||||
uint8_t reg_type; // 0 is io reg, 1 is map reg.
|
||||
} PACKED payload_write = {
|
||||
.reg_type = rw_map ? 1 : 0,
|
||||
.data = io & 0xFF,
|
||||
};
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_ANT_CONTROL_WRITE, (uint8_t*)&payload_write, sizeof(payload_write));
|
||||
if (WaitForResponseTimeout(CMD_ANT_CONTROL_WRITE, &resp, 1000) == false) {
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to write PM5 antenna register");
|
||||
return resp.status;
|
||||
}
|
||||
PrintAndLogEx(INFO, "PM5 antenna register written: 0x%02X", payload_write.data);
|
||||
}
|
||||
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdDeviceFactoryData(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hw factorydata",
|
||||
"Get/Set the factory data for Device",
|
||||
"hw factorydata --load <file> -> Write the factory data to device from file\n"
|
||||
"hw factorydata -> Read and parse the factory data from device\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str0(NULL, "load", "<fn>", "Load factory data from file to device"),
|
||||
arg_param_end
|
||||
};
|
||||
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
|
||||
int fnlen = 0;
|
||||
char filename[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
|
||||
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// Read the factory data from device
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_EEPROM_FACTORY_INFO_READ, NULL, 0);
|
||||
PacketResponseNG resp;
|
||||
if (WaitForResponseTimeout(CMD_EEPROM_FACTORY_INFO_READ, &resp, 1000) == false) {
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to read factory data, maybe eeprom unavailable.");
|
||||
return resp.status;
|
||||
}
|
||||
// Parse the factory data
|
||||
if (resp.length) {
|
||||
proxmark5_factory_info_v1_t *factory = (proxmark5_factory_info_v1_t *)resp.data.asBytes;
|
||||
PrintAndLogEx(INFO, "Factory data read from device:");
|
||||
PrintAndLogEx(INFO, " - Data raw(hex) : 0x%s", sprint_hex_inrow(resp.data.asBytes, resp.length));
|
||||
PrintAndLogEx(INFO, " - Version : %d", factory->factory_info_version);
|
||||
PrintAndLogEx(INFO, " - Signature : %s",
|
||||
sprint_hex_inrow(factory->ecdsa_secp256k1_signature, sizeof(factory->ecdsa_secp256k1_signature)));
|
||||
PrintAndLogEx(INFO, " - Timestamp : %" PRIu64, factory->info.unix_timestamp);
|
||||
PrintAndLogEx(INFO, " - Chip Unique ID : %s",
|
||||
sprint_hex_inrow(factory->info.chip_unique_id, sizeof(factory->info.chip_unique_id)));
|
||||
PrintAndLogEx(INFO, " - Production ID : %" PRIu32, factory->info.production_id);
|
||||
PrintAndLogEx(INFO, " - Hardware Version : %" PRIu32, factory->info.hardware_version);
|
||||
PrintAndLogEx(INFO, " - AES key : %s",
|
||||
sprint_hex_inrow(factory->info.aes_key, sizeof(factory->info.aes_key)));
|
||||
}
|
||||
|
||||
// If data is provided, it needs to be written
|
||||
if (fnlen) {
|
||||
// Load the file content into data buffer
|
||||
uint8_t *data = NULL;
|
||||
size_t datalen = 0;
|
||||
int res = loadFile_safe(filename, ".bin", (void **)&data, &datalen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
free(data);
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
if (datalen != resp.length) {
|
||||
PrintAndLogEx(WARNING, _RED_("The length of the data to write (%zu) does not match the length "
|
||||
"of the factory data read from device (%u)."), datalen, (unsigned int)resp.length);
|
||||
free(data);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
// Write the data to the device
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_EEPROM_FACTORY_INFO_WRITE, data, datalen);
|
||||
PacketResponseNG resp_write;
|
||||
if (WaitForResponseTimeout(CMD_EEPROM_FACTORY_INFO_WRITE, &resp_write, 1000) == false) {
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
free(data);
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp_write.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to write factory data, maybe eeprom unavailable.");
|
||||
free(data);
|
||||
return resp_write.status;
|
||||
}
|
||||
// Verify the data write
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_EEPROM_FACTORY_INFO_READ, NULL, 0);
|
||||
if (WaitForResponseTimeout(CMD_EEPROM_FACTORY_INFO_READ, &resp, 1000) == false) {
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
free(data);
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to read factory data after write, maybe eeprom unavailable.");
|
||||
free(data);
|
||||
return resp.status;
|
||||
}
|
||||
bool verify_result = false;
|
||||
if (resp.length == datalen) {
|
||||
verify_result = memcmp(resp.data.asBytes, data, datalen) == 0;
|
||||
}
|
||||
if (verify_result) {
|
||||
PrintAndLogEx(SUCCESS, "Factory data written and verified successfully.");
|
||||
} else {
|
||||
PrintAndLogEx(ERR, "Factory data verification failed after write.");
|
||||
free(data);
|
||||
return PM3_EFAILED;
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdPM5QCTest(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hw qcpm5", "QC Test for the PM5", "hf qcpm5");
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
PrintAndLogEx(INFO, "Performing QC test for the PM5...");
|
||||
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_PM5_QC_TEST, NULL, 0);
|
||||
|
||||
PacketResponseNG resp;
|
||||
if (WaitForResponseTimeout(CMD_PM5_QC_TEST, &resp, 20 * 1000) == false) {
|
||||
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
|
||||
PrintAndLogEx(WARNING, "command execution time out");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "failed to perform QC test on PM5, failed item: %d", resp.data.asBytes[0]);
|
||||
return resp.status;
|
||||
}
|
||||
PrintAndLogEx(INFO, "PM5 QC test successful.");
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static command_t CommandTable[] = {
|
||||
{"help", CmdHelp, AlwaysAvailable, "This help"},
|
||||
{"-------------", CmdHelp, AlwaysAvailable, "----------------------- " _CYAN_("Operation") " -----------------------"},
|
||||
@@ -1714,7 +1950,11 @@ static command_t CommandTable[] = {
|
||||
{"bootloader", CmdBootloader, IfPm3Present, "Reboot into bootloader mode"},
|
||||
{"connect", CmdConnect, AlwaysAvailable, "Connect to the device via serial port"},
|
||||
{"dbg", CmdDbg, IfPm3Present, "Set device side debug level"},
|
||||
{"fpga", CmdFPGA, IfPm3Present, "Fpga commands"},
|
||||
{"fpgaoff", CmdFPGAOff, IfPm3Present, "Turn off FPGA on device"},
|
||||
{"ant_pm5", CmdPM5Ant, IfPm5StdAnt, "Control the antennal of pm5"},
|
||||
{"qc_pm5", CmdPM5QCTest, IfPm5, "Perform QC test for the PM5"},
|
||||
{"factorydata", CmdDeviceFactoryData, IfI2cEeprom, "Get/Set the factory data for Device"},
|
||||
{"lcd", CmdLCD, IfPm3Lcd, "Send command/data to LCD"},
|
||||
{"lcdreset", CmdLCDReset, IfPm3Lcd, "Hardware reset LCD"},
|
||||
{"ping", CmdPing, IfPm3Present, "Test if the Proxmark3 is responsive"},
|
||||
@@ -1815,9 +2055,18 @@ void pm3_version_short(void) {
|
||||
|
||||
struct p *payload = (struct p *)&resp.data.asBytes;
|
||||
|
||||
lookup_chipid_short(payload->id, payload->section_size);
|
||||
// Flash size (bytes) is appended after the version string by newer
|
||||
// firmware; 0 if the device didn't send it (older firmware).
|
||||
uint32_t flash_size = 0;
|
||||
if (resp.length >= 12 + payload->versionstr_len + sizeof(uint32_t)) {
|
||||
memcpy(&flash_size, payload->versionstr + payload->versionstr_len, sizeof(flash_size));
|
||||
}
|
||||
|
||||
if (IfPm3Rdv4Fw()) {
|
||||
lookup_chipid_short(payload->id, payload->section_size, flash_size);
|
||||
|
||||
if (IfPm5()) {
|
||||
PrintAndLogEx(NORMAL, " Target.... %s", _YELLOW_("PM5"));
|
||||
} else if (IfPm3Rdv4Fw()) {
|
||||
|
||||
// validate signature data
|
||||
rdv40_validation_t mem;
|
||||
@@ -1954,7 +2203,10 @@ void pm3_version(bool verbose, bool oneliner) {
|
||||
SendCommandNG(CMD_VERSION, NULL, 0);
|
||||
|
||||
if (WaitForResponseTimeout(CMD_VERSION, &resp, 1000)) {
|
||||
if (IfPm3Rdv4Fw()) {
|
||||
if (IfPm5()) {
|
||||
PrintAndLogEx(NORMAL, " Firmware.................. " _GREEN_("PM5"));
|
||||
PrintAndLogEx(NORMAL, " External flash............ %s", IfPm3Flash() ? _GREEN_("present") : _YELLOW_("absent"));
|
||||
} else if (IfPm3Rdv4Fw()) {
|
||||
|
||||
// validate signature data
|
||||
rdv40_validation_t mem;
|
||||
@@ -2016,11 +2268,31 @@ void pm3_version(bool verbose, bool oneliner) {
|
||||
}
|
||||
}
|
||||
PrintAndLogEx(NORMAL, payload->versionstr);
|
||||
if (strstr(payload->versionstr, FPGA_TYPE) == NULL) {
|
||||
// PM5 doesn't report a built-in FPGA version (Gowin bitstream is loaded
|
||||
// externally), so skip the Xilinx FPGA_TYPE match check for it.
|
||||
if (!IfPm5() && strstr(payload->versionstr, FPGA_TYPE) == NULL) {
|
||||
PrintAndLogEx(NORMAL, " FPGA firmware... %s", _RED_("chip mismatch"));
|
||||
}
|
||||
|
||||
lookupChipID(payload->id, payload->section_size);
|
||||
// Flash size (bytes) is appended after the version string by newer
|
||||
// firmware; 0 if the device didn't send it (older firmware).
|
||||
uint32_t flash_size = 0;
|
||||
if (resp.length >= 12 + payload->versionstr_len + sizeof(uint32_t)) {
|
||||
memcpy(&flash_size, payload->versionstr + payload->versionstr_len, sizeof(flash_size));
|
||||
}
|
||||
|
||||
lookupChipID(payload->id, payload->section_size, flash_size);
|
||||
|
||||
// Get unique id of mainchip
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_MAIN_CHIP_UNIQUEID, NULL, 0);
|
||||
if (WaitForResponseTimeout(CMD_MAIN_CHIP_UNIQUEID, &resp, 1000)) {
|
||||
if (resp.length) { // Some processor maybe no unique id.
|
||||
char* uniqueid_hex = sprint_hex_inrow(resp.data.asBytes, resp.length);
|
||||
PrintAndLogEx(NORMAL, " --= Processor Unique ID: " _YELLOW_("0x%s"), uniqueid_hex);
|
||||
}
|
||||
}
|
||||
|
||||
if (armsrc_mismatch) {
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(WARNING, _RED_("ARM firmware does not match the source at the time the client was compiled"));
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "comms.h"
|
||||
#include "util.h" // set_rgb
|
||||
#include "commonutil.h" // ARRAYLEN
|
||||
#include "lfdemod.h" // device/client demods of LF signals
|
||||
#include "ui.h" // for show graph controls
|
||||
@@ -107,6 +108,22 @@ int lfsim_wait_check(uint32_t cmd) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
// Mirror an antenna tuning level on the PM5 antenna RGB LED. `volt` is scaled
|
||||
// relative to the running peak (`v_max`), so the colour tracks the on-screen bar:
|
||||
// blue = low, green = mid, red = high.
|
||||
static void tune_rgb_update(uint32_t volt, uint32_t v_max) {
|
||||
uint32_t t = (v_max > 0) ? (volt * 510 / v_max) : 0;
|
||||
if (t > 510) {
|
||||
t = 510;
|
||||
}
|
||||
if (t < 255) { // blue -> green
|
||||
set_rgb(0, (uint8_t)t, (uint8_t)(255 - t));
|
||||
} else { // green -> red
|
||||
t -= 255;
|
||||
set_rgb((uint8_t)t, (uint8_t)(255 - t), 0);
|
||||
}
|
||||
}
|
||||
|
||||
static int CmdLFTune(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
@@ -128,6 +145,7 @@ static int CmdLFTune(const char *Cmd) {
|
||||
arg_lit0(NULL, "mix", "mixed style"),
|
||||
arg_lit0(NULL, "value", "values style"),
|
||||
arg_lit0("v", "verbose", "verbose output"),
|
||||
arg_lit0(NULL, "rgb", "(PM5) mirror the tuning level on the antenna RGB LED"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
@@ -139,8 +157,14 @@ static int CmdLFTune(const char *Cmd) {
|
||||
bool is_mix = arg_get_lit(ctx, 5);
|
||||
bool is_value = arg_get_lit(ctx, 6);
|
||||
bool verbose = arg_get_lit(ctx, 7);
|
||||
bool use_rgb = arg_get_lit(ctx, 8);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if (use_rgb && (IfPm5() == false)) {
|
||||
PrintAndLogEx(WARNING, "`--rgb` is only supported on Proxmark5; ignoring");
|
||||
use_rgb = false;
|
||||
}
|
||||
|
||||
if (divisor < 19) {
|
||||
PrintAndLogEx(ERR, "divisor must be between 19 and 255");
|
||||
return PM3_EINVARG;
|
||||
@@ -232,6 +256,12 @@ static int CmdLFTune(const char *Cmd) {
|
||||
v_count++;
|
||||
}
|
||||
print_progress(volt, v_max, style);
|
||||
if (use_rgb) {
|
||||
tune_rgb_update(volt, v_max);
|
||||
}
|
||||
}
|
||||
if (use_rgb) {
|
||||
set_rgb(0, 0, 0); // turn the LED off on exit
|
||||
}
|
||||
|
||||
params[0] = 3;
|
||||
|
||||
@@ -200,7 +200,8 @@ static int doPreambleSearch(size_t *startIdx) {
|
||||
size_t size = (11 > g_DemodBufferLen) ? g_DemodBufferLen : 11;
|
||||
*startIdx = 0;
|
||||
|
||||
// skip first two 0 bits as they might have been missed in the demod
|
||||
// A long time ago, the first two zeros of the preamble were skipped
|
||||
// because previous decoders had a probability of missing the first part of the data.
|
||||
uint8_t preamble[EM_PREAMBLE_LEN] = {0, 0, 0, 0, 1, 0, 1, 0};
|
||||
if (!preambleSearchEx(g_DemodBuffer, preamble, EM_PREAMBLE_LEN, &size, startIdx, true)) {
|
||||
|
||||
|
||||
+25
-1
@@ -30,7 +30,7 @@
|
||||
# include "pthread_spin_lock_shim.h" // spinlock shim for OSX ..
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#if defined(__ANDROID__) && __ANDROID_API__ < 24
|
||||
//Spinlock patch for building with Android NDK
|
||||
|
||||
typedef pthread_mutex_t pthread_spinlock_t;
|
||||
@@ -122,6 +122,30 @@ bool IfPm3FpcUsartFromUsb(void) {
|
||||
return IfPm3FpcUsartHostFromUsb() || IfPm3FpcUsartDevFromUsb();
|
||||
}
|
||||
|
||||
bool IfPm5(void) {
|
||||
if (IfPm3Present() == false)
|
||||
return false;
|
||||
return g_pm3_capabilities.is_pm5;
|
||||
}
|
||||
|
||||
bool IfPm5StdAnt(void) {
|
||||
if (IfPm3Present() == false)
|
||||
return false;
|
||||
return g_pm3_capabilities.is_pm5_std_ant;
|
||||
}
|
||||
|
||||
bool IfFpgaFlash(void) {
|
||||
if (IfPm3Present() == false)
|
||||
return false;
|
||||
return g_pm3_capabilities.hw_available_fpga_flash;
|
||||
}
|
||||
|
||||
bool IfI2cEeprom(void) {
|
||||
if (IfPm3Present() == false)
|
||||
return false;
|
||||
return g_pm3_capabilities.hw_available_i2c_eeprom;
|
||||
}
|
||||
|
||||
bool IfPm3Lf(void) {
|
||||
if (IfPm3Present() == false)
|
||||
return false;
|
||||
|
||||
@@ -41,6 +41,10 @@ bool IfPm3FpcUsartHost(void);
|
||||
bool IfPm3FpcUsartHostFromUsb(void);
|
||||
bool IfPm3FpcUsartDevFromUsb(void);
|
||||
bool IfPm3FpcUsartFromUsb(void);
|
||||
bool IfPm5(void);
|
||||
bool IfPm5StdAnt(void);
|
||||
bool IfFpgaFlash(void);
|
||||
bool IfI2cEeprom(void);
|
||||
bool IfPm3Lf(void);
|
||||
bool IfPm3Hitag(void);
|
||||
bool IfPm3EM4x50(void);
|
||||
|
||||
+324
-105
@@ -27,20 +27,28 @@
|
||||
#include "ui.h"
|
||||
#include "elf.h"
|
||||
#include "proxendian.h"
|
||||
#include "at91sam7s512.h"
|
||||
#include "util_posix.h"
|
||||
#include "comms.h"
|
||||
#include "commonutil.h"
|
||||
#include "fileutils.h"
|
||||
#include "frame_progress.h"
|
||||
|
||||
#define FLASH_START 0x100000
|
||||
#include "at91sam7s512.h"
|
||||
// #include "at32f435_437_flash.h" TODO DXL makefile include dirs need add 'armlib/at32_sys/drivers/inc'
|
||||
|
||||
#define BOOTLOADER_SIZE 0x2000
|
||||
#define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
|
||||
// #define BLOCK_SIZE_AT32 0x800 // For at32, if flash size is 4m, the sector size is 4096byte, otherwise 2048byte.
|
||||
#define FLASH_START_AT32 0x08000000
|
||||
#define BOOTLOADER_SIZE_AT32 0x4000 // defined in 'ldscript.defs.at32'
|
||||
#define BOOTLOADER_END_AT32 (FLASH_START_AT32 + BOOTLOADER_SIZE_AT32)
|
||||
|
||||
#define BLOCK_SIZE 0x200
|
||||
// AT91 series universal definition.
|
||||
#define BLOCK_SIZE_AT91 0x200 // For at91, 512byte = 2page
|
||||
#define FLASH_START_AT91 0x100000
|
||||
#define BOOTLOADER_SIZE_AT91 0x2000 // defined in 'ldscript.defs.at91'
|
||||
#define BOOTLOADER_END_AT91 (FLASH_START_AT91 + BOOTLOADER_SIZE_AT91)
|
||||
|
||||
// It is best for the version number of the flasher to be consistent with the version number of the bootrom,
|
||||
// otherwise some capabilities may be missing.
|
||||
#define FLASHER_VERSION BL_VERSION_1_0_0
|
||||
|
||||
static const uint8_t elf_ident[] = {
|
||||
@@ -50,6 +58,7 @@ static const uint8_t elf_ident[] = {
|
||||
EV_CURRENT
|
||||
};
|
||||
|
||||
// TODO DXL It's best to encapsulate and reuse the code here, and put it in commonutil
|
||||
static int chipid_to_mem_avail(uint32_t iChipID) {
|
||||
int mem_avail = 0;
|
||||
switch ((iChipID & 0xF00) >> 8) {
|
||||
@@ -86,10 +95,48 @@ static int chipid_to_mem_avail(uint32_t iChipID) {
|
||||
return mem_avail;
|
||||
}
|
||||
|
||||
// TODO DXL It's best to encapsulate and reuse the code here, and put it in commonutil
|
||||
static int chipid_to_mem_avail_at32(uint32_t idcode) {
|
||||
struct {
|
||||
uint32_t id; // idcode
|
||||
uint32_t flash_size; // KB
|
||||
} at32_idcode_mem_map[] = {
|
||||
{0x70084540, 4032}, // AT32F435ZMT7
|
||||
{0x70083341, 1024}, // AT32F435ZGT7
|
||||
{0x70083242, 256}, // AT32F435ZCT7
|
||||
{0x70084543, 4032}, // AT32F435VMT7
|
||||
{0x70083344, 1024}, // AT32F435VGT7
|
||||
{0x70083245, 256}, // AT32F435VCT7
|
||||
{0x70084546, 4032}, // AT32F435RMT7
|
||||
{0x70083347, 1024}, // AT32F435RGT7
|
||||
{0x70083248, 256}, // AT32F435RCT7
|
||||
{0x70084549, 4032}, // AT32F435CMT7
|
||||
{0x7008334A, 1024}, // AT32F435CGT7
|
||||
{0x7008324B, 256}, // AT32F435CCT7
|
||||
{0x7008454C, 4032}, // AT32F435CMU7
|
||||
{0x7008334D, 1024}, // AT32F435CGU7
|
||||
{0x7008324E, 256}, // AT32F435CCU7
|
||||
{0x7008454F, 4032}, // AT32F437ZMT7
|
||||
{0x70083350, 1024}, // AT32F437ZGT7
|
||||
{0x70083251, 256}, // AT32F437ZCT7
|
||||
{0x70084552, 4032}, // AT32F437VMT7
|
||||
{0x70083353, 1024}, // AT32F437VGT7
|
||||
{0x70083254, 256}, // AT32F437VCT7
|
||||
{0x70084555, 4032}, // AT32F437RMT7
|
||||
{0x70083356, 1024}, // AT32F437RGT7
|
||||
{0x70083257, 256}, // AT32F437RCT7
|
||||
};
|
||||
for (size_t i = 0; i < ARRAYLEN(at32_idcode_mem_map); i++) {
|
||||
if (at32_idcode_mem_map[i].id == idcode) {
|
||||
return at32_idcode_mem_map[i].flash_size;
|
||||
}
|
||||
}
|
||||
return 256; // No idcode found? return a min size.
|
||||
}
|
||||
|
||||
// Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
|
||||
// unaligned segments if needed
|
||||
static int build_segs_from_phdrs(flash_file_t *ctx, uint32_t flash_size) {
|
||||
uint32_t flash_end = FLASH_START + flash_size;
|
||||
static int build_segs_from_phdrs(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
Elf32_Phdr_t *phdr = ctx->phdrs;
|
||||
flash_seg_t *seg;
|
||||
uint32_t last_end = 0;
|
||||
@@ -133,35 +180,35 @@ static int build_segs_from_phdrs(flash_file_t *ctx, uint32_t flash_size) {
|
||||
PrintAndLogEx(ERR, "Error: PHDRs not sorted or overlap");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
if (paddr < FLASH_START || (paddr + filesz) > flash_end) {
|
||||
if (paddr < flash_dev->flash_start || (paddr + filesz) > flash_dev->flash_end) {
|
||||
PrintAndLogEx(ERR, "Error: PHDR is not contained in Flash");
|
||||
if ((paddr + filesz) > flash_end) {
|
||||
if ((paddr + filesz) > flash_dev->flash_end) {
|
||||
PrintAndLogEx(ERR, "Firmware is probably too big for your device");
|
||||
PrintAndLogEx(ERR, "See README.md for information on compiling for platforms with 256KB of flash memory");
|
||||
}
|
||||
return PM3_EFILE;
|
||||
}
|
||||
if (vaddr >= FLASH_START && vaddr < flash_end && (flags & PF_W)) {
|
||||
if (vaddr >= flash_dev->flash_start && vaddr < flash_dev->flash_end && (flags & PF_W)) {
|
||||
PrintAndLogEx(ERR, "Error: Flash VMA segment is writable");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
uint8_t *data;
|
||||
// make extra space if we need to move the data forward
|
||||
data = calloc(filesz + BLOCK_SIZE, sizeof(uint8_t));
|
||||
data = calloc(filesz + flash_dev->block_size, sizeof(uint8_t));
|
||||
if (!data) {
|
||||
PrintAndLogEx(ERR, "Error: Out of memory");
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
memcpy(data, ctx->elf + offset, filesz);
|
||||
|
||||
uint32_t block_offset = paddr & (BLOCK_SIZE - 1);
|
||||
uint32_t block_offset = paddr & (flash_dev->block_size - 1);
|
||||
if (block_offset) {
|
||||
if (ctx->num_segs) {
|
||||
flash_seg_t *prev_seg = seg - 1;
|
||||
uint32_t this_end = paddr + filesz;
|
||||
uint32_t this_firstblock = paddr & ~(BLOCK_SIZE - 1);
|
||||
uint32_t prev_lastblock = (last_end - 1) & ~(BLOCK_SIZE - 1);
|
||||
uint32_t this_firstblock = paddr & ~(flash_dev->block_size - 1);
|
||||
uint32_t prev_lastblock = (last_end - 1) & ~(flash_dev->block_size - 1);
|
||||
|
||||
if (this_firstblock == prev_lastblock) {
|
||||
uint32_t new_length = this_end - prev_seg->start;
|
||||
@@ -209,28 +256,27 @@ static int build_segs_from_phdrs(flash_file_t *ctx, uint32_t flash_size) {
|
||||
}
|
||||
|
||||
// Sanity check segments and check for bootloader writes
|
||||
static int check_segs(flash_file_t *ctx, int can_write_bl, uint32_t flash_size) {
|
||||
uint32_t flash_end = FLASH_START + flash_size;
|
||||
static int check_segs(flash_file_t *ctx, int can_write_bl, flash_dev_t *flash_dev) {
|
||||
for (int i = 0; i < ctx->num_segs; i++) {
|
||||
flash_seg_t *seg = &ctx->segments[i];
|
||||
|
||||
if (seg->start & (BLOCK_SIZE - 1)) {
|
||||
if (seg->start & (flash_dev->block_size - 1)) {
|
||||
PrintAndLogEx(ERR, "Error: Segment is not aligned");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
if (seg->start < FLASH_START) {
|
||||
if (seg->start < flash_dev->flash_start) {
|
||||
PrintAndLogEx(ERR, "Error: Segment is outside of flash bounds");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
if (seg->start + seg->length > flash_end) {
|
||||
if (seg->start + seg->length > flash_dev->flash_end) {
|
||||
PrintAndLogEx(ERR, "Error: Segment is outside of flash bounds");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
if (!can_write_bl && seg->start < BOOTLOADER_END) {
|
||||
if (!can_write_bl && seg->start < flash_dev->boot_end) {
|
||||
PrintAndLogEx(ERR, "Attempted to write bootloader but bootloader writes are not enabled");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
if (can_write_bl && seg->start < BOOTLOADER_END && (seg->start + seg->length > BOOTLOADER_END)) {
|
||||
if (can_write_bl && seg->start < flash_dev->boot_end && (seg->start + seg->length > flash_dev->boot_end)) {
|
||||
PrintAndLogEx(ERR, "Error: Segment is outside of bootloader bounds");
|
||||
return PM3_EFILE;
|
||||
}
|
||||
@@ -238,18 +284,21 @@ static int check_segs(flash_file_t *ctx, int can_write_bl, uint32_t flash_size)
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int print_and_validate_version(struct version_information_t *vi) {
|
||||
if (vi->magic != VERSION_INFORMATION_MAGIC) {
|
||||
// Check version information section for sanity and compatibility with the client, and print it if valid
|
||||
static int print_and_validate_version(flash_file_t *ctx) {
|
||||
if (!CheckValidInformationMagic(ctx->ver_info)) {
|
||||
PrintAndLogEx(ERR, _RED_("ELF file does not contain valid version information"
|
||||
"(magic = 0x%08x)"), ctx->ver_info->magic);
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
// same limit as for ARM image
|
||||
char temp[PM3_CMD_DATA_SIZE - 12] = {0};
|
||||
FormatVersionInformation(temp, sizeof(temp), "", vi);
|
||||
FormatVersionInformation(temp, sizeof(temp), "", ctx->ver_info);
|
||||
PrintAndLogEx(SUCCESS, _CYAN_("ELF file version") _YELLOW_(" %s"), temp);
|
||||
|
||||
if (strlen(g_version_information.armsrc) == 9) {
|
||||
if (strncmp(vi->armsrc, g_version_information.armsrc, 9) != 0) {
|
||||
if (strncmp(ctx->ver_info->armsrc, g_version_information.armsrc, 9) != 0) {
|
||||
PrintAndLogEx(WARNING, _RED_("ARM firmware does not match the source at the time the client was compiled"));
|
||||
return PM3_EINVARG;
|
||||
} else {
|
||||
@@ -265,7 +314,6 @@ int flash_load(flash_file_t *ctx, bool force) {
|
||||
Elf32_Ehdr_t *ehdr;
|
||||
Elf32_Shdr_t *shdrs = NULL;
|
||||
uint8_t *shstr = NULL;
|
||||
struct version_information_t *vi = NULL;
|
||||
int res = PM3_EUNDEF;
|
||||
|
||||
fd = fopen(ctx->filename, "rb");
|
||||
@@ -347,8 +395,8 @@ int flash_load(flash_file_t *ctx, bool force) {
|
||||
for (uint16_t i = 0; i < le16(ehdr->e_shnum); i++) {
|
||||
|
||||
if (strcmp(((char *)shstr) + shdrs[i].sh_name, ".version_information") == 0) {
|
||||
vi = (struct version_information_t *)(ctx->elf + le32(shdrs[i].sh_offset));
|
||||
res = print_and_validate_version(vi);
|
||||
ctx->ver_info = (struct version_information_t *)(ctx->elf + le32(shdrs[i].sh_offset));
|
||||
res = print_and_validate_version(ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -358,8 +406,8 @@ int flash_load(flash_file_t *ctx, bool force) {
|
||||
if (offset >= le32(shdrs[i].sh_addr)) {
|
||||
offset -= le32(shdrs[i].sh_addr);
|
||||
if (offset < le32(shdrs[i].sh_size)) {
|
||||
vi = (struct version_information_t *)(ctx->elf + le32(shdrs[i].sh_offset) + offset);
|
||||
res = print_and_validate_version(vi);
|
||||
ctx->ver_info = (struct version_information_t *)(ctx->elf + le32(shdrs[i].sh_offset) + offset);
|
||||
res = print_and_validate_version(ctx);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -387,15 +435,22 @@ fail:
|
||||
}
|
||||
|
||||
// Prepare an ELF file for flashing
|
||||
int flash_prepare(flash_file_t *ctx, int can_write_bl, int flash_size) {
|
||||
int flash_prepare(flash_file_t *ctx, int can_write_bl, flash_dev_t *flash_dev) {
|
||||
int res = PM3_EUNDEF;
|
||||
|
||||
res = build_segs_from_phdrs(ctx, flash_size);
|
||||
// Check elf file is build for currently connected device?
|
||||
if (!CheckInformationMagicAndChipType(ctx->ver_info, flash_dev->chiptype)) {
|
||||
PrintAndLogEx(ERR, "The elf file is not applicable to the currently connected device.", flash_dev->chiptype);
|
||||
res = PM3_EFILE;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
res = build_segs_from_phdrs(ctx, flash_dev);
|
||||
if (res != PM3_SUCCESS) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
res = check_segs(ctx, can_write_bl, flash_size);
|
||||
res = check_segs(ctx, can_write_bl, flash_dev);
|
||||
if (res != PM3_SUCCESS) {
|
||||
goto fail;
|
||||
}
|
||||
@@ -489,11 +544,11 @@ static int enter_bootloader(char *serial_port_name, bool wait_appear) {
|
||||
return PM3_EFATAL;
|
||||
}
|
||||
|
||||
// Wait for the device to respond with either ACK or NACK.
|
||||
static int wait_for_ack(PacketResponseNG *ack) {
|
||||
WaitForResponse(CMD_UNKNOWN, ack);
|
||||
|
||||
if (ack->cmd != CMD_ACK) {
|
||||
PrintAndLogEx(ERR, "Error: Unexpected reply 0x%04x %s (expected ACK)",
|
||||
PrintAndLogEx(ERR, "\nError: Unexpected reply 0x%04x %s (expected ACK)",
|
||||
ack->cmd,
|
||||
(ack->cmd == CMD_NACK) ? "NACK" : ""
|
||||
);
|
||||
@@ -502,8 +557,10 @@ static int wait_for_ack(PacketResponseNG *ack) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static bool gs_printed_msg = false;
|
||||
// If the BOOTLOADER is too old or damaged, we can suggest that the user update the BOOT.
|
||||
static void flash_suggest_update_bootloader(void) {
|
||||
// Since it's only used internally, we can define it internally.
|
||||
static bool gs_printed_msg = false;
|
||||
if (gs_printed_msg) {
|
||||
return;
|
||||
}
|
||||
@@ -520,14 +577,75 @@ static void flash_suggest_update_bootloader(void) {
|
||||
PrintAndLogEx(INFO, "---------------------------------------------------");
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
gs_printed_msg = true;
|
||||
if (g_session.stdinOnTTY) {
|
||||
PrintAndLogEx(INFO, "Press ENTER to continue or CTRL-C to cancel...");
|
||||
fflush(stdout);
|
||||
while (kbd_enter_pressed() == false) {
|
||||
msleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the device's boot is newer than the current flasher, we can suggest the user update the flasher.
|
||||
static void flash_suggest_update_flasher(void) {
|
||||
PrintAndLogEx(ERR, _RED_("It is recommended that you first " _YELLOW_("update your flasher")));
|
||||
}
|
||||
|
||||
// AT32 series has a wide range of flash sizes, so we check the chipinfo to set the flash end address and block size.
|
||||
static void flash_dev_at32_init(uint32_t chipinfo, flash_dev_t *flash_dev) {
|
||||
flash_dev->flash_start = FLASH_START_AT32;
|
||||
uint32_t flash_size = chipid_to_mem_avail_at32(chipinfo);
|
||||
if (flash_size > 1024) {
|
||||
flash_dev->block_size = 0x1000; // 4K block size for >1M flash
|
||||
} else {
|
||||
flash_dev->block_size = 0x800; // 2K block size for <=1M flash
|
||||
}
|
||||
flash_dev->flash_end = FLASH_START_AT32 + flash_size * 1024;
|
||||
flash_dev->boot_size = BOOTLOADER_SIZE_AT32;
|
||||
flash_dev->boot_end = BOOTLOADER_END_AT32;
|
||||
}
|
||||
|
||||
// AT91 series has some variations in flash size, so we check the chipinfo to set the flash end address
|
||||
// and warn the user if they have a large flash but an old bootloader that doesn't support it.
|
||||
static void flash_dev_at91_init(uint32_t chipinfo, flash_dev_t *flash_dev, int version) {
|
||||
flash_dev->block_size = BLOCK_SIZE_AT91;
|
||||
flash_dev->flash_start = FLASH_START_AT91;
|
||||
flash_dev->flash_end = FLASH_START_AT91 + AT91C_IFLASH_PAGE_SIZE * AT91C_IFLASH_NB_OF_PAGES / 2; // Default 256K MAX
|
||||
flash_dev->boot_size = BOOTLOADER_SIZE_AT91;
|
||||
flash_dev->boot_end = BOOTLOADER_END_AT91;
|
||||
// Check the flash capacity based on the idcode returned by the device, that is, enable support for 512K FLASH.
|
||||
int mem_avail = chipid_to_mem_avail(chipinfo);
|
||||
if (mem_avail != 0) {
|
||||
PrintAndLogEx(INFO, "Available memory on this board: "_YELLOW_("%uK") " bytes\n", mem_avail);
|
||||
if (mem_avail > 256) {
|
||||
if (BL_VERSION_MAJOR(version) < BL_VERSION_MAJOR(BL_VERSION_1_0_0)) {
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ======================"));
|
||||
PrintAndLogEx(ERR, _RED_("Your bootloader does not support writing above 256k"));
|
||||
flash_suggest_update_bootloader();
|
||||
} else {
|
||||
// The capacity of the main chip of the device is greater than 256K,
|
||||
// and BL also supports OTA for chips with such a large capacity.
|
||||
flash_dev->flash_end = FLASH_START_AT91 + AT91C_IFLASH_PAGE_SIZE * AT91C_IFLASH_NB_OF_PAGES;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
PrintAndLogEx(INFO, "Available memory on this board: "_RED_("UNKNOWN")"\n");
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ======================================"));
|
||||
PrintAndLogEx(ERR, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" CHIP_INFO") _RED_(" command")));
|
||||
flash_suggest_update_bootloader();
|
||||
}
|
||||
}
|
||||
|
||||
// Sending simple cmd without any parameters or data payload, just for arg0.
|
||||
static void send_cmd_for_arg0(const uint64_t cmd, uint32_t *arg0) {
|
||||
SendCommandBL(cmd, 0, 0, 0, NULL, 0);
|
||||
PacketResponseNG resp;
|
||||
WaitForResponse(cmd, &resp);
|
||||
*arg0 = resp.oldarg[0];
|
||||
}
|
||||
|
||||
// Go into flashing mode
|
||||
int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t *max_allowed) {
|
||||
int flash_start_flashing(int enable_bl_writes, char *serial_port_name, flash_dev_t *flash_dev) {
|
||||
|
||||
int ret = enter_bootloader(serial_port_name, true);
|
||||
if (ret != PM3_SUCCESS) {
|
||||
@@ -540,31 +658,26 @@ int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t chipinfo = 0;
|
||||
flash_dev->chiptype = MAIN_CHIP_TYPE_NONE;
|
||||
if ((state & DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_TYPE) == DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_TYPE) {
|
||||
send_cmd_for_arg0(CMD_CHIP_TYPE, &flash_dev->chiptype);
|
||||
}
|
||||
|
||||
uint32_t chipinfo = 0;
|
||||
if ((state & DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO) == DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO) {
|
||||
SendCommandBL(CMD_CHIP_INFO, 0, 0, 0, NULL, 0);
|
||||
PacketResponseNG resp;
|
||||
WaitForResponse(CMD_CHIP_INFO, &resp);
|
||||
chipinfo = resp.oldarg[0];
|
||||
send_cmd_for_arg0(CMD_CHIP_INFO, &chipinfo);
|
||||
}
|
||||
|
||||
int version = BL_VERSION_INVALID;
|
||||
|
||||
if ((state & DEVICE_INFO_FLAG_UNDERSTANDS_VERSION) == DEVICE_INFO_FLAG_UNDERSTANDS_VERSION) {
|
||||
|
||||
SendCommandBL(CMD_BL_VERSION, 0, 0, 0, NULL, 0);
|
||||
PacketResponseNG resp;
|
||||
WaitForResponse(CMD_BL_VERSION, &resp);
|
||||
version = resp.oldarg[0];
|
||||
|
||||
// Get bootrom version for features and sanity checks
|
||||
send_cmd_for_arg0(CMD_BL_VERSION, (uint32_t *)&version);
|
||||
// Is version invalid or outside of expected range? maybe bootrom is very old or corrupted?
|
||||
if ((BL_VERSION_MAJOR(version) < BL_VERSION_FIRST_MAJOR) || (BL_VERSION_MAJOR(version) > BL_VERSION_LAST_MAJOR)) {
|
||||
// version info seems fishy
|
||||
version = BL_VERSION_INVALID;
|
||||
version = BL_VERSION_INVALID; // version info seems fishy
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ==========================="));
|
||||
PrintAndLogEx(ERR, _RED_("Note: Your bootloader reported an invalid version number"));
|
||||
flash_suggest_update_bootloader();
|
||||
//
|
||||
} else if (BL_VERSION_MAJOR(version) < BL_VERSION_MAJOR(FLASHER_VERSION)) {
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ==================================="));
|
||||
PrintAndLogEx(ERR, _RED_("Note: Your bootloader reported a version older than this flasher"));
|
||||
@@ -580,48 +693,39 @@ int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t
|
||||
flash_suggest_update_bootloader();
|
||||
}
|
||||
|
||||
uint32_t flash_end = FLASH_START + AT91C_IFLASH_PAGE_SIZE * AT91C_IFLASH_NB_OF_PAGES / 2;
|
||||
*max_allowed = 256;
|
||||
// 1. The old bootloader does not support pm5, nor does it support the 'CMD_CHIP_TYPE' command.
|
||||
// 2. In the absence of CMD_CHIP_TYPE cmd support, pm3 (at91 platform) is selected as a backup solution.
|
||||
// 3. Only by combining the parameters of chiptype and chipinfo can the detailed information of the chip currently used by the device be correctly obtained
|
||||
// 4. This function does not check if the elf file is compatible with the device, so it needs to be checked within the flash_prepare function
|
||||
switch (flash_dev->chiptype) {
|
||||
case MAIN_CHIP_TYPE_NONE:
|
||||
PrintAndLogEx(ERR, _RED_("Bootloader does not support CMD_CHIP_TYPE, assuming AT91 platform"));
|
||||
flash_dev->chiptype = MAIN_CHIP_TYPE_AT91;
|
||||
flash_suggest_update_bootloader();
|
||||
// break; -> Don't break !!! We want to execute the code for MAIN_CHIP_TYPE_AT91 as well to initialize flash_dev with correct values.
|
||||
|
||||
int mem_avail = chipid_to_mem_avail(chipinfo);
|
||||
if (mem_avail != 0) {
|
||||
case MAIN_CHIP_TYPE_AT91:
|
||||
default:
|
||||
flash_dev_at91_init(chipinfo, flash_dev, version);
|
||||
break;
|
||||
|
||||
PrintAndLogEx(INFO, "Available memory on this board: "_YELLOW_("%uK") " bytes\n", mem_avail);
|
||||
|
||||
if (mem_avail > 256) {
|
||||
if (BL_VERSION_MAJOR(version) < BL_VERSION_MAJOR(BL_VERSION_1_0_0)) {
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ======================"));
|
||||
PrintAndLogEx(ERR, _RED_("Your bootloader does not support writing above 256k"));
|
||||
flash_suggest_update_bootloader();
|
||||
} else {
|
||||
flash_end = FLASH_START + AT91C_IFLASH_PAGE_SIZE * AT91C_IFLASH_NB_OF_PAGES;
|
||||
*max_allowed = mem_avail;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
PrintAndLogEx(INFO, "Available memory on this board: "_RED_("UNKNOWN")"\n");
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ======================================"));
|
||||
PrintAndLogEx(ERR, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" CHIP_INFO") _RED_(" command")));
|
||||
flash_suggest_update_bootloader();
|
||||
case MAIN_CHIP_TYPE_AT32:
|
||||
flash_dev_at32_init(chipinfo, flash_dev);
|
||||
break;
|
||||
}
|
||||
|
||||
if (enable_bl_writes) {
|
||||
PrintAndLogEx(INFO, "Permitted flash range: 0x%08x-0x%08x", FLASH_START, flash_end);
|
||||
} else {
|
||||
PrintAndLogEx(INFO, "Permitted flash range: 0x%08x-0x%08x", BOOTLOADER_END, flash_end);
|
||||
}
|
||||
// If you need to flash bootrom, the start addr must be 'flash_start', otherwise, it can be 'boot_end' to skip the bootrom area and save some time.
|
||||
uint32_t start_flash_addr = enable_bl_writes ? flash_dev->flash_start : flash_dev->boot_end;
|
||||
PrintAndLogEx(INFO, "Permitted flash range: 0x%08x-0x%08x", start_flash_addr, flash_dev->flash_end);
|
||||
|
||||
if ((state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) == DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
|
||||
|
||||
if (enable_bl_writes) {
|
||||
SendCommandBL(CMD_START_FLASH, FLASH_START, flash_end, START_FLASH_MAGIC, NULL, 0);
|
||||
SendCommandBL(CMD_START_FLASH, start_flash_addr, flash_dev->flash_end, START_FLASH_MAGIC, NULL, 0);
|
||||
} else {
|
||||
SendCommandBL(CMD_START_FLASH, BOOTLOADER_END, flash_end, 0, NULL, 0);
|
||||
SendCommandBL(CMD_START_FLASH, start_flash_addr, flash_dev->flash_end, 0, NULL, 0);
|
||||
}
|
||||
PacketResponseNG resp;
|
||||
return wait_for_ack(&resp);
|
||||
|
||||
} else {
|
||||
PrintAndLogEx(ERR, _RED_("====================== OBS ! ========================================"));
|
||||
PrintAndLogEx(ERR, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" START_FLASH") _RED_(" command")));
|
||||
@@ -635,32 +739,148 @@ int flash_reboot_bootloader(char *serial_port_name, bool wait_appear) {
|
||||
return enter_bootloader(serial_port_name, wait_appear);
|
||||
}
|
||||
|
||||
static int write_block(uint32_t address, uint8_t *data, uint32_t length) {
|
||||
uint8_t block_buf[BLOCK_SIZE];
|
||||
memset(block_buf, 0xFF, BLOCK_SIZE);
|
||||
memcpy(block_buf, data, length);
|
||||
PacketResponseNG resp;
|
||||
#if defined ICOPYX
|
||||
SendCommandBL(CMD_FINISH_WRITE, address, 0xFF, 0x1FD, block_buf, length);
|
||||
#else
|
||||
SendCommandBL(CMD_FINISH_WRITE, address, 0, 0, block_buf, length);
|
||||
#endif
|
||||
int ret = wait_for_ack(&resp);
|
||||
if (ret && resp.oldarg[0]) {
|
||||
uint32_t lock_bits = resp.oldarg[0] >> 16;
|
||||
bool lock_error = resp.oldarg[0] & AT91C_MC_LOCKE;
|
||||
bool prog_error = resp.oldarg[0] & AT91C_MC_PROGE;
|
||||
bool security_bit = resp.oldarg[0] & AT91C_MC_SECURITY;
|
||||
// Show error information after write failed on AT91 platform.
|
||||
static void flash_write_err_on_at91(uint32_t err) {
|
||||
if (err) {
|
||||
uint32_t lock_bits = err >> 16;
|
||||
bool lock_error = err & AT91C_MC_LOCKE;
|
||||
bool prog_error = err & AT91C_MC_PROGE;
|
||||
bool security_bit = err & AT91C_MC_SECURITY;
|
||||
PrintAndLogEx(NORMAL, "%s", lock_error ? " Lock Error" : "");
|
||||
PrintAndLogEx(NORMAL, "%s", prog_error ? " Invalid Command or bad Keyword" : "");
|
||||
PrintAndLogEx(NORMAL, "%s", security_bit ? " Security Bit is set!" : "");
|
||||
PrintAndLogEx(NORMAL, " Lock Bits: 0x%04x", lock_bits);
|
||||
}
|
||||
}
|
||||
|
||||
// Show error information after write failed on AT32 platform.
|
||||
static void flash_write_err_on_at32(uint32_t err) {
|
||||
// TODO DXL Need to add the header file path of at32 in the makefile/cake of the client(for flash_status_type).
|
||||
// In order to quickly compile and verify, we will temporarily define constant value.
|
||||
// However, later on, the types in the header file should be used.
|
||||
switch (err) {
|
||||
case 0: // FLASH_OPERATE_BUSY
|
||||
PrintAndLogEx(ERR, "Flash is busy");
|
||||
break;
|
||||
case 1: // FLASH_PROGRAM_ERROR
|
||||
PrintAndLogEx(ERR, "Flash program error");
|
||||
break;
|
||||
case 2: // FLASH_EPP_ERROR
|
||||
PrintAndLogEx(ERR, "Erase/Program protection error");
|
||||
break;
|
||||
case 3: // FLASH_OPERATE_DONE
|
||||
// Nothing to do...
|
||||
break;
|
||||
case 4: // FLASH_OPERATE_TIMEOUT
|
||||
PrintAndLogEx(ERR, "Flash operation timeout");
|
||||
break;
|
||||
default:
|
||||
PrintAndLogEx(ERR, "Unknown flash error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// The error did not occur while writing to the flash memory, but rather during data copying and write boundary checks.
|
||||
// This is a software error and is unrelated to the hardware.
|
||||
static void flash_write_err_software(int pm3_err) {
|
||||
if (pm3_err == PM3_EINVARG) {
|
||||
PrintAndLogEx(ERR, _RED_("Error:") " Device rejected the firmware, invalid argument");
|
||||
PrintAndLogEx(ERR, "This may be because the firmware is not compatible with the device or the bootloader is too old");
|
||||
PrintAndLogEx(ERR, "Make sure to use a compatible ELF file and try updating the bootloader if it's old");
|
||||
} else if (pm3_err == PM3_EOVFLOW) {
|
||||
PrintAndLogEx(ERR, _RED_("Error:") " Device rejected the firmware, overflow");
|
||||
PrintAndLogEx(ERR, "This may be because the firmware is too large for the device");
|
||||
PrintAndLogEx(ERR, "Make sure to use a compatible ELF file and try updating the bootloader if it's old");
|
||||
} else if (pm3_err == PM3_EOUTOFBOUND) {
|
||||
PrintAndLogEx(ERR, _RED_("Error:") " Device rejected the firmware, out of bound");
|
||||
PrintAndLogEx(ERR, "This may be because the firmware is trying to write outside of the flash bounds");
|
||||
PrintAndLogEx(ERR, "Make sure to use a compatible ELF file and try updating the bootloader if it's old");
|
||||
} else {
|
||||
PrintAndLogEx(ERR, _RED_("Error:") " Device rejected the firmware with error code 0x%02x", pm3_err);
|
||||
PrintAndLogEx(ERR, "Make sure to use a compatible ELF file and try updating the bootloader if it's old");
|
||||
}
|
||||
}
|
||||
|
||||
// Send finish write cmd and waiting for response.
|
||||
// The send_buf length is always 512byte(PM3_CMD_DATA_SIZE)
|
||||
static int send_finish_write_cmd(uint32_t address, int magic, uint8_t *send_buf, PacketResponseNG *resp) {
|
||||
// The sending length is always PM3_CMD_DATA_SIZE, which is 512 bytes, because of the limitation of the old frame.
|
||||
const int send_len = PM3_CMD_DATA_SIZE;
|
||||
#if defined ICOPYX
|
||||
// To prevent users from flashing unsupported firmware, icopyx checks arg1 and arg2 in this command.
|
||||
// Therefore, when sending magic to the device, we should not choose a value that happens to be the same as icopyx.
|
||||
// In fact, neither PM3V nor PM5V will be 0xff or 0x1fd, so this should have strong robustness.
|
||||
SendCommandBL(CMD_FINISH_WRITE, address, 0xff, 0x1fd, send_buf, send_len);
|
||||
#else
|
||||
// If it's an older version of the flashher or a flashher specific to icopyx, then arg1 should be 0x00 or 0xff,
|
||||
// not a valid magic value. The client is specifically designed for icopyx.
|
||||
// ---
|
||||
// For devices with older firmware, it doesn't care about arg1,
|
||||
// so OTA can be performed regardless of whether it's a new version of flasher (sending arg1)
|
||||
// or an old version of flasher (arg1 is not a valid magic).
|
||||
// ---
|
||||
// For devices with new firmware, if the sent magic is a valid magic value,
|
||||
// but the firmware cannot work on the device, the device will refuse to write the firmware.
|
||||
// ---
|
||||
// The older client version could always OTA update older devices,
|
||||
// but it couldn't OTA update newer PM5 versions.
|
||||
// This met our needs because the older client version didn't support PM5's ELF files.
|
||||
// ---
|
||||
// The new client version can always continue to OTA update the device version,
|
||||
// and can also OTA update the latest version of PM5.
|
||||
SendCommandBL(CMD_FINISH_WRITE, address, magic, 0, send_buf, send_len);
|
||||
#endif
|
||||
return wait_for_ack(resp);
|
||||
}
|
||||
|
||||
// Write a block of data to flash, padding to the block size if needed. The bootloader will read the entire block,
|
||||
// so we need to make sure to pad it with 0xFF if the data is smaller than the block size.
|
||||
static int write_block(uint32_t address, int magic, uint8_t *data, uint32_t length, flash_dev_t *flash_dev) {
|
||||
// Align length to PM3_CMD_DATA_SIZE or block_size
|
||||
// It is necessary to align with the minimum write unit of the target chip,
|
||||
// otherwise it may cause the device to lose the data or offset errors.
|
||||
uint32_t padded_len = length % MAX(PM3_CMD_DATA_SIZE, flash_dev->block_size);
|
||||
if (padded_len) {
|
||||
padded_len = MAX(PM3_CMD_DATA_SIZE, flash_dev->block_size) - padded_len;
|
||||
}
|
||||
// After aligning PM3_CMD_DATA_SIZE, allocate a new buffer, copy the data, and pad the end with 0xFF.
|
||||
uint32_t aligned_len = length + padded_len;
|
||||
uint8_t *block_buf = malloc(aligned_len);
|
||||
if (block_buf == NULL) {
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
memset(block_buf, 0xFF, aligned_len); // fill 0xFF by aligned length
|
||||
memcpy(block_buf, data, length); // copy data by valid length
|
||||
// Send in packets
|
||||
int ret = PM3_SUCCESS;
|
||||
uint32_t sent = 0;
|
||||
while (sent < aligned_len) {
|
||||
PacketResponseNG resp;
|
||||
ret = send_finish_write_cmd(address, magic, block_buf + sent, &resp);
|
||||
if (ret) {
|
||||
// On new version of flasher, the arg1 is error code of PM3_E*, old version is 0x00, so we can always check it.
|
||||
if (resp.oldarg[1]) { // 0x00 == PM3_SUCCESS
|
||||
flash_write_err_software(resp.oldarg[1]);
|
||||
} else {
|
||||
// If not PM3_E*, maybe some errors of flash write occurred. Or is old version boot.
|
||||
if (flash_dev->chiptype == MAIN_CHIP_TYPE_AT91) {
|
||||
flash_write_err_on_at91(resp.oldarg[0]);
|
||||
} else if (flash_dev->chiptype == MAIN_CHIP_TYPE_AT32) {
|
||||
flash_write_err_on_at32(resp.oldarg[0]);
|
||||
} else {
|
||||
PrintAndLogEx(ERR, "Unknown chip type, cannot decode error information");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
sent += PM3_CMD_DATA_SIZE;
|
||||
}
|
||||
free(block_buf); // remember to free buffer
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Write a file's segments to Flash
|
||||
int flash_write(flash_file_t *ctx) {
|
||||
int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Writing segments for file: %s", ctx->filename);
|
||||
|
||||
@@ -668,7 +888,7 @@ int flash_write(flash_file_t *ctx) {
|
||||
flash_seg_t *seg = &ctx->segments[i];
|
||||
|
||||
uint32_t length = seg->length;
|
||||
uint32_t blocks = (length + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||
uint32_t blocks = (length + flash_dev->block_size - 1) / flash_dev->block_size;
|
||||
uint32_t end = seg->start + length;
|
||||
|
||||
PrintAndLogEx(SUCCESS, " 0x%08x..0x%08x [0x%x / %u blocks]", seg->start, end - 1, length, blocks);
|
||||
@@ -692,12 +912,11 @@ int flash_write(flash_file_t *ctx) {
|
||||
while (length) {
|
||||
|
||||
uint32_t block_size = length;
|
||||
if (block_size > BLOCK_SIZE) {
|
||||
block_size = BLOCK_SIZE;
|
||||
if (block_size > flash_dev->block_size) {
|
||||
block_size = flash_dev->block_size;
|
||||
}
|
||||
|
||||
if (write_block(baddr, data, block_size) < 0) {
|
||||
|
||||
if (write_block(baddr, ctx->ver_info->magic, data, block_size, flash_dev) < 0) {
|
||||
if (blocks > 50) {
|
||||
hadouken_stop();
|
||||
}
|
||||
|
||||
+14
-6
@@ -23,7 +23,6 @@
|
||||
#include "elf.h"
|
||||
|
||||
#define FLASH_MAX_FILES 4
|
||||
#define ONE_KB 1024
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
@@ -36,17 +35,26 @@ typedef struct {
|
||||
uint8_t *elf;
|
||||
Elf32_Phdr_t *phdrs;
|
||||
uint16_t num_phdrs;
|
||||
struct version_information_t *ver_info; // point to an address in *elf, no need to free.
|
||||
int can_write_bl;
|
||||
int num_segs;
|
||||
flash_seg_t *segments;
|
||||
} flash_file_t;
|
||||
|
||||
int flash_load(flash_file_t *ctx, bool force);
|
||||
int flash_prepare(flash_file_t *ctx, int can_write_bl, int flash_size);
|
||||
int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t *max_allowed);
|
||||
typedef struct {
|
||||
uint32_t chiptype; // see: main_chip_type_t
|
||||
uint32_t block_size;
|
||||
uint32_t flash_start;
|
||||
uint32_t flash_end;
|
||||
uint32_t boot_size; // Boot must be at the top of the flash, so flash_start is boot_start.
|
||||
uint32_t boot_end;
|
||||
} flash_dev_t;
|
||||
|
||||
int flash_reboot_bootloader(char *serial_port_name, bool wait_appear);
|
||||
int flash_write(flash_file_t *ctx);
|
||||
int flash_load(flash_file_t *ctx, bool force);
|
||||
int flash_prepare(flash_file_t *ctx, int can_write_bl, flash_dev_t *flash_dev);
|
||||
int flash_start_flashing(int enable_bl_writes, char *serial_port_name, flash_dev_t *flash_dev);
|
||||
int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev);
|
||||
void flash_free(flash_file_t *ctx);
|
||||
int flash_stop_flashing(void);
|
||||
#endif
|
||||
|
||||
@@ -18,10 +18,14 @@
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__ANDROID__)
|
||||
#if !defined(__ANDROID__)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute push(__attribute__((target("avx2"))), apply_to = function)
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC push_options
|
||||
#pragma GCC target("avx2")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define BS_ZERO _mm256_setzero_si256()
|
||||
#define BS_ONES _mm256_set1_epi64x(-1)
|
||||
@@ -121,7 +125,7 @@ static const uint64_t LANE_BITS_256_RAW[8][BS256_WORDS] = {
|
||||
};
|
||||
|
||||
static inline __m256i lane_bits_256(int k) {
|
||||
return _mm256_loadu_si256((const __m256i *)LANE_BITS_256_RAW[k]);
|
||||
return _mm256_loadu_si256((const __m256i *)(const void *)LANE_BITS_256_RAW[k]);
|
||||
}
|
||||
|
||||
static inline void bs_init_state(const __m256i kb[64], __m256i l[8], __m256i r[8], __m256i b[8], __m256i t[16]) {
|
||||
@@ -174,7 +178,7 @@ static inline void bs_init_state(const __m256i kb[64], __m256i l[8], __m256i r[8
|
||||
}
|
||||
|
||||
void prepare_ccnr_bits_bs256(const uint8_t *cc_nr, uint64_t y_bits_bs[96 * BS256_WORDS]) {
|
||||
__m256i *y = (__m256i *)y_bits_bs;
|
||||
__m256i *y = (__m256i *)(void *)y_bits_bs;
|
||||
for (int i = 0; i < 12; i++) {
|
||||
const uint8_t byte = cc_nr[i];
|
||||
for (int bit = 0; bit < 8; bit++) {
|
||||
@@ -184,7 +188,7 @@ void prepare_ccnr_bits_bs256(const uint8_t *cc_nr, uint64_t y_bits_bs[96 * BS256
|
||||
}
|
||||
|
||||
void prepare_target_mac_bs256(const uint8_t target_mac[4], uint64_t target_mac_bs[32 * BS256_WORDS]) {
|
||||
__m256i *tm = (__m256i *)target_mac_bs;
|
||||
__m256i *tm = (__m256i *)(void *)target_mac_bs;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
const uint8_t byte = target_mac[i];
|
||||
for (int bit = 0; bit < 8; bit++) {
|
||||
@@ -194,7 +198,7 @@ void prepare_target_mac_bs256(const uint8_t target_mac[4], uint64_t target_mac_b
|
||||
}
|
||||
|
||||
void build_bitslice_key_256(const uint8_t partial_key[8], uint64_t index_start, uint64_t kb[64 * BS256_WORDS]) {
|
||||
__m256i *k = (__m256i *)kb;
|
||||
__m256i *k = (__m256i *)(void *)kb;
|
||||
|
||||
for (int j = 0; j < 8; j++) {
|
||||
_mm256_storeu_si256(&k[j * 8 + 0], (partial_key[j] & 0x01) ? BS_ONES : BS_ZERO);
|
||||
@@ -226,7 +230,7 @@ void doMAC_brute_match256(const uint64_t y_bits_bs[96 * BS256_WORDS],
|
||||
|
||||
// Load key into local __m256i array for fast access.
|
||||
__m256i k[64];
|
||||
const __m256i *kb_m = (const __m256i *)kb;
|
||||
const __m256i *kb_m = (const __m256i *)(const void *)kb;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
k[i] = _mm256_loadu_si256(&kb_m[i]);
|
||||
}
|
||||
@@ -234,8 +238,8 @@ void doMAC_brute_match256(const uint64_t y_bits_bs[96 * BS256_WORDS],
|
||||
__m256i l[8], r[8], b[8], t[16];
|
||||
bs_init_state(k, l, r, b, t);
|
||||
|
||||
const __m256i *y = (const __m256i *)y_bits_bs;
|
||||
const __m256i *tm = (const __m256i *)target_mac_bs;
|
||||
const __m256i *y = (const __m256i *)(const void *)y_bits_bs;
|
||||
const __m256i *tm = (const __m256i *)(const void *)target_mac_bs;
|
||||
|
||||
for (int i = 0; i < 96; i++) {
|
||||
bs_tick(t, b, l, r, k, _mm256_loadu_si256(&y[i]));
|
||||
@@ -260,12 +264,16 @@ void doMAC_brute_match256(const uint64_t y_bits_bs[96 * BS256_WORDS],
|
||||
}
|
||||
}
|
||||
|
||||
_mm256_storeu_si256((__m256i *)match_out, mac_match);
|
||||
_mm256_storeu_si256((__m256i *)(void *)match_out, mac_match);
|
||||
}
|
||||
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__ANDROID__)
|
||||
#if !defined(__ANDROID__)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool bs_avx2_supported(void) {
|
||||
static int cached = -1;
|
||||
|
||||
@@ -19,10 +19,14 @@
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__ANDROID__)
|
||||
#if !defined(__ANDROID__)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute push(__attribute__((target("avx512f"))), apply_to = function)
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC push_options
|
||||
#pragma GCC target("avx512f")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define BS_ZERO _mm512_setzero_si512()
|
||||
#define BS_ONES _mm512_set1_epi64(-1)
|
||||
@@ -291,9 +295,13 @@ void doMAC_brute_match512(const uint64_t y_bits_bs[96 * BS512_WORDS],
|
||||
_mm512_storeu_si512((void *)match_out, mac_match);
|
||||
}
|
||||
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__ANDROID__)
|
||||
#if !defined(__ANDROID__)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool bs_avx512_supported(void) {
|
||||
static int cached = -1;
|
||||
|
||||
@@ -1404,8 +1404,8 @@ static int DesfireAuthenticateEV1(DesfireContext_t *dctx, DesfireSecureChannel s
|
||||
DesfireCryptoEncDecEx(dctx, DCOMainKey, encRndB, rndlen, RndB, false, false, IV);
|
||||
|
||||
if (g_debugMode > 1) {
|
||||
PrintAndLogEx(DEBUG, "encRndB: %s", sprint_hex(encRndB, 8));
|
||||
PrintAndLogEx(DEBUG, "RndB: %s", sprint_hex(RndB, 8));
|
||||
PrintAndLogEx(DEBUG, "encRndB: %s", sprint_hex(encRndB, rndlen));
|
||||
PrintAndLogEx(DEBUG, "RndB: %s", sprint_hex(RndB, rndlen));
|
||||
}
|
||||
|
||||
// - Rotate RndB by 8 bits
|
||||
@@ -1435,7 +1435,7 @@ static int DesfireAuthenticateEV1(DesfireContext_t *dctx, DesfireSecureChannel s
|
||||
memcpy(tmp + rndlen, rotRndB, rndlen);
|
||||
if (g_debugMode > 1) {
|
||||
PrintAndLogEx(DEBUG, "rotRndB: %s", sprint_hex(rotRndB, rndlen));
|
||||
PrintAndLogEx(DEBUG, "Both : %s", sprint_hex(tmp, 32));
|
||||
PrintAndLogEx(DEBUG, "Both : %s", sprint_hex(tmp, rndlen * 2));
|
||||
}
|
||||
DesfireCryptoEncDecEx(dctx, DCOMainKey, tmp, rndlen * 2, both, true, true, IV);
|
||||
}
|
||||
|
||||
@@ -931,8 +931,8 @@ finish:
|
||||
static int flash_pm3(char *serial_port_name, uint8_t num_files, const char *filenames[FLASH_MAX_FILES], bool can_write_bl, bool force) {
|
||||
|
||||
int ret = PM3_EUNDEF;
|
||||
flash_file_t files[FLASH_MAX_FILES];
|
||||
memset(files, 0, sizeof(files));
|
||||
flash_file_t files[FLASH_MAX_FILES] = {0};
|
||||
flash_dev_t flash_dev = {0};
|
||||
|
||||
if (serial_port_name == NULL) {
|
||||
PrintAndLogEx(ERR, "You must specify a port.\n");
|
||||
@@ -977,8 +977,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, const char *file
|
||||
goto finish2;
|
||||
}
|
||||
|
||||
uint32_t max_allowed = 0;
|
||||
ret = flash_start_flashing(can_write_bl, serial_port_name, &max_allowed);
|
||||
ret = flash_start_flashing(can_write_bl, serial_port_name, &flash_dev);
|
||||
if (ret != PM3_SUCCESS) {
|
||||
goto finish;
|
||||
}
|
||||
@@ -988,7 +987,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, const char *file
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < num_files; ++i) {
|
||||
ret = flash_prepare(&files[i], can_write_bl, max_allowed * ONE_KB);
|
||||
ret = flash_prepare(&files[i], can_write_bl, &flash_dev);
|
||||
if (ret != PM3_SUCCESS) {
|
||||
goto finish;
|
||||
}
|
||||
@@ -998,7 +997,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, const char *file
|
||||
PrintAndLogEx(SUCCESS, _CYAN_("Flashing..."));
|
||||
|
||||
for (int i = 0; i < num_files; i++) {
|
||||
ret = flash_write(&files[i]);
|
||||
ret = flash_write(&files[i], &flash_dev);
|
||||
if (ret != PM3_SUCCESS) {
|
||||
goto finish;
|
||||
}
|
||||
@@ -1074,7 +1073,6 @@ int main(int argc, char *argv[]) {
|
||||
char *port = NULL;
|
||||
uint32_t speed = 0;
|
||||
|
||||
pm3line_init();
|
||||
|
||||
char exec_name[100] = {0};
|
||||
strncpy(exec_name, basename(argv[0]), sizeof(exec_name) - 1);
|
||||
@@ -1523,6 +1521,8 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
*/
|
||||
|
||||
pm3line_init();
|
||||
|
||||
#ifdef HAVE_GUI
|
||||
|
||||
# if defined(_WIN32) || (defined(__MACH__) && defined(__APPLE__))
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
#include <time.h> // Mingw
|
||||
|
||||
#include "ui.h" // PrintAndLog
|
||||
#include "comms.h" // SendCommandNG / WaitForResponseTimeout (set_rgb)
|
||||
#include "pm3_cmd.h" // CMD_PM5_RGB_SET
|
||||
|
||||
#define UTIL_BUFFER_SIZE_SPRINT 8196
|
||||
// global client debug variable
|
||||
@@ -2048,3 +2050,15 @@ int str_copy_without_whitespace(const char *src, char *dst, size_t dst_size, siz
|
||||
*dst_len = out;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
|
||||
struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} PACKED payload = { r, g, b };
|
||||
clearCommandBuffer();
|
||||
SendCommandNG(CMD_PM5_RGB_SET, (uint8_t *)&payload, sizeof(payload));
|
||||
PacketResponseNG resp;
|
||||
WaitForResponseTimeout(CMD_PM5_RGB_SET, &resp, 200);
|
||||
}
|
||||
@@ -231,4 +231,10 @@ void str_unescape_newlines_inplace(char *s);
|
||||
*/
|
||||
int str_copy_without_whitespace(const char *src, char *dst, size_t dst_size, size_t *dst_len);
|
||||
|
||||
/**
|
||||
* @brief Set the antenna RGB LED colour (Proxmark5). No-op reply on devices
|
||||
* without an RGB LED. Sends CMD_PM5_RGB_SET and waits briefly for the ack.
|
||||
*/
|
||||
void set_rgb(uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user