Skip to content

Commit

Permalink
add GenIMG Print out and OpenixIMG Print
Browse files Browse the repository at this point in the history
  • Loading branch information
YuzukiTsuru committed Jun 19, 2022
1 parent b505771 commit d66b1bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
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
2 changes: 2 additions & 0 deletions src/OpenixCard/OpenixCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ void OpenixCard::unpack_target_image() {
// dump the packed 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);
std::cout << cc::reset;
}

void OpenixCard::dump_and_clean() {
Expand Down
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 d66b1bb

Please sign in to comment.