Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

nvs.c 4.3KB

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