From 7e941586844e514d296dd755972902dd64a4c8ba Mon Sep 17 00:00:00 2001 From: Ethan Yonker Date: Fri, 22 Mar 2019 08:18:21 -0500 Subject: Support both old and new mkfs.f2fs Easiest way I could find to determine if we have the old or the new mkfs.f2fs version is to pipe the usage output to grep and grep the output for "reserved" and redirect the output to a file. If the file has any data in it, then we must have the old mkfs.f2fs that uses -r. Change-Id: I7f1ed88b577a3f9827a98f0868d8f82e4e4886ac --- partition.cpp | 44 ++++++++++++++++++++++++++++++++++++-------- twrp-functions.cpp | 12 +++++++----- twrp-functions.hpp | 4 ++-- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/partition.cpp b/partition.cpp index b421598b8..c278fa9fe 100644 --- a/partition.cpp +++ b/partition.cpp @@ -2275,17 +2275,45 @@ bool TWPartition::Wipe_F2FS() { NeedPreserveFooter = false; } - unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); - if (!dev_sz) + gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs")); + // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes" + // or the new mkfs.f2fs that expects the number of sectors as the optional last argument + // Note: some 7.1 trees have the old and some have the new. + command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest"; + TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code + if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) { + LOGINFO("Error determining mkfs.f2fs version\n"); return false; + } + if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) { + LOGINFO("Using newer mkfs.f2fs\n"); + unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); + if (!dev_sz) + return false; - if (NeedPreserveFooter) - Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET; + if (NeedPreserveFooter) + Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET; - gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs")); - command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + to_string(dev_sz / 4096); - if (TWFunc::Path_Exists("/sbin/sload.f2fs")) { - command += " && sload.f2fs -t /data " + Actual_Block_Device; + char dev_sz_str[48]; + sprintf(dev_sz_str, "%llu", (dev_sz / 4096)); + command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str; + if (TWFunc::Path_Exists("/sbin/sload.f2fs")) { + command += " && sload.f2fs -t /data " + Actual_Block_Device; + } + } else { + LOGINFO("Using older mkfs.f2fs\n"); + command = "mkfs.f2fs -t 0"; + if (NeedPreserveFooter) { + // Only use length if we're not decrypted + char len[32]; + int mod_length = Length; + if (Length < 0) + mod_length *= -1; + sprintf(len, "%i", mod_length); + command += " -r "; + command += len; + } + command += " " + Actual_Block_Device; } LOGINFO("mkfs.f2fs command: %s\n", command.c_str()); if (TWFunc::Exec_Cmd(command) == 0) { diff --git a/twrp-functions.cpp b/twrp-functions.cpp index ff34828e2..84451b3b6 100755 --- a/twrp-functions.cpp +++ b/twrp-functions.cpp @@ -77,7 +77,7 @@ int TWFunc::Exec_Cmd(const string& cmd, string &result) { return ret; } -int TWFunc::Exec_Cmd(const string& cmd) { +int TWFunc::Exec_Cmd(const string& cmd, bool Show_Errors) { pid_t pid; int status; switch(pid = fork()) @@ -91,7 +91,7 @@ int TWFunc::Exec_Cmd(const string& cmd) { break; default: { - if (TWFunc::Wait_For_Child(pid, &status, cmd) != 0) + if (TWFunc::Wait_For_Child(pid, &status, cmd, Show_Errors) != 0) return -1; else return 0; @@ -121,18 +121,20 @@ string TWFunc::Get_Path(const string& Path) { return Path; } -int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) { +int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors) { pid_t rc_pid; rc_pid = waitpid(pid, status, 0); if (rc_pid > 0) { if (WIFSIGNALED(*status)) { - gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination + if (Show_Errors) + gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination return -1; } else if (WEXITSTATUS(*status) == 0) { LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success } else { - gui_msg(Msg(msg::kError, "pid_error={1} process ended with ERROR: {2}")(Child_Name)(WEXITSTATUS(*status))); // Graceful exit, but there was an error + if (Show_Errors) + gui_msg(Msg(msg::kError, "pid_error={1} process ended with ERROR: {2}")(Child_Name)(WEXITSTATUS(*status))); // Graceful exit, but there was an error return -1; } } else { // no PID returned diff --git a/twrp-functions.hpp b/twrp-functions.hpp index efaa460d9..669a9e970 100755 --- a/twrp-functions.hpp +++ b/twrp-functions.hpp @@ -56,8 +56,8 @@ public: static string Get_Filename(const string& Path); // Trims the path off of a filename static int Exec_Cmd(const string& cmd, string &result); //execute a command and return the result as a string by reference - static int Exec_Cmd(const string& cmd); //execute a command - static int Wait_For_Child(pid_t pid, int *status, string Child_Name); // Waits for pid to exit and checks exit status + static int Exec_Cmd(const string& cmd, bool Show_Errors = true); //execute a command, displays an error to the GUI if Show_Errors is true, Show_Errors is true by default + static int Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors = true); // Waits for pid to exit and checks exit status, displays an error to the GUI if Show_Errors is true which is the default static int Wait_For_Child_Timeout(pid_t pid, int *status, const string& Child_Name, int timeout); // Waits for a pid to exit until the timeout is hit. If timeout is hit, kill the chilld. static bool Path_Exists(string Path); // Returns true if the path exists static Archive_Type Get_File_Type(string fn); // Determines file type, 0 for unknown, 1 for gzip, 2 for OAES encrypted -- cgit v1.2.3