/*
 * SPDX-FileCopyrightText: Copyright (c) 2016-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * 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 _BINDATA_PRIVATE_H
#define _BINDATA_PRIVATE_H

#include "core/core.h"
#include "lib/zlib/inflate.h"
#include "core/bin_data.h"

/**************************************************************************************************************
*
*    File:  bin_data_private.h
*
*    Description:
*        Private data structure for binary data management API
*
**************************************************************************************************************/

//
// WARNING: This header should not be included directly (outside of bindata
// impl)
//
// TODO: Clean up the references that have snuck in and move outside of the
// public module directory
//

//
// Private data structure for binary data management
//

//
// Binary data management static information (generated by bindata.pl)
//
#define BINDATA_FLAG_COMPRESSED      0x01
#define BINDATA_FLAG_REFERENCED      0x02
#define BINDATA_FLAG_EXTERNAL        0x04 // Only relevant if GR-3428 is enabled
#define BINDATA_FLAG_EXTERNAL_VMWARE 0x08 // GR-3428: Merge with the above
typedef struct
{
    NvU32           actualSize;         // size of (uncompressed) pData
    NvU32           compressedSize;     // size of (compressed) pData array
    const void *    pData;              // pointer to the raw binary (whether compressed or not) data
    NvU32           flags;              // flags for the bindata entry
    const NvU32     relOffset;          // Offset of bin within bindata file
} BINDATA_STORAGE_PVT, *PBINDATA_STORAGE_PVT;

//
// This knob controls whether the data will be placed into .rodata section and
// be considered constant for the lifetime of RM, or if it can be modified
// during execution. Right now, we only need to modify it on GSP to reclaim
// the memory as general purpose heap.
//
#define BINDATA_IS_MUTABLE RMCFG_FEATURE_PLATFORM_GSP
#if BINDATA_IS_MUTABLE
#define BINDATA_CONST __attribute__((section(".bindata")))
#else
#define BINDATA_CONST const
#endif

void bindataMarkReferenced(const BINDATA_STORAGE *pBinStorage);

typedef struct BINDATA_UCODES_BIN_HEADER {
    //
    // Magic value to identify the file type
    //
    #define BINDATA_UCODES_BIN_MAGIC 0x5345444F4355564E // 'NVUCODES'
    NvU64 magic;
    //
    // To differentiate changes if we ever need to consume multiple versions.
    // The file format does not need to be ABI stable, but it is good to be
    // able to figure out if it's the expected version quickly and error out
    // gracefully.
    //
    #define BINDATA_UCODES_BIN_VERSION 1
    NvU32 version;
    //
    // Number of possible bindata files this driver release can have.
    // This is known at driver build time, and each file has a unique index.
    // This value is also embedded inside the appropriate gsp.bin
    //
    NvU32 numEntries;
    //
    // A hash of all the possible filenames, in their index-order.
    // This value is also embedded inside the appropriate gsp.bin
    // If any files get added or removed, the fingerprint will change and the
    // pairing between ucodes.bin and gsp.bin is broken.
    //
    // For code fixes in gsp.bin, the fingerprint will remain the same and we
    // will not have to update ucodes.bin in order to use the new gsp.bin
    //
    NvU64 fingerprint;
} BINDATA_UCODES_BIN_HEADER;

#endif // _BINDATA_PRIVATE_H
