Subversion Repositories QNX 8.QNX8 IFS tool

Rev

Rev 4 | Rev 6 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 5
Line 12... Line 12...
12
#include <time.h>
12
#include <time.h>
13
 
13
 
14
 
14
 
15
#ifdef _MSC_VER
15
#ifdef _MSC_VER
16
#include <io.h>
16
#include <io.h>
-
 
17
#define __ORDER_BIG_ENDIAN__    4321
17
#define __ORDER_LITTLE_ENDIAN__ 1234
18
#define __ORDER_LITTLE_ENDIAN__ 1234
18
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
19
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
19
#define __attribute__(x)
20
#define __attribute__(x)
-
 
21
#define __builtin_bswap32(x) _byteswap_ulong ((unsigned long) (x))
20
#define __builtin_bswap64(x) _byteswap_uint64 ((unsigned long long) (x))
22
#define __builtin_bswap64(x) _byteswap_uint64 ((unsigned long long) (x))
21
#define S_IFIFO 0x1000
23
#define S_IFIFO 0x1000
22
#define S_IFLNK 0xa000
24
#define S_IFLNK 0xa000
23
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
25
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
24
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
26
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
Line 406... Line 408...
406
static void SHA512_Init (SHA512_CTX *context);
408
static void SHA512_Init (SHA512_CTX *context);
407
static void SHA512_Update (SHA512_CTX *context, void *data, size_t len);
409
static void SHA512_Update (SHA512_CTX *context, void *data, size_t len);
408
static void SHA512_Final (uint8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context);
410
static void SHA512_Final (uint8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context);
409
static uint8_t *SHA512 (void *data, size_t data_len, uint8_t *digest); // computes a SHA-512 in one pass (shortcut for SHA512_Init(), SHA512_Update() N times and SHA512_Final())
411
static uint8_t *SHA512 (void *data, size_t data_len, uint8_t *digest); // computes a SHA-512 in one pass (shortcut for SHA512_Init(), SHA512_Update() N times and SHA512_Final())
410
static int32_t update_checksum32 (const int32_t start_value, const void *data, const size_t len); // update the sum of an array of 32-bit signed integers
412
static int32_t update_checksum32 (const int32_t start_value, const void *data, const size_t len); // update the sum of an array of 32-bit signed integers
-
 
413
static int32_t update_image_checksum (const int32_t start_value, const void *data, const size_t data_len, const bool is_foreign_endianness); // compute an IFS image checksum to store in the image trailer
411
static long long read_integer (const char *str); // reads an integer number for a string that may be specified in either hex, octal or decimal base, and may have an optional unit suffix (k, m, g, t)
414
static long long read_integer (const char *str); // reads an integer number for a string that may be specified in either hex, octal or decimal base, and may have an optional unit suffix (k, m, g, t)
412
static void hex_fprintf (FILE *fp, const uint8_t *data, size_t data_size, int howmany_columns, const char *fmt, ...); // hexdump-style formatted output to a file stream (which may be stdout/stderr)
415
static void hex_fprintf (FILE *fp, const uint8_t *data, size_t data_size, int howmany_columns, const char *fmt, ...); // hexdump-style formatted output to a file stream (which may be stdout/stderr)
413
static char *binary (const uint8_t x, char char_for_zero, char char_for_one); // returns the binary representation of byte 'x' as a string
416
static char *binary (const uint8_t x, char char_for_zero, char char_for_one); // returns the binary representation of byte 'x' as a string
414
static char *describe_uint8 (const uint8_t x, const char *bitwise_stringdescs[8]); // returns the ORed description of byte 'x' according to the description strings for each bit
417
static char *describe_uint8 (const uint8_t x, const char *bitwise_stringdescs[8]); // returns the ORed description of byte 'x' according to the description strings for each bit
415
static int fwrite_filecontents (const char *pathname, FILE *fp); // dumps the contents of pathname into fp
418
static int fwrite_filecontents (const char *pathname, FILE *fp); // dumps the contents of pathname into fp
Line 664... Line 667...
664
      sprintf (&digest_as_string[2 * byte_index], "%02x", digest_or_NULL[byte_index]);
667
      sprintf (&digest_as_string[2 * byte_index], "%02x", digest_or_NULL[byte_index]);
