From 2d10d42f0b877e14800be3417e04b7e402fdb8c9 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Mon, 29 Dec 2014 18:19:55 +0100 Subject: [PATCH] add tool to creat an init ram disk --- tools/Makefile | 28 +++++++++ tools/make_initrd.c | 134 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 tools/Makefile create mode 100644 tools/make_initrd.c diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..928168e --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,28 @@ +MAKE = make +CC = gcc +CFLAGS = -O2 -Wall #-m32 +LDFLGAS = +FILE = ../README.md + +# other implicit rules +%.o : %.c + $(CC) -c $(CFLAGS) -o $@ $< + +default: all + +all: make_initrd initrd.img + +initrd.img: $(EXECFILES) make_initrd + ./make_initrd /tmp $(FILE) $(shell basename $(FILE)) + +make_initrd: make_initrd.o + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + $(RM) -rf *.o *~ make_initrd initrd.img + +depend: + $(CC) -MM $(CFLAGS) *.c > Makefile.dep + +-include Makefile.dep +# DO NOT DELETE diff --git a/tools/make_initrd.c b/tools/make_initrd.c new file mode 100644 index 0000000..494b68e --- /dev/null +++ b/tools/make_initrd.c @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2010, Stefan Lankes, RWTH Aachen University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#define INITRD_MAGIC_NUMBER 0x4711 +#define MAX_FNAME 128 +#define PAGE_SIZE 4096 + +typedef struct { + uint32_t magic; + uint32_t nfiles; + char mount_point[MAX_FNAME]; +} initrd_header_t; + +typedef struct { + uint32_t length; + uint32_t offset; + char fname[MAX_FNAME]; +} initrd_file_desc_t; + +static void print_options(void) +{ + printf(" make_initrd mount_point path name [path name]\n"); + printf("\n"); + printf(" mount_point - mount point of init ram disk, where all file will be mounted.\n"); + printf(" path - path to the file, which will be mounted\n"); + printf(" name - file name, which will be used be the initrd\n"); +} + +int main(int argc, char **argv) +{ + int i, nfiles = (argc - 2) / 2; + initrd_header_t header; + initrd_file_desc_t* file_desc; + off_t offset; + FILE* istream; + FILE* ostream; + + if ((argc < 4) || (strcmp(argv[1], "-h") == 0)) { + print_options(); + return 0; + } + + memset(&header, 0x00, sizeof(initrd_header_t)); + header.magic = INITRD_MAGIC_NUMBER; + header.nfiles = nfiles; + strncpy(header.mount_point, argv[1], MAX_FNAME); + + file_desc = (initrd_file_desc_t*) malloc(sizeof(initrd_file_desc_t)*nfiles); + if (!file_desc) { + fprintf(stderr, "No enough memory\n"); + return -1; + } + memset(file_desc, 0x00, sizeof(initrd_file_desc_t)*nfiles); + offset = sizeof(initrd_header_t) + nfiles * sizeof(initrd_file_desc_t); + + for(i=0; i 0); + + fclose(istream); + free(buf); + } + + fclose(ostream); + + return 0; +}