source: mainline/boot/generic/src/tar.c

Last change on this file was 63a045c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Unify handling of compressed init data and use regular tar + gzip to achieve it

There are two issues this commit solves.

First is that architecture-specific code duplicates most of the init binary
handling in each architecture, each with miniscule and confusing variations.
After this commit, the init binary expansion is almost entirely handled by
unified generic code.

Second is that the way we used to generate the incorporated data is somewhat
convoluted. Previously we have a Python script which generates a zip archive
with individual deflate-compressed files and accompanying header and C files
which contain structures describing the archive contents.
The zip file is then extracted and the individual deflate-compressed files are
included in the binary via assembler code.
Since gas doesn't take particular care to be consistent between architectures,
the assembly portions are also not uniform and the build script needs to know
particulars of the architecture's assembly.

Instead of doing that, after this commit we first gzip each included file, then
we pack the gzipped files into a tar archive, and then we include the archive
into the binary using objcopy.
Linker script provides symbols for the start and end of the archive,
and the payload is in a self-describing format, so there is no need for any
generated code.

Note that we are doing the opposite of the conventional .tar.gz format.
It would be somewhat inconvenient to use .tar.gz since the uncompressed files
need to be aligned to page size, so we'd have to first decompress the entire
payload to determine the final position of the files (and hence the required
amount of memory).

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 * Copyright (c) 2018 Jiří Zárevúcky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <align.h>
30#include <tar.h>
31
32static int _digit_val(char c)
33{
34 if (c >= '0' && c <= '9') {
35 return c - '0';
36 }
37 if (c >= 'a' && c <= 'z') {
38 return c - 'a' + 10;
39 }
40 if (c >= 'A' && c <= 'Z') {
41 return c - 'A' + 10;
42 }
43
44 // TODO: error message
45 return 0;
46}
47
48static int64_t _parse_size(const char *s, size_t len, int base)
49{
50 int64_t val = 0;
51
52 for (size_t i = 0; i < len; i++) {
53 if (*s == 0)
54 return val;
55
56 if (*s == ' ') {
57 s++;
58 continue;
59 }
60
61 if ((INT64_MAX - 64) / base <= val) {
62 // TODO: error message
63 return INT64_MAX;
64 }
65
66 val *= base;
67 val += _digit_val(*s);
68 s++;
69 }
70
71 return val;
72}
73
74bool tar_info(const uint8_t *start, const uint8_t *end,
75 const char **name, size_t *length)
76{
77 if (end - start < TAR_BLOCK_SIZE) {
78 // TODO: error message
79 return false;
80 }
81
82 const tar_header_raw_t *h = (const tar_header_raw_t *) start;
83 if (h->filename[0] == '\0')
84 return false;
85
86 ptrdiff_t len = _parse_size(h->size, sizeof(h->size), 8);
87
88 if (end - start < TAR_BLOCK_SIZE + len) {
89 // TODO: error message
90 return false;
91 }
92
93 *name = h->filename;
94 *length = len;
95 return true;
96}
Note: See TracBrowser for help on using the repository browser.