Skip to content

Commit

Permalink
app_storage: Renaming and consistent coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
jarevalo-ledger committed Jun 6, 2024
1 parent c1004d4 commit dfdf6ed
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 67 deletions.
8 changes: 4 additions & 4 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ ifeq ($(ENABLE_SWAP), 1)
endif

#####################################################################
# NVRAM #
# APP STORAGE #
#####################################################################
ifeq ($(ENABLE_NVRAM), 1)
HAVE_APP_NVRAM = 1
DEFINES += HAVE_APP_NVRAM
ifeq ($(ENABLE_APP_STORAGE), 1)
HAVE_APP_STORAGE = 1
DEFINES += HAVE_APP_STORAGE
endif

#####################################################################
Expand Down
74 changes: 43 additions & 31 deletions include/app_storage.h
Original file line number Diff line number Diff line change
@@ -1,69 +1,81 @@
/**
* @file nvram_struct.h
* @brief All definitions of the common part of NVRAM structure, and helpers to access it
* TODO: put in SDK
*/
/*****************************************************************************
* (c) 2024 Ledger SAS.
*
* 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 <stdint.h>
#include <stdbool.h>

#ifndef HAVE_BOLOS
/* "nvram_data.h" needs to be created in all apps including this file */
#include "nvram_data.h"
/* "app_storage_data.h" needs to be created in all apps including this file */
#include "app_storage_data.h"
#endif // HAVE_BOLOS

///< bit to indicate in properties that the NVRAM contains settings
#define CONTAINS_SETTINGS (1 << 0)
///< bit to indicate in properties that the NVRAM contains sensitive data
#define CONTAINS_SENSITIVE_DATA (1 << 1)
///< bit to indicate in properties that the application storage contains settings
#define APP_STORAGE_PROP_SETTINGS (1 << 0)
///< bit to indicate in properties that the application contains data
#define APP_STORAGE_PROP_DATA (1 << 1)

/**
* @brief Structure defining the header of NVRAM
* @brief Structure defining the header of application storage header
*
*/
typedef struct Nvram_header_s {
typedef struct app_storage_header_s {
char tag[4]; ///< ['N','V','R','A'] array, when properly initialized
uint32_t size; ///< size in bytes of the data (Nvram_data_t structure)
uint32_t size; ///< size in bytes of the data (app_storage_data_t structure)
uint16_t struct_version; ///< version of the structure of data (to be set once at first
///< application start-up)
uint16_t properties; ///< used as a bitfield to set properties, like: contains settings,
///< contains sensitive data
uint32_t data_version; ///< version of the content of data (to be updated every time data are
///< updated)
} Nvram_header_t;
} app_storage_header_t;

/**
* @brief Structure defining the NVRAM
* @brief Structure defining the application storage
*
*/
typedef struct Nvram_s {
Nvram_header_t header; ///< header describing the data
typedef struct app_storage_s {
app_storage_header_t header; ///< header describing the data
#ifndef HAVE_BOLOS
Nvram_data_t data; ///< application data, Nvram_data_t must be defined in "nvram_data.h" file
#endif // HAVE_BOLOS
} Nvram_t;
app_storage_data_t data; ///< application data, app_storage_data_t must be defined in
///< "app_storage_data.h" file
#endif // HAVE_BOLOS
} app_storage_t;

#ifndef HAVE_BOLOS
/**
* @brief This variable must be defined in Application code, and never used directly,
* except by @ref N_nvram
*
*/
extern const Nvram_t N_nvram_real;
extern const app_storage_t N_app_storage_real;

/**
* @brief To be used by function accessing NVRAM data (not N_nvram_real)
* @brief To be used by function accessing application storage data (not N_storage_real)
*
*/
#define N_nvram (*(volatile Nvram_t *) PIC(&N_nvram_real))
#define N_app_storage (*(volatile app_storage_t *) PIC(&N_app_storage_real))

void nvram_init(uint32_t data_version);
uint32_t nvram_get_size(void);
uint16_t nvram_get_struct_version(void);
uint16_t nvram_get_properties(void);
uint32_t nvram_get_data_version(void);
bool nvram_is_initalized(void);
void nvram_set_data_version(uint32_t data_version);
void app_storage_init(uint32_t data_version);
uint32_t app_storage_get_size(void);
uint16_t app_storage_get_struct_version(void);
uint16_t app_storage_get_properties(void);
uint32_t app_storage_get_data_version(void);
bool app_storage_is_initalized(void);
void app_storage_set_data_version(uint32_t data_version);

