Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_ |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 19 | |
| 20 | // This module defines file descriptors that deal with NAND media. We are |
| 21 | // concerned with raw NAND access (as MTD device), and through UBI layer. |
| 22 | |
| 23 | #include <mtdutils.h> |
| 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/payload_consumer/file_descriptor.h" |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | // A class defining the file descriptor API for raw MTD device. This file |
| 30 | // descriptor supports either random read, or sequential write but not both at |
| 31 | // once. |
| 32 | class MtdFileDescriptor : public EintrSafeFileDescriptor { |
| 33 | public: |
| 34 | MtdFileDescriptor(); |
| 35 | |
| 36 | static bool IsMtd(const char* path); |
| 37 | |
| 38 | bool Open(const char* path, int flags, mode_t mode) override; |
| 39 | bool Open(const char* path, int flags) override; |
| 40 | ssize_t Read(void* buf, size_t count) override; |
| 41 | ssize_t Write(const void* buf, size_t count) override; |
| 42 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 43 | uint64_t BlockDevSize() override { return 0; } |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 44 | bool BlkIoctl(int request, |
| 45 | uint64_t start, |
| 46 | uint64_t length, |
| 47 | int* result) override { |
| 48 | return false; |
| 49 | } |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 50 | bool Close() override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | std::unique_ptr<MtdReadContext, decltype(&mtd_read_close)> read_ctx_; |
| 54 | std::unique_ptr<MtdWriteContext, decltype(&mtd_write_close)> write_ctx_; |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 55 | uint64_t nr_written_; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 58 | struct UbiVolumeInfo { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 59 | // Number of eraseblocks. |
| 60 | uint64_t reserved_ebs; |
| 61 | // Size of each eraseblock. |
| 62 | uint64_t eraseblock_size; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | // A file descriptor to update a UBI volume, similar to MtdFileDescriptor. |
| 66 | // Once the file descriptor is opened for write, the volume is marked as being |
| 67 | // updated. The volume will not be usable until an update is completed. See |
| 68 | // UBI_IOCVOLUP ioctl operation. |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 69 | class UbiFileDescriptor : public EintrSafeFileDescriptor { |
| 70 | public: |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 71 | // Perform some queries about |path| to see if it is a UBI volume. |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 72 | static bool IsUbi(const char* path); |
| 73 | |
| 74 | bool Open(const char* path, int flags, mode_t mode) override; |
| 75 | bool Open(const char* path, int flags) override; |
| 76 | ssize_t Read(void* buf, size_t count) override; |
| 77 | ssize_t Write(const void* buf, size_t count) override; |
| 78 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 79 | uint64_t BlockDevSize() override { return 0; } |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 80 | bool BlkIoctl(int request, |
| 81 | uint64_t start, |
| 82 | uint64_t length, |
| 83 | int* result) override { |
| 84 | return false; |
| 85 | } |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 86 | bool Close() override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 87 | |
| 88 | private: |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 89 | enum Mode { |
| 90 | kReadOnly, |
| 91 | kWriteOnly |
| 92 | }; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 93 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 94 | uint64_t usable_eb_blocks_; |
| 95 | uint64_t eraseblock_size_; |
| 96 | uint64_t volume_size_; |
| 97 | uint64_t nr_written_; |
| 98 | |
| 99 | Mode mode_; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace chromeos_update_engine |
| 103 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 104 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_ |