Back to home page

LXR

 
 

    


0001 /* ----------------------------------------------------------------------- *
0002  *
0003  *   Copyright 2012 Intel Corporation; author H. Peter Anvin
0004  *
0005  *   This file is part of the Linux kernel, and is made available
0006  *   under the terms of the GNU General Public License version 2, as
0007  *   published by the Free Software Foundation.
0008  *
0009  *   This program is distributed in the hope it will be useful, but
0010  *   WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *   General Public License for more details.
0013  *
0014  * ----------------------------------------------------------------------- */
0015 
0016 /*
0017  * earlycpio.c
0018  *
0019  * Find a specific cpio member; must precede any compressed content.
0020  * This is used to locate data items in the initramfs used by the
0021  * kernel itself during early boot (before the main initramfs is
0022  * decompressed.)  It is the responsibility of the initramfs creator
0023  * to ensure that these items are uncompressed at the head of the
0024  * blob.  Depending on the boot loader or package tool that may be a
0025  * separate file or part of the same file.
0026  */
0027 
0028 #include <linux/earlycpio.h>
0029 #include <linux/kernel.h>
0030 #include <linux/string.h>
0031 
0032 enum cpio_fields {
0033     C_MAGIC,
0034     C_INO,
0035     C_MODE,
0036     C_UID,
0037     C_GID,
0038     C_NLINK,
0039     C_MTIME,
0040     C_FILESIZE,
0041     C_MAJ,
0042     C_MIN,
0043     C_RMAJ,
0044     C_RMIN,
0045     C_NAMESIZE,
0046     C_CHKSUM,
0047     C_NFIELDS
0048 };
0049 
0050 /**
0051  * cpio_data find_cpio_data - Search for files in an uncompressed cpio
0052  * @path:       The directory to search for, including a slash at the end
0053  * @data:       Pointer to the the cpio archive or a header inside
0054  * @len:        Remaining length of the cpio based on data pointer
0055  * @nextoff:    When a matching file is found, this is the offset from the
0056  *              beginning of the cpio to the beginning of the next file, not the
0057  *              matching file itself. It can be used to iterate through the cpio
0058  *              to find all files inside of a directory path.
0059  *
0060  * @return:     struct cpio_data containing the address, length and
0061  *              filename (with the directory path cut off) of the found file.
0062  *              If you search for a filename and not for files in a directory,
0063  *              pass the absolute path of the filename in the cpio and make sure
0064  *              the match returned an empty filename string.
0065  */
0066 
0067 struct cpio_data find_cpio_data(const char *path, void *data,
0068                 size_t len,  long *nextoff)
0069 {
0070     const size_t cpio_header_len = 8*C_NFIELDS - 2;
0071     struct cpio_data cd = { NULL, 0, "" };
0072     const char *p, *dptr, *nptr;
0073     unsigned int ch[C_NFIELDS], *chp, v;
0074     unsigned char c, x;
0075     size_t mypathsize = strlen(path);
0076     int i, j;
0077 
0078     p = data;
0079 
0080     while (len > cpio_header_len) {
0081         if (!*p) {
0082             /* All cpio headers need to be 4-byte aligned */
0083             p += 4;
0084             len -= 4;
0085             continue;
0086         }
0087 
0088         j = 6;      /* The magic field is only 6 characters */
0089         chp = ch;
0090         for (i = C_NFIELDS; i; i--) {
0091             v = 0;
0092             while (j--) {
0093                 v <<= 4;
0094                 c = *p++;
0095 
0096                 x = c - '0';
0097                 if (x < 10) {
0098                     v += x;
0099                     continue;
0100                 }
0101 
0102                 x = (c | 0x20) - 'a';
0103                 if (x < 6) {
0104                     v += x + 10;
0105                     continue;
0106                 }
0107 
0108                 goto quit; /* Invalid hexadecimal */
0109             }
0110             *chp++ = v;
0111             j = 8;  /* All other fields are 8 characters */
0112         }
0113 
0114         if ((ch[C_MAGIC] - 0x070701) > 1)
0115             goto quit; /* Invalid magic */
0116 
0117         len -= cpio_header_len;
0118 
0119         dptr = PTR_ALIGN(p + ch[C_NAMESIZE], 4);
0120         nptr = PTR_ALIGN(dptr + ch[C_FILESIZE], 4);
0121 
0122         if (nptr > p + len || dptr < p || nptr < dptr)
0123             goto quit; /* Buffer overrun */
0124 
0125         if ((ch[C_MODE] & 0170000) == 0100000 &&
0126             ch[C_NAMESIZE] >= mypathsize &&
0127             !memcmp(p, path, mypathsize)) {
0128 
0129             if (nextoff)
0130                 *nextoff = (long)nptr - (long)data;
0131 
0132             if (ch[C_NAMESIZE] - mypathsize >= MAX_CPIO_FILE_NAME) {
0133                 pr_warn(
0134                 "File %s exceeding MAX_CPIO_FILE_NAME [%d]\n",
0135                 p, MAX_CPIO_FILE_NAME);
0136             }
0137             strlcpy(cd.name, p + mypathsize, MAX_CPIO_FILE_NAME);
0138 
0139             cd.data = (void *)dptr;
0140             cd.size = ch[C_FILESIZE];
0141             return cd; /* Found it! */
0142         }
0143         len -= (nptr - p);
0144         p = nptr;
0145     }
0146 
0147 quit:
0148     return cd;
0149 }