
/*
 * Copyright (C) Jan 2006 Mellanox Technologies Ltd. All rights reserved.
 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#ifndef __IMAGE_ACCESS_H__
#define __IMAGE_ACCESS_H__

#include <mlxfwops_com.h>
#include <fw_ops.h>
#include <compatibility.h>
#include "psid_query_item.h"
#include "mlxfwmanager_common.h"
#include "mlnx_dev.h"
#include "pldmlib/pldm_utils.h"
#include "pldmlib/pldm_pkg.h"

using namespace std;
#define MLNX_ERR_BUFF_SIZE 1024
enum
{
    IMG_SIG_TYPE_UNKNOWN = 0,
    IMG_SIG_TYPE_BIN = 1,
    IMG_SIG_TYPE_MFA = 2,
    IMG_SIG_TYPE_PLDM = 3,
    IMG_SIG_TYPE_MFA2 = 4
};

typedef enum
{
    UNKNOWN_FLOW = -1,
    BINARY_IN_MFA_FLOW = 0,
    PLDM_TYPE_1_IN_MFA_FLOW = 1,
    PLDM_TYPE_2_IN_MFA_FLOW = 2,
    PLDM_FLOW = 3,
    BINARY_FLOW = 4,
    MFA2_FLOW = 5
} image_flow_type_t;

class ImageAccess
{
public:
    ImageAccess(int compareFFV);
    ~ImageAccess();
    int queryDirPsid(string& path, string& psid, string& selector_tag, int image_type, vector<PsidQueryItem>& riv);
    int queryPsid(const string& fname,
                const string& psid,
                string& selector_tag,
                int image_type,
                PsidQueryItem& ri);
    int getImage(const string& fname, u_int8_t** filebuf);
    int getImage(const string& fname, const string& psid, string& selector_tag, int image_type, u_int8_t** filebuf);
    int get_file_content(const string& fname, vector<PsidQueryItem>& riv);

    // PLDM functions
    bool loadPldmPkg(const string& fname);
    int getPldmImage(const string& fname, const string& psid);
    bool getPldmDescriptorByPsid(string psid, u_int16_t type, u_int16_t& descriptor);
    int getPldmComponentByPsid(string psid, ComponentIdentifier compIdentifier, u_int8_t** buff, u_int32_t& buffSize);
    int getPldmContent(vector<PsidQueryItem>& riv,
                       ComponentIdentifier compIdentifier = ComponentIdentifier::Identifier_General);
                       bool isPsidInPldm(string psid);
    FwComponent::comps_ids_t ToCompId(ComponentIdentifier compIdentifier);

    static int getFileSignature(const string& fname);
    static bool hasMFAs(string dir);
    string getLastErrMsg();
    string getlastWarning();
    string getLog();

    image_flow_type_t DetermineFlow(const string& fname, const string& psid);

private:
    int queryPsidMfa(const string& fname, const string& psid, string& selector_tag, int image_type, PsidQueryItem& ri);
    int queryPsidMfa2(const string& fname, const string& psid, string& selector_tag, int image_type, PsidQueryItem& ri);
    int queryPsidBin(const string& fname, const string& psid, PsidQueryItem& ri);
    int getImageBin(const string& fname, u_int8_t** filebuf);
    int getImageMfa(const string& fname, const string& psid, string& selector_tag, int image_type, u_int8_t** filebuf);
    int checkImgSignature(const char* fname);

    int get_mfa_content(const string& fname, vector<PsidQueryItem>& riv);
    int get_mfa2_content(const string& fname, vector<PsidQueryItem>& riv);
    int get_bin_content(const string& fname, vector<PsidQueryItem>& riv);
    int fillQueryItemFromBuffer(const string& fname,
                                const string& psid,
                                const u_int8_t* imageData,
                                u_int32_t imageSize,
                                PsidQueryItem& ri);
    bool extract_pldm_image_info(const u_int8_t* buff, u_int32_t size, PsidQueryItem& ri);
    void parse_image_info_data(u_int8_t* image_info_data, PsidQueryItem& query_item);
    static int getBufferSignature(u_int8_t* buf, u_int32_t size);
    bool openImg(fw_hndl_type_t hndlType,
                char* psid,
                char* fileHndl,
                u_int16_t swDevId = 0,
                u_int32_t* data = nullptr,
                u_int32_t dataSize = 0);
    char _errBuff[MLNX_ERR_BUFF_SIZE];
    int _compareFFV;
    string _log;
    string _errMsg;
    string _warning;
    FwOperations::fw_ops_params_t _imgFwParams;
    FwOperations* _imgFwOps;
    PldmBuffer _pldm_buff;
    PldmPkg _pldmPkg;
};

#endif
