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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <stdint.h>
  19. #include <string.h>
  20. #include <gpxe/dhcp.h>
  21. #include <gpxe/nvs.h>
  22. #include <gpxe/nvo.h>
  23. /** @file
  24. *
  25. * Non-volatile stored options
  26. *
  27. */
  28. static size_t nvo_options_len ( struct nvs_options *nvo ) {
  29. struct dhcp_option *option;
  30. uint8_t sum;
  31. unsigned int i;
  32. size_t len;
  33. for ( sum = 0, i = 0 ; i < nvo->nvs->size ; i++ ) {
  34. sum += * ( ( uint8_t * ) ( nvo->options->data + i ) );
  35. }
  36. if ( sum != 0 ) {
  37. DBG ( "NVO %p has bad checksum %02x; assuming empty\n",
  38. nvo, sum );
  39. return 0;
  40. }
  41. option = nvo->options->data;
  42. if ( option->tag == DHCP_PAD ) {
  43. DBG ( "NVO %p has bad start; assuming empty\n", nvo );
  44. return 0;
  45. }
  46. option = find_dhcp_option ( nvo->options, DHCP_END );
  47. if ( ! option ) {
  48. DBG ( "NVO %p has no end tag; assuming empty\n", nvo );
  49. return 0;
  50. }
  51. len = ( ( void * ) option - nvo->options->data + 1 );
  52. DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n",
  53. nvo, len, nvo->nvs->size );
  54. return len;
  55. }
  56. int nvo_register ( struct nvs_options *nvo ) {
  57. struct dhcp_option *option;
  58. int rc;
  59. nvo->options = alloc_dhcp_options ( nvo->nvs->size );
  60. if ( ! nvo->options ) {
  61. DBG ( "NVO %p could not allocate %zd bytes\n",
  62. nvo, nvo->nvs->size );
  63. rc = -ENOMEM;
  64. goto err;
  65. }
  66. if ( ( rc = nvo->nvs->read ( nvo->nvs, 0, nvo->options->data,
  67. nvo->nvs->size ) ) != 0 ) {
  68. DBG ( "NVO %p could not read [0,%zd)\n",
  69. nvo, nvo->nvs->size );
  70. goto err;
  71. }
  72. nvo->options->len = nvo->options->max_len;
  73. nvo->options->len = nvo_options_len ( nvo );
  74. if ( ! nvo->options->len ) {
  75. option = nvo->options->data;
  76. option->tag = DHCP_END;
  77. nvo->options->len = 1;
  78. }
  79. register_dhcp_options ( nvo->options );
  80. return 0;
  81. err:
  82. free_dhcp_options ( nvo->options );
  83. nvo->options = NULL;
  84. return rc;
  85. }
  86. void nvo_unregister ( struct nvs_options *nvo ) {
  87. if ( nvo->options ) {
  88. unregister_dhcp_options ( nvo->options );
  89. free_dhcp_options ( nvo->options );
  90. nvo->options = NULL;
  91. }
  92. }