blob: 4e46367ab120fdd0fb044a82384b7e04e028f099 [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
Jean Lucas9ab9c332016-01-30 01:20:54 -050037#define COVERAGE_MAGIC 0x584d4153
Paul Menzelc824c262015-10-05 20:02:09 +020038
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;
Lee Leahy73402172017-03-10 15:23:24 -080052 current_file =
53 (FILE *)(ALIGN(((unsigned long)previous_file->data
54 + previous_file->len), 16));
Stefan Reinauerd37ab452012-12-18 16:23:28 -080055 }
56
57 // TODO check if we're at the end of the CBMEM region (ENOMEM)
58 if (current_file) {
Paul Menzelc824c262015-10-05 20:02:09 +020059 current_file->magic = COVERAGE_MAGIC;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080060 current_file->next = NULL;
61 if (previous_file)
62 previous_file->next = current_file;
63 current_file->filename = (char *)&current_file[1];
64 strcpy(current_file->filename, path);
Lee Leahy73402172017-03-10 15:23:24 -080065 current_file->data =
66 (char *)ALIGN(((unsigned long)current_file->filename
67 + strlen(path) + 1), 16);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080068 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. */
Lee Leahy38768c32017-03-09 14:07:18 -080087 gcc_assert(offset == 0 && whence == SEEK_SET);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080088#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__;
Lee Leahy35af5c42017-03-09 17:35:28 -0800136 typedef void (*func_ptr)(void);
Lee Leahyb2d834a2017-03-08 16:52:22 -0800137 func_ptr *ctor = (func_ptr *) &__CTOR_LIST__;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800138 if (ctor == NULL)
139 return;
140
Lee Leahy2f919ec2017-03-08 17:37:06 -0800141 for (; *ctor != (func_ptr) 0; ctor++)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800142 (*ctor)();
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800143}
144
145void __gcov_flush(void);
Aaron Durbin4dd87fb2013-04-24 16:28:52 -0500146static void coverage_exit(void *unused)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800147{
148#if CONFIG_DEBUG_COVERAGE
149 printk(BIOS_DEBUG, "Syncing coverage data.\n");
150#endif
151 __gcov_flush();
152}
153
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500154BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
155BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
156BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);