blob: 3ec7d7a6477ab0c74966baa5068e7ad1ee66abb4 [file] [log] [blame]
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001/*
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Stefan Reinauerd37ab452012-12-18 16:23:28 -080018 */
19
20#include <stdint.h>
Aaron Durbin4dd87fb2013-04-24 16:28:52 -050021#include <bootstate.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080022#include <cbmem.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080023
24typedef struct file {
25 uint32_t magic;
26 struct file *next;
27 char *filename;
28 char *data;
29 int offset;
30 int len;
31} FILE;
32
33#define SEEK_SET 0 /* Seek from beginning of file. */
34
35#define DIR_SEPARATOR '/'
36#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
37#define HAS_DRIVE_SPEC(f) (0)
38
39#define COVERAGE_SIZE (32*1024)
40
Paul Menzelc824c262015-10-05 20:02:09 +020041#define COVERAGE_MAGIC 0x584d41534
42
Stefan Reinauerd37ab452012-12-18 16:23:28 -080043static FILE *current_file = NULL;
44static FILE *previous_file = NULL;
45
46static FILE *fopen(const char *path, const char *mode)
47{
48#if CONFIG_DEBUG_COVERAGE
49 printk(BIOS_DEBUG, "fopen %s with mode %s\n",
50 path, mode);
51#endif
52 if (!current_file) {
53 current_file = cbmem_add(CBMEM_ID_COVERAGE, 32*1024);
54 } else {
55 previous_file = current_file;
56 current_file = (FILE *)(ALIGN(((unsigned long)previous_file->data + previous_file->len), 16));
57 }
58
59 // TODO check if we're at the end of the CBMEM region (ENOMEM)
60 if (current_file) {
Paul Menzelc824c262015-10-05 20:02:09 +020061 current_file->magic = COVERAGE_MAGIC;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080062 current_file->next = NULL;
63 if (previous_file)
64 previous_file->next = current_file;
65 current_file->filename = (char *)&current_file[1];
66 strcpy(current_file->filename, path);
67 current_file->data = (char *)ALIGN(((unsigned long)current_file->filename + strlen(path) + 1), 16);
68 current_file->offset = 0;
69 current_file->len = 0;
70 }
71
72 return current_file;
73}
74
75static int fclose(FILE *stream)
76{
77#if CONFIG_DEBUG_COVERAGE
78 printk(BIOS_DEBUG, "fclose %s\n", stream->filename);
79#endif
80 return 0;
81}
82
83static 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. */
87 gcc_assert (offset == 0 && whence == SEEK_SET);
88#if CONFIG_DEBUG_COVERAGE
Stefan Reinauer84463ef2013-04-05 13:49:55 -070089 printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -080090 stream->filename, offset, whence);
91#endif
92 return 0;
93}
94
95static long ftell(FILE *stream)
96{
97 /* ftell should currently not be called */
98 gcc_assert(0);
99#if CONFIG_DEBUG_COVERAGE
100 printk(BIOS_DEBUG, "ftell %s\n", stream->filename);
101#endif
102 return 0;
103}
104
105static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
106{
107#if CONFIG_DEBUG_COVERAGE
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
114static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
115{
116#if CONFIG_DEBUG_COVERAGE
117 printk(BIOS_DEBUG, "fwrite: %zd * 0x%zd bytes to file %s\n",
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
128static void setbuf(FILE *stream, char *buf)
129{
130 gcc_assert(buf == 0);
131}
132
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500133static void coverage_init(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800134{
135 extern long __CTOR_LIST__;
136 typedef void (*func_ptr)(void) ;
137 func_ptr *ctor = (func_ptr*) &__CTOR_LIST__;
138 if (ctor == NULL)
139 return;
140
141 for ( ; *ctor != (func_ptr) 0; ctor++) {
142 (*ctor)();
143 }
144}
145
146void __gcov_flush(void);
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500147static void coverage_exit(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800148{
149#if CONFIG_DEBUG_COVERAGE
150 printk(BIOS_DEBUG, "Syncing coverage data.\n");
151#endif
152 __gcov_flush();
153}
154
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500155BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
156BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
157BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);