/* Copyright Statement:
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
 * the prior written permission of MediaTek inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of MediaTek Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * Copyright  (C) [2020]  MediaTek Inc. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
 *
 * The following software/firmware and/or related documentation ("MediaTek
 * Software") have been modified by MediaTek Inc. All revisions are subject to
 * any receiver's applicable license agreements with MediaTek Inc.
 */

#include <common.h>
#include <debug_impl.h>
#include <mtk_cmdtable.h>
#include <flow.h>
#include <utility.h>
#include <system_impl.h>
#ifdef CONFIG_MULTICORES_PLATFORM
#include <flow_thread.h>
#include <smp/thread_info.h>
#if defined(CONFIG_SECOND_STANDBY)
#include <standby_mode.h>
extern int standby_mode_enter_flag;
#endif
#endif
int do_basic_command_register(void)
{

    mtk_add_command_table("chipid", 0, AFTER_UBOOT_INIT);
    mtk_add_command_table("revisionid", 0, AFTER_UBOOT_INIT);
    mtk_add_command_table("mtk_drv_init", 0, AFTER_UBOOT_INIT);
#ifdef CONFIG_DATA_SEPARATION
    mtk_add_command_table("dataindex setpath", 0, AFTER_UBOOT_INIT);
#endif
    mtk_add_command_table("bootargsarea", 0, AFTER_UBOOT_INIT);
#ifdef CONFIG_GPIO_DEFAULT_FROM_INI
    mtk_add_command_table("gpiodefault", 0, AFTER_UBOOT_INIT);
#endif
#if defined(CONFIG_SECOND_STANDBY)
    mtk_add_command_table("standbyinit", 0, AFTER_UBOOT_INIT);
#endif
//#if defined(CONFIG_MTK_PM)
//    mtk_add_command_table("pm_init", 0, AFTER_UBOOT_INIT);
//#endif
#ifndef CONFIG_MULTICORES_PLATFORM
#if defined(CONFIG_BOOTPMU_EARLY)
#if defined(CONFIG_MTK_PMU)
    mtk_add_command_table("boot_pmu", 0, AFTER_UBOOT_INIT);
#elif defined(CONFIG_REMOTEPROC_MTK_PQU)
    mtk_add_command_table("rproc init", 0, AFTER_UBOOT_INIT);
#endif
#endif
#endif
#if defined(CONFIG_USERDATA_PARTITION_SELF_ADAPTION)
    mtk_add_command_table("emmc_self_adaption", 0, AFTER_UBOOT_INIT);
#endif
#if defined(CONFIG_USB_SCRIPT_TO_ENV)
    mtk_add_command_table("usb_script_to_env", 0, AFTER_UBOOT_INIT);
#endif
    mtk_add_command_table("bootcheck", 0, AFTER_UBOOT_INIT);
#ifdef CONFIG_MULTICORES_PLATFORM
    mtk_add_command_table("fastbootcheck", 0, AFTER_UBOOT_INIT);
#endif
    mtk_add_command_table("propagate_mmap_filename", 0, BEFORE_CONSOLE_INPUT);
	//customer auto upgrade by checking power key
    mtk_add_command_table("cust_upgrade", 0, AFTER_UBOOT_INIT);
    mtk_add_command_table("setup_kernelcore_size", 0, BEFORE_CONSOLE_INPUT);

#ifndef CONFIG_MULTICORES_PLATFORM
    mtk_add_command_table("panel_pre_init", 0, BEFORE_CONSOLE_INPUT);
#ifdef CONFIG_MTK_PANEL
    mtk_add_command_table("panel_mute 1", 0, BEFORE_CONSOLE_INPUT);
    mtk_add_command_table("panel_enable 1", 0, BEFORE_CONSOLE_INPUT);
#endif
#endif

#ifdef CONFIG_FASTBOOT_SHORTCUT
    mtk_add_command_table("fastbootshortcutcheck", 0, BEFORE_CONSOLE_INPUT);
#endif

#ifdef CONFIG_OAD_UPGRADE
    mtk_add_command_table("check_upgrade_mode", 0, AFTER_CONSOLE_INPUT);
#endif
#if (CONFIG_USB_AUTO_UPGRADE == 1)
    mtk_add_command_table("check_usb_auto_upgrade", 0, AFTER_CONSOLE_INPUT);
#endif

#ifndef CONFIG_MULTICORES_PLATFORM
#ifdef CONFIG_REMOTEPROC_MTK_VAD_CORTEX_M4
    mtk_add_command_table("boot_vad", 0, AFTER_CONSOLE_INPUT);
#endif

#if !defined(CONFIG_BOOTPMU_EARLY)
#if defined(CONFIG_MTK_PMU)
    mtk_add_command_table("boot_pmu", 0, AFTER_CONSOLE_INPUT);
#endif
#endif

#ifdef CONFIG_SECOND_STANDBY
    mtk_add_command_table("standby", 0, AFTER_CONSOLE_INPUT);
#endif
#endif

    mtk_add_command_table("boot_dramc", 0, BEFORE_BOOT_KERNEL);

#ifndef CONFIG_MULTICORES_PLATFORM
    mtk_add_command_table("fastbootcheck", 0, BEFORE_BOOT_KERNEL);
#if defined(CONFIG_SOUND)
    mtk_add_command_table("bootmusic init", 0, BEFORE_BOOT_KERNEL);
    mtk_add_command_table("bootmusic play", 0, BEFORE_BOOT_KERNEL);
#endif
#endif

    mtk_add_command_table("upgradecheck", 0, BEFORE_BOOT_KERNEL);
    mtk_add_command_table("addversion", 0, BEFORE_BOOT_KERNEL);
#ifdef CONFIG_STOP_TO_CONSOLE
	mtk_add_command_table("reset_bootdelay", 0, BEFORE_CONSOLE_INPUT);
#endif
    mtk_add_command_table("ExJsonDebug 000", 0, AFTER_CONSOLE_INPUT);

    return 0;
}

