Silicaly

Untitled

Feb 10th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1.  
  2. extern "C" __declspec(dllexport) int SceVerPatch(char *file, uint32_t version)
  3. {
  4.     uint32_t fw_num = version;
  5.     printf("Setting to fw to: %x\n", fw_num);
  6.     printf("Opening: %s\n", file);
  7.     FILE *fp = fopen(file, "r+b");
  8.     if (!fp)
  9.         return (int)fp;
  10.     Elf64_Ehdr ehdr;
  11.     fread(&ehdr, sizeof(ehdr), 1, fp);
  12.  
  13.     printf("Checking if ELF...\n");
  14.     char elf_magic[8] = { 0x7F, 0x45, 0x4C, 0x46, 0x02, 0x01, 0x01, 0x09 };
  15.  
  16.     if (memcmp(&ehdr.e_ident, &elf_magic, 8))
  17.         return 0xFFFFF;
  18.  
  19.     Elf64_Phdr *phdrs = (Elf64_Phdr*)malloc(ehdr.e_phentsize * ehdr.e_phnum);
  20.     fseek(fp, ehdr.e_phoff, SEEK_SET);
  21.     fread(phdrs, ehdr.e_phentsize * ehdr.e_phnum, 1, fp);
  22.     for (int i = 0; i < ehdr.e_phnum; ++i) {
  23.         if (phdrs[i].p_type == 0x61000002 || phdrs[i].p_type == 0x61000001) {
  24.             char *sce_process_param = (char*)malloc(phdrs[i].p_filesz);
  25.             printf("sce_process_param patch\n");
  26.             fseek(fp, phdrs[i].p_offset, SEEK_SET);
  27.             fread(sce_process_param, phdrs[i].p_filesz, 1, fp);
  28.             uint32_t *req_ver = (uint32_t *)sce_process_param + 0x10;
  29.             printf("Original version: %x\n", *req_ver);
  30.             *req_ver = fw_num;
  31.             printf("Writing version: %x\n", *req_ver);
  32.             fseek(fp, phdrs[i].p_offset, SEEK_SET);
  33.             fwrite(sce_process_param, phdrs[i].p_filesz, 1, fp);
  34.             free(sce_process_param);
  35.         }
  36.         if (phdrs[i].p_type == 0x6fffff01) {
  37.             printf("sce_version patch\n");
  38.             char *sce_version = (char*)malloc(phdrs[i].p_filesz);
  39.             fseek(fp, phdrs[i].p_offset, SEEK_SET);
  40.             fread(sce_version, phdrs[i].p_filesz, 1, fp);
  41.  
  42.             size_t offset = 0;
  43.             while (offset < phdrs[i].p_filesz) {
  44.                 int sz = *(sce_version + offset);
  45.                 char name[128] = {};
  46.                 memcpy(name, sce_version + offset + 1, sz - 0x4);
  47.                 printf("Library name: %s\n", name, sz);
  48.                 uint32_t *req_ver = (uint32_t *)sce_version + offset + 1 + sz - 0x4;
  49.                 printf("Original version: %x\n", _byteswap_ulong(*req_ver));
  50.                 *req_ver = _byteswap_ulong(fw_num);
  51.                 printf("Setting version: %x\n", _byteswap_ulong(*req_ver));
  52.                 offset += sz + 1;
  53.             }
  54.  
  55.  
  56.             printf("Writing sce_version...\n");
  57.             fseek(fp, phdrs[i].p_offset, SEEK_SET);
  58.             fwrite(sce_version, phdrs[i].p_filesz, 1, fp);
  59.             free(sce_version);
  60.         }
  61.  
  62.  
  63.     }
  64.  
  65.     return 1;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment