00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <gpxe/dhcp.h>
00026 #include <gpxe/nvs.h>
00027 #include <gpxe/nvo.h>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
00042 uint8_t *data = nvo->data;
00043 uint8_t sum = 0;
00044 unsigned int i;
00045
00046 for ( i = 0 ; i < nvo->total_len ; i++ ) {
00047 sum += *(data++);
00048 }
00049 return sum;
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 static int nvo_load ( struct nvo_block *nvo ) {
00059 void *data = nvo->data;
00060 struct nvo_fragment *frag;
00061 int rc;
00062
00063
00064 for ( frag = nvo->fragments ; frag->len ; frag++ ) {
00065 if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
00066 frag->len ) ) != 0 ) {
00067 DBGC ( nvo, "NVO %p could not read %zd bytes at "
00068 "%#04x\n", nvo, frag->len, frag->address );
00069 return rc;
00070 }
00071 data += frag->len;
00072 }
00073
00074 DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
00075 return 0;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 static int nvo_save ( struct nvo_block *nvo ) {
00085 void *data = nvo->data;
00086 uint8_t *checksum = data;
00087 struct nvo_fragment *frag;
00088 int rc;
00089
00090
00091 *checksum -= nvo_checksum ( nvo );
00092
00093
00094 for ( frag = nvo->fragments ; frag->len ; frag++ ) {
00095 if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
00096 frag->len ) ) != 0 ) {
00097 DBGC ( nvo, "NVO %p could not write %zd bytes at "
00098 "%#04x\n", nvo, frag->len, frag->address );
00099 return rc;
00100 }
00101 data += frag->len;
00102 }
00103
00104 DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
00105 return 0;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
00118 uint8_t *options_data;
00119 size_t options_len;
00120
00121
00122 options_data = ( nvo->data + 1 );
00123 options_len = ( nvo->total_len - 1 );
00124
00125
00126
00127
00128
00129 if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
00130 DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
00131 "assuming empty\n", nvo, nvo_checksum ( nvo ),
00132 options_data[0] );
00133 memset ( nvo->data, 0, nvo->total_len );
00134 }
00135
00136 dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 static int nvo_store ( struct settings *settings, struct setting *setting,
00149 const void *data, size_t len ) {
00150 struct nvo_block *nvo =
00151 container_of ( settings, struct nvo_block, settings );
00152 int rc;
00153
00154
00155 if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
00156 data, len ) ) != 0 ) {
00157 DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
00158 nvo, len, strerror ( rc ) );
00159 return rc;
00160 }
00161
00162
00163 if ( ( rc = nvo_save ( nvo ) ) != 0 )
00164 return rc;
00165
00166 return 0;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 static int nvo_fetch ( struct settings *settings, struct setting *setting,
00182 void *data, size_t len ) {
00183 struct nvo_block *nvo =
00184 container_of ( settings, struct nvo_block, settings );
00185
00186 return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
00187 }
00188
00189
00190 static struct settings_operations nvo_settings_operations = {
00191 .store = nvo_store,
00192 .fetch = nvo_fetch,
00193 };
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
00204 struct nvo_fragment *fragments, struct refcnt *refcnt ) {
00205 nvo->nvs = nvs;
00206 nvo->fragments = fragments;
00207 settings_init ( &nvo->settings, &nvo_settings_operations, refcnt,
00208 "nvo", 0 );
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
00219 struct nvo_fragment *fragment = nvo->fragments;
00220 int rc;
00221
00222
00223 for ( fragment = nvo->fragments ; fragment->len ; fragment++ )
00224 nvo->total_len += fragment->len;
00225
00226
00227 nvo->data = malloc ( nvo->total_len );
00228 if ( ! nvo->data ) {
00229 DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
00230 nvo, nvo->total_len );
00231 rc = -ENOMEM;
00232 goto err_malloc;
00233 }
00234 if ( ( rc = nvo_load ( nvo ) ) != 0 )
00235 goto err_load;
00236
00237
00238 nvo_init_dhcpopts ( nvo );
00239 if ( ( rc = register_settings ( &nvo->settings, parent ) ) != 0 )
00240 goto err_register;
00241
00242 DBGC ( nvo, "NVO %p registered\n", nvo );
00243 return 0;
00244
00245 err_register:
00246 err_load:
00247 free ( nvo->data );
00248 nvo->data = NULL;
00249 err_malloc:
00250 return rc;
00251 }
00252
00253
00254
00255
00256
00257
00258 void unregister_nvo ( struct nvo_block *nvo ) {
00259 unregister_settings ( &nvo->settings );
00260 free ( nvo->data );
00261 nvo->data = NULL;
00262 DBGC ( nvo, "NVO %p unregistered\n", nvo );
00263 }