*
*/
+/**
+ * Calculate length up to next block boundary
+ *
+ * @v nvs NVS device
+ * @v address Starting address
+ * @v max_len Maximum length
+ * @ret len Length to use, stopping at block boundaries
+ */
+static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
+ size_t max_len ) {
+ size_t frag_len;
+
+ /* If there are no block boundaries, return the maximum length */
+ if ( ! nvs->block_size )
+ return max_len;
+
+ /* Calculate space remaining up to next block boundary */
+ frag_len = ( ( nvs->block_size -
+ ( address & ( nvs->block_size - 1 ) ) )
+ << nvs->word_len_log2 );
+
+ /* Limit to maximum length */
+ if ( max_len < frag_len )
+ return max_len;
+
+ return frag_len;
+}
+
/**
* Read from non-volatile storage device
*
while ( len ) {
- /* Calculate space remaining up to next block boundary */
- frag_len = ( ( nvs->block_size -
- ( address & ( nvs->block_size - 1 ) ) )
- << nvs->word_len_log2 );
-
- /* Limit to space remaining in buffer */
- if ( frag_len > len )
- frag_len = len;
+ /* Calculate length to read, stopping at block boundaries */
+ frag_len = nvs_frag_len ( nvs, address, len );
/* Read this portion of the buffer from the device */
if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
while ( len ) {
- /* Calculate space remaining up to next block boundary */
- frag_len = ( ( nvs->block_size -
- ( address & ( nvs->block_size - 1 ) ) )
- << nvs->word_len_log2 );
-
- /* Limit to space remaining in buffer */
- if ( frag_len > len )
- frag_len = len;
+ /* Calculate length to write, stopping at block boundaries */
+ frag_len = nvs_frag_len ( nvs, address, len );
/* Write this portion of the buffer to the device */
if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)