Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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.
  17. */
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <gpxe/dhcp.h>
  22. #include <gpxe/nvs.h>
  23. #include <gpxe/nvo.h>
  24. /** @file
  25. *
  26. * Non-volatile stored options
  27. *
  28. */
  29. /* FIXME: "Temporary hack" */
  30. struct nvo_block *ugly_nvo_hack = NULL;
  31. /**
  32. * Calculate checksum over non-volatile stored options
  33. *
  34. * @v nvo Non-volatile options block
  35. * @ret sum Checksum
  36. */
  37. static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
  38. uint8_t *data = nvo->options->data;
  39. uint8_t sum = 0;
  40. unsigned int i;
  41. for ( i = 0 ; i < nvo->total_len ; i++ ) {
  42. sum += *(data++);
  43. }
  44. return sum;
  45. }
  46. /**
  47. * Load non-volatile stored options from non-volatile storage device
  48. *
  49. * @v nvo Non-volatile options block
  50. * @ret rc Return status code
  51. */
  52. static int nvo_load ( struct nvo_block *nvo ) {
  53. void *data = nvo->options->data;
  54. struct nvo_fragment *fragment;
  55. int rc;
  56. /* Read data a fragment at a time */
  57. for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
  58. if ( ( rc = nvs_read ( nvo->nvs, fragment->address,
  59. data, fragment->len ) ) != 0 ) {
  60. DBG ( "NVO %p could not read %zd bytes at %#04x\n",
  61. nvo, fragment->len, fragment->address );
  62. return rc;
  63. }
  64. data += fragment->len;
  65. }
  66. DBG ( "NVO %p loaded from non-volatile storage\n", nvo );
  67. return 0;
  68. }
  69. /**
  70. * Save non-volatile stored options back to non-volatile storage device
  71. *
  72. * @v nvo Non-volatile options block
  73. * @ret rc Return status code
  74. */
  75. int nvo_save ( struct nvo_block *nvo ) {
  76. void *data = nvo->options->data;
  77. uint8_t *checksum = ( data + nvo->total_len - 1 );
  78. struct nvo_fragment *fragment;
  79. int rc;
  80. /* Recalculate checksum */
  81. *checksum -= nvo_checksum ( nvo );
  82. /* Write data a fragment at a time */
  83. for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
  84. if ( ( rc = nvs_write ( nvo->nvs, fragment->address,
  85. data, fragment->len ) ) != 0 ) {
  86. DBG ( "NVO %p could not write %zd bytes at %#04x\n",
  87. nvo, fragment->len, fragment->address );
  88. return rc;
  89. }
  90. data += fragment->len;
  91. }
  92. DBG ( "NVO %p saved to non-volatile storage\n", nvo );
  93. return 0;
  94. }
  95. /**
  96. * Parse stored options
  97. *
  98. * @v nvo Non-volatile options block
  99. *
  100. * Verifies that the options data is valid, and configures the DHCP
  101. * options block. If the data is not valid, it is replaced with an
  102. * empty options block.
  103. */
  104. static void nvo_init_dhcp ( struct nvo_block *nvo ) {
  105. struct dhcp_option_block *options = nvo->options;
  106. struct dhcp_option *option;
  107. /* Steal one byte for the checksum */
  108. options->max_len = ( nvo->total_len - 1 );
  109. /* Verify checksum over whole block */
  110. if ( nvo_checksum ( nvo ) != 0 ) {
  111. DBG ( "NVO %p has bad checksum %02x; assuming empty\n",
  112. nvo, nvo_checksum ( nvo ) );
  113. goto empty;
  114. }
  115. /* Check that we don't just have a block full of zeroes */
  116. option = options->data;
  117. if ( option->tag == DHCP_PAD ) {
  118. DBG ( "NVO %p has bad start; assuming empty\n", nvo );
  119. goto empty;
  120. }
  121. /* Search for the DHCP_END tag */
  122. options->len = options->max_len;
  123. option = find_dhcp_option ( options, DHCP_END );
  124. if ( ! option ) {
  125. DBG ( "NVO %p has no end tag; assuming empty\n", nvo );
  126. goto empty;
  127. }
  128. /* Set correct length of DHCP options */
  129. options->len = ( ( void * ) option - options->data + 1 );
  130. DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n",
  131. nvo, options->len, options->max_len );
  132. return;
  133. empty:
  134. /* No options found; initialise an empty options block */
  135. option = options->data;
  136. option->tag = DHCP_END;
  137. options->len = 1;
  138. return;
  139. }
  140. /**
  141. * Register non-volatile stored options
  142. *
  143. * @v nvo Non-volatile options block
  144. * @ret rc Return status code
  145. */
  146. int nvo_register ( struct nvo_block *nvo ) {
  147. struct nvo_fragment *fragment = nvo->fragments;
  148. int rc;
  149. /* Calculate total length of all fragments */
  150. nvo->total_len = 0;
  151. for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
  152. nvo->total_len += fragment->len;
  153. }
  154. /* Allocate memory for options and read in from NVS */
  155. nvo->options = alloc_dhcp_options ( nvo->total_len );
  156. if ( ! nvo->options ) {
  157. DBG ( "NVO %p could not allocate %zd bytes\n",
  158. nvo, nvo->total_len );
  159. rc = -ENOMEM;
  160. goto err;
  161. }
  162. if ( ( rc = nvo_load ( nvo ) ) != 0 )
  163. goto err;
  164. /* Verify and register options */
  165. nvo_init_dhcp ( nvo );
  166. register_dhcp_options ( nvo->options );
  167. ugly_nvo_hack = nvo;
  168. DBG ( "NVO %p registered\n", nvo );
  169. return 0;
  170. err:
  171. dhcpopt_put ( nvo->options );
  172. nvo->options = NULL;
  173. return rc;
  174. }
  175. /**
  176. * Unregister non-volatile stored options
  177. *
  178. * @v nvo Non-volatile options block
  179. */
  180. void nvo_unregister ( struct nvo_block *nvo ) {
  181. if ( nvo->options ) {
  182. unregister_dhcp_options ( nvo->options );
  183. dhcpopt_put ( nvo->options );
  184. nvo->options = NULL;
  185. }
  186. DBG ( "NVO %p unregistered\n", nvo );
  187. ugly_nvo_hack = NULL;
  188. }