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.
18 #define _LARGEFILE64_SOURCE
25 #include <base/file.h>
30 #include <openssl/sha.h>
35 #include <sys/ioctl.h>
37 #ifndef IMAGE_NO_SPARSE
38 #include <sparse/sparse.h>
42 void image_init(image *ctx)
44 memset(ctx, 0, sizeof(*ctx));
47 static void mmap_image_free(image *ctx)
50 munmap(ctx->input, (size_t)ctx->inp_size);
51 TEMP_FAILURE_RETRY(close(ctx->inp_fd));
54 if (ctx->fec_mmap_addr) {
55 munmap(ctx->fec_mmap_addr, FEC_BLOCKSIZE + ctx->fec_size);
56 TEMP_FAILURE_RETRY(close(ctx->fec_fd));
59 if (!ctx->inplace && ctx->output) {
64 static void file_image_free(image *ctx)
66 assert(ctx->input == ctx->output);
77 void image_free(image *ctx)
88 static uint64_t get_size(int fd)
92 if (fstat(fd, &st) == -1) {
93 FATAL("failed to fstat: %s\n", strerror(errno));
98 if (S_ISBLK(st.st_mode)) {
99 if (ioctl(fd, BLKGETSIZE64, &size) == -1) {
100 FATAL("failed to ioctl(BLKGETSIZE64): %s\n", strerror(errno));
102 } else if (S_ISREG(st.st_mode)) {
105 FATAL("unknown file mode: %d\n", (int)st.st_mode);
111 static void calculate_rounds(uint64_t size, image *ctx)
114 FATAL("empty file?\n");
115 } else if (size % FEC_BLOCKSIZE) {
116 FATAL("file size %" PRIu64 " is not a multiple of %u bytes\n",
117 size, FEC_BLOCKSIZE);
120 ctx->inp_size = size;
121 ctx->blocks = fec_div_round_up(ctx->inp_size, FEC_BLOCKSIZE);
122 ctx->rounds = fec_div_round_up(ctx->blocks, ctx->rs_n);
125 static void mmap_image_load(int fd, image *ctx, bool output_needed)
127 calculate_rounds(get_size(fd), ctx);
129 /* check that we can memory map the file; on 32-bit platforms we are
130 limited to encoding at most 4 GiB files */
131 if (ctx->inp_size > SIZE_MAX) {
132 FATAL("cannot mmap %" PRIu64 " bytes\n", ctx->inp_size);
136 INFO("memory mapping '%s' (size %" PRIu64 ")\n", ctx->fec_filename,
140 int flags = PROT_READ;
146 void *p = mmap(NULL, (size_t)ctx->inp_size, flags, MAP_SHARED, fd, 0);
148 if (p == MAP_FAILED) {
149 FATAL("failed to mmap '%s' (size %" PRIu64 "): %s\n",
150 ctx->fec_filename, ctx->inp_size, strerror(errno));
154 ctx->input = (uint8_t *)p;
157 ctx->output = ctx->input;
158 } else if (output_needed) {
160 INFO("allocating %" PRIu64 " bytes of memory\n", ctx->inp_size);
163 ctx->output = new uint8_t[ctx->inp_size];
166 FATAL("failed to allocate memory\n");
169 memcpy(ctx->output, ctx->input, ctx->inp_size);
172 /* fd is closed in mmap_image_free */
175 #ifndef IMAGE_NO_SPARSE
176 static int process_chunk(void *priv, const void *data, int len)
178 image *ctx = (image *)priv;
179 assert(len % FEC_BLOCKSIZE == 0);
182 memcpy(&ctx->input[ctx->pos], data, len);
190 static void file_image_load(int fd, image *ctx)
194 #ifdef IMAGE_NO_SPARSE
196 FATAL("sparse files not supported\n");
201 struct sparse_file *file;
204 file = sparse_file_import(fd, false, false);
206 file = sparse_file_import_auto(fd, false, ctx->verbose);
210 FATAL("failed to read file %s\n", ctx->fec_filename);
213 len = sparse_file_len(file, false, false);
214 #endif /* IMAGE_NO_SPARSE */
216 calculate_rounds(len, ctx);
219 INFO("allocating %" PRIu64 " bytes of memory\n", ctx->inp_size);
222 ctx->input = new uint8_t[ctx->inp_size];
225 FATAL("failed to allocate memory\n");
228 memset(ctx->input, 0, ctx->inp_size);
229 ctx->output = ctx->input;
231 #ifdef IMAGE_NO_SPARSE
232 if (!android::base::ReadFully(fd, ctx->input, ctx->inp_size)) {
233 FATAL("failed to read: %s\n", strerror(errno));
237 sparse_file_callback(file, false, false, process_chunk, ctx);
238 sparse_file_destroy(file);
241 TEMP_FAILURE_RETRY(close(fd));
244 bool image_load(const char *filename, image *ctx, bool output_needed)
246 assert(ctx->roots > 0 && ctx->roots < FEC_RSM);
247 ctx->rs_n = FEC_RSM - ctx->roots;
249 int flags = O_RDONLY;
255 int fd = TEMP_FAILURE_RETRY(open(filename, flags | O_LARGEFILE));
258 FATAL("failed to open file '%s': %s\n", filename, strerror(errno));
262 mmap_image_load(fd, ctx, output_needed);
264 file_image_load(fd, ctx);
270 bool image_save(const char *filename, image *ctx)
272 if (ctx->inplace && ctx->mmap) {
273 return true; /* nothing to do */
276 /* TODO: support saving as a sparse file */
277 int fd = TEMP_FAILURE_RETRY(open(filename, O_WRONLY | O_CREAT | O_TRUNC,
281 FATAL("failed to open file '%s: %s'\n", filename, strerror(errno));
284 if (!android::base::WriteFully(fd, ctx->output, ctx->inp_size)) {
285 FATAL("failed to write to output: %s\n", strerror(errno));
288 TEMP_FAILURE_RETRY(close(fd));
292 static void mmap_image_ecc_new(image *ctx)
295 INFO("mmaping '%s' (size %u)\n", ctx->fec_filename, ctx->fec_size);
298 int fd = TEMP_FAILURE_RETRY(open(ctx->fec_filename,
299 O_RDWR | O_CREAT, 0666));
302 FATAL("failed to open file '%s': %s\n", ctx->fec_filename,
306 assert(sizeof(fec_header) <= FEC_BLOCKSIZE);
307 size_t fec_size = FEC_BLOCKSIZE + ctx->fec_size;
309 if (ftruncate(fd, fec_size) == -1) {
310 FATAL("failed to ftruncate file '%s': %s\n", ctx->fec_filename,
315 INFO("memory mapping '%s' (size %zu)\n", ctx->fec_filename, fec_size);
318 void *p = mmap(NULL, fec_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
320 if (p == MAP_FAILED) {
321 FATAL("failed to mmap '%s' (size %zu): %s\n", ctx->fec_filename,
322 fec_size, strerror(errno));
326 ctx->fec_mmap_addr = (uint8_t *)p;
327 ctx->fec = ctx->fec_mmap_addr;
330 static void file_image_ecc_new(image *ctx)
333 INFO("allocating %u bytes of memory\n", ctx->fec_size);
336 ctx->fec = new uint8_t[ctx->fec_size];
339 FATAL("failed to allocate %u bytes\n", ctx->fec_size);
343 bool image_ecc_new(const char *filename, image *ctx)
345 assert(ctx->rounds > 0); /* image_load should be called first */
347 ctx->fec_filename = filename;
348 ctx->fec_size = ctx->rounds * ctx->roots * FEC_BLOCKSIZE;
351 mmap_image_ecc_new(ctx);
353 file_image_ecc_new(ctx);
359 bool image_ecc_load(const char *filename, image *ctx)
361 int fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
364 FATAL("failed to open file '%s': %s\n", filename, strerror(errno));
367 if (lseek64(fd, -FEC_BLOCKSIZE, SEEK_END) < 0) {
368 FATAL("failed to seek to header in '%s': %s\n", filename,
372 assert(sizeof(fec_header) <= FEC_BLOCKSIZE);
374 uint8_t header[FEC_BLOCKSIZE];
375 fec_header *p = (fec_header *)header;
377 if (!android::base::ReadFully(fd, header, sizeof(header))) {
378 FATAL("failed to read %zd bytes from '%s': %s\n", sizeof(header),
379 filename, strerror(errno));
382 if (p->magic != FEC_MAGIC) {
383 FATAL("invalid magic in '%s': %08x\n", filename, p->magic);
386 if (p->version != FEC_VERSION) {
387 FATAL("unsupported version in '%s': %u\n", filename, p->version);
390 if (p->size != sizeof(fec_header)) {
391 FATAL("unexpected header size in '%s': %u\n", filename, p->size);
394 if (p->roots == 0 || p->roots >= FEC_RSM) {
395 FATAL("invalid roots in '%s': %u\n", filename, p->roots);
398 if (p->fec_size % p->roots || p->fec_size % FEC_BLOCKSIZE) {
399 FATAL("invalid length in '%s': %u\n", filename, p->fec_size);
402 ctx->roots = (int)p->roots;
403 ctx->rs_n = FEC_RSM - ctx->roots;
405 calculate_rounds(p->inp_size, ctx);
407 if (!image_ecc_new(filename, ctx)) {
408 FATAL("failed to allocate ecc\n");
411 if (p->fec_size != ctx->fec_size) {
412 FATAL("inconsistent header in '%s'\n", filename);
415 if (lseek64(fd, 0, SEEK_SET) < 0) {
416 FATAL("failed to rewind '%s': %s", filename, strerror(errno));
419 if (!ctx->mmap && !android::base::ReadFully(fd, ctx->fec, ctx->fec_size)) {
420 FATAL("failed to read %u bytes from '%s': %s\n", ctx->fec_size,
421 filename, strerror(errno));
424 TEMP_FAILURE_RETRY(close(fd));
426 uint8_t hash[SHA256_DIGEST_LENGTH];
427 SHA256(ctx->fec, ctx->fec_size, hash);
429 if (memcmp(hash, p->hash, SHA256_DIGEST_LENGTH) != 0) {
430 FATAL("invalid ecc data\n");
436 bool image_ecc_save(image *ctx)
438 assert(sizeof(fec_header) <= FEC_BLOCKSIZE);
440 uint8_t header[FEC_BLOCKSIZE];
444 p = (uint8_t *)&ctx->fec_mmap_addr[ctx->fec_size];
447 memset(p, 0, FEC_BLOCKSIZE);
449 fec_header *f = (fec_header *)p;
451 f->magic = FEC_MAGIC;
452 f->version = FEC_VERSION;
453 f->size = sizeof(fec_header);
454 f->roots = ctx->roots;
455 f->fec_size = ctx->fec_size;
456 f->inp_size = ctx->inp_size;
458 SHA256(ctx->fec, ctx->fec_size, f->hash);
460 /* store a copy of the fec_header at the end of the header block */
461 memcpy(&p[sizeof(header) - sizeof(fec_header)], p, sizeof(fec_header));
464 assert(ctx->fec_filename);
466 int fd = TEMP_FAILURE_RETRY(open(ctx->fec_filename,
467 O_WRONLY | O_CREAT | O_TRUNC, 0666));
470 FATAL("failed to open file '%s': %s\n", ctx->fec_filename,
474 if (!android::base::WriteFully(fd, ctx->fec, ctx->fec_size) ||
475 !android::base::WriteFully(fd, header, sizeof(header))) {
476 FATAL("failed to write to output: %s\n", strerror(errno));
479 TEMP_FAILURE_RETRY(close(fd));
485 static void * process(void *cookie)
487 image_proc_ctx *ctx = (image_proc_ctx *)cookie;
492 bool image_process(image_proc_func func, image *ctx)
494 int threads = ctx->threads;
496 if (threads < IMAGE_MIN_THREADS) {
497 threads = sysconf(_SC_NPROCESSORS_ONLN);
499 if (threads < IMAGE_MIN_THREADS) {
500 threads = IMAGE_MIN_THREADS;
504 assert(ctx->rounds > 0);
506 if ((uint64_t)threads > ctx->rounds) {
507 threads = (int)ctx->rounds;
509 if (threads > IMAGE_MAX_THREADS) {
510 threads = IMAGE_MAX_THREADS;
514 INFO("starting %d threads to compute RS(255, %d)\n", threads,
518 pthread_t pthreads[threads];
519 image_proc_ctx args[threads];
521 uint64_t current = 0;
522 uint64_t end = ctx->rounds * ctx->rs_n * FEC_BLOCKSIZE;
523 uint64_t rs_blocks_per_thread =
524 fec_div_round_up(ctx->rounds * FEC_BLOCKSIZE, threads);
527 INFO("computing %" PRIu64 " codes per thread\n", rs_blocks_per_thread);
530 for (int i = 0; i < threads; ++i) {
535 args[i].fec_pos = current * ctx->roots;
536 args[i].start = current * ctx->rs_n;
537 args[i].end = (current + rs_blocks_per_thread) * ctx->rs_n;
539 args[i].rs = init_rs_char(FEC_PARAMS(ctx->roots));
542 FATAL("failed to initialize encoder for thread %d\n", i);
545 if (args[i].end > end) {
547 } else if (i == threads && args[i].end + rs_blocks_per_thread *
553 INFO("thread %d: [%" PRIu64 ", %" PRIu64 ")\n",
554 i, args[i].start, args[i].end);
557 assert(args[i].start < args[i].end);
558 assert((args[i].end - args[i].start) % ctx->rs_n == 0);
560 if (pthread_create(&pthreads[i], NULL, process, &args[i]) != 0) {
561 FATAL("failed to create thread %d\n", i);
564 current += rs_blocks_per_thread;
569 for (int i = 0; i < threads; ++i) {
570 if (pthread_join(pthreads[i], NULL) != 0) {
571 FATAL("failed to join thread %d: %s\n", i, strerror(errno));
574 ctx->rv += args[i].rv;
577 free_rs_char(args[i].rs);