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 //
16 
17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_
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 <memory>
24 
25 #include <mtdutils.h>
26 
27 #include "update_engine/payload_consumer/file_descriptor.h"
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;
45   uint64_t BlockDevSize() override { return 0; }
46   bool BlkIoctl(int request,
47                 uint64_t start,
48                 uint64_t length,
49                 int* result) override {
50     return false;
51   }
52   bool Close() override;
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_;
57   uint64_t nr_written_;
58 };
59 
60 struct UbiVolumeInfo {
61   // Number of eraseblocks.
62   uint64_t reserved_ebs;
63   // Size of each eraseblock.
64   uint64_t eraseblock_size;
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.
71 class UbiFileDescriptor : public EintrSafeFileDescriptor {
72  public:
73   // Perform some queries about |path| to see if it is a UBI volume.
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;
81   uint64_t BlockDevSize() override { return 0; }
82   bool BlkIoctl(int request,
83                 uint64_t start,
84                 uint64_t length,
85                 int* result) override {
86     return false;
87   }
88   bool Close() override;
89 
90  private:
91   enum Mode { kReadOnly, kWriteOnly };
92 
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_;
99 };
100 
101 }  // namespace chromeos_update_engine
102 
103 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_
104