Advertisement
Guest User

SC FW reader

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