| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 1 | /* drivers/misc/apanic.c | 
|  | 2 | * | 
|  | 3 | * Copyright (C) 2009 Google, Inc. | 
|  | 4 | * Author: San Mehat <san@android.com> | 
|  | 5 | * | 
|  | 6 | * This software is licensed under the terms of the GNU General Public | 
|  | 7 | * License version 2, as published by the Free Software Foundation, and | 
|  | 8 | * may be copied, distributed, and modified under those terms. | 
|  | 9 | * | 
|  | 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. | 
|  | 14 | * | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <linux/module.h> | 
|  | 18 | #include <linux/kernel.h> | 
|  | 19 | #include <linux/string.h> | 
|  | 20 | #include <linux/errno.h> | 
|  | 21 | #include <linux/init.h> | 
|  | 22 | #include <linux/interrupt.h> | 
|  | 23 | #include <linux/device.h> | 
|  | 24 | #include <linux/types.h> | 
|  | 25 | #include <linux/delay.h> | 
|  | 26 | #include <linux/sched.h> | 
|  | 27 | #include <linux/wait.h> | 
|  | 28 | #include <linux/wakelock.h> | 
|  | 29 | #include <linux/platform_device.h> | 
|  | 30 | #include <linux/uaccess.h> | 
|  | 31 | #include <linux/mtd/mtd.h> | 
|  | 32 | #include <linux/notifier.h> | 
|  | 33 | #include <linux/mtd/mtd.h> | 
|  | 34 | #include <linux/debugfs.h> | 
|  | 35 | #include <linux/fs.h> | 
|  | 36 | #include <linux/proc_fs.h> | 
|  | 37 | #include <linux/mutex.h> | 
|  | 38 | #include <linux/workqueue.h> | 
|  | 39 | #include <linux/preempt.h> | 
|  | 40 |  | 
| San Mehat | e3af8d4 | 2009-09-17 14:27:41 -0700 | [diff] [blame] | 41 | extern void ram_console_enable_console(int); | 
|  | 42 |  | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 43 | struct panic_header { | 
|  | 44 | u32 magic; | 
|  | 45 | #define PANIC_MAGIC 0xdeadf00d | 
|  | 46 |  | 
|  | 47 | u32 version; | 
|  | 48 | #define PHDR_VERSION   0x01 | 
|  | 49 |  | 
|  | 50 | u32 console_offset; | 
|  | 51 | u32 console_length; | 
|  | 52 |  | 
|  | 53 | u32 threads_offset; | 
|  | 54 | u32 threads_length; | 
|  | 55 | }; | 
|  | 56 |  | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 57 | struct apanic_data { | 
|  | 58 | struct mtd_info		*mtd; | 
|  | 59 | struct panic_header	curr; | 
|  | 60 | void			*bounce; | 
|  | 61 | struct proc_dir_entry	*apanic_console; | 
|  | 62 | struct proc_dir_entry	*apanic_threads; | 
|  | 63 | }; | 
|  | 64 |  | 
|  | 65 | static struct apanic_data drv_ctx; | 
|  | 66 | static struct work_struct proc_removal_work; | 
|  | 67 | static DEFINE_MUTEX(drv_mutex); | 
|  | 68 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 69 | static unsigned int *apanic_bbt; | 
|  | 70 | static unsigned int apanic_erase_blocks; | 
|  | 71 | static unsigned int apanic_good_blocks; | 
|  | 72 |  | 
|  | 73 | static void set_bb(unsigned int block, unsigned int *bbt) | 
|  | 74 | { | 
|  | 75 | unsigned int flag = 1; | 
|  | 76 |  | 
|  | 77 | BUG_ON(block >= apanic_erase_blocks); | 
|  | 78 |  | 
|  | 79 | flag = flag << (block%32); | 
|  | 80 | apanic_bbt[block/32] |= flag; | 
|  | 81 | apanic_good_blocks--; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static unsigned int get_bb(unsigned int block, unsigned int *bbt) | 
|  | 85 | { | 
|  | 86 | unsigned int flag; | 
|  | 87 |  | 
|  | 88 | BUG_ON(block >= apanic_erase_blocks); | 
|  | 89 |  | 
|  | 90 | flag = 1 << (block%32); | 
|  | 91 | return apanic_bbt[block/32] & flag; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | static void alloc_bbt(struct mtd_info *mtd, unsigned int *bbt) | 
|  | 95 | { | 
|  | 96 | int bbt_size; | 
|  | 97 | apanic_erase_blocks = (mtd->size)>>(mtd->erasesize_shift); | 
|  | 98 | bbt_size = (apanic_erase_blocks+32)/32; | 
|  | 99 |  | 
|  | 100 | apanic_bbt = kmalloc(bbt_size*4, GFP_KERNEL); | 
|  | 101 | memset(apanic_bbt, 0, bbt_size*4); | 
|  | 102 | apanic_good_blocks = apanic_erase_blocks; | 
|  | 103 | } | 
|  | 104 | static void scan_bbt(struct mtd_info *mtd, unsigned int *bbt) | 
|  | 105 | { | 
|  | 106 | int i; | 
|  | 107 |  | 
|  | 108 | for (i = 0; i < apanic_erase_blocks; i++) { | 
|  | 109 | if (mtd->block_isbad(mtd, i*mtd->erasesize)) | 
|  | 110 | set_bb(i, apanic_bbt); | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | #define APANIC_INVALID_OFFSET 0xFFFFFFFF | 
|  | 115 |  | 
|  | 116 | static unsigned int phy_offset(struct mtd_info *mtd, unsigned int offset) | 
|  | 117 | { | 
|  | 118 | unsigned int logic_block = offset>>(mtd->erasesize_shift); | 
|  | 119 | unsigned int phy_block; | 
|  | 120 | unsigned good_block = 0; | 
|  | 121 |  | 
|  | 122 | for (phy_block = 0; phy_block < apanic_erase_blocks; phy_block++) { | 
|  | 123 | if (!get_bb(phy_block, apanic_bbt)) | 
|  | 124 | good_block++; | 
|  | 125 | if (good_block == (logic_block + 1)) | 
|  | 126 | break; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | if (good_block != (logic_block + 1)) | 
|  | 130 | return APANIC_INVALID_OFFSET; | 
|  | 131 |  | 
|  | 132 | return offset + ((phy_block-logic_block)<<mtd->erasesize_shift); | 
|  | 133 | } | 
|  | 134 |  | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 135 | static void apanic_erase_callback(struct erase_info *done) | 
|  | 136 | { | 
|  | 137 | wait_queue_head_t *wait_q = (wait_queue_head_t *) done->priv; | 
|  | 138 | wake_up(wait_q); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | static int apanic_proc_read(char *buffer, char **start, off_t offset, | 
|  | 142 | int count, int *peof, void *dat) | 
|  | 143 | { | 
|  | 144 | struct apanic_data *ctx = &drv_ctx; | 
|  | 145 | size_t file_length; | 
|  | 146 | off_t file_offset; | 
|  | 147 | unsigned int page_no; | 
|  | 148 | off_t page_offset; | 
|  | 149 | int rc; | 
|  | 150 | size_t len; | 
|  | 151 |  | 
|  | 152 | if (!count) | 
|  | 153 | return 0; | 
|  | 154 |  | 
|  | 155 | mutex_lock(&drv_mutex); | 
|  | 156 |  | 
|  | 157 | switch ((int) dat) { | 
|  | 158 | case 1:	/* apanic_console */ | 
|  | 159 | file_length = ctx->curr.console_length; | 
|  | 160 | file_offset = ctx->curr.console_offset; | 
|  | 161 | break; | 
|  | 162 | case 2:	/* apanic_threads */ | 
|  | 163 | file_length = ctx->curr.threads_length; | 
|  | 164 | file_offset = ctx->curr.threads_offset; | 
|  | 165 | break; | 
|  | 166 | default: | 
|  | 167 | pr_err("Bad dat (%d)\n", (int) dat); | 
|  | 168 | mutex_unlock(&drv_mutex); | 
|  | 169 | return -EINVAL; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | if ((offset + count) > file_length) { | 
|  | 173 | mutex_unlock(&drv_mutex); | 
|  | 174 | return 0; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | /* We only support reading a maximum of a flash page */ | 
|  | 178 | if (count > ctx->mtd->writesize) | 
|  | 179 | count = ctx->mtd->writesize; | 
|  | 180 |  | 
|  | 181 | page_no = (file_offset + offset) / ctx->mtd->writesize; | 
|  | 182 | page_offset = (file_offset + offset) % ctx->mtd->writesize; | 
|  | 183 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 184 |  | 
|  | 185 | if (phy_offset(ctx->mtd, (page_no * ctx->mtd->writesize)) | 
|  | 186 | == APANIC_INVALID_OFFSET) { | 
|  | 187 | pr_err("apanic: reading an invalid address\n"); | 
|  | 188 | mutex_unlock(&drv_mutex); | 
|  | 189 | return -EINVAL; | 
|  | 190 | } | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 191 | rc = ctx->mtd->read(ctx->mtd, | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 192 | phy_offset(ctx->mtd, (page_no * ctx->mtd->writesize)), | 
|  | 193 | ctx->mtd->writesize, | 
|  | 194 | &len, ctx->bounce); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 195 |  | 
|  | 196 | if (page_offset) | 
|  | 197 | count -= page_offset; | 
|  | 198 | memcpy(buffer, ctx->bounce + page_offset, count); | 
|  | 199 |  | 
|  | 200 | *start = count; | 
|  | 201 |  | 
|  | 202 | if ((offset + count) == file_length) | 
|  | 203 | *peof = 1; | 
|  | 204 |  | 
|  | 205 | mutex_unlock(&drv_mutex); | 
|  | 206 | return count; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | static void mtd_panic_erase(void) | 
|  | 210 | { | 
|  | 211 | struct apanic_data *ctx = &drv_ctx; | 
|  | 212 | struct erase_info erase; | 
|  | 213 | DECLARE_WAITQUEUE(wait, current); | 
|  | 214 | wait_queue_head_t wait_q; | 
|  | 215 | int rc, i; | 
|  | 216 |  | 
|  | 217 | init_waitqueue_head(&wait_q); | 
|  | 218 | erase.mtd = ctx->mtd; | 
|  | 219 | erase.callback = apanic_erase_callback; | 
|  | 220 | erase.len = ctx->mtd->erasesize; | 
|  | 221 | erase.priv = (u_long)&wait_q; | 
|  | 222 | for (i = 0; i < ctx->mtd->size; i += ctx->mtd->erasesize) { | 
|  | 223 | erase.addr = i; | 
|  | 224 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 225 | add_wait_queue(&wait_q, &wait); | 
|  | 226 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 227 | if (get_bb(erase.addr>>ctx->mtd->erasesize_shift, apanic_bbt)) { | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 228 | printk(KERN_WARNING | 
|  | 229 | "apanic: Skipping erase of bad " | 
|  | 230 | "block @%llx\n", erase.addr); | 
|  | 231 | set_current_state(TASK_RUNNING); | 
|  | 232 | remove_wait_queue(&wait_q, &wait); | 
|  | 233 | continue; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | rc = ctx->mtd->erase(ctx->mtd, &erase); | 
|  | 237 | if (rc) { | 
|  | 238 | set_current_state(TASK_RUNNING); | 
|  | 239 | remove_wait_queue(&wait_q, &wait); | 
|  | 240 | printk(KERN_ERR | 
|  | 241 | "apanic: Erase of 0x%llx, 0x%llx failed\n", | 
|  | 242 | (unsigned long long) erase.addr, | 
|  | 243 | (unsigned long long) erase.len); | 
|  | 244 | if (rc == -EIO) { | 
|  | 245 | if (ctx->mtd->block_markbad(ctx->mtd, | 
|  | 246 | erase.addr)) { | 
|  | 247 | printk(KERN_ERR | 
|  | 248 | "apanic: Err marking blk bad\n"); | 
|  | 249 | goto out; | 
|  | 250 | } | 
|  | 251 | printk(KERN_INFO | 
|  | 252 | "apanic: Marked a bad block" | 
|  | 253 | " @%llx\n", erase.addr); | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 254 | set_bb(erase.addr>>ctx->mtd->erasesize_shift, | 
|  | 255 | apanic_bbt); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 256 | continue; | 
|  | 257 | } | 
|  | 258 | goto out; | 
|  | 259 | } | 
|  | 260 | schedule(); | 
|  | 261 | remove_wait_queue(&wait_q, &wait); | 
|  | 262 | } | 
|  | 263 | printk(KERN_DEBUG "apanic: %s partition erased\n", | 
|  | 264 | CONFIG_APANIC_PLABEL); | 
|  | 265 | out: | 
|  | 266 | return; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | static void apanic_remove_proc_work(struct work_struct *work) | 
|  | 270 | { | 
|  | 271 | struct apanic_data *ctx = &drv_ctx; | 
|  | 272 |  | 
|  | 273 | mutex_lock(&drv_mutex); | 
|  | 274 | mtd_panic_erase(); | 
|  | 275 | memset(&ctx->curr, 0, sizeof(struct panic_header)); | 
|  | 276 | if (ctx->apanic_console) { | 
|  | 277 | remove_proc_entry("apanic_console", NULL); | 
|  | 278 | ctx->apanic_console = NULL; | 
|  | 279 | } | 
|  | 280 | if (ctx->apanic_threads) { | 
|  | 281 | remove_proc_entry("apanic_threads", NULL); | 
|  | 282 | ctx->apanic_threads = NULL; | 
|  | 283 | } | 
|  | 284 | mutex_unlock(&drv_mutex); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | static int apanic_proc_write(struct file *file, const char __user *buffer, | 
|  | 288 | unsigned long count, void *data) | 
|  | 289 | { | 
|  | 290 | schedule_work(&proc_removal_work); | 
|  | 291 | return count; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | static void mtd_panic_notify_add(struct mtd_info *mtd) | 
|  | 295 | { | 
|  | 296 | struct apanic_data *ctx = &drv_ctx; | 
|  | 297 | struct panic_header *hdr = ctx->bounce; | 
|  | 298 | size_t len; | 
|  | 299 | int rc; | 
|  | 300 |  | 
|  | 301 | if (strcmp(mtd->name, CONFIG_APANIC_PLABEL)) | 
|  | 302 | return; | 
|  | 303 |  | 
|  | 304 | ctx->mtd = mtd; | 
|  | 305 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 306 | alloc_bbt(mtd, apanic_bbt); | 
|  | 307 | scan_bbt(mtd, apanic_bbt); | 
|  | 308 |  | 
|  | 309 | if (apanic_good_blocks == 0) { | 
|  | 310 | printk(KERN_ERR "apanic: no any good blocks?!\n"); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 311 | goto out_err; | 
|  | 312 | } | 
|  | 313 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 314 | rc = mtd->read(mtd, phy_offset(mtd, 0), mtd->writesize, | 
|  | 315 | &len, ctx->bounce); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 316 | if (rc && rc == -EBADMSG) { | 
|  | 317 | printk(KERN_WARNING | 
|  | 318 | "apanic: Bad ECC on block 0 (ignored)\n"); | 
|  | 319 | } else if (rc && rc != -EUCLEAN) { | 
|  | 320 | printk(KERN_ERR "apanic: Error reading block 0 (%d)\n", rc); | 
|  | 321 | goto out_err; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | if (len != mtd->writesize) { | 
|  | 325 | printk(KERN_ERR "apanic: Bad read size (%d)\n", rc); | 
|  | 326 | goto out_err; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | printk(KERN_INFO "apanic: Bound to mtd partition '%s'\n", mtd->name); | 
|  | 330 |  | 
|  | 331 | if (hdr->magic != PANIC_MAGIC) { | 
|  | 332 | printk(KERN_INFO "apanic: No panic data available\n"); | 
|  | 333 | mtd_panic_erase(); | 
|  | 334 | return; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | if (hdr->version != PHDR_VERSION) { | 
|  | 338 | printk(KERN_INFO "apanic: Version mismatch (%d != %d)\n", | 
|  | 339 | hdr->version, PHDR_VERSION); | 
|  | 340 | mtd_panic_erase(); | 
|  | 341 | return; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | memcpy(&ctx->curr, hdr, sizeof(struct panic_header)); | 
|  | 345 |  | 
|  | 346 | printk(KERN_INFO "apanic: c(%u, %u) t(%u, %u)\n", | 
|  | 347 | hdr->console_offset, hdr->console_length, | 
|  | 348 | hdr->threads_offset, hdr->threads_length); | 
|  | 349 |  | 
|  | 350 | if (hdr->console_length) { | 
|  | 351 | ctx->apanic_console = create_proc_entry("apanic_console", | 
|  | 352 | S_IFREG | S_IRUGO, NULL); | 
|  | 353 | if (!ctx->apanic_console) | 
|  | 354 | printk(KERN_ERR "%s: failed creating procfile\n", | 
|  | 355 | __func__); | 
|  | 356 | else { | 
|  | 357 | ctx->apanic_console->read_proc = apanic_proc_read; | 
|  | 358 | ctx->apanic_console->write_proc = apanic_proc_write; | 
|  | 359 | ctx->apanic_console->size = hdr->console_length; | 
|  | 360 | ctx->apanic_console->data = (void *) 1; | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | if (hdr->threads_length) { | 
|  | 365 | ctx->apanic_threads = create_proc_entry("apanic_threads", | 
|  | 366 | S_IFREG | S_IRUGO, NULL); | 
|  | 367 | if (!ctx->apanic_threads) | 
|  | 368 | printk(KERN_ERR "%s: failed creating procfile\n", | 
|  | 369 | __func__); | 
|  | 370 | else { | 
|  | 371 | ctx->apanic_threads->read_proc = apanic_proc_read; | 
|  | 372 | ctx->apanic_threads->write_proc = apanic_proc_write; | 
|  | 373 | ctx->apanic_threads->size = hdr->threads_length; | 
|  | 374 | ctx->apanic_threads->data = (void *) 2; | 
|  | 375 | } | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | return; | 
|  | 379 | out_err: | 
|  | 380 | ctx->mtd = NULL; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | static void mtd_panic_notify_remove(struct mtd_info *mtd) | 
|  | 384 | { | 
|  | 385 | struct apanic_data *ctx = &drv_ctx; | 
|  | 386 | if (mtd == ctx->mtd) { | 
|  | 387 | ctx->mtd = NULL; | 
|  | 388 | printk(KERN_INFO "apanic: Unbound from %s\n", mtd->name); | 
|  | 389 | } | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | static struct mtd_notifier mtd_panic_notifier = { | 
|  | 393 | .add	= mtd_panic_notify_add, | 
|  | 394 | .remove	= mtd_panic_notify_remove, | 
|  | 395 | }; | 
|  | 396 |  | 
|  | 397 | static int in_panic = 0; | 
|  | 398 |  | 
|  | 399 | static int apanic_writeflashpage(struct mtd_info *mtd, loff_t to, | 
|  | 400 | const u_char *buf) | 
|  | 401 | { | 
|  | 402 | int rc; | 
|  | 403 | size_t wlen; | 
|  | 404 | int panic = in_interrupt() | in_atomic(); | 
|  | 405 |  | 
|  | 406 | if (panic && !mtd->panic_write) { | 
|  | 407 | printk(KERN_EMERG "%s: No panic_write available\n", __func__); | 
|  | 408 | return 0; | 
|  | 409 | } else if (!panic && !mtd->write) { | 
|  | 410 | printk(KERN_EMERG "%s: No write available\n", __func__); | 
|  | 411 | return 0; | 
|  | 412 | } | 
|  | 413 |  | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 414 | to = phy_offset(mtd, to); | 
|  | 415 | if (to == APANIC_INVALID_OFFSET) { | 
|  | 416 | printk(KERN_EMERG "apanic: write to invalid address\n"); | 
|  | 417 | return 0; | 
|  | 418 | } | 
|  | 419 |  | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 420 | if (panic) | 
|  | 421 | rc = mtd->panic_write(mtd, to, mtd->writesize, &wlen, buf); | 
|  | 422 | else | 
|  | 423 | rc = mtd->write(mtd, to, mtd->writesize, &wlen, buf); | 
|  | 424 |  | 
|  | 425 | if (rc) { | 
|  | 426 | printk(KERN_EMERG | 
|  | 427 | "%s: Error writing data to flash (%d)\n", | 
|  | 428 | __func__, rc); | 
|  | 429 | return rc; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | return wlen; | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | extern int log_buf_copy(char *dest, int idx, int len); | 
|  | 436 | extern void log_buf_clear(void); | 
|  | 437 |  | 
|  | 438 | /* | 
|  | 439 | * Writes the contents of the console to the specified offset in flash. | 
|  | 440 | * Returns number of bytes written | 
|  | 441 | */ | 
|  | 442 | static int apanic_write_console(struct mtd_info *mtd, unsigned int off) | 
|  | 443 | { | 
|  | 444 | struct apanic_data *ctx = &drv_ctx; | 
|  | 445 | int saved_oip; | 
|  | 446 | int idx = 0; | 
|  | 447 | int rc, rc2; | 
|  | 448 | unsigned int last_chunk = 0; | 
|  | 449 |  | 
|  | 450 | while (!last_chunk) { | 
|  | 451 | saved_oip = oops_in_progress; | 
|  | 452 | oops_in_progress = 1; | 
|  | 453 | rc = log_buf_copy(ctx->bounce, idx, mtd->writesize); | 
|  | 454 | if (rc < 0) | 
|  | 455 | break; | 
|  | 456 |  | 
|  | 457 | if (rc != mtd->writesize) | 
|  | 458 | last_chunk = rc; | 
|  | 459 |  | 
|  | 460 | oops_in_progress = saved_oip; | 
|  | 461 | if (rc <= 0) | 
|  | 462 | break; | 
|  | 463 | if (rc != mtd->writesize) | 
|  | 464 | memset(ctx->bounce + rc, 0, mtd->writesize - rc); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 465 |  | 
|  | 466 | rc2 = apanic_writeflashpage(mtd, off, ctx->bounce); | 
|  | 467 | if (rc2 <= 0) { | 
|  | 468 | printk(KERN_EMERG | 
|  | 469 | "apanic: Flash write failed (%d)\n", rc2); | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 470 | return idx; | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 471 | } | 
|  | 472 | if (!last_chunk) | 
|  | 473 | idx += rc2; | 
|  | 474 | else | 
|  | 475 | idx += last_chunk; | 
|  | 476 | off += rc2; | 
|  | 477 | } | 
|  | 478 | return idx; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | static int apanic(struct notifier_block *this, unsigned long event, | 
|  | 482 | void *ptr) | 
|  | 483 | { | 
|  | 484 | struct apanic_data *ctx = &drv_ctx; | 
|  | 485 | struct panic_header *hdr = (struct panic_header *) ctx->bounce; | 
|  | 486 | int console_offset = 0; | 
|  | 487 | int console_len = 0; | 
|  | 488 | int threads_offset = 0; | 
|  | 489 | int threads_len = 0; | 
|  | 490 | int rc; | 
|  | 491 |  | 
|  | 492 | if (in_panic) | 
|  | 493 | return NOTIFY_DONE; | 
|  | 494 | in_panic = 1; | 
|  | 495 | #ifdef CONFIG_PREEMPT | 
|  | 496 | /* Ensure that cond_resched() won't try to preempt anybody */ | 
|  | 497 | add_preempt_count(PREEMPT_ACTIVE); | 
|  | 498 | #endif | 
| Tom Zhu | d2e9d64 | 2009-09-21 16:36:05 -0500 | [diff] [blame^] | 499 | touch_softlockup_watchdog(); | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 500 |  | 
|  | 501 | if (!ctx->mtd) | 
|  | 502 | goto out; | 
|  | 503 |  | 
|  | 504 | if (ctx->curr.magic) { | 
|  | 505 | printk(KERN_EMERG "Crash partition in use!\n"); | 
|  | 506 | goto out; | 
|  | 507 | } | 
|  | 508 | console_offset = ctx->mtd->writesize; | 
|  | 509 |  | 
|  | 510 | /* | 
|  | 511 | * Write out the console | 
|  | 512 | */ | 
|  | 513 | console_len = apanic_write_console(ctx->mtd, console_offset); | 
|  | 514 | if (console_len < 0) { | 
|  | 515 | printk(KERN_EMERG "Error writing console to panic log! (%d)\n", | 
|  | 516 | console_len); | 
|  | 517 | console_len = 0; | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | /* | 
|  | 521 | * Write out all threads | 
|  | 522 | */ | 
|  | 523 | threads_offset = ALIGN(console_offset + console_len, | 
|  | 524 | ctx->mtd->writesize); | 
|  | 525 | if (!threads_offset) | 
|  | 526 | threads_offset = ctx->mtd->writesize; | 
|  | 527 |  | 
| San Mehat | e3af8d4 | 2009-09-17 14:27:41 -0700 | [diff] [blame] | 528 | ram_console_enable_console(0); | 
|  | 529 |  | 
| San Mehat | 01c9968 | 2009-09-01 13:43:01 -0700 | [diff] [blame] | 530 | log_buf_clear(); | 
|  | 531 | show_state_filter(0); | 
|  | 532 | threads_len = apanic_write_console(ctx->mtd, threads_offset); | 
|  | 533 | if (threads_len < 0) { | 
|  | 534 | printk(KERN_EMERG "Error writing threads to panic log! (%d)\n", | 
|  | 535 | threads_len); | 
|  | 536 | threads_len = 0; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | /* | 
|  | 540 | * Finally write the panic header | 
|  | 541 | */ | 
|  | 542 | memset(ctx->bounce, 0, PAGE_SIZE); | 
|  | 543 | hdr->magic = PANIC_MAGIC; | 
|  | 544 | hdr->version = PHDR_VERSION; | 
|  | 545 |  | 
|  | 546 | hdr->console_offset = console_offset; | 
|  | 547 | hdr->console_length = console_len; | 
|  | 548 |  | 
|  | 549 | hdr->threads_offset = threads_offset; | 
|  | 550 | hdr->threads_length = threads_len; | 
|  | 551 |  | 
|  | 552 | rc = apanic_writeflashpage(ctx->mtd, 0, ctx->bounce); | 
|  | 553 | if (rc <= 0) { | 
|  | 554 | printk(KERN_EMERG "apanic: Header write failed (%d)\n", | 
|  | 555 | rc); | 
|  | 556 | goto out; | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | printk(KERN_EMERG "apanic: Panic dump sucessfully written to flash\n"); | 
|  | 560 |  | 
|  | 561 | out: | 
|  | 562 | #ifdef CONFIG_PREEMPT | 
|  | 563 | sub_preempt_count(PREEMPT_ACTIVE); | 
|  | 564 | #endif | 
|  | 565 | in_panic = 0; | 
|  | 566 | return NOTIFY_DONE; | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | static struct notifier_block panic_blk = { | 
|  | 570 | .notifier_call	= apanic, | 
|  | 571 | }; | 
|  | 572 |  | 
|  | 573 | static int panic_dbg_get(void *data, u64 *val) | 
|  | 574 | { | 
|  | 575 | apanic(NULL, 0, NULL); | 
|  | 576 | return 0; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | static int panic_dbg_set(void *data, u64 val) | 
|  | 580 | { | 
|  | 581 | BUG(); | 
|  | 582 | return -1; | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | DEFINE_SIMPLE_ATTRIBUTE(panic_dbg_fops, panic_dbg_get, panic_dbg_set, "%llu\n"); | 
|  | 586 |  | 
|  | 587 | int __init apanic_init(void) | 
|  | 588 | { | 
|  | 589 | register_mtd_user(&mtd_panic_notifier); | 
|  | 590 | atomic_notifier_chain_register(&panic_notifier_list, &panic_blk); | 
|  | 591 | debugfs_create_file("apanic", 0644, NULL, NULL, &panic_dbg_fops); | 
|  | 592 | memset(&drv_ctx, 0, sizeof(drv_ctx)); | 
|  | 593 | drv_ctx.bounce = (void *) __get_free_page(GFP_KERNEL); | 
|  | 594 | INIT_WORK(&proc_removal_work, apanic_remove_proc_work); | 
|  | 595 | printk(KERN_INFO "Android kernel panic handler initialized (bind=%s)\n", | 
|  | 596 | CONFIG_APANIC_PLABEL); | 
|  | 597 | return 0; | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | module_init(apanic_init); |