2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ___FEC_IO_H___
18 #define ___FEC_IO_H___
25 #include <sys/types.h>
27 #include <mincrypt/rsa.h>
33 #ifndef SHA256_DIGEST_LENGTH
34 #define SHA256_DIGEST_LENGTH 32
37 #define FEC_BLOCKSIZE 4096
38 #define FEC_DEFAULT_ROOTS 2
40 #define FEC_MAGIC 0xFECFECFE
43 /* disk format for the header */
51 uint8_t hash[SHA256_DIGEST_LENGTH];
52 } __attribute__ ((packed));
62 struct fec_ecc_metadata {
70 struct fec_verity_metadata {
73 uint8_t signature[RSANUMBYTES];
74 uint8_t ecc_signature[RSANUMBYTES];
76 uint32_t table_length;
79 /* flags for fec_open */
82 FEC_FS_SQUASH = 1 << 1,
83 FEC_VERITY_DISABLE = 1 << 8
89 extern int fec_open(struct fec_handle **f, const char *path, int mode,
90 int flags, int roots);
92 extern int fec_close(struct fec_handle *f);
94 extern int fec_verity_set_status(struct fec_handle *f, bool enabled);
96 extern int fec_verity_get_metadata(struct fec_handle *f,
97 struct fec_verity_metadata *data);
99 extern int fec_ecc_get_metadata(struct fec_handle *f,
100 struct fec_ecc_metadata *data);
102 extern int fec_get_status(struct fec_handle *f, struct fec_status *s);
104 extern int fec_seek(struct fec_handle *f, int64_t offset, int whence);
106 extern ssize_t fec_read(struct fec_handle *f, void *buf, size_t count);
108 extern ssize_t fec_pread(struct fec_handle *f, void *buf, size_t count,
117 /* C++ wrappers for fec_handle and operations */
119 using handle = std::unique_ptr<fec_handle, decltype(&fec_close)>;
123 io() : handle_(nullptr, fec_close) {}
125 io(const std::string& fn, int mode = O_RDONLY, int flags = 0,
126 int roots = FEC_DEFAULT_ROOTS) : handle_(nullptr, fec_close) {
127 open(fn, mode, flags, roots);
130 explicit operator bool() const {
134 bool open(const std::string& fn, int mode = O_RDONLY, int flags = 0,
135 int roots = FEC_DEFAULT_ROOTS)
137 fec_handle *fh = nullptr;
138 int rc = fec_open(&fh, fn.c_str(), mode, flags, roots);
146 return !fec_close(handle_.release());
149 bool seek(int64_t offset, int whence) {
150 return !fec_seek(handle_.get(), offset, whence);
153 ssize_t read(void *buf, size_t count) {
154 return fec_read(handle_.get(), buf, count);
157 ssize_t pread(void *buf, size_t count, uint64_t offset) {
158 return fec_pread(handle_.get(), buf, count, offset);
161 bool get_status(fec_status& status) {
162 return !fec_get_status(handle_.get(), &status);
165 bool get_verity_metadata(fec_verity_metadata& data) {
166 return !fec_verity_get_metadata(handle_.get(), &data);
170 fec_verity_metadata data;
171 return get_verity_metadata(data);
174 bool get_ecc_metadata(fec_ecc_metadata& data) {
175 return !fec_ecc_get_metadata(handle_.get(), &data);
179 fec_ecc_metadata data;
180 return get_ecc_metadata(data) && data.valid;
183 bool set_verity_status(bool enabled) {
184 return !fec_verity_set_status(handle_.get(), enabled);
193 #endif /* ___FEC_IO_H___ */