public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Borislav Petkov <bp@alien8.de>, Thomas Renninger <trenn@suse.de>,
	eric.piel@tremplin-utc.net, vojcek@tlen.pl, dsdt@gaugusch.at,
	linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org,
	x86@kernel.org, Lin Ming <ming.m.lin@intel.com>,
	lenb@kernel.org, robert.moore@intel.com,
	Al Viro <viro@zeniv.linux.org.uk>
Subject: Re: [PATCH] ACPI: Implement overriding of arbitrary ACPI tables via initrd
Date: Sun, 25 Mar 2012 18:36:56 -0700	[thread overview]
Message-ID: <4F6FC838.7000904@zytor.com> (raw)
In-Reply-To: <20120325085420.GA12565@liondog.tnic>

[-- Attachment #1: Type: text/plain, Size: 907 bytes --]

On 03/25/2012 01:54 AM, Borislav Petkov wrote:
> 
> One other downside for this approach would be the need to have
> initrd/initramfs support always compiled into the kernel in order to do
> all those things but, hey, distro kernels already have that so we're
> probably stuck with it anyway.
> 

In theory we could make the early stuff independent of the initramfs
code in the kernel -- just like we always build in an initramfs.

Not that it matters much, at this point the no-initramfs configurations
are mostly theoretical.

I was able to shave a bit more off the code... optimizing for size can
be fun sometimes.  The code is now 442 bytes on x86-64 and 413 on i386
when compiled with -O2 -fomit-frame-pointer; with -Os
-fomit-frame-pointer it is 372/410... all of that without any library
calls or relocations of any kind (which means it should be safe to call
from the prepaging environment.)

	-hpa

[-- Attachment #2: findcpio.c --]
[-- Type: text/x-csrc, Size: 2505 bytes --]

/*
 * findcpio.c
 *
 * Find a specific cpio member; must precede any compressed content.
 */

#include <stddef.h>
#include <stdbool.h>

struct cpio_data {
	void *data;
	unsigned long size;
};

enum cpio_fields {
	C_MAGIC,
	C_INO,
	C_MODE,
	C_UID,
	C_GID,
	C_NLINK,
	C_MTIME,
	C_FILESIZE,
	C_MAJ,
	C_MIN,
	C_RMAJ,
	C_RMIN,
	C_NAMESIZE,
	C_CHKSUM,
	C_NFIELDS
};

#if defined(__i386__) || defined(__x86_64__)
static size_t strlen(const char *name)
{
	size_t n = -1;

	asm("repne; scasb"
	    : "+D" (name), "+c" (n)
	    : "a" (0));

	return -2 - n;
}

static int memcmp(const void *p1, const void *p2, size_t n)
{
	unsigned char rv;

	asm("repe; cmpsb; setne %0"
	    : "=r" (rv), "+S" (p1), "+D" (p2), "+c" (n));

	return rv;
}
#else
static size_t strlen(const char *name)
{
	size_t n = 0;

	while (*name++)
		n++;

	return n;
}

static int memcmp(const void *p1, const void *p2, size_t n)
{
	const unsigned char *u1 = p1;
	const unsigned char *u2 = p2;
	int d;

	while (n--) {
		d = *u2++ - *u1++;
		if (d)
			return d;
	}
	return 0;
}
#endif

#define ALIGN4(p) ((void *)(((size_t)p + 3) & ~3))

struct cpio_data find_cpio_data(const char *name, const void *data, size_t len)
{
	const size_t cpio_header_len = 8*C_NFIELDS - 2;
	struct cpio_data cd = { NULL, 0 };
	const char *p, *dptr, *nptr;
	unsigned int ch[C_NFIELDS], *chp, v;
	unsigned char c, x;
	size_t mynamesize = strlen(name) + 1;
	int i, j;

	p = data;

	while (len > cpio_header_len) {
		if (!*p) {
			/* All cpio headers need to be 4-byte aligned */
			p += 4;
			len -= 4;
			continue;
		}

		j = 6;		/* The magic field is only 6 characters */
		chp = ch;
		for (i = C_NFIELDS; i; i--) {
			v = 0;
			while (j--) {
				v <<= 4;
				c = *p++;

				x = c - '0';
				if (x < 10) {
					v += x;
					continue;
				}

				x = (c | 0x20) - 'a';
				if (x < 6) {
					v += x + 10;
					continue;
				}

				goto quit; /* Invalid hexadecimal */
			}
			*chp++ = v;
			j = 8;	/* All other fields are 8 characters */
		}

		if ((ch[C_MAGIC] - 0x070701) > 1)
			goto quit; /* Invalid magic */

		len -= cpio_header_len;

		dptr = ALIGN4(p + ch[C_NAMESIZE]);
		nptr = ALIGN4(dptr + ch[C_FILESIZE]);

		if (nptr > p + len || dptr < p || nptr < dptr)
			goto quit; /* Buffer overrun */

		if ((ch[C_MODE] & 0170000) == 0100000 &&
		    ch[C_NAMESIZE] == mynamesize &&
		    !memcmp(p, name, mynamesize)) {
			cd.data = (void *)dptr;
			cd.size = ch[C_FILESIZE];
			return cd; /* Found it! */
		}

		len -= (nptr - p);
		p = nptr;
	}

quit:
	return cd;
}


  reply	other threads:[~2012-03-26  1:37 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-23 14:29 [PATCH] ACPI: Implement overriding of arbitrary ACPI tables via initrd Thomas Renninger
2012-03-23 15:51 ` Thomas Renninger
2012-03-23 20:05 ` H. Peter Anvin
2012-03-24  1:42   ` Thomas Renninger
2012-03-24  2:01     ` H. Peter Anvin
2012-03-24  3:02       ` Thomas Renninger
2012-03-24  4:40         ` H. Peter Anvin
2012-03-24  4:43         ` H. Peter Anvin
2012-03-24  4:50           ` Yinghai Lu
2012-03-24  4:58             ` H. Peter Anvin
2012-03-24  9:24           ` Borislav Petkov
2012-03-24 18:49             ` H. Peter Anvin
2012-03-25  8:54               ` Borislav Petkov
2012-03-26  1:36                 ` H. Peter Anvin [this message]
2012-03-26 14:21                   ` Konrad Rzeszutek Wilk
2012-03-26  0:45           ` Thomas Renninger
2012-03-26  1:25             ` H. Peter Anvin
2012-03-26 14:19               ` Thomas Renninger
2012-03-26 14:46                 ` H. Peter Anvin
2012-03-26 14:51                   ` Thomas Renninger
2012-03-27  4:15                     ` H. Peter Anvin
2012-03-27  4:46               ` Peter Stuge
2012-03-27  6:18                 ` H. Peter Anvin
2012-03-24 18:42       ` Konrad Rzeszutek Wilk
2012-03-24 19:15         ` H. Peter Anvin
2012-03-24 19:17           ` Konrad Rzeszutek Wilk
2012-03-24 19:44             ` H. Peter Anvin
2012-03-24 22:21             ` H. Peter Anvin
2012-03-24 22:44               ` H. Peter Anvin
2012-03-25  9:25                 ` Borislav Petkov
2012-03-25 23:29                   ` H. Peter Anvin
2012-03-25  4:17               ` H. Peter Anvin
2012-03-25  9:07                 ` Borislav Petkov
2012-03-23 20:54 ` Yinghai Lu
2012-03-24  0:15   ` Yinghai Lu
2012-03-24  1:05   ` Yinghai Lu
2012-03-24  1:22     ` Thomas Renninger
2012-03-24  1:26   ` Thomas Renninger
2012-03-24  4:41     ` Yinghai Lu
2012-03-26  0:45       ` Thomas Renninger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F6FC838.7000904@zytor.com \
    --to=hpa@zytor.com \
    --cc=bp@alien8.de \
    --cc=dsdt@gaugusch.at \
    --cc=eric.piel@tremplin-utc.net \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.m.lin@intel.com \
    --cc=robert.moore@intel.com \
    --cc=trenn@suse.de \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vojcek@tlen.pl \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox