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