// File: /workspace/github_c_makefile_star2_O2/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 0x6dad
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %r12
movl %esi, %r15d
movq %r15, %rdi
callq 0x2150
testq %rax, %rax
je 0x6db6
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2140
leaq 0x4701(%rip), %rdi      # 0xb3e0
movl $0x9c4, %edx            # imm = 0x9C4
xorl %esi, %esi
callq 0x20d0
callq 0x6ad8
leaq 0x2c(%rbx), %r15
addq $0x8, %r12
leaq 0x4671(%rip), %rdi      # 0xb370
movq %r15, %rsi
movq %r15, %rdx
movl $0x1d0, %ecx            # imm = 0x1D0
movq %r12, %r8
callq 0x28ee
cmpl $0x4b726e00, 0x2c(%rbx) # imm = 0x4B726E00
jne 0x6dbf
movq %r13, (%rsp)
movl 0x30(%rbx), %r13d
andl $-0x10, %r13d
leaq 0x463e(%rip), %rdi      # 0xb370
movq %r15, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq %r12, %r8
callq 0x28ee
leaq 0x1978(%rip), %rdi      # 0x86c2
callq 0x2060
movq %r13, %rdi
callq 0x2150
movq %rax, %r12
movq %rbx, %r15
addq $0x1d30, %r15           # imm = 0x1D30
addl %r13d, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x2170
movq %rax, %r12
movq %rax, %rdi
movl %r13d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x29ac
cmpl $0x80560201, %eax       # imm = 0x80560201
je 0x6d64
movl %eax, %ebp
pushq $0x2
popq %rdx
movq %r12, %rdi
movl %eax, %esi
callq 0x3288
movq %rbx, %rdi
callq 0x2030
testq %r14, %r14
je 0x6ddd
movq %r12, (%r14)
jmp 0x6de5
leaq 0x19a2(%rip), %rdi      # 0x8756
jmp 0x6dc6
leaq 0x18d5(%rip), %rdi      # 0x8692
jmp 0x6dc6
leaq 0x1969(%rip), %rdi      # 0x872f
callq 0x2060
pushq $0x1
popq %rax
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 0x6dce
movl %ebp, (%rcx)
jmp 0x6dce