// File: /workspace/github_c_makefile_star2_O2/jogolden[P]ps4flashtool/flashtool.c
// Function: main
int main(int argc, char **argv) {
    int i;
    char *flashfile = NULL;
    char *flashout = NULL;
    char *extractdir = NULL;
    char *rawextractdir = NULL;
    char *emcipl = NULL;
    char *eapkbl = NULL;
    char *eapkern = NULL;

    char *keyfile = FLASHTOOL_KEYFILE;
    unsigned char *flashdata = NULL;
    unsigned char *ptr = NULL, *ptr2 = NULL;
    unsigned int len, len2;

    printf("~ PlayStation 4 flash tool " FLASHTOOL_VERSION " | by " FLASHTOOL_CREDITS " ~\n");

    if(argc == 1) {
        printf("try '%s --help' for more information\n", argv[0]);
        return 0;
    }

    // make sure this is zero
    verbose = 0;
    noverify = 0;

    for(i = 1; i < argc; i++) {
        char *opt = argv[i];
        char *arg = (i == argc - 1) ? NULL : argv[i + 1];

        if(opt[0] != '-') {
            printf("error: invalid argument syntax!\n");
            return 1;
        }
        if(opt[0] == '-') { opt++; }
        if(opt[0] == '-') { opt++; }
        
        if(opt[0] == 'h' || !strcmp(opt, "help")) {
            print_usage();
            return 1;
        } else if(opt[0] == 'v' || !strcmp(opt, "verbose")) {
            verbose = 1;
        } else if(opt[0] == 'i' || !strcmp(opt, "input")) {
            if(!arg) {
                printf("error: invalid flash input argument!\n");
                return 1;
            }
            flashfile = arg;
            i++;
        } else if(opt[0] == 'o' || !strcmp(opt, "output")) {
            if(!arg) {
                printf("error: invalid flash output argument!\n");
                return 1;
            }
            flashout = arg;
            i++;
        } else if(opt[0] == 'n' || !strcmp(opt, "noverify")) {
            noverify = 1;
        } else if(opt[0] == 'k' || !strcmp(opt, "keyfile")) {
            if(!arg) {
                printf("error: invalid keyfile argument!\n");
                return 1;
            }
            keyfile = arg;
            printf("* changed keyfile to '%s'\n", keyfile);
            i++;
        } else if(!strcmp(opt, "extract")) {
            if(!arg) {
                printf("error: invalid extract directory argument!\n");
                return 1;
            }
            extractdir = arg;
            i++;
        } else if(!strcmp(opt, "rawextract")) {
            if(!arg) {
                printf("error: invalid raw extract directory argument!\n");
                return 1;
            }
            rawextractdir = arg;
            i++;
        } else if(!strcmp(opt, "emcipl")) {
            if(!arg) {
                printf("error: invalid emcipl argument!\n");
                return 1;
            }
            emcipl = arg;
            i++;
        } else if(!strcmp(opt, "eapkbl")) {
            if(!arg) {
                printf("error: invalid eapkbl argument!\n");
                return 1;
            }
            eapkbl = arg;
            i++;
        } else if(!strcmp(opt, "eapkern")) {
            if(!arg) {
                printf("error: invalid eapkern argument!\n");
                return 1;
            }
            eapkern = arg;
            i++;
        } else {
            printf("error: unknown option '%s'\n", opt);
            printf("try '%s --help' for more information\n", argv[0]);
            return 1;
        }
    }

    if(eapkern) {
        char *comma = strstr(eapkern, ",");
        if(!comma) {
            printf("error: please use a comma between the input and output for EAP kernel decryption\n");
            goto end;
        }

        comma[0] = '\0';

        char *input = eapkern;
        char *output = comma + 1;
        
        if(readbin(input, &ptr, &len)) {
            printf("error: failed to read EAP kernel from file!\n");
            goto end;
        }

        printf("attempting to decrypt EAP kernel '%s' to '%s' ...\n", input, output);
        printf("* keys may or not work on your console model/firmware version\n");

        if(decrypt_decompress_eap_kernel(ptr, len, &ptr2, &len2)) {
            printf("error: failed to decrypt EAP kernel\n");
            goto end;
        }

        dumpbin(output, ptr2, len2);

        printf("wrote decrypted EAP kernel\n");

        free(ptr);
        free(ptr2);

        goto end;
    }

    if(!flashfile) {
        printf("error: please specify a PlayStation 4 flash file!\n");
        goto end;
    }
    
    // load flash
    flashdata = load_flashdump(flashfile);
    if(!flashdata) {
        printf("error: invalid flash file!\n");
        goto end;
    }
    
    print_flashinfo(flashdata);

    if(verbose) {
        find_and_print_slb2_info(flashdata);
    }
    
    // raw extract does not require a key file so do it before
    if(rawextractdir) {
        if(rawextract_flashdump(flashdata, rawextractdir)) {
            printf("error: raw extract failed!\n");
        }

        goto end;
    }

    // load the keys
    printf("loading key file '%s' ...\n", keyfile);
    if(keymgr_loadkeys(keyfile)) {
        return 1;
    }

    if(verbose) {
        keymgr_printkeys();
    }
    
    // check flash
    if(!noverify) {
        if(check_flashdump(flashdata)) {
            printf("error: invalid flash data detected!\n");
            goto end;
        }
    }
    
    // replace emc ipl
    if(emcipl) {
        printf("replacing EMC IPL in flash dump with '%s' ...\n", emcipl);

        if(readbin(emcipl, &ptr, &len)) {
            printf("error: failed to read EMC IPL from file!\n");
            goto end;
        }

        if(replace_emc_ipl(flashdata, ptr, len)) {
            printf("warning: failed to replace EMC IPL!\n");
        }

        free(ptr);
    }
    
    // replace eap kbl
    if(eapkbl) {
        printf("replacing EAP KBL in flash dump with '%s' ...\n", eapkbl);

        if(readbin(eapkbl, &ptr, &len)) {
            printf("error: failed to read EAP KBL from file!\n");
            goto end;
        }

        if(replace_eap_kbl(flashdata, ptr, len)) {
            printf("warning: failed to replace EAP KBL!\n");
        }

        free(ptr);
    }
    
    // extract parts
    if(extractdir) {
        if(extract_flashdump(flashdata, extractdir)) {
            printf("error: failed during flash extraction!\n");
            goto end;
        }
    }

    // write flashdata out
    if(flashout) {
        // enable uart ;)
        char uart = 1;
        nvs_write(flashdata, 0, 4, NVS_VAR_BLOCK4_ENABLE_UART, 1, &uart);

        if(verbose) {
            printf("* enabled uart in NVS region\n");
        }

        dumpbin(flashout, flashdata, FLASH_SIZE);
        printf("wrote final flash to '%s'\n", flashout);
    }

end:
    if(flashdata) {
        free(flashdata);
    }

    keymgr_cleanup();

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
andq $0x0, 0x8(%rsp)
movl %edi, %r15d
andq $0x0, 0x28(%rsp)
leaq 0x3d26(%rip), %rdi      # 0x7c7e
callq 0x2060
cmpl $0x1, %r15d
jne 0x3f7b
movq (%r14), %rsi
leaq 0x382b(%rip), %rdi      # 0x7798
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x20b0
jmp 0x43db
andl $0x0, 0x73a2(%rip)      # 0xb324
andl $0x0, 0x739f(%rip)      # 0xb328
leal -0x1(%r15), %eax
movl %eax, 0x34(%rsp)
pushq $0x1
popq %r13
leaq 0x37f0(%rip), %rax      # 0x778c
movq %rax, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x60(%rsp)
cmpl %r15d, %r13d
jge 0x422a
movslq %r13d, %rax
cmpl 0x34(%rsp), %r13d
jne 0x3ff7
xorl %ebp, %ebp
jmp 0x3ffc
movq 0x8(%r14,%rax,8), %rbp
movq (%r14,%rax,8), %rax
cmpb $0x2d, (%rax)
jne 0x4274
leaq 0x1(%rax), %rbx
leaq 0x2(%rax), %rcx
xorl %edx, %edx
cmpb $0x2d, 0x1(%rax)
sete %dl
cmoveq %rcx, %rbx
movb 0x1(%rdx,%rax), %r12b
cmpb $0x68, %r12b
je 0x4223
movq %rbx, %rdi
leaq 0x35c1(%rip), %rsi      # 0x75f8
callq 0x2110
testl %eax, %eax
je 0x4223
cmpb $0x76, %r12b
je 0x416f
movq %rbx, %rdi
leaq 0x35c6(%rip), %rsi      # 0x761e
callq 0x2110
testl %eax, %eax
je 0x416f
cmpb $0x69, %r12b
je 0x417b
movq %rbx, %rdi
leaq 0x35e4(%rip), %rsi      # 0x765d
callq 0x2110
testl %eax, %eax
je 0x417b
cmpb $0x6f, %r12b
je 0x418e
movq %rbx, %rdi
leaq 0x35d6(%rip), %rsi      # 0x7670
callq 0x2110
testl %eax, %eax
je 0x418e
cmpb $0x6e, %r12b
je 0x41a1
movq %rbx, %rdi
leaq 0x35bc(%rip), %rsi      # 0x7677
callq 0x2110
testl %eax, %eax
je 0x41a1
cmpb $0x6b, %r12b
je 0x41ad
movq %rbx, %rdi
leaq 0x35c9(%rip), %rsi      # 0x76a5
callq 0x2110
testl %eax, %eax
je 0x41ad
movq %rbx, %rdi
leaq 0x35ea(%rip), %rsi      # 0x76dd
callq 0x2110
testl %eax, %eax
je 0x41d7
movq %rbx, %rdi
leaq 0x35d0(%rip), %rsi      # 0x76da
callq 0x2110
testl %eax, %eax
je 0x41ea
movq %rbx, %rdi
leaq 0x35e3(%rip), %rsi      # 0x7704
callq 0x2110
testl %eax, %eax
je 0x41fd
movq %rbx, %rdi
leaq 0x35fc(%rip), %rsi      # 0x7734
callq 0x2110
testl %eax, %eax
je 0x4210
movq %rbx, %rdi
leaq 0x3611(%rip), %rsi      # 0x7760
callq 0x2110
testl %eax, %eax
jne 0x4607
testq %rbp, %rbp
je 0x462e
incl %r13d
movq %rbp, 0x58(%rsp)
jmp 0x41cf
movl $0x1, 0x71ab(%rip)      # 0xb324
jmp 0x41cf
testq %rbp, %rbp
je 0x4343
incl %r13d
movq %rbp, 0x60(%rsp)
jmp 0x41cf
testq %rbp, %rbp
je 0x43ec
incl %r13d
movq %rbp, 0x18(%rsp)
jmp 0x41cf
movl $0x1, 0x717d(%rip)      # 0xb328
jmp 0x41cf
testq %rbp, %rbp
je 0x44c8
leaq 0x3601(%rip), %rdi      # 0x77be
movq %rbp, %rsi
xorl %eax, %eax
callq 0x20b0
incl %r13d
movq %rbp, 0x50(%rsp)
incl %r13d
jmp 0x3fe0
testq %rbp, %rbp
je 0x45ef
incl %r13d
movq %rbp, 0x40(%rsp)
jmp 0x41cf
testq %rbp, %rbp
je 0x45fb
incl %r13d
movq %rbp, 0x48(%rsp)
jmp 0x41cf
testq %rbp, %rbp
je 0x463a
incl %r13d
movq %rbp, 0x20(%rsp)
jmp 0x41cf
testq %rbp, %rbp
je 0x4646
incl %r13d
movq %rbp, 0x38(%rsp)
jmp 0x41cf
callq 0x3b86
jmp 0x4280
movq 0x58(%rsp), %r14
testq %r14, %r14
je 0x4288
pushq $0x2c
popq %rsi
movq %r14, %rdi
callq 0x20a0
testq %rax, %rax
je 0x4337
movq %rax, %rbx
movb $0x0, (%rax)
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq 0x3613
testl %eax, %eax
je 0x434f
leaq 0x3c5a(%rip), %rdi      # 0x7ec9
jmp 0x43c3
leaq 0x3da1(%rip), %rdi      # 0x801c
callq 0x2060
pushq $0x1
popq %rbx
jmp 0x43db
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4397
callq 0x36b8
testq %rax, %rax
je 0x43bc
movq %rax, %rbx
movq %rax, %rdi
callq 0x3826
cmpl $0x0, 0x706e(%rip)      # 0xb324
movq 0x20(%rsp), %r13
je 0x43a8
movq %rbx, %rbp
addq $0x30, %rbp
leaq 0x360c(%rip), %r15      # 0x78db
xorl %r12d, %r12d
cmpq $0x1ffffff, %r12        # imm = 0x1FFFFFF
ja 0x43a0
cmpl $0x32424c53, (%rbx,%r12) # imm = 0x32424C53
jne 0x4328
leaq 0x35cc(%rip), %rdi      # 0x78bc
movq %r12, %rsi
xorl %eax, %eax
callq 0x20b0
movq %rbp, %r13
xorl %r14d, %r14d
movl 0xc(%rbx,%r12), %eax
cmpq %rax, %r14
jae 0x4328
movl -0x10(%r13), %edx
movl -0xc(%r13), %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x20b0
incq %r14
addq $0x30, %r13
jmp 0x4300
addq $0x4, %r12
addq $0x4, %rbp
movq 0x20(%rsp), %r13
jmp 0x42d2
leaq 0x3abd(%rip), %rdi      # 0x7dfb
jmp 0x43c3
leaq 0x3cad(%rip), %rdi      # 0x7ff7
jmp 0x427b
incq %rbx
leaq 0x349c(%rip), %rdi      # 0x77f5
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
leaq 0x3adf(%rip), %rdi      # 0x7e4c
callq 0x2060
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %esi
leaq 0x28(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x6c93
testl %eax, %eax
je 0x43f8
leaq 0x3b10(%rip), %rdi      # 0x7ea5
jmp 0x43c3
leaq 0x390e(%rip), %rdi      # 0x7cac
jmp 0x43c3
pushq $0xa
popq %rdi
callq 0x2040
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x442b
movq %rbx, %rdi
callq 0x3a86
jmp 0x43ca
leaq 0x391b(%rip), %rdi      # 0x7cde
callq 0x2060
xorl %ebx, %ebx
movq %rbx, %rdi
callq 0x2030
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x48c8
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3bde(%rip), %rdi      # 0x7fd1
jmp 0x427b
movq 0x28(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x35d1
leaq 0x3a7a(%rip), %rdi      # 0x7e8a
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
movq 0x28(%rsp), %rdi
callq 0x2030
jmp 0x43c8
leaq 0x33f6(%rip), %rdi      # 0x7828
movq 0x50(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x20b0
movq %r14, %rdi
callq 0x47a8
testl %eax, %eax
jne 0x4280
cmpl $0x0, 0x6ecc(%rip)      # 0xb324
je 0x4461
xorl %eax, %eax
callq 0x48fa
cmpl $0x0, 0x6ec0(%rip)      # 0xb328
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
je 0x44b0
testq %r13, %r13
je 0x44ff
leaq 0x33bf(%rip), %rdi      # 0x7843
movq %r13, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r13, %rdi
callq 0x3613
testl %eax, %eax
je 0x44d4
leaq 0x3927(%rip), %rdi      # 0x7dd2
jmp 0x457a
movq %rbx, %rdi
callq 0x38f3
testl %eax, %eax
je 0x4474
leaq 0x3836(%rip), %rdi      # 0x7cf9
jmp 0x457a
leaq 0x3ae1(%rip), %rdi      # 0x7fb0
jmp 0x427b
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
callq 0x6394
testl %eax, %eax
je 0x44f5
leaq 0x382d(%rip), %rdi      # 0x7d1d
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
testq %r15, %r15
je 0x455f
leaq 0x3367(%rip), %rdi      # 0x7872
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r15, %rdi
callq 0x3613
testl %eax, %eax
je 0x4534
leaq 0x3877(%rip), %rdi      # 0x7da9
jmp 0x457a
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
callq 0x67f8
testl %eax, %eax
je 0x4555
leaq 0x37f1(%rip), %rdi      # 0x7d41
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
testq %r14, %r14
je 0x4584
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3929
testl %eax, %eax
je 0x4584
leaq 0x3808(%rip), %rdi      # 0x7d82
callq 0x2060
jmp 0x43ca
cmpq $0x0, 0x18(%rsp)
je 0x43ca
leaq 0x14(%rsp), %r9
movb $0x1, (%r9)
pushq $0x4
popq %rdx
pushq $0x1
popq %r8
movq %rbx, %rdi
xorl %esi, %esi
movl $0x31f, %ecx            # imm = 0x31F
callq 0x4a2c
cmpl $0x0, 0x6d6e(%rip)      # 0xb324
je 0x45c4
leaq 0x37a6(%rip), %rdi      # 0x7d65
callq 0x2060
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2000000, %edx        # imm = 0x2000000
callq 0x35d1
leaq 0x32c1(%rip), %rdi      # 0x78a1
movq %r14, %rsi
xorl %eax, %eax
callq 0x20b0
jmp 0x43ca
leaq 0x38ff(%rip), %rdi      # 0x7ef5
jmp 0x427b
leaq 0x391e(%rip), %rdi      # 0x7f20
jmp 0x427b
leaq 0x31cb(%rip), %rdi      # 0x77d9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20b0
movq (%r14), %rsi
leaq 0x3176(%rip), %rdi      # 0x7798
xorl %eax, %eax
callq 0x20b0
jmp 0x4280
leaq 0x395a(%rip), %rdi      # 0x7f8f
jmp 0x427b
leaq 0x390e(%rip), %rdi      # 0x7f4f
jmp 0x427b
leaq 0x3922(%rip), %rdi      # 0x7f6f
jmp 0x427b
nop