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