/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <stdbool.h>

bool init_rpmb_state(const char* backing_filename);
void destroy_rpmb_state();

/**
 * fail_next_rpmb_writes - Fail the next @count RPMB write commands
 * @count -         Number of subsequent RPMB write commands to fail
 * @commit_writes - If %true, the RPMB device will actually perform the write(s)
 *                  but reply with failure. This simulates issues either in the
 *                  flash chip or in the kernel where the write occurs but
 *                  storage does not receive a valid reply.
 *
 * Used for testing failure conditions
 */
void fail_next_rpmb_writes(int count, bool commit_writes);

/**
 * fail_next_rpmb_reads - Fail the next @count RPMB read commands
 * @count - Number of subsequent RPMB read commands to fail
 *
 * Used for testing failure conditions
 */
void fail_next_rpmb_reads(int count);

/**
 * fail_next_rpmb_get_counters - Fail the next @count RPMB get counter commands
 * @count - Number of subsequent RPMB get counter commands to fail
 *
 * Used for testing failure conditions
 */
void fail_next_rpmb_get_counters(int count);

/**
 * ignore_next_ns_writes - Silently ignore the next @count writes to NS backing
 *                         files
 * @count:      Number of subsequent NS writes to ignore. If %INT_MAX, ignore
 *              all subsequent writes.
 *
 * Used for testing failure conditions
 */
void ignore_next_ns_writes(int count);

/**
 * save_current_ns_state - Save the current the NS backing files so that they
 *                         can be rolled back later.
 *
 * Only one state can be saved at a time. Attempting to save a state while one
 * already exists will trigger an assert failure. Rolling back (with
 * roll_back_ns_state()) will destroy the saved state.
 *
 * Must not be called while the struct block_device_tipc is initialized.
 */
void save_current_ns_state();

/**
 * roll_back_ns_state - Roll back to a state of the NS backing files previously
 *                      saved with save_current_ns_state().
 *
 * This will consume the saved state. Rolling back with no existing saved state
 * will trigger an assert failure.
 *
 * Must not be called while the struct block_device_tipc is initialized.
 */
void roll_back_ns_state();

/**
 * set_is_data_checkpoint_active - Set whether the fake storage proxy will
 * consider data checkpointing to be active, and therefore whether it will
 * disallow writes made with STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT.
 *
 * @is_data_checkpoint_active: The value to set. `true` means to disallow writes
 *                             made with STORAGE_MSG_FLAG_PRE_COMMIT_CHECKPOINT.
 */
void set_is_data_checkpoint_active(bool is_data_checkpoint_active);
