debug.c 12 KB

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