Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <errno.h>
  21. #include <assert.h>
  22. #include <gpxe/nvs.h>
  23. /** @file
  24. *
  25. * Non-volatile storage
  26. *
  27. */
  28. /**
  29. * Read from non-volatile storage device
  30. *
  31. * @v nvs NVS device
  32. * @v address Address from which to read
  33. * @v data Data buffer
  34. * @v len Length of data buffer
  35. * @ret rc Return status code
  36. */
  37. int nvs_read ( struct nvs_device *nvs, unsigned int address,
  38. void *data, size_t len ) {
  39. size_t frag_len;
  40. int rc;
  41. /* We don't even attempt to handle buffer lengths that aren't
  42. * an integral number of words.
  43. */
  44. assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
  45. while ( len ) {
  46. /* Calculate space remaining up to next block boundary */
  47. frag_len = ( ( nvs->block_size -
  48. ( address & ( nvs->block_size - 1 ) ) )
  49. << nvs->word_len_log2 );
  50. /* Limit to space remaining in buffer */
  51. if ( frag_len > len )
  52. frag_len = len;
  53. /* Read this portion of the buffer from the device */
  54. if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
  55. return rc;
  56. /* Update parameters */
  57. data += frag_len;
  58. address += ( frag_len >> nvs->word_len_log2 );
  59. len -= frag_len;
  60. }
  61. return 0;
  62. }
  63. /**
  64. * Verify content of non-volatile storage device
  65. *
  66. * @v nvs NVS device
  67. * @v address Address from which to read
  68. * @v data Data to compare against
  69. * @v len Length of data buffer
  70. * @ret rc Return status code
  71. */
  72. static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
  73. const void *data, size_t len ) {
  74. uint8_t read_data[len];
  75. int rc;
  76. /* Read data into temporary buffer */
  77. if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
  78. return rc;
  79. /* Compare data */
  80. if ( memcmp ( data, read_data, len ) != 0 ) {
  81. DBG ( "NVS %p verification failed at %#04x+%zd\n",
  82. nvs, address, len );
  83. return -EIO;
  84. }
  85. return 0;
  86. }
  87. /**
  88. * Write to non-volatile storage device
  89. *
  90. * @v nvs NVS device
  91. * @v address Address to which to write
  92. * @v data Data buffer
  93. * @v len Length of data buffer
  94. * @ret rc Return status code
  95. */
  96. int nvs_write ( struct nvs_device *nvs, unsigned int address,
  97. const void *data, size_t len ) {
  98. size_t frag_len;
  99. int rc;
  100. /* We don't even attempt to handle buffer lengths that aren't
  101. * an integral number of words.
  102. */
  103. assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
  104. while ( len ) {
  105. /* Calculate space remaining up to next block boundary */
  106. frag_len = ( ( nvs->block_size -
  107. ( address & ( nvs->block_size - 1 ) ) )
  108. << nvs->word_len_log2 );
  109. /* Limit to space remaining in buffer */
  110. if ( frag_len > len )
  111. frag_len = len;
  112. /* Write this portion of the buffer to the device */
  113. if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
  114. return rc;
  115. /* Read back and verify data */
  116. if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
  117. return rc;
  118. /* Update parameters */
  119. data += frag_len;
  120. address += ( frag_len >> nvs->word_len_log2 );
  121. len -= frag_len;
  122. }
  123. return 0;
  124. }