@@ -20,7 +20,7 @@ pub fn read_full(
20
20
auth_handle : NvAuth ,
21
21
nv_index_handle : NvIndexTpmHandle ,
22
22
) -> Result < Vec < u8 > > {
23
- let mut rw = NvOpenOptions :: Index {
23
+ let mut rw = NvOpenOptions :: ExistingIndex {
24
24
auth_handle,
25
25
nv_index_handle,
26
26
}
@@ -84,13 +84,16 @@ pub fn list(context: &mut Context) -> Result<Vec<(NvPublic, Name)>> {
84
84
}
85
85
86
86
/// Options and flags which can be used to determine how a non-volatile storage index is opened.
87
+ #[ non_exhaustive]
87
88
#[ derive( Debug , Clone ) ]
88
89
pub enum NvOpenOptions {
89
- Public {
90
+ /// Define a new NV space with given auth
91
+ NewIndex {
90
92
nv_public : NvPublic ,
91
93
auth_handle : NvAuth ,
92
94
} ,
93
- Index {
95
+ /// Open the NV space at the given handle, with the given auth
96
+ ExistingIndex {
94
97
nv_index_handle : NvIndexTpmHandle ,
95
98
auth_handle : NvAuth ,
96
99
} ,
@@ -101,18 +104,10 @@ impl NvOpenOptions {
101
104
///
102
105
/// The non-volatile storage index may be used for reading or writing or both.
103
106
pub fn open < ' a > ( & self , context : & ' a mut Context ) -> Result < NvReaderWriter < ' a > > {
104
- let buffer_size = context
105
- . get_tpm_property ( PropertyTag :: NvBufferMax ) ?
106
- . map ( usize:: try_from)
107
- . transpose ( )
108
- . map_err ( |_| {
109
- log:: error!( "Failed to obtain valid maximum NV buffer size" ) ;
110
- Error :: WrapperError ( WrapperErrorKind :: InternalError )
111
- } ) ?
112
- . unwrap_or ( MaxNvBuffer :: MAX_SIZE ) ;
107
+ let buffer_size = max_nv_buffer_size ( context) ?;
113
108
114
109
let ( data_size, nv_idx, auth_handle) = match self {
115
- NvOpenOptions :: Index {
110
+ NvOpenOptions :: ExistingIndex {
116
111
nv_index_handle,
117
112
auth_handle,
118
113
} => {
@@ -128,17 +123,18 @@ impl NvOpenOptions {
128
123
auth_handle,
129
124
)
130
125
}
131
- NvOpenOptions :: Public {
126
+ NvOpenOptions :: NewIndex {
132
127
nv_public,
133
128
auth_handle,
134
- } => {
135
- let auth = AuthHandle :: from ( * auth_handle) ;
136
- (
137
- nv_public. data_size ( ) ,
138
- context. nv_define_space ( auth. try_into ( ) ?, None , nv_public. clone ( ) ) ?,
139
- auth_handle,
140
- )
141
- }
129
+ } => (
130
+ nv_public. data_size ( ) ,
131
+ context. nv_define_space (
132
+ AuthHandle :: from ( * auth_handle) . try_into ( ) ?,
133
+ None ,
134
+ nv_public. clone ( ) ,
135
+ ) ?,
136
+ auth_handle,
137
+ ) ,
142
138
} ;
143
139
144
140
Ok ( NvReaderWriter {
@@ -152,6 +148,19 @@ impl NvOpenOptions {
152
148
}
153
149
}
154
150
151
+ /// Get the maximum buffer size for an NV space.
152
+ pub fn max_nv_buffer_size ( ctx : & mut Context ) -> Result < usize > {
153
+ Ok ( ctx
154
+ . get_tpm_property ( PropertyTag :: NvBufferMax ) ?
155
+ . map ( usize:: try_from)
156
+ . transpose ( )
157
+ . map_err ( |_| {
158
+ log:: error!( "Failed to obtain valid maximum NV buffer size" ) ;
159
+ Error :: WrapperError ( WrapperErrorKind :: InternalError )
160
+ } ) ?
161
+ . unwrap_or ( MaxNvBuffer :: MAX_SIZE ) )
162
+ }
163
+
155
164
/// Non-volatile storage index reader/writer
156
165
///
157
166
/// Provides methods and trait implementations to interact with a non-volatile storage index that has been opened.
0 commit comments