Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdint.h> |
Aaron Durbin | 4dd87fb | 2013-04-24 16:28:52 -0500 | [diff] [blame] | 17 | #include <bootstate.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 18 | #include <cbmem.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 19 | |
| 20 | typedef struct file { |
| 21 | uint32_t magic; |
| 22 | struct file *next; |
| 23 | char *filename; |
| 24 | char *data; |
| 25 | int offset; |
| 26 | int len; |
| 27 | } FILE; |
| 28 | |
| 29 | #define SEEK_SET 0 /* Seek from beginning of file. */ |
| 30 | |
| 31 | #define DIR_SEPARATOR '/' |
| 32 | #define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) |
| 33 | #define HAS_DRIVE_SPEC(f) (0) |
| 34 | |
| 35 | #define COVERAGE_SIZE (32*1024) |
| 36 | |
Jean Lucas | 9ab9c33 | 2016-01-30 01:20:54 -0500 | [diff] [blame] | 37 | #define COVERAGE_MAGIC 0x584d4153 |
Paul Menzel | c824c26 | 2015-10-05 20:02:09 +0200 | [diff] [blame] | 38 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 39 | static FILE *current_file = NULL; |
| 40 | static FILE *previous_file = NULL; |
| 41 | |
| 42 | static FILE *fopen(const char *path, const char *mode) |
| 43 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 44 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 45 | printk(BIOS_DEBUG, "fopen %s with mode %s\n", |
| 46 | path, mode); |
| 47 | #endif |
| 48 | if (!current_file) { |
| 49 | current_file = cbmem_add(CBMEM_ID_COVERAGE, 32*1024); |
| 50 | } else { |
| 51 | previous_file = current_file; |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 52 | current_file = |
| 53 | (FILE *)(ALIGN(((unsigned long)previous_file->data |
| 54 | + previous_file->len), 16)); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // TODO check if we're at the end of the CBMEM region (ENOMEM) |
| 58 | if (current_file) { |
Paul Menzel | c824c26 | 2015-10-05 20:02:09 +0200 | [diff] [blame] | 59 | current_file->magic = COVERAGE_MAGIC; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 60 | current_file->next = NULL; |
| 61 | if (previous_file) |
| 62 | previous_file->next = current_file; |
| 63 | current_file->filename = (char *)¤t_file[1]; |
| 64 | strcpy(current_file->filename, path); |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 65 | current_file->data = |
| 66 | (char *)ALIGN(((unsigned long)current_file->filename |
| 67 | + strlen(path) + 1), 16); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 68 | current_file->offset = 0; |
| 69 | current_file->len = 0; |
| 70 | } |
| 71 | |
| 72 | return current_file; |
| 73 | } |
| 74 | |
| 75 | static int fclose(FILE *stream) |
| 76 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 77 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 78 | printk(BIOS_DEBUG, "fclose %s\n", stream->filename); |
| 79 | #endif |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int fseek(FILE *stream, long offset, int whence) |
| 84 | { |
| 85 | /* fseek should only be called with offset==0 and whence==SEEK_SET |
| 86 | * to a freshly opened file. */ |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 87 | gcc_assert(offset == 0 && whence == SEEK_SET); |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 88 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | 84463ef | 2013-04-05 13:49:55 -0700 | [diff] [blame] | 89 | printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n", |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 90 | stream->filename, offset, whence); |
| 91 | #endif |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static long ftell(FILE *stream) |
| 96 | { |
| 97 | /* ftell should currently not be called */ |
| 98 | gcc_assert(0); |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 99 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 100 | printk(BIOS_DEBUG, "ftell %s\n", stream->filename); |
| 101 | #endif |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 106 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 107 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 108 | printk(BIOS_DEBUG, "fread: ptr=%p size=%zd nmemb=%zd FILE*=%p\n", |
| 109 | ptr, size, nmemb, stream); |
| 110 | #endif |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 115 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 116 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Lee Leahy | 36984d8 | 2017-03-10 17:56:44 -0800 | [diff] [blame] | 117 | printk(BIOS_DEBUG, "fwrite: %zd * %zd bytes to file %s\n", |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 118 | nmemb, size, stream->filename); |
| 119 | #endif |
| 120 | // TODO check if file is last opened file and fail otherwise. |
| 121 | |
| 122 | memcpy(stream->data + stream->offset, ptr, size * nmemb); |
| 123 | stream->len += (nmemb * size) - (stream->len - stream->offset); |
| 124 | stream->offset += nmemb * size; |
| 125 | return nmemb; |
| 126 | } |
| 127 | |
| 128 | static void setbuf(FILE *stream, char *buf) |
| 129 | { |
| 130 | gcc_assert(buf == 0); |
| 131 | } |
| 132 | |
Aaron Durbin | 4dd87fb | 2013-04-24 16:28:52 -0500 | [diff] [blame] | 133 | static void coverage_init(void *unused) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 134 | { |
| 135 | extern long __CTOR_LIST__; |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 136 | typedef void (*func_ptr)(void); |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 137 | func_ptr *ctor = (func_ptr *) &__CTOR_LIST__; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 138 | if (ctor == NULL) |
| 139 | return; |
| 140 | |
Lee Leahy | 2f919ec | 2017-03-08 17:37:06 -0800 | [diff] [blame] | 141 | for (; *ctor != (func_ptr) 0; ctor++) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 142 | (*ctor)(); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void __gcov_flush(void); |
Aaron Durbin | 4dd87fb | 2013-04-24 16:28:52 -0500 | [diff] [blame] | 146 | static void coverage_exit(void *unused) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 147 | { |
Martin Roth | 1bf55b4 | 2017-06-24 14:16:38 -0600 | [diff] [blame] | 148 | #if IS_ENABLED(CONFIG_DEBUG_COVERAGE) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 149 | printk(BIOS_DEBUG, "Syncing coverage data.\n"); |
| 150 | #endif |
| 151 | __gcov_flush(); |
| 152 | } |
| 153 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 154 | BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL); |
| 155 | BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL); |
| 156 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL); |