From d65956bf137fa3546bf03b2c686906f067375e97 Mon Sep 17 00:00:00 2001
From: merlokk <807634+merlokk@users.noreply.github.com>
Date: Wed, 2 Feb 2022 19:50:55 +0200
Subject: [PATCH] add commit to write

---
 client/src/cmdhfcipurse.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/client/src/cmdhfcipurse.c b/client/src/cmdhfcipurse.c
index dbc9c9d72..ab99e48ad 100644
--- a/client/src/cmdhfcipurse.c
+++ b/client/src/cmdhfcipurse.c
@@ -643,7 +643,8 @@ static int CmdHFCipurseWriteFile(const char *Cmd) {
                   "Write file in the application by file ID with key ID and key. If no key is supplied, default key of 737373...7373 will be used",
                   "hf cipurse write --fid 2ff7 -d aabb  -> Authenticate with keyID 1, write file with id 2ff7\n"
                   "hf cipurse write -n 2 -k 65656565656565656565656565656565 --fid 2ff7 -d aabb -> Authenticate keyID 2 and write file\n"
-                  "hf cipurse write --aid 4144204631 --fid 0102 -d aabb  -> write file with id 0102 in the 4144204631 application\n");
+                  "hf cipurse write --aid 4144204631 --fid 0102 -d aabb  -> write file with id 0102 in the 4144204631 application\n"
+                  "hf cipurse write --fid 0102 -d aabb --commit  -> write file with id 0102 and perform commit after write\n");
 
     void *argtable[] = {
         arg_param_begin,
@@ -658,6 +659,7 @@ static int CmdHFCipurseWriteFile(const char *Cmd) {
         arg_str0(NULL, "sreq",    "<plain|mac(default)|encode>", "communication reader-PICC security level"),
         arg_str0(NULL, "sresp",   "<plain|mac(default)|encode>", "communication PICC-reader security level"),
         arg_str0("d",  "data",    "<hex>", "hex data to write to new file"),
+        arg_lit0(NULL, "commit",  "need commit after write"),
         arg_param_end
     };
     CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -695,10 +697,12 @@ static int CmdHFCipurseWriteFile(const char *Cmd) {
         return PM3_EINVARG;
     }
 
-    SetAPDULogging(APDULogging);
+    bool needCommit = arg_get_lit(ctx, 12);
 
     CLIParserFree(ctx);
 
+    SetAPDULogging(APDULogging);
+
     size_t len = 0;
     uint16_t sw = 0;
     uint8_t buf[APDU_RES_LEN] = {0};
@@ -755,6 +759,16 @@ static int CmdHFCipurseWriteFile(const char *Cmd) {
 
     PrintAndLogEx(INFO, "File id " _YELLOW_("%x") " successfully written", fileId);
 
+    if (needCommit) {
+        sw = 0;
+        res = CIPURSECommitTransaction(&sw);
+        if (res != 0 || sw != 0x9000)
+            PrintAndLogEx(WARNING, "Commit " _YELLOW_("ERROR") ". Card returns 0x%04x", sw);
+
+        if (verbose)
+            PrintAndLogEx(INFO, "Commit ( " _GREEN_("ok") " )");
+    }
+
     DropField();
     return PM3_SUCCESS;
 }