blob: 6bdb870195b623e56aad211900599692de282ad3 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauerd37ab452012-12-18 16:23:28 -08002
3#include <stdint.h>
Aaron Durbin4dd87fb2013-04-24 16:28:52 -05004#include <bootstate.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -08005#include <cbmem.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -08006
7typedef struct file {
8 uint32_t magic;
9 struct file *next;
10 char *filename;
11 char *data;
12 int offset;
13 int len;
14} FILE;
15
16#define SEEK_SET 0 /* Seek from beginning of file. */
17
18#define DIR_SEPARATOR '/'
19#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
20#define HAS_DRIVE_SPEC(f) (0)
21
22#define COVERAGE_SIZE (32*1024)
23
Jean Lucas9ab9c332016-01-30 01:20:54 -050024#define COVERAGE_MAGIC 0x584d4153
Paul Menzelc824c262015-10-05 20:02:09 +020025
Stefan Reinauerd37ab452012-12-18 16:23:28 -080026static FILE *current_file = NULL;
27static FILE *previous_file = NULL;
28
29static FILE *fopen(const char *path, const char *mode)
30{
Julius Wernercd49cce2019-03-05 16:53:33 -080031#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080032 printk(BIOS_DEBUG, "fopen %s with mode %s\n",
33 path, mode);
34#endif
35 if (!current_file) {
36 current_file = cbmem_add(CBMEM_ID_COVERAGE, 32*1024);
37 } else {
38 previous_file = current_file;
Lee Leahy73402172017-03-10 15:23:24 -080039 current_file =
Elyes Haouasd6b6b222022-10-10 12:34:21 +020040 (FILE *)(ALIGN_UP(((unsigned long)previous_file->data
Lee Leahy73402172017-03-10 15:23:24 -080041 + previous_file->len), 16));
Stefan Reinauerd37ab452012-12-18 16:23:28 -080042 }
43
44 // TODO check if we're at the end of the CBMEM region (ENOMEM)
45 if (current_file) {
Paul Menzelc824c262015-10-05 20:02:09 +020046 current_file->magic = COVERAGE_MAGIC;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080047 current_file->next = NULL;
48 if (previous_file)
49 previous_file->next = current_file;
50 current_file->filename = (char *)&current_file[1];
51 strcpy(current_file->filename, path);
Lee Leahy73402172017-03-10 15:23:24 -080052 current_file->data =
Elyes Haouasd6b6b222022-10-10 12:34:21 +020053 (char *)ALIGN_UP(((unsigned long)current_file->filename
Lee Leahy73402172017-03-10 15:23:24 -080054 + strlen(path) + 1), 16);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080055 current_file->offset = 0;
56 current_file->len = 0;
57 }
58
59 return current_file;
60}
61
62static int fclose(FILE *stream)
63{
Julius Wernercd49cce2019-03-05 16:53:33 -080064#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080065 printk(BIOS_DEBUG, "fclose %s\n", stream->filename);
66#endif
67 return 0;
68}
69
70static int fseek(FILE *stream, long offset, int whence)
71{
72 /* fseek should only be called with offset==0 and whence==SEEK_SET
73 * to a freshly opened file. */
Lee Leahy38768c32017-03-09 14:07:18 -080074 gcc_assert(offset == 0 && whence == SEEK_SET);
Julius Wernercd49cce2019-03-05 16:53:33 -080075#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauer84463ef2013-04-05 13:49:55 -070076 printk(BIOS_DEBUG, "fseek %s offset=%ld whence=%d\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -080077 stream->filename, offset, whence);
78#endif
79 return 0;
80}
81
82static long ftell(FILE *stream)
83{
84 /* ftell should currently not be called */
Patrick Georgiac011062020-08-03 08:40:11 +020085 BUG();
Julius Wernercd49cce2019-03-05 16:53:33 -080086#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080087 printk(BIOS_DEBUG, "ftell %s\n", stream->filename);
88#endif
89 return 0;
90}
91
92static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
93{
Julius Wernercd49cce2019-03-05 16:53:33 -080094#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080095 printk(BIOS_DEBUG, "fread: ptr=%p size=%zd nmemb=%zd FILE*=%p\n",
96 ptr, size, nmemb, stream);
97#endif
98 return 0;
99}
100
101static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
102{
Julius Wernercd49cce2019-03-05 16:53:33 -0800103#if CONFIG(DEBUG_COVERAGE)
Lee Leahy36984d82017-03-10 17:56:44 -0800104 printk(BIOS_DEBUG, "fwrite: %zd * %zd bytes to file %s\n",
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800105 nmemb, size, stream->filename);
106#endif
107 // TODO check if file is last opened file and fail otherwise.
108
109 memcpy(stream->data + stream->offset, ptr, size * nmemb);
110 stream->len += (nmemb * size) - (stream->len - stream->offset);
111 stream->offset += nmemb * size;
112 return nmemb;
113}
114
115static void setbuf(FILE *stream, char *buf)
116{
117 gcc_assert(buf == 0);
118}
119
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500120static void coverage_init(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800121{
122 extern long __CTOR_LIST__;
Lee Leahy35af5c42017-03-09 17:35:28 -0800123 typedef void (*func_ptr)(void);
Lee Leahyb2d834a2017-03-08 16:52:22 -0800124 func_ptr *ctor = (func_ptr *) &__CTOR_LIST__;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800125 if (ctor == NULL)
126 return;
127
Lee Leahy2f919ec2017-03-08 17:37:06 -0800128 for (; *ctor != (func_ptr) 0; ctor++)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800129 (*ctor)();
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800130}
131
132void __gcov_flush(void);
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500133static void coverage_exit(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800134{
Julius Wernercd49cce2019-03-05 16:53:33 -0800135#if CONFIG(DEBUG_COVERAGE)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800136 printk(BIOS_DEBUG, "Syncing coverage data.\n");
137#endif
138 __gcov_flush();
139}
140
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500141BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
142BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
143BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);