debug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. 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. pr_err("UBI error: 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[8];
  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_chk_fastmap)
  244. val = d->chk_fastmap;
  245. else if (dent == d->dfs_disable_bgt)
  246. val = d->disable_bgt;
  247. else if (dent == d->dfs_emulate_bitflips)
  248. val = d->emulate_bitflips;
  249. else if (dent == d->dfs_emulate_io_failures)
  250. val = d->emulate_io_failures;
  251. else if (dent == d->dfs_emulate_power_cut) {
  252. snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
  253. count = simple_read_from_buffer(user_buf, count, ppos,
  254. buf, strlen(buf));
  255. goto out;
  256. } else if (dent == d->dfs_power_cut_min) {
  257. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
  258. count = simple_read_from_buffer(user_buf, count, ppos,
  259. buf, strlen(buf));
  260. goto out;
  261. } else if (dent == d->dfs_power_cut_max) {
  262. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
  263. count = simple_read_from_buffer(user_buf, count, ppos,
  264. buf, strlen(buf));
  265. goto out;
  266. }
  267. else {
  268. count = -EINVAL;
  269. goto out;
  270. }
  271. if (val)
  272. buf[0] = '1';
  273. else
  274. buf[0] = '0';
  275. buf[1] = '\n';
  276. buf[2] = 0x00;
  277. count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  278. out:
  279. ubi_put_device(ubi);
  280. return count;
  281. }
  282. /* Write an UBI debugfs file */
  283. static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
  284. size_t count, loff_t *ppos)
  285. {
  286. unsigned long ubi_num = (unsigned long)file->private_data;
  287. struct dentry *dent = file->f_path.dentry;
  288. struct ubi_device *ubi;
  289. struct ubi_debug_info *d;
  290. size_t buf_size;
  291. char buf[8] = {0};
  292. int val;
  293. ubi = ubi_get_device(ubi_num);
  294. if (!ubi)
  295. return -ENODEV;
  296. d = &ubi->dbg;
  297. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  298. if (copy_from_user(buf, user_buf, buf_size)) {
  299. count = -EFAULT;
  300. goto out;
  301. }
  302. if (dent == d->dfs_power_cut_min) {
  303. if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
  304. count = -EINVAL;
  305. goto out;
  306. } else if (dent == d->dfs_power_cut_max) {
  307. if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
  308. count = -EINVAL;
  309. goto out;
  310. } else if (dent == d->dfs_emulate_power_cut) {
  311. if (kstrtoint(buf, 0, &val) != 0)
  312. count = -EINVAL;
  313. d->emulate_power_cut = val;
  314. goto out;
  315. }
  316. if (buf[0] == '1')
  317. val = 1;
  318. else if (buf[0] == '0')
  319. val = 0;
  320. else {
  321. count = -EINVAL;
  322. goto out;
  323. }
  324. if (dent == d->dfs_chk_gen)
  325. d->chk_gen = val;
  326. else if (dent == d->dfs_chk_io)
  327. d->chk_io = val;
  328. else if (dent == d->dfs_chk_fastmap)
  329. d->chk_fastmap = val;
  330. else if (dent == d->dfs_disable_bgt)
  331. d->disable_bgt = val;
  332. else if (dent == d->dfs_emulate_bitflips)
  333. d->emulate_bitflips = val;
  334. else if (dent == d->dfs_emulate_io_failures)
  335. d->emulate_io_failures = val;
  336. else
  337. count = -EINVAL;
  338. out:
  339. ubi_put_device(ubi);
  340. return count;
  341. }
  342. /* File operations for all UBI debugfs files */
  343. static const struct file_operations dfs_fops = {
  344. .read = dfs_file_read,
  345. .write = dfs_file_write,
  346. .open = simple_open,
  347. .llseek = no_llseek,
  348. .owner = THIS_MODULE,
  349. };
  350. /**
  351. * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
  352. * @ubi: UBI device description object
  353. *
  354. * This function creates all debugfs files for UBI device @ubi. Returns zero in
  355. * case of success and a negative error code in case of failure.
  356. */
  357. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  358. {
  359. int err, n;
  360. unsigned long ubi_num = ubi->ubi_num;
  361. const char *fname;
  362. struct dentry *dent;
  363. struct ubi_debug_info *d = &ubi->dbg;
  364. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  365. return 0;
  366. n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
  367. ubi->ubi_num);
  368. if (n == UBI_DFS_DIR_LEN) {
  369. /* The array size is too small */
  370. fname = UBI_DFS_DIR_NAME;
  371. dent = ERR_PTR(-EINVAL);
  372. goto out;
  373. }
  374. fname = d->dfs_dir_name;
  375. dent = debugfs_create_dir(fname, dfs_rootdir);
  376. if (IS_ERR_OR_NULL(dent))
  377. goto out;
  378. d->dfs_dir = dent;
  379. fname = "chk_gen";
  380. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  381. &dfs_fops);
  382. if (IS_ERR_OR_NULL(dent))
  383. goto out_remove;
  384. d->dfs_chk_gen = dent;
  385. fname = "chk_io";
  386. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  387. &dfs_fops);
  388. if (IS_ERR_OR_NULL(dent))
  389. goto out_remove;
  390. d->dfs_chk_io = dent;
  391. fname = "chk_fastmap";
  392. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  393. &dfs_fops);
  394. if (IS_ERR_OR_NULL(dent))
  395. goto out_remove;
  396. d->dfs_chk_fastmap = dent;
  397. fname = "tst_disable_bgt";
  398. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  399. &dfs_fops);
  400. if (IS_ERR_OR_NULL(dent))
  401. goto out_remove;
  402. d->dfs_disable_bgt = dent;
  403. fname = "tst_emulate_bitflips";
  404. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  405. &dfs_fops);
  406. if (IS_ERR_OR_NULL(dent))
  407. goto out_remove;
  408. d->dfs_emulate_bitflips = dent;
  409. fname = "tst_emulate_io_failures";
  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_emulate_io_failures = dent;
  415. fname = "tst_emulate_power_cut";
  416. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  417. &dfs_fops);
  418. if (IS_ERR_OR_NULL(dent))
  419. goto out_remove;
  420. d->dfs_emulate_power_cut = dent;
  421. fname = "tst_emulate_power_cut_min";
  422. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  423. &dfs_fops);
  424. if (IS_ERR_OR_NULL(dent))
  425. goto out_remove;
  426. d->dfs_power_cut_min = dent;
  427. fname = "tst_emulate_power_cut_max";
  428. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  429. &dfs_fops);
  430. if (IS_ERR_OR_NULL(dent))
  431. goto out_remove;
  432. d->dfs_power_cut_max = dent;
  433. return 0;
  434. out_remove:
  435. debugfs_remove_recursive(d->dfs_dir);
  436. out:
  437. err = dent ? PTR_ERR(dent) : -ENODEV;
  438. ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
  439. fname, err);
  440. return err;
  441. }
  442. /**
  443. * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
  444. * @ubi: UBI device description object
  445. */
  446. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  447. {
  448. if (IS_ENABLED(CONFIG_DEBUG_FS))
  449. debugfs_remove_recursive(ubi->dbg.dfs_dir);
  450. }
  451. /**
  452. * ubi_dbg_power_cut - emulate a power cut if it is time to do so
  453. * @ubi: UBI device description object
  454. * @caller: Flags set to indicate from where the function is being called
  455. *
  456. * Returns non-zero if a power cut was emulated, zero if not.
  457. */
  458. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
  459. {
  460. unsigned int range;
  461. if ((ubi->dbg.emulate_power_cut & caller) == 0)
  462. return 0;
  463. if (ubi->dbg.power_cut_counter == 0) {
  464. ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
  465. if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
  466. range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
  467. ubi->dbg.power_cut_counter += prandom_u32() % range;
  468. }
  469. return 0;
  470. }
  471. ubi->dbg.power_cut_counter--;
  472. if (ubi->dbg.power_cut_counter)
  473. return 0;
  474. ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
  475. ubi_ro_mode(ubi);
  476. return 1;
  477. }
  478. #else
  479. int ubi_debugfs_init(void)
  480. {
  481. return 0;
  482. }
  483. void ubi_debugfs_exit(void)
  484. {
  485. }
  486. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  487. {
  488. return 0;
  489. }
  490. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  491. {
  492. }
  493. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
  494. {
  495. return 0;
  496. }
  497. #endif