^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * MTD Oops/Panic logger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2007 Nokia Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Richard Purdie <rpurdie@openedhand.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kmsg_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Maximum MTD partition size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define MTDOOPS_HEADER_SIZE 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static unsigned long record_size = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) module_param(record_size, ulong, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_PARM_DESC(record_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) "record size for MTD OOPS pages in bytes (default 4096)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static char mtddev[80];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) module_param_string(mtddev, mtddev, 80, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) MODULE_PARM_DESC(mtddev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) "name or index number of the MTD device to use");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int dump_oops = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) module_param(dump_oops, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) MODULE_PARM_DESC(dump_oops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) "set to 1 to dump oopses, 0 to only dump panics (default 1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct mtdoops_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct kmsg_dumper dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int mtd_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct work_struct work_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct work_struct work_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int oops_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int nextpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int nextcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned long *oops_page_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void *oops_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) } oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void mark_page_used(struct mtdoops_context *cxt, int page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) set_bit(page, cxt->oops_page_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void mark_page_unused(struct mtdoops_context *cxt, int page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) clear_bit(page, cxt->oops_page_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int page_is_used(struct mtdoops_context *cxt, int page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return test_bit(page, cxt->oops_page_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u32 start_page = start_page_offset / record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u32 erase_pages = mtd->erasesize / record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct erase_info erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) erase.addr = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) erase.len = mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = mtd_erase(mtd, &erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) (unsigned long long)erase.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (unsigned long long)erase.len, mtddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Mark pages as unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) for (page = start_page; page < start_page + erase_pages; page++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mark_page_unused(cxt, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void mtdoops_inc_counter(struct mtdoops_context *cxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cxt->nextpage++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (cxt->nextpage >= cxt->oops_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) cxt->nextpage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cxt->nextcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (cxt->nextcount == 0xffffffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) cxt->nextcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (page_is_used(cxt, cxt->nextpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) schedule_work(&cxt->work_erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) cxt->nextpage, cxt->nextcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Scheduled work - when we can't proceed without erasing a block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void mtdoops_workfunc_erase(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct mtdoops_context *cxt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) container_of(work, struct mtdoops_context, work_erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int i = 0, j, ret, mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* We were unregistered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mod = (cxt->nextpage * record_size) % mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (mod != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (cxt->nextpage >= cxt->oops_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) cxt->nextpage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) while ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) badblock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) cxt->nextpage * record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (cxt->nextpage >= cxt->oops_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) cxt->nextpage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) printk(KERN_ERR "mtdoops: all blocks bad!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) printk(KERN_ERR "mtdoops: mtd_block_isbad failed, aborting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) cxt->nextpage, cxt->nextcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret == -EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret < 0 && ret != -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto badblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void mtdoops_write(struct mtdoops_context *cxt, int panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) u32 *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Add mtdoops header to the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) hdr = cxt->oops_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) hdr[0] = cxt->nextcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) hdr[1] = MTDOOPS_KERNMSG_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (panic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) record_size, &retlen, cxt->oops_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (ret == -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ret = mtd_write(mtd, cxt->nextpage * record_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) record_size, &retlen, cxt->oops_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (retlen != record_size || ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) cxt->nextpage * record_size, retlen, record_size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) mark_page_used(cxt, cxt->nextpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) memset(cxt->oops_buf, 0xff, record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) mtdoops_inc_counter(cxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void mtdoops_workfunc_write(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct mtdoops_context *cxt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) container_of(work, struct mtdoops_context, work_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) mtdoops_write(cxt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void find_next_position(struct mtdoops_context *cxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) int ret, page, maxpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) u32 count[2], maxcount = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) for (page = 0; page < cxt->oops_pages; page++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (mtd_block_isbad(mtd, page * record_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* Assume the page is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) mark_page_used(cxt, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) &retlen, (u_char *)&count[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (retlen != MTDOOPS_HEADER_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) (ret < 0 && !mtd_is_bitflip(ret))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) page * record_size, retlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) MTDOOPS_HEADER_SIZE, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (count[0] == 0xffffffff && count[1] == 0xffffffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mark_page_unused(cxt, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (maxcount == 0xffffffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) maxcount = count[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) maxpos = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) maxcount = count[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) maxpos = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) } else if (count[0] > maxcount && count[0] < 0xc0000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) maxcount = count[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) maxpos = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else if (count[0] > maxcount && count[0] > 0xc0000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) && maxcount > 0x80000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) maxcount = count[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) maxpos = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (maxcount == 0xffffffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) cxt->nextpage = cxt->oops_pages - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) cxt->nextcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) cxt->nextpage = maxpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) cxt->nextcount = maxcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mtdoops_inc_counter(cxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void mtdoops_do_dump(struct kmsg_dumper *dumper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) enum kmsg_dump_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct mtdoops_context *cxt = container_of(dumper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct mtdoops_context, dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Only dump oopses if dump_oops is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (reason == KMSG_DUMP_OOPS && !dump_oops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) record_size - MTDOOPS_HEADER_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (reason != KMSG_DUMP_OOPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* Panics must be written immediately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) mtdoops_write(cxt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* For other cases, schedule work to write it "nicely" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) schedule_work(&cxt->work_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void mtdoops_notify_add(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct mtdoops_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) u64 mtdoops_pages = div_u64(mtd->size, record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!strcmp(mtd->name, mtddev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) cxt->mtd_index = mtd->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (mtd->size < mtd->erasesize * 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (mtd->erasesize < record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* oops_page_used is a bit field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cxt->oops_page_used =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) vmalloc(array_size(sizeof(unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) DIV_ROUND_UP(mtdoops_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) BITS_PER_LONG)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!cxt->oops_page_used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) printk(KERN_ERR "mtdoops: could not allocate page array\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cxt->dump.max_reason = KMSG_DUMP_OOPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) cxt->dump.dump = mtdoops_do_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) err = kmsg_dump_register(&cxt->dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) vfree(cxt->oops_page_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) cxt->oops_page_used = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) cxt->mtd = mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cxt->oops_pages = (int)mtd->size / record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) find_next_position(cxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static void mtdoops_notify_remove(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct mtdoops_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (kmsg_dump_unregister(&cxt->dump) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) cxt->mtd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) flush_work(&cxt->work_erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) flush_work(&cxt->work_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static struct mtd_notifier mtdoops_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) .add = mtdoops_notify_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) .remove = mtdoops_notify_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int __init mtdoops_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct mtdoops_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int mtd_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (strlen(mtddev) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if ((record_size & 4095) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (record_size < 4096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* Setup the MTD device to use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) cxt->mtd_index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) mtd_index = simple_strtoul(mtddev, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (*endp == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) cxt->mtd_index = mtd_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) cxt->oops_buf = vmalloc(record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (!cxt->oops_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) memset(cxt->oops_buf, 0xff, record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) register_mtd_user(&mtdoops_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static void __exit mtdoops_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct mtdoops_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) unregister_mtd_user(&mtdoops_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) vfree(cxt->oops_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) vfree(cxt->oops_page_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) module_init(mtdoops_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) module_exit(mtdoops_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");