/*
 * Copyright (c) 2018-2019 Atmosphère-NX
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
#include 
#include 
#include 
#include "fsmitm_boot0storage.hpp"
static HosMutex g_boot0_mutex;
static u8 g_boot0_bct_buffer[Boot0Storage::BctEndOffset];
bool Boot0Storage::CanModifyBctPubks() {
    if (IsRcmBugPatched()) {
        /* RCM bug patched. */
        /* Only allow NS to update the BCT pubks. */
        /* AutoRCM on a patched unit will cause a brick, so homebrew should NOT be allowed to write. */
        return this->title_id == sts::ncm::TitleId::Ns;
    } else {
        /* RCM bug unpatched. */
        /* Allow homebrew but not NS to update the BCT pubks. */
        return this->title_id != sts::ncm::TitleId::Ns;
    }
}
Result Boot0Storage::Read(void *_buffer, size_t size, u64 offset) {
    std::scoped_lock lk{g_boot0_mutex};
    return Base::Read(_buffer, size, offset);
}
Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
    std::scoped_lock lk{g_boot0_mutex};
    u8 *buffer = static_cast(_buffer);
    /* Protect the keyblob region from writes. */
    if (offset <= EksStart) {
        if (offset + size < EksStart) {
            /* Fall through, no need to do anything here. */
        } else {
            if (offset + size < EksEnd) {
                /* Adjust size to avoid writing end of data. */
                size = EksStart - offset;
            } else {
                /* Perform portion of write falling past end of keyblobs. */
                const u64 diff = EksEnd - offset;
                R_TRY(Base::Write(buffer + diff, size - diff, EksEnd));
                /* Adjust size to avoid writing end of data. */
                size = EksStart - offset;
            }
        }
    } else {
        if (offset < EksEnd) {
            if (offset + size < EksEnd) {
                /* Ignore writes falling strictly within the region. */
                return ResultSuccess;
            } else {
                /* Only write past the end of the keyblob region. */
                buffer = buffer + (EksEnd - offset);
                size -= (EksEnd - offset);
                offset = EksEnd;
            }
        } else {
            /* Fall through, no need to do anything here. */
        }
    }
    if (size == 0) {
        return ResultSuccess;
    }
    /* We care about protecting autorcm from NS. */
    if (CanModifyBctPubks() || offset >= BctEndOffset || (offset + BctSize >= BctEndOffset && offset % BctSize >= BctPubkEnd)) {
        return Base::Write(buffer, size, offset);
    }
    /* First, let's deal with the data past the end. */
    if (offset + size >= BctEndOffset) {
        const u64 diff = BctEndOffset - offset;
        R_TRY(ProxyStorage::Write(buffer + diff, size - diff, BctEndOffset));
        size = diff;
    }
    /* Read in the current BCT region. */
    R_TRY(ProxyStorage::Read(g_boot0_bct_buffer, BctEndOffset, 0));
    /* Update the bct buffer. */
    for (u64 cur_ofs = offset; cur_ofs < BctEndOffset && cur_ofs < offset + size; cur_ofs++) {
        const u64 cur_bct_rel_ofs = cur_ofs % BctSize;
        if (cur_bct_rel_ofs < BctPubkStart || BctPubkEnd <= cur_bct_rel_ofs) {
            g_boot0_bct_buffer[cur_ofs] = buffer[cur_ofs - offset];
        }
    }
    return ProxyStorage::Write(g_boot0_bct_buffer, BctEndOffset, 0);
}