Line data Source code
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 : #ifdef HAVE_CONFIG_H
30 : # include <config.h>
31 : #endif
32 :
33 : #include "libdwflP.h"
34 : #include "system.h"
35 :
36 : #include <unistd.h>
37 : #include <endian.h>
38 :
39 : #if BYTE_ORDER == LITTLE_ENDIAN
40 : # define LE16(x) (x)
41 : #else
42 : # define LE16(x) bswap_16 (x)
43 : #endif
44 :
45 : /* See Documentation/x86/boot.txt in Linux kernel sources
46 : for an explanation of these format details. */
47 :
48 : #define MAGIC1 0xaa55
49 : #define MAGIC2 0x53726448 /* "HdrS" little-endian */
50 : #define MIN_VERSION 0x0208
51 :
52 : #define H_START (H_SETUP_SECTS & -4)
53 : #define H_SETUP_SECTS 0x1f1
54 : #define H_MAGIC1 0x1fe
55 : #define H_MAGIC2 0x202
56 : #define H_VERSION 0x206
57 : #define H_PAYLOAD_OFFSET 0x248
58 : #define H_PAYLOAD_LENGTH 0x24c
59 : #define H_END 0x250
60 : #define H_READ_SIZE (H_END - H_START)
61 :
62 : Dwfl_Error
63 : internal_function
64 0 : __libdw_image_header (int fd, off_t *start_offset,
65 : void *mapped, size_t mapped_size)
66 : {
67 0 : if (likely (mapped_size > H_END))
68 : {
69 0 : const void *header = mapped;
70 : char header_buffer[H_READ_SIZE];
71 0 : if (header == NULL)
72 : {
73 0 : ssize_t n = pread_retry (fd, header_buffer, H_READ_SIZE,
74 0 : *start_offset + H_START);
75 0 : if (n < 0)
76 0 : return DWFL_E_ERRNO;
77 0 : if (n < H_READ_SIZE)
78 : return DWFL_E_BADELF;
79 :
80 : header = header_buffer - H_START;
81 : }
82 :
83 0 : if (*(uint16_t *) (header + H_MAGIC1) == LE16 (MAGIC1)
84 0 : && *(uint32_t *) (header + H_MAGIC2) == LE32 (MAGIC2)
85 0 : && LE16 (*(uint16_t *) (header + H_VERSION)) >= MIN_VERSION)
86 : {
87 : /* The magic numbers match and the version field is sufficient.
88 : Extract the payload bounds. */
89 :
90 0 : uint32_t offset = LE32 (*(uint32_t *) (header + H_PAYLOAD_OFFSET));
91 0 : uint32_t length = LE32 (*(uint32_t *) (header + H_PAYLOAD_LENGTH));
92 :
93 0 : offset += ((*(uint8_t *) (header + H_SETUP_SECTS) ?: 4) + 1) * 512;
94 :
95 0 : if (offset > H_END && offset < mapped_size
96 0 : && mapped_size - offset >= length)
97 : {
98 : /* It looks kosher. Use it! */
99 0 : *start_offset += offset;
100 0 : return DWFL_E_NOERROR;
101 : }
102 : }
103 : }
104 : return DWFL_E_BADELF;
105 : }
|