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

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