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

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