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