blob: 1d023c4a6f608f7a39f00f515ad98375a40d23f2 [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
17#ifndef UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_
18#define UPDATE_ENGINE_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 <mtdutils.h>
24
25#include "update_engine/file_descriptor.h"
26
27namespace 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.
32class 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;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080043 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080044
45 private:
46 std::unique_ptr<MtdReadContext, decltype(&mtd_read_close)> read_ctx_;
47 std::unique_ptr<MtdWriteContext, decltype(&mtd_write_close)> write_ctx_;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080048 uint64_t nr_written_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080049};
50
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080051struct UbiVolumeInfo {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080052 // Number of eraseblocks.
53 uint64_t reserved_ebs;
54 // Size of each eraseblock.
55 uint64_t eraseblock_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080056};
57
58// A file descriptor to update a UBI volume, similar to MtdFileDescriptor.
59// Once the file descriptor is opened for write, the volume is marked as being
60// updated. The volume will not be usable until an update is completed. See
61// UBI_IOCVOLUP ioctl operation.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080062class UbiFileDescriptor : public EintrSafeFileDescriptor {
63 public:
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080064 // Perform some queries about |path| to see if it is a UBI volume.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080065 static bool IsUbi(const char* path);
66
67 bool Open(const char* path, int flags, mode_t mode) override;
68 bool Open(const char* path, int flags) override;
69 ssize_t Read(void* buf, size_t count) override;
70 ssize_t Write(const void* buf, size_t count) override;
71 off64_t Seek(off64_t offset, int whence) override;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080072 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080073
74 private:
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080075 enum Mode {
76 kReadOnly,
77 kWriteOnly
78 };
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080079
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080080 uint64_t usable_eb_blocks_;
81 uint64_t eraseblock_size_;
82 uint64_t volume_size_;
83 uint64_t nr_written_;
84
85 Mode mode_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080086};
87
88} // namespace chromeos_update_engine
89
90#endif // UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_