665
   return (digest_as_string);
668
   return (digest_as_string);
666
}
669
}
667
 
670
 
668
 
671
 
669
static int32_t update_checksum32 (const int32_t start_value, const void *data, const size_t len)
672
static int32_t update_checksum32_NOT (const int32_t start_value, const void *data, const size_t len)
670
{
673
{
671
   // compute the sum of an array of 32-bit signed integers
674
   // compute the sum of an array of 32-bit signed integers
672
 
675
 
673
   const int32_t *values_array = data;
676
   const int32_t *values_array = data;
674
   int32_t sum = start_value;
677
   int32_t sum = start_value;
675
 
678
 
676
   for (size_t value_index = 0; value_index < len / sizeof (int32_t); value_index++)
679
   for (size_t value_index = 0; value_index < len / sizeof (int32_t); value_index++)
677
      sum += values_array[value_index];
680
      sum += values_array[value_index];
678
 
681
 
679
   return (sum);
682
   return (sum);
-
 
683
}
-
 
684
 
-
 
685
 
-
 
686
static int32_t update_checksum32 (const int32_t start_value, const void *data, const size_t data_len)
-
 
687
{
-
 
688
   uint8_t hold_cksum_4[4] = { 0, 0, 0, 0 };
-
 
689
 
-
 
690
   int32_t image_cksum = start_value;
-
 
691
 
-
 
692
   int hold_index; // ST2C_4@4
-
 
693
   uint64_t i; // [sp+30h] [bp-10h]@3
-
 
694
   char *current_char_ptr; // [sp+38h] [bp-8h]@3
-
 
695
   char *data_start; // [sp+50h] [bp+10h]@1
-
 
696
   uint64_t _data_len; // [sp+58h] [bp+18h]@1
-
 
697
 
-
 
698
   data_start = data;
-
 
699
   _data_len = data_len;
-
 
700
   current_char_ptr = data_start;
-
 
701
   for (i = 0; i < data_len; ++i)
-
 
702
   {
-
 
703
      hold_index = i & 3;
-
 
704
      hold_cksum_4[hold_index] = *current_char_ptr;
-
 
705
      if (hold_index == 3)
-
 
706
      {
-
 
707
         if (false) // endianness
-
 
708
            image_cksum += (hold_cksum_4[3] << 0)
-
 
709
            + (hold_cksum_4[2] << 8)
-
 
710
            + (hold_cksum_4[1] << 16)
-
 
711
            + (hold_cksum_4[0] << 24);
-
 
712
         else
-
 
713
            image_cksum += (hold_cksum_4[0] << 0)
-
 
714
            + (hold_cksum_4[1] << 8)
-
 
715
            + (hold_cksum_4[2] << 16)
-
 
716
            + (hold_cksum_4[3] << 24);
-
 
717
      }
-
 
718
      current_char_ptr++;
-
 
719
   }
-
 
720
   return (-image_cksum);
-
 
721
}
-
 
722
 
-
 
723
 
-
 
724
static int32_t update_image_checksum (const int32_t start_value, const void *data, const size_t data_len, const bool is_foreign_endianness)
-
 
725
{
-
 
726
   // computes the checksum of an IFS image section, i.e. from the start of the image header to the end of the image trailer minus the last 4 bytes where the checksum is stored
-
 
727
 
-
 
728
   uint8_t accumulator[4] = { 0, 0, 0, 0 };
-
 
729
   int32_t image_cksum = start_value;
-
 
730
   char *current_char_ptr = data;
-
 
731
   size_t i;
-
 
732
 
-
 
733
   for (i = 0; i < data_len; i++)
-
 
734
   {
-
 
735
      accumulator[i % 4] = *current_char_ptr;
-
 
736
      if (i % 4 == 3)
-
 
737
         if (is_foreign_endianness)
-
 
738
            image_cksum += (accumulator[3] <<  0) + (accumulator[2] <<  8) + (accumulator[1] << 16) + (accumulator[0] << 24);
-
 
739
         else
-
 
740
            image_cksum += (accumulator[0] <<  0) + (accumulator[1] <<  8) + (accumulator[2] << 16) + (accumulator[3] << 24);
-
 
741
      current_char_ptr++;
-
 
742
   }
-
 
743
 
-
 
744
   return (is_foreign_endianness ? __builtin_bswap32 (-image_cksum) : -image_cksum);
680
}
745
}
681
 
