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;
45 static const char *bootinfo;
46 static size_t bootinfo_size;
48 static struct ram_console_buffer *ram_console_buffer;
49 static size_t ram_console_buffer_size;
50 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
51 static char *ram_console_par_buffer;
52 static struct rs_control *ram_console_rs_decoder;
53 static int ram_console_corrected_bytes;
54 static int ram_console_bad_blocks;
55 #define ECC_BLOCK_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_DATA_SIZE
56 #define ECC_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_ECC_SIZE
57 #define ECC_SYMSIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_SYMBOL_SIZE
58 #define ECC_POLY CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_POLYNOMIAL
61 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
62 static void ram_console_encode_rs8(uint8_t *data, size_t len, uint8_t *ecc)
65 uint16_t par[ECC_SIZE];
66 /* Initialize the parity buffer */
67 memset(par, 0, sizeof(par));
68 encode_rs8(ram_console_rs_decoder, data, len, par, 0);
69 for (i = 0; i < ECC_SIZE; i++)
73 static int ram_console_decode_rs8(void *data, size_t len, uint8_t *ecc)
76 uint16_t par[ECC_SIZE];
77 for (i = 0; i < ECC_SIZE; i++)
79 return decode_rs8(ram_console_rs_decoder, data, par, len,
80 NULL, 0, NULL, 0, NULL);
84 static void ram_console_update(const char *s, unsigned int count)
86 struct ram_console_buffer *buffer = ram_console_buffer;
87 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
88 uint8_t *buffer_end = buffer->data + ram_console_buffer_size;
91 int size = ECC_BLOCK_SIZE;
93 memcpy(buffer->data + buffer->start, s, count);
94 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
95 block = buffer->data + (buffer->start & ~(ECC_BLOCK_SIZE - 1));
96 par = ram_console_par_buffer +
97 (buffer->start / ECC_BLOCK_SIZE) * ECC_SIZE;
99 if (block + ECC_BLOCK_SIZE > buffer_end)
100 size = buffer_end - block;
101 ram_console_encode_rs8(block, size, par);
102 block += ECC_BLOCK_SIZE;
104 } while (block < buffer->data + buffer->start + count);
108 static void ram_console_update_header(void)
110 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
111 struct ram_console_buffer *buffer = ram_console_buffer;
113 par = ram_console_par_buffer +
114 DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
115 ram_console_encode_rs8((uint8_t *)buffer, sizeof(*buffer), par);
120 ram_console_write(struct console *console, const char *s, unsigned int count)
123 struct ram_console_buffer *buffer = ram_console_buffer;
125 if (count > ram_console_buffer_size) {
126 s += count - ram_console_buffer_size;
127 count = ram_console_buffer_size;
129 rem = ram_console_buffer_size - buffer->start;
131 ram_console_update(s, rem);
135 buffer->size = ram_console_buffer_size;
137 ram_console_update(s, count);
139 buffer->start += count;
140 if (buffer->size < ram_console_buffer_size)
141 buffer->size += count;
142 ram_console_update_header();
145 static struct console ram_console = {
147 .write = ram_console_write,
148 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
152 void ram_console_enable_console(int enabled)
155 ram_console.flags |= CON_ENABLED;
157 ram_console.flags &= ~CON_ENABLED;
161 ram_console_save_old(struct ram_console_buffer *buffer, char *dest)
163 size_t old_log_size = buffer->size;
165 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
169 block = buffer->data;
170 par = ram_console_par_buffer;
171 while (block < buffer->data + buffer->size) {
173 int size = ECC_BLOCK_SIZE;
174 if (block + size > buffer->data + ram_console_buffer_size)
175 size = buffer->data + ram_console_buffer_size - block;
176 numerr = ram_console_decode_rs8(block, size, par);
179 printk(KERN_INFO "ram_console: error in block %p, %d\n",
182 ram_console_corrected_bytes += numerr;
183 } else if (numerr < 0) {
185 printk(KERN_INFO "ram_console: uncorrectable error in "
186 "block %p\n", block);
188 ram_console_bad_blocks++;
190 block += ECC_BLOCK_SIZE;
196 dest = kmalloc(old_log_size, GFP_KERNEL);
199 "ram_console: failed to allocate buffer\n");
204 ram_console_old_log = dest;
205 ram_console_old_log_size = old_log_size;
206 memcpy(ram_console_old_log,
207 &buffer->data[buffer->start], buffer->size - buffer->start);
208 memcpy(ram_console_old_log + buffer->size - buffer->start,
209 &buffer->data[0], buffer->start);
212 static int __init ram_console_init(struct ram_console_buffer *buffer,
213 size_t buffer_size, char *old_buf)
215 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
219 ram_console_buffer = buffer;
220 ram_console_buffer_size =
221 buffer_size - sizeof(struct ram_console_buffer);
223 if (ram_console_buffer_size > buffer_size) {
224 pr_err("ram_console: buffer %p, invalid size %zu, "
225 "datasize %zu\n", buffer, buffer_size,
226 ram_console_buffer_size);
230 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
231 ram_console_buffer_size -= (DIV_ROUND_UP(ram_console_buffer_size,
232 ECC_BLOCK_SIZE) + 1) * ECC_SIZE;
234 if (ram_console_buffer_size > buffer_size) {
235 pr_err("ram_console: buffer %p, invalid size %zu, "
236 "non-ecc datasize %zu\n",
237 buffer, buffer_size, ram_console_buffer_size);
241 ram_console_par_buffer = buffer->data + ram_console_buffer_size;
244 /* first consecutive root is 0
245 * primitive element to generate roots = 1
247 ram_console_rs_decoder = init_rs(ECC_SYMSIZE, ECC_POLY, 0, 1, ECC_SIZE);
248 if (ram_console_rs_decoder == NULL) {
249 printk(KERN_INFO "ram_console: init_rs failed\n");
253 ram_console_corrected_bytes = 0;
254 ram_console_bad_blocks = 0;
256 par = ram_console_par_buffer +
257 DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
259 numerr = ram_console_decode_rs8(buffer, sizeof(*buffer), par);
261 printk(KERN_INFO "ram_console: error in header, %d\n", numerr);
262 ram_console_corrected_bytes += numerr;
263 } else if (numerr < 0) {
265 "ram_console: uncorrectable error in header\n");
266 ram_console_bad_blocks++;
270 if (buffer->sig == RAM_CONSOLE_SIG) {
271 if (buffer->size > ram_console_buffer_size
272 || buffer->start > buffer->size)
273 printk(KERN_INFO "ram_console: found existing invalid "
274 "buffer, size %d, start %d\n",
275 buffer->size, buffer->start);
277 printk(KERN_INFO "ram_console: found existing buffer, "
278 "size %d, start %d\n",
279 buffer->size, buffer->start);
280 ram_console_save_old(buffer, old_buf);
283 printk(KERN_INFO "ram_console: no valid data in buffer "
284 "(sig = 0x%08x)\n", buffer->sig);
287 buffer->sig = RAM_CONSOLE_SIG;
291 register_console(&ram_console);
292 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE
298 static ssize_t ram_console_ecc_string(char *str, size_t len)
300 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
303 if (ram_console_corrected_bytes || ram_console_bad_blocks)
304 ret = snprintf(str, len, ""
305 "\n%d Corrected bytes, %d unrecoverable blocks\n",
306 ram_console_corrected_bytes, ram_console_bad_blocks);
308 ret = snprintf(str, len, "\nNo errors detected\n");
316 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
317 static int __init ram_console_early_init(void)
319 return ram_console_init((struct ram_console_buffer *)
320 CONFIG_ANDROID_RAM_CONSOLE_EARLY_ADDR,
321 CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE,
323 ram_console_old_log_init_buffer);
326 static int ram_console_driver_probe(struct platform_device *pdev)
328 struct resource *res = pdev->resource;
332 struct ram_console_platform_data *pdata = pdev->dev.platform_data;
334 if (res == NULL || pdev->num_resources != 1 ||
335 !(res->flags & IORESOURCE_MEM)) {
336 printk(KERN_ERR "ram_console: invalid resource, %p %d flags "
337 "%lx\n", res, pdev->num_resources, res ? res->flags : 0);
340 buffer_size = resource_size(res);
342 printk(KERN_INFO "ram_console: got buffer at %zx, size %zx\n",
344 buffer = ioremap(res->start, buffer_size);
345 if (buffer == NULL) {
346 printk(KERN_ERR "ram_console: failed to map memory\n");
351 bootinfo = kstrdup(pdata->bootinfo, GFP_KERNEL);
353 bootinfo_size = strlen(bootinfo);
356 return ram_console_init(buffer, buffer_size, NULL/* allocate */);
359 static struct platform_driver ram_console_driver = {
360 .probe = ram_console_driver_probe,
362 .name = "ram_console",
366 static int __init ram_console_module_init(void)
369 err = platform_driver_register(&ram_console_driver);
374 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
375 size_t len, loff_t *offset)
377 loff_t pos = *offset;
382 /* Main last_kmsg log */
383 if (pos < ram_console_old_log_size) {
384 count = min(len, (size_t)(ram_console_old_log_size - pos));
385 if (copy_to_user(buf, ram_console_old_log + pos, count))
390 /* ECC correction notice */
391 pos -= ram_console_old_log_size;
392 count = ram_console_ecc_string(NULL, 0);
394 str = kmalloc(count, GFP_KERNEL);
397 ram_console_ecc_string(str, count + 1);
398 count = min(len, (size_t)(count - pos));
399 ret = copy_to_user(buf, str + pos, count);
406 /* Boot info passed through pdata */
408 if (pos < bootinfo_size) {
409 count = min(len, (size_t)(bootinfo_size - pos));
410 if (copy_to_user(buf, bootinfo + pos, count))
423 static const struct file_operations ram_console_file_ops = {
424 .owner = THIS_MODULE,
425 .read = ram_console_read_old,
428 static int __init ram_console_late_init(void)
430 struct proc_dir_entry *entry;
432 if (ram_console_old_log == NULL)
434 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
435 ram_console_old_log = kmemdup(ram_console_old_log_init_buffer,
436 ram_console_old_log_size, GFP_KERNEL);
437 if (ram_console_old_log == NULL) {
439 "ram_console: failed to allocate buffer for old log\n");
440 ram_console_old_log_size = 0;
444 entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
446 printk(KERN_ERR "ram_console: failed to create proc entry\n");
447 kfree(ram_console_old_log);
448 ram_console_old_log = NULL;
452 entry->proc_fops = &ram_console_file_ops;
453 entry->size = ram_console_old_log_size;
457 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
458 console_initcall(ram_console_early_init);
460 postcore_initcall(ram_console_module_init);
462 late_initcall(ram_console_late_init);