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