int do_after_uboot_init(void)
{
    mtk_run_command_table(AFTER_UBOOT_INIT);
#ifdef CONFIG_MULTICORES_PLATFORM
    do_after_uboot_init_task_create();
#endif
    return 0;
}

int do_before_console_input(void)
{
#ifdef CONFIG_OLED_ERROR_CLEAR_BY_KEYPAD
    mtk_add_command_table("oled_panel_err_cnt_clear_keypad", 0, BEFORE_CONSOLE_INPUT);
#else
    mtk_add_command_table("oled_panel_err_cnt_clear", 0, BEFORE_CONSOLE_INPUT);
#endif

    mtk_run_command_table(BEFORE_CONSOLE_INPUT);
#ifdef CONFIG_MULTICORES_PLATFORM
    do_before_console_input_task_create();
#endif
    return 0;
}

int do_after_console_input(void)
{
    mtk_run_command_table(AFTER_CONSOLE_INPUT);
#ifdef CONFIG_MULTICORES_PLATFORM
    do_after_console_input_task_create();
#endif
    return 0;
}

int do_before_boot_kernel(void)
{
    mtk_run_command_table(BEFORE_BOOT_KERNEL);
#ifdef CONFIG_MULTICORES_PLATFORM
    do_before_boot_kernel_task_create();
#endif
    return 0;
}

int do_jump_to_kernel(void)
{
#if defined(CONFIG_MULTICORES_PLATFORM) && defined(CONFIG_SECOND_STANDBY)
    if (standby_mode_enter_flag == -1)
    {
        standby_mode_enter_flag = standby_mode_enter_standby();
    }

    if (standby_mode_enter_flag)
    {
        release_non_boot_core();
    }
#endif
    run_command("checkteesuccess", 0);
#if (CONFIG_CMD_AVB == 1)
    run_command("avb init 0", 0);
#if (CONFIG_AB_SIDELOAD == 1)
    run_command("ab_slt 0", 0);
#else
    run_command("avb verify", 0);
#endif
#else
#if (CONFIG_AB_SIDELOAD == 1)
    run_command("ab_slt 0", 0);
#endif
#endif
    return 0;
}
