Parcourir la source

7.073u-boot: uses cfs, and write boot+loader again

rain il y a 5 ans
Parent
commit
6d48055395
16 fichiers modifiés avec 2075 ajouts et 47 suppressions
  1. 1 0
      LINES
  2. 7 6
      Makefile
  3. 5 0
      README.md
  4. 3 8
      boot/Makefile
  5. 2 27
      boot/boot.asm
  6. 69 5
      boot/loader.asm
  7. 17 1
      tools/Makefile
  8. 24 0
      tools/bitmap.c
  9. 4 0
      tools/bitmap.h
  10. 59 0
      tools/cat.c
  11. 497 0
      tools/cfs.c
  12. 243 0
      tools/cfs.h
  13. 239 0
      tools/dumpcfs.c
  14. 494 0
      tools/initcfs.c
  15. 34 0
      tools/lsroot.c
  16. 377 0
      tools/mkfs.c

+ 1 - 0
LINES

@@ -23,4 +23,5 @@
 2021-7-21 5.011: 5011 lines, 41 files, rain
 2021-7-26 5.039: 5039 lines, 41 files, rain
 2021-7-28 5.063: 5063 lines, 41 files, rain
+2021-8-04 7.073: 7073 lines, 50 files, rain
 

+ 7 - 6
Makefile

@@ -1,4 +1,6 @@
 OBJS = boot/bootsect 
+BOOT_SRCS = boot/boot.asm boot/loader.asm 
+BOOT_OBJS = boot/boot.bin boot/loader.bin 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o 
 KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o font.o $(RLIB_OBJS)
@@ -20,7 +22,7 @@ font.o: tools/bin2s font.psf
 	nasm font.asm -o font.o -f elf64
 
 
-kbinary: $(KSRCS) $(KOBJS) boot/bootsect Makefile
+kbinary: $(KSRCS) $(KOBJS) $(BOOT_SCS) $(BOOT_OBJS) Makefile
 	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
 	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kbinary
 
@@ -28,10 +30,10 @@ vmxnix: $(KSRCS) $(KOBJS) Makefile
 	ld -b elf64-x86-64 $(KOBJS) -e init -T kernel.lds -o vmxnix
 
 
-xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary 
-	dd if=/dev/zero of=xnix.img bs=1024 count=1440
-	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
-	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
+xnix.img: $(BOOT_SRCS) $(BOOT_OBJS) $(KSRCS) $(KOBJS) Makefile kbinary tools/initcfs tools/mkfs 
+	dd if=/dev/zero of=xnix.img bs=1048576 count=64
+	tools/mkfs xnix.img 
+	tools/initcfs xnix.img boot/boot.bin boot/loader.bin kbinary 
 
 
 lines: 
@@ -45,7 +47,6 @@ clear:
 	make -C init/ clear
 	make -C kernel/ clear
 	make -C tools/ clear
-	rm boot/bootsect
 	rm xnix.img kbinary kernel.o vmxnix font.o font.asm 
 	make -C modules/ clear
 

+ 5 - 0
README.md

@@ -97,3 +97,8 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 5.063u-graphic:  
 &nbsp;&nbsp;&nbsp;&nbsp;using more colors (very beautiful! ) :-)
 
+7.073u-boot:  
+&nbsp;&nbsp;&nbsp;&nbsp;using CFS to be start-up device, not a binary device (without any file systems). 
+&nbsp;&nbsp;&nbsp;&nbsp;now cfs can creat/lsdir/mkdir/rw (but only starts at the start of file). 
+&nbsp;&nbsp;&nbsp;&nbsp;and boot jumps to 0x7e60 (0x7e00 + sizeof(struct superblock)), not 0x7e00 (the second sector). 
+

+ 3 - 8
boot/Makefile

@@ -1,13 +1,8 @@
-all: bootsect 
-
-
-bootsect: boot.bin loader.bin
-	cat boot.bin loader.bin > bootsect
-
+all: boot.bin loader.bin 
 
 %.bin: %.asm
-	nasm $*.asm -I ../include/ -o $*.bin 
+	nasm $*.asm -I ../include/ -o $*.bin -l $*.map  
 
 clear:
-	rm *.bin 
+	rm *.bin *.map 
 

+ 2 - 27
boot/boot.asm

@@ -50,32 +50,6 @@ _start:
 
 	call read_it
 
-.set_vga:
-	; get mode info
-	mov ax, 0x4f01
-	mov cx, 0x4118
-	mov di, 0x7000
-
-	int 0x10
-
-die:	jc die
-
-	mov al,[es:di]
-	mov [VMODE], al
-
-	mov ax, [es:di + 18]
-	mov [SCREENX], ax
-
-	mov ax, [es:di + 20]
-	mov [SCREENY], ax
-
-	mov eax, [es:di + 40]
-	mov [FB_ADDR], eax
-
-	mov ax, 0x4f02
-	mov bx, 0x4118
-	int 0x10
-
 	; get e820 memory
 
 	xor ebx, ebx
@@ -102,7 +76,8 @@ die:	jc die
 	cmp ebx, 0
 	jne .get_loop
 
-	jmp 0x0000:0x7e00
+	; jump to loader, 3488 bytes max 
+	jmp 0x0000:0x7e60
 
 .get_failed:
 	mov si, e820_fail_msg

+ 69 - 5
boot/loader.asm

@@ -17,12 +17,21 @@
 
 
 
-org 0x7e00
+org 0x7e60
 
 
 %include "boot/paging.asm"
 
 
+; yeah, it's really 0x7c00 because I don't want to use other memory. 
+
+BOOTDRIVER 	equ 0x7c00
+VMODE	 	equ 0x7c01
+SCREENX 	equ 0x7c02
+SCREENY 	equ 0x7c04
+FB_ADDR 	equ 0x7c06
+
+
 _start:
 	xor ax, ax
 
@@ -32,6 +41,39 @@ _start:
 	mov ss, ax
 	mov sp, 0x7c00
 
+	; put a message on screen
+	mov di, msg
+	call print
+
+.die:	jmp .die
+
+.set_vga:
+	; get mode info
+	mov ax, 0x4f01
+	mov cx, 0x4118
+	mov di, 0x7000
+
+	int 0x10
+
+die:	jc die
+
+	mov al,[es:di]
+	mov [VMODE], al
+
+	mov ax, [es:di + 18]
+	mov [SCREENX], ax
+
+	mov ax, [es:di + 20]
+	mov [SCREENY], ax
+
+	mov eax, [es:di + 40]
+	mov [FB_ADDR], eax
+
+	mov ax, 0x4f02
+	mov bx, 0x4118
+	int 0x10
+	
+
 .open_a20:
 	; disable interrupts before we initial done (enable in kernel)
 
@@ -56,10 +98,34 @@ _start:
 
 	; CPU is in protect-mode now, but segment registers didn't initialze. 
 	
-	; We cannot 'mov cx, xxx'
+	; we cannot 'mov cs, xxx'
 
 	jmp dword code32:init_prot
 
+print:
+	pushaw
+	; message in register di, but lodsb uses si. 
+	mov si, di
+
+.loop:
+	cld
+	lodsb
+	
+	; zero? -> return
+	test al, al
+	jz .ret
+	
+	mov ah, 0x0e
+	mov bx, 0x000f
+	
+	int 0x10
+
+	jmp .loop
+
+.ret:
+	popaw
+	ret
+
 
 bits 32
 
