Advertisement
Guest User

Syscon FW reader [FIX]

a guest
Jan 12th, 2012
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include "tools.h"
  2. #include "types.h"
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10.  
  11. u8 *pkg = NULL;
  12. static u64 dec_size;
  13. static u32 meta_offset;
  14. static u32 n_sections;
  15.  
  16. static void unpack_firmware(const char *name)
  17. {
  18.     u8 *tmp;
  19.     u8 *decompressed;
  20.     u64 offset;
  21.     u64 size;
  22.     u64 size_real;
  23.  
  24.     tmp = pkg + meta_offset + 0x80 + 0x30 * 2;
  25.  
  26.  
  27.     offset = be64(tmp);
  28.     size = be64(tmp + 8);
  29.     size_real = dec_size - 0x80;
  30.  
  31.     printf("[FW size] %u Bytes\n", (u32)size);
  32.  
  33.     if (be32(tmp + 0x2c) == 0x2) {
  34.         decompressed = malloc(size_real);
  35.         memset(decompressed, 0xaa, size_real);
  36.  
  37.         decompress(pkg + offset, size, decompressed, size_real);
  38.  
  39.         memcpy_to_file(name, decompressed, size_real);
  40.     } else {
  41.         memcpy_to_file(name, pkg + offset, size);
  42.     }
  43. }
  44.  
  45.  
  46. static void decrypt_pkg(void)
  47. {
  48.     int j;
  49.  
  50.     u16 flags;
  51.     u16 type;
  52.     u32 hdr_len;
  53.  
  54.     u32 PatchID1;
  55.     u32 PatchID2;
  56.     u16 SoftID;
  57.  
  58.     struct keylist *k;
  59.  
  60.     flags    = be16(pkg + 0x08);
  61.     type     = be16(pkg + 0x0a);
  62.     hdr_len  = be64(pkg + 0x10);
  63.     dec_size = be64(pkg + 0x18);
  64.     SoftID   = be16(pkg + 0x28e);
  65.     PatchID1 = be32(pkg + 0x290);
  66.     PatchID2 = be32(pkg + 0x294);
  67.  
  68.     for(j=0;j<11;j++){
  69.         if(SoftID==Get_Soft_ID(j)){
  70.             printf("[Gen  %02i]", j+1);
  71.             if(j<8) printf("[Phat]\n");
  72.             else    printf("[Slim]\n");
  73.  
  74.             break;
  75.         }if(j==10){
  76.             printf("[New Gen][Slim]\n");
  77.         }
  78.     }
  79.  
  80.  
  81.     printf("[Soft ID] %04X\n", SoftID);
  82.     printf("[PatchID] %08X%08X\n", PatchID1, PatchID2);
  83.  
  84.     if (type != 3)
  85.         fail("not a .pkg file");
  86.  
  87.     k = keys_get(KEY_PKG);
  88.  
  89.     if (k == NULL)
  90.         fail("no key found");
  91.  
  92.     if (sce_decrypt_header(pkg, k) < 0)
  93.         fail("pkg header decryption failed");
  94.  
  95.     if (sce_decrypt_data(pkg) < 0)
  96.         fail("pkg data decryption failed");
  97.  
  98.     meta_offset = be32(pkg + 0x0c);
  99.     n_sections  = be32(pkg + meta_offset + 0x60 + 0xc);
  100.  
  101.     if (n_sections != 3)
  102.         fail("invalid section count: %d", n_sections);
  103.  
  104. }
  105.  
  106. u16 Get_Soft_ID(int i){
  107.     u16 sc_soft_id[11];
  108.     sc_soft_id[ 0] = 0x0B8E;
  109.     sc_soft_id[ 1] = 0x0C16;
  110.     sc_soft_id[ 2] = 0x0D52;
  111.     sc_soft_id[ 3] = 0x0DBF;
  112.     sc_soft_id[ 4] = 0x0E69;
  113.     sc_soft_id[ 5] = 0x0F29;
  114.     sc_soft_id[ 6] = 0x0F38;
  115.     sc_soft_id[ 7] = 0x065D;
  116.     sc_soft_id[ 8] = 0x0832;
  117.     sc_soft_id[ 9] = 0x08C2;
  118.     sc_soft_id[10] = 0x0918;
  119.  
  120.     return sc_soft_id[i];
  121. }
  122.  
  123. void readFW(void)
  124. {
  125.     u32 sc_header = 0x1b2d700f ;
  126.  
  127.     u32 hdr;
  128.     u32 A[8],D[3];
  129.  
  130.     hdr = be32(pkg);
  131.  
  132.     A[0]    = be32(pkg + 0x4);
  133.     A[1]    = be32(pkg + 0x8);
  134.     A[2]    = be32(pkg + 0xc);
  135.     A[3]    = be32(pkg + 0x10);
  136.     A[4]    = be32(pkg + 0x14);
  137.     A[5]    = be32(pkg + 0x18);
  138.     A[6]    = be32(pkg + 0x1c);
  139.     A[7]    = be32(pkg + 0x20);
  140.  
  141. //  This Offset is static.. i don't know what it is...
  142.     D[0]    = be32(pkg + 0x24);
  143.     D[1]    = be32(pkg + 0x28);
  144.     D[2]    = be32(pkg + 0x2c);
  145.  
  146.     if(hdr!=sc_header)
  147.         printf("[New HDR] %08x \n", hdr);
  148.     else
  149.         printf("[SC  HDR] %08x \n", hdr);
  150.  
  151.     printf("[CHKSUM?] %08x %08x %08x %08x \n          %08x %08x %08x %08x\n", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7]);
  152. //  printf("[   B   ] [HEX] %08x\n        [DEC] %u\n", B, B);
  153.     printf("[?static] %08x%08x%08x\n", D[0], D[1], D[2]);
  154.  
  155.  
  156. }
  157.  
  158. int main(int argc, char *argv[])
  159. {
  160.     if (argc == 2) {
  161.         pkg = mmap_file(argv[1]);
  162.         printf("\n[Name FW] %s\n", argv[1]);
  163.         decrypt_pkg();
  164.         unpack_firmware("temp.sc");
  165.         pkg = mmap_file("temp.sc");
  166.         readFW();
  167.  
  168.         if(remove("temp.sc") != 0)
  169.             fail("Error deleting temp file.");
  170.  
  171.     } else {
  172.         fail("usage: unpkg syscon_firmware.pkg");
  173.     }
  174.  
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement