From 612da1f2de35f364f956f5529c529600e50708df Mon Sep 17 00:00:00 2001 From: drwowe Date: Fri, 10 Feb 2012 12:05:30 -0500 Subject: [PATCH] support signed blob as introduced by Transformer Prime culled from https://github.com/sparkym3/BlobTools --- shared/blob.h | 3 +++ src/blobpack.cpp | 38 +++++++++++++++++++++++++++----------- src/blobunpack.cpp | 36 ++++++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/shared/blob.h b/shared/blob.h index d5ee6d9..885885f 100644 --- a/shared/blob.h +++ b/shared/blob.h @@ -4,12 +4,15 @@ // Previous unknown fields grabbed from // http://nv-tegra.nvidia.com/gitweb/?p=android/platform/build.git;a=commitdiff;h=0adc4478615891636d8b7c476c20c2014b788537 +#define SIGNBLOB "\x2d\x53\x49\x47\x4e\x45\x44\x2d\x42\x59\x2d\x53\x49\x47\x4e\x42\x4c\x4f\x42\x2d\x29\xc3\x94\x20\x00\x01\x00\x00" //"-SIGNED-BY-SIGNBLOB-)Ô " +#define SIGNBLOB_SIZE 28 #define MAGIC "MSM-RADIO-UPDATE" #define MAGIC_SIZE 16 #define PART_NAME_LEN 4 typedef struct { + unsigned char signblob[SIGNBLOB_SIZE]; unsigned char magic[MAGIC_SIZE]; unsigned int version; // Always 0x00010000 unsigned int size; // Size of header diff --git a/src/blobpack.cpp b/src/blobpack.cpp index e356404..452e19f 100644 --- a/src/blobpack.cpp +++ b/src/blobpack.cpp @@ -3,6 +3,7 @@ #include #include "blob.h" + typedef struct { char *part_name; @@ -24,16 +25,24 @@ main (int argc, char **argv) memset (&hdr, 0, sizeof (header_type)); - if (argc < (GENERIC_ARGS+2)) // Require at least one partition + int nGenericArgs = GENERIC_ARGS; + bool bSignOffset = false; + if(0 == memcmp (argv[1], "-s", 2)) + { + bSignOffset = true; + ++nGenericArgs; + } + + if (argc < (nGenericArgs+2)) // Require at least one partition { - fprintf (stderr,"Usage: %s ...\n", argv[0]); + fprintf (stderr,"Usage: %s <-s> ...\n", argv[0]); fprintf(stderr, "Any number of partitionname partitionfilename entries can be entered\n"); return -1; } - outname = argv[1]; - partnums = argc - GENERIC_ARGS; + outname = argv[nGenericArgs-1]; + partnums = argc - nGenericArgs; if(partnums <= 0 || partnums % 2 != 0) { fprintf(stderr, "Error in parameters. There needs to be equal partition names and partition filenames."); @@ -45,28 +54,35 @@ main (int argc, char **argv) printf("Found %d partitions as commandline arguments\n", partnums); partitions = (partition_item*)calloc(partnums, sizeof(partition_item)); curr_part = partitions; - for(i=GENERIC_ARGS; ipart_name = argv[i]; curr_part->filename = argv[i+1]; - curr_part++; + ++curr_part; }; + memcpy(hdr.signblob, SIGNBLOB, SIGNBLOB_SIZE); memcpy(hdr.magic, MAGIC, MAGIC_SIZE); hdr.version = 0x00010000; // Taken from - hdr.size = hdr.part_offset = sizeof(header_type); + hdr.size = hdr.part_offset = sizeof(header_type)-SIGNBLOB_SIZE; hdr.num_parts = partnums; outfile = fopen (outname, "wb"); - fwrite (&hdr, sizeof (header_type), 1, outfile); + if(bSignOffset) + { + fwrite (&hdr, sizeof (header_type), 1, outfile); //Write the new way (write the sign as part of the header) + } + else + { + fwrite (&hdr.magic, sizeof (header_type)-SIGNBLOB_SIZE, 1, outfile); //Write the original way (ignore the new variable) + } printf ("Size: %d\n", hdr.size); - printf ("%d partitions starting at offset 0x%X\n", hdr.num_parts, - hdr.part_offset); + printf ("%d partitions starting at offset 0x%X\n", hdr.num_parts, hdr.part_offset); parts = (part_type *)calloc (hdr.num_parts, sizeof (part_type)); memset(parts, 0, sizeof(part_type)*hdr.num_parts); - int currentOffset = sizeof(header_type)+sizeof(part_type)*hdr.num_parts; + int currentOffset = hdr.part_offset+sizeof(part_type)*hdr.num_parts; printf("Offset: %d\n", currentOffset); for (i = 0; i < (int)hdr.num_parts; i++) { diff --git a/src/blobunpack.cpp b/src/blobunpack.cpp index b7352d7..8ce25f9 100644 --- a/src/blobunpack.cpp +++ b/src/blobunpack.cpp @@ -1,11 +1,13 @@ #include #include #include + #include "blob.h" + #ifdef _WIN32 #define snprintf _snprintf #endif -void dumpPartition (FILE * file, char *basename, part_type part); +void dumpPartition (FILE * file, char *basename, part_type part, bool bSignOffset); #define BUFFER_SIZE 2048 int @@ -30,16 +32,24 @@ main (int argc, char **argv) fprintf (stderr, "Unable to open \"%s\"\n", argv[1]); return -1; } - fread (&hdr, sizeof (header_type), 1, file); + + bool bSignOffset = false; + fread (&hdr.magic, sizeof (header_type)-SIGNBLOB_SIZE, 1, file); //Read the original way (ignore the new variable) if (memcmp (hdr.magic, MAGIC, MAGIC_SIZE)) { - fprintf (stderr, "File \"%s\" is not a valid blob file\n", argv[1]); - return -1; + fseek (file, 0, SEEK_SET); + fread (&hdr, sizeof (header_type), 1, file); //Read the new way (read in the sign so it is skipped) + if (memcmp (hdr.magic, MAGIC, MAGIC_SIZE)) + { + fprintf (stderr, "File \"%s\" is not a valid blob file\n", argv[1]); + return -1; + } + printf ("Header contains SIGNBLOB\n"); + bSignOffset = true; } printf ("Header size: %d\n", hdr.size); - printf ("%d partitions starting at offset 0x%X\n", hdr.num_parts, - hdr.part_offset); - printf ("Blob version: %d\n", hdr.version); + printf ("%d partitions starting at offset 0x%X\n", hdr.num_parts, hdr.part_offset); + printf ("Blob version: %d\n", hdr.version/0x10000); for(i=0; i<7; i++) { printf ("Blob 'unknown' %d: %d\n", i, hdr.unknown[i]); @@ -51,7 +61,7 @@ main (int argc, char **argv) fclose (hdrfile);*/ - fseek (file, hdr.part_offset, SEEK_SET); + fseek (file, hdr.part_offset+(bSignOffset?SIGNBLOB_SIZE:0), SEEK_SET); parts = (part_type *)calloc (hdr.num_parts, sizeof (part_type)); fread (parts, sizeof (part_type), hdr.num_parts, file); @@ -61,16 +71,16 @@ main (int argc, char **argv) printf ("Name: %s\n", parts[i].name); printf ("Offset: %d (0x%X)\n", parts[i].offset, parts[i].offset); printf ("Size: %d (0x%X)\n", parts[i].size, parts[i].size); - dumpPartition (file, argv[1], parts[i]); + dumpPartition (file, argv[1], parts[i], bSignOffset); } fclose (file); return 0; } void -dumpPartition (FILE * file, char *basename, part_type part) +dumpPartition (FILE * file, char *basename, part_type part, bool bSignOffset) { - fseek (file, part.offset, SEEK_SET); + fseek (file, part.offset+(bSignOffset?SIGNBLOB_SIZE:0), SEEK_SET); int dataleft = part.size; char buffer[BUFFER_SIZE]; char filename[1024]; @@ -84,8 +94,10 @@ dumpPartition (FILE * file, char *basename, part_type part) int toRead = dataleft > BUFFER_SIZE ? BUFFER_SIZE : dataleft; int dataread = fread (buffer, 1, toRead, file); int datawritten = fwrite (buffer, 1, dataread, outfile); - if (dataread != datawritten) + if (dataread != datawritten || (0 == dataread && 0 < dataleft)) + { abort (); + } dataleft -= dataread; } fclose (outfile);