?? genromfs.c
字號:
/* Generate a ROMFS file system * * Copyright (C) 1997,1998 Janos Farkas <chexum@shadow.banki.hu> * Copyright (C) 1998 Jakub Jelinek <jj@ultra.linux.cz> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Changes: * 2 Jan 1997 Initial release * 6 Aug 1997 Second release * 11 Sep 1998 Alignment support * 11 Jan 2001 special files of name @name,[cpub],major,minor *//* * Some sparse words about how to use the program * * `genromfs' is the `mkfs' equivalent of the other filesystems, but * you must tell it from which directory you want to build the * filesystem. I.e. all files (and directories) in that directory * will be part of the newly created filesystem. Imagine it like * building a cd image, or creating an archive (tar, zip) file. * * Basic usage: * * # genromfs -d rescue/ -f /dev/fd0 * * All files in the rescue directory will be written to /dev/fd0 as a * new romfs filesystem image. You can mount it (if you have the * romfs module loaded, or compiled into the kernel) via: * * # mount -t romfs /dev/fd0 /mnt * * You can also set the volume name of the filesystem (which is not * currently used by the kernel) with the -V option. If you don't * specify one, genromfs will create a volume name of the form: 'rom * xxxxxxxx', where the x's represent the current time in a cryptic * form. * * All in all, it's as simple as: * * # genromfs -d rescue -f testimg.rom -V "Install disk" * * Other options: * -a N force all regular file data to be aligned on N bytes boundary * -A N,/name force named file(s) (shell globbing applied against the filenames) * to be aligned on N bytes boundary * In both cases, N must be a power of two. *//* * Warning! Quite spaghetti code, it was born in a few hours. * Sorry about that. Feel free to contact me if you have problems. */#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */#include <stdlib.h> /* Userland prototypes of the ANSI C std lib functions */#include <string.h> /* Userland prototypes of the string handling funcs */#include <unistd.h> /* Userland prototypes of the Unix std system calls */#include <fcntl.h> /* Flag value for file handling functions */#include <time.h>#include <fnmatch.h>#include <dirent.h>#include <sys/stat.h>#include <sys/types.h>#include <netinet/in.h> /* Consts & structs defined by the internet system *//* good old times without autoconf... */#if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__)#include <sys/sysmacros.h>#endifstatic int case_insensitive_mode = 0;struct romfh { int32_t nextfh; int32_t spec; int32_t size; int32_t checksum;};#define ROMFS_MAXFN 128#define ROMFH_HRD 0#define ROMFH_DIR 1#define ROMFH_REG 2#define ROMFH_LNK 3#define ROMFH_BLK 4#define ROMFH_CHR 5#define ROMFH_SCK 6#define ROMFH_FIF 7#define ROMFH_EXEC 8struct filenode;struct filehdr { /* leave h, t, tp at front, this is a linked list header */ struct filenode *head; struct filenode *tail; struct filenode *tailpred; /* end fragile header */ struct filenode *owner;};struct filenode { /* leave n, p at front, this is a linked list item */ struct filenode *next; struct filenode *prev; /* end fragile header */ struct filenode *parent; struct filehdr dirlist; struct filenode *orig_link; char *name; char *realname; dev_t ondev; dev_t devnode; ino_t onino; mode_t modes; unsigned int offset; unsigned int size; unsigned int pad;};struct aligns { struct aligns *next; int align; char pattern[0];};struct excludes { struct excludes *next; char pattern[0];};void initlist(struct filehdr *fh, struct filenode *owner){ fh->head = (struct filenode *)&fh->tail; fh->tail = NULL; fh->tailpred = (struct filenode *)&fh->head; fh->owner = owner;}int listisempty(struct filehdr *fh){ return fh->head == (struct filenode *)&fh->tail;}void append(struct filehdr *fh, struct filenode *n){ struct filenode *tail = (struct filenode *)&fh->tail; n->next = tail; n->prev = tail->prev; tail->prev = n; n->prev->next =n; n->parent = fh->owner;}void shownode(int level, struct filenode *node, FILE *f){ struct filenode *p; fprintf(f, "%-4d %-20s [0x%-8x, 0x%-8x] %07o, sz %5u, at 0x%-6x", level, node->name, (int)node->ondev, (int)node->onino, node->modes, node->size, node->offset); if (node->orig_link) fprintf(f, " [link to 0x%-6x]", node->orig_link->offset); fprintf(f, "\n"); p = node->dirlist.head; while (p->next) { shownode(level+1, p, f); p = p->next; }}/* Dumping functions */static char bigbuf[4096];static char fixbuf[512];static int atoffs = 0;static int align = 16;struct aligns *alignlist = NULL;struct excludes *excludelist = NULL;int realbase;/* helper function to match an exclusion or align pattern */int nodematch(char *pattern, struct filenode *node){ char *start = node->name; /* XXX: ugly realbase is global */ if (pattern[0] == '/') start = node->realname + realbase; return fnmatch(pattern,start,FNM_PATHNAME|FNM_PERIOD);}int findalign(struct filenode *node){ struct aligns *pa; int i; if (S_ISREG(node->modes)) i = align; else i = 16; for (pa = alignlist; pa; pa = pa->next) { if (pa->align > i) { if (!nodematch(pa->pattern,node)) i = pa->align; } } return i;}int romfs_checksum(void *data, int size){ int32_t sum, word, *ptr; sum = 0; ptr = data; size>>=2; while (size>0) { word = *ptr++; sum += ntohl(word); size--; } return sum;}void fixsum(struct romfh *ri, int size){ ri->checksum = 0; ri->checksum = htonl(-romfs_checksum(ri, size));}void dumpdata(void *addr, int len, FILE *f){ int tocopy; struct romfh *ri; if (!len) return; if (atoffs >= 512) { fwrite(addr, len, 1, f); atoffs+=len; return; } tocopy = len < 512-atoffs ? len : 512-atoffs; memcpy(fixbuf+atoffs, addr, tocopy); atoffs+=tocopy; addr=(char*)addr+tocopy; len-=tocopy; if (atoffs==512) { ri = (struct romfh *)&fixbuf; fixsum(ri, atoffs<ntohl(ri->size)?atoffs:ntohl(ri->size)); fwrite(fixbuf, atoffs, 1, f); } if (len) { fwrite(addr, len, 1, f); atoffs+=len; }}void dumpzero(int len, FILE *f){ memset(bigbuf, 0, len); dumpdata(bigbuf, len, f);}void dumpdataa(void *addr, int len, FILE *f){ dumpdata(addr, len, f); if ((len & 15) != 0) dumpzero(16-(len&15), f);}void dumpstring(char *str, FILE *f){ dumpdataa(str, strlen(str)+1, f);}void dumpri(struct romfh *ri, struct filenode *n, FILE *f){ int len; len = strlen(n->name)+1; memcpy(bigbuf, ri, sizeof(*ri)); memcpy(bigbuf+16, n->name, len); if (len&15) { memset(bigbuf+16+len, 0, 16-(len&15)); len += 16-(len&15); } len+=16; ri=(struct romfh *)bigbuf; if (n->offset) fixsum(ri, len); dumpdata(bigbuf, len, f);#if 0 fprintf(stderr, "RI: [at %06x] %08lx, %08lx, %08lx, %08lx [%s]\n", n->offset, ntohl(ri->nextfh), ntohl(ri->spec), ntohl(ri->size), ntohl(ri->checksum), n->name);#endif}void dumpnode(struct filenode *node, FILE *f){ struct romfh ri; struct filenode *p; ri.nextfh = 0; ri.spec = 0; ri.size = htonl(node->size); ri.checksum = htonl(0x55555555); if (node->pad) dumpzero(node->pad, f); if (node->next && node->next->next) ri.nextfh = htonl(node->next->offset); if ((node->modes & 0111) && (S_ISDIR(node->modes) || S_ISREG(node->modes))) ri.nextfh |= htonl(ROMFH_EXEC); if (node->orig_link) { ri.nextfh |= htonl(ROMFH_HRD); /* Don't allow hardlinks to convey attributes */ ri.nextfh &= ~htonl(ROMFH_EXEC); ri.spec = htonl(node->orig_link->offset); dumpri(&ri, node, f); } else if (S_ISDIR(node->modes)) { ri.nextfh |= htonl(ROMFH_DIR); if (listisempty(&node->dirlist)) { ri.spec = htonl(node->offset); } else { ri.spec = htonl(node->dirlist.head->offset); } dumpri(&ri, node, f); } else if (S_ISLNK(node->modes)) { ri.nextfh |= htonl(ROMFH_LNK); dumpri(&ri, node, f); memset(bigbuf, 0, sizeof(bigbuf)); readlink(node->realname, bigbuf, node->size); dumpdataa(bigbuf, node->size, f); } else if (S_ISREG(node->modes)) { int offset, len, fd, max, avail; ri.nextfh |= htonl(ROMFH_REG); dumpri(&ri, node, f); offset = 0; max = node->size; /* XXX warn about size mismatch */ fd = open(node->realname, O_RDONLY#ifdef O_BINARY| O_BINARY#endif); if (fd) { while(offset < max) { avail = max-offset < sizeof(bigbuf) ? max-offset : sizeof(bigbuf); len = read(fd, bigbuf, avail); if (len <= 0) break; dumpdata(bigbuf, len, f); offset+=len; } close(fd); } max = (max+15)&~15; while (offset < max) { avail = max-offset < sizeof(bigbuf) ? max-offset : sizeof(bigbuf); memset(bigbuf, 0, avail); dumpdata(bigbuf, avail, f); offset+=avail; } } else if (S_ISCHR(node->modes)) { ri.nextfh |= htonl(ROMFH_CHR); ri.spec = htonl(major(node->devnode)<<16|minor(node->devnode)); dumpri(&ri, node, f); } else if (S_ISBLK(node->modes)) { ri.nextfh |= htonl(ROMFH_BLK); ri.spec = htonl(major(node->devnode)<<16|minor(node->devnode)); dumpri(&ri, node, f); } else if (S_ISFIFO(node->modes)) { ri.nextfh |= htonl(ROMFH_FIF); dumpri(&ri, node, f); } else if (S_ISSOCK(node->modes)) { ri.nextfh |= htonl(ROMFH_SCK); dumpri(&ri, node, f); } p = node->dirlist.head; while (p->next) { dumpnode(p, f); p = p->next; }}void dumpall(struct filenode *node, int lastoff, FILE *f){ struct romfh ri; struct filenode *p; ri.nextfh = htonl(0x2d726f6d); ri.spec = htonl(0x3166732d); ri.size = htonl(lastoff); ri.checksum = htonl(0x55555555); dumpri(&ri, node, f); p = node->dirlist.head; while (p->next) { dumpnode(p, f); p = p->next; } /* Align the whole bunch to ROMBSIZE boundary */ if (lastoff&1023) dumpzero(1024-(lastoff&1023), f);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -