您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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