mkyaffs2image.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. * Nick Bane modifications flagged NCB
  9. * Endian handling patches by James Ng.
  10. * mkyaffs2image hacks by NCB
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. /*
  17. * makeyaffs2image.c
  18. *
  19. * Makes a YAFFS2 file system image that can be used to load up a file system.
  20. * Uses default Linux MTD layout - change if you need something different.
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <dirent.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "yaffs_ecc.h"
  31. #include "yaffs_guts.h"
  32. #include "yaffs_tagsvalidity.h"
  33. #include "yaffs_packedtags2.h"
  34. unsigned yaffs_traceMask=0;
  35. #define MAX_OBJECTS 10000
  36. #define chunkSize 2048
  37. #define spareSize 64
  38. const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.4 2007/02/14 01:09:06 wookey Exp $";
  39. typedef struct
  40. {
  41. dev_t dev;
  42. ino_t ino;
  43. int obj;
  44. } objItem;
  45. static objItem obj_list[MAX_OBJECTS];
  46. static int n_obj = 0;
  47. static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
  48. static int nObjects, nDirectories, nPages;
  49. static int outFile;
  50. static int error;
  51. static int convert_endian = 0;
  52. static int obj_compare(const void *a, const void * b)
  53. {
  54. objItem *oa, *ob;
  55. oa = (objItem *)a;
  56. ob = (objItem *)b;
  57. if(oa->dev < ob->dev) return -1;
  58. if(oa->dev > ob->dev) return 1;
  59. if(oa->ino < ob->ino) return -1;
  60. if(oa->ino > ob->ino) return 1;
  61. return 0;
  62. }
  63. static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
  64. {
  65. if(n_obj < MAX_OBJECTS)
  66. {
  67. obj_list[n_obj].dev = dev;
  68. obj_list[n_obj].ino = ino;
  69. obj_list[n_obj].obj = obj;
  70. n_obj++;
  71. qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
  72. }
  73. else
  74. {
  75. // oops! not enough space in the object array
  76. fprintf(stderr,"Not enough space in object array\n");
  77. exit(2);
  78. }
  79. }
  80. static int find_obj_in_list(dev_t dev, ino_t ino)
  81. {
  82. objItem *i = NULL;
  83. objItem test;
  84. test.dev = dev;
  85. test.ino = ino;
  86. if(n_obj > 0)
  87. {
  88. i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
  89. }
  90. if(i)
  91. {
  92. return i->obj;
  93. }
  94. return -1;
  95. }
  96. /* This little function converts a little endian tag to a big endian tag.
  97. * NOTE: The tag is not usable after this other than calculating the CRC
  98. * with.
  99. */
  100. static void little_to_big_endian(yaffs_Tags *tagsPtr)
  101. {
  102. #if 0 // FIXME NCB
  103. yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
  104. yaffs_TagsUnion temp;
  105. memset(&temp, 0, sizeof(temp));
  106. // Ick, I hate magic numbers.
  107. temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
  108. temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
  109. temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
  110. temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
  111. temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
  112. temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
  113. temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
  114. temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
  115. // Now copy it back.
  116. tags->asBytes[0] = temp.asBytes[0];
  117. tags->asBytes[1] = temp.asBytes[1];
  118. tags->asBytes[2] = temp.asBytes[2];
  119. tags->asBytes[3] = temp.asBytes[3];
  120. tags->asBytes[4] = temp.asBytes[4];
  121. tags->asBytes[5] = temp.asBytes[5];
  122. tags->asBytes[6] = temp.asBytes[6];
  123. tags->asBytes[7] = temp.asBytes[7];
  124. #endif
  125. }
  126. static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
  127. {
  128. yaffs_ExtendedTags t;
  129. yaffs_PackedTags2 pt;
  130. error = write(outFile,data,chunkSize);
  131. if(error < 0) return error;
  132. yaffs_InitialiseTags(&t);
  133. t.chunkId = chunkId;
  134. // t.serialNumber = 0;
  135. t.serialNumber = 1; // **CHECK**
  136. t.byteCount = nBytes;
  137. t.objectId = objId;
  138. t.sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  139. // added NCB **CHECK**
  140. t.chunkUsed = 1;
  141. if (convert_endian)
  142. {
  143. little_to_big_endian(&t);
  144. }
  145. nPages++;
  146. yaffs_PackTags2(&pt,&t);
  147. // return write(outFile,&pt,sizeof(yaffs_PackedTags2));
  148. return write(outFile,&pt,spareSize);
  149. }
  150. #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
  151. (((x) & 0x0000FF00) << 8 ) | \
  152. (((x) & 0x00FF0000) >> 8 ) | \
  153. (((x) & 0xFF000000) >> 24))
  154. #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
  155. (((x) & 0xFF00) >> 8))
  156. // This one is easier, since the types are more standard. No funky shifts here.
  157. static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
  158. {
  159. oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
  160. oh->parentObjectId = SWAP32(oh->parentObjectId); // int
  161. oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
  162. // name = skip. Char array. Not swapped.
  163. oh->yst_mode = SWAP32(oh->yst_mode);
  164. #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
  165. // In fact, WinCE would be *THE* place where this would be an issue!
  166. oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
  167. oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
  168. oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
  169. oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
  170. oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
  171. #else
  172. // Regular POSIX.
  173. oh->yst_uid = SWAP32(oh->yst_uid);
  174. oh->yst_gid = SWAP32(oh->yst_gid);
  175. oh->yst_atime = SWAP32(oh->yst_atime);
  176. oh->yst_mtime = SWAP32(oh->yst_mtime);
  177. oh->yst_ctime = SWAP32(oh->yst_ctime);
  178. #endif
  179. oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
  180. oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
  181. // alias - char array.
  182. oh->yst_rdev = SWAP32(oh->yst_rdev);
  183. #ifdef CONFIG_YAFFS_WINCE
  184. oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
  185. oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
  186. oh->win_atime[0] = SWAP32(oh->win_atime[0]);
  187. oh->win_atime[1] = SWAP32(oh->win_atime[1]);
  188. oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
  189. oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
  190. oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
  191. oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
  192. oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
  193. oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
  194. oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
  195. oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
  196. #else
  197. oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
  198. oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
  199. oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
  200. oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
  201. oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
  202. oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
  203. oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
  204. oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
  205. oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
  206. oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
  207. oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
  208. oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
  209. #endif
  210. }
  211. static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
  212. {
  213. __u8 bytes[chunkSize];
  214. yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
  215. memset(bytes,0xff,sizeof(bytes));
  216. oh->type = t;
  217. oh->parentObjectId = parent;
  218. strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
  219. if(t != YAFFS_OBJECT_TYPE_HARDLINK)
  220. {
  221. oh->yst_mode = s->st_mode;
  222. oh->yst_uid = s->st_uid;
  223. // NCB 12/9/02 oh->yst_gid = s->yst_uid;
  224. oh->yst_gid = s->st_gid;
  225. oh->yst_atime = s->st_atime;
  226. oh->yst_mtime = s->st_mtime;
  227. oh->yst_ctime = s->st_ctime;
  228. oh->yst_rdev = s->st_rdev;
  229. }
  230. if(t == YAFFS_OBJECT_TYPE_FILE)
  231. {
  232. oh->fileSize = s->st_size;
  233. }
  234. if(t == YAFFS_OBJECT_TYPE_HARDLINK)
  235. {
  236. oh->equivalentObjectId = equivalentObj;
  237. }
  238. if(t == YAFFS_OBJECT_TYPE_SYMLINK)
  239. {
  240. strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
  241. }
  242. if (convert_endian)
  243. {
  244. object_header_little_to_big_endian(oh);
  245. }
  246. return write_chunk(bytes,objId,0,0xffff);
  247. }
  248. static int process_directory(int parent, const char *path)
  249. {
  250. DIR *dir;
  251. struct dirent *entry;
  252. nDirectories++;
  253. dir = opendir(path);
  254. if(dir)
  255. {
  256. while((entry = readdir(dir)) != NULL)
  257. {
  258. /* Ignore . and .. */
  259. if(strcmp(entry->d_name,".") &&
  260. strcmp(entry->d_name,".."))
  261. {
  262. char full_name[500];
  263. struct stat stats;
  264. int equivalentObj;
  265. int newObj;
  266. sprintf(full_name,"%s/%s",path,entry->d_name);
  267. lstat(full_name,&stats);
  268. if(S_ISLNK(stats.st_mode) ||
  269. S_ISREG(stats.st_mode) ||
  270. S_ISDIR(stats.st_mode) ||
  271. S_ISFIFO(stats.st_mode) ||
  272. S_ISBLK(stats.st_mode) ||
  273. S_ISCHR(stats.st_mode) ||
  274. S_ISSOCK(stats.st_mode))
  275. {
  276. newObj = obj_id++;
  277. nObjects++;
  278. printf("Object %d, %s is a ",newObj,full_name);
  279. /* We're going to create an object for it */
  280. if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
  281. {
  282. /* we need to make a hard link */
  283. printf("hard link to object %d\n",equivalentObj);
  284. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
  285. }
  286. else
  287. {
  288. add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
  289. if(S_ISLNK(stats.st_mode))
  290. {
  291. char symname[500];
  292. memset(symname,0, sizeof(symname));
  293. readlink(full_name,symname,sizeof(symname) -1);
  294. printf("symlink to \"%s\"\n",symname);
  295. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
  296. }
  297. else if(S_ISREG(stats.st_mode))
  298. {
  299. printf("file, ");
  300. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
  301. if(error >= 0)
  302. {
  303. int h;
  304. __u8 bytes[chunkSize];
  305. int nBytes;
  306. int chunk = 0;
  307. h = open(full_name,O_RDONLY);
  308. if(h >= 0)
  309. {
  310. memset(bytes,0xff,sizeof(bytes));
  311. while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
  312. {
  313. chunk++;
  314. write_chunk(bytes,newObj,chunk,nBytes);
  315. memset(bytes,0xff,sizeof(bytes));
  316. }
  317. if(nBytes < 0)
  318. error = nBytes;
  319. printf("%d data chunks written\n",chunk);
  320. }
  321. else
  322. {
  323. perror("Error opening file");
  324. }
  325. close(h);
  326. }
  327. }
  328. else if(S_ISSOCK(stats.st_mode))
  329. {
  330. printf("socket\n");
  331. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
  332. }
  333. else if(S_ISFIFO(stats.st_mode))
  334. {
  335. printf("fifo\n");
  336. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
  337. }
  338. else if(S_ISCHR(stats.st_mode))
  339. {
  340. printf("character device\n");
  341. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
  342. }
  343. else if(S_ISBLK(stats.st_mode))
  344. {
  345. printf("block device\n");
  346. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
  347. }
  348. else if(S_ISDIR(stats.st_mode))
  349. {
  350. printf("directory\n");
  351. error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
  352. // NCB modified 10/9/2001 process_directory(1,full_name);
  353. process_directory(newObj,full_name);
  354. }
  355. }
  356. }
  357. else
  358. {
  359. printf(" we don't handle this type\n");
  360. }
  361. }
  362. }
  363. }
  364. return 0;
  365. }
  366. int main(int argc, char *argv[])
  367. {
  368. struct stat stats;
  369. printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
  370. if(argc < 3)
  371. {
  372. printf("usage: mkyaffs2image dir image_file [convert]\n");
  373. printf(" dir the directory tree to be converted\n");
  374. printf(" image_file the output file to hold the image\n");
  375. printf(" 'convert' produce a big-endian image from a little-endian machine\n");
  376. exit(1);
  377. }
  378. if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
  379. {
  380. convert_endian = 1;
  381. }
  382. if(stat(argv[1],&stats) < 0)
  383. {
  384. printf("Could not stat %s\n",argv[1]);
  385. exit(1);
  386. }
  387. if(!S_ISDIR(stats.st_mode))
  388. {
  389. printf(" %s is not a directory\n",argv[1]);
  390. exit(1);
  391. }
  392. outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
  393. if(outFile < 0)
  394. {
  395. printf("Could not open output file %s\n",argv[2]);
  396. exit(1);
  397. }
  398. printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
  399. error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
  400. if(error)
  401. error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
  402. close(outFile);
  403. if(error < 0)
  404. {
  405. perror("operation incomplete");
  406. exit(1);
  407. }
  408. else
  409. {
  410. printf("Operation complete.\n"
  411. "%d objects in %d directories\n"
  412. "%d NAND pages\n",nObjects, nDirectories, nPages);
  413. }
  414. close(outFile);
  415. exit(0);
  416. }