// File: /workspace/github_c_makefile_star2_O3/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
movl %edi, %r15d
xorl %r12d, %r12d
movq %r12, 0x8(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x498e(%rip), %rdi      # 0x8c8e
callq 0x2060
cmpl $0x1, %r15d
jne 0x4321
movq (%r14), %rsi
leaq 0x4493(%rip), %rdi      # 0x87a8
xorl %eax, %eax
callq 0x20b0
jmp 0x471a
movl %r12d, 0x7ffc(%rip)     # 0xc324
movl %r12d, 0x7ff9(%rip)     # 0xc328
jle 0x4704
leal -0x1(%r15), %eax
movl %eax, 0x2c(%rsp)
leaq 0x4458(%rip), %rax      # 0x879c
movq %rax, 0x50(%rsp)
movl $0x1, %r13d
movq $0x0, 0x60(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movslq %r13d, %rax
cmpl 0x2c(%rsp), %eax
jne 0x439b
xorl %ebp, %ebp
jmp 0x43a0
movq 0x8(%r14,%rax,8), %rbp
movq (%r14,%rax,8), %rax
cmpb $0x2d, (%rax)
jne 0x45d4
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 0x45cd
movq %rbx, %rdi
leaq 0x422d(%rip), %rsi      # 0x8608
callq 0x2110
testl %eax, %eax
je 0x45cd
cmpb $0x76, %r12b
je 0x4513
movq %rbx, %rdi
leaq 0x4232(%rip), %rsi      # 0x862e
callq 0x2110
testl %eax, %eax
je 0x4513
cmpb $0x69, %r12b
je 0x451f
movq %rbx, %rdi
leaq 0x4250(%rip), %rsi      # 0x866d
callq 0x2110
testl %eax, %eax
je 0x451f
cmpb $0x6f, %r12b
je 0x4532
movq %rbx, %rdi
leaq 0x4242(%rip), %rsi      # 0x8680
callq 0x2110
testl %eax, %eax
je 0x4532
cmpb $0x6e, %r12b
je 0x4545
movq %rbx, %rdi
leaq 0x4228(%rip), %rsi      # 0x8687
callq 0x2110
testl %eax, %eax
je 0x4545
cmpb $0x6b, %r12b
je 0x4551
movq %rbx, %rdi
leaq 0x4235(%rip), %rsi      # 0x86b5
callq 0x2110
testl %eax, %eax
je 0x4551
movq %rbx, %rdi
leaq 0x4256(%rip), %rsi      # 0x86ed
callq 0x2110
testl %eax, %eax
je 0x4581
movq %rbx, %rdi
leaq 0x423c(%rip), %rsi      # 0x86ea
callq 0x2110
testl %eax, %eax
je 0x4594
movq %rbx, %rdi
leaq 0x424f(%rip), %rsi      # 0x8714
callq 0x2110
testl %eax, %eax
je 0x45a7
movq %rbx, %rdi
leaq 0x4268(%rip), %rsi      # 0x8744
callq 0x2110
testl %eax, %eax
je 0x45ba
movq %rbx, %rdi
leaq 0x427d(%rip), %rsi      # 0x8770
callq 0x2110
testl %eax, %eax
jne 0x49c9
testq %rbp, %rbp
je 0x49f0
incl %r13d
movq %rbp, 0x58(%rsp)
jmp 0x4573
movl $0x1, 0x7e07(%rip)      # 0xc324
jmp 0x4573
testq %rbp, %rbp
je 0x4735
incl %r13d
movq %rbp, 0x60(%rsp)
jmp 0x4573
testq %rbp, %rbp
je 0x479c
incl %r13d
movq %rbp, 0x18(%rsp)
jmp 0x4573
movl $0x1, 0x7dd9(%rip)      # 0xc328
jmp 0x4573
testq %rbp, %rbp
je 0x4886
leaq 0x426d(%rip), %rdi      # 0x87ce
movq %rbp, %rsi
xorl %eax, %eax
callq 0x20b0
incl %r13d
movq %rbp, 0x50(%rsp)
incl %r13d
cmpl %r15d, %r13d
jl 0x438e
jmp 0x45eb
testq %rbp, %rbp
je 0x49b1
incl %r13d
movq %rbp, 0x40(%rsp)
jmp 0x4573
testq %rbp, %rbp
je 0x49bd
incl %r13d
movq %rbp, 0x48(%rsp)
jmp 0x4573
testq %rbp, %rbp
je 0x49fc
incl %r13d
movq %rbp, 0x38(%rsp)
jmp 0x4573
testq %rbp, %rbp
je 0x4a08
incl %r13d
movq %rbp, 0x30(%rsp)
jmp 0x4573
callq 0x3f0b
jmp 0x45e0
leaq 0x4a51(%rip), %rdi      # 0x902c
callq 0x2060
movl $0x1, %r12d
jmp 0x471a
movq 0x58(%rsp), %r14
testq %r14, %r14
je 0x4637
movq %r14, %rdi
movl $0x2c, %esi
callq 0x20a0
testq %rax, %rax
je 0x472c
movq %rax, %rbx
movb $0x0, (%rax)
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq 0x3968
testl %eax, %eax
je 0x4741
leaq 0x48a7(%rip), %rdi      # 0x8ed9
jmp 0x470b
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4704
callq 0x3a15
testq %rax, %rax
je 0x4790
movq %rax, %rbx
movq %rax, %rdi
callq 0x3b97
cmpl $0x0, 0x7cbf(%rip)      # 0xc324
je 0x46e6
movq %rbx, %rbp
addq $0x30, %rbp
leaq 0x4276(%rip), %r15      # 0x88eb
xorl %r12d, %r12d
cmpl $0x32424c53, (%rbx,%r12) # imm = 0x32424C53
jne 0x46c7
leaq 0x4243(%rip), %rdi      # 0x88cc
movq %r12, %rsi
xorl %eax, %eax
callq 0x20b0
cmpl $0x0, 0xc(%rbx,%r12)
je 0x46c7
movq %rbp, %r13
xorl %r14d, %r14d
movl -0x10(%r13), %edx
movl -0xc(%r13), %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x20b0
incq %r14
movl 0xc(%rbx,%r12), %eax
addq $0x30, %r13
cmpq %rax, %r14
jb 0x46a1
leaq 0x4(%r12), %rax
addq $0x4, %rbp
cmpq $0x1fffffc, %r12        # imm = 0x1FFFFFC
movq %rax, %r12
jb 0x4678
movl $0xa, %edi
callq 0x2040
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x47a8
movq %rbx, %rdi
callq 0x3e0b
movq %rbx, %rdi
jmp 0x4864
leaq 0x45b1(%rip), %rdi      # 0x8cbc
callq 0x2060
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x4d12
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x46d8(%rip), %rdi      # 0x8e0b
jmp 0x470b
leaq 0x48cb(%rip), %rdi      # 0x9007
jmp 0x45db
incq %rbx
leaq 0x40ba(%rip), %rdi      # 0x8805
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
leaq 0x46fd(%rip), %rdi      # 0x8e5c
callq 0x2060
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %esi
leaq 0x20(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x7198
testl %eax, %eax
je 0x4838
leaq 0x472a(%rip), %rdi      # 0x8eb5
jmp 0x470b
leaq 0x4557(%rip), %rdi      # 0x8cee
jmp 0x470b
leaq 0x483e(%rip), %rdi      # 0x8fe1
jmp 0x45db
leaq 0x4089(%rip), %rdi      # 0x8838
movq 0x50(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x20b0
movq %r14, %rdi
callq 0x4be8
movl $0x1, %r12d
testl %eax, %eax
jne 0x471a
cmpl $0x0, 0x7b49(%rip)      # 0xc324
je 0x47e4
xorl %eax, %eax
callq 0x4d5a
cmpl $0x0, 0x7b3d(%rip)      # 0xc328
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r12
je 0x486e
testq %r12, %r12
je 0x48bd
leaq 0x4047(%rip), %rdi      # 0x8853
movq %r12, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r12, %rdi
callq 0x3968
testl %eax, %eax
je 0x4892
leaq 0x45af(%rip), %rdi      # 0x8de2
jmp 0x4938
movq 0x20(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x3922
leaq 0x464a(%rip), %rdi      # 0x8e9a
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x2030
jmp 0x4710
movq %rbx, %rdi
callq 0x3c74
testl %eax, %eax
je 0x47fc
leaq 0x4488(%rip), %rdi      # 0x8d09
jmp 0x4938
leaq 0x4733(%rip), %rdi      # 0x8fc0
jmp 0x45db
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
callq 0x6862
testl %eax, %eax
je 0x48b3
leaq 0x447f(%rip), %rdi      # 0x8d2d
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
testq %r15, %r15
je 0x491d
leaq 0x3fb9(%rip), %rdi      # 0x8882
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r15, %rdi
callq 0x3968
testl %eax, %eax
je 0x48f2
leaq 0x44c9(%rip), %rdi      # 0x8db9
jmp 0x4938
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
callq 0x6cf0
testl %eax, %eax
je 0x4913
leaq 0x4443(%rip), %rdi      # 0x8d51
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2030
testq %r14, %r14
je 0x4942
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cac
testl %eax, %eax
je 0x4942
leaq 0x445a(%rip), %rdi      # 0x8d92
callq 0x2060
jmp 0x46fc
cmpq $0x0, 0x18(%rsp)
je 0x46fc
leaq 0x14(%rsp), %r9
movb $0x1, (%r9)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x4, %edx
movl $0x31f, %ecx            # imm = 0x31F
movl $0x1, %r8d
callq 0x4e92
cmpl $0x0, 0x79ac(%rip)      # 0xc324
je 0x4986
leaq 0x43f4(%rip), %rdi      # 0x8d75
callq 0x2060
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2000000, %edx        # imm = 0x2000000
callq 0x3922
leaq 0x3f0f(%rip), %rdi      # 0x88b1
movq %r14, %rsi
xorl %eax, %eax
callq 0x20b0
jmp 0x46fc
leaq 0x454d(%rip), %rdi      # 0x8f05
jmp 0x45db
leaq 0x456c(%rip), %rdi      # 0x8f30
jmp 0x45db
leaq 0x3e19(%rip), %rdi      # 0x87e9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20b0
movq (%r14), %rsi
leaq 0x3dc4(%rip), %rdi      # 0x87a8
xorl %eax, %eax
callq 0x20b0
jmp 0x45e0
leaq 0x45a8(%rip), %rdi      # 0x8f9f
jmp 0x45db
leaq 0x455c(%rip), %rdi      # 0x8f5f
jmp 0x45db
leaq 0x4570(%rip), %rdi      # 0x8f7f
jmp 0x45db