/* Reflow Oven Controller
*
* Copyright (C) 2020  Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include <reflow-controller/safety/stack-check.h>
#include <stdint.h>
#include <stm-periph/rng.h>
#include <stm-periph/crc-unit.h>

extern char __ld_top_of_stack;
extern char __ld_end_stack;

int32_t stack_check_get_usage()
{
	uint32_t stack_top;
	uint32_t stack_ptr;

	stack_ptr = read_stack_pointer();
	stack_top = (uint32_t)&__ld_top_of_stack;

	return stack_top - stack_ptr;
}

int32_t stack_check_get_free()
{
	uint32_t upper_heap_boundary;
	uint32_t stack_ptr;

	stack_ptr = read_stack_pointer();
	upper_heap_boundary = (uint32_t)&__ld_end_stack;

	return stack_ptr - upper_heap_boundary;
}


#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"

extern uint32_t __ld_start_stack_corruption_detect_area;
extern uint32_t __ld_end_stack_corruption_detect_area;

int stack_check_init_corruption_detect_area(void)
{
	uint32_t *ptr = &__ld_start_stack_corruption_detect_area;
	uint32_t *end_ptr = &__ld_end_stack_corruption_detect_area;
	const uint32_t area_size_in_words = &__ld_end_stack_corruption_detect_area -
						&__ld_start_stack_corruption_detect_area;
	enum random_number_error rng_stat;
	uint32_t rng_number;
	uint32_t crc_val;
	int ret = 0;

	random_number_gen_init(false);

	while (ptr < &end_ptr[-1]) {
		rng_stat = random_number_gen_get_number(&rng_number, true);

		if (rng_stat != RNG_ERROR_OK) {
			ret = -1;
			goto exit_deinit_rng;
		}

		*ptr = rng_number;
		ptr++;
	}

	/* Init CRC unit and leave it on */
	crc_unit_init();
	crc_unit_reset();

	crc_unit_input_array(&__ld_start_stack_corruption_detect_area, area_size_in_words - 1);
	crc_val = crc_unit_get_crc();
	end_ptr[-1] = crc_val;

exit_deinit_rng:
	random_number_gen_deinit();

	return ret;
}

#pragma GCC diagnostic pop

int stack_check_corruption_detect_area(void)
{
	const uint32_t area_size_in_words = &__ld_end_stack_corruption_detect_area -
						&__ld_start_stack_corruption_detect_area;
	crc_unit_reset();
	crc_unit_input_array(&__ld_start_stack_corruption_detect_area, area_size_in_words);
	if (crc_unit_get_crc() == 0UL) {
		return 0;
	} else {
		return -1;
	}
}
