debug.c 15 KB

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