746
 
682
 
747
 
683
static long long read_integer (const char *str)
748
static long long read_integer (const char *str)
684
{
749
{
Line 1194... Line 1259...
1194
   int arg_index;
1259
   int arg_index;
1195
   bool is_quoted_context = false;
1260
   bool is_quoted_context = false;
1196
   bool is_escaped_char = false;
1261
   bool is_escaped_char = false;
1197
   bool want_info = false;
1262
   bool want_info = false;
1198
   bool want_help = false;
1263
   bool want_help = false;
-
 
1264
   bool is_foreign_endianness;
1199
   int string_len;
1265
   int string_len;
1200
   int read_char;
1266
   int read_char;
1201
   FILE *buildfile_fp;
1267
   FILE *buildfile_fp;
1202
   FILE *fp;
1268
   FILE *fp;
1203
 
1269
 
Line 1724... Line 1790...
1724
      exit (1);
1790
      exit (1);
1725
   }
1791
   }
1726
   fseek (fp, imgheader_offset, SEEK_SET);
1792
   fseek (fp, imgheader_offset, SEEK_SET);
1727
   fread (blob_data, 1, blob_datasize, fp);
1793
   fread (blob_data, 1, blob_datasize, fp);
1728
   SHA512 (blob_data, blob_datasize, image_trailer.sha512); // compute SHA512 checksum
1794
   SHA512 (blob_data, blob_datasize, image_trailer.sha512); // compute SHA512 checksum
-
 