@@ -174,6 +240,7 @@ init_prot:
 	
 	jmp code64:0x8000
 
+msg: db "welcome to XNIX", 0x0d, 0x0a, 0x00
 
 alignb 16
 
@@ -215,6 +282,3 @@ gdt64_desc:
 code64 equ gdt64_code - gdt64
 data64 equ gdt64_data - gdt64
 
-
-times 512 - ($ - $$) db 0x00
-

+ 17 - 1
tools/Makefile

@@ -1,4 +1,5 @@
-OBJS =  readpsf bin2s
+OBJS =  readpsf bin2s mkfs dumpcfs initcfs lsroot cat
+CFLAGS = -Wall -Werror -g 
 
 all: $(OBJS)
 
@@ -12,3 +13,18 @@ bin2s: bin2s.c
 	gcc bin2s.c -Wall -Werror -o bin2s
 
 
+initcfs: initcfs.c 
+	gcc initcfs.c -o initcfs $(CFLAGS)
+
+mkfs: mkfs.c 
+	gcc mkfs.c bitmap.c -o mkfs $(CFLAGS) 
+
+dumpcfs: dumpcfs.c 
+	gcc dumpcfs.c -o dumpcfs $(CFLAGS)
+
+lsroot: lsroot.c 
+	gcc lsroot.c cfs.c -o lsroot $(CFLAGS)
+
+cat: cat.c
+	gcc cat.c cfs.c -o cat $(CFLAGS)
+

+ 24 - 0
tools/bitmap.c

@@ -0,0 +1,24 @@
+#include <stdlib.h>
+
+unsigned char *bitmap_init(unsigned int size) {
+	unsigned char *bitmap = malloc(size);
+	return bitmap;
+}
+
+
+void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value) {
+	unsigned char mask = 0x80 >> (pos & 0x7);
+	if (value) {
+		bitmap[pos >> 3] |= mask;
+	}
+	else {
+		bitmap[pos >> 3] &= ~mask;
+	}
+}
+
+char bitmap_read(unsigned char *bitmap, unsigned int pos) {
+	unsigned char mask = 0x80 >> (pos & 0x7);
+
+	return (mask & bitmap[pos >> 3]) == mask ? 1 : 0;
+}
+

+ 4 - 0
tools/bitmap.h

@@ -0,0 +1,4 @@
+extern char *bitmap_init(unsigned int size);
+extern void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value);
+extern char bitmap_read(unsigned char *bitmap, unsigned int pos);
+

+ 59 - 0
tools/cat.c

@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <sys/fcntl.h>
+#include <sys/types.h>
+
+#include "cfs.h"
+
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	char *file = (char *) argv[2];
+	int fd;
+
+	if (!fname) {
+		printf("usage: cat device file-name. \n");
+		return 1;
+	}
+
+	fd = open(fname, O_RDWR);
+	if (fd < 0) {
+		printf("failed when trying to open %s: %s\n", fname, strerror(errno));
+		return 1;
+	}
+
+	cfs_init(fd);
+
+	struct inode *ino = cfs_search(rootdir, file);
+
+	if (!ino) {
+		printf("fatal: %s not found. \n", file);
+		close(fd);
+
+		return 1;
+	}
+
+	char *buf = malloc(fsize(ino));
+	if (!buf) {
+		printf("failed when allocate space: %d bytes need. \n", fsize(ino));
+
+		close(fd);
+		return 1;
+	}
+
+	cfs_read(ino, buf, fsize(ino));
+
+	int i;
+
+	for (i = 0; i < fsize(ino); i++) 
+		putc(buf[i], stdout);
+
+	close(fd);
+
+	return 0;
+}

+ 497 - 0
tools/cfs.c

