blob: 4e5a9e13a0200add601b4a3fe263810c867f4a60 [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.
Stefan Reinauerd37ab452012-12-18 16:23:28 -080014 */
15
16#include <stdint.h>
Aaron Durbin4dd87fb2013-04-24 16:28:52 -050017#include <bootstate.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080018#include <cbmem.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080019
20typedef 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 Menzelc824c262015-10-05 20:02:09 +020037#define COVERAGE_MAGIC 0x584d41534
38
Stefan Reinauerd37ab452012-12-18 16:23:28 -080039static FILE *current_file = NULL;
40static FILE *previous_file = NULL;
41
42static 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 Menzelc824c262015-10-05 20:02:09 +020057 current_file->magic = COVERAGE_MAGIC;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080058 current_file->next = NULL;
59 if (previous_file)
60 previous_file->next = current_file;
61 current_file->filename = (char *)&current_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
71static 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
79static 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 Reinauer84463ef2013-04-05 13:49:55 -070085 printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -080086 stream->filename, offset, whence);
87#endif
88 return 0;
89}
90
91static 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
101static 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
110static 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
124static void setbuf(FILE *stream, char *buf)
125{
126 gcc_assert(buf == 0);
127}
128
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500129static void coverage_init(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800130{
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
142void __gcov_flush(void);
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500143static void coverage_exit(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800144{
145#if CONFIG_DEBUG_COVERAGE
146 printk(BIOS_DEBUG, "Syncing coverage data.\n");
147#endif
148 __gcov_flush();
149}
150
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500151BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
152BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
153BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);