Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <gpxe/dhcp.h>
  23. #include <gpxe/nvs.h>
  24. #include <gpxe/nvo.h>
  25. /** @file
  26. *
  27. * Non-volatile stored options
  28. *
  29. */
  30. /**
  31. * Calculate checksum over non-volatile stored options
  32. *
  33. * @v nvo Non-volatile options block
  34. * @ret sum Checksum
  35. */
  36. static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
  37. uint8_t *data = nvo->data;
  38. uint8_t sum = 0;
  39. unsigned int i;
  40. for ( i = 0 ; i < nvo->total_len ; i++ ) {
  41. sum += *(data++);
  42. }
  43. return sum;
  44. }
  45. /**
  46. * Load non-volatile stored options from non-volatile storage device
  47. *
  48. * @v nvo Non-volatile options block
  49. * @ret rc Return status code
  50. */
  51. static int nvo_load ( struct nvo_block *nvo ) {
  52. void *data = nvo->data;
  53. struct nvo_fragment *frag;
  54. int rc;
  55. /* Read data a fragment at a time */
  56. for ( frag = nvo->fragments ; frag->len ; frag++ ) {
  57. if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
  58. frag->len ) ) != 0 ) {
  59. DBGC ( nvo, "NVO %p could not read %zd bytes at "
  60. "%#04x\n", nvo, frag->len, frag->address );
  61. return rc;
  62. }
  63. data += frag->len;
  64. }
  65. DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
  66. return 0;
  67. }
  68. /**
  69. * Save non-volatile stored options back to non-volatile storage device
  70. *
  71. * @v nvo Non-volatile options block
  72. * @ret rc Return status code
  73. */
  74. static int nvo_save ( struct nvo_block *nvo ) {
  75. void *data = nvo->data;
  76. uint8_t *checksum = data;
  77. struct nvo_fragment *frag;
  78. int rc;
  79. /* Recalculate checksum */
  80. *checksum -= nvo_checksum ( nvo );
  81. /* Write data a fragment at a time */
  82. for ( frag = nvo->fragments ; frag->len ; frag++ ) {
  83. if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
  84. frag->len ) ) != 0 ) {
  85. DBGC ( nvo, "NVO %p could not write %zd bytes at "
  86. "%#04x\n", nvo, frag->len, frag->address );
  87. return rc;
  88. }
  89. data += frag->len;
  90. }
  91. DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
  92. return 0;
  93. }
  94. /**
  95. * Parse stored options
  96. *
  97. * @v nvo Non-volatile options block
  98. *
  99. * Verifies that the options data is valid, and configures the DHCP
  100. * options block. If the data is not valid, it is replaced with an
  101. * empty options block.
  102. */
  103. static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
  104. uint8_t *options_data;
  105. size_t options_len;
  106. /* Steal one byte for the checksum */
  107. options_data = ( nvo->data + 1 );
  108. options_len = ( nvo->total_len - 1 );
  109. /* If checksum fails, or options data starts with a zero,
  110. * assume the whole block is invalid. This should capture the
  111. * case of random initial contents.
  112. */
  113. if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
  114. DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
  115. "assuming empty\n", nvo, nvo_checksum ( nvo ),
  116. options_data[0] );
  117. memset ( nvo->data, 0, nvo->total_len );
  118. }
  119. dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
  120. }
  121. /**
  122. * Store value of NVO setting
  123. *
  124. * @v settings Settings block
  125. * @v setting Setting to store
  126. * @v data Setting data, or NULL to clear setting
  127. * @v len Length of setting data
  128. * @ret rc Return status code
  129. */
  130. static int nvo_store ( struct settings *settings, struct setting *setting,
  131. const void *data, size_t len ) {
  132. struct nvo_block *nvo =
  133. container_of ( settings, struct nvo_block, settings );
  134. int rc;
  135. /* Update stored options */
  136. if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
  137. data, len ) ) != 0 ) {
  138. DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
  139. nvo, len, strerror ( rc ) );
  140. return rc;
  141. }
  142. /* Save updated options to NVS */
  143. if ( ( rc = nvo_save ( nvo ) ) != 0 )
  144. return rc;
  145. return 0;
  146. }
  147. /**
  148. * Fetch value of NVO setting
  149. *
  150. * @v settings Settings block
  151. * @v setting Setting to fetch
  152. * @v data Buffer to fill with setting data
  153. * @v len Length of buffer
  154. * @ret len Length of setting data, or negative error
  155. *
  156. * The actual length of the setting will be returned even if
  157. * the buffer was too small.
  158. */
  159. static int nvo_fetch ( struct settings *settings, struct setting *setting,
  160. void *data, size_t len ) {
  161. struct nvo_block *nvo =
  162. container_of ( settings, struct nvo_block, settings );
  163. return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
  164. }
  165. /** NVO settings operations */
  166. static struct settings_operations nvo_settings_operations = {
  167. .store = nvo_store,
  168. .fetch = nvo_fetch,
  169. };
  170. /**
  171. * Initialise non-volatile stored options
  172. *
  173. * @v nvo Non-volatile options block
  174. * @v nvs Underlying non-volatile storage device
  175. * @v fragments List of option-containing fragments
  176. * @v refcnt Containing object reference counter, or NULL
  177. */
  178. void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
  179. struct nvo_fragment *fragments, struct refcnt *refcnt ) {
  180. nvo->nvs = nvs;
  181. nvo->fragments = fragments;
  182. settings_init ( &nvo->settings, &nvo_settings_operations, refcnt,
  183. "nvo" );
  184. }
  185. /**
  186. * Register non-volatile stored options
  187. *
  188. * @v nvo Non-volatile options block
  189. * @v parent Parent settings block, or NULL
  190. * @ret rc Return status code
  191. */
  192. int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
  193. struct nvo_fragment *fragment = nvo->fragments;
  194. int rc;
  195. /* Calculate total length of all fragments */
  196. for ( fragment = nvo->fragments ; fragment->len ; fragment++ )
  197. nvo->total_len += fragment->len;
  198. /* Allocate memory for options and read in from NVS */
  199. nvo->data = malloc ( nvo->total_len );
  200. if ( ! nvo->data ) {
  201. DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
  202. nvo, nvo->total_len );
  203. rc = -ENOMEM;
  204. goto err_malloc;
  205. }
  206. if ( ( rc = nvo_load ( nvo ) ) != 0 )
  207. goto err_load;
  208. /* Verify and register options */
  209. nvo_init_dhcpopts ( nvo );
  210. if ( ( rc = register_settings ( &nvo->settings, parent ) ) != 0 )
  211. goto err_register;
  212. DBGC ( nvo, "NVO %p registered\n", nvo );
  213. return 0;
  214. err_register:
  215. err_load:
  216. free ( nvo->data );
  217. nvo->data = NULL;
  218. err_malloc:
  219. return rc;
  220. }
  221. /**
  222. * Unregister non-volatile stored options
  223. *
  224. * @v nvo Non-volatile options block
  225. */
  226. void unregister_nvo ( struct nvo_block *nvo ) {
  227. unregister_settings ( &nvo->settings );
  228. free ( nvo->data );
  229. nvo->data = NULL;
  230. DBGC ( nvo, "NVO %p unregistered\n", nvo );
  231. }