debug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <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(ubi, "err %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(ubi, "dumping %d bytes of data from PEB %d, offset %d",
  38. len, pnum, offset);
  39. print_hex_dump("", 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("", 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("", 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. printf("\tskip_check %d\n", vol->skip_check);
  106. if (vol->name_len <= UBI_VOL_NAME_MAX &&
  107. strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
  108. printf("\tname %s\n", vol->name);
  109. } else {
  110. printf("\t1st 5 characters of name: %c%c%c%c%c\n",
  111. vol->name[0], vol->name[1], vol->name[2],
  112. vol->name[3], vol->name[4]);
  113. }
  114. }
  115. /**
  116. * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
  117. * @r: the object to dump
  118. * @idx: volume table index
  119. */
  120. void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
  121. {
  122. int name_len = be16_to_cpu(r->name_len);
  123. pr_err("Volume table record %d dump:\n", idx);
  124. pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
  125. pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
  126. pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
  127. pr_err("\tvol_type %d\n", (int)r->vol_type);
  128. pr_err("\tupd_marker %d\n", (int)r->upd_marker);
  129. pr_err("\tname_len %d\n", name_len);
  130. if (r->name[0] == '\0') {
  131. pr_err("\tname NULL\n");
  132. return;
  133. }
  134. if (name_len <= UBI_VOL_NAME_MAX &&
  135. strnlen(&r->name[0], name_len + 1) == name_len) {
  136. pr_err("\tname %s\n", &r->name[0]);
  137. } else {
  138. pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
  139. r->name[0], r->name[1], r->name[2], r->name[3],
  140. r->name[4]);
  141. }
  142. pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
  143. }
  144. /**
  145. * ubi_dump_av - dump a &struct ubi_ainf_volume object.
  146. * @av: the object to dump
  147. */
  148. void ubi_dump_av(const struct ubi_ainf_volume *av)
  149. {
  150. pr_err("Volume attaching information dump:\n");
  151. pr_err("\tvol_id %d\n", av->vol_id);
  152. pr_err("\thighest_lnum %d\n", av->highest_lnum);
  153. pr_err("\tleb_count %d\n", av->leb_count);
  154. pr_err("\tcompat %d\n", av->compat);
  155. pr_err("\tvol_type %d\n", av->vol_type);
  156. pr_err("\tused_ebs %d\n", av->used_ebs);
  157. pr_err("\tlast_data_size %d\n", av->last_data_size);
  158. pr_err("\tdata_pad %d\n", av->data_pad);
  159. }
  160. /**
  161. * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
  162. * @aeb: the object to dump
  163. * @type: object type: 0 - not corrupted, 1 - corrupted
  164. */
  165. void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
  166. {
  167. pr_err("eraseblock attaching information dump:\n");
  168. pr_err("\tec %d\n", aeb->ec);
  169. pr_err("\tpnum %d\n", aeb->pnum);
  170. if (type == 0) {
  171. pr_err("\tlnum %d\n", aeb->lnum);
  172. pr_err("\tscrub %d\n", aeb->scrub);
  173. pr_err("\tsqnum %llu\n", aeb->sqnum);
  174. }
  175. }
  176. /**
  177. * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
  178. * @req: the object to dump
  179. */
  180. void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
  181. {
  182. char nm[17];
  183. pr_err("Volume creation request dump:\n");
  184. pr_err("\tvol_id %d\n", req->vol_id);
  185. pr_err("\talignment %d\n", req->alignment);
  186. pr_err("\tbytes %lld\n", (long long)req->bytes);
  187. pr_err("\tvol_type %d\n", req->vol_type);
  188. pr_err("\tname_len %d\n", req->name_len);
  189. memcpy(nm, req->name, 16);
  190. nm[16] = 0;
  191. pr_err("\t1st 16 characters of name: %s\n", nm);
  192. }
  193. #ifndef __UBOOT__
  194. /*
  195. * Root directory for UBI stuff in debugfs. Contains sub-directories which
  196. * contain the stuff specific to particular UBI devices.
  197. */
  198. static struct dentry *dfs_rootdir;
  199. /**
  200. * ubi_debugfs_init - create UBI debugfs directory.
  201. *
  202. * Create UBI debugfs directory. Returns zero in case of success and a negative
  203. * error code in case of failure.
  204. */
  205. int ubi_debugfs_init(void)
  206. {
  207. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  208. return 0;
  209. dfs_rootdir = debugfs_create_dir("ubi", NULL);
  210. if (IS_ERR_OR_NULL(dfs_rootdir)) {
  211. int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
  212. pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
  213. err);
  214. return err;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * ubi_debugfs_exit - remove UBI debugfs directory.
  220. */
  221. void ubi_debugfs_exit(void)
  222. {
  223. if (IS_ENABLED(CONFIG_DEBUG_FS))
  224. debugfs_remove(dfs_rootdir);
  225. }
  226. /* Read an UBI debugfs file */
  227. static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
  228. size_t count, loff_t *ppos)
  229. {
  230. unsigned long ubi_num = (unsigned long)file->private_data;
  231. struct dentry *dent = file->f_path.dentry;
  232. struct ubi_device *ubi;
  233. struct ubi_debug_info *d;
  234. char buf[8];
  235. int val;
  236. ubi = ubi_get_device(ubi_num);
  237. if (!ubi)
  238. return -ENODEV;
  239. d = &ubi->dbg;
  240. if (dent == d->dfs_chk_gen)
  241. val = d->chk_gen;
  242. else if (dent == d->dfs_chk_io)
  243. val = d->chk_io;
  244. else if (dent == d->dfs_chk_fastmap)
  245. val = d->chk_fastmap;
  246. else if (dent == d->dfs_disable_bgt)
  247. val = d->disable_bgt;
  248. else if (dent == d->dfs_emulate_bitflips)
  249. val = d->emulate_bitflips;
  250. else if (dent == d->dfs_emulate_io_failures)
  251. val = d->emulate_io_failures;
  252. else if (dent == d->dfs_emulate_power_cut) {
  253. snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
  254. count = simple_read_from_buffer(user_buf, count, ppos,
  255. buf, strlen(buf));
  256. goto out;
  257. } else if (dent == d->dfs_power_cut_min) {
  258. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
  259. count = simple_read_from_buffer(user_buf, count, ppos,
  260. buf, strlen(buf));
  261. goto out;
  262. } else if (dent == d->dfs_power_cut_max) {
  263. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
  264. count = simple_read_from_buffer(user_buf, count, ppos,
  265. buf, strlen(buf));
  266. goto out;
  267. }
  268. else {
  269. count = -EINVAL;
  270. goto out;
  271. }
  272. if (val)
  273. buf[0] = '1';
  274. else
  275. buf[0] = '0';
  276. buf[1] = '\n';
  277. buf[2] = 0x00;
  278. count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  279. out:
  280. ubi_put_device(ubi);
  281. return count;
  282. }
  283. /* Write an UBI debugfs file */
  284. static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
  285. size_t count, loff_t *ppos)
  286. {
  287. unsigned long ubi_num = (unsigned long)file->private_data;
  288. struct dentry *dent = file->f_path.dentry;
  289. struct ubi_device *ubi;
  290. struct ubi_debug_info *d;
  291. size_t buf_size;
  292. char buf[8] = {0};
  293. int val;
  294. ubi = ubi_get_device(ubi_num);
  295. if (!ubi)
  296. return -ENODEV;
  297. d = &ubi->dbg;
  298. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  299. if (copy_from_user(buf, user_buf, buf_size)) {
  300. count = -EFAULT;
  301. goto out;
  302. }
  303. if (dent == d->dfs_power_cut_min) {
  304. if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
  305. count = -EINVAL;
  306. goto out;
  307. } else if (dent == d->dfs_power_cut_max) {
  308. if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
  309. count = -EINVAL;
  310. goto out;
  311. } else if (dent == d->dfs_emulate_power_cut) {
  312. if (kstrtoint(buf, 0, &val) != 0)
  313. count = -EINVAL;
  314. d->emulate_power_cut = val;
  315. goto out;
  316. }
  317. if (buf[0] == '1')
  318. val = 1;
  319. else if (buf[0] == '0')
  320. val = 0;
  321. else {
  322. count = -EINVAL;
  323. goto out;
  324. }
  325. if (dent == d->dfs_chk_gen)
  326. d->chk_gen = val;
  327. else if (dent == d->dfs_chk_io)
  328. d->chk_io = val;
  329. else if (dent == d->dfs_chk_fastmap)
  330. d->chk_fastmap = val;
  331. else if (dent == d->dfs_disable_bgt)
  332. d->disable_bgt = val;
  333. else if (dent == d->dfs_emulate_bitflips)
  334. d->emulate_bitflips = val;
  335. else if (dent == d->dfs_emulate_io_failures)
  336. d->emulate_io_failures = val;
  337. else
  338. count = -EINVAL;
  339. out:
  340. ubi_put_device(ubi);
  341. return count;
  342. }
  343. /* File operations for all UBI debugfs files */
  344. static const struct file_operations dfs_fops = {
  345. .read = dfs_file_read,
  346. .write = dfs_file_write,
  347. .open = simple_open,
  348. .llseek = no_llseek,
  349. .owner = THIS_MODULE,
  350. };
  351. /**
  352. * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
  353. * @ubi: UBI device description object
  354. *
  355. * This function creates all debugfs files for UBI device @ubi. Returns zero in
  356. * case of success and a negative error code in case of failure.
  357. */
  358. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  359. {
  360. int err, n;
  361. unsigned long ubi_num = ubi->ubi_num;
  362. const char *fname;
  363. struct dentry *dent;
  364. struct ubi_debug_info *d = &ubi->dbg;
  365. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  366. return 0;
  367. n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
  368. ubi->ubi_num);
  369. if (n == UBI_DFS_DIR_LEN) {
  370. /* The array size is too small */
  371. fname = UBI_DFS_DIR_NAME;
  372. dent = ERR_PTR(-EINVAL);
  373. goto out;
  374. }
  375. fname = d->dfs_dir_name;
  376. dent = debugfs_create_dir(fname, dfs_rootdir);
  377. if (IS_ERR_OR_NULL(dent))
  378. goto out;
  379. d->dfs_dir = dent;
  380. fname = "chk_gen";
  381. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  382. &dfs_fops);
  383. if (IS_ERR_OR_NULL(dent))
  384. goto out_remove;
  385. d->dfs_chk_gen = dent;
  386. fname = "chk_io";
  387. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  388. &dfs_fops);
  389. if (IS_ERR_OR_NULL(dent))
  390. goto out_remove;
  391. d->dfs_chk_io = dent;
  392. fname = "chk_fastmap";
  393. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  394. &dfs_fops);
  395. if (IS_ERR_OR_NULL(dent))
  396. goto out_remove;
  397. d->dfs_chk_fastmap = dent;
  398. fname = "tst_disable_bgt";
  399. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  400. &dfs_fops);
  401. if (IS_ERR_OR_NULL(dent))
  402. goto out_remove;
  403. d->dfs_disable_bgt = dent;
  404. fname = "tst_emulate_bitflips";
  405. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  406. &dfs_fops);
  407. if (IS_ERR_OR_NULL(dent))
  408. goto out_remove;
  409. d->dfs_emulate_bitflips = dent;
  410. fname = "tst_emulate_io_failures";
  411. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  412. &dfs_fops);
  413. if (IS_ERR_OR_NULL(dent))
  414. goto out_remove;
  415. d->dfs_emulate_io_failures = dent;
  416. fname = "tst_emulate_power_cut";
  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_power_cut = dent;
  422. fname = "tst_emulate_power_cut_min";
  423. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  424. &dfs_fops);
  425. if (IS_ERR_OR_NULL(dent))
  426. goto out_remove;
  427. d->dfs_power_cut_min = dent;
  428. fname = "tst_emulate_power_cut_max";
  429. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  430. &dfs_fops);
  431. if (IS_ERR_OR_NULL(dent))
  432. goto out_remove;
  433. d->dfs_power_cut_max = dent;
  434. return 0;
  435. out_remove:
  436. debugfs_remove_recursive(d->dfs_dir);
  437. out:
  438. err = dent ? PTR_ERR(dent) : -ENODEV;
  439. ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
  440. fname, err);
  441. return err;
  442. }
  443. /**
  444. * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
  445. * @ubi: UBI device description object
  446. */
  447. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  448. {
  449. if (IS_ENABLED(CONFIG_DEBUG_FS))
  450. debugfs_remove_recursive(ubi->dbg.dfs_dir);
  451. }
  452. /**
  453. * ubi_dbg_power_cut - emulate a power cut if it is time to do so
  454. * @ubi: UBI device description object
  455. * @caller: Flags set to indicate from where the function is being called
  456. *
  457. * Returns non-zero if a power cut was emulated, zero if not.
  458. */
  459. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
  460. {
  461. unsigned int range;
  462. if ((ubi->dbg.emulate_power_cut & caller) == 0)
  463. return 0;
  464. if (ubi->dbg.power_cut_counter == 0) {
  465. ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
  466. if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
  467. range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
  468. ubi->dbg.power_cut_counter += prandom_u32() % range;
  469. }
  470. return 0;
  471. }
  472. ubi->dbg.power_cut_counter--;
  473. if (ubi->dbg.power_cut_counter)
  474. return 0;
  475. ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
  476. ubi_ro_mode(ubi);
  477. return 1;
  478. }
  479. #else
  480. int ubi_debugfs_init(void)
  481. {
  482. return 0;
  483. }
  484. void ubi_debugfs_exit(void)
  485. {
  486. }
  487. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  488. {
  489. return 0;
  490. }
  491. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  492. {
  493. }
  494. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
  495. {
  496. return 0;
  497. }
  498. #endif