blob: 9e182d5daa7eacf2f1ae2cc268a23402372f8941 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_STORAGE_VOLUME_IMAGE_FTL_RAW_NAND_IMAGE_UTILS_H_
#define SRC_STORAGE_VOLUME_IMAGE_FTL_RAW_NAND_IMAGE_UTILS_H_
#include <lib/fpromise/result.h>
#include <lib/stdcompat/span.h>
#include <cstdint>
#include <fbl/algorithm.h>
#include "src/storage/volume_image/ftl/options.h"
#include "src/storage/volume_image/utils/writer.h"
namespace storage::volume_image {
// Returns the adjusted page size of a RawNandImage with the given |options|.
constexpr uint64_t RawNandImageGetAdjustedPageSize(const RawNandOptions& options) {
return options.page_size + options.oob_bytes_size;
}
// Returns the adjusted erase block size size of a RawNandImage with the given |options|.
constexpr uint64_t RawNandImageGetAdjustedEraseBlockSize(const RawNandOptions& options) {
return options.pages_per_block * RawNandImageGetAdjustedPageSize(options);
}
// Returns the offset in bytes of |page_number| page from the start, with a known |page_size| and
// |oob_bytes_size|.
constexpr uint64_t RawNandImageGetPageOffset(uint64_t page_number, const RawNandOptions& options) {
return page_number * RawNandImageGetAdjustedPageSize(options);
}
// Returns the offset of the first erase block that start after or at |start_offset|.
constexpr uint64_t RawNandImageGetNextEraseBlockOffset(uint64_t start_offset,
const RawNandOptions& options) {
return fbl::round_up(start_offset, RawNandImageGetAdjustedEraseBlockSize(options));
}
// Writes a block of size |oob_bytes.size() + page_content.size()| into |writer| at |offset|.
inline fpromise::result<void, std::string> RawNandImageWritePage(
cpp20::span<const uint8_t> page_content, cpp20::span<const uint8_t> oob_bytes, uint64_t offset,
Writer* writer) {
auto result = writer->Write(offset, page_content);
if (result.is_error()) {
return result.take_error_result();
}
return writer->Write(offset + page_content.size(), oob_bytes);
}
} // namespace storage::volume_image
#endif // SRC_STORAGE_VOLUME_IMAGE_FTL_RAW_NAND_IMAGE_UTILS_H_