Blame libdwfl/image-header.c

Packit 032894
/* Linux kernel image support for libdwfl.
Packit 032894
   Copyright (C) 2009-2011 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include "libdwflP.h"
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
#include <unistd.h>
Packit 032894
#include <endian.h>
Packit 032894
Packit 032894
#if BYTE_ORDER == LITTLE_ENDIAN
Packit 032894
# define LE16(x)	(x)
Packit 032894
#else
Packit 032894
# define LE16(x)	bswap_16 (x)
Packit 032894
#endif
Packit 032894
Packit 032894
/* See Documentation/x86/boot.txt in Linux kernel sources
Packit 032894
   for an explanation of these format details.  */
Packit 032894
Packit 032894
#define MAGIC1			0xaa55
Packit 032894
#define MAGIC2			0x53726448 /* "HdrS" little-endian */
Packit 032894
#define MIN_VERSION		0x0208
Packit 032894
Packit 032894
#define H_START			(H_SETUP_SECTS & -4)
Packit 032894
#define H_SETUP_SECTS		0x1f1
Packit 032894
#define H_MAGIC1		0x1fe
Packit 032894
#define H_MAGIC2		0x202
Packit 032894
#define H_VERSION		0x206
Packit 032894
#define H_PAYLOAD_OFFSET	0x248
Packit 032894
#define H_PAYLOAD_LENGTH	0x24c
Packit 032894
#define H_END			0x250
Packit 032894
#define H_READ_SIZE		(H_END - H_START)
Packit 032894
Packit 032894
Dwfl_Error
Packit 032894
internal_function
Packit 032894
__libdw_image_header (int fd, off_t *start_offset,
Packit 032894
		      void *mapped, size_t mapped_size)
Packit 032894
{
Packit 032894
  if (likely (mapped_size > H_END))
Packit 032894
    {
Packit 032894
      const void *header = mapped;
Packit 032894
      char header_buffer[H_READ_SIZE];
Packit 032894
      if (header == NULL)
Packit 032894
	{
Packit 032894
	  ssize_t n = pread_retry (fd, header_buffer, H_READ_SIZE,
Packit 032894
				   *start_offset + H_START);
Packit 032894
	  if (n < 0)
Packit 032894
	    return DWFL_E_ERRNO;
Packit 032894
	  if (n < H_READ_SIZE)
Packit 032894
	    return DWFL_E_BADELF;
Packit 032894
Packit 032894
	  header = header_buffer - H_START;
Packit 032894
	}
Packit 032894
Packit 032894
      if (*(uint16_t *) (header + H_MAGIC1) == LE16 (MAGIC1)
Packit 032894
	  && *(uint32_t *) (header + H_MAGIC2) == LE32 (MAGIC2)
Packit 032894
	  && LE16 (*(uint16_t *) (header + H_VERSION)) >= MIN_VERSION)
Packit 032894
	{
Packit 032894
	  /* The magic numbers match and the version field is sufficient.
Packit 032894
	     Extract the payload bounds.  */
Packit 032894
Packit 032894
	  uint32_t offset = LE32 (*(uint32_t *) (header + H_PAYLOAD_OFFSET));
Packit 032894
	  uint32_t length = LE32 (*(uint32_t *) (header + H_PAYLOAD_LENGTH));
Packit 032894
Packit 032894
	  offset += ((*(uint8_t *) (header + H_SETUP_SECTS) ?: 4) + 1) * 512;
Packit 032894
Packit 032894
	  if (offset > H_END && offset < mapped_size
Packit 032894
	      && mapped_size - offset >= length)
Packit 032894
	    {
Packit 032894
	      /* It looks kosher.  Use it!  */
Packit 032894
	      *start_offset += offset;
Packit 032894
	      return DWFL_E_NOERROR;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
    }
Packit 032894
  return DWFL_E_BADELF;
Packit 032894
}