@@ -0,0 +1,497 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of CFS. */
+
+/* 
+  CFS 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 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  CFS is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with CFS. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <string.h>
+#include <errno.h>
+
+#include <sys/fcntl.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#define __CFS
+#include "cfs.h"
+
+void fill_super(int);
+void init_bim_buffer(int);
+
+__bn_t bmap(int, __inode_t, __bn_t);
+__bn_t create_block(int, __inode_t, __bn_t);
+
+void lsdir(int, struct inode *);
+struct inode *search_file(int, struct inode *, char *);
+
+struct inode *create_file(int, struct inode *, char *, __uint8_t);
+struct inode *mkdir(int, struct inode *, char *, __uint8_t);
+
+static __uint32_t gettime(void);
+
+__uint32_t write_file(int, struct inode *, char *, __uint32_t);
+__uint32_t read_file(int, struct inode *, char *, __uint32_t);
+
+void dsync(int, struct inode *, char *);
+
+
+#define sync_sb() 					\
+	lseek(fd, 512, SEEK_SET);			\
+	write(fd, &sb, sizeof(struct superblock));	\
+	lseek(fd, 0, SEEK_SET);
+
+
+#ifdef DEBUG
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	int fd;
+
+	if (!fname) {
+		printf("fatal: excepted one argument 'file name', but %d given. \n", argc - 1);
+		printf("usage: %s device\n", argv[0]);
+
+		return 1;
+	}
+	
+	fd = open(fname, O_RDWR);
+
+	if (fd < 0) {
+		printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
+		return 1;
+	}
+
+	fill_super(fd);
+	init_bim_buffer(fd);
+
+	lsdir(fd, &sb.root_ino);
+
+	struct inode *inode;
+
+	struct inode *cyyds_c = create_file(fd, &sb.root_ino, "cyyds.c", A_ROOT_RW | A_USER_RW | A_REGFILE);
+
+	if (cyyds_c) {
+		printf("writing to cyyds.c\n");
+
+		char buffer[] = "C is the best language in the wowrld! I will love it forever. because it's very fast and very clear. I love it. ";
+
+		write_file(fd, cyyds_c, buffer, strlen(buffer));
+
+		/* write back that inode (not data) */
+		sync_sb();
+	}
+
+	char buf[250];
+
+	inode = search_file(fd, &sb.root_ino, "cyyds.c");
+
+	read_file(fd, inode, buf, 250);
+
+	printf("\n%s\n", buf);
+
+	inode = mkdir(fd, &sb.root_ino, "usr", A_ROOT_RW | A_USER_RW);
+
+	if (inode) 
+		printf("new directory 'usr'\n");
+
+	lsdir(fd, &sb.root_ino);
+
+	printf("listing /usr/: \n");
+
+	if (!inode) 
+		inode = search_file(fd, &sb.root_ino, "usr");
+
+	lsdir(fd, inode);
+	
+
+	close(fd);
+
+	return 0;
+}
+
+#endif
+
+
+int cfs_init(int fd) {
+	fs_fd = fd;
+
+	fill_super(fd);
+	init_bim_buffer(fd);
+
+	return 0;
+}
+
+
+void fill_super(int fd) {
+	/* read super block from fd */
+
+	lseek(fd, 512, SEEK_SET);
+	read(fd, &sb, sizeof(struct superblock));
+
+	if (sb.magic != MAGIC) {
+		printf("fatal: bad magic number. \n");
+
+		close(fd);
+		exit(1);
+	}
+
+	return;
+}
+
+
+/* 
+ * initial the bim buffer. (struct bim_buffer). 
+ * : read the first block of bim to buffer, and 
+ *   clear 'dirty'. 
+ * */
+void init_bim_buffer(int fd) {
+	bimbuf.this_block = sb.bim_bn;
+	bimbuf.dirty = 0;
+	bimbuf.bn_start = sb.first_data_block;
+
+	bimbuf.buffer = (struct bim_entry *) malloc(BLOCK_SIZE);
+
+	if (!bimbuf.buffer) {
+		printf("fatal: unable to allocate space for bim buffer. (%d bytes requested) \n", BLOCK_SIZE);
+
+		close(fd);
+		exit(1);
+	}
+
+	lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	return;
+}
+
+
+void read_inode(int fd, __inode_t ino, struct inode *inode) {
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
+	read(fd, inode, sizeof(struct inode));
+
+	return;
+}
+
+void write_inode(int fd, __inode_t ino, struct inode *inode) {
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
+	write(fd, inode, sizeof(struct inode));
+
+	return;
+}
+
+
+/* 
+ * a function that used to rw inodes. (get the `bn`th block of inode `ino`)
+ *
+ * STEPS: 
+ * 	1. check in bim buffer. 
+ * 		1a. (found):     return real block. 
+ * 		1b, (not-found): read buffer again (next bim block) and check again. 
+ * */
+
+__bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
+	__bn_t i;
+	struct bim_entry *buf;
+       
+repeat:
+	buf = bimbuf.buffer;
+
+	/* search in buf */
+	for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
+		if (buf[i].inode == ino  && buf[i].th_block == bn) 
+			return i + bimbuf.bn_start;
+	}
+
+	/* not found, so load buffer again and repeat */
+	bimbuf.this_block++;
+	bimbuf.dirty = 0;
+
+	/* += how many blocks it can describe */
+	bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
+
+	lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	goto repeat;
+
+	return 0;
+}
+
+
+void sync_bimbuf(int fd) {
+	lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
+	write(fd, bimbuf.buffer, BLOCK_SIZE);
+	bimbuf.dirty = 0;
+
+	return;
+}
+
+
+/* 
+ * find a new free block, and register it in bim buffer and device. 
+ * */
+
+__bn_t create_block(int fd, __inode_t ino, __bn_t bn) {
+	__bn_t i;
+	struct bim_entry *buf;
+
+repeat:
+	buf = bimbuf.buffer;
+
+	for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
+		if (!buf[i].inode) {
+			/* register it in buffer first */
+			buf[i].inode = ino;
+			buf[i].th_block = bn;
+
+			/* and write it back to device (sync) */
+			sync_bimbuf(fd);
+
+			return i + bimbuf.bn_start;
+		}
+	}
+
+	bimbuf.this_block++;
+	bimbuf.dirty = 0;
+	bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
+
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	goto repeat;
+
+	return 0;
+}
+
+
+/* list the files in root directory, until it's empty */
+void lsdir(int fd, struct inode *dir) {
+	int i;
+	struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
+
+	for (i = 0; i < dir->blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
+			if (!entries[j].ino) continue;
+			printf("%d: %s\n", j, entries[j].name);
+		}
+	}
+
+	return;
+}
+
+
+/* 
+ * search file 'name' in directory 'dir'. 
+ *
+ * 'name' is only a file name, unlike '/foo.bar' or '/foo/bar.foo'
+ * */
+
+struct inode *search_file(int fd, struct inode *dir, char *name) {
+	/* read root directory until find file 'name' */
+
+	int i;
+	struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
+	struct inode *ino = malloc(sizeof(struct inode));
+
+	for (i = 0; i < dir->blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
+				read_inode(fd, entries[j].ino, ino);
+				return ino;
+			}
+		}
+	}
+
+	/* root directory is dirty so it runs here. */
+	return NULL;
+}
+
+
+/* 
+ * sync the directory 'inode' (write all data blocks back)
+ * (calls from create file or other functions that changed inode of the 
+ * child of this directory)
+ * */
+void dsync(int fd, struct inode *inode, char *entries) {
+	int i;
+
+	for (i = 0; i < inode->blocks_nr; i++) {
+		lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		write(fd, entries + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	return;
+}
+
+
+
+/* 
+ * create a file 'name' in directory 'dir' with access mode 'acc'. 
+ * */
+struct inode *create_file(int fd, struct inode *dir, char *name, __uint8_t acc) {
+	/* find a free entry in root directory */
+
+	int i;
+	struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
+	struct inode *ino = malloc(sizeof(struct inode));
+
+	memset(ino, 0x00, sizeof(struct inode));
+
+	for (i = 0; i < sb.root_ino.blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		/* first: check this file is non exists */
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
+				printf("create_file: failed: file '%s' exists (inode %d)\n", 
+					name, entries[j].ino);
+
+				return 0;
+			}
+		}
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (!entries[j].ino) {
+				ino->inode = entries[j].ino = sb.next_inode++;
+
+				ino->pinode = ROOT_INO;
+
+				ino->access = acc;
+				ino->blocks_nr = ino->upsize = 0;
+
+				ino->ctime = ino->wtime = gettime();
+				ino->nlinks = 1;
+
+				ino->uid = ino->gid = 0;
+				ino->eaccess = 0;
+
+				/* copy file name */
+				strncpy(entries[j].name, name, FNAME_LEN);
+				sync_sb();
+				
+				dsync(fd, dir, (char *) entries);
+
+				return ino;
+			}
+		}
+	}
+
+	printf("fatal: create_file: no enough space in this directory\n");
+
+	return 0;
+}
+
+
+__uint32_t write_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
+	/* bad bad, we shouldn't do this */
+
+	char *buffer = malloc(ALIGNUP_BLOCK(nbytes));
+	int i;
+
+	__uint32_t nblocks = ALIGNUP_BLOCK(nbytes) >> BLOCK_SHIFT;
+
+	memcpy(buffer, __buf, nbytes);
+
+	for (i = 0; i < nblocks; i++) {
+		lseek(fd, create_block(fd, inode->inode, inode->blocks_nr + i) << BLOCK_SHIFT, SEEK_SET);
+		write(fd, buffer + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	inode->blocks_nr = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
+	inode->upsize = nbytes - (inode->blocks_nr << BLOCK_SHIFT);
+
+	inode->wtime = gettime();
+
+	free(buffer);
+
+	/* sync, then we can return */
+	write_inode(fd, inode->inode, inode);
+
+	return nbytes;
+}
+
+
+__uint32_t read_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
+	/* first read blocks that aligned */
+	__uint32_t nblocks = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
+	int i;
+
+	for (i = 0; i < nblocks; i++) {
+		lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, __buf + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	if (nblocks << BLOCK_SHIFT != nbytes) {
+		char buf[BLOCK_SIZE];
+
+		lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, buf, BLOCK_SIZE);
+		memcpy(__buf + (i << BLOCK_SHIFT), buf, nbytes - (nbytes & BLOCK_MASK));
+	}
+
+	return nbytes;
+}
+
+
+__uint32_t gettime(void) {
+	struct timeval t = {0};
+
+	gettimeofday(&t, NULL);
+	return t.tv_sec;
+}
+
+
+struct inode *mkdir(int fd, struct inode *dir, char *name, __uint8_t acc) {
+	struct inode *new_dir = malloc(sizeof(struct inode));
+
+	/* first create a file in directory 'dir' */
+	if (!(new_dir = create_file(fd, dir, name, acc & A_DIR & A_EXEC))) {
+		return NULL;
+	}
+
+	new_dir->blocks_nr = 1;
+	create_block(fd, new_dir->inode, 0);
+
+	/* then create '.' and '..' in new directory */
+	create_file(fd, new_dir, ".", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
+	create_file(fd, new_dir, "..", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
+
+	write_inode(fd, new_dir->inode, new_dir);
+
+	return new_dir;
+}

+ 243 - 0
tools/cfs.h

@@ -0,0 +1,243 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of CFS. */
+
+/* 
+  CFS 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 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  CFS is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with CFS. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#ifndef __CFS_H
+#define __CFS_H
+
+#include <sys/types.h>
+
+
+
+typedef __uint32_t __inode_t;
+
+#define BLOCK_SIZE  4096
+#define BLOCK_SHIFT 12ul
+#define BLOCK_MASK  ~(BLOCK_SIZE - 1)
+#define ALIGNUP_BLOCK(n) (((n) + BLOCK_SIZE - 1) & BLOCK_MASK)
+
+/* a map that describes block - inodes */
+/* should I call it ibm or bim? :-) */
+
+struct bim_entry {
+	__inode_t inode;
+	__uint32_t th_block;
+
+/* we don't need this, because this is already 4-bytes aligned */
+} __attribute__ ((packed));
+
+
+struct inode {
+
+#define ROOT_INO 2
+
+	__inode_t inode;
+	__inode_t pinode;
+
+#define A_ROOTRD  (0x01)
+#define A_ROOTWR  (0x02 | A_ROOTRD)
+
+#define A_ROOT_RW (A_ROOTWR | A_ROOTRD)
+
+#define A_USERRD  (0x04 | A_ROOTRD)
+#define A_USERWR  (0x08 | A_ROOT_RW)
+
+#define A_USER_RW (A_USERWR | A_ROOT_RW)
+
+#define A_REGFILE 0x10
+#define A_DIR     0x20
+
+/* A_DIR can exists with A_LINK */
+
+#define A_LINK    0x40
+
+/* directory must be executable */
+#define A_EXEC    0x80
+
+/* we support blkdev & chrdev etc in vfs, not here */
+
+	__uint8_t access;
+
+	/* these also in link files */
+
+	/* aligned blocks nr */
+	__uint32_t blocks_nr;
+
+	/* blocks_nr * BLOCK_SIZE + upsize = fsize (maybe a __uint64_T) */
+	__uint32_t upsize;
+
+	/* create time */
+	__uint32_t ctime;
+
+	/* last write time */
+	__uint32_t wtime;
+
+	/* no access-time here */
+
+	/* zero in link files */
+	__uint8_t nlinks;
+
+	/* creator's uid and gid */
+	__uint16_t uid;
+	__uint16_t gid;
+
+	/* extend access (unuse now) */
+	__uint16_t eaccess;
+} __attribute__ ((packed));
+
+
+#define FNAME_LEN 124
+
+struct dentry {
+	__inode_t ino;
+	char name[FNAME_LEN];
+} __attribute__ ((packed));
+
+typedef __uint32_t __bn_t;
+typedef __uint32_t __nr_t;
+
+struct superblock {
+
+#define MAGIC 0x01328e3b
+
+	__uint32_t magic;
+
+	__nr_t ninodes;
+	__nr_t nblocks;
+
+	/* blocks count of block-bitmap */
+	__nr_t nb_bbm;
+	__nr_t nb_ibm;
+
+	/* block nr of bbm */
+	__bn_t bbm_bn;
+	__bn_t ibm_bn;
+
+	__nr_t nb_bim;
+	__nr_t nb_inodes;
+
+	__bn_t bim_bn;
+	__bn_t inodes_bn;
+
+	/* 32 bytes */
+	struct inode root_ino;
+	
+	__bn_t first_data_block;
+
+	__inode_t next_inode;
+
+	/* 12 bytes */
+	__uint32_t zero[3];
+#ifdef __CFS
+} sb;
+#else 
+};
+extern struct superblock sb;
+#endif
+
+
+struct bim_buffer {
+	/* the block in device of this block (point to bim) */
+	__bn_t this_block;
+	__uint8_t dirty;
+	
+	/* real block the buffer[0] points to (a data block) */
+	__nr_t bn_start;
+
+	/* 4096 bytes, 512 entries  */
+	struct bim_entry *buffer;
+#ifdef __CFS
+} bimbuf;
+#else
+};
+extern struct bim_buffer bimbuf;
+#endif
+
+#ifndef __CFS
+
+/* for applications */
+int fs_fd;
+
+#else 
+extern int fs_fd;
+
+#endif
+
+
+int cfs_init(int);
+
+void fill_super(int);
+void init_bim_buffer(int);
+
+__bn_t bmap(int, __inode_t, __bn_t);
+__bn_t create_block(int, __inode_t, __bn_t);
+
+void lsdir(int, struct inode *);
+struct inode *search_file(int, struct inode *, char *);
+
+struct inode *create_file(int, struct inode *, char *, __uint8_t);
+struct inode *mkdir(int, struct inode *, char *, __uint8_t);
+
+__uint32_t write_file(int, struct inode *, char *, __uint32_t);
+__uint32_t read_file(int, struct inode *, char *, __uint32_t);
+
+#ifndef __CFS
+
+#define sync_ino(buf) 				write_inode(fs_fd, (buf)->inode, buf);
+
+/* get real block of inode's bn-th block, returns real-block nr */
+#define cfs_bmap(ino, bn)			bmap(fs_fd, inode, bn)
+
+/* list the directory 'dir' */
+#define cfs_lsdir(dir) 				lsdir(fs_fd, dir)
+
+/* create a new block for inode 'inode' (of the bn-th block), 
+ *and returns the block number 
+ */
+#define cfs_create_block(inode, bn)		create_block(fs_fd, inode, bn)
+
+/* create a new directory (with a '.' and '..') with name 'name', attribute 'attr', 
+ * returns the new one's inode 
+ */
+#define cfs_mkdir(dir, name, attr)		lsdir(fs_fd, dir, name, attr)
+
+/* search the file 'name' in directory 'dir' */
+#define cfs_search(dir, name)			search_file(fs_fd, dir, name)
+
+/* create a new file in directory 'dir', with name 'name', attribute 'attr' */
+#define cfs_create(dir, name, attr)		create_file(fs_fd, name, attr)
+
+/* write/read a file 'inode' for 'nbytes' bytes, returns bytes write/read */
+#define cfs_write(inode, buf, nbytes) 		write_file(fs_fd, inode, buf, nbytes)
+#define cfs_read(inode, buf, nbytes)		read_file(fs_fd, inode, buf, nbytes)
+
+#endif
+
+#define fsize(inode)				((inode->blocks_nr << BLOCK_SHIFT) + inode->upsize)
+
+#define rootdir (&sb.root_ino)
+
+#endif
+

