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 | |
Paul Menzel | c824c26 | 2015-10-05 20:02:09 +0200 | [diff] [blame] | 37 | #define COVERAGE_MAGIC 0x584d41534 |
| 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 | { |
| 44 | #if CONFIG_DEBUG_COVERAGE |
| 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; |
| 52 | current_file = (FILE *)(ALIGN(((unsigned long)previous_file->data + previous_file->len), 16)); |
| 53 | } |
| 54 | |
| 55 | // TODO check if we're at the end of the CBMEM region (ENOMEM) |
| 56 | if (current_file) { |
Paul Menzel | c824c26 | 2015-10-05 20:02:09 +0200 | [diff] [blame] | 57 | current_file->magic = COVERAGE_MAGIC; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 58 | current_file->next = NULL; |
| 59 | if (previous_file) |
| 60 | previous_file->next = current_file; |
| 61 | current_file->filename = (char *)¤t_file[1]; |
| 62 | strcpy(current_file->filename, path); |
| 63 | current_file->data = (char *)ALIGN(((unsigned long)current_file->filename + strlen(path) + 1), 16); |
| 64 | current_file->offset = 0; |
| 65 | current_file->len = 0; |
| 66 | } |
| 67 | |
| 68 | return current_file; |
| 69 | } |
| 70 | |
| 71 | static int fclose(FILE *stream) |
| 72 | { |
| 73 | #if CONFIG_DEBUG_COVERAGE |
| 74 | printk(BIOS_DEBUG, "fclose %s\n", stream->filename); |
| 75 | #endif |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int fseek(FILE *stream, long offset, int whence) |
| 80 | { |
| 81 | /* fseek should only be called with offset==0 and whence==SEEK_SET |
| 82 | * to a freshly opened file. */ |
| 83 | gcc_assert (offset == 0 && whence == SEEK_SET); |
| 84 | #if CONFIG_DEBUG_COVERAGE |
Stefan Reinauer | 84463ef | 2013-04-05 13:49:55 -0700 | [diff] [blame] | 85 | printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n", |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 86 | stream->filename, offset, whence); |
| 87 | #endif |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static long ftell(FILE *stream) |
| 92 | { |
| 93 | /* ftell should currently not be called */ |
| 94 | gcc_assert(0); |
| 95 | #if CONFIG_DEBUG_COVERAGE |
| 96 | printk(BIOS_DEBUG, "ftell %s\n", stream->filename); |
| 97 | #endif |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 102 | { |
| 103 | #if CONFIG_DEBUG_COVERAGE |
| 104 | printk(BIOS_DEBUG, "fread: ptr=%p size=%zd nmemb=%zd FILE*=%p\n", |
| 105 | ptr, size, nmemb, stream); |
| 106 | #endif |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 111 | { |
| 112 | #if CONFIG_DEBUG_COVERAGE |
| 113 | printk(BIOS_DEBUG, "fwrite: %zd * 0x%zd bytes to file %s\n", |
| 114 | nmemb, size, stream->filename); |
| 115 | #endif |
| 116 | // TODO check if file is last opened file and fail otherwise. |
| 117 | |
| 118 | memcpy(stream->data + stream->offset, ptr, size * nmemb); |
| 119 | stream->len += (nmemb * size) - (stream->len - stream->offset); |
| 120 | stream->offset += nmemb * size; |
| 121 | return nmemb; |
| 122 | } |
| 123 | |
| 124 | static void setbuf(FILE *stream, char *buf) |
| 125 | { |
| 126 | gcc_assert(buf == 0); |
| 127 | } |
| 128 | |
Aaron Durbin | 4dd87fb | 2013-04-24 16:28:52 -0500 | [diff] [blame] | 129 | static void coverage_init(void *unused) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 130 | { |
| 131 | extern long __CTOR_LIST__; |
| 132 | typedef void (*func_ptr)(void) ; |
| 133 | func_ptr *ctor = (func_ptr*) &__CTOR_LIST__; |
| 134 | if (ctor == NULL) |
| 135 | return; |
| 136 | |
| 137 | for ( ; *ctor != (func_ptr) 0; ctor++) { |
| 138 | (*ctor)(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void __gcov_flush(void); |
Aaron Durbin | 4dd87fb | 2013-04-24 16:28:52 -0500 | [diff] [blame] | 143 | static void coverage_exit(void *unused) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 144 | { |
| 145 | #if CONFIG_DEBUG_COVERAGE |
| 146 | printk(BIOS_DEBUG, "Syncing coverage data.\n"); |
| 147 | #endif |
| 148 | __gcov_flush(); |
| 149 | } |
| 150 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 151 | BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL); |
| 152 | BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL); |
| 153 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL); |