Android-cuttlefish cvd tool
io.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 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 */
16
17#ifndef ___FEC_IO_H___
18#define ___FEC_IO_H___
19
20#include <fcntl.h>
21#include <inttypes.h>
22#include <limits.h>
23#include <stdbool.h>
24#include <stdio.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include <crypto_utils/android_pubkey.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#ifndef SHA256_DIGEST_LENGTH
35#define SHA256_DIGEST_LENGTH 32
36#endif
37
38#define FEC_BLOCKSIZE 4096
39#define FEC_DEFAULT_ROOTS 2
40
41#define FEC_MAGIC 0xFECFECFE
42#define FEC_VERSION 0
43
44/* disk format for the header */
45struct fec_header {
46 uint32_t magic;
47 uint32_t version;
48 uint32_t size;
49 uint32_t roots;
50 uint32_t fec_size;
51 uint64_t inp_size;
53} __attribute__ ((packed));
54
55struct fec_status {
56 int flags;
57 int mode;
58 uint64_t errors;
59 uint64_t data_size;
60 uint64_t size;
61};
62
64 bool valid;
65 uint32_t roots;
66 uint64_t blocks;
67 uint64_t rounds;
68 uint64_t start;
69};
70
73 uint64_t data_size;
76 const char *table;
77 uint32_t table_length;
78};
79
80/* flags for fec_open */
81enum {
82 FEC_FS_EXT4 = 1 << 0,
83 FEC_FS_SQUASH = 1 << 1,
84 FEC_VERITY_DISABLE = 1 << 8
85};
86
87struct fec_handle;
88
89/* file access */
90extern int fec_open(struct fec_handle **f, const char *path, int mode,
91 int flags, int roots);
92
93extern int fec_close(struct fec_handle *f);
94
95extern int fec_verity_set_status(struct fec_handle *f, bool enabled);
96
97extern int fec_verity_get_metadata(struct fec_handle *f,
99
100extern int fec_ecc_get_metadata(struct fec_handle *f,
101 struct fec_ecc_metadata *data);
102
103extern int fec_get_status(struct fec_handle *f, struct fec_status *s);
104
105extern int fec_seek(struct fec_handle *f, int64_t offset, int whence);
106
107extern ssize_t fec_read(struct fec_handle *f, void *buf, size_t count);
108
109extern ssize_t fec_pread(struct fec_handle *f, void *buf, size_t count,
110 uint64_t offset);
111
112#ifdef __cplusplus
113} /* extern "C" */
114
115#include <memory>
116#include <string>
117
118/* C++ wrappers for fec_handle and operations */
119namespace fec {
120 using handle = std::unique_ptr<fec_handle, decltype(&fec_close)>;
121
122 class io {
123 public:
124 io() : handle_(nullptr, fec_close) {}
125
126 explicit io(const std::string& fn, int mode = O_RDONLY, int flags = 0,
127 int roots = FEC_DEFAULT_ROOTS) : handle_(nullptr, fec_close) {
128 open(fn, mode, flags, roots);
129 }
130
131 explicit operator bool() const {
132 return !!handle_;
133 }
134
135 bool open(const std::string& fn, int mode = O_RDONLY, int flags = 0,
137 {
138 fec_handle *fh = nullptr;
139 int rc = fec_open(&fh, fn.c_str(), mode, flags, roots);
140 if (!rc) {
141 handle_.reset(fh);
142 }
143 return !rc;
144 }
145
146 bool close() {
147 return !fec_close(handle_.release());
148 }
149
150 bool seek(int64_t offset, int whence) {
151 return !fec_seek(handle_.get(), offset, whence);
152 }
153
154 ssize_t read(void *buf, size_t count) {
155 return fec_read(handle_.get(), buf, count);
156 }
157
158 ssize_t pread(void *buf, size_t count, uint64_t offset) {
159 return fec_pread(handle_.get(), buf, count, offset);
160 }
161
162 bool get_status(fec_status& status) {
163 return !fec_get_status(handle_.get(), &status);
164 }
165
166 bool get_verity_metadata(fec_verity_metadata& data) {
167 return !fec_verity_get_metadata(handle_.get(), &data);
168 }
169
170 bool has_verity() {
172 return get_verity_metadata(data);
173 }
174
175 bool get_ecc_metadata(fec_ecc_metadata& data) {
176 return !fec_ecc_get_metadata(handle_.get(), &data);
177 }
178
179 bool has_ecc() {
181 return get_ecc_metadata(data) && data.valid;
182 }
183
184 bool set_verity_status(bool enabled) {
185 return !fec_verity_set_status(handle_.get(), enabled);
186 }
187
188 private:
189 handle handle_;
190 };
191}
192#endif
193
194#endif /* ___FEC_IO_H___ */
#define ANDROID_PUBKEY_MODULUS_SIZE
Definition: android_pubkey.h:31
int status()
Definition: health.cpp:42
char data[Size]
Definition: incremental_server.cpp:1
int fec_ecc_get_metadata(struct fec_handle *f, struct fec_ecc_metadata *data)
Definition: fec_open.cpp:457
int fec_open(struct fec_handle **f, const char *path, int mode, int flags, int roots)
Definition: fec_open.cpp:497
int fec_close(struct fec_handle *f)
Definition: fec_open.cpp:409
int fec_verity_set_status(struct fec_handle *f, bool enabled)
Definition: fec_verity.cpp:627
struct fec_status __attribute__
uint32_t roots
Definition: io.h:3
int fec_verity_get_metadata(struct fec_handle *f, struct fec_verity_metadata *data)
Definition: fec_open.cpp:429
ssize_t fec_read(struct fec_handle *f, void *buf, size_t count)
Definition: fec_read.cpp:448
#define FEC_DEFAULT_ROOTS
Definition: io.h:39
int fec_get_status(struct fec_handle *f, struct fec_status *s)
Definition: fec_open.cpp:481
ssize_t fec_pread(struct fec_handle *f, void *buf, size_t count, uint64_t offset)
Definition: fec_read.cpp:520
int fec_seek(struct fec_handle *f, int64_t offset, int whence)
Definition: fec_read.cpp:407
#define SHA256_DIGEST_LENGTH
Definition: io.h:35
@ FEC_FS_EXT4
Definition: io.h:82
@ FEC_FS_SQUASH
Definition: io.h:83
@ FEC_VERITY_DISABLE
Definition: io.h:84
Definition: io.h:63
bool valid
Definition: io.h:64
uint64_t blocks
Definition: io.h:66
uint32_t roots
Definition: io.h:65
uint64_t start
Definition: io.h:68
uint64_t rounds
Definition: io.h:67
Definition: fec_private.h:134
int mode
Definition: fec_private.h:138
int flags
Definition: fec_private.h:137
Definition: io.h:45
uint32_t magic
Definition: io.h:46
uint8_t hash[SHA256_DIGEST_LENGTH]
Definition: io.h:52
uint32_t version
Definition: io.h:47
uint64_t inp_size
Definition: io.h:51
uint32_t size
Definition: io.h:48
uint32_t roots
Definition: io.h:49
uint32_t fec_size
Definition: io.h:50
Definition: io.h:55
uint64_t data_size
Definition: io.h:59
int flags
Definition: io.h:56
uint64_t errors
Definition: io.h:58
uint64_t size
Definition: io.h:60
int mode
Definition: io.h:57
Definition: io.h:71
uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE]
Definition: io.h:74
bool disabled
Definition: io.h:72
const char * table
Definition: io.h:76
uint32_t table_length
Definition: io.h:77
uint64_t data_size
Definition: io.h:73
uint8_t ecc_signature[ANDROID_PUBKEY_MODULUS_SIZE]
Definition: io.h:75
#define pread
Definition: sysdeps.h:545
#define read
Definition: sysdeps.h:543
#define open
Definition: sysdeps.h:492
int close(const android::base::unique_fd_impl< T > &) __attribute__((__unavailable__("close called on unique_fd")))