nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #include nuclear@90: #ifdef __linux__ nuclear@90: #include nuclear@90: #endif nuclear@90: #ifdef __darwin__ nuclear@90: #include nuclear@90: #endif nuclear@90: #include "fs.h" nuclear@90: nuclear@90: int mkfs(int fd, int blksize, uint32_t nblocks); nuclear@90: uint32_t get_block_count(int fd, int blksize); nuclear@90: int user_readblock(int dev, uint32_t blk, void *buf); nuclear@90: int user_writeblock(int dev, uint32_t blk, void *buf); nuclear@90: int parse_args(int argc, char **argv); nuclear@90: nuclear@90: int fd; nuclear@90: uint32_t num_blocks; nuclear@90: nuclear@90: int main(int argc, char **argv) nuclear@90: { nuclear@90: if(parse_args(argc, argv) == -1) { nuclear@90: return 1; nuclear@90: } nuclear@90: nuclear@90: if((num_blocks = get_block_count(fd, BLKSZ)) == 0) { nuclear@90: fprintf(stderr, "could not determine the number of blocks\n"); nuclear@90: return 1; nuclear@90: } nuclear@90: printf("total blocks: %u\n", (unsigned int)num_blocks); nuclear@90: nuclear@90: if(mkfs(fd, num_blocks) == -1) { nuclear@90: return 1; nuclear@90: } nuclear@90: nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@90: int mkfs(int fd, int blksize, uint32_t nblocks) nuclear@90: { nuclear@90: struct superblock *sb; nuclear@90: nuclear@93: sb = malloc(BLKSZ); nuclear@93: assert(sb); nuclear@93: nuclear@93: sb->magic = MAGIC; nuclear@93: sb->ver = 0; nuclear@93: sb->num_blocks = nblocks; nuclear@90: } nuclear@90: nuclear@90: uint32_t get_block_count(int fd, int blksize) nuclear@90: { nuclear@90: unsigned long sz = 0; nuclear@90: uint64_t sz64 = 0; nuclear@90: struct stat st; nuclear@90: nuclear@90: #ifdef BLKGETSIZE64 nuclear@90: if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) { nuclear@90: return sz64 / blksize; nuclear@90: } nuclear@90: #endif nuclear@90: nuclear@90: #ifdef BLKGETSIZE nuclear@90: if(ioctl(fd, BLKGETSIZE, &sz) != -1) { nuclear@90: return sz / (blksize / 512); nuclear@90: } nuclear@90: #endif nuclear@90: nuclear@90: #ifdef DKIOCGETBLOCKCOUNT nuclear@90: if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) { nuclear@90: return sz64 / (blksize / 512); nuclear@90: } nuclear@90: #endif nuclear@90: nuclear@90: if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) { nuclear@90: return st.st_size / blksize; nuclear@90: } nuclear@90: nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@93: int blk_read(void*, uint32_t blk, int count, void *buf) nuclear@90: { nuclear@90: if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) { nuclear@90: return -1; nuclear@90: } nuclear@93: if(read(fd, buf, BLKSZ * count) < BLKSZ * count) { nuclear@90: return -1; nuclear@90: } nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@93: int blk_write(void*, uint32_t blk, int count, void *buf) nuclear@90: { nuclear@90: if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) { nuclear@90: return -1; nuclear@90: } nuclear@93: if(write(fd, buf, BLKSZ * count) < BLKSZ * count) { nuclear@90: return -1; nuclear@90: } nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@90: int parse_args(int argc, char **argv) nuclear@90: { nuclear@90: int i; nuclear@90: nuclear@90: fd = -1; nuclear@90: nuclear@90: for(i=1; i\n", argv[0]); nuclear@90: exit(0); nuclear@90: nuclear@90: default: nuclear@90: goto invalid; nuclear@90: } nuclear@90: } else { nuclear@90: if(fd != -1) { nuclear@90: goto invalid; nuclear@90: } nuclear@90: nuclear@90: if((fd = open(argv[i], O_RDWR)) == -1) { nuclear@90: fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno)); nuclear@90: return -1; nuclear@90: } nuclear@90: } nuclear@90: } nuclear@90: nuclear@90: if(fd == -1) { nuclear@90: fprintf(stderr, "you must specify a device or image file\n"); nuclear@90: return -1; nuclear@90: } nuclear@90: nuclear@90: return 0; nuclear@90: nuclear@90: invalid: nuclear@90: fprintf(stderr, "invalid argument: %s\n", argv[i]); nuclear@90: return -1; nuclear@90: }