1795
   if (   ( (image_header.flags & IMAGE_FLAGS_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
-
 
1796
       || (!(image_header.flags & IMAGE_FLAGS_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
-
 
1797
      is_foreign_endianness = true; // if the header is big endian and we're on a little endian machine, or the other way around, it's a foreign endianness
-
 
1798
   else
-
 
1799
      is_foreign_endianness = false; // else this header is for the same endianness as us
1729
   image_trailer.cksum = 0; // compute old checksum
1800
   image_trailer.cksum = 0; // compute old checksum
1730
   image_trailer.cksum = update_checksum32 (image_trailer.cksum, (const uint32_t *) blob_data, blob_datasize);
1801
   image_trailer.cksum = update_image_checksum (image_trailer.cksum, (const uint32_t *) blob_data, blob_datasize, is_foreign_endianness);
1731
   image_trailer.cksum = update_checksum32 (image_trailer.cksum, (const uint32_t *) image_trailer.sha512, sizeof (image_trailer.sha512));
1802
   image_trailer.cksum = update_image_checksum (image_trailer.cksum, (const uint32_t *) image_trailer.sha512, sizeof (image_trailer.sha512), is_foreign_endianness);
1732
   free (blob_data);
1803
   free (blob_data);
1733
 
1804
 
1734
   // rewrite image trailer with final checksum values
1805
   // rewrite image trailer with final checksum values
1735
   fseek (fp, imgtrailer_offset, SEEK_SET);
1806
   fseek (fp, imgtrailer_offset, SEEK_SET);
1736
   fwrite (&image_trailer, sizeof (image_trailer), 1, fp); // write image trailer
1807
   fwrite (&image_trailer, sizeof (image_trailer), 1, fp); // write image trailer
Line 1781... Line 1852...
1781
   fsentry_t *current_fsentry = NULL;
1852
   fsentry_t *current_fsentry = NULL;
1782
   char recorded_sha512[2 * SHA512_DIGEST_LENGTH + 1] = "";
1853
   char recorded_sha512[2 * SHA512_DIGEST_LENGTH + 1] = "";
1783
   char computed_sha512[2 * SHA512_DIGEST_LENGTH + 1] = "";
1854
   char computed_sha512[2 * SHA512_DIGEST_LENGTH + 1] = "";
1784
   size_t startupfile_blobsize = 0;
1855
   size_t startupfile_blobsize = 0;
1785
   void *reallocated_ptr;
1856
   void *reallocated_ptr;
-
 
1857
   bool is_foreign_endianness;
1786
   size_t bootfile_blobsize = 0;
1858
   size_t bootfile_blobsize = 0;
1787
   size_t current_offset;
1859
   size_t current_offset;
1788
   size_t fsentry_index;
1860
   size_t fsentry_index;
1789
   size_t nearest_distance;
1861
   size_t nearest_distance;
1790
   size_t nearest_index;
1862
   size_t nearest_index;
Line 1859... Line 1931...
1859
         if (current_offset + startup_header->startup_size > filesize)
1931
         if (current_offset + startup_header->startup_size > filesize)
1860
         {
1932
         {
1861
            fprintf (stderr, "WARNING: this IFS file is too short (startup trailer would be past end of file)\n");
1933
            fprintf (stderr, "WARNING: this IFS file is too short (startup trailer would be past end of file)\n");
1862
            break;
1934
            break;
1863
         }
1935
         }
-
 
1936
 
-
 
1937
         // check if this endianness is ours
-
 
1938
         if (   ( (startup_header->flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
-
 
1939
             || (!(startup_header->flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
-
 
1940
            is_foreign_endianness = true; // if the header is big endian and we're on a little endian machine, or the other way around, it's a foreign endianness
-
 
1941
         else
-
 
1942
            is_foreign_endianness = false; // else this header is for the same endianness as us
1864
 
1943
 
1865
         // locate the right startup trailer at the right offset
1944
         // locate the right startup trailer at the right offset
1866
         if (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2)
1945
         if (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2)
1867
         {
1946
         {
1868
            startup_trailer_v2 = (startup_trailer_v2_t *) &filedata[current_offset + startup_header->startup_size - sizeof (startup_trailer_v2_t)];
1947
            startup_trailer_v2 = (startup_trailer_v2_t *) &filedata[current_offset + startup_header->startup_size - sizeof (startup_trailer_v2_t)];
Line 1876... Line 1955...
1876
 
1955
 
1877
         current_offset += sizeof (startup_header_t); // jump over the startup header and reach the startup blob
1956
         current_offset += sizeof (startup_header_t); // jump over the startup header and reach the startup blob
1878
         printf ("\n");
1957
         printf ("\n");
1879
         printf ("Startup blob at offset 0x%zx (%zd):\n", current_offset, current_offset);
1958
         printf ("Startup blob at offset 0x%zx (%zd):\n", current_offset, current_offset);
1880
         printf ("   size 0x%zx (%zd) bytes\n", startupfile_blobsize, startupfile_blobsize);
1959
         printf ("   size 0x%zx (%zd) bytes\n", startupfile_blobsize, startupfile_blobsize);
1881
         printf ("   checksum %d\n", update_checksum32 (0, (uint32_t *) &filedata[current_offset], startupfile_blobsize));
1960
         printf ("   checksum %d\n", update_checksum32 (0, &filedata[current_offset], startupfile_blobsize));
1882
 
1961
 
1883
         current_offset += startupfile_blobsize; // jump over the startup blob and reach the startup trailer
1962
         current_offset += startupfile_blobsize; // jump over the startup blob and reach the startup trailer
1884
         printf ("\n");
1963
         printf ("\n");
1885
         printf ("Startup trailer at offset 0x%zx (%zd) - version %d:\n", current_offset, current_offset, (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2 ? 2 : 1));
1964
         printf ("Startup trailer at offset 0x%zx (%zd) - version %d:\n", current_offset, current_offset, (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2 ? 2 : 1));
1886
         if (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2)
1965
         if (startup_header->flags1 & STARTUP_HDR_FLAGS1_TRAILER_V2)
1887
         {
1966
         {
1888
            for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++)
1967
            for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++)
1889
               sprintf (&recorded_sha512[2 * byte_index], "%02x", startup_trailer_v2->sha512[byte_index]);
1968
               sprintf (&recorded_sha512[2 * byte_index], "%02x", startup_trailer_v2->sha512[byte_index]);
1890
            strcpy (computed_sha512, SHA512 (startup_header, (size_t) ((uint8_t *) startup_trailer_v2 - (uint8_t *) startup_header), NULL));
1969
            strcpy (computed_sha512, SHA512 (startup_header, (size_t) ((uint8_t *) startup_trailer_v2 - (uint8_t *) startup_header), NULL));
1891
            recorded_checksum = startup_trailer_v2->cksum;
1970
            recorded_checksum = startup_trailer_v2->cksum;
1892
            computed_checksum = update_checksum32 (0, (uint32_t *) startup_header, sizeof (startup_header) + startupfile_blobsize + SHA512_DIGEST_LENGTH);
1971
            computed_checksum = update_checksum32 (0, startup_header, sizeof (startup_header) + startupfile_blobsize + SHA512_DIGEST_LENGTH);
1893
            printf ("    sha512 = %s - %s\n", recorded_sha512, (strcasecmp (computed_sha512, recorded_sha512) == 0 ? "GOOD" : "BAD"));
1972
            printf ("    sha512 = %s - %s\n", recorded_sha512, (strcasecmp (computed_sha512, recorded_sha512) == 0 ? "GOOD" : "BAD"));
1894
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
1973
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
1895
            if (strcasecmp (computed_sha512, recorded_sha512) != 0)
1974
            if (strcasecmp (computed_sha512, recorded_sha512) != 0)
1896
               printf ("Computed SHA-512: %s\n", computed_sha512);
1975
               printf ("Computed SHA-512: %s\n", computed_sha512);
1897
            if (computed_checksum != recorded_checksum)
1976
            if (computed_checksum != recorded_checksum)
1898
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
1977
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
1899
         }
1978
         }
1900
         else // old v1 trailer
1979
         else // old v1 trailer
1901
         {
1980
         {
1902
            recorded_checksum = startup_trailer_v1->cksum;
1981
            recorded_checksum = startup_trailer_v1->cksum;
1903
            computed_checksum = update_checksum32 (0, (uint32_t *) startup_header, sizeof (startup_header) + startupfile_blobsize);
1982
            computed_checksum = update_checksum32 (0, startup_header, sizeof (startup_header) + startupfile_blobsize);
1904
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
1983
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
1905
            if (computed_checksum != recorded_checksum)
1984
            if (computed_checksum != recorded_checksum)
1906
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
1985
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
1907
         }
1986
         }
1908
 
1987
 
Line 1942... Line 2021...
1942
         if (current_offset + image_header->image_size > filesize)
2021
         if (current_offset + image_header->image_size > filesize)
1943
         {
2022
         {
1944
            fprintf (stderr, "WARNING: this IFS file is too short (image trailer would be past end of file)\n");
2023
            fprintf (stderr, "WARNING: this IFS file is too short (image trailer would be past end of file)\n");
1945
            break;
2024
            break;
1946
         }
2025
         }
-
 
2026
 
-
 
2027
         // check if this endianness is ours
-
 
2028
         if (   ( (image_header->flags & IMAGE_FLAGS_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
-
 
2029
             || (!(image_header->flags & IMAGE_FLAGS_BIGENDIAN) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
-
 
2030
            is_foreign_endianness = true; // if the header is big endian and we're on a little endian machine, or the other way around, it's a foreign endianness
-
 
2031
         else
-
 
2032
            is_foreign_endianness = false; // else this header is for the same endianness as us
1947
 
2033
 
1948
         // locate the image trailer at the right offset
2034
         // locate the image trailer at the right offset
1949
         if (image_header->flags & IMAGE_FLAGS_TRAILER_V2)
2035
         if (image_header->flags & IMAGE_FLAGS_TRAILER_V2)
1950
         {
2036
         {
1951
            imagetrailer_offset = current_offset + image_header->image_size - sizeof (image_trailer_v2_t);
2037
            imagetrailer_offset = current_offset + image_header->image_size - sizeof (image_trailer_v2_t);
Line 2054... Line 2140...
2054
               printf ("   corresponding dirent index: %zd/%zd\n", fsentry_index, fsentry_count);
2140
               printf ("   corresponding dirent index: %zd/%zd\n", fsentry_index, fsentry_count);
2055
               printf ("   corresponding inode 0x%08x (%d) - %s%s%s%s\n", current_fsentry->header.ino, current_fsentry->header.ino, (current_fsentry->header.ino & 0xE0000000 ? "is" : "nothing special"), (current_fsentry->header.ino & IFS_INO_PROCESSED_ELF ? " PROCESSED_ELF" : ""), (current_fsentry->header.ino & IFS_INO_RUNONCE_ELF ? " RUNONCE_ELF" : ""), (current_fsentry->header.ino & IFS_INO_BOOTSTRAP_EXE ? " BOOTSTRAP_EXE" : ""));
2141
               printf ("   corresponding inode 0x%08x (%d) - %s%s%s%s\n", current_fsentry->header.ino, current_fsentry->header.ino, (current_fsentry->header.ino & 0xE0000000 ? "is" : "nothing special"), (current_fsentry->header.ino & IFS_INO_PROCESSED_ELF ? " PROCESSED_ELF" : ""), (current_fsentry->header.ino & IFS_INO_RUNONCE_ELF ? " RUNONCE_ELF" : ""), (current_fsentry->header.ino & IFS_INO_BOOTSTRAP_EXE ? " BOOTSTRAP_EXE" : ""));
2056
               printf ("   corresponding path: \"%s\"\n", (char *) &current_fsentry->u.file.path); // convert from pointer to char array
2142
               printf ("   corresponding path: \"%s\"\n", (char *) &current_fsentry->u.file.path); // convert from pointer to char array
2057
               printf ("   size 0x%zx (%zd) bytes\n", (size_t) current_fsentry->u.file.size, (size_t) current_fsentry->u.file.size);
2143
               printf ("   size 0x%zx (%zd) bytes\n", (size_t) current_fsentry->u.file.size, (size_t) current_fsentry->u.file.size);
2058
               printf ("   first 4 bytes: %02x%02x%02x%02x [%c%c%c%c] (%s)\n", (uint8_t) filedata[current_offset + 0], (uint8_t) filedata[current_offset + 1], (uint8_t) filedata[current_offset + 2], (uint8_t) filedata[current_offset + 3], (isprint (filedata[current_offset + 0]) ? filedata[current_offset + 0] : '.'), (isprint (filedata[current_offset + 1]) ? filedata[current_offset + 1] : '.'), (isprint (filedata[current_offset + 2]) ? filedata[current_offset + 2] : '.'), (isprint (filedata[current_offset + 3]) ? filedata[current_offset + 3] : '.'), (memcmp (&filedata[current_offset], "\x7f" "ELF", 4) == 0 ? "ELF binary" : (memcmp (&filedata[current_offset], "#!", 2) == 0 ? "shell script" : "???")));
2144
               printf ("   first 4 bytes: %02x%02x%02x%02x [%c%c%c%c] (%s)\n", (uint8_t) filedata[current_offset + 0], (uint8_t) filedata[current_offset + 1], (uint8_t) filedata[current_offset + 2], (uint8_t) filedata[current_offset + 3], (isprint (filedata[current_offset + 0]) ? filedata[current_offset + 0] : '.'), (isprint (filedata[current_offset + 1]) ? filedata[current_offset + 1] : '.'), (isprint (filedata[current_offset + 2]) ? filedata[current_offset + 2] : '.'), (isprint (filedata[current_offset + 3]) ? filedata[current_offset + 3] : '.'), (memcmp (&filedata[current_offset], "\x7f" "ELF", 4) == 0 ? "ELF binary" : (memcmp (&filedata[current_offset], "#!", 2) == 0 ? "shell script" : "???")));
2059
               printf ("   checksum %d\n", update_checksum32 (0, (uint32_t *) &filedata[current_offset], current_fsentry->u.file.size));
2145
               printf ("   checksum %d\n", update_image_checksum (0, &filedata[current_offset], current_fsentry->u.file.size, is_foreign_endianness));
2060
 
2146
 
2061
               current_offset += current_fsentry->u.file.size; // now jump over this file's data
2147
               current_offset += current_fsentry->u.file.size; // now jump over this file's data
2062
            }
2148
            }
2063
         }
2149
         }
2064
 
2150
 
Line 2073... Line 2159...
2073
         {
2159
         {
2074
            for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++)
2160
            for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++)
2075
               sprintf (&recorded_sha512[2 * byte_index], "%02x", image_trailer_v2->sha512[byte_index]);
2161
               sprintf (&recorded_sha512[2 * byte_index], "%02x", image_trailer_v2->sha512[byte_index]);
2076
            strcpy (computed_sha512, SHA512 (image_header, (size_t) ((uint8_t *) image_trailer_v2 - (uint8_t *) image_header), NULL));
2162
            strcpy (computed_sha512, SHA512 (image_header, (size_t) ((uint8_t *) image_trailer_v2 - (uint8_t *) image_header), NULL));
2077
            recorded_checksum = image_trailer_v2->cksum;
2163
            recorded_checksum = image_trailer_v2->cksum;
2078
            computed_checksum = update_checksum32 (0, (uint32_t *) image_header, sizeof (image_header) + image_header->image_size - sizeof (image_trailer_v2_t) + SHA512_DIGEST_LENGTH);
2164
            computed_checksum = update_image_checksum (0, image_header, /*sizeof (image_header) +*/ image_header->image_size - sizeof (image_trailer_v2_t) + SHA512_DIGEST_LENGTH, is_foreign_endianness);
2079
            printf ("    sha512 = %s - %s\n", recorded_sha512, (strcasecmp (computed_sha512, recorded_sha512) == 0 ? "GOOD" : "BAD"));
2165
            printf ("    sha512 = %s - %s\n", recorded_sha512, (strcasecmp (computed_sha512, recorded_sha512) == 0 ? "GOOD" : "BAD"));
2080
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
2166
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
2081
            if (strcasecmp (computed_sha512, recorded_sha512) != 0)
2167
            if (strcasecmp (computed_sha512, recorded_sha512) != 0)
2082
               printf ("Computed SHA-512: %s\n", computed_sha512);
2168
               printf ("Computed SHA-512: %s\n", computed_sha512);
2083
            if (computed_checksum != recorded_checksum)
2169
            if (computed_checksum != recorded_checksum)
2084
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
2170
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
2085
         }
2171
         }
2086
         else // old v1 trailer
2172
         else // old v1 trailer
2087
         {
2173
         {
2088
            recorded_checksum = image_trailer_v1->cksum;
2174
            recorded_checksum = image_trailer_v1->cksum;
2089
            computed_checksum = update_checksum32 (0, (uint32_t *) image_header, sizeof (image_header) + image_header->image_size - sizeof (image_trailer_v1_t));
2175
            computed_checksum = update_image_checksum (0, image_header, /*sizeof (image_header) +*/ image_header->image_size - sizeof (image_trailer_v1_t), is_foreign_endianness);
2090
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
2176
            printf ("    cksum = 0x%08x (%d) - %s\n", recorded_checksum, recorded_checksum, (computed_checksum == recorded_checksum ? "GOOD" : "BAD"));
2091
            if (computed_checksum != recorded_checksum)
2177
            if (computed_checksum != recorded_checksum)
2092
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
2178
               printf ("Computed cksum: 0x%08x (%d)\n", computed_checksum, computed_checksum);
2093
         }
2179
         }
2094
 
2180
 
Line 2107... Line 2193...
2107
            break; // if not found, stop scanning
2193
            break; // if not found, stop scanning
2108
 
2194
 
2109
         bootfile_blobsize = byte_index - current_offset;
2195
         bootfile_blobsize = byte_index - current_offset;
2110
         printf ("Boot blob at offset 0x%zx (%zd):\n", current_offset, current_offset);
2196
         printf ("Boot blob at offset 0x%zx (%zd):\n", current_offset, current_offset);
2111
         printf ("   size 0x%zx (%zd) bytes\n", bootfile_blobsize, bootfile_blobsize);
2197
         printf ("   size 0x%zx (%zd) bytes\n", bootfile_blobsize, bootfile_blobsize);
2112
         printf ("   checksum %d\n", update_checksum32 (0, (uint32_t *) &filedata[current_offset], bootfile_blobsize));
2198
         printf ("   checksum 0x%08x\n", update_checksum32 (0, &filedata[current_offset], bootfile_blobsize));
2113
 
2199
 
2114
         current_offset = byte_index; // now reach the next segment
2200
         current_offset = byte_index; // now reach the next segment
2115
      }
2201
      }
2116
   }
2202
   }
2117
 
2203