ftl_format.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Ported to MTD system.
  2. * Based on:
  3. */
  4. /*======================================================================
  5. Utility to create an FTL partition in a memory region
  6. ftl_format.c 1.13 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_format"
  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 <time.h>
  39. #include <getopt.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 const char LinkTarget[] = {
  58. 0x13, 0x03, 'C', 'I', 'S'
  59. };
  60. static const char DataOrg[] = {
  61. 0x46, 0x39, 0x00, 'F', 'T', 'L', '1', '0', '0', 0x00
  62. };
  63. static void build_header(erase_unit_header_t *hdr, u_int RegionSize,
  64. u_int BlockSize, u_int Spare, int Reserve,
  65. u_int BootSize)
  66. {
  67. u_int i, BootUnits, nbam, __FormattedSize;
  68. /* Default everything to the erased state */
  69. memset(hdr, 0xff, sizeof(*hdr));
  70. memcpy(hdr->LinkTargetTuple, LinkTarget, 5);
  71. memcpy(hdr->DataOrgTuple, DataOrg, 10);
  72. hdr->EndTuple[0] = hdr->EndTuple[1] = 0xff;
  73. BootSize = (BootSize + (BlockSize-1)) & ~(BlockSize-1);
  74. BootUnits = BootSize / BlockSize;
  75. /* We only support 512-byte blocks */
  76. hdr->BlockSize = 9;
  77. hdr->EraseUnitSize = 0;
  78. for (i = BlockSize; i > 1; i >>= 1)
  79. hdr->EraseUnitSize++;
  80. hdr->EraseCount = cpu_to_le32(0);
  81. hdr->FirstPhysicalEUN = cpu_to_le16(BootUnits);
  82. hdr->NumEraseUnits = cpu_to_le16((RegionSize - BootSize) >> hdr->EraseUnitSize);
  83. hdr->NumTransferUnits = Spare;
  84. __FormattedSize = RegionSize - ((Spare + BootUnits) << hdr->EraseUnitSize);
  85. /* Leave a little bit of space between the CIS and BAM */
  86. hdr->BAMOffset = cpu_to_le32(0x80);
  87. /* Adjust size to account for BAM space */
  88. nbam = ((1 << (hdr->EraseUnitSize - hdr->BlockSize)) * sizeof(u_int)
  89. + le32_to_cpu(hdr->BAMOffset) + (1 << hdr->BlockSize) - 1) >> hdr->BlockSize;
  90. __FormattedSize -=
  91. (le16_to_cpu(hdr->NumEraseUnits) - Spare) * (nbam << hdr->BlockSize);
  92. __FormattedSize -= ((__FormattedSize * Reserve / 100) & ~0xfff);
  93. hdr->FormattedSize = cpu_to_le32(__FormattedSize);
  94. /* hdr->FirstVMAddress defaults to erased state */
  95. hdr->NumVMPages = cpu_to_le16(0);
  96. hdr->Flags = 0;
  97. /* hdr->Code defaults to erased state */
  98. hdr->SerialNumber = cpu_to_le32(time(NULL));
  99. /* hdr->AltEUHOffset defaults to erased state */
  100. } /* build_header */
  101. /*====================================================================*/
  102. static int format_partition(int fd, int quiet, int interrogate,
  103. u_int spare, int reserve, u_int bootsize)
  104. {
  105. mtd_info_t mtd;
  106. erase_info_t erase;
  107. erase_unit_header_t hdr;
  108. u_int step, lun, i, nbam, *bam;
  109. /* Get partition size, block size */
  110. if (ioctl(fd, MEMGETINFO, &mtd) != 0) {
  111. perror("get info failed");
  112. return -1;
  113. }
  114. #if 0
  115. /* Intel Series 100 Flash: skip first block */
  116. if ((region.JedecMfr == 0x89) && (region.JedecInfo == 0xaa) &&
  117. (bootsize == 0)) {
  118. if (!quiet)
  119. printf("Skipping first block to protect CIS info...\n");
  120. bootsize = 1;
  121. }
  122. #endif
  123. /* Create header */
  124. build_header(&hdr, mtd.size, mtd.erasesize,
  125. spare, reserve, bootsize);
  126. if (!quiet) {
  127. printf("Partition size = ");
  128. print_size(mtd.size);
  129. printf(", erase unit size = ");
  130. print_size(mtd.erasesize);
  131. printf(", %d transfer units\n", spare);
  132. if (bootsize != 0) {
  133. print_size(le16_to_cpu(hdr.FirstPhysicalEUN) << hdr.EraseUnitSize);
  134. printf(" allocated for boot image\n");
  135. }
  136. printf("Reserved %d%%, formatted size = ", reserve);
  137. print_size(le32_to_cpu(hdr.FormattedSize));
  138. printf("\n");
  139. fflush(stdout);
  140. }
  141. if (interrogate)
  142. if (!prompt("This will destroy all data on the target device. Confirm?", false))
  143. return -1;
  144. /* Create basic block allocation table for control blocks */
  145. nbam = ((mtd.erasesize >> hdr.BlockSize) * sizeof(u_int)
  146. + le32_to_cpu(hdr.BAMOffset) + (1 << hdr.BlockSize) - 1) >> hdr.BlockSize;
  147. bam = malloc(nbam * sizeof(u_int));
  148. for (i = 0; i < nbam; i++)
  149. bam[i] = cpu_to_le32(BLOCK_CONTROL);
  150. /* Erase partition */
  151. if (!quiet) {
  152. printf("Erasing all blocks...\n");
  153. fflush(stdout);
  154. }
  155. erase.length = mtd.erasesize;
  156. erase.start = mtd.erasesize * le16_to_cpu(hdr.FirstPhysicalEUN);
  157. for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
  158. if (ioctl(fd, MEMERASE, &erase) < 0) {
  159. if (!quiet) {
  160. putchar('\n');
  161. fflush(stdout);
  162. }
  163. perror("block erase failed");
  164. free(bam);
  165. return -1;
  166. }
  167. erase.start += erase.length;
  168. if (!quiet) {
  169. if (mtd.size <= 0x800000) {
  170. if (erase.start % 0x100000) {
  171. if (!(erase.start % 0x20000)) putchar('-');
  172. }
  173. else putchar('+');
  174. }
  175. else {
  176. if (erase.start % 0x800000) {
  177. if (!(erase.start % 0x100000)) putchar('+');
  178. }
  179. else putchar('*');
  180. }
  181. fflush(stdout);
  182. }
  183. }
  184. if (!quiet) putchar('\n');
  185. /* Prepare erase units */
  186. if (!quiet) {
  187. printf("Writing erase unit headers...\n");
  188. fflush(stdout);
  189. }
  190. lun = 0;
  191. /* Distribute transfer units over the entire region */
  192. step = spare ? (le16_to_cpu(hdr.NumEraseUnits) / spare) : (le16_to_cpu(hdr.NumEraseUnits) + 1);
  193. for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
  194. off_t ofs = (off_t) (i + le16_to_cpu(hdr.FirstPhysicalEUN)) << hdr.EraseUnitSize;
  195. if (lseek(fd, ofs, SEEK_SET) == -1) {
  196. perror("seek failed");
  197. break;
  198. }
  199. /* Is this a transfer unit? */
  200. if (((i+1) % step) == 0)
  201. hdr.LogicalEUN = cpu_to_le16(0xffff);
  202. else {
  203. hdr.LogicalEUN = cpu_to_le16(lun);
  204. lun++;
  205. }
  206. if (write(fd, &hdr, sizeof(hdr)) == -1) {
  207. perror("write failed");
  208. break;
  209. }
  210. if (lseek(fd, ofs + le32_to_cpu(hdr.BAMOffset), SEEK_SET) == -1) {
  211. perror("seek failed");
  212. break;
  213. }
  214. if (write(fd, bam, nbam * sizeof(u_int)) == -1) {
  215. perror("write failed");
  216. break;
  217. }
  218. }
  219. free(bam);
  220. if (i < le16_to_cpu(hdr.NumEraseUnits))
  221. return -1;
  222. else
  223. return 0;
  224. } /* format_partition */
  225. /*====================================================================*/
  226. static const struct option long_opts[] = {
  227. {"help", no_argument, 0, 'h'},
  228. {"version", no_argument, 0, 'V'},
  229. {0, 0, 0, 0},
  230. };
  231. static const char *short_opts = "qir:s:b:Vh";
  232. int main(int argc, char *argv[])
  233. {
  234. int quiet, interrogate, reserve, c;
  235. int optch, errflg, fd, ret;
  236. u_int spare, bootsize;
  237. char *s;
  238. extern char *optarg;
  239. struct stat buf;
  240. quiet = 0;
  241. interrogate = 0;
  242. spare = 1;
  243. reserve = 5;
  244. errflg = 0;
  245. bootsize = 0;
  246. while ((optch = getopt_long(argc, argv, short_opts, long_opts, &c)) != -1) {
  247. switch (optch) {
  248. case 'q':
  249. quiet = 1; break;
  250. case 'i':
  251. interrogate = 1; break;
  252. case 's':
  253. spare = strtoul(optarg, NULL, 0); break;
  254. case 'r':
  255. reserve = strtoul(optarg, NULL, 0); break;
  256. case 'b':
  257. bootsize = strtoul(optarg, &s, 0);
  258. if ((*s == 'k') || (*s == 'K'))
  259. bootsize *= 1024;
  260. break;
  261. case 'V':
  262. common_print_version();
  263. exit(EXIT_SUCCESS);
  264. case 'h':
  265. errflg = 1; break;
  266. default:
  267. errflg = -1; break;
  268. }
  269. }
  270. if (errflg || (optind != argc-1)) {
  271. fprintf(stderr, "usage: %s [-q] [-i] [-s spare-blocks]"
  272. " [-r reserve-percent] [-b bootsize] device\n", PROGRAM_NAME);
  273. exit(errflg > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  274. }
  275. fd = open(argv[optind], O_RDWR);
  276. if (fd == -1) {
  277. perror("open failed");
  278. exit(EXIT_FAILURE);
  279. }
  280. if (fstat(fd, &buf) != 0) {
  281. perror("status check failed");
  282. close(fd);
  283. exit(EXIT_FAILURE);
  284. }
  285. if (!(buf.st_mode & S_IFCHR)) {
  286. fprintf(stderr, "%s is not a character special device\n",
  287. argv[optind]);
  288. close(fd);
  289. exit(EXIT_FAILURE);
  290. }
  291. ret = format_partition(fd, quiet, interrogate, spare, reserve,
  292. bootsize);
  293. if (!quiet) {
  294. if (ret)
  295. printf("format failed.\n");
  296. else
  297. printf("format successful.\n");
  298. }
  299. close(fd);
  300. return ret ? EXIT_FAILURE : EXIT_SUCCESS;
  301. }