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