Skip to content

Commit

Permalink
Merge pull request #10 from YuzukiTsuru/dev
Browse files Browse the repository at this point in the history
Bump to OpenixCard 1.1.1
  • Loading branch information
YuzukiTsuru authored Jun 19, 2022
2 parents 51b0ee6 + d66b1bb commit 82f7606
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 72 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ OpenixCard -d -i <img> - Convert Allwinner image to regular image
OpenixCard -p -i <dir> - Pack dumped Allwinner image to regular image from folder
```

## Download
### ArchLinux
OpenixCard Now available at [AUR](https://aur.archlinux.org/packages/openixcard) [#3](https://github.com/YuzukiTsuru/OpenixCard/issues/3#issuecomment-1135317155)
```
yay -S openixcard
```


## Build from source

```
Expand Down
7 changes: 1 addition & 6 deletions src/GenIMG/genimage-src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ int systemp(struct image *image, const char *fmt, ...)
if (!buf)
return -ENOMEM;

if (loglevel() >= 3)
o = " (stderr+stdout):";
else if (loglevel() >= 1)
o = " (stderr):";
else
o = "";
o = "";

image_info(image, "cmd: \"%s\"%s\n", buf, o);

Expand Down
123 changes: 71 additions & 52 deletions src/OpenixCard/OpenixCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,6 @@ extern "C" {
#include "OpenixCard.h"

OpenixCard::OpenixCard(int argc, char **argv) {
parse_args(argc, argv);

if (this->is_dump) {
LOG::INFO("Input file: " + this->input_file + " Now converting...");
check_file(this->input_file);
unpack_target_image();
LOG::INFO("Convert Done! Prasing the partition tables...");
dump_and_clean();
} else if (this->is_unpack) {
LOG::INFO("Input file: " + this->input_file + " Now converting...");
check_file(this->input_file);
unpack_target_image();
if (this->is_cfg)
LOG::INFO("Unpack Done! Your image file and cfg file at " + this->temp_file_path);
else
LOG::INFO("Unpack Done! Your image file at " + this->temp_file_path);
} else if (this->is_pack) {
pack();
}
}

void OpenixCard::parse_args(int argc, char **argv) {
argparse::ArgumentParser parser("OpenixCard", []() {
if (std::string(PROJECT_GIT_HASH).empty())
return PROJECT_VER;
Expand All @@ -72,6 +50,10 @@ void OpenixCard::parse_args(int argc, char **argv) {
.help("Pack dumped Allwinner image to regular image from folder")
.default_value(false)
.implicit_value(true);
parser.add_argument("-s", "--size")
.help("Dump the real size of Allwinner image")
.default_value(false)
.implicit_value(true);
parser.add_argument("-i", "--input")
.help("Input Allwinner image file or dumped image directory")
.required();
Expand All @@ -86,24 +68,62 @@ void OpenixCard::parse_args(int argc, char **argv) {
std::exit(1);
}

this->input_file = parser.get<std::string>("input");
input_file = parser.get<std::string>("input");

// if input file path is absolute path, convert to relative path, #1
std::filesystem::path input_path(input_file);

this->is_absolute = input_path.is_absolute();
this->temp_file_path = input_file + ".dump";
this->output_file_path = temp_file_path + ".out";
is_absolute = input_path.is_absolute();
temp_file_path = input_file + ".dump";
output_file_path = temp_file_path + ".out";

// Basic Operator
mode = [&]() {
if (parser.get<bool>("pack")) {
return OpenixCardOperator::PACK;
} else if (parser.get<bool>("unpack")) {
return OpenixCardOperator::UNPACK;
} else if (parser.get<bool>("dump")) {
return OpenixCardOperator::DUMP;
} else if (parser.get<bool>("size")) {
return OpenixCardOperator::SIZE;
} else {
return OpenixCardOperator::NONE;
}
}();

this->is_pack = parser.get<bool>("pack");
this->is_unpack = parser.get<bool>("unpack");
this->is_dump = parser.get<bool>("dump");
this->is_cfg = parser.get<bool>("cfg");
if (mode == OpenixCardOperator::NONE) {
std::cout << parser;
throw operator_missing_error();
}

if (!this->is_unpack && !this->is_dump && !this->is_pack) {
LOG::ERROR("No action selected, please select one of the following actions: -u, -d");
std::cout << parser; // show help
std::exit(1);
// Addition Operator
mode_ext = [&]() {
if (parser.get<bool>("cfg")) {
return OpenixCardOperator::UNPACKCFG;
} else {
return OpenixCardOperator::NONE;
}
}();

if (mode == OpenixCardOperator::DUMP) {
LOG::INFO("Input file: " + input_file + " Now converting...");
check_file(input_file);
unpack_target_image();
LOG::INFO("Convert Done! Prasing the partition tables...");
dump_and_clean();
} else if (mode == OpenixCardOperator::UNPACK) {
LOG::INFO("Input file: " + input_file + " Now converting...");
check_file(input_file);
unpack_target_image();
if (mode_ext == OpenixCardOperator::UNPACKCFG) {
LOG::INFO("Unpack Done! Your image file and cfg file at " + temp_file_path);
save_cfg_file();
} else {
LOG::INFO("Unpack Done! Your image file at " + temp_file_path);
}
} else if (mode == OpenixCardOperator::PACK) {
pack();
}
}

Expand All @@ -124,9 +144,9 @@ void OpenixCard::pack() {

std::string target_cfg_path = {};

auto a = std::filesystem::directory_iterator(this->input_file);
auto a = std::filesystem::directory_iterator(input_file);

for (const auto &entry: std::filesystem::directory_iterator(this->input_file)) {
for (const auto &entry: std::filesystem::directory_iterator(input_file)) {
if (entry.path().extension() == ".cfg") {
if (entry.path().filename() != "image.cfg") {
target_cfg_path = entry.path().string();
Expand All @@ -139,54 +159,53 @@ void OpenixCard::pack() {
return;
}

GenIMG gen_img(target_cfg_path, this->input_file, this->input_file);
GenIMG gen_img(target_cfg_path, input_file, input_file);

// check gen_img-src result
if (gen_img.get_status() != 0) {
LOG::ERROR("Generate image failed!");
std::exit(1);
}

LOG::INFO("Generate Done! Your image file at " + this->input_file + " Cleaning up...");
LOG::INFO("Generate Done! Your image file at " + input_file + " Cleaning up...");
}

void OpenixCard::unpack_target_image() {
// dump the packed image
std::filesystem::create_directories(this->temp_file_path);
std::filesystem::create_directories(temp_file_path);
crypto_init();
unpack_image(this->input_file.c_str(), this->temp_file_path.c_str(), this->is_absolute);
if (this->is_cfg) {
save_cfg_file();
}
std::cout << cc::cyan;
unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute);
std::cout << cc::reset;
}

void OpenixCard::dump_and_clean() {
FEX2CFG fex2Cfg(this->temp_file_path);
auto target_cfg_path = fex2Cfg.save_file(this->temp_file_path);
FEX2CFG fex2Cfg(temp_file_path);
auto target_cfg_path = fex2Cfg.save_file(temp_file_path);
auto image_name = fex2Cfg.get_image_name();
// generate the image
LOG::INFO("Prase Done! Generating target image...");

GenIMG genimage(target_cfg_path, this->temp_file_path, this->output_file_path);
GenIMG genimage(target_cfg_path, temp_file_path, output_file_path);

// check genimage-src result
if (genimage.get_status() != 0) {
LOG::ERROR("Generate image failed!");
std::exit(1);
}

LOG::INFO("Generate Done! Your image file at " + this->output_file_path + " Cleaning up...");
std::filesystem::remove_all(this->temp_file_path);
LOG::INFO("Generate Done! Your image file at " + output_file_path + " Cleaning up...");
std::filesystem::remove_all(temp_file_path);
}

void OpenixCard::save_cfg_file() {
FEX2CFG fex2Cfg(this->temp_file_path);
auto target_cfg_path = fex2Cfg.save_file(this->temp_file_path);
FEX2CFG fex2Cfg(temp_file_path);
auto target_cfg_path = fex2Cfg.save_file(temp_file_path);
LOG::INFO("Prase Done! Your cfg file at " + target_cfg_path);
}

void OpenixCard::check_file(const std::string& file_path) {
if(!std::filesystem::exists(file_path)){
void OpenixCard::check_file(const std::string &file_path) {
if (!std::filesystem::exists(file_path)) {
throw file_open_error(file_path);
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/OpenixCard/OpenixCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ class OpenixCard {
std::string temp_file_path;
std::string output_file_path;

bool is_pack = false;
bool is_unpack = false;
bool is_dump = false;
bool is_cfg = false;
enum OpenixCardOperator {
NONE,
PACK,
UNPACK,
UNPACKCFG,
DUMP,
SIZE
};

OpenixCardOperator mode;
OpenixCardOperator mode_ext;
bool is_absolute = false;

private:
static void show_logo();

static void check_file(const std::string& file_path);
private:
void parse_args(int argc, char **argv);

void pack();

void unpack_target_image();
Expand Down
10 changes: 10 additions & 0 deletions src/OpenixCard/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ class file_open_error : public std::runtime_error {
explicit file_open_error(const std::string &what) : std::runtime_error("Fail to open file: " + what + ".") {};
};

class operator_error : public std::runtime_error {
public:
explicit operator_error(const std::string &what) : std::runtime_error("Operate ERROR: " + what + ".") {};
};

class operator_missing_error : public std::runtime_error {
public:
explicit operator_missing_error() : std::runtime_error("Operate ERROR, You must specify a Operator.") {};
};

#endif //OPENIXCARD_EXCEPTION_H
24 changes: 16 additions & 8 deletions src/OpenixIMG/src/OpenixIMG.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

int flag_encryption_enabled;

#define OpenixIMG_LOG(fmt, arg...) \
do { \
printf(fmt, ##arg); \
} while (0)

#define O_LOG(fmt, arg...) OpenixIMG_LOG("[OpenixIMG INFO] " fmt, ##arg)
#define O_ERR(fmt, arg...) OpenixIMG_LOG("[OpenixIMG ERROR] " fmt, ##arg)

/* Crypto */
rc6_ctx_t header_ctx;
rc6_ctx_t fileheaders_ctx;
Expand Down Expand Up @@ -130,7 +138,7 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {

ifp = fopen(infn, "rb");
if (ifp == NULL) {
fprintf(stderr, "Error: unable to open %s!\n", infn);
O_ERR("Error: unable to open %s!\n", infn);
return 2;
}

Expand All @@ -139,14 +147,13 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
fseek(ifp, 0, SEEK_SET);

if (imagesize <= 0) {
fprintf(stderr, "Error: Invalid file size %ld (%s)\n",
imagesize, strerror(errno));
O_ERR("Error: Invalid file size %ld (%s)\n", imagesize, strerror(errno));
return 3;
}

image = malloc(imagesize);
if (!image) {
fprintf(stderr, "Error: Unable to allocate memory for image: %ld\n", imagesize);
O_ERR("Error: Unable to allocate memory for image: %ld\n", imagesize);
return 4;
}

Expand All @@ -159,10 +166,11 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
flag_encryption_enabled = 0;

/* Decrypt header (padded to 1024 bytes) */
O_LOG("Now Decrypt IMG header...\n");
curr = rc6_decrypt_inplace(image, 1024, &header_ctx);


/* Check version of header and setup our local state */
O_LOG("The IMG version is: 0x%0x\n", header->header_version);
if (header->header_version == 0x0300) {
num_files = header->v3.num_files;
hardware_id = header->v3.hardware_id;
Expand All @@ -181,6 +189,7 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
curr = rc6_decrypt_inplace(curr, num_files * 1024, &fileheaders_ctx);

/* Decrypt file contents */
O_LOG("Now Decrypt IMG file contents...\n");
for (i = 0; i < num_files; i++) {
struct imagewty_file_header *filehdr;
uint64_t stored_length;
Expand All @@ -197,6 +206,7 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
curr = next;
}

O_LOG("Writing the IMG config data...\n");
cfp = dir_fopen(outdn, "image.cfg", "wb", is_absolute);
if (cfp != NULL) {
char timestr[256];
Expand All @@ -223,9 +233,8 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
}

for (i = 0; i < num_files; i++) {
uint32_t stored_length, original_length;
uint32_t original_length;
struct imagewty_file_header *filehdr;
char hdrfname[32], contfname[32];
const char *filename;
uint32_t offset;

Expand All @@ -239,7 +248,6 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
filename = filehdr->v1.filename;
offset = filehdr->v1.offset;
}

ofp = dir_fopen(outdn, filename, "wb", is_absolute);
if (ofp) {
fwrite(image + offset, original_length, 1, ofp);
Expand Down

0 comments on commit 82f7606

Please sign in to comment.