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 */
ubi_dump_flash(struct ubi_device * ubi,int pnum,int offset,int len)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 */
ubi_dump_ec_hdr(const struct ubi_ec_hdr * ec_hdr)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 */
ubi_dump_vid_hdr(const struct ubi_vid_hdr * vid_hdr)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 */
ubi_dump_vol_info(const struct ubi_volume * vol)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 printf("\tskip_check %d\n", vol->skip_check);
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 */
ubi_dump_vtbl_record(const struct ubi_vtbl_record * r,int idx)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 */
ubi_dump_av(const struct ubi_ainf_volume * av)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 */
ubi_dump_aeb(const struct ubi_ainf_peb * aeb,int type)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 */
ubi_dump_mkvol_req(const struct ubi_mkvol_req * req)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 */
ubi_debugfs_init(void)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 pr_err("UBI error: 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 */
ubi_debugfs_exit(void)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 */
dfs_file_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)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[8];
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_chk_fastmap)
271 val = d->chk_fastmap;
272 else if (dent == d->dfs_disable_bgt)
273 val = d->disable_bgt;
274 else if (dent == d->dfs_emulate_bitflips)
275 val = d->emulate_bitflips;
276 else if (dent == d->dfs_emulate_io_failures)
277 val = d->emulate_io_failures;
278 else if (dent == d->dfs_emulate_power_cut) {
279 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
280 count = simple_read_from_buffer(user_buf, count, ppos,
281 buf, strlen(buf));
282 goto out;
283 } else if (dent == d->dfs_power_cut_min) {
284 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
285 count = simple_read_from_buffer(user_buf, count, ppos,
286 buf, strlen(buf));
287 goto out;
288 } else if (dent == d->dfs_power_cut_max) {
289 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
290 count = simple_read_from_buffer(user_buf, count, ppos,
291 buf, strlen(buf));
292 goto out;
293 }
294 else {
295 count = -EINVAL;
296 goto out;
297 }
298
299 if (val)
300 buf[0] = '1';
301 else
302 buf[0] = '0';
303 buf[1] = '\n';
304 buf[2] = 0x00;
305
306 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
307
308 out:
309 ubi_put_device(ubi);
310 return count;
311 }
312
313 /* Write an UBI debugfs file */
dfs_file_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)314 static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
315 size_t count, loff_t *ppos)
316 {
317 unsigned long ubi_num = (unsigned long)file->private_data;
318 struct dentry *dent = file->f_path.dentry;
319 struct ubi_device *ubi;
320 struct ubi_debug_info *d;
321 size_t buf_size;
322 char buf[8] = {0};
323 int val;
324
325 ubi = ubi_get_device(ubi_num);
326 if (!ubi)
327 return -ENODEV;
328 d = &ubi->dbg;
329
330 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
331 if (copy_from_user(buf, user_buf, buf_size)) {
332 count = -EFAULT;
333 goto out;
334 }
335
336 if (dent == d->dfs_power_cut_min) {
337 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
338 count = -EINVAL;
339 goto out;
340 } else if (dent == d->dfs_power_cut_max) {
341 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
342 count = -EINVAL;
343 goto out;
344 } else if (dent == d->dfs_emulate_power_cut) {
345 if (kstrtoint(buf, 0, &val) != 0)
346 count = -EINVAL;
347 d->emulate_power_cut = val;
348 goto out;
349 }
350
351 if (buf[0] == '1')
352 val = 1;
353 else if (buf[0] == '0')
354 val = 0;
355 else {
356 count = -EINVAL;
357 goto out;
358 }
359
360 if (dent == d->dfs_chk_gen)
361 d->chk_gen = val;
362 else if (dent == d->dfs_chk_io)
363 d->chk_io = val;
364 else if (dent == d->dfs_chk_fastmap)
365 d->chk_fastmap = val;
366 else if (dent == d->dfs_disable_bgt)
367 d->disable_bgt = val;
368 else if (dent == d->dfs_emulate_bitflips)
369 d->emulate_bitflips = val;
370 else if (dent == d->dfs_emulate_io_failures)
371 d->emulate_io_failures = val;
372 else
373 count = -EINVAL;
374
375 out:
376 ubi_put_device(ubi);
377 return count;
378 }
379
380 /* File operations for all UBI debugfs files */
381 static const struct file_operations dfs_fops = {
382 .read = dfs_file_read,
383 .write = dfs_file_write,
384 .open = simple_open,
385 .llseek = no_llseek,
386 .owner = THIS_MODULE,
387 };
388
389 /**
390 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
391 * @ubi: UBI device description object
392 *
393 * This function creates all debugfs files for UBI device @ubi. Returns zero in
394 * case of success and a negative error code in case of failure.
395 */
ubi_debugfs_init_dev(struct ubi_device * ubi)396 int ubi_debugfs_init_dev(struct ubi_device *ubi)
397 {
398 int err, n;
399 unsigned long ubi_num = ubi->ubi_num;
400 const char *fname;
401 struct dentry *dent;
402 struct ubi_debug_info *d = &ubi->dbg;
403
404 if (!IS_ENABLED(CONFIG_DEBUG_FS))
405 return 0;
406
407 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
408 ubi->ubi_num);
409 if (n == UBI_DFS_DIR_LEN) {
410 /* The array size is too small */
411 fname = UBI_DFS_DIR_NAME;
412 dent = ERR_PTR(-EINVAL);
413 goto out;
414 }
415
416 fname = d->dfs_dir_name;
417 dent = debugfs_create_dir(fname, dfs_rootdir);
418 if (IS_ERR_OR_NULL(dent))
419 goto out;
420 d->dfs_dir = dent;
421
422 fname = "chk_gen";
423 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
424 &dfs_fops);
425 if (IS_ERR_OR_NULL(dent))
426 goto out_remove;
427 d->dfs_chk_gen = dent;
428
429 fname = "chk_io";
430 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
431 &dfs_fops);
432 if (IS_ERR_OR_NULL(dent))
433 goto out_remove;
434 d->dfs_chk_io = dent;
435
436 fname = "chk_fastmap";
437 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
438 &dfs_fops);
439 if (IS_ERR_OR_NULL(dent))
440 goto out_remove;
441 d->dfs_chk_fastmap = dent;
442
443 fname = "tst_disable_bgt";
444 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
445 &dfs_fops);
446 if (IS_ERR_OR_NULL(dent))
447 goto out_remove;
448 d->dfs_disable_bgt = dent;
449
450 fname = "tst_emulate_bitflips";
451 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
452 &dfs_fops);
453 if (IS_ERR_OR_NULL(dent))
454 goto out_remove;
455 d->dfs_emulate_bitflips = dent;
456
457 fname = "tst_emulate_io_failures";
458 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
459 &dfs_fops);
460 if (IS_ERR_OR_NULL(dent))
461 goto out_remove;
462 d->dfs_emulate_io_failures = dent;
463
464 fname = "tst_emulate_power_cut";
465 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
466 &dfs_fops);
467 if (IS_ERR_OR_NULL(dent))
468 goto out_remove;
469 d->dfs_emulate_power_cut = dent;
470
471 fname = "tst_emulate_power_cut_min";
472 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
473 &dfs_fops);
474 if (IS_ERR_OR_NULL(dent))
475 goto out_remove;
476 d->dfs_power_cut_min = dent;
477
478 fname = "tst_emulate_power_cut_max";
479 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
480 &dfs_fops);
481 if (IS_ERR_OR_NULL(dent))
482 goto out_remove;
483 d->dfs_power_cut_max = dent;
484
485 return 0;
486
487 out_remove:
488 debugfs_remove_recursive(d->dfs_dir);
489 out:
490 err = dent ? PTR_ERR(dent) : -ENODEV;
491 ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
492 fname, err);
493 return err;
494 }
495
496 /**
497 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
498 * @ubi: UBI device description object
499 */
ubi_debugfs_exit_dev(struct ubi_device * ubi)500 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
501 {
502 if (IS_ENABLED(CONFIG_DEBUG_FS))
503 debugfs_remove_recursive(ubi->dbg.dfs_dir);
504 }
505
506 /**
507 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
508 * @ubi: UBI device description object
509 * @caller: Flags set to indicate from where the function is being called
510 *
511 * Returns non-zero if a power cut was emulated, zero if not.
512 */
ubi_dbg_power_cut(struct ubi_device * ubi,int caller)513 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
514 {
515 unsigned int range;
516
517 if ((ubi->dbg.emulate_power_cut & caller) == 0)
518 return 0;
519
520 if (ubi->dbg.power_cut_counter == 0) {
521 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
522
523 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
524 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
525 ubi->dbg.power_cut_counter += prandom_u32() % range;
526 }
527 return 0;
528 }
529
530 ubi->dbg.power_cut_counter--;
531 if (ubi->dbg.power_cut_counter)
532 return 0;
533
534 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
535 ubi_ro_mode(ubi);
536 return 1;
537 }
538 #else
ubi_debugfs_init(void)539 int ubi_debugfs_init(void)
540 {
541 return 0;
542 }
543
ubi_debugfs_exit(void)544 void ubi_debugfs_exit(void)
545 {
546 }
547
ubi_debugfs_init_dev(struct ubi_device * ubi)548 int ubi_debugfs_init_dev(struct ubi_device *ubi)
549 {
550 return 0;
551 }
552
ubi_debugfs_exit_dev(struct ubi_device * ubi)553 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
554 {
555 }
556
ubi_dbg_power_cut(struct ubi_device * ubi,int caller)557 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
558 {
559 return 0;
560 }
561 #endif
562