1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _NRF_HW_MODEL_NHW_NVM_BACKEND_H
8 #define _NRF_HW_MODEL_NHW_NVM_BACKEND_H
9 
10 #include <stdint.h>
11 #include "bs_types.h"
12 #include "nsi_utils.h"
13 
14 #ifdef __cplusplus
15 extern "C"{
16 #endif
17 
18 typedef struct {
19   uint8_t *storage;
20   const char *file_path;
21   const char *type_s;
22   int fd;
23   size_t size;
24   bool erase_at_start;
25   bool rm_at_exit;
26   bool in_ram;
27 } nvm_storage_state_t;
28 
29 struct nhw_nvm_st_args_t {
30   char *file;
31   bool erase;
32   bool rm;
33   bool in_ram;
34 };
35 
36 void nhw_nvm_initialize_data_storage(nvm_storage_state_t *st);
37 void nhw_nvm_clear_storage(nvm_storage_state_t *st);
38 void nhw_nvm_init_storage(nvm_storage_state_t *st, struct nhw_nvm_st_args_t *args,
39                           size_t size, char *type);
40 
41 #define _NVM_BACKEND_PARAMS(x, real_x, arg, X, descr_prefix)                  \
42 { .is_switch = true,                                                          \
43   .option = NSI_STRINGIFY(x) "_erase",                                        \
44   .type = 'b',                                                                \
45   .dest = (void*)&nvmc_args. arg .erase,                                      \
46   .descript = descr_prefix "Reset the " # X " storage to their erase values (0xFF) at boot"\
47 },                                                                            \
48 { .option = NSI_STRINGIFY(x) "_file",                                         \
49   .name = "path",                                                             \
50   .type = 's',                                                                \
51   .dest = (void*)&nvmc_args. arg .file,                                       \
52   .call_when_found = arg_##real_x##_file_found,                               \
53   .descript = descr_prefix "Path to the binary file where the " #X " content "\
54   "is stored (if set, toggles " NSI_STRINGIFY(x) "_in_ram to false)"          \
55 },                                                                            \
56 { .option = NSI_STRINGIFY(x),                                                 \
57   .name = "path",                                                             \
58   .type = 's',                                                                \
59   .dest = (void*)&nvmc_args. arg . file,                                      \
60   .call_when_found = arg_##real_x##_file_found,                               \
61   .descript = descr_prefix "Alias for " NSI_STRINGIFY(x) "_file"              \
62 },                                                                            \
63 { .is_switch = true,                                                          \
64   .option = NSI_STRINGIFY(x) "_rm",                                           \
65   .type = 'b',                                                                \
66   .dest = (void*)&nvmc_args. arg .rm,                                         \
67   .descript = descr_prefix "Remove the " # X " file when terminating the execution "\
68                "(default no)"                                                 \
69 },                                                                            \
70 { .is_switch = true,                                                          \
71   .option = NSI_STRINGIFY(x) "_in_ram",                                       \
72   .type = 'b',                                                                \
73   .call_when_found = arg_##real_x##_in_ram_found,                             \
74   .descript = descr_prefix "(default)  Instead of a file, keep the " #X " content in RAM."\
75         "If this is set " NSI_STRINGIFY(x) "_erase/_file & _rm "              \
76         "are ignored, and the " #X " content is always reset at startup"      \
77 }
78 
79 #define NVM_BACKEND_PARAMS(x, arg, X)                                         \
80 		_NVM_BACKEND_PARAMS(x, x, arg, X, "")
81 
82 #define NVM_BACKEND_PARAMS_ALIAS(x, real_x, arg, X, descr_prefix)             \
83     _NVM_BACKEND_PARAMS(x, real_x, arg, X, descr_prefix)
84 
85 
86 #define NVM_BACKEND_PARAMS_CALLBACS(x, args) \
87 static void arg_##x##_file_found(char *argv, int offset){ \
88   (void) argv; \
89   (void) offset; \
90   nvmc_args. args .in_ram = false; \
91 } \
92 static void arg_##x##_in_ram_found(char *argv, int offset){ \
93   (void) argv; \
94   (void) offset; \
95   nvmc_args. args .in_ram = true; \
96 }
97 
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif /* _NRF_HW_MODEL_NHW_NVM_BACKEND_H */
104