From 7d5a38ada35ac196919c26675f211adb995b84ae Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Fri, 18 May 2012 14:47:44 +0200 Subject: Added initrd. Status: VFS support, totally useless shell. --- src/tools/makeinitrd/Makefile | 30 +++++++++++++ src/tools/makeinitrd/main.cpp | 95 ++++++++++++++++++++++++++++++++++++++++ src/tools/makeinitrd/makeinitrd | Bin 0 -> 16457 bytes 3 files changed, 125 insertions(+) create mode 100644 src/tools/makeinitrd/Makefile create mode 100644 src/tools/makeinitrd/main.cpp create mode 100755 src/tools/makeinitrd/makeinitrd (limited to 'src/tools') diff --git a/src/tools/makeinitrd/Makefile b/src/tools/makeinitrd/Makefile new file mode 100644 index 0000000..3ac993c --- /dev/null +++ b/src/tools/makeinitrd/Makefile @@ -0,0 +1,30 @@ +.PHONY: clean, mrproper + +CC = gcc +CXX = g++ +LD = gcc +LDFLAGS = -lstdc++ +CFLAGS = +CXXFLAGS = + +OutFile = makeinitrd +Objects = main.o + +all: $(OutFile) + +rebuild: mrproper all + +$(OutFile): $(Objects) + $(LD) -o $(OutFile) $(LDFLAGS) $^ + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) + +%.o: %.cpp + $(CXX) -c $< -o $@ $(CXXFLAGS) + +clean: + rm -rf *.o + +mrproper: clean + rm -rf $(OutFile) diff --git a/src/tools/makeinitrd/main.cpp b/src/tools/makeinitrd/main.cpp new file mode 100644 index 0000000..ea8966b --- /dev/null +++ b/src/tools/makeinitrd/main.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +using namespace std; + +struct ramfs_header { + unsigned int magic; //For error checking + unsigned int files; +}; + +struct ramfs_file_header { + unsigned int name_length; + unsigned int file_length; +}; + +#define INITRD_MAGIC 0x12379846 + +int main(int argc, char *argv[]) { + if (argc < 2) { + cerr << "Error : no output file specified." << endl; + cerr << "Usage : MakeRamFS [: [...] ]" << endl; + return 1; + } + + ofstream output(argv[1], ios::out | ios::binary); + + ramfs_header hdr; + hdr.magic = INITRD_MAGIC; + hdr.files = argc - 2; + + output.write((char*)&hdr, sizeof(ramfs_header)); + + for (int i = 2; i < argc; i++) { + string name(argv[i]); + string file; + while (!name.empty()) { + if (name[0] == ':') { + name = name.substr(1, name.size() - 1); + break; + } + file += name[0]; + name = name.substr(1, name.size() - 1); + } + + ramfs_file_header fhdr; + + if (file == "") { //This is a directory + fhdr.name_length = name.size(); + fhdr.file_length = 0; //File length of 0 means directory + output.write((char*)&fhdr, sizeof(ramfs_file_header)); + output << name; + output << '\0'; + continue; + } + + ifstream infile(file.c_str(), ios::in | ios::binary); + + if (!infile) { + fhdr.name_length = 0; //Name and length = 0 means invalid file + fhdr.file_length = 0; + output.write((char*)&fhdr, sizeof(ramfs_file_header)); + continue; + } + + fhdr.name_length = name.size(); + fhdr.file_length = 0; + while (!infile.eof()) { + char c; + infile.read(&c, 1); + fhdr.file_length++; + } + + infile.close(); infile.open(file.c_str(), ios::in | ios::binary); //Rewind file + + output.write((char*)&fhdr, sizeof(ramfs_file_header)); + + output << name; + output << '\0'; + + char *c = new char[fhdr.file_length]; + for (int i = 0; i < fhdr.file_length; i++) { + char ch; + infile.read(&ch, 1); + output.write(&ch, 1); + } + delete [] c; + + infile.close(); + } + + output.close(); + + return 0; +} diff --git a/src/tools/makeinitrd/makeinitrd b/src/tools/makeinitrd/makeinitrd new file mode 100755 index 0000000..afb042c Binary files /dev/null and b/src/tools/makeinitrd/makeinitrd differ -- cgit v1.2.3