2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Author: Artem Bityutskiy (Битюцкий Артём)
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
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
25 void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
30 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
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);
42 ubi_msg("dumping %d bytes of data from PEB %d, offset %d",
44 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
51 * ubi_dump_ec_hdr - dump an erase counter header.
52 * @ec_hdr: the erase counter header to dump
54 void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
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);
70 * ubi_dump_vid_hdr - dump a volume identifier header.
71 * @vid_hdr: the volume identifier header to dump
73 void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
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);
95 * ubi_dump_vol_info - dump volume information.
96 * @vol: UBI volume description object
98 void ubi_dump_vol_info(const struct ubi_volume *vol)
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);
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);
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]);
125 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
126 * @r: the object to dump
127 * @idx: volume table index
129 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
131 int name_len = be16_to_cpu(r->name_len);
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);
141 if (r->name[0] == '\0') {
142 pr_err("\tname NULL\n");
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]);
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],
154 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
158 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
159 * @av: the object to dump
161 void ubi_dump_av(const struct ubi_ainf_volume *av)
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);
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
179 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
181 pr_err("eraseblock attaching information dump:\n");
182 pr_err("\tec %d\n", aeb->ec);
183 pr_err("\tpnum %d\n", aeb->pnum);
185 pr_err("\tlnum %d\n", aeb->lnum);
186 pr_err("\tscrub %d\n", aeb->scrub);
187 pr_err("\tsqnum %llu\n", aeb->sqnum);
192 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
193 * @req: the object to dump
195 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
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);
206 memcpy(nm, req->name, 16);
208 pr_err("\t1st 16 characters of name: %s\n", nm);
213 * Root directory for UBI stuff in debugfs. Contains sub-directories which
214 * contain the stuff specific to particular UBI devices.
216 static struct dentry *dfs_rootdir;
219 * ubi_debugfs_init - create UBI debugfs directory.
221 * Create UBI debugfs directory. Returns zero in case of success and a negative
222 * error code in case of failure.
224 int ubi_debugfs_init(void)
226 if (!IS_ENABLED(CONFIG_DEBUG_FS))
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);
233 ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
242 * ubi_debugfs_exit - remove UBI debugfs directory.
244 void ubi_debugfs_exit(void)
246 if (IS_ENABLED(CONFIG_DEBUG_FS))
247 debugfs_remove(dfs_rootdir);
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)
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;
261 ubi = ubi_get_device(ubi_num);
266 if (dent == d->dfs_chk_gen)
268 else if (dent == d->dfs_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;
288 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
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)
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;
307 ubi = ubi_get_device(ubi_num);
312 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
313 if (copy_from_user(buf, user_buf, buf_size)) {
320 else if (buf[0] == '0')
327 if (dent == d->dfs_chk_gen)
329 else if (dent == d->dfs_chk_io)
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;
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,
351 .owner = THIS_MODULE,
355 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
356 * @ubi: UBI device description object
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.
361 int ubi_debugfs_init_dev(struct ubi_device *ubi)
364 unsigned long ubi_num = ubi->ubi_num;
367 struct ubi_debug_info *d = &ubi->dbg;
369 if (!IS_ENABLED(CONFIG_DEBUG_FS))
372 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
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);
381 fname = d->dfs_dir_name;
382 dent = debugfs_create_dir(fname, dfs_rootdir);
383 if (IS_ERR_OR_NULL(dent))
388 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
390 if (IS_ERR_OR_NULL(dent))
392 d->dfs_chk_gen = dent;
395 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
397 if (IS_ERR_OR_NULL(dent))
399 d->dfs_chk_io = dent;
401 fname = "tst_disable_bgt";
402 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
404 if (IS_ERR_OR_NULL(dent))
406 d->dfs_disable_bgt = dent;
408 fname = "tst_emulate_bitflips";
409 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
411 if (IS_ERR_OR_NULL(dent))
413 d->dfs_emulate_bitflips = dent;
415 fname = "tst_emulate_io_failures";
416 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
418 if (IS_ERR_OR_NULL(dent))
420 d->dfs_emulate_io_failures = dent;
425 debugfs_remove_recursive(d->dfs_dir);
427 err = dent ? PTR_ERR(dent) : -ENODEV;
428 ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
434 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
435 * @ubi: UBI device description object
437 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
439 if (IS_ENABLED(CONFIG_DEBUG_FS))
440 debugfs_remove_recursive(ubi->dbg.dfs_dir);
443 int ubi_debugfs_init(void)
448 void ubi_debugfs_exit(void)
452 int ubi_debugfs_init_dev(struct ubi_device *ubi)
457 void ubi_debugfs_exit_dev(struct ubi_device *ubi)