ftl_check.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Ported to MTD system.
  2. * Based on:
  3. */
  4. /*======================================================================
  5. Utility to create an FTL partition in a memory region
  6. ftl_check.c 1.10 1999/10/25 20:01:35
  7. The contents of this file are subject to the Mozilla Public
  8. License Version 1.1 (the "License"); you may not use this file
  9. except in compliance with the License. You may obtain a copy of
  10. the License at http://www.mozilla.org/MPL/
  11. Software distributed under the License is distributed on an "AS
  12. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  13. implied. See the License for the specific language governing
  14. rights and limitations under the License.
  15. The initial developer of the original code is David A. Hinds
  16. <dhinds@pcmcia.sourceforge.org>. Portions created by David A. Hinds
  17. are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  18. Alternatively, the contents of this file may be used under the
  19. terms of the GNU Public License version 2 (the "GPL"), in which
  20. case the provisions of the GPL are applicable instead of the
  21. above. If you wish to allow the use of your version of this file
  22. only under the terms of the GPL and not to allow others to use
  23. your version of this file under the MPL, indicate your decision
  24. by deleting the provisions above and replace them with the notice
  25. and other provisions required by the GPL. If you do not delete
  26. the provisions above, a recipient may use your version of this
  27. file under either the MPL or the GPL.
  28. ======================================================================*/
  29. #define PROGRAM_NAME "ftl_check"
  30. #include <sys/types.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stddef.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include <getopt.h>
  39. #include <sys/time.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/stat.h>
  42. #include <mtd/mtd-user.h>
  43. #include <mtd/ftl-user.h>
  44. #include <mtd_swab.h>
  45. #include "common.h"
  46. /*====================================================================*/
  47. static void print_size(u_int s)
  48. {
  49. if ((s > 0x100000) && ((s % 0x100000) == 0))
  50. printf("%d mb", s / 0x100000);
  51. else if ((s > 0x400) && ((s % 0x400) == 0))
  52. printf("%d kb", s / 0x400);
  53. else
  54. printf("%d bytes", s);
  55. }
  56. /*====================================================================*/
  57. static void check_partition(int fd)
  58. {
  59. mtd_info_t mtd;
  60. erase_unit_header_t hdr, hdr2;
  61. off_t i;
  62. u_int j, nbam, *bam;
  63. int control, data, blk_free, deleted;
  64. /* Get partition size, block size */
  65. if (ioctl(fd, MEMGETINFO, &mtd) != 0) {
  66. perror("get info failed");
  67. return;
  68. }
  69. printf("Memory region info:\n");
  70. printf(" Region size = ");
  71. print_size(mtd.size);
  72. printf(" Erase block size = ");
  73. print_size(mtd.erasesize);
  74. printf("\n\n");
  75. for (i = 0; i < mtd.size/mtd.erasesize; i++) {
  76. if (lseek(fd, (i * mtd.erasesize), SEEK_SET) == -1) {
  77. perror("seek failed");
  78. break;
  79. }
  80. read_nocheck(fd, &hdr, sizeof(hdr));
  81. if ((le32_to_cpu(hdr.FormattedSize) > 0) &&
  82. (le32_to_cpu(hdr.FormattedSize) <= mtd.size) &&
  83. (le16_to_cpu(hdr.NumEraseUnits) > 0) &&
  84. (le16_to_cpu(hdr.NumEraseUnits) <= mtd.size/mtd.erasesize))
  85. break;
  86. }
  87. if (i == mtd.size/mtd.erasesize) {
  88. fprintf(stderr, "No valid erase unit headers!\n");
  89. return;
  90. }
  91. printf("Partition header:\n");
  92. printf(" Formatted size = ");
  93. print_size(le32_to_cpu(hdr.FormattedSize));
  94. printf(", erase units = %d, transfer units = %d\n",
  95. le16_to_cpu(hdr.NumEraseUnits), hdr.NumTransferUnits);
  96. printf(" Erase unit size = ");
  97. print_size(1 << hdr.EraseUnitSize);
  98. printf(", virtual block size = ");
  99. print_size(1 << hdr.BlockSize);
  100. printf("\n");
  101. /* Create basic block allocation table for control blocks */
  102. nbam = (mtd.erasesize >> hdr.BlockSize);
  103. bam = malloc(nbam * sizeof(u_int));
  104. for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
  105. if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
  106. perror("seek failed");
  107. break;
  108. }
  109. if (read(fd, &hdr2, sizeof(hdr2)) == -1) {
  110. perror("read failed");
  111. break;
  112. }
  113. printf("\nErase unit %lld:\n", (long long)i);
  114. if ((hdr2.FormattedSize != hdr.FormattedSize) ||
  115. (hdr2.NumEraseUnits != hdr.NumEraseUnits) ||
  116. (hdr2.SerialNumber != hdr.SerialNumber))
  117. printf(" Erase unit header is corrupt.\n");
  118. else if (le16_to_cpu(hdr2.LogicalEUN) == 0xffff)
  119. printf(" Transfer unit, erase count = %d\n", le32_to_cpu(hdr2.EraseCount));
  120. else {
  121. printf(" Logical unit %d, erase count = %d\n",
  122. le16_to_cpu(hdr2.LogicalEUN), le32_to_cpu(hdr2.EraseCount));
  123. if (lseek(fd, (i << hdr.EraseUnitSize)+le32_to_cpu(hdr.BAMOffset),
  124. SEEK_SET) == -1) {
  125. perror("seek failed");
  126. break;
  127. }
  128. if (read(fd, bam, nbam * sizeof(u_int)) == -1) {
  129. perror("read failed");
  130. break;
  131. }
  132. blk_free = deleted = control = data = 0;
  133. for (j = 0; j < nbam; j++) {
  134. if (BLOCK_FREE(le32_to_cpu(bam[j])))
  135. blk_free++;
  136. else if (BLOCK_DELETED(le32_to_cpu(bam[j])))
  137. deleted++;
  138. else switch (BLOCK_TYPE(le32_to_cpu(bam[j]))) {
  139. case BLOCK_CONTROL: control++; break;
  140. case BLOCK_DATA: data++; break;
  141. default: break;
  142. }
  143. }
  144. printf(" Block allocation: %d control, %d data, %d free,"
  145. " %d deleted\n", control, data, blk_free, deleted);
  146. }
  147. }
  148. free(bam);
  149. } /* format_partition */
  150. /* Show usage information */
  151. static void showusage(void)
  152. {
  153. fprintf(stderr, "usage: %s device\n", PROGRAM_NAME);
  154. }
  155. /*====================================================================*/
  156. static const struct option long_opts[] = {
  157. {"help", no_argument, 0, 'h'},
  158. {"version", no_argument, 0, 'V'},
  159. {0, 0, 0, 0},
  160. };
  161. int main(int argc, char *argv[])
  162. {
  163. int c, optch, errflg, fd;
  164. struct stat buf;
  165. errflg = 0;
  166. while ((optch = getopt_long(argc, argv, "hV", long_opts, &c)) != -1) {
  167. switch (optch) {
  168. case 'V':
  169. common_print_version();
  170. exit(EXIT_SUCCESS);
  171. case 'h':
  172. errflg = 1; break;
  173. default:
  174. errflg = -1; break;
  175. }
  176. }
  177. if (errflg || (optind != argc-1)) {
  178. showusage();
  179. exit(errflg > 0 ? 0 : EXIT_FAILURE);
  180. }
  181. fd = open(argv[optind], O_RDONLY);
  182. if (fd == -1) {
  183. perror("open failed");
  184. exit(EXIT_FAILURE);
  185. }
  186. if (fstat(fd, &buf) != 0) {
  187. perror("status check failed");
  188. close(fd);
  189. exit(EXIT_FAILURE);
  190. }
  191. if (!(buf.st_mode & S_IFCHR)) {
  192. fprintf(stderr, "%s is not a character special device\n",
  193. argv[optind]);
  194. close(fd);
  195. exit(EXIT_FAILURE);
  196. }
  197. check_partition(fd);
  198. close(fd);
  199. exit(EXIT_SUCCESS);
  200. return 0;
  201. }