<commit_before>/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#ifndef COMPONENT_FLASHIAP
#error [NOT_SUPPORTED] Target must have internal FlashIAP for this test
#else

#include "mbed.h"
#include <stdio.h>
#include <string.h>
#include "DeviceKey.h"
#include "KVStore.h"
#include "KVMap.h"
#include "kv_config.h"
#include "TDBStore.h"
#include "FlashIAP.h"
#include "FlashIAPBlockDevice.h"
#include "DirectAccessDevicekey.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>

using namespace utest::v1;
using namespace mbed;

#define TEST_DEVICEKEY_LENGTH 32

static inline uint32_t align_up(uint64_t val, uint64_t size)
{
    return (((val - 1) / size) + 1) * size;
}

static inline uint32_t align_down(uint64_t val, uint64_t size)
{
    return (((val) / size)) * size;
}

int  get_virtual_TDBStore_position(uint32_t conf_start_address, uint32_t conf_size,
                                   uint32_t *tdb_start_address, uint32_t *tdb_end_address)
{
    uint32_t bd_final_size = conf_size;
    uint32_t flash_end_address;
    uint32_t flash_start_address;
    uint32_t aligned_start_address;
    FlashIAP flash;
    static const int STORE_SECTORS = 2;

    int ret = flash.init();
    if (ret != 0) {
        return -1;
    }

    uint32_t flash_first_writable_sector_address = (uint32_t)(align_up(FLASHIAP_APP_ROM_END_ADDR,
                                                                       flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)));

    //Get flash parameters before starting
    flash_start_address = flash.get_flash_start();
    flash_end_address = flash_start_address + flash.get_flash_size();

    bool request_default = false;
    if (conf_start_address == 0 && conf_size == 0) {
        request_default = true;
    }

    if (!request_default) {
        if (conf_start_address == 0) {
            conf_start_address = flash_end_address - conf_size;
        }
        aligned_start_address = align_down(conf_start_address, flash.get_sector_size(conf_start_address));
        if (conf_size == 0) {
            bd_final_size = (flash_end_address - aligned_start_address);
        }

        if ((conf_size == 0) || (conf_start_address == 0)) {
            if (aligned_start_address < flash_first_writable_sector_address) {
                flash.deinit();
                return -2;
            }
        }
    } else {
        // Assumption is that last two sectors are reserved for the TDBStore
        aligned_start_address = flash.get_flash_start() + flash.get_flash_size();

        for (int i = STORE_SECTORS; i; i--) {
            bd_size_t sector_size = flash.get_sector_size(aligned_start_address - 1);
            aligned_start_address -= sector_size;
        }

        if (aligned_start_address < flash_first_writable_sector_address) {
            flash.deinit();
            return -2;
        }
        bd_final_size = (flash_end_address - aligned_start_address);
    }

    (*tdb_start_address) = aligned_start_address;
    (*tdb_end_address) = aligned_start_address + bd_final_size;

    flash.deinit();

    return 0;
}


void test_direct_access_to_devicekey_tdb_flashiap_default()
{
    utest_printf("Test Direct Access To DeviceKey Test Entire FlashIAP Default Address\n");

    uint32_t flash_bd_start_address;
    uint32_t flash_bd_end_address;

    int err = get_virtual_TDBStore_position(MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS, MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE, &flash_bd_start_address, &flash_bd_end_address);
    TEST_SKIP_UNLESS_MESSAGE(err != -2, "Test skipped. Not enough available space on Internal FlashIAP");<commit_msg>Reuse TDBStore default size computation in devicekey test<commit_after>/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#ifndef COMPONENT_FLASHIAP
#error [NOT_SUPPORTED] Target must have internal FlashIAP for this test
#else

#include "mbed.h"
#include <stdio.h>
#include <string.h>
#include "DeviceKey.h"
#include "KVStore.h"
#include "KVMap.h"
#include "kv_config.h"
#include "TDBStore.h"
#include "FlashIAP.h"
#include "FlashIAPBlockDevice.h"
#include "DirectAccessDevicekey.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include <stdlib.h>

using namespace utest::v1;
using namespace mbed;

#define TEST_DEVICEKEY_LENGTH 32

static inline uint32_t align_up(uint64_t val, uint64_t size)
{
    return (((val - 1) / size) + 1) * size;
}

static inline uint32_t align_down(uint64_t val, uint64_t size)
{
    return (((val) / size)) * size;
}

int  get_virtual_TDBStore_position(uint32_t conf_start_address, uint32_t conf_size,
                                   uint32_t *tdb_start_address, uint32_t *tdb_end_address)
{
    uint32_t bd_final_size = conf_size;
    uint32_t flash_end_address;
    uint32_t flash_start_address;
    uint32_t aligned_start_address;
    FlashIAP flash;

    int ret = flash.init();
    if (ret != 0) {
        return -1;
    }

    uint32_t flash_first_writable_sector_address = (uint32_t)(align_up(FLASHIAP_APP_ROM_END_ADDR,
                                                                       flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)));

    //Get flash parameters before starting
    flash_start_address = flash.get_flash_start();
    flash_end_address = flash_start_address + flash.get_flash_size();

    bool request_default = false;
    if (conf_start_address == 0 && conf_size == 0) {
        request_default = true;
    }

    if (!request_default) {
        if (conf_start_address == 0) {
            conf_start_address = flash_end_address - conf_size;
        }
        aligned_start_address = align_down(conf_start_address, flash.get_sector_size(conf_start_address));
        if (conf_size == 0) {
            bd_final_size = (flash_end_address - aligned_start_address);
        }

        if ((conf_size == 0) || (conf_start_address == 0)) {
            if (aligned_start_address < flash_first_writable_sector_address) {
                flash.deinit();
                return -2;
            }
        }
    } else {
        bd_addr_t default_start;
        bd_size_t default_size;
        TDBStore::get_default_flash_addresses(&default_start, &default_size);
        aligned_start_address = (uint32_t)default_start;
        bd_final_size = (uint32_t)default_size;
    }

    (*tdb_start_address) = aligned_start_address;
    (*tdb_end_address) = aligned_start_address + bd_final_size;

    flash.deinit();

    return 0;
}


void test_direct_access_to_devicekey_tdb_flashiap_default()
{
    utest_printf("Test Direct Access To DeviceKey Test Entire FlashIAP Default Address\n");

    uint32_t flash_bd_start_address;
    uint32_t flash_bd_end_address;

    int err = get_virtual_TDBStore_position(MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS, MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE, &flash_bd_start_address, &flash_bd_end_address);
    TEST_SKIP_UNLESS_MESSAGE(err != -2, "Test skipped. Not enough available space on Internal FlashIAP");
{'fraction_non_alphanumeric': 0.08845964566929133, 'fraction_numerical': 0.016117125984251968, 'mean_word_length': 3.386940097139773, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2497650', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2617, 'n_words': 624}