blob: af254da4883c375cc9dd39efdb4df43c6454ea46 [file] [log] [blame]
Kyungmin Park961df832008-11-19 16:25:44 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Park961df832008-11-19 16:25:44 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
Kyungmin Park961df832008-11-19 16:25:44 +01009#include <ubi_uboot.h>
Kyungmin Park961df832008-11-19 16:25:44 +010010#include "ubi.h"
Heiko Schocherff94bc42014-06-24 10:10:04 +020011#define __UBOOT__
12#ifndef __UBOOT__
13#include <linux/debugfs.h>
14#include <linux/uaccess.h>
15#include <linux/module.h>
16#endif
Kyungmin Park961df832008-11-19 16:25:44 +010017
18/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020019 * 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
24 */
25void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
26{
27 int err;
28 size_t read;
29 void *buf;
30 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
31
32 buf = vmalloc(len);
33 if (!buf)
34 return;
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);
39 goto out;
40 }
41
42 ubi_msg("dumping %d bytes of data from PEB %d, offset %d",
43 len, pnum, offset);
44 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
45out:
46 vfree(buf);
47 return;
48}
49
50/**
51 * ubi_dump_ec_hdr - dump an erase counter header.
Kyungmin Park961df832008-11-19 16:25:44 +010052 * @ec_hdr: the erase counter header to dump
53 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020054void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010055{
Heiko Schocherff94bc42014-06-24 10:10:04 +020056 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");
Kyungmin Park961df832008-11-19 16:25:44 +010065 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
66 ec_hdr, UBI_EC_HDR_SIZE, 1);
67}
68
69/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020070 * ubi_dump_vid_hdr - dump a volume identifier header.
Kyungmin Park961df832008-11-19 16:25:44 +010071 * @vid_hdr: the volume identifier header to dump
72 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020073void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010074{
Heiko Schocherff94bc42014-06-24 10:10:04 +020075 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",
Kyungmin Park961df832008-11-19 16:25:44 +010087 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Heiko Schocherff94bc42014-06-24 10:10:04 +020088 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);
Kyungmin Park961df832008-11-19 16:25:44 +010092}
93
94/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020095 * ubi_dump_vol_info - dump volume information.
Kyungmin Park961df832008-11-19 16:25:44 +010096 * @vol: UBI volume description object
97 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020098void ubi_dump_vol_info(const struct ubi_volume *vol)
Kyungmin Park961df832008-11-19 16:25:44 +010099{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200100 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);
Kyungmin Park961df832008-11-19 16:25:44 +0100113
114 if (vol->name_len <= UBI_VOL_NAME_MAX &&
115 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200116 printf("\tname %s\n", vol->name);
Kyungmin Park961df832008-11-19 16:25:44 +0100117 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200118 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]);
Kyungmin Park961df832008-11-19 16:25:44 +0100121 }
122}
123
124/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200125 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
Kyungmin Park961df832008-11-19 16:25:44 +0100126 * @r: the object to dump
127 * @idx: volume table index
128 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
Kyungmin Park961df832008-11-19 16:25:44 +0100130{
131 int name_len = be16_to_cpu(r->name_len);
132
Heiko Schocherff94bc42014-06-24 10:10:04 +0200133 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);
Kyungmin Park961df832008-11-19 16:25:44 +0100140
141 if (r->name[0] == '\0') {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200142 pr_err("\tname NULL\n");
Kyungmin Park961df832008-11-19 16:25:44 +0100143 return;
144 }
145
146 if (name_len <= UBI_VOL_NAME_MAX &&
147 strnlen(&r->name[0], name_len + 1) == name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200148 pr_err("\tname %s\n", &r->name[0]);
Kyungmin Park961df832008-11-19 16:25:44 +0100149 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200150 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
Kyungmin Park961df832008-11-19 16:25:44 +0100151 r->name[0], r->name[1], r->name[2], r->name[3],
152 r->name[4]);
153 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200154 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
Kyungmin Park961df832008-11-19 16:25:44 +0100155}
156
157/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200158 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
159 * @av: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100160 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200161void ubi_dump_av(const struct ubi_ainf_volume *av)
Kyungmin Park961df832008-11-19 16:25:44 +0100162{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200163 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);
Kyungmin Park961df832008-11-19 16:25:44 +0100172}
173
174/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200175 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
176 * @aeb: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100177 * @type: object type: 0 - not corrupted, 1 - corrupted
178 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200179void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
Kyungmin Park961df832008-11-19 16:25:44 +0100180{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200181 pr_err("eraseblock attaching information dump:\n");
182 pr_err("\tec %d\n", aeb->ec);
183 pr_err("\tpnum %d\n", aeb->pnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100184 if (type == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200185 pr_err("\tlnum %d\n", aeb->lnum);
186 pr_err("\tscrub %d\n", aeb->scrub);
187 pr_err("\tsqnum %llu\n", aeb->sqnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100188 }
189}
190
191/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200192 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
Kyungmin Park961df832008-11-19 16:25:44 +0100193 * @req: the object to dump
194 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200195void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
Kyungmin Park961df832008-11-19 16:25:44 +0100196{
197 char nm[17];
198
Heiko Schocherff94bc42014-06-24 10:10:04 +0200199 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);
Kyungmin Park961df832008-11-19 16:25:44 +0100205
206 memcpy(nm, req->name, 16);
207 nm[16] = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200208 pr_err("\t1st 16 characters of name: %s\n", nm);
Kyungmin Park961df832008-11-19 16:25:44 +0100209}
210
Heiko Schocherff94bc42014-06-24 10:10:04 +0200211#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 */
216static 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 */
224int 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 ubi_err("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 */
244void 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 */
251static 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[3];
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_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 {
277 count = -EINVAL;
278 goto out;
279 }
280
281 if (val)
282 buf[0] = '1';
283 else
284 buf[0] = '0';
285 buf[1] = '\n';
286 buf[2] = 0x00;
287
288 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
289
290out:
291 ubi_put_device(ubi);
292 return count;
293}
294
295/* Write an UBI debugfs file */
296static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
297 size_t count, loff_t *ppos)
298{
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;
303 size_t buf_size;
304 char buf[8];
305 int val;
306
307 ubi = ubi_get_device(ubi_num);
308 if (!ubi)
309 return -ENODEV;
310 d = &ubi->dbg;
311
312 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
313 if (copy_from_user(buf, user_buf, buf_size)) {
314 count = -EFAULT;
315 goto out;
316 }
317
318 if (buf[0] == '1')
319 val = 1;
320 else if (buf[0] == '0')
321 val = 0;
322 else {
323 count = -EINVAL;
324 goto out;
325 }
326
327 if (dent == d->dfs_chk_gen)
328 d->chk_gen = val;
329 else if (dent == d->dfs_chk_io)
330 d->chk_io = val;
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;
337 else
338 count = -EINVAL;
339
340out:
341 ubi_put_device(ubi);
342 return count;
343}
344
345/* File operations for all UBI debugfs files */
346static const struct file_operations dfs_fops = {
347 .read = dfs_file_read,
348 .write = dfs_file_write,
349 .open = simple_open,
350 .llseek = no_llseek,
351 .owner = THIS_MODULE,
352};
353
354/**
355 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
356 * @ubi: UBI device description object
357 *
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.
360 */
361int ubi_debugfs_init_dev(struct ubi_device *ubi)
362{
363 int err, n;
364 unsigned long ubi_num = ubi->ubi_num;
365 const char *fname;
366 struct dentry *dent;
367 struct ubi_debug_info *d = &ubi->dbg;
368
369 if (!IS_ENABLED(CONFIG_DEBUG_FS))
370 return 0;
371
372 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
373 ubi->ubi_num);
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);
378 goto out;
379 }
380
381 fname = d->dfs_dir_name;
382 dent = debugfs_create_dir(fname, dfs_rootdir);
383 if (IS_ERR_OR_NULL(dent))
384 goto out;
385 d->dfs_dir = dent;
386
387 fname = "chk_gen";
388 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
389 &dfs_fops);
390 if (IS_ERR_OR_NULL(dent))
391 goto out_remove;
392 d->dfs_chk_gen = dent;
393
394 fname = "chk_io";
395 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
396 &dfs_fops);
397 if (IS_ERR_OR_NULL(dent))
398 goto out_remove;
399 d->dfs_chk_io = dent;
400
401 fname = "tst_disable_bgt";
402 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
403 &dfs_fops);
404 if (IS_ERR_OR_NULL(dent))
405 goto out_remove;
406 d->dfs_disable_bgt = dent;
407
408 fname = "tst_emulate_bitflips";
409 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
410 &dfs_fops);
411 if (IS_ERR_OR_NULL(dent))
412 goto out_remove;
413 d->dfs_emulate_bitflips = dent;
414
415 fname = "tst_emulate_io_failures";
416 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
417 &dfs_fops);
418 if (IS_ERR_OR_NULL(dent))
419 goto out_remove;
420 d->dfs_emulate_io_failures = dent;
421
422 return 0;
423
424out_remove:
425 debugfs_remove_recursive(d->dfs_dir);
426out:
427 err = dent ? PTR_ERR(dent) : -ENODEV;
428 ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
429 fname, err);
430 return err;
431}
432
433/**
434 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
435 * @ubi: UBI device description object
436 */
437void ubi_debugfs_exit_dev(struct ubi_device *ubi)
438{
439 if (IS_ENABLED(CONFIG_DEBUG_FS))
440 debugfs_remove_recursive(ubi->dbg.dfs_dir);
441}
442#else
443int ubi_debugfs_init(void)
444{
445 return 0;
446}
447
448void ubi_debugfs_exit(void)
449{
450}
451
452int ubi_debugfs_init_dev(struct ubi_device *ubi)
453{
454 return 0;
455}
456
457void ubi_debugfs_exit_dev(struct ubi_device *ubi)
458{
459}
460#endif