You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <ipxe/dhcp.h>
  24. #include <ipxe/nvs.h>
  25. #include <ipxe/nvo.h>
  26. /** @file
  27. *
  28. * Non-volatile stored options
  29. *
  30. */
  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->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->data;
  54. struct nvo_fragment *frag;
  55. int rc;
  56. /* Read data a fragment at a time */
  57. for ( frag = nvo->fragments ; frag->len ; frag++ ) {
  58. if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
  59. frag->len ) ) != 0 ) {
  60. DBGC ( nvo, "NVO %p could not read %zd bytes at "
  61. "%#04x\n", nvo, frag->len, frag->address );
  62. return rc;
  63. }
  64. data += frag->len;
  65. }
  66. DBGC ( nvo, "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. static int nvo_save ( struct nvo_block *nvo ) {
  76. void *data = nvo->data;
  77. uint8_t *checksum = data;
  78. struct nvo_fragment *frag;
  79. int rc;
  80. /* Recalculate checksum */
  81. *checksum -= nvo_checksum ( nvo );
  82. /* Write data a fragment at a time */
  83. for ( frag = nvo->fragments ; frag->len ; frag++ ) {
  84. if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
  85. frag->len ) ) != 0 ) {
  86. DBGC ( nvo, "NVO %p could not write %zd bytes at "
  87. "%#04x\n", nvo, frag->len, frag->address );
  88. return rc;
  89. }
  90. data += frag->len;
  91. }
  92. DBGC ( nvo, "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_dhcpopts ( struct nvo_block *nvo ) {
  105. uint8_t *options_data;
  106. size_t options_len;
  107. /* Steal one byte for the checksum */
  108. options_data = ( nvo->data + 1 );
  109. options_len = ( nvo->total_len - 1 );
  110. /* If checksum fails, or options data starts with a zero,
  111. * assume the whole block is invalid. This should capture the
  112. * case of random initial contents.
  113. */
  114. if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
  115. DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
  116. "assuming empty\n", nvo, nvo_checksum ( nvo ),
  117. options_data[0] );
  118. memset ( nvo->data, 0, nvo->total_len );
  119. }
  120. dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
  121. }
  122. /**
  123. * Store value of NVO setting
  124. *
  125. * @v settings Settings block
  126. * @v setting Setting to store
  127. * @v data Setting data, or NULL to clear setting
  128. * @v len Length of setting data
  129. * @ret rc Return status code
  130. */
  131. static int nvo_store ( struct settings *settings, struct setting *setting,
  132. const void *data, size_t len ) {
  133. struct nvo_block *nvo =
  134. container_of ( settings, struct nvo_block, settings );
  135. int rc;
  136. /* Update stored options */
  137. if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
  138. data, len ) ) != 0 ) {
  139. DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
  140. nvo, len, strerror ( rc ) );
  141. return rc;
  142. }
  143. /* Save updated options to NVS */
  144. if ( ( rc = nvo_save ( nvo ) ) != 0 )
  145. return rc;
  146. return 0;
  147. }
  148. /**
  149. * Fetch value of NVO setting
  150. *
  151. * @v settings Settings block
  152. * @v setting Setting to fetch
  153. * @v data Buffer to fill with setting data
  154. * @v len Length of buffer
  155. * @ret len Length of setting data, or negative error
  156. *
  157. * The actual length of the setting will be returned even if
  158. * the buffer was too small.
  159. */
  160. static int nvo_fetch ( struct settings *settings, struct setting *setting,
  161. void *data, size_t len ) {
  162. struct nvo_block *nvo =
  163. container_of ( settings, struct nvo_block, settings );
  164. return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
  165. }
  166. /** NVO settings operations */
  167. static struct settings_operations nvo_settings_operations = {
  168. .store = nvo_store,
  169. .fetch = nvo_fetch,
  170. };
  171. /**
  172. * Initialise non-volatile stored options
  173. *
  174. * @v nvo Non-volatile options block
  175. * @v nvs Underlying non-volatile storage device
  176. * @v fragments List of option-containing fragments, or NULL
  177. * @v refcnt Containing object reference counter, or NULL
  178. */
  179. void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
  180. struct nvo_fragment *fragments, struct refcnt *refcnt ) {
  181. nvo->nvs = nvs;
  182. nvo->fragments = fragments;
  183. settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 );
  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, if applicable */
  196. if ( fragment ) {
  197. for ( ; fragment->len ; fragment++ )
  198. nvo->total_len += fragment->len;
  199. } else {
  200. nvo->total_len = nvo->nvs->size;
  201. }
  202. /* Allocate memory for options (and fragment list, if applicable) */
  203. nvo->data = zalloc ( nvo->total_len +
  204. ( fragment ? 0 : ( 2 * sizeof ( *fragment ) ) ) );
  205. if ( ! nvo->data ) {
  206. DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
  207. nvo, nvo->total_len );
  208. rc = -ENOMEM;
  209. goto err_malloc;
  210. }
  211. /* Create fragment list, if applicable */
  212. if ( ! fragment ) {
  213. fragment = ( nvo->data + nvo->total_len );
  214. fragment->len = nvo->total_len;
  215. nvo->fragments = fragment;
  216. }
  217. /* Read data from NVS */
  218. if ( ( rc = nvo_load ( nvo ) ) != 0 )
  219. goto err_load;
  220. /* Verify and register options */
  221. nvo_init_dhcpopts ( nvo );
  222. if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 )
  223. goto err_register;
  224. DBGC ( nvo, "NVO %p registered\n", nvo );
  225. return 0;
  226. err_register:
  227. err_load:
  228. free ( nvo->data );
  229. nvo->data = NULL;
  230. err_malloc:
  231. return rc;
  232. }
  233. /**
  234. * Unregister non-volatile stored options
  235. *
  236. * @v nvo Non-volatile options block
  237. */
  238. void unregister_nvo ( struct nvo_block *nvo ) {
  239. unregister_settings ( &nvo->settings );
  240. free ( nvo->data );
  241. nvo->data = NULL;
  242. DBGC ( nvo, "NVO %p unregistered\n", nvo );
  243. }