Commit | Line | Data |
---|---|---|
801c135c AB |
1 | /* |
2 | * Copyright (c) International Business Machines Corp., 2006 | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
12 | * the GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | * Author: Artem Bityutskiy (Битюцкий Артём) | |
19 | */ | |
20 | ||
801c135c | 21 | #include "ubi.h" |
2a734bb8 AB |
22 | #include <linux/debugfs.h> |
23 | #include <linux/uaccess.h> | |
b342efd4 | 24 | #include <linux/module.h> |
801c135c | 25 | |
ef7088e7 AB |
26 | |
27 | /** | |
28 | * ubi_dump_flash - dump a region of flash. | |
29 | * @ubi: UBI device description object | |
30 | * @pnum: the physical eraseblock number to dump | |
31 | * @offset: the starting offset within the physical eraseblock to dump | |
32 | * @len: the length of the region to dump | |
33 | */ | |
34 | void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len) | |
35 | { | |
36 | int err; | |
37 | size_t read; | |
38 | void *buf; | |
39 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; | |
40 | ||
41 | buf = vmalloc(len); | |
42 | if (!buf) | |
43 | return; | |
44 | err = mtd_read(ubi->mtd, addr, len, &read, buf); | |
45 | if (err && err != -EUCLEAN) { | |
049333ce AB |
46 | ubi_err("error %d while reading %d bytes from PEB %d:%d, read %zd bytes", |
47 | err, len, pnum, offset, read); | |
ef7088e7 AB |
48 | goto out; |
49 | } | |
50 | ||
51 | ubi_msg("dumping %d bytes of data from PEB %d, offset %d", | |
52 | len, pnum, offset); | |
53 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); | |
54 | out: | |
55 | vfree(buf); | |
56 | return; | |
57 | } | |
58 | ||
801c135c | 59 | /** |
a904e3f1 | 60 | * ubi_dump_ec_hdr - dump an erase counter header. |
801c135c AB |
61 | * @ec_hdr: the erase counter header to dump |
62 | */ | |
a904e3f1 | 63 | void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) |
801c135c | 64 | { |
e28453bb AB |
65 | pr_err("Erase counter header dump:\n"); |
66 | pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic)); | |
67 | pr_err("\tversion %d\n", (int)ec_hdr->version); | |
68 | pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec)); | |
69 | pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset)); | |
70 | pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset)); | |
71 | pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq)); | |
72 | pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc)); | |
73 | pr_err("erase counter header hexdump:\n"); | |
6986646b AB |
74 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
75 | ec_hdr, UBI_EC_HDR_SIZE, 1); | |
801c135c AB |
76 | } |
77 | ||
78 | /** | |
a904e3f1 | 79 | * ubi_dump_vid_hdr - dump a volume identifier header. |
801c135c AB |
80 | * @vid_hdr: the volume identifier header to dump |
81 | */ | |
a904e3f1 | 82 | void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) |
801c135c | 83 | { |
e28453bb AB |
84 | pr_err("Volume identifier header dump:\n"); |
85 | pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic)); | |
86 | pr_err("\tversion %d\n", (int)vid_hdr->version); | |
87 | pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type); | |
88 | pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag); | |
89 | pr_err("\tcompat %d\n", (int)vid_hdr->compat); | |
90 | pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id)); | |
91 | pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum)); | |
92 | pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size)); | |
93 | pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs)); | |
94 | pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad)); | |
95 | pr_err("\tsqnum %llu\n", | |
3261ebd7 | 96 | (unsigned long long)be64_to_cpu(vid_hdr->sqnum)); |
e28453bb AB |
97 | pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc)); |
98 | pr_err("Volume identifier header hexdump:\n"); | |
c8566350 AB |
99 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
100 | vid_hdr, UBI_VID_HDR_SIZE, 1); | |
801c135c AB |
101 | } |
102 | ||
103 | /** | |
766381f0 | 104 | * ubi_dump_vol_info - dump volume information. |
801c135c AB |
105 | * @vol: UBI volume description object |
106 | */ | |
766381f0 | 107 | void ubi_dump_vol_info(const struct ubi_volume *vol) |
801c135c | 108 | { |
e28453bb AB |
109 | pr_err("Volume information dump:\n"); |
110 | pr_err("\tvol_id %d\n", vol->vol_id); | |
111 | pr_err("\treserved_pebs %d\n", vol->reserved_pebs); | |
112 | pr_err("\talignment %d\n", vol->alignment); | |
113 | pr_err("\tdata_pad %d\n", vol->data_pad); | |
114 | pr_err("\tvol_type %d\n", vol->vol_type); | |
115 | pr_err("\tname_len %d\n", vol->name_len); | |
116 | pr_err("\tusable_leb_size %d\n", vol->usable_leb_size); | |
117 | pr_err("\tused_ebs %d\n", vol->used_ebs); | |
118 | pr_err("\tused_bytes %lld\n", vol->used_bytes); | |
119 | pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes); | |
120 | pr_err("\tcorrupted %d\n", vol->corrupted); | |
121 | pr_err("\tupd_marker %d\n", vol->upd_marker); | |
801c135c AB |
122 | |
123 | if (vol->name_len <= UBI_VOL_NAME_MAX && | |
124 | strnlen(vol->name, vol->name_len + 1) == vol->name_len) { | |
e28453bb | 125 | pr_err("\tname %s\n", vol->name); |
801c135c | 126 | } else { |
e28453bb | 127 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
c8566350 AB |
128 | vol->name[0], vol->name[1], vol->name[2], |
129 | vol->name[3], vol->name[4]); | |
801c135c AB |
130 | } |
131 | } | |
132 | ||
133 | /** | |
1f021e1d | 134 | * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object. |
801c135c AB |
135 | * @r: the object to dump |
136 | * @idx: volume table index | |
137 | */ | |
1f021e1d | 138 | void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) |
801c135c | 139 | { |
3261ebd7 | 140 | int name_len = be16_to_cpu(r->name_len); |
801c135c | 141 | |
e28453bb AB |
142 | pr_err("Volume table record %d dump:\n", idx); |
143 | pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs)); | |
144 | pr_err("\talignment %d\n", be32_to_cpu(r->alignment)); | |
145 | pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad)); | |
146 | pr_err("\tvol_type %d\n", (int)r->vol_type); | |
147 | pr_err("\tupd_marker %d\n", (int)r->upd_marker); | |
148 | pr_err("\tname_len %d\n", name_len); | |
801c135c AB |
149 | |
150 | if (r->name[0] == '\0') { | |
e28453bb | 151 | pr_err("\tname NULL\n"); |
801c135c AB |
152 | return; |
153 | } | |
154 | ||
155 | if (name_len <= UBI_VOL_NAME_MAX && | |
156 | strnlen(&r->name[0], name_len + 1) == name_len) { | |
e28453bb | 157 | pr_err("\tname %s\n", &r->name[0]); |
801c135c | 158 | } else { |
e28453bb | 159 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
801c135c AB |
160 | r->name[0], r->name[1], r->name[2], r->name[3], |
161 | r->name[4]); | |
162 | } | |
e28453bb | 163 | pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc)); |
801c135c AB |
164 | } |
165 | ||
166 | /** | |
517af48c AB |
167 | * ubi_dump_av - dump a &struct ubi_ainf_volume object. |
168 | * @av: the object to dump | |
801c135c | 169 | */ |
517af48c | 170 | void ubi_dump_av(const struct ubi_ainf_volume *av) |
801c135c | 171 | { |
e28453bb AB |
172 | pr_err("Volume attaching information dump:\n"); |
173 | pr_err("\tvol_id %d\n", av->vol_id); | |
174 | pr_err("\thighest_lnum %d\n", av->highest_lnum); | |
175 | pr_err("\tleb_count %d\n", av->leb_count); | |
176 | pr_err("\tcompat %d\n", av->compat); | |
177 | pr_err("\tvol_type %d\n", av->vol_type); | |
178 | pr_err("\tused_ebs %d\n", av->used_ebs); | |
179 | pr_err("\tlast_data_size %d\n", av->last_data_size); | |
180 | pr_err("\tdata_pad %d\n", av->data_pad); | |
801c135c AB |
181 | } |
182 | ||
183 | /** | |
2c5ec5ce AB |
184 | * ubi_dump_aeb - dump a &struct ubi_ainf_peb object. |
185 | * @aeb: the object to dump | |
801c135c AB |
186 | * @type: object type: 0 - not corrupted, 1 - corrupted |
187 | */ | |
2c5ec5ce | 188 | void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type) |
801c135c | 189 | { |
e28453bb AB |
190 | pr_err("eraseblock attaching information dump:\n"); |
191 | pr_err("\tec %d\n", aeb->ec); | |
192 | pr_err("\tpnum %d\n", aeb->pnum); | |
801c135c | 193 | if (type == 0) { |
e28453bb AB |
194 | pr_err("\tlnum %d\n", aeb->lnum); |
195 | pr_err("\tscrub %d\n", aeb->scrub); | |
196 | pr_err("\tsqnum %llu\n", aeb->sqnum); | |
801c135c AB |
197 | } |
198 | } | |
199 | ||
200 | /** | |
718c00bb | 201 | * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object. |
801c135c AB |
202 | * @req: the object to dump |
203 | */ | |
718c00bb | 204 | void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req) |
801c135c AB |
205 | { |
206 | char nm[17]; | |
207 | ||
e28453bb AB |
208 | pr_err("Volume creation request dump:\n"); |
209 | pr_err("\tvol_id %d\n", req->vol_id); | |
210 | pr_err("\talignment %d\n", req->alignment); | |
211 | pr_err("\tbytes %lld\n", (long long)req->bytes); | |
212 | pr_err("\tvol_type %d\n", req->vol_type); | |
213 | pr_err("\tname_len %d\n", req->name_len); | |
801c135c AB |
214 | |
215 | memcpy(nm, req->name, 16); | |
216 | nm[16] = 0; | |
e28453bb | 217 | pr_err("\t1st 16 characters of name: %s\n", nm); |
801c135c AB |
218 | } |
219 | ||
2a734bb8 AB |
220 | /* |
221 | * Root directory for UBI stuff in debugfs. Contains sub-directories which | |
222 | * contain the stuff specific to particular UBI devices. | |
223 | */ | |
224 | static struct dentry *dfs_rootdir; | |
225 | ||
226 | /** | |
227 | * ubi_debugfs_init - create UBI debugfs directory. | |
228 | * | |
229 | * Create UBI debugfs directory. Returns zero in case of success and a negative | |
230 | * error code in case of failure. | |
231 | */ | |
232 | int ubi_debugfs_init(void) | |
233 | { | |
903e0e4e | 234 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 AB |
235 | return 0; |
236 | ||
2a734bb8 AB |
237 | dfs_rootdir = debugfs_create_dir("ubi", NULL); |
238 | if (IS_ERR_OR_NULL(dfs_rootdir)) { | |
239 | int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir); | |
240 | ||
241 | ubi_err("cannot create \"ubi\" debugfs directory, error %d\n", | |
242 | err); | |
243 | return err; | |
244 | } | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | /** | |
250 | * ubi_debugfs_exit - remove UBI debugfs directory. | |
251 | */ | |
252 | void ubi_debugfs_exit(void) | |
253 | { | |
903e0e4e | 254 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 | 255 | debugfs_remove(dfs_rootdir); |
2a734bb8 AB |
256 | } |
257 | ||
258 | /* Read an UBI debugfs file */ | |
259 | static ssize_t dfs_file_read(struct file *file, char __user *user_buf, | |
260 | size_t count, loff_t *ppos) | |
261 | { | |
262 | unsigned long ubi_num = (unsigned long)file->private_data; | |
263 | struct dentry *dent = file->f_path.dentry; | |
264 | struct ubi_device *ubi; | |
265 | struct ubi_debug_info *d; | |
266 | char buf[3]; | |
267 | int val; | |
268 | ||
269 | ubi = ubi_get_device(ubi_num); | |
270 | if (!ubi) | |
271 | return -ENODEV; | |
eab73772 | 272 | d = &ubi->dbg; |
2a734bb8 AB |
273 | |
274 | if (dent == d->dfs_chk_gen) | |
275 | val = d->chk_gen; | |
276 | else if (dent == d->dfs_chk_io) | |
277 | val = d->chk_io; | |
cd6d8567 AB |
278 | else if (dent == d->dfs_disable_bgt) |
279 | val = d->disable_bgt; | |
280 | else if (dent == d->dfs_emulate_bitflips) | |
281 | val = d->emulate_bitflips; | |
282 | else if (dent == d->dfs_emulate_io_failures) | |
283 | val = d->emulate_io_failures; | |
2a734bb8 AB |
284 | else { |
285 | count = -EINVAL; | |
286 | goto out; | |
287 | } | |
288 | ||
289 | if (val) | |
290 | buf[0] = '1'; | |
291 | else | |
292 | buf[0] = '0'; | |
293 | buf[1] = '\n'; | |
294 | buf[2] = 0x00; | |
295 | ||
296 | count = simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
297 | ||
298 | out: | |
299 | ubi_put_device(ubi); | |
300 | return count; | |
301 | } | |
302 | ||
303 | /* Write an UBI debugfs file */ | |
304 | static ssize_t dfs_file_write(struct file *file, const char __user *user_buf, | |
305 | size_t count, loff_t *ppos) | |
306 | { | |
307 | unsigned long ubi_num = (unsigned long)file->private_data; | |
308 | struct dentry *dent = file->f_path.dentry; | |
309 | struct ubi_device *ubi; | |
310 | struct ubi_debug_info *d; | |
311 | size_t buf_size; | |
312 | char buf[8]; | |
313 | int val; | |
314 | ||
315 | ubi = ubi_get_device(ubi_num); | |
316 | if (!ubi) | |
317 | return -ENODEV; | |
eab73772 | 318 | d = &ubi->dbg; |
2a734bb8 AB |
319 | |
320 | buf_size = min_t(size_t, count, (sizeof(buf) - 1)); | |
321 | if (copy_from_user(buf, user_buf, buf_size)) { | |
322 | count = -EFAULT; | |
323 | goto out; | |
324 | } | |
325 | ||
326 | if (buf[0] == '1') | |
327 | val = 1; | |
328 | else if (buf[0] == '0') | |
329 | val = 0; | |
330 | else { | |
331 | count = -EINVAL; | |
332 | goto out; | |
333 | } | |
334 | ||
335 | if (dent == d->dfs_chk_gen) | |
336 | d->chk_gen = val; | |
337 | else if (dent == d->dfs_chk_io) | |
338 | d->chk_io = val; | |
cd6d8567 AB |
339 | else if (dent == d->dfs_disable_bgt) |
340 | d->disable_bgt = val; | |
341 | else if (dent == d->dfs_emulate_bitflips) | |
342 | d->emulate_bitflips = val; | |
343 | else if (dent == d->dfs_emulate_io_failures) | |
344 | d->emulate_io_failures = val; | |
2a734bb8 AB |
345 | else |
346 | count = -EINVAL; | |
347 | ||
348 | out: | |
349 | ubi_put_device(ubi); | |
350 | return count; | |
351 | } | |
352 | ||
2a734bb8 AB |
353 | /* File operations for all UBI debugfs files */ |
354 | static const struct file_operations dfs_fops = { | |
355 | .read = dfs_file_read, | |
356 | .write = dfs_file_write, | |
234e3405 | 357 | .open = simple_open, |
2a734bb8 AB |
358 | .llseek = no_llseek, |
359 | .owner = THIS_MODULE, | |
360 | }; | |
361 | ||
362 | /** | |
363 | * ubi_debugfs_init_dev - initialize debugfs for an UBI device. | |
364 | * @ubi: UBI device description object | |
365 | * | |
366 | * This function creates all debugfs files for UBI device @ubi. Returns zero in | |
367 | * case of success and a negative error code in case of failure. | |
368 | */ | |
369 | int ubi_debugfs_init_dev(struct ubi_device *ubi) | |
370 | { | |
371 | int err, n; | |
372 | unsigned long ubi_num = ubi->ubi_num; | |
373 | const char *fname; | |
374 | struct dentry *dent; | |
eab73772 | 375 | struct ubi_debug_info *d = &ubi->dbg; |
2a734bb8 | 376 | |
903e0e4e | 377 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 AB |
378 | return 0; |
379 | ||
2a734bb8 AB |
380 | n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME, |
381 | ubi->ubi_num); | |
382 | if (n == UBI_DFS_DIR_LEN) { | |
383 | /* The array size is too small */ | |
384 | fname = UBI_DFS_DIR_NAME; | |
385 | dent = ERR_PTR(-EINVAL); | |
386 | goto out; | |
387 | } | |
388 | ||
389 | fname = d->dfs_dir_name; | |
390 | dent = debugfs_create_dir(fname, dfs_rootdir); | |
391 | if (IS_ERR_OR_NULL(dent)) | |
392 | goto out; | |
393 | d->dfs_dir = dent; | |
394 | ||
395 | fname = "chk_gen"; | |
396 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
397 | &dfs_fops); | |
398 | if (IS_ERR_OR_NULL(dent)) | |
399 | goto out_remove; | |
400 | d->dfs_chk_gen = dent; | |
401 | ||
402 | fname = "chk_io"; | |
403 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
404 | &dfs_fops); | |
405 | if (IS_ERR_OR_NULL(dent)) | |
406 | goto out_remove; | |
407 | d->dfs_chk_io = dent; | |
408 | ||
cd6d8567 AB |
409 | fname = "tst_disable_bgt"; |
410 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
411 | &dfs_fops); | |
412 | if (IS_ERR_OR_NULL(dent)) | |
413 | goto out_remove; | |
414 | d->dfs_disable_bgt = dent; | |
415 | ||
416 | fname = "tst_emulate_bitflips"; | |
417 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
418 | &dfs_fops); | |
419 | if (IS_ERR_OR_NULL(dent)) | |
420 | goto out_remove; | |
421 | d->dfs_emulate_bitflips = dent; | |
422 | ||
423 | fname = "tst_emulate_io_failures"; | |
424 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
425 | &dfs_fops); | |
426 | if (IS_ERR_OR_NULL(dent)) | |
427 | goto out_remove; | |
428 | d->dfs_emulate_io_failures = dent; | |
429 | ||
2a734bb8 AB |
430 | return 0; |
431 | ||
432 | out_remove: | |
433 | debugfs_remove_recursive(d->dfs_dir); | |
434 | out: | |
435 | err = dent ? PTR_ERR(dent) : -ENODEV; | |
436 | ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n", | |
437 | fname, err); | |
438 | return err; | |
439 | } | |
440 | ||
441 | /** | |
442 | * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi | |
443 | * @ubi: UBI device description object | |
444 | */ | |
445 | void ubi_debugfs_exit_dev(struct ubi_device *ubi) | |
446 | { | |
903e0e4e | 447 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
eab73772 | 448 | debugfs_remove_recursive(ubi->dbg.dfs_dir); |
2a734bb8 | 449 | } |