blob: c0170b785cfd97f505ff38ba3b1448ac39feadaa [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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. Nguyenf1d582e2014-12-08 15:07:17 -080016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080019
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 Hassani008c4582019-01-13 16:22:47 -080023#include <memory>
24
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080025#include <mtdutils.h>
26
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/payload_consumer/file_descriptor.h"
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080028
29namespace 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.
34class 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 Deymob86787c2016-05-12 18:46:25 -070045 uint64_t BlockDevSize() override { return 0; }
Alex Deymo79715ad2015-10-02 14:27:53 -070046 bool BlkIoctl(int request,
47 uint64_t start,
48 uint64_t length,
49 int* result) override {
50 return false;
51 }
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080052 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080053
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. Nguyena78b28c2015-03-06 22:30:12 -080057 uint64_t nr_written_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080058};
59
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080060struct UbiVolumeInfo {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080061 // Number of eraseblocks.
62 uint64_t reserved_ebs;
63 // Size of each eraseblock.
64 uint64_t eraseblock_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080065};
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. Nguyenf1d582e2014-12-08 15:07:17 -080071class UbiFileDescriptor : public EintrSafeFileDescriptor {
72 public:
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080073 // Perform some queries about |path| to see if it is a UBI volume.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080074 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 Deymob86787c2016-05-12 18:46:25 -070081 uint64_t BlockDevSize() override { return 0; }
Alex Deymo79715ad2015-10-02 14:27:53 -070082 bool BlkIoctl(int request,
83 uint64_t start,
84 uint64_t length,
85 int* result) override {
86 return false;
87 }
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080088 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080089
90 private:
Amin Hassani008c4582019-01-13 16:22:47 -080091 enum Mode { kReadOnly, kWriteOnly };
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080092
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080093 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. Nguyenf1d582e2014-12-08 15:07:17 -080099};
100
101} // namespace chromeos_update_engine
102
Alex Deymo39910dc2015-11-09 17:04:30 -0800103#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_MTD_FILE_DESCRIPTOR_H_