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