/* 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 <command.h>
#include <vsprintf.h>
#include <debug_impl.h>
#include <mtk_ab.h>
#include <utility.h>

int do_mtk_ab_pre_select (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    char storage[STORAGE_DEVICE_BUF_SIZE] = {0};
    char storage_info[STORAGE_INDEX_BUF_SIZE] = {0};
    int ret;
    unsigned int device = 0;

    ret = sys_get_boot_device(storage, sizeof(storage));
    if(ret < 0)
    {
        UBOOT_ERROR("Error: Get booting device name failure, Unknown storage device.\n");
        return -1;
    }

    device = simple_strtoul(argv[1], NULL, 10);
    ret = sys_get_storage_info(device,"misc",storage_info);
    if(ret < 0)
        UBOOT_ERROR("Read misc partition information failure\n");

    ret = ab_selection(storage, storage_info, 1);
    UBOOT_DEBUG("A/B selection information: %d\n",ret);
    if(ret < 0)
        UBOOT_ERROR("Get ab selection information failure\n");

    return CMD_RET_SUCCESS;
}

U_BOOT_CMD(
    ab_pre_slt, CONFIG_SYS_MAXARGS, 1,    do_mtk_ab_pre_select,
    "ab_pre_slt - do a/b partition preselection\n",
    "<device_number>: Access the 'device_number'th storage device."
    "command: ab_pre_slt <device_number>\n"
);

int do_mtk_ab_select (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    char storage[STORAGE_DEVICE_BUF_SIZE] = {0};
    char storage_info[STORAGE_INDEX_BUF_SIZE] = {0};
#if (CONFIG_AVB_VERIFY == 1)
    char cmd[COMMAND_BUF_SIZE] = {0};
    char *slot_suffix;
#endif
    int ret;
    unsigned int device = 0;
	uint64_t start = 0, end = 0;

	start = get_timer(0);
    ret = sys_get_boot_device(storage, sizeof(storage));
    if (ret < 0)
    {
        UBOOT_ERROR("Error: Get booting device name failure, Unknown storage device.\n");
        return -1;
    }

    device = simple_strtoul(argv[1], NULL, 10);
    ret = sys_get_storage_info(device, "misc", storage_info);
    if (ret < 0)
    {
        UBOOT_ERROR("Error: Select MISC partition failure.\n");
        return -1;
    }

    ret = ab_selection(storage, storage_info, 0);
    if (ret < 0)
    {
        UBOOT_ERROR("A/B partition select failure\n");
        if (ret == -ENOTSUPP)
        {
            UBOOT_ERROR("misc partition is not exist.\n");
        }
        else if (ret == -ENODATA)
        {
            UBOOT_ERROR("Data is not exist in MISC partition.\n");
        }
        else
        {
            UBOOT_ERROR("Create default A/B structure failure.\n");
        }
        goto AB_SELECTION_RET;
    }

    end = get_timer(0);
    UBOOT_BOOTTIME("[ab slt][start:%llu][end:%llu][total time:%llu]\n", start, end, end-start);
#if (CONFIG_AVB_VERIFY == 1)
    slot_suffix = env_get("ab_select");
    memset(cmd, 0, COMMAND_BUF_SIZE);
    strncpy(cmd, "avb verify", sizeof(cmd));
    UBOOT_DEBUG("cmd=%s\n", cmd);
    if (run_command(cmd, 0) != 0)
    {
        UBOOT_ERROR("Verify partition %s failure\n", slot_suffix);
#if defined(CONFIG_AB_FROM_ROM) && defined(CONFIG_AB_SIDELOAD)
        reset_cpu(0);
#endif
    }
    else
    {
        UBOOT_DEBUG("Verify partition %s pass\n", slot_suffix);
    }
#endif

AB_SELECTION_RET:
    return CMD_RET_SUCCESS;
}

U_BOOT_CMD(
    ab_slt, CONFIG_SYS_MAXARGS, 1,    do_mtk_ab_select,
    "ab_slt - do a/b partition selection\n",
    "<device_number>: Access the 'device_number'th storage device."
    "command: ab_slt <device_number>\n"
);
