1 /* drivers/android/ram_console.c
3 * Copyright (C) 2007-2008 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/proc_fs.h>
21 #include <linux/string.h>
22 #include <linux/uaccess.h>
24 #include "ram_console.h"
26 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
27 #include <linux/rslib.h>
30 struct ram_console_buffer
{
37 #define RAM_CONSOLE_SIG (0x43474244) /* DBGC */
39 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
40 static char __initdata
41 ram_console_old_log_init_buffer
[CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE
];
43 static char *ram_console_old_log
;
44 static size_t ram_console_old_log_size
;
46 static struct ram_console_buffer
*ram_console_buffer
;
47 static size_t ram_console_buffer_size
;
48 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
49 static char *ram_console_par_buffer
;
50 static struct rs_control
*ram_console_rs_decoder
;
51 static int ram_console_corrected_bytes
;
52 static int ram_console_bad_blocks
;
53 #define ECC_BLOCK_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_DATA_SIZE
54 #define ECC_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_ECC_SIZE
55 #define ECC_SYMSIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_SYMBOL_SIZE
56 #define ECC_POLY CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_POLYNOMIAL
59 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
60 static void ram_console_encode_rs8(uint8_t *data
, size_t len
, uint8_t *ecc
)
63 uint16_t par
[ECC_SIZE
];
64 /* Initialize the parity buffer */
65 memset(par
, 0, sizeof(par
));
66 encode_rs8(ram_console_rs_decoder
, data
, len
, par
, 0);
67 for (i
= 0; i
< ECC_SIZE
; i
++)
71 static int ram_console_decode_rs8(void *data
, size_t len
, uint8_t *ecc
)
74 uint16_t par
[ECC_SIZE
];
75 for (i
= 0; i
< ECC_SIZE
; i
++)
77 return decode_rs8(ram_console_rs_decoder
, data
, par
, len
,
78 NULL
, 0, NULL
, 0, NULL
);
82 static void ram_console_update(const char *s
, unsigned int count
)
84 struct ram_console_buffer
*buffer
= ram_console_buffer
;
85 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
86 uint8_t *buffer_end
= buffer
->data
+ ram_console_buffer_size
;
89 int size
= ECC_BLOCK_SIZE
;
91 memcpy(buffer
->data
+ buffer
->start
, s
, count
);
92 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
93 block
= buffer
->data
+ (buffer
->start
& ~(ECC_BLOCK_SIZE
- 1));
94 par
= ram_console_par_buffer
+
95 (buffer
->start
/ ECC_BLOCK_SIZE
) * ECC_SIZE
;
97 if (block
+ ECC_BLOCK_SIZE
> buffer_end
)
98 size
= buffer_end
- block
;
99 ram_console_encode_rs8(block
, size
, par
);
100 block
+= ECC_BLOCK_SIZE
;
102 } while (block
< buffer
->data
+ buffer
->start
+ count
);
106 static void ram_console_update_header(void)
108 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
109 struct ram_console_buffer
*buffer
= ram_console_buffer
;
111 par
= ram_console_par_buffer
+
112 DIV_ROUND_UP(ram_console_buffer_size
, ECC_BLOCK_SIZE
) * ECC_SIZE
;
113 ram_console_encode_rs8((uint8_t *)buffer
, sizeof(*buffer
), par
);
118 ram_console_write(struct console
*console
, const char *s
, unsigned int count
)
121 struct ram_console_buffer
*buffer
= ram_console_buffer
;
123 if (count
> ram_console_buffer_size
) {
124 s
+= count
- ram_console_buffer_size
;
125 count
= ram_console_buffer_size
;
127 rem
= ram_console_buffer_size
- buffer
->start
;
129 ram_console_update(s
, rem
);
133 buffer
->size
= ram_console_buffer_size
;
135 ram_console_update(s
, count
);
137 buffer
->start
+= count
;
138 if (buffer
->size
< ram_console_buffer_size
)
139 buffer
->size
+= count
;
140 ram_console_update_header();
143 static struct console ram_console
= {
145 .write
= ram_console_write
,
146 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
150 void ram_console_enable_console(int enabled
)
153 ram_console
.flags
|= CON_ENABLED
;
155 ram_console
.flags
&= ~CON_ENABLED
;
159 ram_console_save_old(struct ram_console_buffer
*buffer
, const char *bootinfo
,
162 size_t old_log_size
= buffer
->size
;
163 size_t bootinfo_size
= 0;
164 size_t total_size
= old_log_size
;
166 const char *bootinfo_label
= "Boot info:\n";
168 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
174 block
= buffer
->data
;
175 par
= ram_console_par_buffer
;
176 while (block
< buffer
->data
+ buffer
->size
) {
178 int size
= ECC_BLOCK_SIZE
;
179 if (block
+ size
> buffer
->data
+ ram_console_buffer_size
)
180 size
= buffer
->data
+ ram_console_buffer_size
- block
;
181 numerr
= ram_console_decode_rs8(block
, size
, par
);
184 printk(KERN_INFO
"ram_console: error in block %p, %d\n",
187 ram_console_corrected_bytes
+= numerr
;
188 } else if (numerr
< 0) {
190 printk(KERN_INFO
"ram_console: uncorrectable error in "
191 "block %p\n", block
);
193 ram_console_bad_blocks
++;
195 block
+= ECC_BLOCK_SIZE
;
198 if (ram_console_corrected_bytes
|| ram_console_bad_blocks
)
199 strbuf_len
= snprintf(strbuf
, sizeof(strbuf
),
200 "\n%d Corrected bytes, %d unrecoverable blocks\n",
201 ram_console_corrected_bytes
, ram_console_bad_blocks
);
203 strbuf_len
= snprintf(strbuf
, sizeof(strbuf
),
204 "\nNo errors detected\n");
205 if (strbuf_len
>= sizeof(strbuf
))
206 strbuf_len
= sizeof(strbuf
) - 1;
207 total_size
+= strbuf_len
;
211 bootinfo_size
= strlen(bootinfo
) + strlen(bootinfo_label
);
212 total_size
+= bootinfo_size
;
215 dest
= kmalloc(total_size
, GFP_KERNEL
);
218 "ram_console: failed to allocate buffer\n");
223 ram_console_old_log
= dest
;
224 ram_console_old_log_size
= total_size
;
225 memcpy(ram_console_old_log
,
226 &buffer
->data
[buffer
->start
], buffer
->size
- buffer
->start
);
227 memcpy(ram_console_old_log
+ buffer
->size
- buffer
->start
,
228 &buffer
->data
[0], buffer
->start
);
229 ptr
= ram_console_old_log
+ old_log_size
;
230 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
231 memcpy(ptr
, strbuf
, strbuf_len
);
235 memcpy(ptr
, bootinfo_label
, strlen(bootinfo_label
));
236 ptr
+= strlen(bootinfo_label
);
237 memcpy(ptr
, bootinfo
, bootinfo_size
);
238 ptr
+= bootinfo_size
;
242 static int __init
ram_console_init(struct ram_console_buffer
*buffer
,
243 size_t buffer_size
, const char *bootinfo
,
246 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
250 ram_console_buffer
= buffer
;
251 ram_console_buffer_size
=
252 buffer_size
- sizeof(struct ram_console_buffer
);
254 if (ram_console_buffer_size
> buffer_size
) {
255 pr_err("ram_console: buffer %p, invalid size %zu, "
256 "datasize %zu\n", buffer
, buffer_size
,
257 ram_console_buffer_size
);
261 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
262 ram_console_buffer_size
-= (DIV_ROUND_UP(ram_console_buffer_size
,
263 ECC_BLOCK_SIZE
) + 1) * ECC_SIZE
;
265 if (ram_console_buffer_size
> buffer_size
) {
266 pr_err("ram_console: buffer %p, invalid size %zu, "
267 "non-ecc datasize %zu\n",
268 buffer
, buffer_size
, ram_console_buffer_size
);
272 ram_console_par_buffer
= buffer
->data
+ ram_console_buffer_size
;
275 /* first consecutive root is 0
276 * primitive element to generate roots = 1
278 ram_console_rs_decoder
= init_rs(ECC_SYMSIZE
, ECC_POLY
, 0, 1, ECC_SIZE
);
279 if (ram_console_rs_decoder
== NULL
) {
280 printk(KERN_INFO
"ram_console: init_rs failed\n");
284 ram_console_corrected_bytes
= 0;
285 ram_console_bad_blocks
= 0;
287 par
= ram_console_par_buffer
+
288 DIV_ROUND_UP(ram_console_buffer_size
, ECC_BLOCK_SIZE
) * ECC_SIZE
;
290 numerr
= ram_console_decode_rs8(buffer
, sizeof(*buffer
), par
);
292 printk(KERN_INFO
"ram_console: error in header, %d\n", numerr
);
293 ram_console_corrected_bytes
+= numerr
;
294 } else if (numerr
< 0) {
296 "ram_console: uncorrectable error in header\n");
297 ram_console_bad_blocks
++;
301 if (buffer
->sig
== RAM_CONSOLE_SIG
) {
302 if (buffer
->size
> ram_console_buffer_size
303 || buffer
->start
> buffer
->size
)
304 printk(KERN_INFO
"ram_console: found existing invalid "
305 "buffer, size %d, start %d\n",
306 buffer
->size
, buffer
->start
);
308 printk(KERN_INFO
"ram_console: found existing buffer, "
309 "size %d, start %d\n",
310 buffer
->size
, buffer
->start
);
311 ram_console_save_old(buffer
, bootinfo
, old_buf
);
314 printk(KERN_INFO
"ram_console: no valid data in buffer "
315 "(sig = 0x%08x)\n", buffer
->sig
);
318 buffer
->sig
= RAM_CONSOLE_SIG
;
322 register_console(&ram_console
);
323 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE
329 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
330 static int __init
ram_console_early_init(void)
332 return ram_console_init((struct ram_console_buffer
*)
333 CONFIG_ANDROID_RAM_CONSOLE_EARLY_ADDR
,
334 CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE
,
336 ram_console_old_log_init_buffer
);
339 static int ram_console_driver_probe(struct platform_device
*pdev
)
341 struct resource
*res
= pdev
->resource
;
345 const char *bootinfo
= NULL
;
346 struct ram_console_platform_data
*pdata
= pdev
->dev
.platform_data
;
348 if (res
== NULL
|| pdev
->num_resources
!= 1 ||
349 !(res
->flags
& IORESOURCE_MEM
)) {
350 printk(KERN_ERR
"ram_console: invalid resource, %p %d flags "
351 "%lx\n", res
, pdev
->num_resources
, res
? res
->flags
: 0);
354 buffer_size
= res
->end
- res
->start
+ 1;
356 printk(KERN_INFO
"ram_console: got buffer at %zx, size %zx\n",
358 buffer
= ioremap(res
->start
, buffer_size
);
359 if (buffer
== NULL
) {
360 printk(KERN_ERR
"ram_console: failed to map memory\n");
365 bootinfo
= pdata
->bootinfo
;
367 return ram_console_init(buffer
, buffer_size
, bootinfo
, NULL
/* allocate */);
370 static struct platform_driver ram_console_driver
= {
371 .probe
= ram_console_driver_probe
,
373 .name
= "ram_console",
377 static int __init
ram_console_module_init(void)
380 err
= platform_driver_register(&ram_console_driver
);
385 static ssize_t
ram_console_read_old(struct file
*file
, char __user
*buf
,
386 size_t len
, loff_t
*offset
)
388 loff_t pos
= *offset
;
391 if (pos
>= ram_console_old_log_size
)
394 count
= min(len
, (size_t)(ram_console_old_log_size
- pos
));
395 if (copy_to_user(buf
, ram_console_old_log
+ pos
, count
))
402 static const struct file_operations ram_console_file_ops
= {
403 .owner
= THIS_MODULE
,
404 .read
= ram_console_read_old
,
407 static int __init
ram_console_late_init(void)
409 struct proc_dir_entry
*entry
;
411 if (ram_console_old_log
== NULL
)
413 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
414 ram_console_old_log
= kmalloc(ram_console_old_log_size
, GFP_KERNEL
);
415 if (ram_console_old_log
== NULL
) {
417 "ram_console: failed to allocate buffer for old log\n");
418 ram_console_old_log_size
= 0;
421 memcpy(ram_console_old_log
,
422 ram_console_old_log_init_buffer
, ram_console_old_log_size
);
424 entry
= create_proc_entry("last_kmsg", S_IFREG
| S_IRUGO
, NULL
);
426 printk(KERN_ERR
"ram_console: failed to create proc entry\n");
427 kfree(ram_console_old_log
);
428 ram_console_old_log
= NULL
;
432 entry
->proc_fops
= &ram_console_file_ops
;
433 entry
->size
= ram_console_old_log_size
;
437 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
438 console_initcall(ram_console_early_init
);
440 postcore_initcall(ram_console_module_init
);
442 late_initcall(ram_console_late_init
);