pmt: Improvements for developers

- Android.bp was written to be included in ROMs/recoveries etc. in Android build system.
 - Macros have been added to simplify feature creation.
 - Some bug fixes (for JSON and pmt).
This commit is contained in:
2025-08-27 15:50:40 +03:00
parent 631c735a9a
commit e0f0b5b484
18 changed files with 200 additions and 109 deletions

View File

@@ -71,7 +71,7 @@ void basic_function_manager::registerFunction(
std::unique_ptr<basic_function> _func, CLI::App &_app) {
LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl;
for (const auto &f : _functions) {
if (strcmp(f->name(), _func->name()) != 0) {
if (std::string(_func->name()) == std::string(f->name())) {
LOGN(PMTF, INFO) << "Function is already registered: " << _func->name()
<< ". Skipping." << std::endl;
return;

View File

@@ -20,7 +20,9 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#ifndef ANDROID_BUILD
#include <generated/buildInfo.hpp>
#endif
#include <string>
#include <unistd.h>

View File

@@ -27,7 +27,7 @@
#define BFUN "backupFunction"
namespace PartitionManager {
pair backupFunction::runAsync(const std::string &partitionName,
RUN_ASYNC(backupFunction)(const std::string &partitionName,
const std::string &outputName,
const uint64_t bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName))
@@ -105,7 +105,7 @@ pair backupFunction::runAsync(const std::string &partitionName,
true};
}
bool backupFunction::init(CLI::App &_app) {
INIT(backupFunction) {
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
@@ -123,7 +123,7 @@ bool backupFunction::init(CLI::App &_app) {
return true;
}
bool backupFunction::run() {
RUN(backupFunction) {
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames,
',', true);
if (!outputNames.empty() && partitions.size() != outputNames.size())
@@ -161,7 +161,7 @@ bool backupFunction::run() {
return endResult;
}
bool backupFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(backupFunction)
const char *backupFunction::name() const { return BFUN; }
NAME(backupFunction) { return BFUN; }
} // namespace PartitionManager

View File

@@ -25,7 +25,7 @@ Copyright 2025 Yağız Zengin
#define EFUN "eraseFunction"
namespace PartitionManager {
pair eraseFunction::runAsync(const std::string &partitionName,
RUN_ASYNC(eraseFunction)(const std::string &partitionName,
const uint64_t bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
@@ -87,7 +87,7 @@ pair eraseFunction::runAsync(const std::string &partitionName,
true};
}
bool eraseFunction::init(CLI::App &_app) {
INIT(eraseFunction) {
LOGN(EFUN, INFO) << "Initializing variables of erase function." << std::endl;
cmd = _app.add_subcommand("erase", "Writes zero bytes to partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")
@@ -100,7 +100,7 @@ bool eraseFunction::init(CLI::App &_app) {
return true;
}
bool eraseFunction::run() {
RUN(eraseFunction) {
std::vector<std::future<pair>> futures;
for (const auto &partitionName : partitions) {
uint64_t buf = bufferSize;
@@ -127,7 +127,7 @@ bool eraseFunction::run() {
return endResult;
}
bool eraseFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(eraseFunction)
const char *eraseFunction::name() const { return EFUN; }
NAME(eraseFunction) { return EFUN; }
} // namespace PartitionManager

View File

@@ -25,7 +25,7 @@ Copyright 2025 Yağız Zengin
#define FFUN "flashFunction"
namespace PartitionManager {
pair flashFunction::runAsync(const std::string &partitionName,
RUN_ASYNC(flashFunction)(const std::string &partitionName,
const std::string &imageName,
const uint64_t bufferSize) {
if (!Helper::fileIsExists(imageName))
@@ -92,7 +92,7 @@ pair flashFunction::runAsync(const std::string &partitionName,
true};
}
bool flashFunction::init(CLI::App &_app) {
INIT(flashFunction) {
LOGN(FFUN, INFO) << "Initializing variables of flash function." << std::endl;
cmd = _app.add_subcommand("flash", "Flash image(s) to partition(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
@@ -110,7 +110,7 @@ bool flashFunction::init(CLI::App &_app) {
return true;
}
bool flashFunction::run() {
RUN(flashFunction) {
processCommandLine(partitions, imageNames, rawPartitions, rawImageNames, ',',
true);
if (partitions.size() != imageNames.size())
@@ -148,7 +148,7 @@ bool flashFunction::run() {
return endResult;
}
bool flashFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(flashFunction)
const char *flashFunction::name() const { return FFUN; }
NAME(flashFunction) { return FFUN; }
} // namespace PartitionManager

View File

@@ -24,7 +24,8 @@ Copyright 2025 Yağız Zengin
#define IFUN "infoFunction"
namespace PartitionManager {
bool infoFunction::init(CLI::App &_app) {
INIT(infoFunction) {
LOGN(IFUN, INFO) << "Initializing variables of info printer function."
<< std::endl;
cmd = _app.add_subcommand("info", "Tell info(s) of input partition list")
@@ -53,7 +54,7 @@ bool infoFunction::init(CLI::App &_app) {
return true;
}
bool infoFunction::run() {
RUN(infoFunction) {
if (partitions.back() == "get-all" || partitions.back() == "getvar-all") {
partitions.clear();
const auto parts = Variables->PartMap->getPartitionList();
@@ -111,7 +112,7 @@ bool infoFunction::run() {
return true;
}
bool infoFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(infoFunction)
const char *infoFunction::name() const { return IFUN; };
NAME(infoFunction) { return IFUN; };
} // namespace PartitionManager

View File

@@ -27,7 +27,7 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager {
bool memoryTestFunction::init(CLI::App &_app) {
INIT(memoryTestFunction) {
LOGN(MTFUN, INFO) << "Initializing variables of memory test function."
<< std::endl;
cmd = _app.add_subcommand("memtest", "Test your write/read speed of device.");
@@ -56,7 +56,7 @@ bool memoryTestFunction::init(CLI::App &_app) {
return true;
}
bool memoryTestFunction::run() {
RUN(memoryTestFunction) {
LOGN(MTFUN, INFO) << "Starting memory test on " << testPath << std::endl;
Helper::garbageCollector collector;
const std::string test = Helper::pathJoin(testPath, "test.bin");
@@ -122,8 +122,7 @@ bool memoryTestFunction::run() {
return true;
}
bool memoryTestFunction::isUsed() const { return cmd->parsed(); }
const char *memoryTestFunction::name() const { return MTFUN; }
IS_USED_COMMON_BODY(memoryTestFunction)
NAME(memoryTestFunction) { return MTFUN; }
} // namespace PartitionManager

View File

@@ -27,7 +27,7 @@ std::string convertTo(const uint64_t size, const std::string &multiple) {
}
namespace PartitionManager {
bool partitionSizeFunction::init(CLI::App &_app) {
INIT(partitionSizeFunction) {
LOGN(SFUN, INFO)
<< "Initializing variables of partition size getter function."
<< std::endl;
@@ -54,7 +54,7 @@ bool partitionSizeFunction::init(CLI::App &_app) {
return true;
}
bool partitionSizeFunction::run() {
RUN(partitionSizeFunction) {
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
@@ -89,7 +89,7 @@ bool partitionSizeFunction::run() {
return true;
}
bool partitionSizeFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(partitionSizeFunction)
const char *partitionSizeFunction::name() const { return SFUN; }
NAME(partitionSizeFunction) { return SFUN; }
} // namespace PartitionManager

View File

@@ -20,7 +20,7 @@
#define RPFUN "realPathFunction"
namespace PartitionManager {
bool realPathFunction::init(CLI::App &_app) {
INIT(realPathFunction) {
LOGN(RPFUN, INFO) << "Initializing variables of real path function."
<< std::endl;
cmd = _app.add_subcommand("real-path", "Tell real paths of partition(s)");
@@ -32,7 +32,7 @@ bool realPathFunction::init(CLI::App &_app) {
return true;
}
bool realPathFunction::run() {
RUN(realPathFunction) {
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
@@ -56,7 +56,7 @@ bool realPathFunction::run() {
return true;
}
bool realPathFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(realPathFunction)
const char *realPathFunction::name() const { return RPFUN; }
NAME(realPathFunction) { return RPFUN; }
} // namespace PartitionManager

View File

@@ -20,7 +20,7 @@ Copyright 2025 Yağız Zengin
#define RFUN "rebootFunction"
namespace PartitionManager {
bool rebootFunction::init(CLI::App &_app) {
INIT(rebootFunction) {
LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl;
cmd = _app.add_subcommand("reboot", "Reboots device");
cmd->add_option("rebootTarget", rebootTarget,
@@ -28,7 +28,7 @@ bool rebootFunction::init(CLI::App &_app) {
return true;
}
bool rebootFunction::run() {
RUN(rebootFunction) {
LOGN(RFUN, INFO) << "Rebooting device!!! (custom reboot target: "
<< (rebootTarget.empty() ? "none" : rebootTarget)
<< std::endl;
@@ -39,7 +39,7 @@ bool rebootFunction::run() {
return true;
}
bool rebootFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(rebootFunction)
const char *rebootFunction::name() const { return RFUN; }
NAME(rebootFunction) { return RFUN; }
} // namespace PartitionManager

View File

@@ -20,7 +20,7 @@
#define TFUN "typeFunction"
namespace PartitionManager {
bool typeFunction::init(CLI::App &_app) {
INIT(typeFunction) {
LOGN(TFUN, INFO) << "Initializing variables of type function." << std::endl;
cmd = _app.add_subcommand("type", "Get type of the partition(s) or image(s)");
cmd->add_option("content(s)", contents, "Content(s)")
@@ -39,7 +39,7 @@ bool typeFunction::init(CLI::App &_app) {
return true;
}
bool typeFunction::run() {
RUN(typeFunction) {
std::unordered_map<uint64_t, std::string> magics;
if (onlyCheckAndroidMagics)
magics.merge(PartitionMap::Extras::AndroidMagicMap);
@@ -75,8 +75,8 @@ bool typeFunction::run() {
return true;
}
bool typeFunction::isUsed() const { return cmd->parsed(); }
IS_USED_COMMON_BODY(typeFunction)
const char *typeFunction::name() const { return TFUN; }
NAME(typeFunction) { return TFUN; }
} // namespace PartitionManager

View File

@@ -21,6 +21,20 @@
#include <utility>
#include <vector>
#define INIT(cls) bool cls::init(CLI::App &_app)
#define RUN(cls) bool cls::run()
#define RUN_ASYNC(cls) pair cls::runAsync
#define IS_USED(cls) bool cls::isUsed() const
#define IS_USED_COMMON_BODY(cls) bool cls::isUsed() const { return cmd->parsed(); }
#define NAME(cls) const char *cls::name() const
#define COMMON_FUNCTION_BODY() \
CLI::App *cmd = nullptr; \
bool init(CLI::App &_app) override; \
bool run() override; \
[[nodiscard]] bool isUsed() const override; \
[[nodiscard]] const char *name() const override
namespace PartitionManager {
using pair = std::pair<std::string, bool>;
@@ -32,15 +46,9 @@ private:
uint64_t bufferSize = 0;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
COMMON_FUNCTION_BODY();
static pair runAsync(const std::string &partitionName,
const std::string &outputName, uint64_t bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Image flasher function
@@ -51,15 +59,9 @@ private:
uint64_t bufferSize = 0;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
COMMON_FUNCTION_BODY();
static pair runAsync(const std::string &partitionName,
const std::string &imageName, uint64_t bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Eraser function (writes zero bytes to partition)
@@ -69,14 +71,8 @@ private:
uint64_t bufferSize = 0;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
COMMON_FUNCTION_BODY();
static pair runAsync(const std::string &partitionName, uint64_t bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Partition size getter function
@@ -87,13 +83,7 @@ private:
asGiga = false;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
// Partition info getter function
@@ -105,13 +95,7 @@ private:
bool jsonFormat = false;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
class realPathFunction final : public FunctionBase {
@@ -120,13 +104,7 @@ private:
bool realLinkPath = false;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
class typeFunction final : public FunctionBase {
@@ -136,13 +114,7 @@ private:
uint64_t bufferSize = 0;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
class rebootFunction final : public FunctionBase {
@@ -150,13 +122,7 @@ private:
std::string rebootTarget;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
class memoryTestFunction final : public FunctionBase {
@@ -166,13 +132,7 @@ private:
bool doNotReadTest = false;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
COMMON_FUNCTION_BODY();
};
} // namespace PartitionManager