/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */

/*
 * ----------------------------------------------------------------------------------
 * The code in this file is copied (with a slight modifications) from:
 *     core_esp8266_eboot_command.cpp - interface to the eboot bootloader
 * ----------------------------------------------------------------------------------
 * Copyright (C) 2015 Ivan Grokhotkov. All rights reserved.
 *
 * This file is part of the esp8266 core for Arduino environment.
 *
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library 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 the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 * ----------------------------------------------------------------------------------
 */


extern "C" {
    #include <eboot/eboot_command.h>
}

#include "CoreLib.h"


namespace eCxx {

namespace NodeMCU {


//
// Helper functions
//
static inline uint32_t ebootCommandcalculate_crc32(const eboot_command* cmd) __ECXX_ATTR_ALWAYS_INLINE;
static inline uint32_t ebootCommandcalculate_crc32(const eboot_command* cmd)
{ return crc32( reinterpret_cast<const uint8_t*>(cmd), offsetof(eboot_command, crc32) ); }


//
// Interface to the eboot bootloader
//
extern int32_t ebootCommandRead(eboot_command* cmd)
{
    const uint32_t dwCount = sizeof(eboot_command) / sizeof(uint32_t);

    uint32_t* dst = reinterpret_cast<uint32_t*>(cmd);
    for(uint32_t i = 0; i < dwCount; ++i) dst[i] = RTC_MEM[i];

    const uint32_t crc32 = ebootCommandcalculate_crc32(cmd);
    if( ( cmd->magic & EBOOT_MAGIC_MASK ) != EBOOT_MAGIC || ( cmd->crc32 != crc32 ) ) return 1;

    return 0;
}


extern void ebootCommandWrite(eboot_command* cmd)
{
    cmd->magic = EBOOT_MAGIC;
    cmd->crc32 = ebootCommandcalculate_crc32(cmd);

    const uint32_t dwCount = sizeof(eboot_command) / sizeof(uint32_t);

    const uint32_t* src = reinterpret_cast<uint32_t*>(cmd);
    for(uint32_t i = 0; i < dwCount; ++i) RTC_MEM[i] = src[i];
}


extern void ebootCommandClear()
{
    RTC_MEM[ offsetof(eboot_command, magic) / sizeof(uint32_t) ] = 0;
    RTC_MEM[ offsetof(eboot_command, crc32) / sizeof(uint32_t) ] = 0;
}


} // namespace NodeMCU

} // namespace eCxx


//
// Function aliases with "C" linkage for compatibility with the Arduino library part
//
extern "C" {

    // make mangle CODE='struct eboot_command; namespace eCxx { namespace NodeMCU { void ebootCommandWrite(eboot_command* cmd) {} } }'
    extern void eboot_command_write(eboot_command* cmd) __ECXX_ATTR_ALIAS("_ZN4eCxx7NodeMCU17ebootCommandWriteEP13eboot_command");

} // extern "C"
