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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. int nvo_applies ( struct settings *settings __unused,
  170. const struct setting *setting ) {
  171. return ( ( setting->scope == NULL ) &&
  172. dhcpopt_applies ( setting->tag ) );
  173. }
  174. /**
  175. * Store value of NVO setting
  176. *
  177. * @v settings Settings block
  178. * @v setting Setting to store
  179. * @v data Setting data, or NULL to clear setting
  180. * @v len Length of setting data
  181. * @ret rc Return status code
  182. */
  183. static int nvo_store ( struct settings *settings, const struct setting *setting,
  184. const void *data, size_t len ) {
  185. struct nvo_block *nvo =
  186. container_of ( settings, struct nvo_block, settings );
  187. int rc;
  188. /* Update stored options */
  189. if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
  190. data, len ) ) != 0 ) {
  191. DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
  192. nvo, len, strerror ( rc ) );
  193. return rc;
  194. }
  195. /* Save updated options to NVS */
  196. if ( ( rc = nvo_save ( nvo ) ) != 0 )
  197. return rc;
  198. return 0;
  199. }
  200. /**
  201. * Fetch value of NVO setting
  202. *
  203. * @v settings Settings block
  204. * @v setting Setting to fetch
  205. * @v data Buffer to fill with setting data
  206. * @v len Length of buffer
  207. * @ret len Length of setting data, or negative error
  208. *
  209. * The actual length of the setting will be returned even if
  210. * the buffer was too small.
  211. */
  212. static int nvo_fetch ( struct settings *settings, struct setting *setting,
  213. void *data, size_t len ) {
  214. struct nvo_block *nvo =
  215. container_of ( settings, struct nvo_block, settings );
  216. return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
  217. }
  218. /** NVO settings operations */
  219. static struct settings_operations nvo_settings_operations = {
  220. .applies = nvo_applies,
  221. .store = nvo_store,
  222. .fetch = nvo_fetch,
  223. };
  224. /**
  225. * Initialise non-volatile stored options
  226. *
  227. * @v nvo Non-volatile options block
  228. * @v nvs Underlying non-volatile storage device
  229. * @v address Address within NVS device
  230. * @v len Length of non-volatile options data
  231. * @v resize Resize method
  232. * @v refcnt Containing object reference counter, or NULL
  233. */
  234. void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
  235. size_t address, size_t len,
  236. int ( * resize ) ( struct nvo_block *nvo, size_t len ),
  237. struct refcnt *refcnt ) {
  238. nvo->nvs = nvs;
  239. nvo->address = address;
  240. nvo->len = len;
  241. nvo->resize = resize;
  242. dhcpopt_init ( &nvo->dhcpopts, NULL, 0, nvo_realloc_dhcpopt );
  243. settings_init ( &nvo->settings, &nvo_settings_operations,
  244. refcnt, NULL );
  245. }
  246. /**
  247. * Register non-volatile stored options
  248. *
  249. * @v nvo Non-volatile options block
  250. * @v parent Parent settings block, or NULL
  251. * @ret rc Return status code
  252. */
  253. int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
  254. int rc;
  255. /* Allocate memory for options */
  256. if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
  257. goto err_realloc;
  258. /* Read data from NVS */
  259. if ( ( rc = nvo_load ( nvo ) ) != 0 )
  260. goto err_load;
  261. /* Register settings */
  262. if ( ( rc = register_settings ( &nvo->settings, parent,
  263. NVO_SETTINGS_NAME ) ) != 0 )
  264. goto err_register;
  265. DBGC ( nvo, "NVO %p registered\n", nvo );
  266. return 0;
  267. err_register:
  268. err_load:
  269. nvo_realloc ( nvo, 0 );
  270. err_realloc:
  271. return rc;
  272. }
  273. /**
  274. * Unregister non-volatile stored options
  275. *
  276. * @v nvo Non-volatile options block
  277. */
  278. void unregister_nvo ( struct nvo_block *nvo ) {
  279. unregister_settings ( &nvo->settings );
  280. nvo_realloc ( nvo, 0 );
  281. DBGC ( nvo, "NVO %p unregistered\n", nvo );
  282. }