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
17extern "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
35enum {
36    MODE_ENCODE,
37    MODE_DECODE,
38    MODE_PRINTSIZE,
39    MODE_GETECCSTART,
40    MODE_GETVERITYSTART
41};
42
43static 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
60static 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
93static 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           "  -m, --mmap                        use memory mapping\n"
109           "  -j, --threads=<threads>           number of threads to use\n"
110           "  -S                                treat data as a sparse file\n"
111           "decoding options:\n"
112           "  -i, --inplace                     correct <data> in place\n"
113        );
114
115    return 1;
116}
117
118static uint64_t parse_arg(const char *arg, const char *name, uint64_t maxval)
119{
120    char* endptr;
121    errno = 0;
122
123    unsigned long long int value = strtoull(arg, &endptr, 0);
124
125    if (arg[0] == '\0' || *endptr != '\0' ||
126            (errno == ERANGE && value == ULLONG_MAX)) {
127        FATAL("invalid value of %s\n", name);
128    }
129    if (value > maxval) {
130        FATAL("value of roots too large (max. %" PRIu64 ")\n", maxval);
131    }
132
133    return (uint64_t)value;
134}
135
136static int print_size(image& ctx)
137{
138    /* output size including header */
139    printf("%" PRIu64 "\n", fec_ecc_get_size(ctx.inp_size, ctx.roots));
140    return 0;
141}
142
143static int get_start(int mode, const std::string& filename)
144{
145    fec::io fh(filename, O_RDONLY, FEC_VERITY_DISABLE);
146
147    if (!fh) {
148        FATAL("failed to open input\n");
149    }
150
151    if (mode == MODE_GETECCSTART) {
152        fec_ecc_metadata data;
153
154        if (!fh.get_ecc_metadata(data)) {
155            FATAL("no ecc data\n");
156        }
157
158        printf("%" PRIu64 "\n", data.start);
159    } else {
160        fec_verity_metadata data;
161
162        if (!fh.get_verity_metadata(data)) {
163            FATAL("no verity data\n");
164        }
165
166        printf("%" PRIu64 "\n", data.data_size);
167    }
168
169    return 0;
170}
171
172static int encode(image& ctx, const std::vector<std::string>& inp_filenames,
173        const std::string& fec_filename)
174{
175    if (ctx.inplace) {
176        FATAL("invalid parameters: inplace can only used when decoding\n");
177    }
178
179    if (!image_load(inp_filenames, &ctx, false)) {
180        FATAL("failed to read input\n");
181    }
182
183    if (!image_ecc_new(fec_filename, &ctx)) {
184        FATAL("failed to allocate ecc\n");
185    }
186
187    INFO("encoding RS(255, %d) to '%s' for input files:\n", ctx.rs_n,
188        fec_filename.c_str());
189
190    size_t n = 1;
191
192    for (auto fn : inp_filenames) {
193        INFO("\t%zu: '%s'\n", n++, fn.c_str());
194    }
195
196    if (ctx.verbose) {
197        INFO("\traw fec size: %u\n", ctx.fec_size);
198        INFO("\tblocks: %" PRIu64 "\n", ctx.blocks);
199        INFO("\trounds: %" PRIu64 "\n", ctx.rounds);
200    }
201
202    if (!image_process(encode_rs, &ctx)) {
203        FATAL("failed to process input\n");
204    }
205
206    if (!image_ecc_save(&ctx)) {
207        FATAL("failed to write output\n");
208    }
209
210    image_free(&ctx);
211    return 0;
212}
213
214static int decode(image& ctx, const std::vector<std::string>& inp_filenames,
215        const std::string& fec_filename, std::string& out_filename)
216{
217    const std::string& inp_filename = inp_filenames.front();
218
219    if (ctx.inplace && ctx.sparse) {
220        FATAL("invalid parameters: inplace cannot be used with sparse "
221            "files\n");
222    }
223
224    if (!image_ecc_load(fec_filename, &ctx) ||
225            !image_load(inp_filenames, &ctx, !out_filename.empty())) {
226        FATAL("failed to read input\n");
227    }
228
229    if (ctx.inplace) {
230        INFO("correcting '%s' using RS(255, %d) from '%s'\n",
231            inp_filename.c_str(), ctx.rs_n, fec_filename.c_str());
232
233        out_filename = inp_filename;
234    } else {
235        INFO("decoding '%s' to '%s' using RS(255, %d) from '%s'\n",
236            inp_filename.c_str(),
237            out_filename.empty() ? out_filename.c_str() : "<none>", ctx.rs_n,
238            fec_filename.c_str());
239    }
240
241    if (ctx.verbose) {
242        INFO("\traw fec size: %u\n", ctx.fec_size);
243        INFO("\tblocks: %" PRIu64 "\n", ctx.blocks);
244        INFO("\trounds: %" PRIu64 "\n", ctx.rounds);
245    }
246
247    if (!image_process(decode_rs, &ctx)) {
248        FATAL("failed to process input\n");
249    }
250
251    if (ctx.rv) {
252        INFO("corrected %" PRIu64 " errors\n", ctx.rv);
253    } else {
254        INFO("no errors found\n");
255    }
256
257    if (!out_filename.empty() && !image_save(out_filename, &ctx)) {
258        FATAL("failed to write output\n");
259    }
260
261    image_free(&ctx);
262    return 0;
263}
264
265int main(int argc, char **argv)
266{
267    std::string fec_filename;
268    std::string out_filename;
269    std::vector<std::string> inp_filenames;
270    int mode = MODE_ENCODE;
271    image ctx;
272
273    image_init(&ctx);
274    ctx.roots = FEC_DEFAULT_ROOTS;
275
276    while (1) {
277        const static struct option long_options[] = {
278            {"help", no_argument, 0, 'h'},
279            {"encode", no_argument, 0, 'e'},
280            {"decode", no_argument, 0, 'd'},
281            {"sparse", no_argument, 0, 'S'},
282            {"roots", required_argument, 0, 'r'},
283            {"inplace", no_argument, 0, 'i'},
284            {"mmap", no_argument, 0, 'm'},
285            {"threads", required_argument, 0, 'j'},
286            {"print-fec-size", required_argument, 0, 's'},
287            {"get-ecc-start", required_argument, 0, 'E'},
288            {"get-verity-start", required_argument, 0, 'V'},
289            {"verbose", no_argument, 0, 'v'},
290            {NULL, 0, 0, 0}
291        };
292        int c = getopt_long(argc, argv, "hedSr:imj:s:E:V:v", long_options, NULL);
293        if (c < 0) {
294            break;
295        }
296
297        switch (c) {
298        case 'h':
299            return usage();
300        case 'S':
301            ctx.sparse = true;
302            break;
303        case 'e':
304            if (mode != MODE_ENCODE) {
305                return usage();
306            }
307            break;
308        case 'd':
309            if (mode != MODE_ENCODE) {
310                return usage();
311            }
312            mode = MODE_DECODE;
313            break;
314        case 'r':
315            ctx.roots = (int)parse_arg(optarg, "roots", FEC_RSM);
316            break;
317        case 'i':
318            ctx.inplace = true;
319            break;
320        case 'm':
321            ctx.mmap = true;
322            break;
323        case 'j':
324            ctx.threads = (int)parse_arg(optarg, "threads", IMAGE_MAX_THREADS);
325            break;
326        case 's':
327            if (mode != MODE_ENCODE) {
328                return usage();
329            }
330            mode = MODE_PRINTSIZE;
331            ctx.inp_size = parse_arg(optarg, "print-fec-size", UINT64_MAX);
332            break;
333        case 'E':
334            if (mode != MODE_ENCODE) {
335                return usage();
336            }
337            mode = MODE_GETECCSTART;
338            inp_filenames.push_back(optarg);
339            break;
340        case 'V':
341            if (mode != MODE_ENCODE) {
342                return usage();
343            }
344            mode = MODE_GETVERITYSTART;
345            inp_filenames.push_back(optarg);
346            break;
347        case 'v':
348            ctx.verbose = true;
349            break;
350        case '?':
351            return usage();
352        default:
353            abort();
354        }
355    }
356
357    argc -= optind;
358    argv += optind;
359
360    assert(ctx.roots > 0 && ctx.roots < FEC_RSM);
361
362    /* check for input / output parameters */
363    if (mode == MODE_ENCODE) {
364        /* allow multiple input files */
365        for (int i = 0; i < (argc - 1); ++i) {
366            inp_filenames.push_back(argv[i]);
367        }
368
369        if (inp_filenames.empty()) {
370            return usage();
371        }
372
373        /* the last one is the output file */
374        fec_filename = argv[argc - 1];
375    } else if (mode == MODE_DECODE) {
376        if (argc < 2 || argc > 3) {
377            return usage();
378        } else if (argc == 3) {
379            if (ctx.inplace) {
380                return usage();
381            }
382            out_filename = argv[2];
383        }
384
385        inp_filenames.push_back(argv[0]);
386        fec_filename = argv[1];
387    }
388
389    switch (mode) {
390    case MODE_PRINTSIZE:
391        return print_size(ctx);
392    case MODE_GETECCSTART:
393    case MODE_GETVERITYSTART:
394        return get_start(mode, inp_filenames.front());
395    case MODE_ENCODE:
396        return encode(ctx, inp_filenames, fec_filename);
397    case MODE_DECODE:
398        return decode(ctx, inp_filenames, fec_filename, out_filename);
399    default:
400        abort();
401    }
402
403    return 1;
404}
405