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 
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
57 #endif
58 
59 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
ram_console_encode_rs8(uint8_t * data,size_t len,uint8_t * ecc)60 static void ram_console_encode_rs8(uint8_t *data, size_t len, uint8_t *ecc)
61 {
62 	int i;
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++)
68 		ecc[i] = par[i];
69 }
70 
ram_console_decode_rs8(void * data,size_t len,uint8_t * ecc)71 static int ram_console_decode_rs8(void *data, size_t len, uint8_t *ecc)
72 {
73 	int i;
74 	uint16_t par[ECC_SIZE];
75 	for (i = 0; i < ECC_SIZE; i++)
76 		par[i] = ecc[i];
77 	return decode_rs8(ram_console_rs_decoder, data, par, len,
78 				NULL, 0, NULL, 0, NULL);
79 }
80 #endif
81 
ram_console_update(const char * s,unsigned int count)82 static void ram_console_update(const char *s, unsigned int count)
83 {
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;
87 	uint8_t *block;
88 	uint8_t *par;
89 	int size = ECC_BLOCK_SIZE;
90 #endif
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;
96 	do {
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;
101 		par += ECC_SIZE;
102 	} while (block < buffer->data + buffer->start + count);
103 #endif
104 }
105 
ram_console_update_header(void)106 static void ram_console_update_header(void)
107 {
108 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
109 	struct ram_console_buffer *buffer = ram_console_buffer;
110 	uint8_t *par;
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);
114 #endif
115 }
116 
117 static void
ram_console_write(struct console * console,const char * s,unsigned int count)118 ram_console_write(struct console *console, const char *s, unsigned int count)
119 {
120 	int rem;
121 	struct ram_console_buffer *buffer = ram_console_buffer;
122 
123 	if (count > ram_console_buffer_size) {
124 		s += count - ram_console_buffer_size;
125 		count = ram_console_buffer_size;
126 	}
127 	rem = ram_console_buffer_size - buffer->start;
128 	if (rem < count) {
129 		ram_console_update(s, rem);
130 		s += rem;
131 		count -= rem;
132 		buffer->start = 0;
133 		buffer->size = ram_console_buffer_size;
134 	}
135 	ram_console_update(s, count);
136 
137 	buffer->start += count;
138 	if (buffer->size < ram_console_buffer_size)
139 		buffer->size += count;
140 	ram_console_update_header();
141 }
142 
143 static struct console ram_console = {
144 	.name	= "ram",
145 	.write	= ram_console_write,
146 	.flags	= CON_PRINTBUFFER | CON_ENABLED,
147 	.index	= -1,
148 };
149 
ram_console_enable_console(int enabled)150 void ram_console_enable_console(int enabled)
151 {
152 	if (enabled)
153 		ram_console.flags |= CON_ENABLED;
154 	else
155 		ram_console.flags &= ~CON_ENABLED;
156 }
157 
158 static void __init
ram_console_save_old(struct ram_console_buffer * buffer,const char * bootinfo,char * dest)159 ram_console_save_old(struct ram_console_buffer *buffer, const char *bootinfo,
160 	char *dest)
161 {
162 	size_t old_log_size = buffer->size;
163 	size_t bootinfo_size = 0;
164 	size_t total_size = old_log_size;
165 	char *ptr;
166 	const char *bootinfo_label = "Boot info:\n";
167 
168 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
169 	uint8_t *block;
170 	uint8_t *par;
171 	char strbuf[80];
172 	int strbuf_len = 0;
173 
174 	block = buffer->data;
175 	par = ram_console_par_buffer;
176 	while (block < buffer->data + buffer->size) {
177 		int numerr;
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);
182 		if (numerr > 0) {
183 #if 0
184 			printk(KERN_INFO "ram_console: error in block %p, %d\n",
185 			       block, numerr);
186 #endif
187 			ram_console_corrected_bytes += numerr;
188 		} else if (numerr < 0) {
189 #if 0
190 			printk(KERN_INFO "ram_console: uncorrectable error in "
191 			       "block %p\n", block);
192 #endif
193 			ram_console_bad_blocks++;
194 		}
195 		block += ECC_BLOCK_SIZE;
196 		par += ECC_SIZE;
197 	}
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);
202 	else
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;
208 #endif
209 
210 	if (bootinfo)
211 		bootinfo_size = strlen(bootinfo) + strlen(bootinfo_label);
212 	total_size += bootinfo_size;
213 
214 	if (dest == NULL) {
215 		dest = kmalloc(total_size, GFP_KERNEL);
216 		if (dest == NULL) {
217 			printk(KERN_ERR
218 			       "ram_console: failed to allocate buffer\n");
219 			return;
220 		}
221 	}
222 
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);
232 	ptr += strbuf_len;
233 #endif
234 	if (bootinfo) {
235 		memcpy(ptr, bootinfo_label, strlen(bootinfo_label));
236 		ptr += strlen(bootinfo_label);
237 		memcpy(ptr, bootinfo, bootinfo_size);
238 		ptr += bootinfo_size;
239 	}
240 }
241 
ram_console_init(struct ram_console_buffer * buffer,size_t buffer_size,const char * bootinfo,char * old_buf)242 static int __init ram_console_init(struct ram_console_buffer *buffer,
243 				   size_t buffer_size, const char *bootinfo,
244 				   char *old_buf)
245 {
246 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
247 	int numerr;
248 	uint8_t *par;
249 #endif
250 	ram_console_buffer = buffer;
251 	ram_console_buffer_size =
252 		buffer_size - sizeof(struct ram_console_buffer);
253 
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);
258 		return 0;
259 	}
260 
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;
264 
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);
269 		return 0;
270 	}
271 
272 	ram_console_par_buffer = buffer->data + ram_console_buffer_size;
273 
274 
275 	/* first consecutive root is 0
276 	 * primitive element to generate roots = 1
277 	 */
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");
281 		return 0;
282 	}
283 
284 	ram_console_corrected_bytes = 0;
285 	ram_console_bad_blocks = 0;
286 
287 	par = ram_console_par_buffer +
288 	      DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
289 
290 	numerr = ram_console_decode_rs8(buffer, sizeof(*buffer), par);
291 	if (numerr > 0) {
292 		printk(KERN_INFO "ram_console: error in header, %d\n", numerr);
293 		ram_console_corrected_bytes += numerr;
294 	} else if (numerr < 0) {
295 		printk(KERN_INFO
296 		       "ram_console: uncorrectable error in header\n");
297 		ram_console_bad_blocks++;
298 	}
299 #endif
300 
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);
307 		else {
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);
312 		}
313 	} else {
314 		printk(KERN_INFO "ram_console: no valid data in buffer "
315 		       "(sig = 0x%08x)\n", buffer->sig);
316 	}
317 
318 	buffer->sig = RAM_CONSOLE_SIG;
319 	buffer->start = 0;
320 	buffer->size = 0;
321 
322 	register_console(&ram_console);
323 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE
324 	console_verbose();
325 #endif
326 	return 0;
327 }
328 
329 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
ram_console_early_init(void)330 static int __init ram_console_early_init(void)
331 {
332 	return ram_console_init((struct ram_console_buffer *)
333 		CONFIG_ANDROID_RAM_CONSOLE_EARLY_ADDR,
334 		CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE,
335 		NULL,
336 		ram_console_old_log_init_buffer);
337 }
338 #else
ram_console_driver_probe(struct platform_device * pdev)339 static int ram_console_driver_probe(struct platform_device *pdev)
340 {
341 	struct resource *res = pdev->resource;
342 	size_t start;
343 	size_t buffer_size;
344 	void *buffer;
345 	const char *bootinfo = NULL;
346 	struct ram_console_platform_data *pdata = pdev->dev.platform_data;
347 
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);
352 		return -ENXIO;
353 	}
354 	buffer_size = res->end - res->start + 1;
355 	start = res->start;
356 	printk(KERN_INFO "ram_console: got buffer at %zx, size %zx\n",
357 	       start, buffer_size);
358 	buffer = ioremap(res->start, buffer_size);
359 	if (buffer == NULL) {
360 		printk(KERN_ERR "ram_console: failed to map memory\n");
361 		return -ENOMEM;
362 	}
363 
364 	if (pdata)
365 		bootinfo = pdata->bootinfo;
366 
367 	return ram_console_init(buffer, buffer_size, bootinfo, NULL/* allocate */);
368 }
369 
370 static struct platform_driver ram_console_driver = {
371 	.probe = ram_console_driver_probe,
372 	.driver		= {
373 		.name	= "ram_console",
374 	},
375 };
376 
ram_console_module_init(void)377 static int __init ram_console_module_init(void)
378 {
379 	int err;
380 	err = platform_driver_register(&ram_console_driver);
381 	return err;
382 }
383 #endif
384 
ram_console_read_old(struct file * file,char __user * buf,size_t len,loff_t * offset)385 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
386 				    size_t len, loff_t *offset)
387 {
388 	loff_t pos = *offset;
389 	ssize_t count;
390 
391 	if (pos >= ram_console_old_log_size)
392 		return 0;
393 
394 	count = min(len, (size_t)(ram_console_old_log_size - pos));
395 	if (copy_to_user(buf, ram_console_old_log + pos, count))
396 		return -EFAULT;
397 
398 	*offset += count;
399 	return count;
400 }
401 
402 static const struct file_operations ram_console_file_ops = {
403 	.owner = THIS_MODULE,
404 	.read = ram_console_read_old,
405 };
406 
ram_console_late_init(void)407 static int __init ram_console_late_init(void)
408 {
409 	struct proc_dir_entry *entry;
410 
411 	if (ram_console_old_log == NULL)
412 		return 0;
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) {
416 		printk(KERN_ERR
417 		       "ram_console: failed to allocate buffer for old log\n");
418 		ram_console_old_log_size = 0;
419 		return 0;
420 	}
421 	memcpy(ram_console_old_log,
422 	       ram_console_old_log_init_buffer, ram_console_old_log_size);
423 #endif
424 	entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
425 	if (!entry) {
426 		printk(KERN_ERR "ram_console: failed to create proc entry\n");
427 		kfree(ram_console_old_log);
428 		ram_console_old_log = NULL;
429 		return 0;
430 	}
431 
432 	entry->proc_fops = &ram_console_file_ops;
433 	entry->size = ram_console_old_log_size;
434 	return 0;
435 }
436 
437 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
438 console_initcall(ram_console_early_init);
439 #else
440 postcore_initcall(ram_console_module_init);
441 #endif
442 late_initcall(ram_console_late_init);
443 
444