/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*----------------------------------------------------------------------------*/
/*! @file c_dlm.h 
 *  $RCSfile: $
 *  $Revision: #1 $
 *  $Date: 2015/05/29 $
 *  $Author: brianpc.huang $
 *  
 *  @par Description:
 */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*! @addtogroup groupMW_DLM
 *  @{ 
 */
/*----------------------------------------------------------------------------*/


#ifndef C_DLM_H
#define C_DLM_H

#include "u_dlm.h"

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_reg_hdr_callback
 *              User may replace our default file header, in case they need 
 *              use their API to check file header, hence this API provide user 
 *              to register their check API.
 *  @param [in] fn               User file header check API.
 *  @param [in] ui4_hdr_len   User define file header size.
 *  @return Return the error code.
 *  @retval DLM_OK Register their check API OK.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_reg_hdr_callback(dlm_hdr_parse_fn fn, UINT32 ui4_hdr_len);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_reg_append_callback
 *              The codefile from contain many user define data, they may firmware
 *              or other internal data, when donwload process is running it will 
 *              dispath user defined data to each user, hence, user "MUST" provide
 *              their own API to process this data.
 *  @param [in] pc_type       4 bytes tag, user defined, this tag "MUST" same as build code file.
 *  @param [in] ui4_buf_size  Buffer size for this data.
 *  @param [in] fn                User defined callback.
 *  @return Return the error code.
 *  @retval DLM_OK                       If there did not exsit any failed.
 *  @retval DLM_DUPLICATE_TAG     The tag already in use.
 *  @retval DLM_DUPLICATE_API      The API already in use.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_reg_append_callback(UCHAR *pc_type, UINT32 ui4_buf_size, dlm_append_data_fn fn);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_reg_append_callback2
 *              The codefile from contain many user define data, they may firmware
 *              or other internal data, when donwload process is running it will 
 *              dispath user defined data to each user, hence, user "MUST" provide
 *              their own API to process this data.
 *  @param [in] pc_type          4 bytes tag, user defined, this tag "MUST" same as build code file.
 *  @param [in] ui4_buf_size     Buffer size for this data.
 *  @param [in] fn                  User defined callback.
 *  @return Return the error code.
 *  @retval DLM_OK                       If there did not exsit any failed.
 *  @retval DLM_DUPLICATE_TAG     The tag already in use.
 *  @retval DLM_DUPLICATE_API      The API already in use.

 */
/*------------------------------------------------------------------*/
INT32 c_dlm_reg_append_callback2(UCHAR *pc_type, UINT32 ui4_buf_size, dlm_append_data_fn2 fn);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_unreg_append_callback
 *              This API is use to remove installed append data call back. 
 *  @param [in] puac_tag Call back tag.
 *  @return Return the error code.
 *  @retval DLM_OK                               Uninstall ok.
 *  @retval DLM_NO_HELPER_REGISTERED  No such tag exist.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_unreg_append_callback(UCHAR *puac_tag);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_register_progress_notify
 *              User may register this api to monitor current tag download or upload progress.
 *  @param [in] fn   User progress notify function.
 *  @return Return the error code.
 *  @retval DLM_OK uninstall ok.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_register_progress_notify(dlm_progress_notify_fn fn);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_register_tag_progress_notify
 *              User may register this api to monitor current tag download or upload progress.
 *  @param [in] fn  user progress notify function.
 *  @return Return the error code.
 *  @retval DLM_OK
 */
/*------------------------------------------------------------------*/

INT32     c_dlm_register_tag_progress_notify(dlm_progress_notify_fn fn);

/*------------------------------------------------------------------*/
/*! @brief  c_dlm_integrity_check
 *              This API will check file integrity as user defined.
 *  @param [in] ps_filename            Filename.
 *  @param [in] pt_user_io              IO function.
 *  @return Return the error code.
 *  @retval DLM_OK check ok,others failed.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_integrity_check(CHAR *ps_filename, DLM_IO_T *pt_user_io);

/*------------------------------------------------------------------*/
/*! @brief   c_dlm_download
 *               User can use this API to download codefile from USB flash.
 *  @param [in] filename            Code file name.
 *  @param [in] pt_user_io         IO function.
 *  @param [in] i4_check           File Header Check flag.
 *  @return Return the error code.
 *  @retval DLM_OK  download finished, others    download failed.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_download(CHAR *filename, DLM_IO_T *pt_user_io, INT32 i4_check);
/*------------------------------------------------------------------*/
/*! @brief   c_dlm_upload
 *              Upload device internal data to USB flash for future use.
 *              During upload process, this API make proper header for internal.
 *              data and add digest to header to provide codefile intergrity.
 *              
 *  @param [in] pac_tag          Which data to upload.
 *  @param [in] pt_user_io       User define I/O callback.
 *  @param [in] ps_path          Output file and path. 
 *  @return Return the error code.
 *  @retval  DLM_OK                                      Upload data done.
 *  @retval  DLM_ALLOCATE_MEMORY_FAILED    Allocate memory failed.
 *  @retval  DLM_OPEN_FILE_FAILED                Open file failed.
 *  @retval  DLM_WRITE_DATA_FAILED             Write data failed.
 *  @retval  DLM_NO_HELPER_REGISTERED         No such data exist.
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_upload(CHAR *pac_tag, DLM_IO_T *pt_io, CHAR* ps_path);


/*------------------------------------------------------------------*/
/*! @brief c_dlm_get_download_hdr
 *             User can use this API to get the header of the file.
 *  @return Return header of download file.
 *  @retval Point of the header.
 */
/*------------------------------------------------------------------*/
CHAR     *c_dlm_get_download_hdr(VOID);


/*------------------------------------------------------------------*/
/*! @brief   c_dlm_set_ignore_check_item
 *               This API is use to disable some integrity check item
 *  @param [in] uc_flag   Check flag.
 *  @return Return the error code.
 *  @retval DLM_OK  Check ok.
                others    check failed
 */
/*------------------------------------------------------------------*/
INT32     c_dlm_set_ignore_check_item(UCHAR uc_flag);

/*------------------------------------------------------------------*/
/*! @brief      c_dlm_get_codefile_tag_list
 *                  User can call this api to get the tag list of append data for future process.
 *  @param [in] ps_filepath  Codefile name.
 *  @return Return the append data list.
 *  @retval  Append_T*  If NULL,   codefile doesn't exist any tag,others, Tag list.
 */
/*------------------------------------------------------------------*/
Append_T *c_dlm_get_codefile_tag_list(CHAR *ps_filepath);


/*------------------------------------------------------------------*/
/*! @brief  c_dlm_download_by_tag
 *              This API is use to download specify append data.
 *  @param [in] ps_file         Codefile name.
 *  @param [in] puc_tag      User defined tag.
 *  @param [in] pt_user_io   User specify I/O.
 *  @return Return the error code.
 *  @retval DLM_OK     download ok.
 *             others       download failed.
 */
/*------------------------------------------------------------------*/
INT32 c_dlm_download_by_tag(CHAR *ps_file, UCHAR *puc_tag, DLM_IO_T *pt_user_io);
/*------------------------------------------------------------------*/
/*! @brief  c_dlm_download_by_division
 *             This API is use to download specify append data.
 *  @param [in] ui4_len       Length of puc_buffer.
 *  @param [in] puc_buffer  Append data.
 *  @param [in] puc_tag     User defined tag.
 *  @return Return the error code.
 *  @retval DLM_OK     Download ok.
 *             others       Download failed.
 */
/*------------------------------------------------------------------*/

INT32 c_dlm_download_by_division(UINT32 ui4_len, UCHAR*  puc_buffer, UCHAR* puc_tag);


/*------------------------------------------------------------------*/
/*! @brief  c_dlm_tag_test
 *              This API is use to download specify append data.
 *  @param [in] ps_file           Codefile name.
 *  @param [in] puac_tag       User specify tag name.
 *  @param [in] pac_buffer     The buffer to store downloaded data.
 *  @param [in] i4_buf_size     Pac_buffer size.
 *  @return Return the error code.
 *  @retval DLM_OK     Download OK.
 *  @retval others       Download failed.
 */
/*------------------------------------------------------------------*/
INT32 c_dlm_tag_test(CHAR *ps_file, UCHAR *puac_tag, DLM_IO_T *pt_user_io, CHAR *pac_buffer, INT32 i4_buf_size);

/*------------------------------------------------------------------*/
/*! @brief   c_dlm_reg_version_compare_callback
 *               Register user defined version compare API.
 *  @param [in] p_fn   Compare API.
 *  @retval     VOID
 */
/*------------------------------------------------------------------*/
VOID  c_dlm_reg_version_compare_callback(dlm_ver_chk_fn p_fn);


#if ((defined(DLM_RSA_SIGN_SUPPORT)) && (!defined(CC_MTK_LOADER)))
INT32        c_dlm_set_rsa_public_key(CHAR *ps_file);
INT32        c_dlm_set_rsa_private_key(CHAR *ps_file);
INT32        c_dlm_set_aes_key_iv(VOID* key, INT32 key_len, VOID* iv, INT32 iv_len);
#endif


#endif
/*----------------------------------------------------------------------------*/
/*! @} */   /* end of  groupMW_DLM*/
/*----------------------------------------------------------------------------*/


