📄 mkyaffs2image.c
字号:
oh->yst_gid = SWAP32(oh->yst_gid); oh->yst_atime = SWAP32(oh->yst_atime); oh->yst_mtime = SWAP32(oh->yst_mtime); oh->yst_ctime = SWAP32(oh->yst_ctime);#endif oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that! oh->equivalentObjectId = SWAP32(oh->equivalentObjectId); // alias - char array. oh->yst_rdev = SWAP32(oh->yst_rdev);#ifdef CONFIG_YAFFS_WINCE oh->win_ctime[0] = SWAP32(oh->win_ctime[0]); oh->win_ctime[1] = SWAP32(oh->win_ctime[1]); oh->win_atime[0] = SWAP32(oh->win_atime[0]); oh->win_atime[1] = SWAP32(oh->win_atime[1]); oh->win_mtime[0] = SWAP32(oh->win_mtime[0]); oh->win_mtime[1] = SWAP32(oh->win_mtime[1]); oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]); oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]); oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]); oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]); oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]); oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);#else oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]); oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]); oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]); oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]); oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]); oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]); oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]); oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]); oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]); oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]); oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]); oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);#endif}#endifstatic int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias){ __u8 bytes[chunkSize]; yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes; memset(bytes,0xff,sizeof(bytes)); oh->type = t; oh->parentObjectId = parent; strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH); if(t != YAFFS_OBJECT_TYPE_HARDLINK) { oh->yst_mode = s->st_mode; oh->yst_uid = s->st_uid;// NCB 12/9/02 oh->yst_gid = s->yst_uid; oh->yst_gid = s->st_gid; oh->yst_atime = s->st_atime; oh->yst_mtime = s->st_mtime; oh->yst_ctime = s->st_ctime; oh->yst_rdev = s->st_rdev; } if(t == YAFFS_OBJECT_TYPE_FILE) { oh->fileSize = s->st_size; } if(t == YAFFS_OBJECT_TYPE_HARDLINK) { oh->equivalentObjectId = equivalentObj; } if(t == YAFFS_OBJECT_TYPE_SYMLINK) { strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH); }/* KSI: FUBAR. Left for a leter time. */#if 0 if (convert_endian) { object_header_little_to_big_endian(oh); }#endif return write_chunk(bytes,objId,0,0xffff); }static int process_directory(int parent, const char *path){ DIR *dir; struct dirent *entry; nDirectories++; dir = opendir(path); if(dir) { while((entry = readdir(dir)) != NULL) { /* Ignore . and .. */ if(strcmp(entry->d_name,".") && strcmp(entry->d_name,"..")) { char full_name[500]; struct stat stats; int equivalentObj; int newObj; sprintf(full_name,"%s/%s",path,entry->d_name); lstat(full_name,&stats); if(S_ISLNK(stats.st_mode) || S_ISREG(stats.st_mode) || S_ISDIR(stats.st_mode) || S_ISFIFO(stats.st_mode) || S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode) || S_ISSOCK(stats.st_mode)) { newObj = obj_id++; nObjects++; printf("Object %d, %s is a ",newObj,full_name); /* We're going to create an object for it */ if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0) { /* we need to make a hard link */ printf("hard link to object %d\n",equivalentObj); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL); } else { add_obj_to_list(stats.st_dev,stats.st_ino,newObj); if(S_ISLNK(stats.st_mode)) { char symname[500]; memset(symname,0, sizeof(symname)); readlink(full_name,symname,sizeof(symname) -1); printf("symlink to \"%s\"\n",symname); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname); } else if(S_ISREG(stats.st_mode)) { printf("file, "); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL); if(error >= 0) { int h; __u8 bytes[chunkSize]; int nBytes; int chunk = 0; h = open(full_name,O_RDONLY); if(h >= 0) { memset(bytes,0xff,sizeof(bytes)); while((nBytes = read(h,bytes,sizeof(bytes))) > 0) { chunk++; write_chunk(bytes,newObj,chunk,nBytes); memset(bytes,0xff,sizeof(bytes)); } if(nBytes < 0) error = nBytes; printf("%d data chunks written\n",chunk); close(h); } else { perror("Error opening file"); } } } else if(S_ISSOCK(stats.st_mode)) { printf("socket\n"); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL); } else if(S_ISFIFO(stats.st_mode)) { printf("fifo\n"); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL); } else if(S_ISCHR(stats.st_mode)) { printf("character device\n"); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL); } else if(S_ISBLK(stats.st_mode)) { printf("block device\n"); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL); } else if(S_ISDIR(stats.st_mode)) { printf("directory\n"); error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);// NCB modified 10/9/2001 process_directory(1,full_name); process_directory(newObj,full_name); } } } else { printf(" we don't handle this type\n"); } } } /* KSI: * Who is supposed to close those open directories in this * recursive function, lord Byron? Stock "ulimit -n" is 1024 * and e.g. stock Fedora /etc directory has more that 1024 * directories... */ closedir(dir); } return 0;}void usage(void){ printf("usage: mkyaffs2image layout# dir image_file [convert]\n"); printf(" layout# NAND OOB layout # (0 - raw, 1 - nand_oob_64)\n"); printf(" dir the directory tree to be converted\n"); printf(" image_file the output file to hold the image\n"); printf(" 'convert' make a big-endian img on a little-endian machine. BROKEN !\n"); exit(1);}int main(int argc, char *argv[]){ struct stat stats; int i; printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n"); if ((argc < 4) || (sscanf(argv[1], "%u", &layout_no) != 1)) { usage(); } i = 0; while (oob_layout[i].useecc != -1) i++; if (layout_no >= i) usage(); if ((argc == 5) && (!strncmp(argv[4], "convert", strlen("convert")))) { /* KSI: Broken as of now. TBD. Fail. */ usage(); convert_endian = 1; } if(stat(argv[2],&stats) < 0) { printf("Could not stat %s\n",argv[2]); exit(1); } if(!S_ISDIR(stats.st_mode)) { printf(" %s is not a directory\n",argv[2]); exit(1); } outFile = open(argv[3],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE); if(outFile < 0) { printf("Could not open output file %s\n",argv[3]); exit(1); } printf("Processing directory %s into image file %s\n",argv[2],argv[3]); error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL); if(error) error = process_directory(YAFFS_OBJECTID_ROOT,argv[2]); close(outFile); if(error < 0) { perror("operation incomplete"); exit(1); } else { printf("Operation complete.\n" "%d objects in %d directories\n" "%d NAND pages\n",nObjects, nDirectories, nPages); } close(outFile); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -