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_PRIVATE_H__
18 #define __FEC_PRIVATE_H__
28 #include <sys/syscall.h>
32 #include <utils/Compat.h>
33 #include <mincrypt/rsa.h>
34 #include <openssl/sha.h>
38 /* processing parameters */
39 #define WORK_MIN_THREADS 1
40 #define WORK_MAX_THREADS 64
42 /* verity parameters */
43 #define VERITY_CACHE_BLOCKS 4096
44 #define VERITY_NO_CACHE UINT64_MAX
46 /* verity definitions */
47 #define VERITY_METADATA_SIZE (8 * FEC_BLOCKSIZE)
48 #define VERITY_TABLE_ARGS 10 /* mandatory arguments */
49 #define VERITY_MIN_TABLE_SIZE (VERITY_TABLE_ARGS * 2) /* for a sanity check */
50 #define VERITY_MAX_TABLE_SIZE (VERITY_METADATA_SIZE - sizeof(verity_header))
52 /* verity header and metadata */
53 #define VERITY_MAGIC 0xB001B001
54 #define VERITY_MAGIC_DISABLE 0x46464F56
55 #define VERITY_VERSION 0
56 #define VERITY_TABLE_FIELDS 10
57 #define VERITY_TABLE_VERSION 1
59 struct verity_header {
62 uint8_t signature[RSANUMBYTES];
74 uint64_t start; /* offset in file */
80 uint32_t hash_data_blocks;
82 uint64_t hash_data_offset;
88 uint64_t metadata_start; /* offset in file */
89 uint8_t zero_hash[SHA256_DIGEST_LENGTH];
91 verity_header ecc_header;
94 struct verity_block_info {
102 int flags; /* additional flags passed to fec_open */
103 int mode; /* mode for open(2) */
104 pthread_mutex_t mutex;
113 extern bool raw_pread(fec_handle *f, void *buf, size_t count,
115 extern bool raw_pwrite(fec_handle *f, const void *buf, size_t count,
118 /* processing functions */
119 typedef ssize_t (*read_func)(fec_handle *f, uint8_t *dest, size_t count,
120 uint64_t offset, size_t *errors);
122 extern ssize_t process(fec_handle *f, uint8_t *buf, size_t count,
123 uint64_t offset, read_func func);
125 /* verity functions */
126 extern uint64_t verity_get_size(uint64_t file_size, uint32_t *verity_levels,
127 uint32_t *level_hashes);
129 extern int verity_parse_header(fec_handle *f, uint64_t offset);
131 extern bool verity_check_block(fec_handle *f, const uint8_t *expected,
132 const uint8_t *block);
136 #define unlikely(x) __builtin_expect(!!(x), 0)
137 #define likely(x) __builtin_expect(!!(x), 1)
141 #define __stringify(x) #x
142 #define stringify(x) __stringify(x)
145 /* warnings, errors, debug output */
147 #define __log(func, type, format, args...) \
148 fprintf(stderr, "fec: <%d> " type ": %s: " format "\n", \
149 (int)syscall(SYS_gettid), __FUNCTION__, ##args)
151 #include <cutils/klog.h>
153 #define __log(func, type, format, args...) \
154 KLOG_##func("fec", "<%d> " type ": %s: " format "\n", \
155 (int)syscall(SYS_gettid), __FUNCTION__, ##args)
159 #define debug(format, args...)
161 #define debug(format, args...) __log(DEBUG, "debug", format, ##args)
164 #define warn(format, args...) __log(WARNING, "warning", format, ##args)
165 #define error(format, args...) __log(ERROR, "error", format, ##args)
168 if (unlikely(!(p))) { \
169 error("`%s' failed", #p); \
174 #endif /* __FEC_PRIVATE_H__ */