/*!
 * In Samsung Ukraine R&D Center (SRK) under a contract between
 * LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
 * and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 * Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
 *
 * Created on: Dec 05, 2017
 * Author: Pavel Marusyk <p.marusik@samsung.com>
 */

#include <stdint.h>
#include <stdlib.h>
#include "stack_protection.h"

#ifdef ARM64
uint64_t __stack_chk_guard = 0;
#else // ARM32
uint32_t __stack_chk_guard = 0;
#endif /* ifdef ARM64 */

#ifdef NO_STACK_CHK_IMPLEMENTED
void stack_protection_init(void)
{
    size_t length = sizeof(__stack_chk_guard);

    tlApiRandomGenerateData(TLAPI_ALG_SECURE_RANDOM, (uint8_t *)&__stack_chk_guard, &length);
}

/*
 * Function that is called when stack corruption detected
 * It is referenced only by a compiler, so no need to declare it in a header
 * In case of specific chipset, already defined into TlEntry.lib.
 */
#ifndef STACK_CHK_FAIL_IMPLEMENTED
void __stack_chk_fail(void)
{
    abort();
}
#endif /* #ifndef STACK_CHK_FAIL_IMPLEMENTED */

#endif /* ifndef NO_STACK_CHK_IMPLEMENTED */
