staging: android: ram_console: move footer strings
[pandora-kernel.git] / drivers / staging / android / ram_console.c
1 /* drivers/android/ram_console.c
2  *
3  * Copyright (C) 2007-2008 Google, Inc.
4  *
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.
8  *
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.
13  *
14  */
15
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>
23 #include <linux/io.h>
24 #include "ram_console.h"
25
26 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
27 #include <linux/rslib.h>
28 #endif
29
30 struct ram_console_buffer {
31         uint32_t    sig;
32         uint32_t    start;
33         uint32_t    size;
34         uint8_t     data[0];
35 };
36
37 #define RAM_CONSOLE_SIG (0x43474244) /* DBGC */
38
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];
42 #endif
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;
47
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
59 #endif
60
61 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
62 static void ram_console_encode_rs8(uint8_t *data, size_t len, uint8_t *ecc)
63 {
64         int i;
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++)
70                 ecc[i] = par[i];
71 }
72
73 static int ram_console_decode_rs8(void *data, size_t len, uint8_t *ecc)
74 {
75         int i;
76         uint16_t par[ECC_SIZE];
77         for (i = 0; i < ECC_SIZE; i++)
78                 par[i] = ecc[i];
79         return decode_rs8(ram_console_rs_decoder, data, par, len,
80                                 NULL, 0, NULL, 0, NULL);
81 }
82 #endif
83
84 static void ram_console_update(const char *s, unsigned int count)
85 {
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;
89         uint8_t *block;
90         uint8_t *par;
91         int size = ECC_BLOCK_SIZE;
92 #endif
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;
98         do {
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;
103                 par += ECC_SIZE;
104         } while (block < buffer->data + buffer->start + count);
105 #endif
106 }
107
108 static void ram_console_update_header(void)
109 {
110 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
111         struct ram_console_buffer *buffer = ram_console_buffer;
112         uint8_t *par;
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);
116 #endif
117 }
118
119 static void
120 ram_console_write(struct console *console, const char *s, unsigned int count)
121 {
122         int rem;
123         struct ram_console_buffer *buffer = ram_console_buffer;
124
125         if (count > ram_console_buffer_size) {
126                 s += count - ram_console_buffer_size;
127                 count = ram_console_buffer_size;
128         }
129         rem = ram_console_buffer_size - buffer->start;
130         if (rem < count) {
131                 ram_console_update(s, rem);
132                 s += rem;
133                 count -= rem;
134                 buffer->start = 0;
135                 buffer->size = ram_console_buffer_size;
136         }
137         ram_console_update(s, count);
138
139         buffer->start += count;
140         if (buffer->size < ram_console_buffer_size)
141                 buffer->size += count;
142         ram_console_update_header();
143 }
144
145 static struct console ram_console = {
146         .name   = "ram",
147         .write  = ram_console_write,
148         .flags  = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
149         .index  = -1,
150 };
151
152 void ram_console_enable_console(int enabled)
153 {
154         if (enabled)
155                 ram_console.flags |= CON_ENABLED;
156         else
157                 ram_console.flags &= ~CON_ENABLED;
158 }
159
160 static void __init
161 ram_console_save_old(struct ram_console_buffer *buffer, char *dest)
162 {
163         size_t old_log_size = buffer->size;
164
165 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
166         uint8_t *block;
167         uint8_t *par;
168
169         block = buffer->data;
170         par = ram_console_par_buffer;
171         while (block < buffer->data + buffer->size) {
172                 int numerr;
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);
177                 if (numerr > 0) {
178 #if 0
179                         printk(KERN_INFO "ram_console: error in block %p, %d\n",
180                                block, numerr);
181 #endif
182                         ram_console_corrected_bytes += numerr;
183                 } else if (numerr < 0) {
184 #if 0
185                         printk(KERN_INFO "ram_console: uncorrectable error in "
186                                "block %p\n", block);
187 #endif
188                         ram_console_bad_blocks++;
189                 }
190                 block += ECC_BLOCK_SIZE;
191                 par += ECC_SIZE;
192         }
193 #endif
194
195         if (dest == NULL) {
196                 dest = kmalloc(old_log_size, GFP_KERNEL);
197                 if (dest == NULL) {
198                         printk(KERN_ERR
199                                "ram_console: failed to allocate buffer\n");
200                         return;
201                 }
202         }
203
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);
210 }
211
212 static int __init ram_console_init(struct ram_console_buffer *buffer,
213                                    size_t buffer_size, char *old_buf)
214 {
215 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
216         int numerr;
217         uint8_t *par;
218 #endif
219         ram_console_buffer = buffer;
220         ram_console_buffer_size =
221                 buffer_size - sizeof(struct ram_console_buffer);
222
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);
227                 return 0;
228         }
229
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;
233
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);
238                 return 0;
239         }
240
241         ram_console_par_buffer = buffer->data + ram_console_buffer_size;
242
243
244         /* first consecutive root is 0
245          * primitive element to generate roots = 1
246          */
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");
250                 return 0;
251         }
252
253         ram_console_corrected_bytes = 0;
254         ram_console_bad_blocks = 0;
255
256         par = ram_console_par_buffer +
257               DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
258
259         numerr = ram_console_decode_rs8(buffer, sizeof(*buffer), par);
260         if (numerr > 0) {
261                 printk(KERN_INFO "ram_console: error in header, %d\n", numerr);
262                 ram_console_corrected_bytes += numerr;
263         } else if (numerr < 0) {
264                 printk(KERN_INFO
265                        "ram_console: uncorrectable error in header\n");
266                 ram_console_bad_blocks++;
267         }
268 #endif
269
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);
276                 else {
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);
281                 }
282         } else {
283                 printk(KERN_INFO "ram_console: no valid data in buffer "
284                        "(sig = 0x%08x)\n", buffer->sig);
285         }
286
287         buffer->sig = RAM_CONSOLE_SIG;
288         buffer->start = 0;
289         buffer->size = 0;
290
291         register_console(&ram_console);
292 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE
293         console_verbose();
294 #endif
295         return 0;
296 }
297
298 static ssize_t ram_console_ecc_string(char *str, size_t len)
299 {
300 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
301         ssize_t ret;
302
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);
307         else
308                 ret = snprintf(str, len, "\nNo errors detected\n");
309
310         return ret;
311 #else
312         return 0;
313 #endif
314 }
315
316 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
317 static int __init ram_console_early_init(void)
318 {
319         return ram_console_init((struct ram_console_buffer *)
320                 CONFIG_ANDROID_RAM_CONSOLE_EARLY_ADDR,
321                 CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE,
322                 NULL,
323                 ram_console_old_log_init_buffer);
324 }
325 #else
326 static int ram_console_driver_probe(struct platform_device *pdev)
327 {
328         struct resource *res = pdev->resource;
329         size_t start;
330         size_t buffer_size;
331         void *buffer;
332         struct ram_console_platform_data *pdata = pdev->dev.platform_data;
333
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);
338                 return -ENXIO;
339         }
340         buffer_size = resource_size(res);
341         start = res->start;
342         printk(KERN_INFO "ram_console: got buffer at %zx, size %zx\n",
343                start, buffer_size);
344         buffer = ioremap(res->start, buffer_size);
345         if (buffer == NULL) {
346                 printk(KERN_ERR "ram_console: failed to map memory\n");
347                 return -ENOMEM;
348         }
349
350         if (pdata) {
351                 bootinfo = kstrdup(pdata->bootinfo, GFP_KERNEL);
352                 if (bootinfo)
353                         bootinfo_size = strlen(bootinfo);
354         }
355
356         return ram_console_init(buffer, buffer_size, NULL/* allocate */);
357 }
358
359 static struct platform_driver ram_console_driver = {
360         .probe = ram_console_driver_probe,
361         .driver         = {
362                 .name   = "ram_console",
363         },
364 };
365
366 static int __init ram_console_module_init(void)
367 {
368         int err;
369         err = platform_driver_register(&ram_console_driver);
370         return err;
371 }
372 #endif
373
374 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
375                                     size_t len, loff_t *offset)
376 {
377         loff_t pos = *offset;
378         ssize_t count;
379         char *str;
380         int ret;
381
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))
386                         return -EFAULT;
387                 goto out;
388         }
389
390         /* ECC correction notice */
391         pos -= ram_console_old_log_size;
392         count = ram_console_ecc_string(NULL, 0);
393         if (pos < count) {
394                 str = kmalloc(count, GFP_KERNEL);
395                 if (!str)
396                         return -ENOMEM;
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);
400                 kfree(str);
401                 if (ret)
402                         return -EFAULT;
403                 goto out;
404         }
405
406         /* Boot info passed through pdata */
407         pos -= count;
408         if (pos < bootinfo_size) {
409                 count = min(len, (size_t)(bootinfo_size - pos));
410                 if (copy_to_user(buf, bootinfo + pos, count))
411                         return -EFAULT;
412                 goto out;
413         }
414
415         /* EOF */
416         return 0;
417
418 out:
419         *offset += count;
420         return count;
421 }
422
423 static const struct file_operations ram_console_file_ops = {
424         .owner = THIS_MODULE,
425         .read = ram_console_read_old,
426 };
427
428 static int __init ram_console_late_init(void)
429 {
430         struct proc_dir_entry *entry;
431
432         if (ram_console_old_log == NULL)
433                 return 0;
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) {
438                 printk(KERN_ERR
439                        "ram_console: failed to allocate buffer for old log\n");
440                 ram_console_old_log_size = 0;
441                 return 0;
442         }
443 #endif
444         entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
445         if (!entry) {
446                 printk(KERN_ERR "ram_console: failed to create proc entry\n");
447                 kfree(ram_console_old_log);
448                 ram_console_old_log = NULL;
449                 return 0;
450         }
451
452         entry->proc_fops = &ram_console_file_ops;
453         entry->size = ram_console_old_log_size;
454         return 0;
455 }
456
457 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
458 console_initcall(ram_console_early_init);
459 #else
460 postcore_initcall(ram_console_module_init);
461 #endif
462 late_initcall(ram_console_late_init);
463