change "mem wipe" / "mem info" to use NG.\nsome more style changes

This commit is contained in:
iceman1001 2025-09-12 22:06:37 +02:00
parent af9567a19e
commit 45d869e87a
6 changed files with 101 additions and 73 deletions

View file

@ -1827,7 +1827,6 @@ static void PacketReceived(PacketCommandNG *packet) {
break;
}
case CMD_HF_MIFAREU_READBL: {
MifareUReadBlock(packet->oldarg[0], packet->oldarg[1], packet->data.asBytes);
break;
}
@ -2916,20 +2915,22 @@ static void PacketReceived(PacketCommandNG *packet) {
LED_B_ON();
uint8_t page = packet->oldarg[0];
uint8_t initialwipe = packet->oldarg[1];
bool isok = false;
if (initialwipe) {
isok = Flash_WipeMemory();
reply_mix(CMD_ACK, isok, 0, 0, 0, 0);
reply_ng(CMD_FLASHMEM_WIPE, (isok) ? PM3_SUCCESS : PM3_EFAILED, NULL, 0);
LED_B_OFF();
break;
}
if (page < spi_flash_pages64k - 1) {
isok = Flash_WipeMemoryPage(page);
// let spiffs check and update its info post flash erase
rdv40_spiffs_check();
}
reply_mix(CMD_ACK, isok, 0, 0, 0, 0);
reply_ng(CMD_FLASHMEM_WIPE, (isok) ? PM3_SUCCESS : PM3_EFAILED, NULL, 0);
LED_B_OFF();
break;
}
@ -2950,13 +2951,15 @@ static void PacketReceived(PacketCommandNG *packet) {
for (size_t i = 0; i < numofbytes; i += PM3_CMD_DATA_SIZE) {
size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE);
Flash_CheckBusy(BUSY_TIMEOUT);
bool isok = Flash_ReadDataCont(startidx + i, mem, len);
if (isok == false)
Dbprintf("reading flash memory failed :: | bytes between %d - %d", i, len);
uint16_t isok = Flash_ReadDataCont(startidx + i, mem, len);
if (isok == false) {
Dbprintf("reading flash memory failed with bytes between %d - %d", i, len);
}
isok = reply_old(CMD_FLASHMEM_DOWNLOADED, i, len, 0, mem, len);
if (isok != 0)
Dbprintf("transfer to client failed :: | bytes between %d - %d", i, len);
if (isok != PM3_SUCCESS) {
Dbprintf("transfer to client failed with bytes between %d - %d", i, len);
}
}
FlashStop();
@ -2968,15 +2971,19 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_FLASHMEM_INFO: {
LED_B_ON();
rdv40_validation_t *info = (rdv40_validation_t *)BigBuf_calloc(sizeof(rdv40_validation_t));
bool isok = Flash_ReadData(FLASH_MEM_SIGNATURE_OFFSET_P(spi_flash_pages64k), info->signature, FLASH_MEM_SIGNATURE_LEN);
// returns 0 when failing
uint16_t isok = Flash_ReadData(FLASH_MEM_SIGNATURE_OFFSET_P(spi_flash_pages64k), info->signature, FLASH_MEM_SIGNATURE_LEN);
if (FlashInit()) {
// re-init since command above calls FlashStop()
if (isok && FlashInit()) {
Flash_UniqueID(info->flashid);
FlashStop();
}
reply_mix(CMD_ACK, isok, 0, 0, info, sizeof(rdv40_validation_t));
reply_ng(CMD_FLASHMEM_INFO, (isok) ? PM3_SUCCESS : PM3_EFLASH, (uint8_t*)info, sizeof(rdv40_validation_t));
BigBuf_free();
LED_B_OFF();
@ -2986,15 +2993,14 @@ static void PacketReceived(PacketCommandNG *packet) {
LED_B_ON();
bool isok = false;
if (FlashInit()) {
isok = true;
bool isok = FlashInit();
if (isok) {
if (g_dbglevel >= DBG_DEBUG) {
Dbprintf(" CMD_FLASHMEM_PAGE64K 0x%02x (%d 64k pages)", spi_flash_pages64k, spi_flash_pages64k);
}
FlashStop();
}
reply_mix(CMD_ACK, isok, 0, 0, &spi_flash_pages64k, sizeof(uint8_t));
reply_ng(CMD_FLASHMEM_PAGES64K, (isok) ? PM3_SUCCESS : PM3_EFLASH, &spi_flash_pages64k, sizeof(uint8_t));
LED_B_OFF();
break;

View file

@ -554,7 +554,7 @@ void MifareValue(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) {
crypto1_deinit(pcs);
if (g_dbglevel >= 2) DbpString("WRITE BLOCK FINISHED");
if (g_dbglevel >= DBG_INFO) DbpString("WRITE BLOCK FINISHED");
reply_mix(CMD_ACK, isOK, 0, 0, 0, 0);
@ -624,7 +624,7 @@ static void MifareUWriteBlockEx(uint8_t arg0, uint8_t arg1, uint8_t *datain, boo
return;
};
if (g_dbglevel >= 2) DbpString("WRITE BLOCK FINISHED");
if (g_dbglevel >= DBG_INFO) DbpString("WRITE BLOCK FINISHED");
if (reply) {
reply_mix(CMD_ACK, 1, 0, 0, 0, 0);
@ -699,7 +699,7 @@ void MifareUWriteBlockCompat(uint8_t arg0, uint8_t arg1, uint8_t *datain) {
return;
};
if (g_dbglevel >= 2) DbpString("WRITE BLOCK FINISHED");
if (g_dbglevel >= DBG_INFO) DbpString("WRITE BLOCK FINISHED");
reply_mix(CMD_ACK, 1, 0, 0, 0, 0);
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
@ -864,7 +864,7 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) {
CHK_TIMEOUT();
if (len != 4) {
if (g_dbglevel >= 2) Dbprintf("AcquireNonces: Auth1 error");
if (g_dbglevel >= DBG_INFO) Dbprintf("AcquireNonces: Auth1 error");
continue;
}
@ -878,7 +878,7 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) {
reply_old(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));
LED_B_OFF();
if (g_dbglevel >= 3) DbpString("AcquireNonces finished");
if (g_dbglevel >= DBG_DEBUG) DbpString("AcquireNonces finished");
if (field_off) {
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
@ -1818,7 +1818,7 @@ static void chkKey_scanA(struct chk_t *c, struct sector_t *k_sector, uint8_t *fo
found[(s * 2)] = 1;
++*foundkeys;
if (g_dbglevel >= 3) Dbprintf("ChkKeys_fast: Scan A found (%d)", c->block);
if (g_dbglevel >= DBG_DEBUG) Dbprintf("ChkKeys_fast: Scan A found (%d)", c->block);
}
}
}
@ -1844,7 +1844,7 @@ static void chkKey_scanB(struct chk_t *c, struct sector_t *k_sector, uint8_t *fo
found[(s * 2) + 1] = 1;
++*foundkeys;
if (g_dbglevel >= 3) Dbprintf("ChkKeys_fast: Scan B found (%d)", c->block);
if (g_dbglevel >= DBG_DEBUG) Dbprintf("ChkKeys_fast: Scan B found (%d)", c->block);
}
}
}
@ -1870,7 +1870,7 @@ static void chkKey_loopBonly(struct chk_t *c, struct sector_t *k_sector, uint8_t
found[(s * 2) + 1] = 1;
++*foundkeys;
if (g_dbglevel >= 3) Dbprintf("ChkKeys_fast: Reading B found (%d)", c->block);
if (g_dbglevel >= DBG_DEBUG) Dbprintf("ChkKeys_fast: Reading B found (%d)", c->block);
// try quick find all B?
// assume: keys comes in groups. Find one B, test against all B.

View file

@ -59,18 +59,17 @@ int rdv4_get_flash_pages64k(uint8_t *pages64k) {
clearCommandBuffer();
SendCommandNG(CMD_FLASHMEM_PAGES64K, NULL, 0);
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_ACK, &resp, 2500) == false) {
if (WaitForResponseTimeout(CMD_FLASHMEM_PAGES64K, &resp, 2500) == false) {
PrintAndLogEx(WARNING, "rdv4_get_flash_pages64k() timeout while waiting for reply");
return PM3_ETIMEOUT;
}
uint8_t isok = resp.oldarg[0] & 0xFF;
if (isok == false) {
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "fail reading from flash (pages 64k)");
return PM3_EFLASH;
}
memcpy(pages64k, (uint8_t *)resp.data.asBytes, sizeof(uint8_t));
*pages64k = resp.data.asBytes[0];
return PM3_SUCCESS;
}
@ -82,18 +81,16 @@ int rdv4_get_signature(rdv40_validation_t *out) {
clearCommandBuffer();
SendCommandNG(CMD_FLASHMEM_INFO, NULL, 0);
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_ACK, &resp, 2500) == false) {
if (WaitForResponseTimeout(CMD_FLASHMEM_INFO, &resp, 2500) == false) {
PrintAndLogEx(WARNING, "timeout while waiting for reply");
return PM3_ETIMEOUT;
}
uint8_t isok = resp.oldarg[0] & 0xFF;
if (isok == false) {
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "fail reading from flashmemory");
return PM3_EFLASH;
}
//rdv40_validation_t mem;
memcpy(out, (rdv40_validation_t *)resp.data.asBytes, sizeof(rdv40_validation_t));
return PM3_SUCCESS;
}
@ -498,7 +495,7 @@ static int CmdFlashMemWipe(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_int0("p", NULL, "<dec>", "0,1,2 page memory"),
arg_int0("p", NULL, "<dec>", "page memory"),
// arg_lit0("i", NULL, "initial total wipe"),
arg_param_end
};
@ -524,21 +521,19 @@ static int CmdFlashMemWipe(const char *Cmd) {
clearCommandBuffer();
SendCommandMIX(CMD_FLASHMEM_WIPE, page, initialwipe, 0, NULL, 0);
PacketResponseNG resp;
if (!WaitForResponseTimeout(CMD_ACK, &resp, 10000)) {
if (WaitForResponseTimeout(CMD_FLASHMEM_WIPE, &resp, 10000) == false) {
PrintAndLogEx(WARNING, "timeout while waiting for reply");
return PM3_ETIMEOUT;
}
const char *msg = "Flash WIPE ";
uint8_t isok = resp.oldarg[0] & 0xFF;
if (isok)
if (resp.status == PM3_SUCCESS) {
PrintAndLogEx(SUCCESS, "%s ( " _GREEN_("ok")" )", msg);
else {
} else {
PrintAndLogEx(FAILED, "%s ( " _RED_("failed") " )", msg);
return PM3_EFLASH;
}
return PM3_SUCCESS;
return resp.status;
}
static int CmdFlashMemInfo(const char *Cmd) {
@ -555,7 +550,7 @@ static int CmdFlashMemInfo(const char *Cmd) {
arg_str0("d", NULL, "<hex>", "flash memory id, 8 hex bytes"),
arg_str0("p", "pem", "<fn>", "key in PEM format"),
arg_lit0("v", "verbose", "verbose output"),
// arg_lit0("w", "write", "write signature to flash memory"),
arg_lit0("w", "write", "write signature to flash memory"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@ -571,8 +566,7 @@ static int CmdFlashMemInfo(const char *Cmd) {
CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)pem_fn, FILE_PATH_SIZE, &pemlen);
bool verbose = arg_get_lit(ctx, 4);
bool shall_write = false;
// shall_write = arg_get_lit(ctx, 5);
bool shall_write = arg_get_lit(ctx, 5);
CLIParserFree(ctx);
if (res) {

View file

@ -304,7 +304,7 @@ int hf14a_getconfig(hf14a_config_t *config) {
SendCommandNG(CMD_HF_ISO14443A_GET_CONFIG, NULL, 0);
PacketResponseNG resp;
if (!WaitForResponseTimeout(CMD_HF_ISO14443A_GET_CONFIG, &resp, 2000)) {
if (WaitForResponseTimeout(CMD_HF_ISO14443A_GET_CONFIG, &resp, 2000) == false) {
PrintAndLogEx(WARNING, "command execution time out");
return PM3_ETIMEOUT;
}

View file

@ -2905,7 +2905,7 @@ static int CmdHF14AMfURdBl(const char *Cmd) {
}
if (ak_len) {
PrintAndLogEx(INFO, "Using %s " _GREEN_("%s"), (ak_len == 16) ? "3des" : "pwd", sprint_hex(authenticationkey, ak_len));
PrintAndLogEx(INFO, "Using %s... " _GREEN_("%s"), (ak_len == 16) ? "3des" : "pwd", sprint_hex_inrow(authenticationkey, ak_len));
}
//Read Block
@ -2931,7 +2931,7 @@ static int CmdHF14AMfURdBl(const char *Cmd) {
PrintAndLogEx(INFO, "-----------------------------");
PrintAndLogEx(INFO, "%02d/0x%02X | %s| %s\n", blockno, blockno, sprint_hex(d, 4), sprint_ascii(d, 4));
} else {
PrintAndLogEx(WARNING, "Failed reading block: ( %02x )", isOK);
PrintAndLogEx(WARNING, "Failed reading block %u ( %02x )", blockno, isOK);
}
} else {
PrintAndLogEx(WARNING, "command execution time out");
@ -3934,6 +3934,8 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
// If no hex key is specified, try default keys
if (ak_len == 0) {
PrintAndLogEx(INFO, "Called with no key, checking default keys...");
isok = try_default_3des_keys(false, &authKeyPtr);
} else {
// try user-supplied

View file

@ -55,15 +55,16 @@ void FlashmemSetSpiBaudrate(uint32_t baudrate) {
// read ID out
bool Flash_ReadID(flash_device_type_t *result, bool read_jedec) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) return false;
if (Flash_CheckBusy(BUSY_TIMEOUT)) {
return false;
}
if (read_jedec) {
// 0x9F JEDEC
FlashSendByte(JEDECID);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id = (FlashSendByte(0xFF) & 0xFF);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id2 = (FlashSendLastByte(0xFF) & 0xFF);
} else {
// 0x90 Manufacture ID / device ID
@ -72,7 +73,7 @@ bool Flash_ReadID(flash_device_type_t *result, bool read_jedec) {
FlashSendByte(0x00);
FlashSendByte(0x00);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id = (FlashSendLastByte(0xFF) & 0xFF);
}
@ -81,10 +82,14 @@ bool Flash_ReadID(flash_device_type_t *result, bool read_jedec) {
uint16_t Flash_ReadData(uint32_t address, uint8_t *out, uint16_t len) {
if (!FlashInit()) return 0;
if (FlashInit() == false) {
return 0;
}
// length should never be zero
if (!len || Flash_CheckBusy(BUSY_TIMEOUT)) return 0;
if ((len == 0) || Flash_CheckBusy(BUSY_TIMEOUT)) {
return 0;
}
uint8_t cmd = (FASTFLASH) ? FASTREAD : READDATA;
@ -99,6 +104,7 @@ uint16_t Flash_ReadData(uint32_t address, uint8_t *out, uint16_t len) {
for (; i < (len - 1); i++) {
out[i] = (FlashSendByte(0xFF) & 0xFF);
}
out[i] = (FlashSendLastByte(0xFF) & 0xFF);
FlashStop();
return len;
@ -114,7 +120,9 @@ void Flash_TransferAdresse(uint32_t address) {
uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len) {
// length should never be zero
if (!len) return 0;
if (len == 0) {
return 0;
}
uint8_t cmd = (FASTFLASH) ? FASTREAD : READDATA;
@ -129,6 +137,7 @@ uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len) {
for (; i < (len - 1); i++) {
out[i] = (FlashSendByte(0xFF) & 0xFF);
}
out[i] = (FlashSendLastByte(0xFF) & 0xFF);
return len;
}
@ -139,8 +148,9 @@ uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len) {
uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) {
// length should never be zero
if (!len)
if (len == 0) {
return 0;
}
// Max 256 bytes write
if (((address & 0xFF) + len) > 256) {
@ -148,8 +158,8 @@ uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) {
return 0;
}
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("Flash_WriteData init fail");
if (FlashInit() == false) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return 0;
}
@ -170,8 +180,9 @@ uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) {
FlashSendByte((address >> 0) & 0xFF);
uint16_t i = 0;
for (; i < (len - 1); i++)
for (; i < (len - 1); i++) {
FlashSendByte(in[i]);
}
FlashSendLastByte(in[i]);
@ -203,8 +214,9 @@ uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len) {
FlashSendByte((address >> 0) & 0xFF);
uint16_t i = 0;
for (; i < (len - 1); i++)
for (; i < (len - 1); i++) {
FlashSendByte(in[i]);
}
FlashSendLastByte(in[i]);
return len;
@ -233,8 +245,9 @@ uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len) {
isok = (res == bytes_in_packet);
if (!isok)
if (isok == false) {
goto out;
}
}
out:
@ -246,10 +259,12 @@ out:
// they should inform the file system of this change
// e.g., rdv40_spiffs_check()
bool Flash_WipeMemoryPage(uint8_t page) {
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("Flash_WriteData init fail");
if (FlashInit() == false) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return false;
}
Flash_ReadStat1();
// Each block is 64Kb. One block erase takes 1s ( 1000ms )
@ -263,10 +278,12 @@ bool Flash_WipeMemoryPage(uint8_t page) {
}
// Wipes flash memory completely, fills with 0xFF
bool Flash_WipeMemory(void) {
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("Flash_WriteData init fail");
if (FlashInit() == false) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return false;
}
Flash_ReadStat1();
// Each block is 64Kb. Four blocks
@ -284,14 +301,16 @@ bool Flash_WipeMemory(void) {
// enable the flash write
void Flash_WriteEnable(void) {
FlashSendLastByte(WRITEENABLE);
if (g_dbglevel > 3) Dbprintf("Flash Write enabled");
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash Write enabled");
}
// erase 4K at one time
// execution time: 0.8ms / 800us
bool Flash_Erase4k(uint8_t block, uint8_t sector) {
if (block > spi_flash_pages64k || sector > MAX_SECTORS) return false;
if (block > spi_flash_pages64k || sector > MAX_SECTORS) {
return false;
}
FlashSendByte(SECTORERASE);
FlashSendByte(block);
@ -326,7 +345,9 @@ bool Flash_Erase32k(uint32_t address) {
// 0x03 00 00 -- 0x 03 FF FF == block 3
bool Flash_Erase64k(uint8_t block) {
if (block > spi_flash_pages64k) return false;
if (block > spi_flash_pages64k) {
return false;
}
FlashSendByte(BLOCK64ERASE);
FlashSendByte(block);
@ -346,7 +367,7 @@ void Flashmem_print_status(void) {
DbpString(_CYAN_("Flash memory"));
Dbprintf(" Baudrate................ " _GREEN_("%d MHz"), FLASHMEM_SPIBAUDRATE / 1000000);
if (!FlashInit()) {
if (FlashInit() == false) {
DbpString(" Init.................... " _RED_("failed"));
return;
}
@ -374,7 +395,7 @@ void Flashmem_print_status(void) {
uid[0], uid[1], uid[2], uid[3],
uid[4], uid[5], uid[6], uid[7]
);
if (g_dbglevel > 3) {
if (g_dbglevel > DBG_DEBUG) {
Dbprintf(" Unique ID (le).......... " _YELLOW_("0x%02X%02X%02X%02X%02X%02X%02X%02X"),
uid[7], uid[6], uid[5], uid[4],
uid[3], uid[2], uid[1], uid[0]
@ -384,6 +405,7 @@ void Flashmem_print_status(void) {
}
bool FlashDetect(void) {
flash_device_type_t flash_data = {0};
bool ret = false;
// read using 0x9F (JEDEC)
@ -392,8 +414,9 @@ bool FlashDetect(void) {
spi_flash_data.jedec_id = (flash_data.device_id << 8) + flash_data.device_id2;
ret = true;
} else {
if (g_dbglevel > 3) Dbprintf("Flash_ReadID failed reading JEDEC (0x9F)");
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_ReadID failed reading JEDEC (0x9F)");
}
// read using 0x90 (Manufacturer / Device ID)
if (Flash_ReadID(&flash_data, false)) {
if (spi_flash_data.manufacturer_id == 0) {
@ -402,8 +425,9 @@ bool FlashDetect(void) {
spi_flash_data.device_id = flash_data.device_id;
ret = true;
} else {
if (g_dbglevel > 3) Dbprintf("Flash_ReadID failed reading Mfr/Dev (0x90)");
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_ReadID failed reading Mfr/Dev (0x90)");
}
// Check JEDEC data is valid, compare the reported device types and then calculate the number of pages
// It is covering the most (known) cases of devices but probably there are vendors with different data
// They will be handled when there is such cases
@ -434,7 +458,7 @@ bool FlashInit(void) {
#ifndef AS_BOOTROM
if (spi_flash_data.manufacturer_id == 0) {
if (!FlashDetect()) {
if (FlashDetect() == false) {
return false;
}
}
@ -446,7 +470,9 @@ bool FlashInit(void) {
// read unique id for chip.
void Flash_UniqueID(uint8_t *uid) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) return;
if (Flash_CheckBusy(BUSY_TIMEOUT)) {
return;
}
// reading unique serial number
FlashSendByte(UNIQUE_ID);
@ -483,7 +509,7 @@ void FlashStop(void) {
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS;
#ifndef AS_BOOTROM
if (g_dbglevel > 3) Dbprintf("FlashStop");
if (g_dbglevel > DBG_DEBUG) Dbprintf("FlashStop");
#endif // AS_BOOTROM
StopTicks();