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.
21 #include <gpxe/dhcp.h>
27 * Non-volatile stored options
32 * Calculate checksum over non-volatile stored options
34 * @v nvo Non-volatile options block
37 static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
38 uint8_t *data = nvo->options->data;
42 for ( i = 0 ; i < nvo->total_len ; i++ ) {
49 * Load non-volatile stored options from non-volatile storage device
51 * @v nvo Non-volatile options block
52 * @ret rc Return status code
54 static int nvo_load ( struct nvo_block *nvo ) {
55 void *data = nvo->options->data;
56 struct nvo_fragment *fragment;
59 /* Read data a fragment at a time */
60 for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
61 if ( ( rc = nvs_read ( nvo->nvs, fragment->address,
62 data, fragment->len ) ) != 0 ) {
63 DBG ( "NVO %p could not read %zd bytes at %#04x\n",
64 nvo, fragment->len, fragment->address );
67 data += fragment->len;
74 * Save non-volatile stored options back to non-volatile storage device
76 * @v nvo Non-volatile options block
77 * @ret rc Return status code
79 int nvo_save ( struct nvo_block *nvo ) {
80 void *data = nvo->options->data;
81 uint8_t *checksum = ( data + nvo->total_len - 1 );
82 struct nvo_fragment *fragment;
85 /* Recalculate checksum */
86 checksum -= nvo_checksum ( nvo );
88 /* Write data a fragment at a time */
89 for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
90 if ( ( rc = nvs_write ( nvo->nvs, fragment->address,
91 data, fragment->len ) ) != 0 ) {
92 DBG ( "NVO %p could not write %zd bytes at %#04x\n",
93 nvo, fragment->len, fragment->address );
96 data += fragment->len;
103 * Parse stored options
105 * @v nvo Non-volatile options block
107 * Verifies that the options data is valid, and configures the DHCP
108 * options block. If the data is not valid, it is replaced with an
109 * empty options block.
111 static void nvo_init_dhcp ( struct nvo_block *nvo ) {
112 struct dhcp_option_block *options = nvo->options;
113 struct dhcp_option *option;
115 /* Steal one byte for the checksum */
116 options->max_len = ( nvo->total_len - 1 );
118 /* Verify checksum over whole block */
119 if ( nvo_checksum ( nvo ) != 0 ) {
120 DBG ( "NVO %p has bad checksum %02x; assuming empty\n",
121 nvo, nvo_checksum ( nvo ) );
125 /* Check that we don't just have a block full of zeroes */
126 option = options->data;
127 if ( option->tag == DHCP_PAD ) {
128 DBG ( "NVO %p has bad start; assuming empty\n", nvo );
132 /* Search for the DHCP_END tag */
133 options->len = options->max_len;
134 option = find_dhcp_option ( options, DHCP_END );
136 DBG ( "NVO %p has no end tag; assuming empty\n", nvo );
140 /* Set correct length of DHCP options */
141 options->len = ( ( void * ) option - options->data + 1 );
142 DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n",
143 nvo, options->len, options->max_len );
147 /* No options found; initialise an empty options block */
148 option = options->data;
149 option->tag = DHCP_END;
155 * Register non-volatile stored options
157 * @v nvo Non-volatile options block
158 * @ret rc Return status code
160 int nvo_register ( struct nvo_block *nvo ) {
161 struct nvo_fragment *fragment = nvo->fragments;
164 /* Calculate total length of all fragments */
166 for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
167 nvo->total_len += fragment->len;
170 /* Allocate memory for options and read in from NVS */
171 nvo->options = alloc_dhcp_options ( nvo->total_len );
172 if ( ! nvo->options ) {
173 DBG ( "NVO %p could not allocate %zd bytes\n",
174 nvo, nvo->total_len );
178 if ( ( rc = nvo_load ( nvo ) ) != 0 )
181 /* Verify and register options */
182 nvo_init_dhcp ( nvo );
183 register_dhcp_options ( nvo->options );
188 free_dhcp_options ( nvo->options );
194 * Unregister non-volatile stored options
196 * @v nvo Non-volatile options block
198 void nvo_unregister ( struct nvo_block *nvo ) {
199 if ( nvo->options ) {
200 unregister_dhcp_options ( nvo->options );
201 free_dhcp_options ( nvo->options );