blob: b7d4b7331ac42eca0d4b3f2978eb66753cf5cf26 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauerd37ab452012-12-18 16:23:28 -08003
4#include <stdint.h>
Aaron Durbin4dd87fb2013-04-24 16:28:52 -05005#include <bootstate.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -08006#include <cbmem.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -08007
8typedef struct file {
9 uint32_t magic;
10 struct file *next;
11 char *filename;
12 char *data;
13 int offset;
14 int len;
15} FILE;
16
17#define SEEK_SET 0 /* Seek from beginning of file. */
18
19#define DIR_SEPARATOR '/'
20#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
21#define HAS_DRIVE_SPEC(f) (0)
22
23#define COVERAGE_SIZE (32*1024)
24
Jean Lucas9ab9c332016-01-30 01:20:54 -050025#define COVERAGE_MAGIC 0x584d4153
Paul Menzelc824c262015-10-05 20:02:09 +020026
Stefan Reinauerd37ab452012-12-18 16:23:28 -080027static FILE *current_file = NULL;
28static FILE *previous_file = NULL;
29
30static FILE *fopen(const char *path, const char *mode)
31{
Julius Wernercd49cce2019-03-05 16:53:33 -080032#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080033 printk(BIOS_DEBUG, "fopen %s with mode %s\n",
34 path, mode);
35#endif
36 if (!current_file) {
37 current_file = cbmem_add(CBMEM_ID_COVERAGE, 32*1024);
38 } else {
39 previous_file = current_file;
Lee Leahy73402172017-03-10 15:23:24 -080040 current_file =
41 (FILE *)(ALIGN(((unsigned long)previous_file->data
42 + previous_file->len), 16));
Stefan Reinauerd37ab452012-12-18 16:23:28 -080043 }
44
45 // TODO check if we're at the end of the CBMEM region (ENOMEM)
46 if (current_file) {
Paul Menzelc824c262015-10-05 20:02:09 +020047 current_file->magic = COVERAGE_MAGIC;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080048 current_file->next = NULL;
49 if (previous_file)
50 previous_file->next = current_file;
51 current_file->filename = (char *)&current_file[1];
52 strcpy(current_file->filename, path);
Lee Leahy73402172017-03-10 15:23:24 -080053 current_file->data =
54 (char *)ALIGN(((unsigned long)current_file->filename
55 + strlen(path) + 1), 16);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080056 current_file->offset = 0;
57 current_file->len = 0;
58 }
59
60 return current_file;
61}
62
63static int fclose(FILE *stream)
64{
Julius Wernercd49cce2019-03-05 16:53:33 -080065#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080066 printk(BIOS_DEBUG, "fclose %s\n", stream->filename);
67#endif
68 return 0;
69}
70
71static int fseek(FILE *stream, long offset, int whence)
72{
73 /* fseek should only be called with offset==0 and whence==SEEK_SET
74 * to a freshly opened file. */
Lee Leahy38768c32017-03-09 14:07:18 -080075 gcc_assert(offset == 0 && whence == SEEK_SET);
Julius Wernercd49cce2019-03-05 16:53:33 -080076#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauer84463ef2013-04-05 13:49:55 -070077 printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -080078 stream->filename, offset, whence);
79#endif
80 return 0;
81}
82
83static long ftell(FILE *stream)
84{
85 /* ftell should currently not be called */
86 gcc_assert(0);
Julius Wernercd49cce2019-03-05 16:53:33 -080087#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080088 printk(BIOS_DEBUG, "ftell %s\n", stream->filename);
89#endif
90 return 0;
91}
92
93static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
94{
Julius Wernercd49cce2019-03-05 16:53:33 -080095#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080096 printk(BIOS_DEBUG, "fread: ptr=%p size=%zd nmemb=%zd FILE*=%p\n",
97 ptr, size, nmemb, stream);
98#endif
99 return 0;
100}
101
102static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
103{
Julius Wernercd49cce2019-03-05 16:53:33 -0800104#if CONFIG(DEBUG_COVERAGE)
Lee Leahy36984d82017-03-10 17:56:44 -0800105 printk(BIOS_DEBUG, "fwrite: %zd * %zd bytes to file %s\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800106 nmemb, size, stream->filename);
107#endif
108 // TODO check if file is last opened file and fail otherwise.
109
110 memcpy(stream->data + stream->offset, ptr, size * nmemb);
111 stream->len += (nmemb * size) - (stream->len - stream->offset);
112 stream->offset += nmemb * size;
113 return nmemb;
114}
115
116static void setbuf(FILE *stream, char *buf)
117{
118 gcc_assert(buf == 0);
119}
120
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500121static void coverage_init(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800122{
123 extern long __CTOR_LIST__;
Lee Leahy35af5c42017-03-09 17:35:28 -0800124 typedef void (*func_ptr)(void);
Lee Leahyb2d834a2017-03-08 16:52:22 -0800125 func_ptr *ctor = (func_ptr *) &__CTOR_LIST__;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800126 if (ctor == NULL)
127 return;
128
Lee Leahy2f919ec2017-03-08 17:37:06 -0800129 for (; *ctor != (func_ptr) 0; ctor++)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800130 (*ctor)();
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800131}
132
133void __gcov_flush(void);
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500134static void coverage_exit(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800135{
Julius Wernercd49cce2019-03-05 16:53:33 -0800136#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800137 printk(BIOS_DEBUG, "Syncing coverage data.\n");
138#endif
139 __gcov_flush();
140}
141
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500142BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
143BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
144BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);