1 /*
2 * Copyright (C) 2010 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 #define _FILE_OFFSET_BITS 64
18 #define _LARGEFILE64_SOURCE 1
19
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <zlib.h>
31
32 #include "defs.h"
33 #include "output_file.h"
34 #include "sparse_crc32.h"
35 #include "sparse_format.h"
36
37 #ifndef _WIN32
38 #include <sys/mman.h>
39 #define O_BINARY 0
40 #else
41 #define ftruncate64 ftruncate
42 #endif
43
44 #if defined(__APPLE__) && defined(__MACH__)
45 #define lseek64 lseek
46 #define ftruncate64 ftruncate
47 #define mmap64 mmap
48 #define off64_t off_t
49 #endif
50
51 #define min(a, b) \
52 ({ \
53 typeof(a) _a = (a); \
54 typeof(b) _b = (b); \
55 (_a < _b) ? _a : _b; \
56 })
57
58 #define SPARSE_HEADER_MAJOR_VER 1
59 #define SPARSE_HEADER_MINOR_VER 0
60 #define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
61 #define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
62
63 #define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
64
65 struct output_file_ops {
66 int (*open)(struct output_file*, int fd);
67 int (*skip)(struct output_file*, int64_t);
68 int (*pad)(struct output_file*, int64_t);
69 int (*write)(struct output_file*, void*, size_t);
70 void (*close)(struct output_file*);
71 };
72
73 struct sparse_file_ops {
74 int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
75 int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
76 int (*write_skip_chunk)(struct output_file* out, int64_t len);
77 int (*write_end_chunk)(struct output_file* out);
78 };
79
80 struct output_file {
81 int64_t cur_out_ptr;
82 unsigned int chunk_cnt;
83 uint32_t crc32;
84 struct output_file_ops* ops;
85 struct sparse_file_ops* sparse_ops;
86 int use_crc;
87 unsigned int block_size;
88 int64_t len;
89 char* zero_buf;
90 uint32_t* fill_buf;
91 char* buf;
92 };
93
94 struct output_file_gz {
95 struct output_file out;
96 gzFile gz_fd;
97 };
98
99 #define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
100
101 struct output_file_normal {
102 struct output_file out;
103 int fd;
104 };
105
106 #define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
107
108 struct output_file_callback {
109 struct output_file out;
110 void* priv;
111 int (*write)(void* priv, const void* buf, size_t len);
112 };
113
114 #define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
115
file_open(struct output_file * out,int fd)116 static int file_open(struct output_file* out, int fd) {
117 struct output_file_normal* outn = to_output_file_normal(out);
118
119 outn->fd = fd;
120 return 0;
121 }
122
file_skip(struct output_file * out,int64_t cnt)123 static int file_skip(struct output_file* out, int64_t cnt) {
124 off64_t ret;
125 struct output_file_normal* outn = to_output_file_normal(out);
126
127 ret = lseek64(outn->fd, cnt, SEEK_CUR);
128 if (ret < 0) {
129 error_errno("lseek64");
130 return -1;
131 }
132 return 0;
133 }
134
file_pad(struct output_file * out,int64_t len)135 static int file_pad(struct output_file* out, int64_t len) {
136 int ret;
137 struct output_file_normal* outn = to_output_file_normal(out);
138
139 ret = ftruncate64(outn->fd, len);
140 if (ret < 0) {
141 return -errno;
142 }
143
144 return 0;
145 }
146
file_write(struct output_file * out,void * data,size_t len)147 static int file_write(struct output_file* out, void* data, size_t len) {
148 ssize_t ret;
149 struct output_file_normal* outn = to_output_file_normal(out);
150
151 while (len > 0) {
152 ret = write(outn->fd, data, len);
153 if (ret < 0) {
154 if (errno == EINTR) {
155 continue;
156 }
157 error_errno("write");
158 return -1;
159 }
160
161 data = (char*)data + ret;
162 len -= ret;
163 }
164
165 return 0;
166 }
167
file_close(struct output_file * out)168 static void file_close(struct output_file* out) {
169 struct output_file_normal* outn = to_output_file_normal(out);
170
171 free(outn);
172 }
173
174 static struct output_file_ops file_ops = {
175 .open = file_open,
176 .skip = file_skip,
177 .pad = file_pad,
178 .write = file_write,
179 .close = file_close,
180 };
181
gz_file_open(struct output_file * out,int fd)182 static int gz_file_open(struct output_file* out, int fd) {
183 struct output_file_gz* outgz = to_output_file_gz(out);
184
185 outgz->gz_fd = gzdopen(fd, "wb9");
186 if (!outgz->gz_fd) {
187 error_errno("gzopen");
188 return -errno;
189 }
190
191 return 0;
192 }
193
gz_file_skip(struct output_file * out,int64_t cnt)194 static int gz_file_skip(struct output_file* out, int64_t cnt) {
195 off64_t ret;
196 struct output_file_gz* outgz = to_output_file_gz(out);
197
198 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
199 if (ret < 0) {
200 error_errno("gzseek");
201 return -1;
202 }
203 return 0;
204 }
205
gz_file_pad(struct output_file * out,int64_t len)206 static int gz_file_pad(struct output_file* out, int64_t len) {
207 off64_t ret;
208 struct output_file_gz* outgz = to_output_file_gz(out);
209
210 ret = gztell(outgz->gz_fd);
211 if (ret < 0) {
212 return -1;
213 }
214
215 if (ret >= len) {
216 return 0;
217 }
218
219 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
220 if (ret < 0) {
221 return -1;
222 }
223
224 gzwrite(outgz->gz_fd, "", 1);
225
226 return 0;
227 }
228
gz_file_write(struct output_file * out,void * data,size_t len)229 static int gz_file_write(struct output_file* out, void* data, size_t len) {
230 int ret;
231 struct output_file_gz* outgz = to_output_file_gz(out);
232
233 while (len > 0) {
234 ret = gzwrite(outgz->gz_fd, data, min(len, (unsigned int)INT_MAX));
235 if (ret == 0) {
236 error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
237 return -1;
238 }
239 len -= ret;
240 data = (char*)data + ret;
241 }
242
243 return 0;
244 }
245
gz_file_close(struct output_file * out)246 static void gz_file_close(struct output_file* out) {
247 struct output_file_gz* outgz = to_output_file_gz(out);
248
249 gzclose(outgz->gz_fd);
250 free(outgz);
251 }
252
253 static struct output_file_ops gz_file_ops = {
254 .open = gz_file_open,
255 .skip = gz_file_skip,
256 .pad = gz_file_pad,
257 .write = gz_file_write,
258 .close = gz_file_close,
259 };
260
callback_file_open(struct output_file * out __unused,int fd __unused)261 static int callback_file_open(struct output_file* out __unused, int fd __unused) {
262 return 0;
263 }
264
callback_file_skip(struct output_file * out,int64_t off)265 static int callback_file_skip(struct output_file* out, int64_t off) {
266 struct output_file_callback* outc = to_output_file_callback(out);
267 int to_write;
268 int ret;
269
270 while (off > 0) {
271 to_write = min(off, (int64_t)INT_MAX);
272 ret = outc->write(outc->priv, nullptr, to_write);
273 if (ret < 0) {
274 return ret;
275 }
276 off -= to_write;
277 }
278
279 return 0;
280 }
281
callback_file_pad(struct output_file * out __unused,int64_t len __unused)282 static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
283 return -1;
284 }
285
callback_file_write(struct output_file * out,void * data,size_t len)286 static int callback_file_write(struct output_file* out, void* data, size_t len) {
287 struct output_file_callback* outc = to_output_file_callback(out);
288
289 return outc->write(outc->priv, data, len);
290 }
291
callback_file_close(struct output_file * out)292 static void callback_file_close(struct output_file* out) {
293 struct output_file_callback* outc = to_output_file_callback(out);
294
295 free(outc);
296 }
297
298 static struct output_file_ops callback_file_ops = {
299 .open = callback_file_open,
300 .skip = callback_file_skip,
301 .pad = callback_file_pad,
302 .write = callback_file_write,
303 .close = callback_file_close,
304 };
305
read_all(int fd,void * buf,size_t len)306 int read_all(int fd, void* buf, size_t len) {
307 size_t total = 0;
308 int ret;
309 char* ptr = reinterpret_cast<char*>(buf);
310
311 while (total < len) {
312 ret = read(fd, ptr, len - total);
313
314 if (ret < 0) return -errno;
315
316 if (ret == 0) return -EINVAL;
317
318 ptr += ret;
319 total += ret;
320 }
321
322 return 0;
323 }
324
write_sparse_skip_chunk(struct output_file * out,int64_t skip_len)325 static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
326 chunk_header_t chunk_header;
327 int ret;
328
329 if (skip_len % out->block_size) {
330 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
331 out->block_size);
332 return -1;
333 }
334
335 /* We are skipping data, so emit a don't care chunk. */
336 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
337 chunk_header.reserved1 = 0;
338 chunk_header.chunk_sz = skip_len / out->block_size;
339 chunk_header.total_sz = CHUNK_HEADER_LEN;
340 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
341 if (ret < 0) return -1;
342
343 out->cur_out_ptr += skip_len;
344 out->chunk_cnt++;
345
346 return 0;
347 }
348
write_sparse_fill_chunk(struct output_file * out,unsigned int len,uint32_t fill_val)349 static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
350 chunk_header_t chunk_header;
351 int rnd_up_len, count;
352 int ret;
353
354 /* Round up the fill length to a multiple of the block size */
355 rnd_up_len = ALIGN(len, out->block_size);
356
357 /* Finally we can safely emit a chunk of data */
358 chunk_header.chunk_type = CHUNK_TYPE_FILL;
359 chunk_header.reserved1 = 0;
360 chunk_header.chunk_sz = rnd_up_len / out->block_size;
361 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
362 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
363
364 if (ret < 0) return -1;
365 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
366 if (ret < 0) return -1;
367
368 if (out->use_crc) {
369 count = out->block_size / sizeof(uint32_t);
370 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
371 }
372
373 out->cur_out_ptr += rnd_up_len;
374 out->chunk_cnt++;
375
376 return 0;
377 }
378
write_sparse_data_chunk(struct output_file * out,unsigned int len,void * data)379 static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data) {
380 chunk_header_t chunk_header;
381 int rnd_up_len, zero_len;
382 int ret;
383
384 /* Round up the data length to a multiple of the block size */
385 rnd_up_len = ALIGN(len, out->block_size);
386 zero_len = rnd_up_len - len;
387
388 /* Finally we can safely emit a chunk of data */
389 chunk_header.chunk_type = CHUNK_TYPE_RAW;
390 chunk_header.reserved1 = 0;
391 chunk_header.chunk_sz = rnd_up_len / out->block_size;
392 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
393 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
394
395 if (ret < 0) return -1;
396 ret = out->ops->write(out, data, len);
397 if (ret < 0) return -1;
398 if (zero_len) {
399 ret = out->ops->write(out, out->zero_buf, zero_len);
400 if (ret < 0) return -1;
401 }
402
403 if (out->use_crc) {
404 out->crc32 = sparse_crc32(out->crc32, data, len);
405 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
406 }
407
408 out->cur_out_ptr += rnd_up_len;
409 out->chunk_cnt++;
410
411 return 0;
412 }
413
write_sparse_end_chunk(struct output_file * out)414 int write_sparse_end_chunk(struct output_file* out) {
415 chunk_header_t chunk_header;
416 int ret;
417
418 if (out->use_crc) {
419 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
420 chunk_header.reserved1 = 0;
421 chunk_header.chunk_sz = 0;
422 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
423
424 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
425 if (ret < 0) {
426 return ret;
427 }
428 out->ops->write(out, &out->crc32, 4);
429 if (ret < 0) {
430 return ret;
431 }
432
433 out->chunk_cnt++;
434 }
435
436 return 0;
437 }
438
439 static struct sparse_file_ops sparse_file_ops = {
440 .write_data_chunk = write_sparse_data_chunk,
441 .write_fill_chunk = write_sparse_fill_chunk,
442 .write_skip_chunk = write_sparse_skip_chunk,
443 .write_end_chunk = write_sparse_end_chunk,
444 };
445
write_normal_data_chunk(struct output_file * out,unsigned int len,void * data)446 static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data) {
447 int ret;
448 unsigned int rnd_up_len = ALIGN(len, out->block_size);
449
450 ret = out->ops->write(out, data, len);
451 if (ret < 0) {
452 return ret;
453 }
454
455 if (rnd_up_len > len) {
456 ret = out->ops->skip(out, rnd_up_len - len);
457 }
458
459 return ret;
460 }
461
write_normal_fill_chunk(struct output_file * out,unsigned int len,uint32_t fill_val)462 static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
463 int ret;
464 unsigned int i;
465 unsigned int write_len;
466
467 /* Initialize fill_buf with the fill_val */
468 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
469 out->fill_buf[i] = fill_val;
470 }
471
472 while (len) {
473 write_len = min(len, out->block_size);
474 ret = out->ops->write(out, out->fill_buf, write_len);
475 if (ret < 0) {
476 return ret;
477 }
478
479 len -= write_len;
480 }
481
482 return 0;
483 }
484
write_normal_skip_chunk(struct output_file * out,int64_t len)485 static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
486 return out->ops->skip(out, len);
487 }
488
write_normal_end_chunk(struct output_file * out)489 int write_normal_end_chunk(struct output_file* out) {
490 return out->ops->pad(out, out->len);
491 }
492
493 static struct sparse_file_ops normal_file_ops = {
494 .write_data_chunk = write_normal_data_chunk,
495 .write_fill_chunk = write_normal_fill_chunk,
496 .write_skip_chunk = write_normal_skip_chunk,
497 .write_end_chunk = write_normal_end_chunk,
498 };
499
output_file_close(struct output_file * out)500 void output_file_close(struct output_file* out) {
501 out->sparse_ops->write_end_chunk(out);
502 out->ops->close(out);
503 }
504
output_file_init(struct output_file * out,int block_size,int64_t len,bool sparse,int chunks,bool crc)505 static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
506 int chunks, bool crc) {
507 int ret;
508
509 out->len = len;
510 out->block_size = block_size;
511 out->cur_out_ptr = 0LL;
512 out->chunk_cnt = 0;
513 out->crc32 = 0;
514 out->use_crc = crc;
515
516 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
517 if (!out->zero_buf) {
518 error_errno("malloc zero_buf");
519 return -ENOMEM;
520 }
521
522 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
523 if (!out->fill_buf) {
524 error_errno("malloc fill_buf");
525 ret = -ENOMEM;
526 goto err_fill_buf;
527 }
528
529 if (sparse) {
530 out->sparse_ops = &sparse_file_ops;
531 } else {
532 out->sparse_ops = &normal_file_ops;
533 }
534
535 if (sparse) {
536 sparse_header_t sparse_header = {
537 .magic = SPARSE_HEADER_MAGIC,
538 .major_version = SPARSE_HEADER_MAJOR_VER,
539 .minor_version = SPARSE_HEADER_MINOR_VER,
540 .file_hdr_sz = SPARSE_HEADER_LEN,
541 .chunk_hdr_sz = CHUNK_HEADER_LEN,
542 .blk_sz = out->block_size,
543 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
544 .total_chunks = static_cast<unsigned>(chunks),
545 .image_checksum = 0};
546
547 if (out->use_crc) {
548 sparse_header.total_chunks++;
549 }
550
551 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
552 if (ret < 0) {
553 goto err_write;
554 }
555 }
556
557 return 0;
558
559 err_write:
560 free(out->fill_buf);
561 err_fill_buf:
562 free(out->zero_buf);
563 return ret;
564 }
565
output_file_new_gz(void)566 static struct output_file* output_file_new_gz(void) {
567 struct output_file_gz* outgz =
568 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
569 if (!outgz) {
570 error_errno("malloc struct outgz");
571 return nullptr;
572 }
573
574 outgz->out.ops = &gz_file_ops;
575
576 return &outgz->out;
577 }
578
output_file_new_normal(void)579 static struct output_file* output_file_new_normal(void) {
580 struct output_file_normal* outn =
581 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
582 if (!outn) {
583 error_errno("malloc struct outn");
584 return nullptr;
585 }
586
587 outn->out.ops = &file_ops;
588
589 return &outn->out;
590 }
591
output_file_open_callback(int (* write)(void *,const void *,size_t),void * priv,unsigned int block_size,int64_t len,int gz __unused,int sparse,int chunks,int crc)592 struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
593 unsigned int block_size, int64_t len, int gz __unused,
594 int sparse, int chunks, int crc) {
595 int ret;
596 struct output_file_callback* outc;
597
598 outc =
599 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
600 if (!outc) {
601 error_errno("malloc struct outc");
602 return nullptr;
603 }
604
605 outc->out.ops = &callback_file_ops;
606 outc->priv = priv;
607 outc->write = write;
608
609 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
610 if (ret < 0) {
611 free(outc);
612 return nullptr;
613 }
614
615 return &outc->out;
616 }
617
output_file_open_fd(int fd,unsigned int block_size,int64_t len,int gz,int sparse,int chunks,int crc)618 struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
619 int sparse, int chunks, int crc) {
620 int ret;
621 struct output_file* out;
622
623 if (gz) {
624 out = output_file_new_gz();
625 } else {
626 out = output_file_new_normal();
627 }
628 if (!out) {
629 return nullptr;
630 }
631
632 out->ops->open(out, fd);
633
634 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
635 if (ret < 0) {
636 free(out);
637 return nullptr;
638 }
639
640 return out;
641 }
642
643 /* Write a contiguous region of data blocks from a memory buffer */
write_data_chunk(struct output_file * out,unsigned int len,void * data)644 int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
645 return out->sparse_ops->write_data_chunk(out, len, data);
646 }
647
648 /* Write a contiguous region of data blocks with a fill value */
write_fill_chunk(struct output_file * out,unsigned int len,uint32_t fill_val)649 int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
650 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
651 }
652
write_fd_chunk(struct output_file * out,unsigned int len,int fd,int64_t offset)653 int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
654 int ret;
655 int64_t aligned_offset;
656 int aligned_diff;
657 uint64_t buffer_size;
658 char* ptr;
659
660 aligned_offset = offset & ~(4096 - 1);
661 aligned_diff = offset - aligned_offset;
662 buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
663
664 #ifndef _WIN32
665 if (buffer_size > SIZE_MAX) return -E2BIG;
666 char* data =
667 reinterpret_cast<char*>(mmap64(nullptr, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
668 if (data == MAP_FAILED) {
669 return -errno;
670 }
671 ptr = data + aligned_diff;
672 #else
673 off64_t pos;
674 char* data = reinterpret_cast<char*>(malloc(len));
675 if (!data) {
676 return -errno;
677 }
678 pos = lseek64(fd, offset, SEEK_SET);
679 if (pos < 0) {
680 free(data);
681 return -errno;
682 }
683 ret = read_all(fd, data, len);
684 if (ret < 0) {
685 free(data);
686 return ret;
687 }
688 ptr = data;
689 #endif
690
691 ret = out->sparse_ops->write_data_chunk(out, len, ptr);
692
693 #ifndef _WIN32
694 munmap(data, buffer_size);
695 #else
696 free(data);
697 #endif
698
699 return ret;
700 }
701
702 /* Write a contiguous region of data blocks from a file */
write_file_chunk(struct output_file * out,unsigned int len,const char * file,int64_t offset)703 int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
704 int ret;
705
706 int file_fd = open(file, O_RDONLY | O_BINARY);
707 if (file_fd < 0) {
708 return -errno;
709 }
710
711 ret = write_fd_chunk(out, len, file_fd, offset);
712
713 close(file_fd);
714
715 return ret;
716 }
717
write_skip_chunk(struct output_file * out,int64_t len)718 int write_skip_chunk(struct output_file* out, int64_t len) {
719 return out->sparse_ops->write_skip_chunk(out, len);
720 }
721