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.

nvs.c 2.6KB

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