blob: ad545b6ed4aa6b4f1163aa1038e8c8ebfc8ee809 [file] [log] [blame]
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_
6#define UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_
7
8// This module defines file descriptors that deal with NAND media. We are
9// concerned with raw NAND access (as MTD device), and through UBI layer.
10
11#include <mtdutils.h>
12
13#include "update_engine/file_descriptor.h"
14
15namespace chromeos_update_engine {
16
17// A class defining the file descriptor API for raw MTD device. This file
18// descriptor supports either random read, or sequential write but not both at
19// once.
20class MtdFileDescriptor : public EintrSafeFileDescriptor {
21 public:
22 MtdFileDescriptor();
23
24 static bool IsMtd(const char* path);
25
26 bool Open(const char* path, int flags, mode_t mode) override;
27 bool Open(const char* path, int flags) override;
28 ssize_t Read(void* buf, size_t count) override;
29 ssize_t Write(const void* buf, size_t count) override;
30 off64_t Seek(off64_t offset, int whence) override;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080031 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080032
33 private:
34 std::unique_ptr<MtdReadContext, decltype(&mtd_read_close)> read_ctx_;
35 std::unique_ptr<MtdWriteContext, decltype(&mtd_write_close)> write_ctx_;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080036 uint64_t nr_written_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080037};
38
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080039struct UbiVolumeInfo {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080040 // Number of eraseblocks.
41 uint64_t reserved_ebs;
42 // Size of each eraseblock.
43 uint64_t eraseblock_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080044};
45
46// A file descriptor to update a UBI volume, similar to MtdFileDescriptor.
47// Once the file descriptor is opened for write, the volume is marked as being
48// updated. The volume will not be usable until an update is completed. See
49// UBI_IOCVOLUP ioctl operation.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080050class UbiFileDescriptor : public EintrSafeFileDescriptor {
51 public:
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080052 // Perform some queries about |path| to see if it is a UBI volume.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080053 static bool IsUbi(const char* path);
54
55 bool Open(const char* path, int flags, mode_t mode) override;
56 bool Open(const char* path, int flags) override;
57 ssize_t Read(void* buf, size_t count) override;
58 ssize_t Write(const void* buf, size_t count) override;
59 off64_t Seek(off64_t offset, int whence) override;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080060 bool Close() override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080061
62 private:
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080063 enum Mode {
64 kReadOnly,
65 kWriteOnly
66 };
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080067
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080068 uint64_t usable_eb_blocks_;
69 uint64_t eraseblock_size_;
70 uint64_t volume_size_;
71 uint64_t nr_written_;
72
73 Mode mode_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080074};
75
76} // namespace chromeos_update_engine
77
78#endif // UPDATE_ENGINE_MTD_FILE_DESCRIPTOR_H_