2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <gpxe/dhcp.h>
29 * Non-volatile stored options
34 * Calculate checksum over non-volatile stored options
36 * @v nvo Non-volatile options block
39 static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
40 uint8_t *data = nvo->data;
44 for ( i = 0 ; i < nvo->total_len ; i++ ) {
51 * Load non-volatile stored options from non-volatile storage device
53 * @v nvo Non-volatile options block
54 * @ret rc Return status code
56 static int nvo_load ( struct nvo_block *nvo ) {
57 void *data = nvo->data;
58 struct nvo_fragment *frag;
61 /* Read data a fragment at a time */
62 for ( frag = nvo->fragments ; frag->len ; frag++ ) {
63 if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
64 frag->len ) ) != 0 ) {
65 DBGC ( nvo, "NVO %p could not read %zd bytes at "
66 "%#04x\n", nvo, frag->len, frag->address );
72 DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
77 * Save non-volatile stored options back to non-volatile storage device
79 * @v nvo Non-volatile options block
80 * @ret rc Return status code
82 static int nvo_save ( struct nvo_block *nvo ) {
83 void *data = nvo->data;
84 uint8_t *checksum = data;
85 struct nvo_fragment *frag;
88 /* Recalculate checksum */
89 *checksum -= nvo_checksum ( nvo );
91 /* Write data a fragment at a time */
92 for ( frag = nvo->fragments ; frag->len ; frag++ ) {
93 if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
94 frag->len ) ) != 0 ) {
95 DBGC ( nvo, "NVO %p could not write %zd bytes at "
96 "%#04x\n", nvo, frag->len, frag->address );
102 DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
107 * Parse stored options
109 * @v nvo Non-volatile options block
111 * Verifies that the options data is valid, and configures the DHCP
112 * options block. If the data is not valid, it is replaced with an
113 * empty options block.
115 static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
116 uint8_t *options_data;
119 /* Steal one byte for the checksum */
120 options_data = ( nvo->data + 1 );
121 options_len = ( nvo->total_len - 1 );
123 /* If checksum fails, or options data starts with a zero,
124 * assume the whole block is invalid. This should capture the
125 * case of random initial contents.
127 if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
128 DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
129 "assuming empty\n", nvo, nvo_checksum ( nvo ),
131 memset ( nvo->data, 0, nvo->total_len );
134 dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
138 * Store value of NVO setting
140 * @v settings Settings block
141 * @v setting Setting to store
142 * @v data Setting data, or NULL to clear setting
143 * @v len Length of setting data
144 * @ret rc Return status code
146 static int nvo_store ( struct settings *settings, struct setting *setting,
147 const void *data, size_t len ) {
148 struct nvo_block *nvo =
149 container_of ( settings, struct nvo_block, settings );
152 /* Update stored options */
153 if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
154 data, len ) ) != 0 ) {
155 DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
156 nvo, len, strerror ( rc ) );
160 /* Save updated options to NVS */
161 if ( ( rc = nvo_save ( nvo ) ) != 0 )
168 * Fetch value of NVO setting
170 * @v settings Settings block
171 * @v setting Setting to fetch
172 * @v data Buffer to fill with setting data
173 * @v len Length of buffer
174 * @ret len Length of setting data, or negative error
176 * The actual length of the setting will be returned even if
177 * the buffer was too small.
179 static int nvo_fetch ( struct settings *settings, struct setting *setting,
180 void *data, size_t len ) {
181 struct nvo_block *nvo =
182 container_of ( settings, struct nvo_block, settings );
184 return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
187 /** NVO settings operations */
188 static struct settings_operations nvo_settings_operations = {
194 * Initialise non-volatile stored options
196 * @v nvo Non-volatile options block
197 * @v nvs Underlying non-volatile storage device
198 * @v fragments List of option-containing fragments
199 * @v refcnt Containing object reference counter, or NULL
201 void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
202 struct nvo_fragment *fragments, struct refcnt *refcnt ) {
204 nvo->fragments = fragments;
205 settings_init ( &nvo->settings, &nvo_settings_operations, refcnt,
210 * Register non-volatile stored options
212 * @v nvo Non-volatile options block
213 * @v parent Parent settings block, or NULL
214 * @ret rc Return status code
216 int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
217 struct nvo_fragment *fragment = nvo->fragments;
220 /* Calculate total length of all fragments */
221 for ( fragment = nvo->fragments ; fragment->len ; fragment++ )
222 nvo->total_len += fragment->len;
224 /* Allocate memory for options and read in from NVS */
225 nvo->data = malloc ( nvo->total_len );
227 DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
228 nvo, nvo->total_len );
232 if ( ( rc = nvo_load ( nvo ) ) != 0 )
235 /* Verify and register options */
236 nvo_init_dhcpopts ( nvo );
237 if ( ( rc = register_settings ( &nvo->settings, parent ) ) != 0 )
240 DBGC ( nvo, "NVO %p registered\n", nvo );
252 * Unregister non-volatile stored options
254 * @v nvo Non-volatile options block
256 void unregister_nvo ( struct nvo_block *nvo ) {
257 unregister_settings ( &nvo->settings );
260 DBGC ( nvo, "NVO %p unregistered\n", nvo );