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