diff --git a/src/OpenixCard/FEX2CFG.cpp b/src/OpenixCard/FEX2CFG.cpp index 5c677a0..4c954a5 100644 --- a/src/OpenixCard/FEX2CFG.cpp +++ b/src/OpenixCard/FEX2CFG.cpp @@ -146,7 +146,7 @@ void FEX2CFG::gen_cfg() { std::cout << "Remaining space."; } } else if (opt.get_name() == "size") { - std::cout << opt.get() / 0x300 << "MB - " << opt.get() << "KB"; + std::cout << static_cast(opt.get()) / 2 / 0x300 << "MB - " << opt.get() / 2 << "KB"; } } std::cout << std::endl; diff --git a/src/OpenixCard/OpenixCard.cpp b/src/OpenixCard/OpenixCard.cpp index a2986aa..fd675d2 100644 --- a/src/OpenixCard/OpenixCard.cpp +++ b/src/OpenixCard/OpenixCard.cpp @@ -145,7 +145,7 @@ void OpenixCard::show_logo() { "| |___ ___ ___|_|_ _| |___ ___ _| |\n" "| | | . | -_| | |_'_| --| .'| _| . |\n" "|_____| _|___|_|_|_|_,_|_____|__,|_| |___|\n" - " |_| Version: " << PROJECT_GIT_HASH + " |_| Version: " << PROJECT_GIT_HASH << " Commit: " << PROJECT_VER << cc::magenta << "\nCopyright (c) 2022, YuzukiTsuru \n" << cc::reset << std::endl; @@ -189,8 +189,21 @@ void OpenixCard::unpack_target_image() { std::filesystem::create_directories(temp_file_path); crypto_init(); std::cout << cc::cyan; - unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute); + auto unpack_img_ret = unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute); std::cout << cc::reset; + + switch (unpack_img_ret) { + case 2: + throw file_open_error(input_file); + case 3: + throw file_size_error(input_file); + case 4: + throw std::runtime_error("Unable to allocate memory for image: "+ input_file); + case 5: + throw file_format_error(input_file); + default: + break; + } } void OpenixCard::dump_and_clean() { @@ -229,5 +242,6 @@ void OpenixCard::get_real_size() { FEX2CFG fex2Cfg(temp_file_path); auto real_size = fex2Cfg.get_image_real_size(true); LOG::DATA("The accurate size of image: " + std::to_string(real_size / 1024) + "MB, " + std::to_string(real_size) + "KB"); + std::filesystem::remove_all(temp_file_path); } diff --git a/src/OpenixCard/exception.h b/src/OpenixCard/exception.h index c784acc..745b5a4 100644 --- a/src/OpenixCard/exception.h +++ b/src/OpenixCard/exception.h @@ -20,6 +20,16 @@ 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 file_format_error : public std::runtime_error { +public: + explicit file_format_error(const std::string &what) : std::runtime_error("File: " + what + " is not Allwinner image.") {}; +}; + +class file_size_error : public std::runtime_error { +public: + explicit file_size_error(const std::string &what) : std::runtime_error("Invalid file size: " + what + ".") {}; +}; + class no_file_provide_error : public std::runtime_error { public: no_file_provide_error() : std::runtime_error("No file Provide.") {}; diff --git a/src/OpenixIMG/src/OpenixIMG.c b/src/OpenixIMG/src/OpenixIMG.c index df345ac..d7419d3 100644 --- a/src/OpenixIMG/src/OpenixIMG.c +++ b/src/OpenixIMG/src/OpenixIMG.c @@ -27,7 +27,6 @@ int flag_encryption_enabled; } 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; @@ -138,7 +137,6 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) { ifp = fopen(infn, "rb"); if (ifp == NULL) { - O_ERR("Error: unable to open %s!\n", infn); return 2; } @@ -147,13 +145,11 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) { fseek(ifp, 0, SEEK_SET); if (imagesize <= 0) { - O_ERR("Error: Invalid file size %ld (%s)\n", imagesize, strerror(errno)); return 3; } image = malloc(imagesize); if (!image) { - O_ERR("Error: Unable to allocate memory for image: %ld\n", imagesize); return 4; } @@ -177,12 +173,14 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) { firmware_id = header->v3.firmware_id; pid = header->v3.pid; vid = header->v3.vid; - } else /*if (header->header_version == 0x0100)*/ { + } else if (header->header_version == 0x0100) { num_files = header->v1.num_files; hardware_id = header->v1.hardware_id; firmware_id = header->v1.firmware_id; pid = header->v1.pid; vid = header->v1.vid; + } else { + return 5; } /* Decrypt file headers */