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 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 23 | #include <memory> |
| 24 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 25 | #include <mtdutils.h> |
| 26 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/payload_consumer/file_descriptor.h" |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | // A class defining the file descriptor API for raw MTD device. This file |
| 32 | // descriptor supports either random read, or sequential write but not both at |
| 33 | // once. |
| 34 | class MtdFileDescriptor : public EintrSafeFileDescriptor { |
| 35 | public: |
| 36 | MtdFileDescriptor(); |
| 37 | |
| 38 | static bool IsMtd(const char* path); |
| 39 | |
| 40 | bool Open(const char* path, int flags, mode_t mode) override; |
| 41 | bool Open(const char* path, int flags) override; |
| 42 | ssize_t Read(void* buf, size_t count) override; |
| 43 | ssize_t Write(const void* buf, size_t count) override; |
| 44 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 45 | uint64_t BlockDevSize() override { return 0; } |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 46 | bool BlkIoctl(int request, |
| 47 | uint64_t start, |
| 48 | uint64_t length, |
| 49 | int* result) override { |
| 50 | return false; |
| 51 | } |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 52 | bool Close() override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | std::unique_ptr<MtdReadContext, decltype(&mtd_read_close)> read_ctx_; |
| 56 | std::unique_ptr<MtdWriteContext, decltype(&mtd_write_close)> write_ctx_; |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 57 | uint64_t nr_written_; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 60 | struct UbiVolumeInfo { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 61 | // Number of eraseblocks. |
| 62 | uint64_t reserved_ebs; |
| 63 | // Size of each eraseblock. |
| 64 | uint64_t eraseblock_size; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | // A file descriptor to update a UBI volume, similar to MtdFileDescriptor. |
| 68 | // Once the file descriptor is opened for write, the volume is marked as being |
| 69 | // updated. The volume will not be usable until an update is completed. See |
| 70 | // UBI_IOCVOLUP ioctl operation. |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 71 | class UbiFileDescriptor : public EintrSafeFileDescriptor { |
| 72 | public: |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 73 | // 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] | 74 | static bool IsUbi(const char* path); |
| 75 | |
| 76 | bool Open(const char* path, int flags, mode_t mode) override; |
| 77 | bool Open(const char* path, int flags) override; |
| 78 | ssize_t Read(void* buf, size_t count) override; |
| 79 | ssize_t Write(const void* buf, size_t count) override; |
| 80 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 81 | uint64_t BlockDevSize() override { return 0; } |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 82 | bool BlkIoctl(int request, |
| 83 | uint64_t start, |
| 84 | uint64_t length, |
| 85 | int* result) override { |
| 86 | return false; |
| 87 | } |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 88 | bool Close() override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 89 | |
| 90 | private: |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 91 | enum Mode { kReadOnly, kWriteOnly }; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 92 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 93 | uint64_t usable_eb_blocks_; |
| 94 | uint64_t eraseblock_size_; |
| 95 | uint64_t volume_size_; |
| 96 | uint64_t nr_written_; |
| 97 | |
| 98 | Mode mode_; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace chromeos_update_engine |
| 102 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 103 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_ |