Skip to content

Commit

Permalink
add patcher for fdl1
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKing062 committed Jul 31, 2024
1 parent 9853567 commit adf2709
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 79 additions & 0 deletions gen_fdl1-dl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define ERR_EXIT(...) \
do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)

static uint8_t* loadfile(const char* fn, size_t* num, size_t extra) {
size_t n, j = 0; uint8_t* buf = 0;
FILE* fi = fopen(fn, "rb");
if (fi) {
fseek(fi, 0, SEEK_END);
n = ftell(fi);
if (n) {
fseek(fi, 0, SEEK_SET);
buf = (uint8_t*)malloc(n + extra);
if (buf) j = fread(buf, 1, n, fi);
}
fclose(fi);
}
if (num) *num = j;
return buf;
}

int main(int argc, char** argv)
{
if (argc < 2) ERR_EXIT("Usage: %s <filename>\n", argv[0]);

char* filename = argv[1];
uint8_t* mem; size_t size = 0;
mem = loadfile(filename, &size, 0);
if (!mem) ERR_EXIT("loadfile(\"%s\") failed\n", filename);
if ((uint64_t)size >> 32) ERR_EXIT("file too big\n");

if (!(*(uint32_t*)&mem[0x30])) ERR_EXIT("The file is not sprd trusted firmware\n");
if (*(uint32_t*)&mem[0x30] + 0x200 > size) ERR_EXIT("The file is not complete\n");
if (*(uint32_t*)&mem[0x30] != *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x10])
size = *(uint32_t*)&mem[0x30] + 0x200;
else if (*(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x50])
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x50] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x58];
else if (*(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x30])
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x30] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x38];
else
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x20] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x28];
printf("0x%zx\n", size);

FILE* file = fopen("temp", "wb");
if (file == NULL) ERR_EXIT("Failed to create the file.\n");
size_t bytes_written = fwrite(mem, sizeof(unsigned char), size, file);
if (bytes_written != size) ERR_EXIT("Failed to write the file.\n");
fclose(file);

if (remove(filename)) ERR_EXIT("Failed to delete the file.\n");
if (rename("temp", filename)) ERR_EXIT("Failed to rename the file.\n");

size_t offset = 0x200;
while (offset < *(uint32_t*)&mem[0x30] + 0x200)
{
if (*(uint32_t*)&mem[offset] == 0x34000040)
if ((*(uint32_t*)&mem[offset - 4]) >> 8 == 0x940000)
if (*(uint32_t*)&mem[offset + 4] == 0x14000000)
{
*(uint32_t*)&mem[offset - 4] = 0xD503201F;
*(uint32_t*)&mem[offset] = 0xD503201F;
*(uint32_t*)&mem[offset + 4] = 0xD503201F;
offset += 4;
}
offset += 4;
}
file = fopen("fdl1-dl.bin", "wb");
if (file == NULL) ERR_EXIT("Failed to create the file.\n");
bytes_written = fwrite(mem, sizeof(unsigned char), size, file);
if (bytes_written != size) ERR_EXIT("Failed to write the file.\n");
fclose(file);

free(mem);

return 0;
}
2 changes: 1 addition & 1 deletion spl_patcher.c → gen_spl-unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main(int argc, char** argv)
}
offset += 4;
}
file = fopen("spl-patched.bin", "wb");
file = fopen("spl-unlock.bin", "wb");
if (file == NULL) ERR_EXIT("Failed to create the file.\n");
bytes_written = fwrite(mem, sizeof(unsigned char), size, file);
if (bytes_written != size) ERR_EXIT("Failed to write the file.\n");
Expand Down

0 comments on commit adf2709

Please sign in to comment.