// File: /workspace/github_c_makefile_star2_O1/jogolden[P]ps4flashtool/southbridge.c
// Function: decrypt_decompress_eap_kernel
int decrypt_decompress_eap_kernel(unsigned char *eapkernel, unsigned int eaplength, unsigned char **data, unsigned int *length) {
    struct eap_kernel_storage_hdr *shdr = (struct eap_kernel_storage_hdr *)eapkernel;
    struct eap_kernel_hdr *hdr;
    unsigned char *ptr, *output;
    unsigned int len, outlen;

    if(shdr->magic != EAP_KERNEL_STORAGE_HEADER_MAGIC) {
        printf("error: invalid EAP kernel storage header magic\n");
        return 1;
    }

    ptr = (unsigned char *)malloc(eaplength);
    if(!ptr) {
        printf("error: could not allocate memory for EAP kernel\n");
        return 1;
    }

    memcpy(ptr, eapkernel, eaplength);

    memset(random_seeds, 0, sizeof(random_seeds));
    init_eap_keys();

    // decrypt
    len = 512 - 0x2C;
    len -= len % 0x10;
    aes128_cbc_decrypt(eap_keys[1], ptr + 0x2C, ptr + 0x2C, len, shdr->iv);
    hdr = (struct eap_kernel_hdr *)(ptr + 0x2C);

    if(hdr->magic != EAP_KERNEL_HEADER_MAGIC) {
        printf("error: invalid EAP kernel header magic\n");
        return 1;
    }

    // decrypt
    len = hdr->length;
    len -= hdr->length % 0x10;
    aes128_cbc_decrypt(eap_keys[1], ptr + 0x2C, ptr + 0x2C, len, shdr->iv);

    printf("warning: EAP kernel decryption and decompression will remove the decompressor part of the EAP kernel loading\n");

    // decompress
    output = (unsigned char *)malloc(len);
    outlen = len;
    do {
        outlen *= 2; // resize buffer
        output = realloc(output, outlen);
        len = arzl_decompress(output, outlen, ptr + 0x1D30, NULL);
    } while (len == 0x80560201); // out of space

    arzl_deobfuscate(output, len, 2);

    free(ptr);

    if(data) {
        *data = output;
    } else {
        free(output);
    }

    if(length) {
        *length = len;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x12ebc95c, (%rdi)     # imm = 0x12EBC95C
jne 0x709b
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %r12
movl %esi, %r15d
movq %r15, %rdi
callq 0x2140
testq %rax, %rax
je 0x70a4
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2130
leaq 0x5415(%rip), %rdi      # 0xc3e0
movl $0x9c4, %edx            # imm = 0x9C4
xorl %esi, %esi
callq 0x20d0
callq 0x6dd5
leaq 0x2c(%rbx), %r15
addq $0x8, %r12
leaq 0x5385(%rip), %rdi      # 0xc370
movq %r15, %rsi
movq %r15, %rdx
movl $0x1d0, %ecx            # imm = 0x1D0
movq %r12, %r8
callq 0x29fc
cmpl $0x4b726e00, 0x2c(%rbx) # imm = 0x4B726E00
jne 0x70ad
movq %r13, (%rsp)
movl 0x30(%rbx), %r13d
andl $-0x10, %r13d
leaq 0x5352(%rip), %rdi      # 0xc370
movq %r15, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq %r12, %r8
callq 0x29fc
leaq 0x267c(%rip), %rdi      # 0x96b2
callq 0x2060
movq %r13, %rdi
callq 0x2140
movq %rax, %r12
movq %rbx, %r15
addq $0x1d30, %r15           # imm = 0x1D30
addl %r13d, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x2160
movq %rax, %r12
movq %rax, %rdi
movl %r13d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x2abc
cmpl $0x80560201, %eax       # imm = 0x80560201
je 0x7050
movl %eax, %ebp
movq %r12, %rdi
movl %eax, %esi
movl $0x2, %edx
callq 0x3371
movq %rbx, %rdi
callq 0x2030
testq %r14, %r14
je 0x70cd
movq %r12, (%r14)
jmp 0x70d5
leaq 0x26a4(%rip), %rdi      # 0x9746
jmp 0x70b4
leaq 0x25d7(%rip), %rdi      # 0x9682
jmp 0x70b4
leaq 0x266b(%rip), %rdi      # 0x971f
callq 0x2060
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x2030
movq (%rsp), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x70be
movl %ebp, (%rcx)
jmp 0x70be