Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 5 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 6 | */ |
| 7 | |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 8 | #include <hexdump.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 10 | #include <ubi_uboot.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 11 | #include <linux/printk.h> |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 12 | #include "ubi.h" |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 13 | #ifndef __UBOOT__ |
| 14 | #include <linux/debugfs.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
| 17 | #include <linux/module.h> |
| 18 | #endif |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 19 | |
| 20 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | */ |
| 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) { |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 39 | ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 40 | err, len, pnum, offset, read); |
| 41 | goto out; |
| 42 | } |
| 43 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 44 | ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 45 | len, pnum, offset); |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 46 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 47 | out: |
| 48 | vfree(buf); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * ubi_dump_ec_hdr - dump an erase counter header. |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 54 | * @ec_hdr: the erase counter header to dump |
| 55 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 56 | void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 57 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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"); |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 67 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 68 | ec_hdr, UBI_EC_HDR_SIZE, 1); |
| 69 | } |
| 70 | |
| 71 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 72 | * ubi_dump_vid_hdr - dump a volume identifier header. |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 73 | * @vid_hdr: the volume identifier header to dump |
| 74 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 75 | void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 76 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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", |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 89 | (unsigned long long)be64_to_cpu(vid_hdr->sqnum)); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 90 | pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc)); |
| 91 | pr_err("Volume identifier header hexdump:\n"); |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 92 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 93 | vid_hdr, UBI_VID_HDR_SIZE, 1); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 97 | * ubi_dump_vol_info - dump volume information. |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 98 | * @vol: UBI volume description object |
| 99 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 100 | void ubi_dump_vol_info(const struct ubi_volume *vol) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 101 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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); |
Stefan Roese | 524acec | 2019-09-12 16:41:02 +0200 | [diff] [blame] | 115 | printf("\tskip_check %d\n", vol->skip_check); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 116 | |
| 117 | if (vol->name_len <= UBI_VOL_NAME_MAX && |
| 118 | strnlen(vol->name, vol->name_len + 1) == vol->name_len) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 119 | printf("\tname %s\n", vol->name); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 120 | } else { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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]); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 128 | * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object. |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 129 | * @r: the object to dump |
| 130 | * @idx: volume table index |
| 131 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 132 | void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 133 | { |
| 134 | int name_len = be16_to_cpu(r->name_len); |
| 135 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 143 | |
| 144 | if (r->name[0] == '\0') { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 145 | pr_err("\tname NULL\n"); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (name_len <= UBI_VOL_NAME_MAX && |
| 150 | strnlen(&r->name[0], name_len + 1) == name_len) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 151 | pr_err("\tname %s\n", &r->name[0]); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 152 | } else { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 153 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 154 | r->name[0], r->name[1], r->name[2], r->name[3], |
| 155 | r->name[4]); |
| 156 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 157 | pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc)); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 161 | * ubi_dump_av - dump a &struct ubi_ainf_volume object. |
| 162 | * @av: the object to dump |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 163 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 164 | void ubi_dump_av(const struct ubi_ainf_volume *av) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 165 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 178 | * ubi_dump_aeb - dump a &struct ubi_ainf_peb object. |
| 179 | * @aeb: the object to dump |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 180 | * @type: object type: 0 - not corrupted, 1 - corrupted |
| 181 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 182 | void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 183 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 184 | pr_err("eraseblock attaching information dump:\n"); |
| 185 | pr_err("\tec %d\n", aeb->ec); |
| 186 | pr_err("\tpnum %d\n", aeb->pnum); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 187 | if (type == 0) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 188 | pr_err("\tlnum %d\n", aeb->lnum); |
| 189 | pr_err("\tscrub %d\n", aeb->scrub); |
| 190 | pr_err("\tsqnum %llu\n", aeb->sqnum); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 195 | * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object. |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 196 | * @req: the object to dump |
| 197 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 198 | void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req) |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 199 | { |
| 200 | char nm[17]; |
| 201 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 208 | |
| 209 | memcpy(nm, req->name, 16); |
| 210 | nm[16] = 0; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 211 | pr_err("\t1st 16 characters of name: %s\n", nm); |
Kyungmin Park | 961df83 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | */ |
| 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 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 236 | pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n", |
| 237 | err); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 238 | return err; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * ubi_debugfs_exit - remove UBI debugfs directory. |
| 246 | */ |
| 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 */ |
| 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; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 261 | char buf[8]; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 273 | else if (dent == d->dfs_chk_fastmap) |
| 274 | val = d->chk_fastmap; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 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 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 */ |
| 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; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 325 | char buf[8] = {0}; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 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 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 367 | else if (dent == d->dfs_chk_fastmap) |
| 368 | d->chk_fastmap = val; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | */ |
| 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 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 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 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 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 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 488 | return 0; |
| 489 | |
| 490 | out_remove: |
| 491 | debugfs_remove_recursive(d->dfs_dir); |
| 492 | out: |
| 493 | err = dent ? PTR_ERR(dent) : -ENODEV; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 494 | ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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 | */ |
| 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 | } |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 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 | */ |
| 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 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 541 | #else |
| 542 | int ubi_debugfs_init(void) |
| 543 | { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | void ubi_debugfs_exit(void) |
| 548 | { |
| 549 | } |
| 550 | |
| 551 | int ubi_debugfs_init_dev(struct ubi_device *ubi) |
| 552 | { |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | void ubi_debugfs_exit_dev(struct ubi_device *ubi) |
| 557 | { |
| 558 | } |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 559 | |
| 560 | int ubi_dbg_power_cut(struct ubi_device *ubi, int caller) |
| 561 | { |
| 562 | return 0; |
| 563 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 564 | #endif |