Advertisement
Guest User

Syscon FW reader [FIX 2]

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