]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/ubi/debug.c
Merge branch 'master' of git://git.denx.de/u-boot-arc
[karo-tx-uboot.git] / drivers / mtd / ubi / debug.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  *
6  * Author: Artem Bityutskiy (Битюцкий Артём)
7  */
8
9 #include <ubi_uboot.h>
10 #include "ubi.h"
11 #define __UBOOT__
12 #ifndef __UBOOT__
13 #include <linux/debugfs.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("error %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("dumping %d bytes of data from PEB %d, offset %d",
43                 len, pnum, offset);
44         print_hex_dump(KERN_DEBUG, "", 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(KERN_DEBUG, "", 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(KERN_DEBUG, "", 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
114         if (vol->name_len <= UBI_VOL_NAME_MAX &&
115             strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
116                 printf("\tname            %s\n", vol->name);
117         } else {
118                 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
119                        vol->name[0], vol->name[1], vol->name[2],
120                        vol->name[3], vol->name[4]);
121         }
122 }
123
124 /**
125  * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
126  * @r: the object to dump
127  * @idx: volume table index
128  */
129 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
130 {
131         int name_len = be16_to_cpu(r->name_len);
132
133         pr_err("Volume table record %d dump:\n", idx);
134         pr_err("\treserved_pebs   %d\n", be32_to_cpu(r->reserved_pebs));
135         pr_err("\talignment       %d\n", be32_to_cpu(r->alignment));
136         pr_err("\tdata_pad        %d\n", be32_to_cpu(r->data_pad));
137         pr_err("\tvol_type        %d\n", (int)r->vol_type);
138         pr_err("\tupd_marker      %d\n", (int)r->upd_marker);
139         pr_err("\tname_len        %d\n", name_len);
140
141         if (r->name[0] == '\0') {
142                 pr_err("\tname            NULL\n");
143                 return;
144         }
145
146         if (name_len <= UBI_VOL_NAME_MAX &&
147             strnlen(&r->name[0], name_len + 1) == name_len) {
148                 pr_err("\tname            %s\n", &r->name[0]);
149         } else {
150                 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
151                         r->name[0], r->name[1], r->name[2], r->name[3],
152                         r->name[4]);
153         }
154         pr_err("\tcrc             %#08x\n", be32_to_cpu(r->crc));
155 }
156
157 /**
158  * ubi_dump_av - dump a &struct ubi_ainf_volume object.
159  * @av: the object to dump
160  */
161 void ubi_dump_av(const struct ubi_ainf_volume *av)
162 {
163         pr_err("Volume attaching information dump:\n");
164         pr_err("\tvol_id         %d\n", av->vol_id);
165         pr_err("\thighest_lnum   %d\n", av->highest_lnum);
166         pr_err("\tleb_count      %d\n", av->leb_count);
167         pr_err("\tcompat         %d\n", av->compat);
168         pr_err("\tvol_type       %d\n", av->vol_type);
169         pr_err("\tused_ebs       %d\n", av->used_ebs);
170         pr_err("\tlast_data_size %d\n", av->last_data_size);
171         pr_err("\tdata_pad       %d\n", av->data_pad);
172 }
173
174 /**
175  * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
176  * @aeb: the object to dump
177  * @type: object type: 0 - not corrupted, 1 - corrupted
178  */
179 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
180 {
181         pr_err("eraseblock attaching information dump:\n");
182         pr_err("\tec       %d\n", aeb->ec);
183         pr_err("\tpnum     %d\n", aeb->pnum);
184         if (type == 0) {
185                 pr_err("\tlnum     %d\n", aeb->lnum);
186                 pr_err("\tscrub    %d\n", aeb->scrub);
187                 pr_err("\tsqnum    %llu\n", aeb->sqnum);
188         }
189 }
190
191 /**
192  * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
193  * @req: the object to dump
194  */
195 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
196 {
197         char nm[17];
198
199         pr_err("Volume creation request dump:\n");
200         pr_err("\tvol_id    %d\n",   req->vol_id);
201         pr_err("\talignment %d\n",   req->alignment);
202         pr_err("\tbytes     %lld\n", (long long)req->bytes);
203         pr_err("\tvol_type  %d\n",   req->vol_type);
204         pr_err("\tname_len  %d\n",   req->name_len);
205
206         memcpy(nm, req->name, 16);
207         nm[16] = 0;
208         pr_err("\t1st 16 characters of name: %s\n", nm);
209 }
210
211 #ifndef __UBOOT__
212 /*
213  * Root directory for UBI stuff in debugfs. Contains sub-directories which
214  * contain the stuff specific to particular UBI devices.
215  */
216 static struct dentry *dfs_rootdir;
217
218 /**
219  * ubi_debugfs_init - create UBI debugfs directory.
220  *
221  * Create UBI debugfs directory. Returns zero in case of success and a negative
222  * error code in case of failure.
223  */
224 int ubi_debugfs_init(void)
225 {
226         if (!IS_ENABLED(CONFIG_DEBUG_FS))
227                 return 0;
228
229         dfs_rootdir = debugfs_create_dir("ubi", NULL);
230         if (IS_ERR_OR_NULL(dfs_rootdir)) {
231                 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
232
233                 ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
234                         err);
235                 return err;
236         }
237
238         return 0;
239 }
240
241 /**
242  * ubi_debugfs_exit - remove UBI debugfs directory.
243  */
244 void ubi_debugfs_exit(void)
245 {
246         if (IS_ENABLED(CONFIG_DEBUG_FS))
247                 debugfs_remove(dfs_rootdir);
248 }
249
250 /* Read an UBI debugfs file */
251 static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
252                              size_t count, loff_t *ppos)
253 {
254         unsigned long ubi_num = (unsigned long)file->private_data;
255         struct dentry *dent = file->f_path.dentry;
256         struct ubi_device *ubi;
257         struct ubi_debug_info *d;
258         char buf[3];
259         int val;
260
261         ubi = ubi_get_device(ubi_num);
262         if (!ubi)
263                 return -ENODEV;
264         d = &ubi->dbg;
265
266         if (dent == d->dfs_chk_gen)
267                 val = d->chk_gen;
268         else if (dent == d->dfs_chk_io)
269                 val = d->chk_io;
270         else if (dent == d->dfs_disable_bgt)
271                 val = d->disable_bgt;
272         else if (dent == d->dfs_emulate_bitflips)
273                 val = d->emulate_bitflips;
274         else if (dent == d->dfs_emulate_io_failures)
275                 val = d->emulate_io_failures;
276         else {
277                 count = -EINVAL;
278                 goto out;
279         }
280
281         if (val)
282                 buf[0] = '1';
283         else
284                 buf[0] = '0';
285         buf[1] = '\n';
286         buf[2] = 0x00;
287
288         count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
289
290 out:
291         ubi_put_device(ubi);
292         return count;
293 }
294
295 /* Write an UBI debugfs file */
296 static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
297                               size_t count, loff_t *ppos)
298 {
299         unsigned long ubi_num = (unsigned long)file->private_data;
300         struct dentry *dent = file->f_path.dentry;
301         struct ubi_device *ubi;
302         struct ubi_debug_info *d;
303         size_t buf_size;
304         char buf[8];
305         int val;
306
307         ubi = ubi_get_device(ubi_num);
308         if (!ubi)
309                 return -ENODEV;
310         d = &ubi->dbg;
311
312         buf_size = min_t(size_t, count, (sizeof(buf) - 1));
313         if (copy_from_user(buf, user_buf, buf_size)) {
314                 count = -EFAULT;
315                 goto out;
316         }
317
318         if (buf[0] == '1')
319                 val = 1;
320         else if (buf[0] == '0')
321                 val = 0;
322         else {
323                 count = -EINVAL;
324                 goto out;
325         }
326
327         if (dent == d->dfs_chk_gen)
328                 d->chk_gen = val;
329         else if (dent == d->dfs_chk_io)
330                 d->chk_io = val;
331         else if (dent == d->dfs_disable_bgt)
332                 d->disable_bgt = val;
333         else if (dent == d->dfs_emulate_bitflips)
334                 d->emulate_bitflips = val;
335         else if (dent == d->dfs_emulate_io_failures)
336                 d->emulate_io_failures = val;
337         else
338                 count = -EINVAL;
339
340 out:
341         ubi_put_device(ubi);
342         return count;
343 }
344
345 /* File operations for all UBI debugfs files */
346 static const struct file_operations dfs_fops = {
347         .read   = dfs_file_read,
348         .write  = dfs_file_write,
349         .open   = simple_open,
350         .llseek = no_llseek,
351         .owner  = THIS_MODULE,
352 };
353
354 /**
355  * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
356  * @ubi: UBI device description object
357  *
358  * This function creates all debugfs files for UBI device @ubi. Returns zero in
359  * case of success and a negative error code in case of failure.
360  */
361 int ubi_debugfs_init_dev(struct ubi_device *ubi)
362 {
363         int err, n;
364         unsigned long ubi_num = ubi->ubi_num;
365         const char *fname;
366         struct dentry *dent;
367         struct ubi_debug_info *d = &ubi->dbg;
368
369         if (!IS_ENABLED(CONFIG_DEBUG_FS))
370                 return 0;
371
372         n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
373                      ubi->ubi_num);
374         if (n == UBI_DFS_DIR_LEN) {
375                 /* The array size is too small */
376                 fname = UBI_DFS_DIR_NAME;
377                 dent = ERR_PTR(-EINVAL);
378                 goto out;
379         }
380
381         fname = d->dfs_dir_name;
382         dent = debugfs_create_dir(fname, dfs_rootdir);
383         if (IS_ERR_OR_NULL(dent))
384                 goto out;
385         d->dfs_dir = dent;
386
387         fname = "chk_gen";
388         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
389                                    &dfs_fops);
390         if (IS_ERR_OR_NULL(dent))
391                 goto out_remove;
392         d->dfs_chk_gen = dent;
393
394         fname = "chk_io";
395         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
396                                    &dfs_fops);
397         if (IS_ERR_OR_NULL(dent))
398                 goto out_remove;
399         d->dfs_chk_io = dent;
400
401         fname = "tst_disable_bgt";
402         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
403                                    &dfs_fops);
404         if (IS_ERR_OR_NULL(dent))
405                 goto out_remove;
406         d->dfs_disable_bgt = dent;
407
408         fname = "tst_emulate_bitflips";
409         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
410                                    &dfs_fops);
411         if (IS_ERR_OR_NULL(dent))
412                 goto out_remove;
413         d->dfs_emulate_bitflips = dent;
414
415         fname = "tst_emulate_io_failures";
416         dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
417                                    &dfs_fops);
418         if (IS_ERR_OR_NULL(dent))
419                 goto out_remove;
420         d->dfs_emulate_io_failures = dent;
421
422         return 0;
423
424 out_remove:
425         debugfs_remove_recursive(d->dfs_dir);
426 out:
427         err = dent ? PTR_ERR(dent) : -ENODEV;
428         ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
429                 fname, err);
430         return err;
431 }
432
433 /**
434  * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
435  * @ubi: UBI device description object
436  */
437 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
438 {
439         if (IS_ENABLED(CONFIG_DEBUG_FS))
440                 debugfs_remove_recursive(ubi->dbg.dfs_dir);
441 }
442 #else
443 int ubi_debugfs_init(void)
444 {
445         return 0;
446 }
447
448 void ubi_debugfs_exit(void)
449 {
450 }
451
452 int ubi_debugfs_init_dev(struct ubi_device *ubi)
453 {
454         return 0;
455 }
456
457 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
458 {
459 }
460 #endif