-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathlog.c
More file actions
169 lines (143 loc) · 4.39 KB
/
log.c
File metadata and controls
169 lines (143 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* Based on an idea by https://github.com/JamesC1 */
/* Support for writing the console log to a file.
*
* The main problem here is that some devices (e.g. SD cards) have limited
* write cycles, so writing every character to them is problematic. Thus, we
* buffer writes to try and do, e.g. one block at a time. The problem here is
* that if the guest OS crashes at some point, the buffer may not be flushed to
* disk. We try and ameliorate this by 1) providing the guest OS with a
* fflush() function to flush at important moments, and 2) automatically
* flushing if log_putc is called and it has been more than a certain time
* since the last flush.
*
* This can still miss certain log outputs, but is probably the best
* compromise. The ideal situation would be to have a background thread
* periodically flushing the buffer to disk, but as we are only a bootloader,
* we cannot hijack the timer interrupt that the guest OS would probably want
* to use itself.
*/
#include <stdint.h>
#include <stdlib.h>
#include "timer.h"
#include "vfs.h"
#include "output.h"
FILE *log_fp = NULL;
uint8_t *log_buf = NULL;
size_t buf_size;
size_t buf_ptr;
struct timer_wait last_update;
// Time between flushes (requires that log_putc is actually called at some point)
#define LOG_TIMEOUT 5000000
int log_putc(int c)
{
if(last_update.trigger_value == 0)
last_update = register_timer(LOG_TIMEOUT);
if(log_buf && buf_size)
{
log_buf[buf_ptr++] = c;
if(buf_ptr >= buf_size)
{
if(log_fp && log_fp->fflush_cb)
{
log_fp->fflush_cb(log_fp);
last_update = register_timer(LOG_TIMEOUT);
}
buf_ptr = 0;
}
return 0;
}
else if(log_fp)
{
// Disable output to the log for the write
rpi_boot_output_state state = output_get_state();
output_disable_log();
// Write one character
fwrite(&c, 1, 1, log_fp);
// Restore saved output state
output_restore_state(state);
return 0;
}
return EOF;
}
static int log_fflush(FILE *fp)
{
// Flush the buffer
if(fp && log_buf)
{
// Disable output to the log for the write
rpi_boot_output_state state = output_get_state();
output_disable_log();
// Write the buffer
fwrite(log_buf, 1, buf_ptr, fp);
// Restore the state
output_restore_state(state);
}
return 0;
}
int register_log_file(FILE *fp, size_t buffer_size)
{
// If we have a current log, flush it
if(log_fp)
{
fflush(log_fp);
// deregister fflush callback
log_fp->fflush_cb = NULL;
}
// If passed NULL, then set no log file
if(fp == NULL)
{
if(log_buf)
free(log_buf);
// We can still use a buffer without a file, for flushing
// later to the file
if(buffer_size)
log_buf = (uint8_t *)malloc(buffer_size);
else
log_buf = NULL;
buf_size = buffer_size;
buf_ptr = 0;
log_fp = NULL;
return 0;
}
// Store the fflush callback
fp->fflush_cb = log_fflush;
// If no current log, and there is a buffer, then flush
// what's in it to the new file
if(!log_fp && log_buf)
{
log_fp = fp;
fflush(fp);
}
// If we have a buffer free it
if((buf_size != buffer_size) && log_buf)
log_buf = (uint8_t *)realloc(log_buf, buffer_size);
else if(log_buf == NULL)
log_buf = (uint8_t *)malloc(buffer_size);
buf_size = buffer_size;
buf_ptr = 0;
// Store the log file pointer
log_fp = fp;
return 0;
}
FILE *get_log_file()
{
return log_fp;
}