XboxHacker BBS
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 08:21:18 AM


Login with username, password and session length


Pages: 1
  Print  
Author Topic: Patching King Kong Disk  (Read 1844 times)
is0-mick
Master Hacker
****
Posts: 108


View Profile
« on: March 04, 2007, 07:34:35 AM »

Im trying to compile patcher.c

now is this to be done under linux? or Visual C? or what?

I tried this under cygwin, but get errors... same with gcc also.

$ cc -o patcher patcher.c
patcher.c:17: error: parse error before "off64_t"
patcher.c: In function `patch_filename':
patcher.c:19: error: `offset' undeclared (first use in this function)
patcher.c:19: error: (Each undeclared identifier is reported only once
patcher.c:19: error: for each function it appears in.)
patcher.c:20: error: `fd' undeclared (first use in this function)
patcher.c:27: error: `orig' undeclared (first use in this function)
patcher.c: At top level:
patcher.c:52: error: parse error before "off64_t"
patcher.c: In function `patch_shader':
patcher.c:56: error: `offset' undeclared (first use in this function)
patcher.c:68: error: `fd' undeclared (first use in this function)
patcher.c:146: error: `off64_t' undeclared (first use in this function)
patcher.c:146: error: parse error before "x"
patcher.c:189: error: `x' undeclared (first use in this function)
patcher.c: In function `main':
patcher.c:208: error: `O_LARGEFILE' undeclared (first use in this function)


could you not just replace one of the king shader files with shader.bin from the crwl360loader directory using wx360 or somthing?
or does shader.bin need to be inserted into one of the existing shader files?

either that or could someone compile this for windows?

Thanks
Mick

Logged
kalle_19
Member
**
Posts: 44


View Profile
« Reply #1 on: March 04, 2007, 08:54:47 AM »

The original should work in linux.

Maybe intresting if you're on cygwin: http://sourceware.org/ml/newlib/2003/msg00150.html

Anyhow, I think I got it working on os x, if anyone is interested:
Code:
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define OFFSET_PARTITION_1 0xFD90000ULL

#define OFFSET_FILENAME_1  (OFFSET_PARTITION_1 + 0x06AABECEAULL)
#define OFFSET_FILENAME_2  (OFFSET_PARTITION_1 + 0x06AABED06ULL)
#define OFFSET_FILENAME_3  (OFFSET_PARTITION_1 + 0x06AABED22ULL)

#define OFFSET_PAYLOAD_0   (OFFSET_PARTITION_1 + 0x087004800ULL)
#define OFFSET_SHADERS     (OFFSET_PARTITION_1 + 0x09E8E8000ULL)

void patch_filename(int fd, int64_t offset, char orig)
{
printf("patching filename at %llx...", (long long) offset); fflush(stdout);
if (lseek(fd, offset, SEEK_SET) != offset)
{
fprintf(stderr, "seek failed: %m");
exit(1);
}
char r;
read(fd, &r, 1);
if (r != orig)
{
fprintf(stderr, "patch failed: expected %02x and found %02x\n", r, orig);
exit(1);
}

lseek(fd, offset, SEEK_SET);
r++;

if (write(fd, &r, 1) != 1)
{
fprintf(stderr, "write failed: %m");
exit(1);
}
printf("done.\n");
}


struct bla {
unsigned char id[4];
int r0, num_files, num_shaders, nametable_off, other_off;
};

unsigned long ref[1024];

void patch_shader(int fd, int64_t offset)
{
struct bla hdr;

printf("patching shader at %llx...\n", (long long)offset);
FILE *f = fopen("shader.bin", "rb");
if (!f)
{
perror("shader.bin");
exit(1);
}
int shader_size = fread(ref, 1, 1024*4, f);
fclose(f);

printf("hack shader size: %d\n", shader_size);

if (lseek(fd, offset, SEEK_SET) != offset)
{
fprintf(stderr, "seek failed - %m\n");
exit(1);
}

read(fd, &hdr, sizeof(hdr));

if (memcmp(hdr.id, "2BDS", 4))
{
fprintf(stderr, "incorrect shader database ID\n");
exit(1);
}

hdr.r0 = ntohl(hdr.r0);
hdr.num_files = ntohl(hdr.num_files);
hdr.num_shaders = ntohl(hdr.num_shaders);

hdr.nametable_off = ntohl(hdr.nametable_off);
hdr.other_off = ntohl(hdr.other_off);

lseek(fd, offset + hdr.nametable_off, SEEK_SET);

int i;

if ((ref[0]&0xFFFFFF00) == 0x102a0e00)
for (i=0; i<shader_size/sizeof(*ref); ++i)
ref[i] = htonl(ref[i]);


if ((ref[0]&0x00FFFFFF) != 0x000e2a10)
{
fprintf(stderr, "hack shader has wrong magic\n");
exit(1);
}

for (i=0; i<hdr.num_files; ++i)
{
struct file
{
char filename[0x3c];
int z0, z1, z2, z3, z4;
} file;

if (read(fd, &file, sizeof(file)) != sizeof(file))
break;

file.z4 = htonl(file.z4);
lseek(fd, file.z4, SEEK_CUR);
}

lseek(fd, offset + hdr.other_off, SEEK_SET);

printf("num shaders: %08x\n", hdr.num_shaders);

union entry_u
{
int array[9];
struct
{
int type, zero, v1, v2, v3, v4, v5, offset, len;
};
};

int shader_offset = 0, shader_len;
int shader_data[9];

int j;
for (j=0; j< hdr.num_shaders; ++j)
{
union entry_u entry;

if (read(fd, &entry, sizeof(entry)) != sizeof(entry))
break;
memcpy(shader_data, &entry, sizeof(entry));
for (i=0; i<9; ++i)
entry.array[i] = ntohl(entry.array[i]);

int64_t x = lseek(fd, 0, SEEK_CUR);

lseek(fd, offset + entry.offset, SEEK_SET);
unsigned char hexdump[16];
read(fd, hexdump, 16);

if (entry.type == 1)
{
int l1, l2;
l1 = htonl(*(long*)hexdump);
l2 = htonl(*(long*)(hexdump + 4));

printf("%08x %08x\n", l1, l2);

if ((l1 >= shader_size) && (l2 >= shader_size))
{
lseek(fd, offset + entry.offset + 8, SEEK_SET);
read(fd, hexdump, 4);
if (memcmp(hexdump, "\x10\x2A\x0E\x01", 4))
{
printf("!!! does not fit\n");
exit(1);
}
lseek(fd, offset + entry.offset + 8 + l1, SEEK_SET);
read(fd, hexdump, 4);
if (memcmp(hexdump, "\x10\x2A\x0E\x01", 4))
{
printf("!!! does not fit\n");
exit(1);
}

if (j < 100)
{
printf("patch nr %d (%08x %08x %08x)\n", j, l1, l2, shader_size);
lseek(fd, offset + entry.offset + 8, SEEK_SET);
write(fd, ref, shader_size);
}

shader_offset = entry.offset;
shader_len = entry.len;
}
}

lseek(fd, x, SEEK_SET);

}

if (!shader_offset)
{
fprintf(stderr, "fyy. no suitable shader found\n");
exit(1);
}
}

int main(int argc, char **argv)
{
int fd;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <kk.000>\n", *argv);
return 1;
}
fd = open(argv[1], O_RDWR);
if (fd < 0)
{
perror(argv[1]);
return 2;
}

patch_filename(fd, OFFSET_FILENAME_1, 'U');
patch_filename(fd, OFFSET_FILENAME_2, 'U');
patch_filename(fd, OFFSET_FILENAME_3, 'W');
patch_shader(fd, OFFSET_SHADERS);

close(fd);

printf("Patch OK.\n");

return 0;
}
Logged
warpjavier
Master Hacker
****
Posts: 108


View Profile
« Reply #2 on: March 04, 2007, 03:13:37 PM »

This is the code I modify to have it compile under windows with GCC 4.1.1
Code:
#define _LARGEFILE64_SOURCE
#define O_LARGEFILE 0100000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define OFFSET_PARTITION_1 0xFD90000ULL

#define OFFSET_FILENAME_1  (OFFSET_PARTITION_1 + 0x06AABECEAULL)
#define OFFSET_FILENAME_2  (OFFSET_PARTITION_1 + 0x06AABED06ULL)
#define OFFSET_FILENAME_3  (OFFSET_PARTITION_1 + 0x06AABED22ULL)

#define OFFSET_PAYLOAD_0   (OFFSET_PARTITION_1 + 0x087004800ULL)
#define OFFSET_SHADERS     (OFFSET_PARTITION_1 + 0x09E8E8000ULL)

#define SWAP32(val) \
   ((unsigned int) ( \
    (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
    (((unsigned int) (val) & (unsigned int) 0x0000ff00U) <<  8) | \
    (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >>  8) | \
    (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))


void patch_filename(int fd, off64_t offset, char orig)
{
printf("patching filename at %llx...", (long long) offset); fflush(stdout);
if (lseek64(fd, offset, SEEK_SET) != offset)
{
fprintf(stderr, "seek failed: %m");
exit(1);
}
char r;
read(fd, &r, 1);
if (r != orig)
{
fprintf(stderr, "patch failed: expected %02x and found %02x\n", r, orig);
exit(1);
}

lseek64(fd, offset, SEEK_SET);
r++;

if (write(fd, &r, 1) != 1)
{
fprintf(stderr, "write failed: %m");
exit(1);
}
printf("done.\n");
}


struct bla {
unsigned char id[4];
int r0, num_files, num_shaders, nametable_off, other_off;
};

unsigned long ref[1024];

void patch_shader(int fd, off64_t offset)
{
struct bla hdr;

printf("patching shader at %llx...\n", (long long)offset);
FILE *f = fopen("shader.bin", "rb");
if (!f)
{
perror("shader.bin");
exit(1);
}
int shader_size = fread(ref, 1, 1024*4, f);
fclose(f);

printf("hack shader size: %d\n", shader_size);

if (lseek64(fd, offset, SEEK_SET) != offset)
{
fprintf(stderr, "seek failed - %m\n");
exit(1);
}

read(fd, &hdr, sizeof(hdr));

if (memcmp(hdr.id, "2BDS", 4))
{
fprintf(stderr, "incorrect shader database ID\n");
exit(1);
}

hdr.r0 = SWAP32(hdr.r0);//ntohl(hdr.r0);
hdr.num_files = SWAP32(hdr.num_files);//ntohl(hdr.num_files);
hdr.num_shaders = SWAP32(hdr.num_shaders);//ntohl(hdr.num_shaders);

hdr.nametable_off = SWAP32(hdr.nametable_off);//ntohl(hdr.nametable_off);
hdr.other_off = SWAP32(hdr.other_off);//ntohl(hdr.other_off);

lseek64(fd, offset + hdr.nametable_off, SEEK_SET);

int i;

if ((ref[0]&0xFFFFFF00) == 0x102a0e00)
for (i=0; i<shader_size/sizeof(*ref); ++i)
ref[i] = SWAP32(ref[i]);//htonl(ref[i]);


if ((ref[0]&0x00FFFFFF) != 0x000e2a10)
{
fprintf(stderr, "hack shader has wrong magic\n");
exit(1);
}

for (i=0; i<hdr.num_files; ++i)
{
struct file
{
char filename[0x3c];
int z0, z1, z2, z3, z4;
} file;

if (read(fd, &file, sizeof(file)) != sizeof(file))
break;

file.z4 = SWAP32(file.z4);//htonl(file.z4);
lseek64(fd, file.z4, SEEK_CUR);
}

lseek64(fd, offset + hdr.other_off, SEEK_SET);

printf("num shaders: %08x\n", hdr.num_shaders);

union entry_u
{
int array[9];
struct
{
int type, zero, v1, v2, v3, v4, v5, offset, len;
};
};

int shader_offset = 0, shader_len;
int shader_data[9];

int j;
for (j=0; j< hdr.num_shaders; ++j)
{
union entry_u entry;

if (read(fd, &entry, sizeof(entry)) != sizeof(entry))
break;
memcpy(shader_data, &entry, sizeof(entry));
for (i=0; i<9; ++i)
entry.array[i] = SWAP32(entry.array[i]);//ntohl(entry.array[i]);

off64_t x = lseek64(fd, 0, SEEK_CUR);

lseek64(fd, offset + entry.offset, SEEK_SET);
unsigned char hexdump[16];
read(fd, hexdump, 16);

if (entry.type == 1)
{
int l1, l2;
l1 = SWAP32(*(long*)hexdump);//htonl(*(long*)hexdump);
l2 = SWAP32(*(long*)(hexdump + 4));//htonl(*(long*)(hexdump + 4));

printf("%08x %08x\n", l1, l2);

if ((l1 >= shader_size) && (l2 >= shader_size))
{
lseek64(fd, offset + entry.offset + 8, SEEK_SET);
read(fd, hexdump, 4);
if (memcmp(hexdump, "\x10\x2A\x0E\x01", 4))
{
printf("!!! does not fit\n");
exit(1);
}
lseek64(fd, offset + entry.offset + 8 + l1, SEEK_SET);
read(fd, hexdump, 4);
if (memcmp(hexdump, "\x10\x2A\x0E\x01", 4))
{
printf("!!! does not fit\n");
exit(1);
}

if (j < 100)
{
printf("patch nr %d (%08x %08x %08x)\n", j, l1, l2, shader_size);
lseek64(fd, offset + entry.offset + 8, SEEK_SET);
write(fd, ref, shader_size);
}

shader_offset = entry.offset;
shader_len = entry.len;
}
}

lseek64(fd, x, SEEK_SET);

}

if (!shader_offset)
{
fprintf(stderr, "ouch. no suitable shader found\n");
exit(1);
}
}

int main(int argc, char **argv)
{
int fd;
if (argc != 2)
{
fprintf(stderr, "usage: %s <kk.000>\n", *argv);
return 1;
}
fd = open(argv[1], O_RDWR | O_LARGEFILE );
if (fd < 0)
{
perror(argv[1]);
return 2;
}

patch_filename(fd, OFFSET_FILENAME_1, 'U');
patch_filename(fd, OFFSET_FILENAME_2, 'U');
patch_filename(fd, OFFSET_FILENAME_3, 'W');
patch_shader(fd, OFFSET_SHADERS);

close(fd);

printf("patch ok.\n");

return 0;
}

The code compiles ok, and it should work.
I'm still not 100% sure about ntohl and htonl functions, I read the documentation and only seems to swap the bit order, so I have SWAP32 macro included as ntohl nor htonl compile under GCC for me.

Any suggestion will be appreciated.

warpjavier
Logged

Internet Explorer is only useful to download Firefox.
is0-mick
Master Hacker
****
Posts: 108


View Profile
« Reply #3 on: March 05, 2007, 01:36:37 PM »

I got it to compile with the help of a friend (thanks AS ) under cygwin


replace all instances of int64_t with long long

replace all lseek64 with lseek

add

#define O_LARGEFILE 1

It seemed to work and patched the king image.000

now when i boot with the new image and serial connected i get a Xe> prompt Smiley


Logged
Pages: 1
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM