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.

nvo.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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->len ; i++ ) {
  42. sum += *(data++);
  43. }
  44. return sum;
  45. }
  46. /**
  47. * Reallocate non-volatile stored options block
  48. *
  49. * @v nvo Non-volatile options block
  50. * @v len New length
  51. * @ret rc Return status code
  52. */
  53. static int nvo_realloc ( struct nvo_block *nvo, size_t len ) {
  54. void *new_data;
  55. /* Reallocate data */
  56. new_data = realloc ( nvo->data, len );
  57. if ( ! new_data ) {
  58. DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
  59. nvo, len );
  60. return -ENOMEM;
  61. }
  62. nvo->data = new_data;
  63. nvo->len = len;
  64. /* Update DHCP option block */
  65. if ( len ) {
  66. nvo->dhcpopts.data = ( nvo->data + 1 /* checksum */ );
  67. nvo->dhcpopts.alloc_len = ( len - 1 /* checksum */ );
  68. } else {
  69. nvo->dhcpopts.data = NULL;
  70. nvo->dhcpopts.used_len = 0;
  71. nvo->dhcpopts.alloc_len = 0;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * Reallocate non-volatile stored options DHCP option block
  77. *
  78. * @v options DHCP option block
  79. * @v len New length
  80. * @ret rc Return status code
  81. */
  82. static int nvo_realloc_dhcpopt ( struct dhcp_options *options, size_t len ) {
  83. struct nvo_block *nvo =
  84. container_of ( options, struct nvo_block, dhcpopts );
  85. int rc;
  86. /* Refuse to reallocate if we have no way to resize the block */
  87. if ( ! nvo->resize )
  88. return dhcpopt_no_realloc ( options, len );
  89. /* Allow one byte for the checksum (if any data is present) */
  90. if ( len )
  91. len += 1;
  92. /* Resize underlying non-volatile options block */
  93. if ( ( rc = nvo->resize ( nvo, len ) ) != 0 ) {
  94. DBGC ( nvo, "NVO %p could not resize to %zd bytes: %s\n",
  95. nvo, len, strerror ( rc ) );
  96. return rc;
  97. }
  98. /* Reallocate in-memory options block */
  99. if ( ( rc = nvo_realloc ( nvo, len ) ) != 0 )
  100. return rc;
  101. return 0;
  102. }
  103. /**
  104. * Load non-volatile stored options from non-volatile storage device
  105. *
  106. * @v nvo Non-volatile options block
  107. * @ret rc Return status code
  108. */
  109. static int nvo_load ( struct nvo_block *nvo ) {
  110. uint8_t *options_data = nvo->dhcpopts.data;
  111. int rc;
  112. /* Skip reading zero-length NVO fields */
  113. if ( nvo->len == 0 ) {
  114. DBGC ( nvo, "NVO %p is empty; skipping load\n", nvo );
  115. return 0;
  116. }
  117. /* Read data */
  118. if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
  119. nvo->len ) ) != 0 ) {
  120. DBGC ( nvo, "NVO %p could not read %zd bytes at %#04x: %s\n",
  121. nvo, nvo->len, nvo->address, strerror ( rc ) );
  122. return rc;
  123. }
  124. /* If checksum fails, or options data starts with a zero,
  125. * assume the whole block is invalid. This should capture the
  126. * case of random initial contents.
  127. */
  128. if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
  129. DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
  130. "assuming empty\n", nvo, nvo_checksum ( nvo ),
  131. options_data[0] );
  132. memset ( nvo->data, 0, nvo->len );
  133. }
  134. /* Rescan DHCP option block */
  135. dhcpopt_update_used_len ( &nvo->dhcpopts );
  136. DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
  137. return 0;
  138. }
  139. /**
  140. * Save non-volatile stored options back to non-volatile storage device
  141. *
  142. * @v nvo Non-volatile options block
  143. * @ret rc Return status code
  144. */
  145. static int nvo_save ( struct nvo_block *nvo ) {
  146. uint8_t *checksum = nvo->data;
  147. int rc;
  148. /* Recalculate checksum, if applicable */
  149. if ( nvo->len > 0 )
  150. *checksum -= nvo_checksum ( nvo );
  151. /* Write data */
  152. if ( ( rc = nvs_write ( nvo->nvs, nvo->address, nvo->data,
  153. nvo->len ) ) != 0 ) {
  154. DBGC ( nvo, "NVO %p could not write %zd bytes at %#04x: %s\n",
  155. nvo, nvo->len, nvo->address, strerror ( rc ) );
  156. return rc;
  157. }
  158. DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
  159. return 0;
  160. }
  161. /**
  162. * Check applicability of NVO setting
  163. *
  164. * @v settings Settings block
  165. * @v setting Setting
  166. * @ret applies Setting applies within this settings block
  167. */
  168. static int nvo_applies ( struct settings *settings __unused,
  169. struct setting *setting ) {
  170. return dhcpopt_applies ( setting->tag );
  171. }
  172. /**
  173. * Store value of NVO setting
  174. *
  175. * @v settings Settings block
  176. * @v setting Setting to store
  177. * @v data Setting data, or NULL to clear setting
  178. * @v len Length of setting data
  179. * @ret rc Return status code
  180. */
  181. static int nvo_store ( struct settings *settings, struct setting *setting,
  182. const void *data, size_t len ) {
  183. struct nvo_block *nvo =
  184. container_of ( settings, struct nvo_block, settings );
  185. int rc;
  186. /* Update stored options */
  187. if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
  188. data, len ) ) != 0 ) {
  189. DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
  190. nvo, len, strerror ( rc ) );
  191. return rc;
  192. }
  193. /* Save updated options to NVS */
  194. if ( ( rc = nvo_save ( nvo ) ) != 0 )
  195. return rc;
  196. return 0;
  197. }
  198. /**
  199. * Fetch value of NVO setting
  200. *
  201. * @v settings Settings block
  202. * @v setting Setting to fetch
  203. * @v data Buffer to fill with setting data
  204. * @v len Length of buffer
  205. * @ret len Length of setting data, or negative error
  206. *
  207. * The actual length of the setting will be returned even if
  208. * the buffer was too small.
  209. */
  210. static int nvo_fetch ( struct settings *settings, struct setting *setting,
  211. void *data, size_t len ) {
  212. struct nvo_block *nvo =
  213. container_of ( settings, struct nvo_block, settings );
  214. return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
  215. }
  216. /** NVO settings operations */
  217. static struct settings_operations nvo_settings_operations = {
  218. .applies = nvo_applies,
  219. .store = nvo_store,
  220. .fetch = nvo_fetch,
  221. };
  222. /**
  223. * Initialise non-volatile stored options
  224. *
  225. * @v nvo Non-volatile options block
  226. * @v nvs Underlying non-volatile storage device
  227. * @v address Address within NVS device
  228. * @v len Length of non-volatile options data
  229. * @v resize Resize method
  230. * @v refcnt Containing object reference counter, or NULL
  231. */
  232. void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
  233. size_t address, size_t len,
  234. int ( * resize ) ( struct nvo_block *nvo, size_t len ),
  235. struct refcnt *refcnt ) {
  236. nvo->nvs = nvs;
  237. nvo->address = address;
  238. nvo->len = len;
  239. nvo->resize = resize;
  240. dhcpopt_init ( &nvo->dhcpopts, NULL, 0, nvo_realloc_dhcpopt );
  241. settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 );
  242. }
  243. /**
  244. * Register non-volatile stored options
  245. *
  246. * @v nvo Non-volatile options block
  247. * @v parent Parent settings block, or NULL
  248. * @ret rc Return status code
  249. */
  250. int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
  251. int rc;
  252. /* Allocate memory for options */
  253. if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
  254. goto err_realloc;
  255. /* Read data from NVS */
  256. if ( ( rc = nvo_load ( nvo ) ) != 0 )
  257. goto err_load;
  258. /* Register settings */
  259. if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 )
  260. goto err_register;
  261. DBGC ( nvo, "NVO %p registered\n", nvo );
  262. return 0;
  263. err_register:
  264. err_load:
  265. nvo_realloc ( nvo, 0 );
  266. err_realloc:
  267. return rc;
  268. }
  269. /**
  270. * Unregister non-volatile stored options
  271. *
  272. * @v nvo Non-volatile options block
  273. */
  274. void unregister_nvo ( struct nvo_block *nvo ) {
  275. unregister_settings ( &nvo->settings );
  276. nvo_realloc ( nvo, 0 );
  277. DBGC ( nvo, "NVO %p unregistered\n", nvo );
  278. }