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 <xen/init.h>
29 #include <xen/lib.h>
30 #include <xen/string.h>
31 #include <xen/earlycpio.h>
32
33 #define ALIGN(x, a) ((x + (a) - 1) & ~((a) - 1))
34 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
35
36 enum cpio_fields {
37 C_MAGIC,
38 C_INO,
39 C_MODE,
40 C_UID,
41 C_GID,
42 C_NLINK,
43 C_MTIME,
44 C_FILESIZE,
45 C_MAJ,
46 C_MIN,
47 C_RMAJ,
48 C_RMIN,
49 C_NAMESIZE,
50 C_CHKSUM,
51 C_NFIELDS
52 };
53
54 /**
55 * cpio_data find_cpio_data - Search for files in an uncompressed cpio
56 * @path: The directory to search for, including a slash at the end
57 * @data: Pointer to the the cpio archive or a header inside
58 * @len: Remaining length of the cpio based on data pointer
59 * @nextoff: When a matching file is found, this is the offset from the
60 * beginning of the cpio to the beginning of the next file, not the
61 * matching file itself. It can be used to iterate through the cpio
62 * to find all files inside of a directory path.
63 *
64 * @return: struct cpio_data containing the address, length and
65 * filename (with the directory path cut off) of the found file.
66 * If you search for a filename and not for files in a directory,
67 * pass the absolute path of the filename in the cpio and make sure
68 * the match returned an empty filename string.
69 */
70
find_cpio_data(const char * path,void * data,size_t len,long * nextoff)71 struct cpio_data __init find_cpio_data(const char *path, void *data,
72 size_t len, long *nextoff)
73 {
74 const size_t cpio_header_len = 8*C_NFIELDS - 2;
75 struct cpio_data cd = { NULL, 0, "" };
76 const char *p, *dptr, *nptr;
77 unsigned int ch[C_NFIELDS], *chp, v;
78 unsigned char c, x;
79 size_t mypathsize = strlen(path);
80 int i, j;
81
82 p = data;
83
84 while (len > cpio_header_len) {
85 if (!*p) {
86 /* All cpio headers need to be 4-byte aligned */
87 p += 4;
88 len -= 4;
89 continue;
90 }
91
92 j = 6; /* The magic field is only 6 characters */
93 chp = ch;
94 for (i = C_NFIELDS; i; i--) {
95 v = 0;
96 while (j--) {
97 v <<= 4;
98 c = *p++;
99
100 x = c - '0';
101 if (x < 10) {
102 v += x;
103 continue;
104 }
105
106 x = (c | 0x20) - 'a';
107 if (x < 6) {
108 v += x + 10;
109 continue;
110 }
111
112 goto quit; /* Invalid hexadecimal */
113 }
114 *chp++ = v;
115 j = 8; /* All other fields are 8 characters */
116 }
117
118 if ((ch[C_MAGIC] - 0x070701) > 1)
119 goto quit; /* Invalid magic */
120
121 len -= cpio_header_len;
122
123 dptr = PTR_ALIGN(p + ch[C_NAMESIZE], 4);
124 nptr = PTR_ALIGN(dptr + ch[C_FILESIZE], 4);
125
126 if (nptr > p + len || dptr < p || nptr < dptr)
127 goto quit; /* Buffer overrun */
128
129 if ((ch[C_MODE] & 0170000) == 0100000 &&
130 ch[C_NAMESIZE] >= mypathsize &&
131 !memcmp(p, path, mypathsize)) {
132 *nextoff = (long)nptr - (long)data;
133 if (ch[C_NAMESIZE] - mypathsize >= MAX_CPIO_FILE_NAME) {
134 printk(
135 "File %s exceeding MAX_CPIO_FILE_NAME [%d]\n",
136 p, MAX_CPIO_FILE_NAME);
137 }
138 strlcpy(cd.name, p + mypathsize, MAX_CPIO_FILE_NAME);
139
140 cd.data = (void *)dptr;
141 cd.size = ch[C_FILESIZE];
142 return cd; /* Found it! */
143 }
144 len -= (nptr - p);
145 p = nptr;
146 }
147
148 quit:
149 return cd;
150 }
151
152