OSDN Git Service

DO NOT MERGE: fec: remove unused mmap code
[android-x86/system-extras.git] / verity / fec / main.cpp
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 extern "C" {
18     #include <fec.h>
19 }
20
21 #undef NDEBUG
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <android-base/file.h>
33 #include "image.h"
34
35 enum {
36     MODE_ENCODE,
37     MODE_DECODE,
38     MODE_PRINTSIZE,
39     MODE_GETECCSTART,
40     MODE_GETVERITYSTART
41 };
42
43 static void encode_rs(struct image_proc_ctx *ctx)
44 {
45     struct image *fcx = ctx->ctx;
46     int j;
47     uint8_t data[fcx->rs_n];
48     uint64_t i;
49
50     for (i = ctx->start; i < ctx->end; i += fcx->rs_n) {
51         for (j = 0; j < fcx->rs_n; ++j) {
52             data[j] = image_get_interleaved_byte(i + j, fcx);
53         }
54
55         encode_rs_char(ctx->rs, data, &fcx->fec[ctx->fec_pos]);
56         ctx->fec_pos += fcx->roots;
57     }
58 }
59
60 static void decode_rs(struct image_proc_ctx *ctx)
61 {
62     struct image *fcx = ctx->ctx;
63     int j, rv;
64     uint8_t data[fcx->rs_n + fcx->roots];
65     uint64_t i;
66
67     assert(sizeof(data) == FEC_RSM);
68
69     for (i = ctx->start; i < ctx->end; i += fcx->rs_n) {
70         for (j = 0; j < fcx->rs_n; ++j) {
71             data[j] = image_get_interleaved_byte(i + j, fcx);
72         }
73
74         memcpy(&data[fcx->rs_n], &fcx->fec[ctx->fec_pos], fcx->roots);
75         rv = decode_rs_char(ctx->rs, data, NULL, 0);
76
77         if (rv < 0) {
78             FATAL("failed to recover [%" PRIu64 ", %" PRIu64 ")\n",
79                 i, i + fcx->rs_n);
80         } else if (rv > 0) {
81             /* copy corrected data to output */
82             for (j = 0; j < fcx->rs_n; ++j) {
83                 image_set_interleaved_byte(i + j, fcx, data[j]);
84             }
85
86             ctx->rv += rv;
87         }
88
89         ctx->fec_pos += fcx->roots;
90     }
91 }
92
93 static int usage()
94 {
95     printf("fec: a tool for encoding and decoding files using RS(255, N).\n"
96            "\n"
97            "usage: fec <mode> [ <options> ] [ <data> <fec> [ <output> ] ]\n"
98            "mode:\n"
99            "  -e  --encode                      encode (default)\n"
100            "  -d  --decode                      decode\n"
101            "  -s, --print-fec-size=<data size>  print FEC size\n"
102            "  -E, --get-ecc-start=data          print ECC offset in data\n"
103            "  -V, --get-verity-start=data       print verity offset\n"
104            "options:\n"
105            "  -h                                show this help\n"
106            "  -v                                enable verbose logging\n"
107            "  -r, --roots=<bytes>               number of parity bytes\n"
108            "  -j, --threads=<threads>           number of threads to use\n"
109            "  -S                                treat data as a sparse file\n"
110            "decoding options:\n"
111            "  -i, --inplace                     correct <data> in place\n"
112         );
113
114     return 1;
115 }
116
117 static uint64_t parse_arg(const char *arg, const char *name, uint64_t maxval)
118 {
119     char* endptr;
120     errno = 0;
121
122     unsigned long long int value = strtoull(arg, &endptr, 0);
123
124     if (arg[0] == '\0' || *endptr != '\0' ||
125             (errno == ERANGE && value == ULLONG_MAX)) {
126         FATAL("invalid value of %s\n", name);
127     }
128     if (value > maxval) {
129         FATAL("value of roots too large (max. %" PRIu64 ")\n", maxval);
130     }
131
132     return (uint64_t)value;
133 }
134
135 static int print_size(image& ctx)
136 {
137     /* output size including header */
138     printf("%" PRIu64 "\n", fec_ecc_get_size(ctx.inp_size, ctx.roots));
139     return 0;
140 }
141
142 static int get_start(int mode, const std::string& filename)
143 {
144     fec::io fh(filename, O_RDONLY, FEC_VERITY_DISABLE);
145
146     if (!fh) {
147         FATAL("failed to open input\n");
148     }
149
150     if (mode == MODE_GETECCSTART) {
151         fec_ecc_metadata data;
152
153         if (!fh.get_ecc_metadata(data)) {
154             FATAL("no ecc data\n");
155         }
156
157         printf("%" PRIu64 "\n", data.start);
158     } else {
159         fec_verity_metadata data;
160
161         if (!fh.get_verity_metadata(data)) {
162             FATAL("no verity data\n");
163         }
164
165         printf("%" PRIu64 "\n", data.data_size);
166     }
167
168     return 0;
169 }
170
171 static int encode(image& ctx, const std::vector<std::string>& inp_filenames,
172         const std::string& fec_filename)
173 {
174     if (ctx.inplace) {
175         FATAL("invalid parameters: inplace can only used when decoding\n");
176     }
177
178     if (!image_load(inp_filenames, &ctx)) {
179         FATAL("failed to read input\n");
180     }
181
182     if (!image_ecc_new(fec_filename, &ctx)) {
183         FATAL("failed to allocate ecc\n");
184     }
185
186     INFO("encoding RS(255, %d) to '%s' for input files:\n", ctx.rs_n,
187         fec_filename.c_str());
188
189     size_t n = 1;
190
191     for (auto fn : inp_filenames) {
192         INFO("\t%zu: '%s'\n", n++, fn.c_str());
193     }
194
195     if (ctx.verbose) {
196         INFO("\traw fec size: %u\n", ctx.fec_size);
197         INFO("\tblocks: %" PRIu64 "\n", ctx.blocks);
198         INFO("\trounds: %" PRIu64 "\n", ctx.rounds);
199     }
200
201     if (!image_process(encode_rs, &ctx)) {
202         FATAL("failed to process input\n");
203     }
204
205     if (!image_ecc_save(&ctx)) {
206         FATAL("failed to write output\n");
207     }
208
209     image_free(&ctx);
210     return 0;
211 }
212
213 static int decode(image& ctx, const std::vector<std::string>& inp_filenames,
214         const std::string& fec_filename, std::string& out_filename)
215 {
216     const std::string& inp_filename = inp_filenames.front();
217
218     if (ctx.inplace && ctx.sparse) {
219         FATAL("invalid parameters: inplace cannot be used with sparse "
220             "files\n");
221     }
222
223     if (!image_ecc_load(fec_filename, &ctx) ||
224             !image_load(inp_filenames, &ctx)) {
225         FATAL("failed to read input\n");
226     }
227
228     if (ctx.inplace) {
229         INFO("correcting '%s' using RS(255, %d) from '%s'\n",
230             inp_filename.c_str(), ctx.rs_n, fec_filename.c_str());
231
232         out_filename = inp_filename;
233     } else {
234         INFO("decoding '%s' to '%s' using RS(255, %d) from '%s'\n",
235             inp_filename.c_str(),
236             out_filename.empty() ? out_filename.c_str() : "<none>", ctx.rs_n,
237             fec_filename.c_str());
238     }
239
240     if (ctx.verbose) {
241         INFO("\traw fec size: %u\n", ctx.fec_size);
242         INFO("\tblocks: %" PRIu64 "\n", ctx.blocks);
243         INFO("\trounds: %" PRIu64 "\n", ctx.rounds);
244     }
245
246     if (!image_process(decode_rs, &ctx)) {
247         FATAL("failed to process input\n");
248     }
249
250     if (ctx.rv) {
251         INFO("corrected %" PRIu64 " errors\n", ctx.rv);
252     } else {
253         INFO("no errors found\n");
254     }
255
256     if (!out_filename.empty() && !image_save(out_filename, &ctx)) {
257         FATAL("failed to write output\n");
258     }
259
260     image_free(&ctx);
261     return 0;
262 }
263
264 int main(int argc, char **argv)
265 {
266     std::string fec_filename;
267     std::string out_filename;
268     std::vector<std::string> inp_filenames;
269     int mode = MODE_ENCODE;
270     image ctx;
271
272     image_init(&ctx);
273     ctx.roots = FEC_DEFAULT_ROOTS;
274
275     while (1) {
276         const static struct option long_options[] = {
277             {"help", no_argument, 0, 'h'},
278             {"encode", no_argument, 0, 'e'},
279             {"decode", no_argument, 0, 'd'},
280             {"sparse", no_argument, 0, 'S'},
281             {"roots", required_argument, 0, 'r'},
282             {"inplace", no_argument, 0, 'i'},
283             {"threads", required_argument, 0, 'j'},
284             {"print-fec-size", required_argument, 0, 's'},
285             {"get-ecc-start", required_argument, 0, 'E'},
286             {"get-verity-start", required_argument, 0, 'V'},
287             {"verbose", no_argument, 0, 'v'},
288             {NULL, 0, 0, 0}
289         };
290         int c = getopt_long(argc, argv, "hedSr:imj:s:E:V:v", long_options, NULL);
291         if (c < 0) {
292             break;
293         }
294
295         switch (c) {
296         case 'h':
297             return usage();
298         case 'S':
299             ctx.sparse = true;
300             break;
301         case 'e':
302             if (mode != MODE_ENCODE) {
303                 return usage();
304             }
305             break;
306         case 'd':
307             if (mode != MODE_ENCODE) {
308                 return usage();
309             }
310             mode = MODE_DECODE;
311             break;
312         case 'r':
313             ctx.roots = (int)parse_arg(optarg, "roots", FEC_RSM);
314             break;
315         case 'i':
316             ctx.inplace = true;
317             break;
318         case 'j':
319             ctx.threads = (int)parse_arg(optarg, "threads", IMAGE_MAX_THREADS);
320             break;
321         case 's':
322             if (mode != MODE_ENCODE) {
323                 return usage();
324             }
325             mode = MODE_PRINTSIZE;
326             ctx.inp_size = parse_arg(optarg, "print-fec-size", UINT64_MAX);
327             break;
328         case 'E':
329             if (mode != MODE_ENCODE) {
330                 return usage();
331             }
332             mode = MODE_GETECCSTART;
333             inp_filenames.push_back(optarg);
334             break;
335         case 'V':
336             if (mode != MODE_ENCODE) {
337                 return usage();
338             }
339             mode = MODE_GETVERITYSTART;
340             inp_filenames.push_back(optarg);
341             break;
342         case 'v':
343             ctx.verbose = true;
344             break;
345         case '?':
346             return usage();
347         default:
348             abort();
349         }
350     }
351
352     argc -= optind;
353     argv += optind;
354
355     assert(ctx.roots > 0 && ctx.roots < FEC_RSM);
356
357     /* check for input / output parameters */
358     if (mode == MODE_ENCODE) {
359         /* allow multiple input files */
360         for (int i = 0; i < (argc - 1); ++i) {
361             inp_filenames.push_back(argv[i]);
362         }
363
364         if (inp_filenames.empty()) {
365             return usage();
366         }
367
368         /* the last one is the output file */
369         fec_filename = argv[argc - 1];
370     } else if (mode == MODE_DECODE) {
371         if (argc < 2 || argc > 3) {
372             return usage();
373         } else if (argc == 3) {
374             if (ctx.inplace) {
375                 return usage();
376             }
377             out_filename = argv[2];
378         }
379
380         inp_filenames.push_back(argv[0]);
381         fec_filename = argv[1];
382     }
383
384     switch (mode) {
385     case MODE_PRINTSIZE:
386         return print_size(ctx);
387     case MODE_GETECCSTART:
388     case MODE_GETVERITYSTART:
389         return get_start(mode, inp_filenames.front());
390     case MODE_ENCODE:
391         return encode(ctx, inp_filenames, fec_filename);
392     case MODE_DECODE:
393         return decode(ctx, inp_filenames, fec_filename, out_filename);
394     default:
395         abort();
396     }
397
398     return 1;
399 }