0001
0002
0003
0004
0005
0006
0007
0008 #include "ubi.h"
0009 #include <linux/debugfs.h>
0010 #include <linux/uaccess.h>
0011 #include <linux/module.h>
0012 #include <linux/seq_file.h>
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
0023 {
0024 int err;
0025 size_t read;
0026 void *buf;
0027 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
0028
0029 buf = vmalloc(len);
0030 if (!buf)
0031 return;
0032 err = mtd_read(ubi->mtd, addr, len, &read, buf);
0033 if (err && err != -EUCLEAN) {
0034 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
0035 err, len, pnum, offset, read);
0036 goto out;
0037 }
0038
0039 ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
0040 len, pnum, offset);
0041 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
0042 out:
0043 vfree(buf);
0044 return;
0045 }
0046
0047
0048
0049
0050
0051 void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
0052 {
0053 pr_err("Erase counter header dump:\n");
0054 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
0055 pr_err("\tversion %d\n", (int)ec_hdr->version);
0056 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
0057 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
0058 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
0059 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
0060 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
0061 pr_err("erase counter header hexdump:\n");
0062 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
0063 ec_hdr, UBI_EC_HDR_SIZE, 1);
0064 }
0065
0066
0067
0068
0069
0070 void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
0071 {
0072 pr_err("Volume identifier header dump:\n");
0073 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
0074 pr_err("\tversion %d\n", (int)vid_hdr->version);
0075 pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
0076 pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
0077 pr_err("\tcompat %d\n", (int)vid_hdr->compat);
0078 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
0079 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
0080 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
0081 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
0082 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
0083 pr_err("\tsqnum %llu\n",
0084 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
0085 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
0086 pr_err("Volume identifier header hexdump:\n");
0087 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
0088 vid_hdr, UBI_VID_HDR_SIZE, 1);
0089 }
0090
0091
0092
0093
0094
0095 void ubi_dump_vol_info(const struct ubi_volume *vol)
0096 {
0097 pr_err("Volume information dump:\n");
0098 pr_err("\tvol_id %d\n", vol->vol_id);
0099 pr_err("\treserved_pebs %d\n", vol->reserved_pebs);
0100 pr_err("\talignment %d\n", vol->alignment);
0101 pr_err("\tdata_pad %d\n", vol->data_pad);
0102 pr_err("\tvol_type %d\n", vol->vol_type);
0103 pr_err("\tname_len %d\n", vol->name_len);
0104 pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
0105 pr_err("\tused_ebs %d\n", vol->used_ebs);
0106 pr_err("\tused_bytes %lld\n", vol->used_bytes);
0107 pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
0108 pr_err("\tcorrupted %d\n", vol->corrupted);
0109 pr_err("\tupd_marker %d\n", vol->upd_marker);
0110 pr_err("\tskip_check %d\n", vol->skip_check);
0111
0112 if (vol->name_len <= UBI_VOL_NAME_MAX &&
0113 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
0114 pr_err("\tname %s\n", vol->name);
0115 } else {
0116 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
0117 vol->name[0], vol->name[1], vol->name[2],
0118 vol->name[3], vol->name[4]);
0119 }
0120 }
0121
0122
0123
0124
0125
0126
0127 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
0128 {
0129 int name_len = be16_to_cpu(r->name_len);
0130
0131 pr_err("Volume table record %d dump:\n", idx);
0132 pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
0133 pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
0134 pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
0135 pr_err("\tvol_type %d\n", (int)r->vol_type);
0136 pr_err("\tupd_marker %d\n", (int)r->upd_marker);
0137 pr_err("\tname_len %d\n", name_len);
0138
0139 if (r->name[0] == '\0') {
0140 pr_err("\tname NULL\n");
0141 return;
0142 }
0143
0144 if (name_len <= UBI_VOL_NAME_MAX &&
0145 strnlen(&r->name[0], name_len + 1) == name_len) {
0146 pr_err("\tname %s\n", &r->name[0]);
0147 } else {
0148 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
0149 r->name[0], r->name[1], r->name[2], r->name[3],
0150 r->name[4]);
0151 }
0152 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
0153 }
0154
0155
0156
0157
0158
0159 void ubi_dump_av(const struct ubi_ainf_volume *av)
0160 {
0161 pr_err("Volume attaching information dump:\n");
0162 pr_err("\tvol_id %d\n", av->vol_id);
0163 pr_err("\thighest_lnum %d\n", av->highest_lnum);
0164 pr_err("\tleb_count %d\n", av->leb_count);
0165 pr_err("\tcompat %d\n", av->compat);
0166 pr_err("\tvol_type %d\n", av->vol_type);
0167 pr_err("\tused_ebs %d\n", av->used_ebs);
0168 pr_err("\tlast_data_size %d\n", av->last_data_size);
0169 pr_err("\tdata_pad %d\n", av->data_pad);
0170 }
0171
0172
0173
0174
0175
0176
0177 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
0178 {
0179 pr_err("eraseblock attaching information dump:\n");
0180 pr_err("\tec %d\n", aeb->ec);
0181 pr_err("\tpnum %d\n", aeb->pnum);
0182 if (type == 0) {
0183 pr_err("\tlnum %d\n", aeb->lnum);
0184 pr_err("\tscrub %d\n", aeb->scrub);
0185 pr_err("\tsqnum %llu\n", aeb->sqnum);
0186 }
0187 }
0188
0189
0190
0191
0192
0193 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
0194 {
0195 char nm[17];
0196
0197 pr_err("Volume creation request dump:\n");
0198 pr_err("\tvol_id %d\n", req->vol_id);
0199 pr_err("\talignment %d\n", req->alignment);
0200 pr_err("\tbytes %lld\n", (long long)req->bytes);
0201 pr_err("\tvol_type %d\n", req->vol_type);
0202 pr_err("\tname_len %d\n", req->name_len);
0203
0204 memcpy(nm, req->name, 16);
0205 nm[16] = 0;
0206 pr_err("\t1st 16 characters of name: %s\n", nm);
0207 }
0208
0209
0210
0211
0212
0213 static struct dentry *dfs_rootdir;
0214
0215
0216
0217
0218
0219
0220
0221 int ubi_debugfs_init(void)
0222 {
0223 if (!IS_ENABLED(CONFIG_DEBUG_FS))
0224 return 0;
0225
0226 dfs_rootdir = debugfs_create_dir("ubi", NULL);
0227 if (IS_ERR_OR_NULL(dfs_rootdir)) {
0228 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
0229
0230 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
0231 err);
0232 return err;
0233 }
0234
0235 return 0;
0236 }
0237
0238
0239
0240
0241 void ubi_debugfs_exit(void)
0242 {
0243 if (IS_ENABLED(CONFIG_DEBUG_FS))
0244 debugfs_remove(dfs_rootdir);
0245 }
0246
0247
0248 static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
0249 size_t count, loff_t *ppos)
0250 {
0251 unsigned long ubi_num = (unsigned long)file->private_data;
0252 struct dentry *dent = file->f_path.dentry;
0253 struct ubi_device *ubi;
0254 struct ubi_debug_info *d;
0255 char buf[8];
0256 int val;
0257
0258 ubi = ubi_get_device(ubi_num);
0259 if (!ubi)
0260 return -ENODEV;
0261 d = &ubi->dbg;
0262
0263 if (dent == d->dfs_chk_gen)
0264 val = d->chk_gen;
0265 else if (dent == d->dfs_chk_io)
0266 val = d->chk_io;
0267 else if (dent == d->dfs_chk_fastmap)
0268 val = d->chk_fastmap;
0269 else if (dent == d->dfs_disable_bgt)
0270 val = d->disable_bgt;
0271 else if (dent == d->dfs_emulate_bitflips)
0272 val = d->emulate_bitflips;
0273 else if (dent == d->dfs_emulate_io_failures)
0274 val = d->emulate_io_failures;
0275 else if (dent == d->dfs_emulate_power_cut) {
0276 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
0277 count = simple_read_from_buffer(user_buf, count, ppos,
0278 buf, strlen(buf));
0279 goto out;
0280 } else if (dent == d->dfs_power_cut_min) {
0281 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
0282 count = simple_read_from_buffer(user_buf, count, ppos,
0283 buf, strlen(buf));
0284 goto out;
0285 } else if (dent == d->dfs_power_cut_max) {
0286 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
0287 count = simple_read_from_buffer(user_buf, count, ppos,
0288 buf, strlen(buf));
0289 goto out;
0290 }
0291 else {
0292 count = -EINVAL;
0293 goto out;
0294 }
0295
0296 if (val)
0297 buf[0] = '1';
0298 else
0299 buf[0] = '0';
0300 buf[1] = '\n';
0301 buf[2] = 0x00;
0302
0303 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
0304
0305 out:
0306 ubi_put_device(ubi);
0307 return count;
0308 }
0309
0310
0311 static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
0312 size_t count, loff_t *ppos)
0313 {
0314 unsigned long ubi_num = (unsigned long)file->private_data;
0315 struct dentry *dent = file->f_path.dentry;
0316 struct ubi_device *ubi;
0317 struct ubi_debug_info *d;
0318 size_t buf_size;
0319 char buf[8] = {0};
0320 int val;
0321
0322 ubi = ubi_get_device(ubi_num);
0323 if (!ubi)
0324 return -ENODEV;
0325 d = &ubi->dbg;
0326
0327 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
0328 if (copy_from_user(buf, user_buf, buf_size)) {
0329 count = -EFAULT;
0330 goto out;
0331 }
0332
0333 if (dent == d->dfs_power_cut_min) {
0334 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
0335 count = -EINVAL;
0336 goto out;
0337 } else if (dent == d->dfs_power_cut_max) {
0338 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
0339 count = -EINVAL;
0340 goto out;
0341 } else if (dent == d->dfs_emulate_power_cut) {
0342 if (kstrtoint(buf, 0, &val) != 0)
0343 count = -EINVAL;
0344 else
0345 d->emulate_power_cut = val;
0346 goto out;
0347 }
0348
0349 if (buf[0] == '1')
0350 val = 1;
0351 else if (buf[0] == '0')
0352 val = 0;
0353 else {
0354 count = -EINVAL;
0355 goto out;
0356 }
0357
0358 if (dent == d->dfs_chk_gen)
0359 d->chk_gen = val;
0360 else if (dent == d->dfs_chk_io)
0361 d->chk_io = val;
0362 else if (dent == d->dfs_chk_fastmap)
0363 d->chk_fastmap = val;
0364 else if (dent == d->dfs_disable_bgt)
0365 d->disable_bgt = val;
0366 else if (dent == d->dfs_emulate_bitflips)
0367 d->emulate_bitflips = val;
0368 else if (dent == d->dfs_emulate_io_failures)
0369 d->emulate_io_failures = val;
0370 else
0371 count = -EINVAL;
0372
0373 out:
0374 ubi_put_device(ubi);
0375 return count;
0376 }
0377
0378
0379
0380
0381 static const struct file_operations dfs_fops = {
0382 .read = dfs_file_read,
0383 .write = dfs_file_write,
0384 .open = simple_open,
0385 .llseek = no_llseek,
0386 .owner = THIS_MODULE,
0387 };
0388
0389
0390
0391
0392 static void *eraseblk_count_seq_start(struct seq_file *s, loff_t *pos)
0393 {
0394 struct ubi_device *ubi = s->private;
0395
0396 if (*pos < ubi->peb_count)
0397 return pos;
0398
0399 return NULL;
0400 }
0401
0402
0403
0404
0405 static void *eraseblk_count_seq_next(struct seq_file *s, void *v, loff_t *pos)
0406 {
0407 struct ubi_device *ubi = s->private;
0408
0409 (*pos)++;
0410
0411 if (*pos < ubi->peb_count)
0412 return pos;
0413
0414 return NULL;
0415 }
0416
0417 static void eraseblk_count_seq_stop(struct seq_file *s, void *v)
0418 {
0419 }
0420
0421 static int eraseblk_count_seq_show(struct seq_file *s, void *iter)
0422 {
0423 struct ubi_device *ubi = s->private;
0424 struct ubi_wl_entry *wl;
0425 int *block_number = iter;
0426 int erase_count = -1;
0427 int err;
0428
0429
0430 if (*block_number == 0)
0431 seq_puts(s, "physical_block_number\terase_count\n");
0432
0433 err = ubi_io_is_bad(ubi, *block_number);
0434 if (err)
0435 return err;
0436
0437 spin_lock(&ubi->wl_lock);
0438
0439 wl = ubi->lookuptbl[*block_number];
0440 if (wl)
0441 erase_count = wl->ec;
0442
0443 spin_unlock(&ubi->wl_lock);
0444
0445 if (erase_count < 0)
0446 return 0;
0447
0448 seq_printf(s, "%-22d\t%-11d\n", *block_number, erase_count);
0449
0450 return 0;
0451 }
0452
0453 static const struct seq_operations eraseblk_count_seq_ops = {
0454 .start = eraseblk_count_seq_start,
0455 .next = eraseblk_count_seq_next,
0456 .stop = eraseblk_count_seq_stop,
0457 .show = eraseblk_count_seq_show
0458 };
0459
0460 static int eraseblk_count_open(struct inode *inode, struct file *f)
0461 {
0462 struct seq_file *s;
0463 int err;
0464
0465 err = seq_open(f, &eraseblk_count_seq_ops);
0466 if (err)
0467 return err;
0468
0469 s = f->private_data;
0470 s->private = ubi_get_device((unsigned long)inode->i_private);
0471
0472 if (!s->private)
0473 return -ENODEV;
0474 else
0475 return 0;
0476 }
0477
0478 static int eraseblk_count_release(struct inode *inode, struct file *f)
0479 {
0480 struct seq_file *s = f->private_data;
0481 struct ubi_device *ubi = s->private;
0482
0483 ubi_put_device(ubi);
0484
0485 return seq_release(inode, f);
0486 }
0487
0488 static const struct file_operations eraseblk_count_fops = {
0489 .owner = THIS_MODULE,
0490 .open = eraseblk_count_open,
0491 .read = seq_read,
0492 .llseek = seq_lseek,
0493 .release = eraseblk_count_release,
0494 };
0495
0496
0497
0498
0499
0500
0501
0502
0503 int ubi_debugfs_init_dev(struct ubi_device *ubi)
0504 {
0505 unsigned long ubi_num = ubi->ubi_num;
0506 struct ubi_debug_info *d = &ubi->dbg;
0507 int n;
0508
0509 if (!IS_ENABLED(CONFIG_DEBUG_FS))
0510 return 0;
0511
0512 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
0513 ubi->ubi_num);
0514 if (n > UBI_DFS_DIR_LEN) {
0515
0516 return -EINVAL;
0517 }
0518
0519 d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
0520
0521 d->dfs_chk_gen = debugfs_create_file("chk_gen", S_IWUSR, d->dfs_dir,
0522 (void *)ubi_num, &dfs_fops);
0523
0524 d->dfs_chk_io = debugfs_create_file("chk_io", S_IWUSR, d->dfs_dir,
0525 (void *)ubi_num, &dfs_fops);
0526
0527 d->dfs_chk_fastmap = debugfs_create_file("chk_fastmap", S_IWUSR,
0528 d->dfs_dir, (void *)ubi_num,
0529 &dfs_fops);
0530
0531 d->dfs_disable_bgt = debugfs_create_file("tst_disable_bgt", S_IWUSR,
0532 d->dfs_dir, (void *)ubi_num,
0533 &dfs_fops);
0534
0535 d->dfs_emulate_bitflips = debugfs_create_file("tst_emulate_bitflips",
0536 S_IWUSR, d->dfs_dir,
0537 (void *)ubi_num,
0538 &dfs_fops);
0539
0540 d->dfs_emulate_io_failures = debugfs_create_file("tst_emulate_io_failures",
0541 S_IWUSR, d->dfs_dir,
0542 (void *)ubi_num,
0543 &dfs_fops);
0544
0545 d->dfs_emulate_power_cut = debugfs_create_file("tst_emulate_power_cut",
0546 S_IWUSR, d->dfs_dir,
0547 (void *)ubi_num,
0548 &dfs_fops);
0549
0550 d->dfs_power_cut_min = debugfs_create_file("tst_emulate_power_cut_min",
0551 S_IWUSR, d->dfs_dir,
0552 (void *)ubi_num, &dfs_fops);
0553
0554 d->dfs_power_cut_max = debugfs_create_file("tst_emulate_power_cut_max",
0555 S_IWUSR, d->dfs_dir,
0556 (void *)ubi_num, &dfs_fops);
0557
0558 debugfs_create_file("detailed_erase_block_info", S_IRUSR, d->dfs_dir,
0559 (void *)ubi_num, &eraseblk_count_fops);
0560
0561 return 0;
0562 }
0563
0564
0565
0566
0567
0568 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
0569 {
0570 if (IS_ENABLED(CONFIG_DEBUG_FS))
0571 debugfs_remove_recursive(ubi->dbg.dfs_dir);
0572 }
0573
0574
0575
0576
0577
0578
0579
0580
0581 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
0582 {
0583 unsigned int range;
0584
0585 if ((ubi->dbg.emulate_power_cut & caller) == 0)
0586 return 0;
0587
0588 if (ubi->dbg.power_cut_counter == 0) {
0589 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
0590
0591 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
0592 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
0593 ubi->dbg.power_cut_counter += prandom_u32() % range;
0594 }
0595 return 0;
0596 }
0597
0598 ubi->dbg.power_cut_counter--;
0599 if (ubi->dbg.power_cut_counter)
0600 return 0;
0601
0602 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
0603 ubi_ro_mode(ubi);
0604 return 1;
0605 }