• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Linux kernel image support for libdwfl.
2    Copyright (C) 2009-2011 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7 
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11 
12    or
13 
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17 
18    or both in parallel, as here.
19 
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24 
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28 
29 #include "libdwflP.h"
30 #include "system.h"
31 
32 #include <unistd.h>
33 #include <endian.h>
34 
35 #if BYTE_ORDER == LITTLE_ENDIAN
36 # define LE16(x)	(x)
37 #else
38 # define LE16(x)	bswap_16 (x)
39 #endif
40 
41 /* See Documentation/x86/boot.txt in Linux kernel sources
42    for an explanation of these format details.  */
43 
44 #define MAGIC1			0xaa55
45 #define MAGIC2			0x53726448 /* "HdrS" little-endian */
46 #define MIN_VERSION		0x0208
47 
48 #define H_START			(H_SETUP_SECTS & -4)
49 #define H_SETUP_SECTS		0x1f1
50 #define H_MAGIC1		0x1fe
51 #define H_MAGIC2		0x202
52 #define H_VERSION		0x206
53 #define H_PAYLOAD_OFFSET	0x248
54 #define H_PAYLOAD_LENGTH	0x24c
55 #define H_END			0x250
56 #define H_READ_SIZE		(H_END - H_START)
57 
58 Dwfl_Error
59 internal_function
__libdw_image_header(int fd,off_t * start_offset,void * mapped,size_t mapped_size)60 __libdw_image_header (int fd, off_t *start_offset,
61 		      void *mapped, size_t mapped_size)
62 {
63   if (likely (mapped_size > H_END))
64     {
65       const void *header = mapped;
66       char header_buffer[H_READ_SIZE];
67       if (header == NULL)
68 	{
69 	  ssize_t n = pread_retry (fd, header_buffer, H_READ_SIZE,
70 				   *start_offset + H_START);
71 	  if (n < 0)
72 	    return DWFL_E_ERRNO;
73 	  if (n < H_READ_SIZE)
74 	    return DWFL_E_BADELF;
75 
76 	  header = header_buffer - H_START;
77 	}
78 
79       if (*(uint16_t *) (header + H_MAGIC1) == LE16 (MAGIC1)
80 	  && *(uint32_t *) (header + H_MAGIC2) == LE32 (MAGIC2)
81 	  && LE16 (*(uint16_t *) (header + H_VERSION)) >= MIN_VERSION)
82 	{
83 	  /* The magic numbers match and the version field is sufficient.
84 	     Extract the payload bounds.  */
85 
86 	  uint32_t offset = LE32 (*(uint32_t *) (header + H_PAYLOAD_OFFSET));
87 	  uint32_t length = LE32 (*(uint32_t *) (header + H_PAYLOAD_LENGTH));
88 
89 	  offset += ((*(uint8_t *) (header + H_SETUP_SECTS) ?: 4) + 1) * 512;
90 
91 	  if (offset > H_END && offset < mapped_size
92 	      && mapped_size - offset >= length)
93 	    {
94 	      /* It looks kosher.  Use it!  */
95 	      *start_offset += offset;
96 	      return DWFL_E_NOERROR;
97 	    }
98 	}
99     }
100   return DWFL_E_BADELF;
101 }
102