diff --git a/CHANGELOG.md b/CHANGELOG.md index abe7c55b7..38458b852 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hf goodisplay load` - write images to Goodisplay NFC e-paper tags GDEY0154D67, GDEY0213B74, GDEY029T94 and GDEY037T03, protocol from the Flipper Zero NFC Eink app (@iceman1001) +- Changed `hf 14a info` - hints at `hf waveshare` / `hf goodisplay` for e-paper tags (@iceman1001) - Fixed `hf waveshare load` - 2.7" height is 264, and last pixels of non byte-aligned rows (2.13") were misplaced (@iceman1001) - Preserve jobserver in Makefile builds to fix ProxSpace build speeds (@tpglitch) - Added `hf gp info`, `hf gp crs` and `hf gp aids` - read GlobalPlatform card info, list CRS registered applets and find applet AIDs, all without keys (@iceman1001) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 069ebefc4..11345ba18 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -676,6 +676,7 @@ if (NOT SKIPGD EQUAL 1 AND GD_FOUND) set(ADDITIONAL_LNKDIRS ${GD_LIBRARY_DIRS} ${ADDITIONAL_LNKDIRS}) set(TARGET_SOURCES ${PM3_ROOT}/client/src/imgutils.c + ${PM3_ROOT}/client/src/cmdhfgoodisplay.c ${PM3_ROOT}/client/src/cmdhfwaveshare.c ${TARGET_SOURCES}) add_definitions("-DHAVE_GD") diff --git a/client/Makefile b/client/Makefile index 4758d8664..b013bb9a2 100644 --- a/client/Makefile +++ b/client/Makefile @@ -970,6 +970,7 @@ SRCS += bucketsort.c \ ifeq ($(GD_FOUND),1) # electronic shelf labels SRCS += imgutils.c \ + cmdhfgoodisplay.c \ cmdhfwaveshare.c endif diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index b53c58801..6bcf9a444 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -554,6 +554,7 @@ if (NOT SKIPGD EQUAL 1 AND GD_FOUND) set(ADDITIONAL_LNKDIRS ${GD_LIBRARY_DIRS} ${ADDITIONAL_LNKDIRS}) set(TARGET_SOURCES ${PM3_ROOT}/client/src/imgutils.c + ${PM3_ROOT}/client/src/cmdhfgoodisplay.c ${PM3_ROOT}/client/src/cmdhfwaveshare.c ${TARGET_SOURCES}) add_definitions("-DHAVE_GD") diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index 396213847..d999f6ca4 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -60,6 +60,7 @@ #include "cmdhftopaz.h" // TOPAZ #include "cmdhfvas.h" // Value added services #include "cmdhfwaveshare.h" // Waveshare +#include "cmdhfgoodisplay.h" // Goodisplay #include "cmdhfxerox.h" // Xerox #include "cmdtrace.h" // trace list #include "ui.h" @@ -646,6 +647,9 @@ static command_t CommandTable[] = { {"fmcos", CmdHFFmcos, AlwaysAvailable, "{ FMCOS CPU cards... }"}, {"fudan", CmdHFFudan, AlwaysAvailable, "{ Fudan RFIDs... }"}, {"gallagher", CmdHFGallagher, AlwaysAvailable, "{ Gallagher DESFire RFIDs... }"}, +#ifdef HAVE_GD + {"goodisplay", CmdHFGoodisplay, AlwaysAvailable, "{ Goodisplay NFC ePaper... }"}, +#endif {"gp", CmdHFGP, AlwaysAvailable, "{ GlobalPlatform secure elements... }"}, {"gst", CmdHFGST, AlwaysAvailable, "{ Google Smart Tap passes... }"}, {"secc", CmdHFHIDConfig, AlwaysAvailable, "{ iClass SE Config Card Emulator... }"}, diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index 6df5b9daf..0a6340828 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -3146,6 +3146,8 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { bool isISO18092 = false; bool isNTAG424 = false; bool isSEOS = false; + bool isWaveshare = false; + bool isGoodisplay = false; int nxptype = MTNONE; if (card.uidlen <= 4) { @@ -3237,8 +3239,9 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { break; } case 0x46: { - if (memcmp(card.uid, "FSTN10m", 7) == 0) { + if ((memcmp(card.uid, "FSTN10m", 7) == 0) || (memcmp(card.uid, "FSTN11m", 7) == 0)) { isMifareClassic = false; + isWaveshare = true; printTag("Waveshare NFC-Powered e-Paper 1.54\" (please disregard MANUFACTURER mapping above)"); } break; @@ -3246,6 +3249,7 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { case 0x57: { if (memcmp(card.uid, "WSDZ10m", 7) == 0) { isMifareClassic = false; + isWaveshare = true; printTag("Waveshare NFC-Powered e-Paper (please disregard MANUFACTURER mapping above)"); } break; @@ -3353,6 +3357,13 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { isMifareDESFire = false; } + // Goodisplay NFC e-paper (FM1280), historical bytes 90 00 + if ((card.sak == 0x28) && (card.uidlen == 4) && (card.ats_len == 13) && + (memcmp(card.ats, "\x0B\x78\x33\xA0\x02\x90", 6) == 0) && + (memcmp(card.ats + 6, card.uid, 4) == 0) && (card.ats[10] == 0x00)) { + isGoodisplay = true; + } + PrintAndLogEx(SUCCESS, "ATS: " _YELLOW_("%s")"[ %02X %02X ]", sprint_hex(card.ats, card.ats_len - 2), card.ats[card.ats_len - 2], card.ats[card.ats_len - 1]); PrintAndLogEx(INFO, " " _YELLOW_("%02X") "............... TL length is " _GREEN_("%d") " bytes", card.ats[0], card.ats[0]); @@ -3655,6 +3666,20 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf ntag424 info") "`"); } +#ifdef HAVE_GD + if (isWaveshare) { + PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf waveshare load -h") "`"); + } + + if (isGoodisplay) { + PrintAndLogEx(HINT, "Hint: Possible Goodisplay NFC e-paper, try `" _YELLOW_("hf goodisplay load -h") "`"); + } +#else + // e-paper commands need libgd + (void)isWaveshare; + (void)isGoodisplay; +#endif + if (isMifareClassic || isMifareMini) { if (((isMagic & MAGIC_FLAG_GEN_1A) == MAGIC_FLAG_GEN_1A) || ((isMagic & MAGIC_FLAG_GEN_1B) == MAGIC_FLAG_GEN_1B)) { PrintAndLogEx(HINT, "Hint: Use `" _YELLOW_("hf mf c*") "` magic commands"); diff --git a/client/src/cmdhfgoodisplay.c b/client/src/cmdhfgoodisplay.c new file mode 100644 index 000000000..dc6b11df5 --- /dev/null +++ b/client/src/cmdhfgoodisplay.c @@ -0,0 +1,449 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Goodisplay NFC e-paper commands +//----------------------------------------------------------------------------- +// Protocol from the Flipper Zero NFC Eink app (GPLv3) by RebornedBrain +// https://github.com/RebornedBrain/nfc_eink +// Tags use a Fudan FM1280, ISO14443-4 with proprietary CLA F0 APDUs. + +#include "cmdhfgoodisplay.h" + +#include +#include +#include "comms.h" +#include "cmdparser.h" +#include "cmdhf14a.h" +#include "ui.h" +#include "util.h" +#include "fileutils.h" +#include "util_posix.h" // msleep +#include "commonutil.h" // ARRAYLEN, Uint2byteToMemBe +#include "cliparser.h" +#include "imgutils.h" + +#define GD_BLOCK_SIZE 250 // image bytes per F0 D2 APDU +#define GD_CHAIN_SIZE 248 // image bytes in the first frame of a chain +#define GD_CHANNEL_BW 0x20 +#define GD_UPDATE_POLLS 20 +#define GD_UPDATE_POLL_MS 150 + +typedef struct { + const char *desc; + uint8_t size_code; + uint16_t width; // source pixels, bytes per row = width / 8 + uint16_t height; // gate lines, one row each +} gd_model_t; + +// panel native portrait orientation +static const gd_model_t gd_models[] = { + {"GDEY0154D67 1.54 inch", 0x12, 200, 200}, + {"GDEY0213B74 2.13 inch", 0x00, 122, 250}, + {"GDEY029T94 2.9 inch", 0x01, 128, 296}, + {"GDEY037T03 3.7 inch", 0x51, 240, 416}, +}; + +// F0 DB 00 00 config, SSD16xx init sequence captured from the NFC-D1 app +static const uint8_t gd_config[] = { + 0xf0, 0xdb, 0x00, 0x00, 0x67, 0xa0, 0x06, 0x00, 0x20, 0x00, 0x80, 0x01, 0x28, 0xa4, 0x01, 0x0c, + 0xa5, 0x02, 0x00, 0x0a, 0xa4, 0x01, 0x08, 0xa5, 0x02, 0x00, 0x0a, 0xa4, 0x01, 0x0c, 0xa5, 0x02, + 0x00, 0x0a, 0xa4, 0x01, 0x02, 0xa1, 0x01, 0x12, 0xa4, 0x01, 0x02, 0xa1, 0x04, 0x01, 0x27, 0x01, + 0x01, 0xa1, 0x02, 0x11, 0x01, 0xa1, 0x03, 0x44, 0x00, 0x0f, 0xa1, 0x05, 0x45, 0x27, 0x01, 0x00, + 0x00, 0xa1, 0x02, 0x3c, 0x05, 0xa1, 0x03, 0x21, 0x00, 0x80, 0xa1, 0x02, 0x18, 0x80, 0xa1, 0x02, + 0x4e, 0x00, 0xa1, 0x03, 0x4f, 0x27, 0x01, 0xa3, 0x01, 0x24, 0xa2, 0x02, 0x22, 0xf7, 0xa2, 0x01, + 0x20, 0xa4, 0x01, 0x02, 0xa2, 0x02, 0x10, 0x01, 0xa5, 0x02, 0x00, 0x0a +}; + +// offsets of the A0 size record inside gd_config +#define GD_CFG_SIZE_CODE 7 +#define GD_CFG_CHANNEL 8 +#define GD_CFG_SOURCES 9 +#define GD_CFG_GATES 11 + +// next ISO14443-4 block number, mirrors the firmware's +static uint8_t gd_blocknum = 0; + +static int CmdHelp(const char *Cmd); + +static int gd_select(void) { + iso14a_card_select_t card; + int res = SelectCard14443A_4(false, false, &card); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "No ISO14443-4 tag found"); + return res; + } + gd_blocknum = 0; + return PM3_SUCCESS; +} + +// raw single byte PCB frame (S-block / R-block), any reply accepted +static int gd_send_pcb(uint8_t pcb) { + clearCommandBuffer(); + SendIso14aReader(ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, &pcb, 1); + uint16_t len = 0; + if (WaitForIso14aReply(NULL, 1000, &len, NULL) == false || len == 0) { + return PM3_ECARDEXCHANGE; + } + return PM3_SUCCESS; +} + +// S(DESELECT), field off for 100 ms, reselect +static int gd_reset(void) { + int res = gd_send_pcb(0xC2); + if (res != PM3_SUCCESS) { + PrintAndLogEx(DEBUG, "DESELECT failed"); + return res; + } + DropField(); + msleep(100); + return gd_select(); +} + +// R(NAK) presence check +static int gd_presence_check(void) { + return gd_send_pcb(0xB2 | gd_blocknum); +} + +// one I-block, rx gets the reply without PCB and CRC +static int gd_exchange(const uint8_t *tx, uint16_t txlen, bool chaining, uint8_t *rx, uint16_t rxmax, uint16_t *rxlen, uint8_t *pcb) { + uint32_t flags = ISO14A_APDU | ISO14A_NO_DISCONNECT; + if (chaining) { + flags |= ISO14A_SEND_CHAINING; + } + + clearCommandBuffer(); + SendIso14aReader(flags, tx, txlen); + + PacketResponseNG resp; + uint16_t len = 0; + uint8_t res = 0; + if (WaitForIso14aReply(&resp, 5000, &len, &res) == false) { + PrintAndLogEx(DEBUG, "APDU reply timeout"); + return PM3_ETIMEOUT; + } + + // len counts the CRC + if (len < 2 || len - 2 > rxmax) { + PrintAndLogEx(DEBUG, "APDU bad reply length %u", len); + return PM3_EAPDU_FAIL; + } + + // I-block or R(ACK) with the current block number toggles it + if ((((res & 0xC0) == 0x00) || ((res & 0xD0) == 0x80)) && ((res & 0x01) == gd_blocknum)) { + gd_blocknum ^= 1; + } + + *rxlen = len - 2; + memcpy(rx, resp.data.asBytes, *rxlen); + if (pcb) { + *pcb = res; + } + return PM3_SUCCESS; +} + +static bool gd_reply_is(const uint8_t *rx, uint16_t rxlen, const uint8_t *exp, uint16_t explen) { + return (rxlen == explen) && (memcmp(rx, exp, explen) == 0); +} + +// APDU expecting exactly `exp` back +static int gd_apdu(const uint8_t *tx, uint16_t txlen, const uint8_t *exp, uint16_t explen) { + uint8_t rx[32] = {0}; + uint16_t rxlen = 0; + int res = gd_exchange(tx, txlen, false, rx, sizeof(rx), &rxlen, NULL); + if (res != PM3_SUCCESS) { + return res; + } + if (gd_reply_is(rx, rxlen, exp, explen) == false) { + PrintAndLogEx(DEBUG, "APDU %s", sprint_hex(tx, MIN(txlen, 8))); + PrintAndLogEx(DEBUG, "unexpected reply %s", sprint_hex(rx, rxlen)); + return PM3_EAPDU_FAIL; + } + return PM3_SUCCESS; +} + +static const uint8_t SW_OK[] = {0x90, 0x00}; + +// NFC Forum Type 4 probe the app does before writing +static int gd_probe(void) { + const uint8_t sel_app[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01, 0x00}; + const uint8_t sel_cc[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x03}; + const uint8_t read_cc[] = {0x00, 0xB0, 0x00, 0x00, 0x0F}; + const uint8_t sel_ndef[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x04}; + const uint8_t read_ndef[] = {0x00, 0xB0, 0x00, 0x00, 0x02}; + const uint8_t ndef_empty[] = {0x00, 0x00, 0x90, 0x00}; + + int res = gd_apdu(sel_app, sizeof(sel_app), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + + res = gd_apdu(sel_cc, sizeof(sel_cc), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + + // CCLEN must be 0x000F + uint8_t rx[32] = {0}; + uint16_t rxlen = 0; + res = gd_exchange(read_cc, sizeof(read_cc), false, rx, sizeof(rx), &rxlen, NULL); + if (res != PM3_SUCCESS) { + return res; + } + if (rxlen < 2 || rx[0] != 0x00 || rx[1] != 0x0F) { + PrintAndLogEx(DEBUG, "unexpected CC %s", sprint_hex(rx, rxlen)); + return PM3_EAPDU_FAIL; + } + + res = gd_apdu(sel_ndef, sizeof(sel_ndef), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + return gd_apdu(read_ndef, sizeof(read_ndef), ndef_empty, sizeof(ndef_empty)); +} + +static int gd_send_config(const gd_model_t *model) { + const uint8_t cfg_start[] = {0xF0, 0xDB, 0x02, 0x00, 0x00}; + int res = gd_apdu(cfg_start, sizeof(cfg_start), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + + uint8_t cfg[sizeof(gd_config)]; + memcpy(cfg, gd_config, sizeof(cfg)); + cfg[GD_CFG_SIZE_CODE] = model->size_code; + cfg[GD_CFG_CHANNEL] = GD_CHANNEL_BW; + Uint2byteToMemBe(cfg + GD_CFG_SOURCES, model->width); + Uint2byteToMemBe(cfg + GD_CFG_GATES, model->height); + res = gd_apdu(cfg, sizeof(cfg), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + + const uint8_t cfg_end[] = {0xF0, 0xDA, 0x00, 0x00, 0x03, 0xF0, model->size_code, GD_CHANNEL_BW}; + res = gd_apdu(cfg_end, sizeof(cfg_end), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + return res; + } + + return gd_presence_check(); +} + +// F0 D2 FA <250 bytes>, sent as a two frame chain +static int gd_send_block(uint16_t blocknr, const uint8_t *data) { + uint8_t frame[5 + GD_CHAIN_SIZE] = {0xF0, 0xD2, 0x00, 0x00, GD_BLOCK_SIZE}; + Uint2byteToMemBe(frame + 2, blocknr); + memcpy(frame + 5, data, GD_CHAIN_SIZE); + + uint8_t rx[32] = {0}; + uint16_t rxlen = 0; + uint8_t pcb = 0; + int res = gd_exchange(frame, sizeof(frame), true, rx, sizeof(rx), &rxlen, &pcb); + if (res != PM3_SUCCESS) { + return res; + } + + // expect R(ACK) + if ((pcb & 0xF2) != 0xA2) { + PrintAndLogEx(DEBUG, "block %u, chaining not acknowledged, PCB %02X", blocknr, pcb); + return PM3_EAPDU_FAIL; + } + + return gd_exchange(data + GD_CHAIN_SIZE, GD_BLOCK_SIZE - GD_CHAIN_SIZE, false, rx, sizeof(rx), &rxlen, NULL); +} + +static int gd_write_image(const gd_model_t *model, const uint8_t *plane) { + + uint32_t plane_len = ((model->width + 7) / 8) * model->height; + uint16_t blocks = (plane_len + GD_BLOCK_SIZE - 1) / GD_BLOCK_SIZE; + + // last block padded with white + uint8_t *data = malloc(blocks * GD_BLOCK_SIZE); + if (data == NULL) { + return PM3_EMALLOC; + } + memset(data, 0xFF, blocks * GD_BLOCK_SIZE); + memcpy(data, plane, plane_len); + + // select, deselect / reselect twice around the probe, as the app does + int res = gd_select(); + if (res != PM3_SUCCESS) { + goto out; + } + + res = gd_reset(); + if (res != PM3_SUCCESS) { + goto out; + } + + res = gd_probe(); + if (res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Card doesn't look like a Goodisplay tag"); + goto out; + } + + for (uint8_t i = 0; i < 2; i++) { + res = gd_reset(); + if (res != PM3_SUCCESS) { + goto out; + } + } + + PrintAndLogEx(DEBUG, "Sending config"); + res = gd_send_config(model); + if (res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Config rejected"); + goto out; + } + + for (uint16_t i = 0; i < blocks; i++) { + res = gd_send_block(i, data + (i * GD_BLOCK_SIZE)); + if (res != PM3_SUCCESS) { + PROMPT_CLEARLINE; + PrintAndLogEx(WARNING, "Transmission failed at block %u, please try again.", i); + goto out; + } + PrintAndLogEx(INPLACE, "Progress: %d %%", (i + 1) * 100 / blocks); + } + PROMPT_CLEARLINE; + + // tag answers with WTX while refreshing, handled by the device + PrintAndLogEx(INFO, "E-paper Reflashing, Waiting"); + const uint8_t apply[] = {0xF0, 0xD4, 0x05, 0x80, 0x00}; + res = gd_apdu(apply, sizeof(apply), SW_OK, sizeof(SW_OK)); + if (res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Update failed, please try again."); + goto out; + } + + // keep the tag powered until the panel is done + for (uint8_t i = 0; i < GD_UPDATE_POLLS; i++) { + res = gd_presence_check(); + if (res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Tag lost during refresh"); + goto out; + } + msleep(GD_UPDATE_POLL_MS); + } + + PrintAndLogEx(SUCCESS, "E-paper Update OK"); + +out: + DropField(); + free(data); + return res; +} + +static int CmdHFGoodisplayLoad(const char *Cmd) { + + char desc[600] = {0}; + for (uint8_t i = 0; i < ARRAYLEN(gd_models); i++) { + snprintf(desc + strlen(desc), + sizeof(desc) - strlen(desc), + "hf goodisplay load -f myfile -m %u -> %s ( %u, %u )\n", + i, + gd_models[i].desc, + gd_models[i].width, + gd_models[i].height + ); + } + + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf goodisplay load", + "Load image file to Goodisplay NFC ePaper (NFC-D1 tags).\n" + "Image is cropped to the panel's native portrait size, black/white only.", + desc + ); + + char modeldesc[40]; + snprintf(modeldesc, sizeof(modeldesc), "model number [0 - %d] of your tag", (int)ARRAYLEN(gd_models) - 1); + + void *argtable[] = { + arg_param_begin, + arg_int1("m", NULL, "", modeldesc), + arg_str1("f", "file", "", "specify image to upload to tag"), + arg_str0("s", "save", "", "save paletized version in file"), + arg_param_end + }; + + CLIExecWithReturn(ctx, Cmd, argtable, false); + + int model_nr = arg_get_int_def(ctx, 1, -1); + + int infilelen, outfilelen; + char infile[FILE_PATH_SIZE]; + char outfile[FILE_PATH_SIZE]; + CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)infile, FILE_PATH_SIZE, &infilelen); + CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)outfile, FILE_PATH_SIZE, &outfilelen); + CLIParserFree(ctx); + + if (infilelen < 1) { + PrintAndLogEx(WARNING, "Missing input file"); + return PM3_EINVARG; + } + if (model_nr < 0 || model_nr >= (int)ARRAYLEN(gd_models)) { + PrintAndLogEx(WARNING, "Unknown model"); + return PM3_EINVARG; + } + if (!g_session.pm3_present && !outfilelen) { + PrintAndLogEx(WARNING, "Offline - can only perform image conversion"); + return PM3_ENOTTY; + } + + const gd_model_t *model = &gd_models[model_nr]; + + int pal[2]; + pal[0] = gdTrueColorAlpha(0xFF, 0xFF, 0xFF, 0); // White + pal[1] = gdTrueColorAlpha(0x00, 0x00, 0x00, 0); // Black + + gdImagePtr pal_img = NULL; + int res = img_load_dithered(infile, model->width, model->height, pal, ARRAYLEN(pal), &pal_img); + if (res != PM3_SUCCESS) { + return res; + } + + if (outfilelen) { + res = img_save(pal_img, outfile); + gdImageDestroy(pal_img); + return res; + } + + uint8_t *plane = img_to_bitplane(pal_img, 1); + gdImageDestroy(pal_img); + if (plane == NULL) { + PrintAndLogEx(WARNING, "Could not convert image to bit plane"); + return PM3_EMALLOC; + } + + res = gd_write_image(model, plane); + free(plane); + return res; +} + +static command_t CommandTable[] = { + {"help", CmdHelp, AlwaysAvailable, "This help"}, + {"load", CmdHFGoodisplayLoad, AlwaysAvailable, "Load image file to Goodisplay NFC ePaper"}, + {NULL, NULL, NULL, NULL} +}; + +static int CmdHelp(const char *Cmd) { + (void)Cmd; // Cmd is not used so far + CmdsHelp(CommandTable); + return PM3_SUCCESS; +} + +int CmdHFGoodisplay(const char *Cmd) { + clearCommandBuffer(); + return CmdsParse(CommandTable, Cmd); +} diff --git a/client/src/cmdhfgoodisplay.h b/client/src/cmdhfgoodisplay.h new file mode 100644 index 000000000..57d480b01 --- /dev/null +++ b/client/src/cmdhfgoodisplay.h @@ -0,0 +1,26 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Goodisplay NFC e-paper commands +//----------------------------------------------------------------------------- + +#ifndef CMDHFGOODISPLAY_H__ +#define CMDHFGOODISPLAY_H__ + +#include "common.h" + +int CmdHFGoodisplay(const char *Cmd); + +#endif diff --git a/client/src/cmdhfwaveshare.c b/client/src/cmdhfwaveshare.c index c57dec8c7..6e1e0f416 100644 --- a/client/src/cmdhfwaveshare.c +++ b/client/src/cmdhfwaveshare.c @@ -82,37 +82,6 @@ static model_t models[] = { static int CmdHelp(const char *Cmd); -static uint8_t *map8to1(gdImagePtr img, int color) { - // Calculate width rounding up - uint16_t width8 = (gdImageSX(img) + 7) / 8; - - uint8_t *colormap8 = calloc(width8 * gdImageSY(img), sizeof(uint8_t)); - if (!colormap8) { - return NULL; - } - - uint8_t data = 0; - uint8_t count = 0; - for (uint16_t Y = 0; Y < gdImageSY(img); Y++) { - for (uint16_t X = 0; X < gdImageSX(img); X++) { - if (gdImageGetPixel(img, X, Y) == color) { - data |= 1; - } - count += 1; - if ((count >= 8) || (X == gdImageSX(img) - 1)) { - // MSB-align a partial last byte - data = (data << (8 - count)) & 0xFF; - colormap8[X / 8 + Y * width8] = (~data) & 0xFF; - count = 0; - data = 0; - } - data = (data << 1) & 0xFF; - } - } - - return colormap8; -} - static void read_black(uint32_t i, uint8_t *l, uint8_t model_nr, const uint8_t *black) { for (uint8_t j = 0; j < models[model_nr].len; j++) { l[3 + j] = black[i * models[model_nr].len + j]; @@ -630,20 +599,6 @@ static int CmdHF14AWSLoad(const char *Cmd) { bool model_has_red = model_nr == M1in54B || model_nr == M2in13B; - gdImagePtr rgb_img = gdImageCreateFromFile(infile); - if (!rgb_img) { - PrintAndLogEx(WARNING, "Could not load image from " _YELLOW_("%s"), infile); - return PM3_EFILE; - } - - gdImagePtr scaled_img = img_crop_to_fit(rgb_img, models[model_nr].width, models[model_nr].height); - if (scaled_img == NULL) { - PrintAndLogEx(WARNING, "Failed to scale input image"); - gdImageDestroy(rgb_img); - return PM3_EFILE; - } - gdImageDestroy(rgb_img); - int pal_len = 2; int pal[3]; pal[0] = gdTrueColorAlpha(0xFF, 0xFF, 0xFF, 0); // White @@ -653,27 +608,19 @@ static int CmdHF14AWSLoad(const char *Cmd) { pal[2] = gdTrueColorAlpha(0xFF, 0x00, 0x00, 0); // Red } - gdImagePtr pal_img = img_palettize(scaled_img, pal, pal_len); - gdImageDestroy(scaled_img); - - if (!pal_img) { - PrintAndLogEx(WARNING, "Could not convert image"); - return PM3_EMALLOC; + gdImagePtr pal_img = NULL; + int res = img_load_dithered(infile, models[model_nr].width, models[model_nr].height, pal, pal_len, &pal_img); + if (res != PM3_SUCCESS) { + return res; } if (outfilelen) { - if (gdImageFile(pal_img, outfile)) { - PrintAndLogEx(INFO, "Save converted image to " _YELLOW_("%s"), outfile); - gdImageDestroy(pal_img); - return PM3_SUCCESS; - } else { - PrintAndLogEx(WARNING, "Could not save converted image %s", outfile); - gdImageDestroy(pal_img); - return PM3_EFILE; - } + res = img_save(pal_img, outfile); + gdImageDestroy(pal_img); + return res; } - uint8_t *black_plane = map8to1(pal_img, 1); + uint8_t *black_plane = img_to_bitplane(pal_img, 1); if (!black_plane) { PrintAndLogEx(WARNING, "Could not convert image to bit plane"); gdImageDestroy(pal_img); @@ -682,7 +629,7 @@ static int CmdHF14AWSLoad(const char *Cmd) { uint8_t *red_plane = NULL; if (model_has_red) { - red_plane = map8to1(pal_img, 2); + red_plane = img_to_bitplane(pal_img, 2); if (!red_plane) { PrintAndLogEx(WARNING, "Could not convert image to bit plane"); free(black_plane); @@ -692,7 +639,7 @@ static int CmdHF14AWSLoad(const char *Cmd) { } gdImageDestroy(pal_img); - int res = start_drawing(model_nr, black_plane, red_plane); + res = start_drawing(model_nr, black_plane, red_plane); free(black_plane); free(red_plane); return res; diff --git a/client/src/imgutils.c b/client/src/imgutils.c index c6c4d0e96..2a89ba5f8 100644 --- a/client/src/imgutils.c +++ b/client/src/imgutils.c @@ -1,6 +1,10 @@ #include +#include +#include #include "imgutils.h" +#include "ui.h" +#include "pm3_cmd.h" struct ycbcr_t { int y; @@ -204,3 +208,58 @@ gdImagePtr img_crop_to_fit(gdImagePtr orig, int width, int height) { return res; } + +int img_load_dithered(const char *filename, int width, int height, int *palette, int palette_size, gdImagePtr *out) { + + gdImagePtr rgb_img = gdImageCreateFromFile(filename); + if (rgb_img == NULL) { + PrintAndLogEx(WARNING, "Could not load image from " _YELLOW_("%s"), filename); + return PM3_EFILE; + } + + gdImagePtr scaled_img = img_crop_to_fit(rgb_img, width, height); + gdImageDestroy(rgb_img); + if (scaled_img == NULL) { + PrintAndLogEx(WARNING, "Failed to scale input image"); + return PM3_EFILE; + } + + gdImagePtr pal_img = img_palettize(scaled_img, palette, palette_size); + gdImageDestroy(scaled_img); + if (pal_img == NULL) { + PrintAndLogEx(WARNING, "Could not convert image"); + return PM3_EMALLOC; + } + + *out = pal_img; + return PM3_SUCCESS; +} + +int img_save(gdImagePtr img, const char *filename) { + if (gdImageFile(img, filename)) { + PrintAndLogEx(INFO, "Save converted image to " _YELLOW_("%s"), filename); + return PM3_SUCCESS; + } + PrintAndLogEx(WARNING, "Could not save converted image %s", filename); + return PM3_EFILE; +} + +uint8_t *img_to_bitplane(gdImagePtr img, int color) { + int width8 = (gdImageSX(img) + 7) / 8; + + // start all white, clear the bits of matching pixels + uint8_t *plane = malloc(width8 * gdImageSY(img)); + if (plane == NULL) { + return NULL; + } + memset(plane, 0xFF, width8 * gdImageSY(img)); + + for (int y = 0; y < gdImageSY(img); y++) { + for (int x = 0; x < gdImageSX(img); x++) { + if (gdImageGetPixel(img, x, y) == color) { + plane[y * width8 + x / 8] &= ~(0x80 >> (x % 8)); + } + } + } + return plane; +} diff --git a/client/src/imgutils.h b/client/src/imgutils.h index d38f02d04..4c526b6c3 100644 --- a/client/src/imgutils.h +++ b/client/src/imgutils.h @@ -18,6 +18,7 @@ #ifndef IMGUTILS_H__ #define IMGUTILS_H__ +#include #include /* @@ -36,4 +37,21 @@ gdImagePtr img_palettize(gdImagePtr rgb, int *palette, int palette_size); */ gdImagePtr img_crop_to_fit(gdImagePtr orig, int width, int height); +/* + * Loads an image file, crops it to width x height and dithers it to the palette. + * Returns a PM3 status code, *out is set on success only. + */ +int img_load_dithered(const char *filename, int width, int height, int *palette, int palette_size, gdImagePtr *out); + +/* + * Saves an image, format chosen by file extension. Returns a PM3 status code. + */ +int img_save(gdImagePtr img, const char *filename); + +/* + * Packs one palette index into a 1bpp plane, MSB first, rows padded to whole bytes. + * Pixels of that index are 0, all others 1. Caller frees. + */ +uint8_t *img_to_bitplane(gdImagePtr img, int color); + #endif diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 3280d6e10..c93d65581 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -715,6 +715,7 @@ while true; do if ! CheckExecute "hf saflok test" "$CLIENTBIN -c 'hf saflok test'" "Tests \( ok"; then break; fi if ! CheckExecute "mad test" "$CLIENTBIN -c 'mad test'" "Tests \( ok"; then break; fi if ! CheckExecute "hf waveshare load" "$CLIENTBIN -c 'hf waveshare load -m 6 -f tools/lena.bmp -s dither.bmp' && echo '34ff55fe7257876acf30dae00eb0e439 dither.bmp' | md5sum -c -" "dither.bmp: OK"; then break; fi + if ! CheckExecute "hf goodisplay load" "$CLIENTBIN -c 'hf goodisplay load -m 2 -f tools/lena.bmp -s gd_dither.bmp' && echo '3738750a8ce7f89913c7af012acfc1fa gd_dither.bmp' | md5sum -c -" "gd_dither.bmp: OK"; then break; fi fi echo -e "\n------------------------------------------------------------" echo -e "Tests [ ${C_GREEN}OK${C_NC} ] ${C_OK}\n"