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.

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