dm: core: Require users of devres to include the header
[oweals/u-boot.git] / drivers / mtd / ubi / debug.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  *
5  * Author: Artem Bityutskiy (Битюцкий Артём)
6  */
7
8 #include <hexdump.h>
9 #include <ubi_uboot.h>
10 #include "ubi.h"
11 #ifndef __UBOOT__
12 #include <linux/debugfs.h>
13 #include <linux/err.h>
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #endif
17
18 /**
19  * ubi_dump_flash - dump a region of flash.
20  * @ubi: UBI device description object
21  * @pnum: the physical eraseblock number to dump
22  * @offset: the starting offset within the physical eraseblock to dump
23  * @len: the length of the region to dump
24  */
25 void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
26 {
27         int err;
28         size_t read;
29         void *buf;
30         loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
31
32         buf = vmalloc(len);
33         if (!buf)
34                 return;
35         err = mtd_read(ubi->mtd, addr, len, &read, buf);
36         if (err && err != -EUCLEAN) {
37                 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
38                         err, len, pnum, offset, read);
39                 goto out;
40         }
41
42         ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
43                 len, pnum, offset);
44         print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
45 out:
46         vfree(buf);
47         return;
48 }
49
50 /**
51  * ubi_dump_ec_hdr - dump an erase counter header.
52  * @ec_hdr: the erase counter header to dump
53  */
54 void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
55 {
56         pr_err("Erase counter header dump:\n");
57         pr_err("\tmagic          %#08x\n", be32_to_cpu(ec_hdr->magic));
58         pr_err("\tversion        %d\n", (int)ec_hdr->version);
59         pr_err("\tec             %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
60         pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
61         pr_err("\tdata_offset    %d\n", be32_to_cpu(ec_hdr->data_offset));
62         pr_err("\timage_seq      %d\n", be32_to_cpu(ec_hdr->image_seq));
63         pr_err("\thdr_crc        %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
64         pr_err("erase counter header hexdump:\n");
65         print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
66                        ec_hdr, UBI_EC_HDR_SIZE, 1);
67 }
68
69 /**
70  * ubi_dump_vid_hdr - dump a volume identifier header.
71  * @vid_hdr: the volume identifier header to dump
72  */
73 void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
74 {
75         pr_err("Volume identifier header dump:\n");
76         pr_err("\tmagic     %08x\n", be32_to_cpu(vid_hdr->magic));
77         pr_err("\tversion   %d\n",  (int)vid_hdr->version);
78         pr_err("\tvol_type  %d\n",  (int)vid_hdr->vol_type);
79         pr_err("\tcopy_flag %d\n",  (int)vid_hdr->copy_flag);
80         pr_err("\tcompat    %d\n",  (int)vid_hdr->compat);
81         pr_err("\tvol_id    %d\n",  be32_to_cpu(vid_hdr->vol_id));
82         pr_err("\tlnum      %d\n",  be32_to_cpu(vid_hdr->lnum));
83         pr_err("\tdata_size %d\n",  be32_to_cpu(vid_hdr->data_size));
84         pr_err("\tused_ebs  %d\n",  be32_to_cpu(vid_hdr->used_ebs));
85         pr_err("\tdata_pad  %d\n",  be32_to_cpu(vid_hdr->data_pad));
86         pr_err("\tsqnum     %llu\n",
87                 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
88         pr_err("\thdr_crc   %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
89         pr_err("Volume identifier header hexdump:\n");
90         print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
91                        vid_hdr, UBI_VID_HDR_SIZE, 1);
92 }
93
94 /**
95  * ubi_dump_vol_info - dump volume information.
96  * @vol: UBI volume description object
97  */
98 void ubi_dump_vol_info(const struct ubi_volume *vol)
99 {
100         printf("Volume information dump:\n");
101         printf("\tvol_id          %d\n", vol->vol_id);
102         printf("\treserved_pebs   %d\n", vol->reserved_pebs);
103         printf("\talignment       %d\n", vol->alignment);
104         printf("\tdata_pad        %d\n", vol->data_pad);
105         printf("\tvol_type        %d\n", vol->vol_type);
106         printf("\tname_len        %d\n", vol->name_len);
107         printf("\tusable_leb_size %d\n", vol->usable_leb_size);
108         printf("\tused_ebs        %d\n", vol->used_ebs);
109         printf("\tused_bytes      %lld\n", vol->used_bytes);
110         printf("\tlast_eb_bytes   %d\n", vol->last_eb_bytes);
111         printf("\tcorrupted       %d\n", vol->corrupted);
112         printf("\tupd_marker      %d\n", vol->upd_marker);
113         printf("\tskip_check      %d\n", vol->skip_check);
114
115         if (vol->name_len <= UBI_VOL_NAME_MAX &&
116             strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
117                 printf("\tname            %s\n", vol->name);
118         } else {
119                 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
120                        vol->name[0], vol->name[1], vol->name[2],
121                        vol->name[3], vol->name[4]);
122         }
123 }
124
125 /**
126  * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
127  * @r: the object to dump
128  * @idx: volume table index
129  */
130 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
131 {
132         int name_len = be16_to_cpu(r->name_len);
133
134         pr_err("Volume table record %d dump:\n", idx);
135         pr_err("\treserved_pebs   %d\n", be32_to_cpu(r->reserved_pebs));
136         pr_err("\talignment       %d\n", be32_to_cpu(r->alignment));
137         pr_err("\tdata_pad        %d\n", be32_to_cpu(r->data_pad));
138         pr_err("\tvol_type        %d\n", (int)r->vol_type);
139         pr_err("\tupd_marker      %d\n", (int)r->upd_marker);
140         pr_err("\tname_len        %d\n", name_len);
141
142         if (r->name[0] == '\0') {
143                 pr_err("\tname            NULL\n");
144                 return;
145         }
146
147         if (name_len <= UBI_VOL_NAME_MAX &&
148             strnlen(&r->name[0], name_len + 1) == name_len) {
149                 pr_err("\tname            %s\n", &r->name[0]);
150         } else {
151                 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
152                         r->name[0], r->name[1], r->name[2], r->name[3],
153                         r->name[4]);
154         }
155         pr_err("\tcrc             %#08x\n", be32_to_cpu(r->crc));
156 }
157
158 /**
159  * ubi_dump_av - dump a &struct ubi_ainf_volume object.
160  * @av: the object to dump
161  */
162 void ubi_dump_av(const struct ubi_ainf_volume *av)
163 {
164         pr_err("Volume attaching information dump:\n");
165         pr_err("\tvol_id         %d\n", av->vol_id);
166         pr_err("\thighest_lnum   %d\n", av->highest_lnum);
167         pr_err("\tleb_count      %d\n", av->leb_count);
168         pr_err("\tcompat         %d\n", av->compat);
169         pr_err("\tvol_type       %d\n", av->vol_type);
170         pr_err("\tused_ebs       %d\n", av->used_ebs);
171         pr_err("\tlast_data_size %d\n", av->last_data_size);
172         pr_err("\tdata_pad       %d\n", av->data_pad);
173 }
174
175 /**
176  * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
177  * @aeb: the object to dump
178  * @type: object type: 0 - not corrupted, 1 - corrupted
179  */
180 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
181 {
182         pr_err("eraseblock attaching information dump:\n");
183         pr_err("\tec       %d\n", aeb->ec);
184         pr_err("\tpnum     %d\n", aeb->pnum);
185         if (type == 0) {
186                 pr_err("\tlnum     %d\n", aeb->lnum);
187                 pr_err("\tscrub    %d\n", aeb->scrub);
188                 pr_err("\tsqnum    %llu\n", aeb->sqnum);
189         }
190 }
191
192 /**
193  * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
194  * @req: the object to dump
195  */
196 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
197 {
198         char nm[17];
199
200         pr_err("Volume creation request dump:\n");
201         pr_err("\tvol_id    %d\n",   req->vol_id);
202         pr_err("\talignment %d\n",   req->alignment);
203         pr_err("\tbytes     %lld\n", (long long)req->bytes);
204         pr_err("\tvol_type  %d\n",   req->vol_type);
205         pr_err("\tname_len  %d\n",   req->name_len);
206
207         memcpy(nm, req->name, 16);
208         nm[16] = 0;
209         pr_err("\t1st 16 characters of name: %s\n", nm);
210 }
211
212 #ifndef __UBOOT__
213 /*
214  * Root directory for UBI stuff in debugfs. Contains sub-directories which
215  * contain the stuff specific to particular UBI devices.
216  */
217 static struct dentry *dfs_rootdir;
218
219 /**
220  * ubi_debugfs_init - create UBI debugfs directory.
221  *
222  * Create UBI debugfs directory. Returns zero in case of success and a negative
223  * error code in case of failure.
224  */
225 int ubi_debugfs_init(void)
226 {
227         if (!IS_ENABLED(CONFIG_DEBUG_FS))
228                 return 0;
229
230         dfs_rootdir = debugfs_create_dir("ubi", NULL);
231         if (IS_ERR_OR_NULL(dfs_rootdir)) {
232                 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
233
234                 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
235                        err);
236                 return err;
237         }
238
239         return 0;
240 }
241
242 /**
243  * ubi_debugfs_exit - remove UBI debugfs directory.
244  */
245 void ubi_debugfs_exit(void)
246 {
247         if (IS_ENABLED(CONFIG_DEBUG_FS))
248                 debugfs_remove(dfs_rootdir);
249 }
250
251 /* Read an UBI debugfs file */
252 static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
253                              size_t count, loff_t *ppos)
254 {
255         unsigned long ubi_num = (unsigned long)file->private_data;
256         struct dentry *dent = file->f_path.dentry;
257         struct ubi_device *ubi;
258         struct ubi_debug_info *d;
259         char buf[8];
260         int val;
261
262         ubi = ubi_get_device(ubi_num);
263         if (!ubi)
264                 return -ENODEV;
265         d = &ubi->dbg;
266
267         if (dent == d->dfs_chk_gen)
268                 val = d->chk_gen;
269         else if (dent == d->dfs_chk_io)
270                 val = d->chk_io;
271         else if (dent == d->dfs_chk_fastmap)
272                 val = d->chk_fastmap;
273         else if (dent == d->dfs_disable_bgt)
274                 val = d->disable_bgt;
275         else if (dent == d->dfs_emulate_bitflips)
276                 val = d->emulate_bitflips;
277         else if (dent == d->dfs_emulate_io_failures)
278                 val = d->emulate_io_failures;
279         else if (dent == d->dfs_emulate_power_cut) {
280                 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
281                 count = simple_read_from_buffer(user_buf, count, ppos,
282                                                 buf, strlen(buf));
283                 goto out;
284         } else if (dent == d->dfs_power_cut_min) {
285                 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
286                 count = simple_read_from_buffer(user_buf, count, ppos,
287                                                 buf, strlen(buf));
288                 goto out;
289         } else if (dent == d->dfs_power_cut_max) {
290                 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
291                 count = simple_read_from_buffer(user_buf, count, ppos,
292                                                 buf, strlen(buf));
293                 goto out;
294         }
295         else {
296                 count = -EINVAL;
297                 goto out;
298         }
299
300         if (val)
301                 buf[0] = '1';
302         else
303                 buf[0] = '0';
304         buf[1] = '\n';
305         buf[2] = 0x00;
306
307         count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
308
309 out:
310         ubi_put_device(ubi);
311         return count;
312 }
313
314 /* Write an UBI debugfs file */
315 static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
316                               size_t count, loff_t *ppos)
317 {
318         unsigned long ubi_num = (unsigned long)file->private_data;
319         struct dentry *dent = file->f_path.dentry;
320         struct ubi_device *ubi;
321         struct ubi_debug_info *d;
322         size_t buf_size;
323         char buf[8] = {0};
324         int val;
325
326         ubi = ubi_get_device(ubi_num);
327         if (!ubi)
328                 return -ENODEV;
329         d = &ubi->dbg;
330
331         buf_size = min_t(size_t, count, (sizeof(buf) - 1));
332         if (copy_from_user(buf, user_buf, buf_size)) {
333                 count = -EFAULT;
334                 goto out;
335         }
336
337         if (dent == d->dfs_power_cut_min) {
338                 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
339                         count = -EINVAL;
340                 goto out;
341         } else if (dent == d->dfs_power_cut_max) {
342                 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
343                         count = -EINVAL;
344                 goto out;
345         } else if (dent == d->dfs_emulate_power_cut) {
346                 if (kstrtoint(buf, 0, &val) != 0)
347                         count = -EINVAL;
348                 d->emulate_power_cut = val;
349                 goto out;
350         }
351
352         if (buf[0] == '1')
353                 val = 1;
354         else if (buf[0] == '0')
355                 val = 0;
356         else {
357                 count = -EINVAL;
358                 goto out;
359         }
360
361         if (dent == d->dfs_chk_gen)
362                 d->chk_gen = val;
363         else if (dent == d->dfs_chk_io)
364                 d->chk_io = val;
365         else if (dent == d->dfs_chk_fastmap)
366                 d->chk_fastmap = val;
367         else if (dent == d->dfs_disable_bgt)
368                 d->disable_bgt = val;
369         else if (dent == d->dfs_emulate_bitflips)
370                 d->emulate_bitflips = val;
371         else if (dent == d->dfs_emulate_io_failures)
372                 d->emulate_io_failures = val;
373         else
374                 count = -EINVAL;
375
376 out:
377         ubi_put_device(ubi);
378         return count;
379 }
380
381 /* File operations for all UBI debugfs files */
382 static const struct file_operations dfs_fops = {
383         .read   = dfs_file_read,
384         .write  = dfs_file_write,
385         .open   = simple_open,
386         .llseek = no_llseek,
387         .owner  = THIS_MODULE,
388 };
389
390 /**
391  * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
392  * @ubi: UBI device description object
393  *
394  * This function creates all debugfs files for UBI device @ubi. Returns zero in
395  * case of success and a negative error code in case of failure.
396  */
397 int ubi_debugfs_init_dev(struct ubi_device *ubi)
398 {
399         int err, n;
400         unsigned long ubi_num = ubi->ubi_num;
401         const char *fname;
402         struct dentry *dent;
403         struct ubi_debug_info *d = &ubi->dbg;
404
405         if (!IS_ENABLED(CONFIG_DEBUG_FS))
406                 return 0;
407
408         n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
409                      ubi->ubi_num);
410         if (n == UBI_DFS_DIR_LEN) {
411                 /* The array size is too small */
412                 fname = UBI_DFS_DIR_NAME;
413                 dent = ERR_PTR(-EINVAL);
414                 goto out;
415         }
416
417         fname = d->dfs_dir_name;
418         dent = debugfs_create_dir(fname, dfs_rootdir);
419         if (IS_ERR_OR_NULL(dent))
420                 goto out;
421         d->dfs_dir = dent;
422
423         fname = "chk_gen";
424         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
425                                    &dfs_fops);
426         if (IS_ERR_OR_NULL(dent))
427                 goto out_remove;
428         d->dfs_chk_gen = dent;
429
430         fname = "chk_io";
431         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
432                                    &dfs_fops);
433         if (IS_ERR_OR_NULL(dent))
434                 goto out_remove;
435         d->dfs_chk_io = dent;
436
437         fname = "chk_fastmap";
438         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
439                                    &dfs_fops);
440         if (IS_ERR_OR_NULL(dent))
441                 goto out_remove;
442         d->dfs_chk_fastmap = dent;
443
444         fname = "tst_disable_bgt";
445         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
446                                    &dfs_fops);
447         if (IS_ERR_OR_NULL(dent))
448                 goto out_remove;
449         d->dfs_disable_bgt = dent;
450
451         fname = "tst_emulate_bitflips";
452         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
453                                    &dfs_fops);
454         if (IS_ERR_OR_NULL(dent))
455                 goto out_remove;
456         d->dfs_emulate_bitflips = dent;
457
458         fname = "tst_emulate_io_failures";
459         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
460                                    &dfs_fops);
461         if (IS_ERR_OR_NULL(dent))
462                 goto out_remove;
463         d->dfs_emulate_io_failures = dent;
464
465         fname = "tst_emulate_power_cut";
466         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
467                                    &dfs_fops);
468         if (IS_ERR_OR_NULL(dent))
469                 goto out_remove;
470         d->dfs_emulate_power_cut = dent;
471
472         fname = "tst_emulate_power_cut_min";
473         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
474                                    &dfs_fops);
475         if (IS_ERR_OR_NULL(dent))
476                 goto out_remove;
477         d->dfs_power_cut_min = dent;
478
479         fname = "tst_emulate_power_cut_max";
480         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
481                                    &dfs_fops);
482         if (IS_ERR_OR_NULL(dent))
483                 goto out_remove;
484         d->dfs_power_cut_max = dent;
485
486         return 0;
487
488 out_remove:
489         debugfs_remove_recursive(d->dfs_dir);
490 out:
491         err = dent ? PTR_ERR(dent) : -ENODEV;
492         ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
493                 fname, err);
494         return err;
495 }
496
497 /**
498  * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
499  * @ubi: UBI device description object
500  */
501 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
502 {
503         if (IS_ENABLED(CONFIG_DEBUG_FS))
504                 debugfs_remove_recursive(ubi->dbg.dfs_dir);
505 }
506
507 /**
508  * ubi_dbg_power_cut - emulate a power cut if it is time to do so
509  * @ubi: UBI device description object
510  * @caller: Flags set to indicate from where the function is being called
511  *
512  * Returns non-zero if a power cut was emulated, zero if not.
513  */
514 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
515 {
516         unsigned int range;
517
518         if ((ubi->dbg.emulate_power_cut & caller) == 0)
519                 return 0;
520
521         if (ubi->dbg.power_cut_counter == 0) {
522                 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
523
524                 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
525                         range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
526                         ubi->dbg.power_cut_counter += prandom_u32() % range;
527                 }
528                 return 0;
529         }
530
531         ubi->dbg.power_cut_counter--;
532         if (ubi->dbg.power_cut_counter)
533                 return 0;
534
535         ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
536         ubi_ro_mode(ubi);
537         return 1;
538 }
539 #else
540 int ubi_debugfs_init(void)
541 {
542         return 0;
543 }
544
545 void ubi_debugfs_exit(void)
546 {
547 }
548
549 int ubi_debugfs_init_dev(struct ubi_device *ubi)
550 {
551         return 0;
552 }
553
554 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
555 {
556 }
557
558 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
559 {
560         return 0;
561 }
562 #endif