blob: 5d7758a12f7274900776eefbc3f3bed4de3c41fe [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#include "update_engine/payload_consumer/mtd_file_descriptor.h"
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080018
19#include <fcntl.h>
20#include <mtd/ubi-user.h>
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080021#include <sys/ioctl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
Amin Hassanicd7edbe2017-09-18 17:05:02 -070024
25#include <memory>
26#include <string>
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080027
28#include <base/files/file_path.h>
29#include <base/strings/string_number_conversions.h>
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080030#include <base/strings/string_util.h>
31#include <base/strings/stringprintf.h>
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080032
Alex Deymoe67a8bb2015-11-12 14:58:58 -080033#include "update_engine/common/subprocess.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/utils.h"
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080035
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080036using std::string;
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080037
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080038namespace {
39
40static const char kSysfsClassUbi[] = "/sys/class/ubi/";
41static const char kUsableEbSize[] = "/usable_eb_size";
42static const char kReservedEbs[] = "/reserved_ebs";
43
44using chromeos_update_engine::UbiVolumeInfo;
45using chromeos_update_engine::utils::ReadFile;
46
47// Return a UbiVolumeInfo pointer if |path| is a UBI volume. Otherwise, return
48// a null unique pointer.
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080049std::unique_ptr<UbiVolumeInfo> GetUbiVolumeInfo(const string& path) {
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080050 base::FilePath device_node(path);
51 base::FilePath ubi_name(device_node.BaseName());
52
Alex Deymo39910dc2015-11-09 17:04:30 -080053 string sysfs_node(kSysfsClassUbi);
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080054 sysfs_node.append(ubi_name.MaybeAsASCII());
55
56 std::unique_ptr<UbiVolumeInfo> ret;
57
58 // Obtain volume info from sysfs.
Alex Deymo39910dc2015-11-09 17:04:30 -080059 string s_reserved_ebs;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080060 if (!ReadFile(sysfs_node + kReservedEbs, &s_reserved_ebs)) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080061 LOG(ERROR) << "Cannot read " << sysfs_node + kReservedEbs;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080062 return ret;
63 }
Alex Deymo39910dc2015-11-09 17:04:30 -080064 string s_eb_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080065 if (!ReadFile(sysfs_node + kUsableEbSize, &s_eb_size)) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080066 LOG(ERROR) << "Cannot read " << sysfs_node + kUsableEbSize;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080067 return ret;
68 }
69
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080070 base::TrimWhitespaceASCII(s_reserved_ebs,
71 base::TRIM_TRAILING,
72 &s_reserved_ebs);
73 base::TrimWhitespaceASCII(s_eb_size, base::TRIM_TRAILING, &s_eb_size);
74
75 uint64_t reserved_ebs, eb_size;
76 if (!base::StringToUint64(s_reserved_ebs, &reserved_ebs)) {
77 LOG(ERROR) << "Cannot parse reserved_ebs: " << s_reserved_ebs;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080078 return ret;
79 }
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080080 if (!base::StringToUint64(s_eb_size, &eb_size)) {
81 LOG(ERROR) << "Cannot parse usable_eb_size: " << s_eb_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080082 return ret;
83 }
84
85 ret.reset(new UbiVolumeInfo);
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080086 ret->reserved_ebs = reserved_ebs;
87 ret->eraseblock_size = eb_size;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080088 return ret;
89}
90
91} // namespace
92
93namespace chromeos_update_engine {
94
95MtdFileDescriptor::MtdFileDescriptor()
96 : read_ctx_(nullptr, &mtd_read_close),
97 write_ctx_(nullptr, &mtd_write_close) {}
98
99bool MtdFileDescriptor::IsMtd(const char* path) {
100 uint64_t size;
101 return mtd_node_info(path, &size, nullptr, nullptr) == 0;
102}
103
104bool MtdFileDescriptor::Open(const char* path, int flags, mode_t mode) {
105 // This File Descriptor does not support read and write.
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800106 TEST_AND_RETURN_FALSE((flags & O_ACCMODE) != O_RDWR);
107 // But we need to open the underlying file descriptor in O_RDWR mode because
108 // during write, we need to read back to verify the write actually sticks or
109 // we have to skip the block. That job is done by mtdutils library.
110 if ((flags & O_ACCMODE) == O_WRONLY) {
111 flags &= ~O_ACCMODE;
112 flags |= O_RDWR;
113 }
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800114 TEST_AND_RETURN_FALSE(
115 EintrSafeFileDescriptor::Open(path, flags | O_CLOEXEC, mode));
116
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800117 if ((flags & O_ACCMODE) == O_RDWR) {
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800118 write_ctx_.reset(mtd_write_descriptor(fd_, path));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800119 nr_written_ = 0;
120 } else {
121 read_ctx_.reset(mtd_read_descriptor(fd_, path));
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800122 }
123
124 if (!read_ctx_ && !write_ctx_) {
125 Close();
126 return false;
127 }
128
129 return true;
130}
131
132bool MtdFileDescriptor::Open(const char* path, int flags) {
133 mode_t cur = umask(022);
134 umask(cur);
135 return Open(path, flags, 0777 & ~cur);
136}
137
138ssize_t MtdFileDescriptor::Read(void* buf, size_t count) {
139 CHECK(read_ctx_);
140 return mtd_read_data(read_ctx_.get(), static_cast<char*>(buf), count);
141}
142
143ssize_t MtdFileDescriptor::Write(const void* buf, size_t count) {
144 CHECK(write_ctx_);
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800145 ssize_t result = mtd_write_data(write_ctx_.get(),
146 static_cast<const char*>(buf),
147 count);
148 if (result > 0) {
149 nr_written_ += result;
150 }
151 return result;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800152}
153
154off64_t MtdFileDescriptor::Seek(off64_t offset, int whence) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800155 if (write_ctx_) {
156 // Ignore seek in write mode.
157 return nr_written_;
158 }
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800159 return EintrSafeFileDescriptor::Seek(offset, whence);
160}
161
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800162bool MtdFileDescriptor::Close() {
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800163 read_ctx_.reset();
164 write_ctx_.reset();
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800165 return EintrSafeFileDescriptor::Close();
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800166}
167
168bool UbiFileDescriptor::IsUbi(const char* path) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800169 base::FilePath device_node(path);
170 base::FilePath ubi_name(device_node.BaseName());
Alex Vakulenko0103c362016-01-20 07:56:15 -0800171 TEST_AND_RETURN_FALSE(base::StartsWith(ubi_name.MaybeAsASCII(), "ubi",
172 base::CompareCase::SENSITIVE));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800173
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800174 return static_cast<bool>(GetUbiVolumeInfo(path));
175}
176
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800177bool UbiFileDescriptor::Open(const char* path, int flags, mode_t mode) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800178 std::unique_ptr<UbiVolumeInfo> info = GetUbiVolumeInfo(path);
179 if (!info) {
180 return false;
181 }
182
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800183 // This File Descriptor does not support read and write.
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800184 TEST_AND_RETURN_FALSE((flags & O_ACCMODE) != O_RDWR);
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800185 TEST_AND_RETURN_FALSE(
186 EintrSafeFileDescriptor::Open(path, flags | O_CLOEXEC, mode));
187
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800188 usable_eb_blocks_ = info->reserved_ebs;
189 eraseblock_size_ = info->eraseblock_size;
190 volume_size_ = usable_eb_blocks_ * eraseblock_size_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800191
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800192 if ((flags & O_ACCMODE) == O_WRONLY) {
193 // It's best to use volume update ioctl so that UBI layer will mark the
194 // volume as being updated, and only clear that mark if the update is
195 // successful. We will need to pad to the whole volume size at close.
196 uint64_t vsize = volume_size_;
197 if (ioctl(fd_, UBI_IOCVOLUP, &vsize) != 0) {
198 PLOG(ERROR) << "Cannot issue volume update ioctl";
199 EintrSafeFileDescriptor::Close();
200 return false;
201 }
202 mode_ = kWriteOnly;
203 nr_written_ = 0;
204 } else {
205 mode_ = kReadOnly;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800206 }
207
208 return true;
209}
210
211bool UbiFileDescriptor::Open(const char* path, int flags) {
212 mode_t cur = umask(022);
213 umask(cur);
214 return Open(path, flags, 0777 & ~cur);
215}
216
217ssize_t UbiFileDescriptor::Read(void* buf, size_t count) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800218 CHECK(mode_ == kReadOnly);
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800219 return EintrSafeFileDescriptor::Read(buf, count);
220}
221
222ssize_t UbiFileDescriptor::Write(const void* buf, size_t count) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800223 CHECK(mode_ == kWriteOnly);
224 ssize_t nr_chunk = EintrSafeFileDescriptor::Write(buf, count);
225 if (nr_chunk >= 0) {
226 nr_written_ += nr_chunk;
227 }
228 return nr_chunk;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800229}
230
231off64_t UbiFileDescriptor::Seek(off64_t offset, int whence) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800232 if (mode_ == kWriteOnly) {
233 // Ignore seek in write mode.
234 return nr_written_;
235 }
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800236 return EintrSafeFileDescriptor::Seek(offset, whence);
237}
238
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800239bool UbiFileDescriptor::Close() {
240 bool pad_ok = true;
241 if (IsOpen() && mode_ == kWriteOnly) {
242 char buf[1024];
243 memset(buf, 0xFF, sizeof(buf));
244 while (nr_written_ < volume_size_) {
245 // We have written less than the whole volume. In order for us to clear
246 // the update marker, we need to fill the rest. It is recommended to fill
247 // UBI writes with 0xFF.
248 uint64_t to_write = volume_size_ - nr_written_;
249 if (to_write > sizeof(buf)) {
250 to_write = sizeof(buf);
251 }
252 ssize_t nr_chunk = EintrSafeFileDescriptor::Write(buf, to_write);
253 if (nr_chunk < 0) {
254 LOG(ERROR) << "Cannot 0xFF-pad before closing.";
255 // There is an error, but we can't really do any meaningful thing here.
256 pad_ok = false;
257 break;
258 }
259 nr_written_ += nr_chunk;
260 }
261 }
262 return EintrSafeFileDescriptor::Close() && pad_ok;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800263}
264
265} // namespace chromeos_update_engine