#endif // HAVE_BOLOS
79 changes: 47 additions & 32 deletions lib_standard_app/app_storage.c
Original file line number Diff line number Diff line change
@@ -1,80 +1,93 @@
/**
* @file nvram_struct.c
* @brief helpers to access NVRAM features
* TODO: put in SDK
*/
#ifdef HAVE_APP_NVRAM
#include <string.h>
/*****************************************************************************
* (c) 2024 Ledger SAS.
*
* 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.
*****************************************************************************/

#ifdef HAVE_APP_STORAGE
#include <string.h>
#include "app_storage.h"
#include "os_nvm.h"
#include "os_pic.h"

const Nvram_t N_nvram_real;
// TODO: Create new section for the linker script
/*app_storage app_storage_real __attribute__((section(".bss.app_storage")));*/
const app_storage_t N_app_storage_real;

/**
* @brief init header of NVRAM structure :
* @brief init header of application storage structure :
* - set "NVRA" tag
* - set size
* - set struct and data versions
* - set properties
* @param data_version Version of the data
*/
void nvram_init(uint32_t data_version)
void app_storage_init(uint32_t data_version)
{
Nvram_header_t header;
app_storage_header_t header = {0};

memcpy(header.tag, (void *) "NVRA", 4);
// NVRAM_STRUCT_VERSION and NVRAM_DATA_PROPERTIES must be defined in nvram_data.h
header.struct_version = NVRAM_STRUCT_VERSION;
// APP_STORAGE_DATA_STRUCT_VERSION and APP_STORAGE_PROPERTIES must be defined in
// app_storage_data.h
header.struct_version = APP_STORAGE_DATA_STRUCT_VERSION;
header.data_version = data_version;
header.properties = NVRAM_DATA_PROPERTIES;
header.size = sizeof(Nvram_data_t);
nvm_write((void *) &N_nvram.header, (void *) &header, sizeof(Nvram_header_t));
header.properties = APP_STORAGE_PROPERTIES;
// TODO: Doing this lead to have app storage bigger than needed
header.size = sizeof(app_storage_data_t);
nvm_write((void *) &N_app_storage.header, &header, sizeof(header));
}

/**
* @brief get the size of app data
*/
uint32_t nvram_get_size(void)
uint32_t app_storage_get_size(void)
{
return N_nvram.header.size;
;
return N_app_storage.header.size;
}

/**
* @brief get the version of app data structure
*/
uint16_t nvram_get_struct_version(void)
uint16_t app_storage_get_struct_version(void)
{
return N_nvram.header.struct_version;
return N_app_storage.header.struct_version;
}

/**
* @brief get the version of app data
*/
uint32_t nvram_get_data_version(void)
uint32_t app_storage_get_data_version(void)
{
return N_nvram.header.data_version;
return N_app_storage.header.data_version;
}

/**
* @brief get the properties of app data
*/
uint16_t nvram_get_properties(void)
uint16_t app_storage_get_properties(void)
{
return N_nvram.header.data_version;
return N_app_storage.header.properties;
}

/**
* @brief ensure NVRAM struct is initialized
* @brief ensure app storage struct is initialized
*/
bool nvram_is_initalized(void)
bool app_storage_is_initalized(void)
{
if (memcmp((const void *) N_nvram.header.tag, "NVRA", 4)) {
if (memcmp((void *) N_app_storage.header.tag, "NVRA", 4)) {
return false;
}
if (N_nvram.header.size == 0) {
if (N_app_storage.header.size == 0) {
return false;
}
return true;
Expand All @@ -83,9 +96,11 @@ bool nvram_is_initalized(void)
/**
* @brief set data version of app data
*/
void nvram_set_data_version(uint32_t data_version)
void app_storage_set_data_version(uint32_t data_version)
{
nvm_write((void *) &N_nvram.header.data_version, (void *) &data_version, sizeof(uint32_t));
nvm_write((void *) &N_app_storage.header.data_version,
(void *) &data_version,
sizeof(N_app_storage.header.data_version));
}

#endif // HAVE_APP_NVRAM
#endif // HAVE_APP_STORAGE

0 comments on commit dfdf6ed

Please sign in to comment.