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