]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/earlycpio.c
Merge tag 'linux-kselftest-4.8-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kerne...
[mirror_ubuntu-bionic-kernel.git] / lib / earlycpio.c
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2012 Intel Corporation; author H. Peter Anvin
4 *
5 * This file is part of the Linux kernel, and is made available
6 * under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * ----------------------------------------------------------------------- */
15
16 /*
17 * earlycpio.c
18 *
19 * Find a specific cpio member; must precede any compressed content.
20 * This is used to locate data items in the initramfs used by the
21 * kernel itself during early boot (before the main initramfs is
22 * decompressed.) It is the responsibility of the initramfs creator
23 * to ensure that these items are uncompressed at the head of the
24 * blob. Depending on the boot loader or package tool that may be a
25 * separate file or part of the same file.
26 */
27
28 #include <linux/earlycpio.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31
32 enum cpio_fields {
33 C_MAGIC,
34 C_INO,
35 C_MODE,
36 C_UID,
37 C_GID,
38 C_NLINK,
39 C_MTIME,
40 C_FILESIZE,
41 C_MAJ,
42 C_MIN,
43 C_RMAJ,
44 C_RMIN,
45 C_NAMESIZE,
46 C_CHKSUM,
47 C_NFIELDS
48 };
49
50 /**
51 * cpio_data find_cpio_data - Search for files in an uncompressed cpio
52 * @path: The directory to search for, including a slash at the end
53 * @data: Pointer to the the cpio archive or a header inside
54 * @len: Remaining length of the cpio based on data pointer
55 * @nextoff: When a matching file is found, this is the offset from the
56 * beginning of the cpio to the beginning of the next file, not the
57 * matching file itself. It can be used to iterate through the cpio
58 * to find all files inside of a directory path.
59 *
60 * @return: struct cpio_data containing the address, length and
61 * filename (with the directory path cut off) of the found file.
62 * If you search for a filename and not for files in a directory,
63 * pass the absolute path of the filename in the cpio and make sure
64 * the match returned an empty filename string.
65 */
66
67 struct cpio_data find_cpio_data(const char *path, void *data,
68 size_t len, long *nextoff)
69 {
70 const size_t cpio_header_len = 8*C_NFIELDS - 2;
71 struct cpio_data cd = { NULL, 0, "" };
72 const char *p, *dptr, *nptr;
73 unsigned int ch[C_NFIELDS], *chp, v;
74 unsigned char c, x;
75 size_t mypathsize = strlen(path);
76 int i, j;
77
78 p = data;
79
80 while (len > cpio_header_len) {
81 if (!*p) {
82 /* All cpio headers need to be 4-byte aligned */
83 p += 4;
84 len -= 4;
85 continue;
86 }
87
88 j = 6; /* The magic field is only 6 characters */
89 chp = ch;
90 for (i = C_NFIELDS; i; i--) {
91 v = 0;
92 while (j--) {
93 v <<= 4;
94 c = *p++;
95
96 x = c - '0';
97 if (x < 10) {
98 v += x;
99 continue;
100 }
101
102 x = (c | 0x20) - 'a';
103 if (x < 6) {
104 v += x + 10;
105 continue;
106 }
107
108 goto quit; /* Invalid hexadecimal */
109 }
110 *chp++ = v;
111 j = 8; /* All other fields are 8 characters */
112 }
113
114 if ((ch[C_MAGIC] - 0x070701) > 1)
115 goto quit; /* Invalid magic */
116
117 len -= cpio_header_len;
118
119 dptr = PTR_ALIGN(p + ch[C_NAMESIZE], 4);
120 nptr = PTR_ALIGN(dptr + ch[C_FILESIZE], 4);
121
122 if (nptr > p + len || dptr < p || nptr < dptr)
123 goto quit; /* Buffer overrun */
124
125 if ((ch[C_MODE] & 0170000) == 0100000 &&
126 ch[C_NAMESIZE] >= mypathsize &&
127 !memcmp(p, path, mypathsize)) {
128
129 if (nextoff)
130 *nextoff = (long)nptr - (long)data;
131
132 if (ch[C_NAMESIZE] - mypathsize >= MAX_CPIO_FILE_NAME) {
133 pr_warn(
134 "File %s exceeding MAX_CPIO_FILE_NAME [%d]\n",
135 p, MAX_CPIO_FILE_NAME);
136 }
137 strlcpy(cd.name, p + mypathsize, MAX_CPIO_FILE_NAME);
138
139 cd.data = (void *)dptr;
140 cd.size = ch[C_FILESIZE];
141 return cd; /* Found it! */
142 }
143 len -= (nptr - p);
144 p = nptr;
145 }
146
147 quit:
148 return cd;
149 }