+ 239 - 0
tools/dumpcfs.c

@@ -0,0 +1,239 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of CFS. */
+
+/* 
+  CFS 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 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  CFS is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with CFS. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <string.h>
+#include <errno.h>
+
+#include <sys/fcntl.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#define __CFS
+#include "cfs.h"
+
+void dump_sb(int);
+void dump_inode(struct inode *);
+void dump_dir(int fd, struct inode *);
+
+__bn_t bmap(int, __inode_t, __bn_t);
+
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	int fd;
+
+	if (!fname) {
+		printf("fatal: excepted one argument 'file name', but %d given. \n", argc - 1);
+		printf("usage: %s device\n", argv[0]);
+
+		return 1;
+	}
+	
+	fd = open(fname, O_RDWR);
+
+	if (fd < 0) {
+		printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
+		return 1;
+	}
+
+	dump_sb(fd);
+
+	close(fd);
+
+	return 0;
+}
+
+void dump_sb(int fd) {
+	lseek(fd, 512, SEEK_SET);
+	read(fd, &sb, sizeof(struct superblock));
+
+	if (sb.magic != MAGIC) {
+		printf("fatal: bad magic number. \n");
+		close(fd);
+
+		exit(1);
+	}
+
+	printf("super block: \n");
+
+	printf("\tmagic number: \t\t0x%08x (ok)\n", sb.magic);
+	printf("\ttotal blocks: \t\t%10d\t0x%08x\n", sb.nblocks, sb.nblocks);
+	printf("\ttotal inodes: \t\t%10d\t0x%08x\n", sb.ninodes, sb.ninodes);
+
+	printf("\tbbm blocks count: \t%10d\t0x%08x\n", sb.nb_bbm, sb.nb_bbm);
+	printf("\tibm blocks count: \t%10d\t0x%08x\n", sb.nb_ibm, sb.nb_ibm);
+	printf("\tbim blocks count: \t%10d\t0x%08x\n", sb.nb_bim, sb.nb_bim);
+	printf("\tinode blocks count: \t%10d\t0x%08x\n", sb.nb_inodes, sb.nb_inodes);
+
+	printf("\tstart block of bbm: \t%10d\t0x%08x\n", sb.bbm_bn, sb.bbm_bn);
+	printf("\tstart block of ibm: \t%10d\t0x%08x\n", sb.ibm_bn, sb.ibm_bn);
+	printf("\tstart block of bim: \t%10d\t0x%08x\n", sb.bim_bn, sb.bim_bn);
+	printf("\tstart block of inodes: \t%10d\t0x%08x\n", sb.inodes_bn, sb.inodes_bn);
+
+	printf("\tfirst data block: \t%10d\t0x%08x\n", sb.first_data_block, sb.first_data_block);
+	printf("\tnext free inode number: %10d\t0x%08x\n", sb.next_inode, sb.next_inode);
+
+	dump_inode(&sb.root_ino);
+	dump_dir(fd, &sb.root_ino);
+
+	return;
+}
+
+
+void read_inode(int fd, __inode_t ino, struct inode *inode) {
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
+	read(fd, inode, sizeof(struct inode));
+
+	return;
+}
+
+void dump_inode(struct inode *inode) {
+	printf("\ninode %10u: ", inode->inode);
+
+	if (inode->inode == ROOT_INO) 
+		printf("(root)\n");
+
+	else 
+		putc('\n', stdout);
+
+	printf("\tparent inode number: \t%10u ", inode->pinode);
+
+	if (inode->pinode == ROOT_INO)
+		printf("(root)\n");
+
+	else 
+		putc('\n', stdout);
+
+
+	printf("\taccess: \t\t%10d\t0x%08x\n", inode->access, inode->access);
+	
+	/* for access */
+	if (inode->access & A_ROOTRD) 
+		printf("\t\t\t\troot readable\t");
+
+	else 
+		printf("\t\t\t\troot unreadable\t");
+
+	if (inode->access & A_ROOTWR) 
+		printf("root writable\t");
+	
+	else 
+		printf("root unwritable\t");
+
+	putc('\n', stdout);
+
+	if (inode->access & A_USERRD) 
+		printf("\t\t\t\tuser readable\t");
+
+	else 
+		printf("\t\t\t\tuser unreadable\t");
+
+	if (inode->access & A_USERWR) 
+		printf("user writable\t");
+
+	else 
+		printf("user unwritable\t");
+
+	putc('\n', stdout);
+
+	if (inode->access & A_REGFILE) 
+		printf("\t\t\t\tregular file\n");
+
+	if (inode->access & A_DIR) 
+		printf("\t\t\t\tdirectory\n");
+
+	if (inode->access & A_LINK) 
+		printf("\t\t\t\thard/soft link\n");
+
+	if (inode->access & A_EXEC) 
+		printf("\t\t\t\texecutable\n");
+
+	printf("\ttotal blocks: \t\t%10u\t0x%08x\n", inode->blocks_nr, inode->blocks_nr);
+	printf("\tup size: \t\t%10u\t0x%08x\n", inode->upsize, inode->upsize);
+
+	printf("\ttotal file size: \t%10u\t", (inode->blocks_nr << BLOCK_SHIFT) + inode->upsize);
+	printf("%d KB %d bytes\n", fsize(inode) >> 10, fsize(inode) % 1024);
+
+	printf("\tcreate time: \t\t%10u\n", inode->ctime);
+	printf("\tlast write time: \t%10u\n", inode->wtime);
+
+	printf("\tuid: \t\t\t%10d\n", inode->uid);
+	printf("\tgid: \t\t\t%10d\n", inode->gid);
+
+	printf("\textend access: \t\t%10d\t0x%08x\n", inode->eaccess, inode->eaccess);
+
+	return;
+}
+
+
+
+void dump_dir(int fd, struct inode *dir) {
+	int i;
+	struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
+
+	printf("directory: \n");
+
+	for (i = 0; i < dir->blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
+			printf("\tinode %u: %s\n", entries[j].ino, entries[j].name);
+
+			struct inode ino;
+
+			read_inode(fd, entries[j].ino, &ino);
+			dump_inode(&ino);
+
+			if (ino.access & A_DIR) 
+				dump_dir(fd, &ino);
+		}
+	}
+
+	return;
+}
+
+__bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
+	__bn_t i;
+	struct bim_entry *buf = (struct bim_entry *) malloc(BLOCK_SIZE);
+
+	lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
+	read(fd, buf, BLOCK_SIZE);
+
+	/* search in buf */
+	for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
+		if (buf[i].inode == ino  && buf[i].th_block == bn) 
+			return i + sb.first_data_block;
+	}
+
+	return 0;
+}

