NFS: Kill fscache warnings when mounting without -ofsc
[linux-2.6/libata-dev.git] / lib / earlycpio.c
blob8078ef49cb793fe475c05cf033944fff5e05a8bb
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2012 Intel Corporation; author H. Peter Anvin
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.
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.
14 * ----------------------------------------------------------------------- */
17 * earlycpio.c
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.
28 #include <linux/earlycpio.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
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
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 * @offset: When a matching file is found, this is the offset to the
56 * beginning of the cpio. It can be used to iterate through
57 * the cpio to find all files inside of a directory path
59 * @return: struct cpio_data containing the address, length and
60 * filename (with the directory path cut off) of the found file.
61 * If you search for a filename and not for files in a directory,
62 * pass the absolute path of the filename in the cpio and make sure
63 * the match returned an empty filename string.
66 struct cpio_data __cpuinit find_cpio_data(const char *path, void *data,
67 size_t len, long *offset)
69 const size_t cpio_header_len = 8*C_NFIELDS - 2;
70 struct cpio_data cd = { NULL, 0, "" };
71 const char *p, *dptr, *nptr;
72 unsigned int ch[C_NFIELDS], *chp, v;
73 unsigned char c, x;
74 size_t mypathsize = strlen(path);
75 int i, j;
77 p = data;
79 while (len > cpio_header_len) {
80 if (!*p) {
81 /* All cpio headers need to be 4-byte aligned */
82 p += 4;
83 len -= 4;
84 continue;
87 j = 6; /* The magic field is only 6 characters */
88 chp = ch;
89 for (i = C_NFIELDS; i; i--) {
90 v = 0;
91 while (j--) {
92 v <<= 4;
93 c = *p++;
95 x = c - '0';
96 if (x < 10) {
97 v += x;
98 continue;
101 x = (c | 0x20) - 'a';
102 if (x < 6) {
103 v += x + 10;
104 continue;
107 goto quit; /* Invalid hexadecimal */
109 *chp++ = v;
110 j = 8; /* All other fields are 8 characters */
113 if ((ch[C_MAGIC] - 0x070701) > 1)
114 goto quit; /* Invalid magic */
116 len -= cpio_header_len;
118 dptr = PTR_ALIGN(p + ch[C_NAMESIZE], 4);
119 nptr = PTR_ALIGN(dptr + ch[C_FILESIZE], 4);
121 if (nptr > p + len || dptr < p || nptr < dptr)
122 goto quit; /* Buffer overrun */
124 if ((ch[C_MODE] & 0170000) == 0100000 &&
125 ch[C_NAMESIZE] >= mypathsize &&
126 !memcmp(p, path, mypathsize)) {
127 *offset = (long)nptr - (long)data;
128 if (ch[C_NAMESIZE] - mypathsize >= MAX_CPIO_FILE_NAME) {
129 pr_warn(
130 "File %s exceeding MAX_CPIO_FILE_NAME [%d]\n",
131 p, MAX_CPIO_FILE_NAME);
133 strlcpy(cd.name, p + mypathsize, MAX_CPIO_FILE_NAME);
135 cd.data = (void *)dptr;
136 cd.size = ch[C_FILESIZE];
137 return cd; /* Found it! */
139 len -= (nptr - p);
140 p = nptr;
143 quit:
144 return cd;