2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
20 #include <console/console.h>
21 #include <console/cbmem_console.h>
23 #include <arch/early_variables.h>
27 * Structure describing console buffer. It is overlaid on a flat memory area,
28 * with buffer_body covering the extent of the memory. Once the buffer is
29 * full, the cursor keeps going but the data is dropped on the floor. This
30 * allows to tell how much data was lost in the process.
32 struct cbmem_console
{
36 } __attribute__ ((__packed__
));
38 static struct cbmem_console
*cbmem_console_p CAR_GLOBAL
;
42 * While running from ROM, before DRAM is initialized, some area in cache as
43 * ram space is used for the console buffer storage. The size and location of
44 * the area are defined in the config.
47 extern struct cbmem_console preram_cbmem_console
;
52 * When running from RAM, a lot of console output is generated before CBMEM is
53 * reinitialized. This static buffer is used to store that output temporarily,
54 * to be concatenated with the CBMEM console buffer contents accumulated
55 * during the ROM stage, once CBMEM becomes available at RAM stage.
58 #if CONFIG_DYNAMIC_CBMEM
59 #define STATIC_CONSOLE_SIZE 1024
61 #define STATIC_CONSOLE_SIZE CONFIG_CONSOLE_CBMEM_BUFFER_SIZE
63 static u8 static_console
[STATIC_CONSOLE_SIZE
];
66 static inline struct cbmem_console
*current_console(void)
68 return car_get_var(cbmem_console_p
);
71 static inline void current_console_set(struct cbmem_console
*new_console_p
)
73 car_set_var(cbmem_console_p
, new_console_p
);
76 static inline void init_console_ptr(void *storage
, u32 total_space
)
78 struct cbmem_console
*cbm_cons_p
= storage
;
80 /* Initialize the cache-as-ram pointer and underlying structure. */
81 car_set_var(cbmem_console_p
, cbm_cons_p
);
82 cbm_cons_p
->buffer_size
= total_space
- sizeof(struct cbmem_console
);
83 cbm_cons_p
->buffer_cursor
= 0;
86 void cbmemc_init(void)
89 init_console_ptr(&preram_cbmem_console
,
90 CONFIG_CONSOLE_PRERAM_BUFFER_SIZE
);
93 * Initializing before CBMEM is available, use static buffer to store
96 init_console_ptr(static_console
, sizeof(static_console
));
100 void cbmemc_tx_byte(unsigned char data
)
102 struct cbmem_console
*cbm_cons_p
= current_console();
108 cursor
= cbm_cons_p
->buffer_cursor
++;
109 if (cursor
< cbm_cons_p
->buffer_size
)
110 cbm_cons_p
->buffer_body
[cursor
] = data
;
114 * Copy the current console buffer (either from the cache as RAM area, or from
115 * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
116 * buffer space (pointed at by new_cons_p), concatenating the copied data with
117 * the CBMEM console buffer contents.
119 * If there is overflow - add to the destination area a string, reporting the
120 * overflow and the number of dropped characters.
122 static void copy_console_buffer(struct cbmem_console
*new_cons_p
)
124 u32 copy_size
, dropped_chars
;
125 u32 cursor
= new_cons_p
->buffer_cursor
;
126 struct cbmem_console
*old_cons_p
;
128 old_cons_p
= current_console();
130 if (old_cons_p
->buffer_cursor
< old_cons_p
->buffer_size
)
131 copy_size
= old_cons_p
->buffer_cursor
;
133 copy_size
= old_cons_p
->buffer_size
;
135 if (cursor
> new_cons_p
->buffer_size
)
137 else if (cursor
+ copy_size
> new_cons_p
->buffer_size
)
138 copy_size
= new_cons_p
->buffer_size
- cursor
;
140 dropped_chars
= old_cons_p
->buffer_cursor
- copy_size
;
142 /* Reserve 80 chars to report overflow, if possible. */
149 memcpy(new_cons_p
->buffer_body
+ cursor
, old_cons_p
->buffer_body
,
155 const char loss_str1
[] = "\n\n*** Log truncated, ";
156 const char loss_str2
[] = " characters dropped. ***\n\n";
159 * When running from ROM sprintf is not available, a simple
160 * itoa implementation is used instead.
162 int got_first_digit
= 0;
164 /* Way more than possible number of dropped characters. */
167 strcpy((char *)new_cons_p
->buffer_body
+ cursor
, loss_str1
);
168 cursor
+= sizeof(loss_str1
) - 1;
171 int digit
= dropped_chars
/ mult
;
172 if (got_first_digit
|| digit
) {
173 new_cons_p
->buffer_body
[cursor
++] = digit
+ '0';
174 dropped_chars
%= mult
;
175 /* Excessive, but keeps it simple */
181 strcpy((char *)new_cons_p
->buffer_body
+ cursor
, loss_str2
);
182 cursor
+= sizeof(loss_str2
) - 1;
184 new_cons_p
->buffer_cursor
= cursor
;
187 void cbmemc_reinit(void)
189 struct cbmem_console
*cbm_cons_p
= NULL
;
192 if (IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE
))
197 cbm_cons_p
= cbmem_find(CBMEM_ID_CONSOLE
);
201 cbm_cons_p
= cbmem_add(CBMEM_ID_CONSOLE
,
202 CONFIG_CONSOLE_CBMEM_BUFFER_SIZE
);
205 current_console_set(NULL
);
209 cbm_cons_p
->buffer_size
= CONFIG_CONSOLE_CBMEM_BUFFER_SIZE
-
210 sizeof(struct cbmem_console
);
212 cbm_cons_p
->buffer_cursor
= 0;
215 copy_console_buffer(cbm_cons_p
);
217 current_console_set(cbm_cons_p
);
220 /* Call cbmemc_reinit() at CAR migration time. */
221 CAR_MIGRATE(cbmemc_reinit
)