Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_ |
| 6 | #define UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_ |
| 7 | |
| 8 | // This module defines file descriptors that deal with NAND media. We are |
| 9 | // concerned with raw NAND access (as MTD device), and through UBI layer. |
| 10 | |
| 11 | #include <mtdutils.h> |
| 12 | |
| 13 | #include "update_engine/file_descriptor.h" |
| 14 | |
| 15 | namespace chromeos_update_engine { |
| 16 | |
| 17 | // A class defining the file descriptor API for raw MTD device. This file |
| 18 | // descriptor supports either random read, or sequential write but not both at |
| 19 | // once. |
| 20 | class MtdFileDescriptor : public EintrSafeFileDescriptor { |
| 21 | public: |
| 22 | MtdFileDescriptor(); |
| 23 | |
| 24 | static bool IsMtd(const char* path); |
| 25 | |
| 26 | bool Open(const char* path, int flags, mode_t mode) override; |
| 27 | bool Open(const char* path, int flags) override; |
| 28 | ssize_t Read(void* buf, size_t count) override; |
| 29 | ssize_t Write(const void* buf, size_t count) override; |
| 30 | off64_t Seek(off64_t offset, int whence) override; |
| 31 | void Reset() override; |
| 32 | |
| 33 | private: |
| 34 | std::unique_ptr<MtdReadContext, decltype(&mtd_read_close)> read_ctx_; |
| 35 | std::unique_ptr<MtdWriteContext, decltype(&mtd_write_close)> write_ctx_; |
| 36 | }; |
| 37 | |
| 38 | // TODO(namnguyen) This is a placeholder struct. This struct, and the |
| 39 | // UbiFileDescriptor class below will need finalized later. |
| 40 | struct UbiVolumeInfo { |
| 41 | uint64_t size; |
| 42 | }; |
| 43 | |
| 44 | // A file descriptor to update a UBI volume, similar to MtdFileDescriptor. |
| 45 | // Once the file descriptor is opened for write, the volume is marked as being |
| 46 | // updated. The volume will not be usable until an update is completed. See |
| 47 | // UBI_IOCVOLUP ioctl operation. |
| 48 | // TODO(namnguyen) Again, this needs fleshed out when we have better library to |
| 49 | // interact with UBI volumes. I would expect this class to be very similar to |
| 50 | // MtdFileDescriptor, with two different contexts to bridge C-C++ divide. |
| 51 | class UbiFileDescriptor : public EintrSafeFileDescriptor { |
| 52 | public: |
| 53 | static bool IsUbi(const char* path); |
| 54 | |
| 55 | bool Open(const char* path, int flags, mode_t mode) override; |
| 56 | bool Open(const char* path, int flags) override; |
| 57 | ssize_t Read(void* buf, size_t count) override; |
| 58 | ssize_t Write(const void* buf, size_t count) override; |
| 59 | off64_t Seek(off64_t offset, int whence) override; |
| 60 | void Reset() override; |
| 61 | |
| 62 | private: |
| 63 | std::unique_ptr<UbiVolumeInfo> CreateWriteContext(const char* path); |
| 64 | |
| 65 | std::unique_ptr<UbiVolumeInfo> read_ctx_; |
| 66 | std::unique_ptr<UbiVolumeInfo> write_ctx_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace chromeos_update_engine |
| 70 | |
| 71 | #endif // UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_ |