+ 494 - 0
tools/initcfs.c

@@ -0,0 +1,494 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of CFS. */
+
+/* 
+  CFS 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 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  CFS is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with CFS. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <string.h>
+#include <errno.h>
+
+#include <sys/fcntl.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#define __CFS
+#include "cfs.h"
+
+
+void fill_super(int);
+void init_bim_buffer(int);
+
+__bn_t bmap(int, __inode_t, __bn_t);
+__bn_t create_block(int, __inode_t, __bn_t);
+
+void lsdir(int, struct inode *);
+struct inode *search_file(int, struct inode *, char *);
+
+struct inode *create_file(int, struct inode *, char *, __uint8_t);
+struct inode *mkdir(int, struct inode *, char *, __uint8_t);
+
+__uint32_t gettime(void);
+
+__uint32_t write_file(int, struct inode *, char *, __uint32_t);
+__uint32_t read_file(int, struct inode *, char *, __uint32_t);
+
+void dsync(int, struct inode *, char *);
+
+
+#define sync_sb() 					\
+	lseek(fd, 512, SEEK_SET);			\
+	write(fd, &sb, sizeof(struct superblock));	\
+	lseek(fd, 0, SEEK_SET);
+
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	int fd, bootfd, loaderfd, sysfd;
+
+	if (argc < 5) {
+		printf("fatal: excepted four arguments, but %d given. \n", argc - 1);
+		printf("usage: %s device boot loader ksetup\n", argv[0]);
+
+		return 1;
+	}
+	
+	fd = open(fname, O_RDWR);
+
+	if (fd < 0) {
+		printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
+		return 1;
+	}
+
+	fill_super(fd);
+	init_bim_buffer(fd);
+
+	/* write boot to mbr, loader to that space that after superblock */
+	bootfd = open(argv[2], O_RDONLY);
+	loaderfd = open(argv[3], O_RDONLY);
+
+	/* sys (vmxnix) to a file that called vmxnix */
+	sysfd = open(argv[4], O_RDONLY);
+
+	__uint32_t fsize = lseek(sysfd, 0, SEEK_END);
+	lseek(sysfd, 0, SEEK_SET);
+
+	char *buf = malloc(fsize);
+
+	if (!buf) {
+		printf("fatal: failed when allocate space: %u bytes need. ", fsize);
+
+		close(fd);
+		close(bootfd);
+		close(sysfd);
+	}
+
+	int bytesread;
+
+	bytesread = read(bootfd, buf, 512);
+	lseek(fd, 0, SEEK_SET);
+	write(fd, buf, 512);
+
+	close(bootfd);
+
+	printf("boot: %d bytes. \n", bytesread);
+
+	/* max bytes for loader */
+	bytesread = read(loaderfd, buf, BLOCK_SIZE - 512 - sizeof(struct superblock));
+	lseek(fd, 512 + sizeof(struct superblock), SEEK_SET);
+	write(fd, buf, bytesread);
+
+	close(loaderfd);
+
+	printf("loader: %d bytes\n", bytesread);
+
+	bytesread = read(sysfd, buf, fsize);
+
+	struct inode *ino = create_file(fd, &sb.root_ino, "ksetup", 
+			A_ROOTRD | A_USERRD | A_REGFILE | A_EXEC);
+
+	if (!ino) 
+		printf("file vmxnix exists, not continuing. \n");
+
+	else
+		write_file(fd, ino, buf, fsize);
+
+	close(fd);
+
+	printf("vmxnix: %d bytes. \n", bytesread);
+
+	return 0;
+}
+
+
+void fill_super(int fd) {
+	/* read super block from fd */
+
+	lseek(fd, 512, SEEK_SET);
+	read(fd, &sb, sizeof(struct superblock));
+
+	if (sb.magic != MAGIC) {
+		printf("fatal: bad magic number. \n");
+
+		close(fd);
+		exit(1);
+	}
+
+	return;
+}
+
+
+/* 
+ * initial the bim buffer. (struct bim_buffer). 
+ * : read the first block of bim to buffer, and 
+ *   clear 'dirty'. 
+ * */
+void init_bim_buffer(int fd) {
+	bimbuf.this_block = sb.bim_bn;
+	bimbuf.dirty = 0;
+	bimbuf.bn_start = sb.first_data_block;
+
+	bimbuf.buffer = (struct bim_entry *) malloc(BLOCK_SIZE);
+
+	if (!bimbuf.buffer) {
+		printf("fatal: unable to allocate space for bim buffer. (%d bytes requested) \n", BLOCK_SIZE);
+
+		close(fd);
+		exit(1);
+	}
+
+	lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	return;
+}
+
+
+void read_inode(int fd, __inode_t ino, struct inode *inode) {
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
+	read(fd, inode, sizeof(struct inode));
+
+	return;
+}
+
+void write_inode(int fd, __inode_t ino, struct inode *inode) {
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
+	write(fd, inode, sizeof(struct inode));
+
+	return;
+}
+
+
+/* 
+ * a function that used to rw inodes. (get the `bn`th block of inode `ino`)
+ *
+ * STEPS: 
+ * 	1. check in bim buffer. 
+ * 		1a. (found):     return real block. 
+ * 		1b, (not-found): read buffer again (next bim block) and check again. 
+ * */
+
+__bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
+	__bn_t i;
+	struct bim_entry *buf;
+       
+repeat:
+	buf = bimbuf.buffer;
+
+	/* search in buf */
+	for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
+		if (buf[i].inode == ino  && buf[i].th_block == bn) 
+			return i + bimbuf.bn_start;
+	}
+
+	/* not found, so load buffer again and repeat */
+	bimbuf.this_block++;
+	bimbuf.dirty = 0;
+
+	/* += how many blocks it can describe */
+	bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
+
+	lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	goto repeat;
+
+	return 0;
+}
+
+
+void sync_bimbuf(int fd) {
+	lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
+	write(fd, bimbuf.buffer, BLOCK_SIZE);
+	bimbuf.dirty = 0;
+
+	return;
+}
+
+
+/* 
+ * find a new free block, and register it in bim buffer and device. 
+ * */
+
+__bn_t create_block(int fd, __inode_t ino, __bn_t bn) {
+	__bn_t i;
+	struct bim_entry *buf;
+
+repeat:
+	buf = bimbuf.buffer;
+
+	for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
+		if (!buf[i].inode) {
+			/* register it in buffer first */
+			buf[i].inode = ino;
+			buf[i].th_block = bn;
+
+			/* and write it back to device (sync) */
+			sync_bimbuf(fd);
+
+			return i + bimbuf.bn_start;
+		}
+	}
+
+	bimbuf.this_block++;
+	bimbuf.dirty = 0;
+	bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
+
+	read(fd, bimbuf.buffer, BLOCK_SIZE);
+
+	goto repeat;
+
+	return 0;
+}
+
+
+/* list the files in root directory, until it's empty */
+void lsdir(int fd, struct inode *dir) {
+	int i;
+	struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
+
+	for (i = 0; i < dir->blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
+			if (!entries[j].ino) continue;
+			printf("%d: %s\n", j, entries[j].name);
+		}
+	}
+
+	return;
+}
+
+
+/* 
+ * search file 'name' in directory 'dir'. 
+ *
+ * 'name' is only a file name, unlike '/foo.bar' or '/foo/bar.foo'
+ * */
+
+struct inode *search_file(int fd, struct inode *dir, char *name) {
+	/* read root directory until find file 'name' */
+
+	int i;
+	struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
+	struct inode *ino = malloc(sizeof(struct inode));
+
+	for (i = 0; i < dir->blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
+				read_inode(fd, entries[j].ino, ino);
+				return ino;
+			}
+		}
+	}
+
+	/* root directory is dirty so it runs here. */
+	return NULL;
+}
+
+
+/* 
+ * sync the directory 'inode' (write all data blocks back)
+ * (calls from create file or other functions that changed inode of the 
+ * child of this directory)
+ * */
+void dsync(int fd, struct inode *inode, char *entries) {
+	int i;
+
+	for (i = 0; i < inode->blocks_nr; i++) {
+		lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		write(fd, entries + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	return;
+}
+
+
+
+/* 
+ * create a file 'name' in directory 'dir' with access mode 'acc'. 
+ * */
+struct inode *create_file(int fd, struct inode *dir, char *name, __uint8_t acc) {
+	/* find a free entry in root directory */
+
+	int i;
+	struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
+	struct inode *ino = malloc(sizeof(struct inode));
+
+	memset(ino, 0x00, sizeof(struct inode));
+
+	for (i = 0; i < sb.root_ino.blocks_nr; i++) {
+		lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, entries, BLOCK_SIZE);
+
+		int j;
+
+		/* first: check this file is non exists */
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
+				printf("create_file: failed: file '%s' exists (inode %d)\n", 
+					name, entries[j].ino);
+
+				return 0;
+			}
+		}
+
+		for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
+			if (!entries[j].ino) {
+				ino->inode = entries[j].ino = sb.next_inode++;
+
+				ino->pinode = ROOT_INO;
+
+				ino->access = acc;
+				ino->blocks_nr = ino->upsize = 0;
+
+				ino->ctime = ino->wtime = gettime();
+				ino->nlinks = 1;
+
+				ino->uid = ino->gid = 0;
+				ino->eaccess = 0;
+
+				/* copy file name */
+				strncpy(entries[j].name, name, FNAME_LEN);
+				sync_sb();
+				
+				dsync(fd, dir, (char *) entries);
+
+				return ino;
+			}
+		}
+	}
+
+	printf("fatal: create_file: no enough space in this directory\n");
+
+	return 0;
+}
+
+
+__uint32_t write_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
+	/* bad bad, we shouldn't do this */
+
+	char *buffer = malloc(ALIGNUP_BLOCK(nbytes));
+	int i;
+
+	__uint32_t nblocks = ALIGNUP_BLOCK(nbytes) >> BLOCK_SHIFT;
+
+	memcpy(buffer, __buf, nbytes);
+
+	for (i = 0; i < nblocks; i++) {
+		lseek(fd, create_block(fd, inode->inode, inode->blocks_nr + i) << BLOCK_SHIFT, SEEK_SET);
+		write(fd, buffer + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	inode->blocks_nr = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
+	inode->upsize = nbytes - (inode->blocks_nr << BLOCK_SHIFT);
+
+	inode->wtime = gettime();
+
+	free(buffer);
+
+	/* sync, then we can return */
+	write_inode(fd, inode->inode, inode);
+
+	return nbytes;
+}
+
+
+__uint32_t read_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
+	/* first read blocks that aligned */
+	__uint32_t nblocks = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
+	int i;
+
+	for (i = 0; i < nblocks; i++) {
+		lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+		read(fd, __buf + (i << BLOCK_SHIFT), BLOCK_SIZE);
+	}
+
+	char buf[BLOCK_SIZE];
+
+	lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
+	read(fd, buf, BLOCK_SIZE);
+	memcpy(__buf + (i << BLOCK_SHIFT), buf, nbytes - (nbytes & BLOCK_MASK));
+
+	return nbytes;
+}
+
+
+__uint32_t gettime(void) {
+	struct timeval t = {0};
+
+	gettimeofday(&t, NULL);
+	return t.tv_sec;
+}
+
+
+struct inode *mkdir(int fd, struct inode *dir, char *name, __uint8_t acc) {
+	struct inode *new_dir = malloc(sizeof(struct inode));
+
+	/* first create a file in directory 'dir' */
+	if (!(new_dir = create_file(fd, dir, name, acc & A_DIR & A_EXEC))) {
+		return NULL;
+	}
+
+	new_dir->blocks_nr = 1;
+	create_block(fd, new_dir->inode, 0);
+
+	/* then create '.' and '..' in new directory */
+	create_file(fd, new_dir, ".", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
+	create_file(fd, new_dir, "..", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
+
+	write_inode(fd, new_dir->inode, new_dir);
+
+	return new_dir;
+}

+ 34 - 0
tools/lsroot.c

@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <errno.h>
+
+#include <sys/fcntl.h>
+#include <sys/types.h>
+
+#include "cfs.h"
+
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	int fd;
+
+	if (!fname) {
+		printf("usage: lsdir device. \n");
+		return 1;
+	}
+
+	fd = open(fname, O_RDWR);
+	if (fd < 0) {
+		printf("failed when trying to open %s: %s\n", fname, strerror(errno));
+		return 1;
+	}
+
+	cfs_init(fd);
+	cfs_lsdir(rootdir);
+
+	close(fd);
+
+	return 0;
+}

+ 377 - 0
tools/mkfs.c

@@ -0,0 +1,377 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of CFS. */
+
+/* 
+  CFS 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 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  CFS is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with CFS. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#define __CFS
+#include "cfs.h"
+#include "bitmap.h"
+
+
+void fill_super(int);
+void setup_bbm(int);
+void setup_bim(int);
+void setup_ibm(int);
+void setup_root(int);
+
+void setup_rfile(int, char *, struct dentry *, __uint8_t);
+__uint32_t gettime(void);
+
+
+int main(int argc, const char **argv) {
+	char *fname = (char *) argv[1];
+	int fd;
+
+	if (!fname) {
+		printf("fatal: excepted one argument, but %d given. \n", argc - 1);
+		printf("usage: %s device\n", argv[0]);
+
+		return 1;
+	}
+
+	fd = open(fname, O_RDWR | O_CREAT);
+
+	if (fd < 0) {
+		printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
+
+		return 1;
+	}
+
+	printf("fill super block...");
+	fill_super(fd);
+	printf("OK\n");
+
+	printf("setup block bitmap...");
+	setup_bbm(fd);
+	printf("OK\n");
+
+	printf("setup bim");
+	setup_bim(fd);
+	printf("OK\n");
+
+	printf("setup inodes bitmap");
+	setup_ibm(fd);
+	printf("OK\n");
+
+	printf("setup root...");
+	setup_root(fd);
+	printf("OK\n");
+
+	close(fd);
+
+	printf("all done\n");
+
+	return 0;
+}
+
+
+void fill_super(int fd) {
+	/* fill sb, and write it back to the fd */
+	memset(&sb, 0x00, sizeof(struct superblock));
+
+	sb.magic = MAGIC;
+	sb.nblocks = lseek(fd, 0, SEEK_END) >> BLOCK_SHIFT;
+
+	/* inodes size : nblocks = 1 : 16 */
+	sb.nb_inodes = sb.nblocks >> 4;
+	sb.nb_ibm = ALIGNUP_BLOCK(sb.nb_inodes * sizeof(struct inode) >> 3) >> BLOCK_SHIFT;
+
+	sb.ninodes = (sb.nb_inodes << BLOCK_SHIFT) / sizeof(struct  inode);
+	
+	lseek(fd, 0, SEEK_SET);
+
+	/* (sb.nblocks >> 3) + 1 is the size (bytes) of bbm, and align it to blocks */
+	sb.nb_bbm = ALIGNUP_BLOCK((sb.nblocks >> 3) + 1) >> BLOCK_SHIFT;
+
+	/* the second block */
+	sb.bbm_bn = 1;
+
+	/* << 3: * 8 (size of a bim_entry) */
+	sb.nb_bim = ALIGNUP_BLOCK(sb.nblocks << 3) >> BLOCK_SHIFT;
+	sb.bim_bn = 1 + sb.nb_bbm;
+
+	sb.ibm_bn = sb.bim_bn + sb.nb_bim;
+	sb.inodes_bn = sb.ibm_bn + sb.nb_ibm;
+
+	/* root_ino will set in setup_root */
+
+	/* and write back */
+
+#define update_sb() 						\
+	lseek(fd, 512, SEEK_SET);				\
+	write(fd, (char *) &sb, sizeof(struct superblock));	\
+	lseek(fd, 0, SEEK_SET);
+
+
+	/* ignore that MBR (512 bytes) */
+	update_sb();
+
+	return;
+}
+
+
+/* clean bitmap, and then write (only first block) */
+void setup_bbm(int fd) {
+	/* clear all bits in bitmap */
+
+	int bn;
+	char block[BLOCK_SIZE] = {0};
+
+	lseek(fd, sb.bbm_bn << BLOCK_SHIFT, SEEK_SET);
+
+	for (bn = sb.bbm_bn; bn < sb.bbm_bn + sb.nb_bbm; bn++) {
+		/* write one block to fd */
+		write(fd, block, BLOCK_SIZE);
+
+		/* point to next point */
+		lseek(fd, BLOCK_SIZE, SEEK_CUR);
+	}
+
+	/* and calc how many blocks are using */
+
+	__nr_t blocks_used = sb.bbm_bn + sb.nb_bbm + sb.nb_bim + sb.nb_ibm + sb.nb_inodes;
+
+	sb.first_data_block = blocks_used;
+
+	update_sb();
+
+	lseek(fd, sb.bbm_bn << BLOCK_SHIFT, SEEK_SET);
+
+	/* set them */
+
+	unsigned char *bm = malloc(ALIGNUP_BLOCK(blocks_used >> 3));
+
+	if (!bm) {
+		printf("fatal: failed to allocate space for bitmap. %d bytes needs. \n", 
+			blocks_used >> 3);
+
+		exit(1);
+	}
+
+	memset(bm, 0x00, ALIGNUP_BLOCK(blocks_used >> 3));
+
+	for (bn = 0; bn < blocks_used; bn++) 
+		bitmap_set(bm, bn, 1);
+
+	/* write it back */
+
+	for (bn = 0; bn < ALIGNUP_BLOCK(blocks_used >> 3) >> BLOCK_SHIFT; bn++) 
+		write(fd, bm + (bn << BLOCK_SHIFT), BLOCK_SIZE);
+
+	free(bm);
+
+	return;
+}
+
+void setup_ibm(int fd) {
+	int i;
+	char block[BLOCK_SIZE] = {0};
+
+	/* put 16 '.'s on the screen */
+	int points = sb.nb_ibm >> 4;
+
+	lseek(fd, sb.ibm_bn << BLOCK_SHIFT, SEEK_SET);
+
+	for (i = 0; i < sb.nb_ibm; i++) {
+		/* write one block to fd */
+		write(fd, block, BLOCK_SIZE);
+
+		if (points && i % points == 0) 
+			putc('.', stdout);
+	}
+
+	if (!points) 
+		printf("................");
+
+	return;
+}
+
+
+void setup_bim(int fd) {
+	int i;
+	char block[BLOCK_SIZE] = {0};
+
+	/* put 16 '.'s on the screen */
+	int points = sb.nb_bim >> 4;
+
+	lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
+
+	for (i = 0; i < sb.nb_bim; i++) {
+		/* write one block to fd */
+		write(fd, block, BLOCK_SIZE);
+
+		if (i % points == 0) 
+			putc('.', stdout);
+	}
+
+	return;
+}
+
+
+/* 
+ * setup that root directory '/'. 
+ *
+ * STEPS: 
+ * 	1. setup root_ino in sb. 
+ *	2. setup bits in bim (initialize size 4k)
+ *	3. setup dentry
+ * */
+void setup_root(int fd) {
+	sb.root_ino.inode = ROOT_INO;
+	sb.root_ino.pinode = ROOT_INO;
+
+	sb.root_ino.access = A_DIR | A_USERRD | A_ROOT_RW | A_EXEC;
+
+	/* 4096 bytes = 1 block */
+	sb.root_ino.blocks_nr = 1;
+	sb.root_ino.upsize = 0;
+
+	sb.root_ino.ctime = sb.root_ino.wtime = gettime();
+
+	/* '.' + '..' */
+	sb.root_ino.nlinks = 2;
+
+	/* unused now in user space */
+	sb.root_ino.uid = sb.root_ino.gid = 0;
+	sb.root_ino.eaccess = 0;
+
+	update_sb();
+
+	/* okay, now setup bim */
+	
+	struct bim_entry entry;
+
+	memset(&entry, 0x00, sizeof(struct bim_entry));
+	
+	entry.inode = ROOT_INO;
+	entry.th_block = 0;
+
+	/* write back */
+	lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
+	write(fd, &entry, sizeof(struct bim_entry));
+
+	/* now root's 1st block is the first_data_block */
+	/* setup '.' and '..' in it */
+
+	struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
+	memset(entries, 0x00, BLOCK_SIZE);
+
+	/* for clean these */
+	lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
+	write(fd, (void *) entries, BLOCK_SIZE);
+
+	sb.next_inode = ROOT_INO + 1;
+
+	setup_rfile(fd, ".", entries, A_DIR | A_LINK | A_USERRD | A_ROOT_RW);
+	setup_rfile(fd, "..", entries, A_DIR | A_LINK | A_USERRD | A_ROOT_RW);
+
+	update_sb();
+
+	/* write back to device */
+	lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
+	write(fd, entries, BLOCK_SIZE);
+
+	return;
+}
+
+
+
+static __inode_t find_free_inode(int fd) {
+	int i = 0;
+	__uint8_t block[BLOCK_SIZE];
+
+repeat:
+	lseek(fd, (sb.ibm_bn + i) << BLOCK_SHIFT, SEEK_SET);
+	read(fd, block, BLOCK_SIZE);
+
+	int j;
+
+	for (j = 0; bitmap_read(block, j) && j < 4096; j++);
+
+	if (j == 4096) {
+		i++;
+		goto repeat;
+	}
+
+	bitmap_set(block, j, 1);
+	lseek(fd, (sb.ibm_bn + i) << BLOCK_SHIFT, SEEK_SET);
+	write(fd, block, BLOCK_SIZE);
+
+	return (i << BLOCK_SHIFT << 3) + j;
+}
+
+
+void setup_rfile(int fd, char *fname, struct dentry *dentry, __uint8_t acc) {
+	__inode_t ino = find_free_inode(fd);
+
+	struct inode inode;
+
+	inode.inode = sb.next_inode++;
+
+	inode.pinode = ROOT_INO;
+	inode.access = acc;
+	inode.blocks_nr = 0;
+	inode.upsize = 0;
+
+	inode.ctime = inode.wtime = gettime();
+	inode.nlinks = 1;
+
+	inode.uid = inode.gid = 0;
+	inode.eaccess = 0;
+
+	lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + inode.inode * sizeof(struct inode), SEEK_SET);
+	write(fd, &inode, sizeof(struct inode));
+
+	dentry[ino].ino = inode.inode;
+	strncpy(dentry[ino].name, fname, FNAME_LEN - 1);
+	/* and sync dentry */
+
+	/* we only sync the first block, because this is mkfs. :-) */
+	lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
+	write(fd, dentry, BLOCK_SIZE);
+
+	return;
+
+#undef inode
+}
+
+
+__uint32_t gettime(void) {
+	struct timeval t = {0};
+
+	gettimeofday(&t, NULL);
+	return t.tv_sec;
+}
+