From 6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 27 Dec 2019 22:09:21 +0100 Subject: add userspace tool for creating ramfs image --- .gitignore | 1 + Makefile | 3 +++ makefs.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 makefs.c diff --git a/.gitignore b/.gitignore index db6eb43..480597b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.elf *.img pipe_image +makefs diff --git a/Makefile b/Makefile index d782eda..3d6b095 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,9 @@ run-on-rpi : kernel.img pipe_image pipe_image : pipe_image.c lib/rs232/rs232.c gcc -Wall -std=gnu99 -O3 $^ -o $@ +makefs : makefs.c + gcc -Wall -std=gnu99 -O3 $^ -o $@ + clean : -rm *.img *.elf *.o pipe_image diff --git a/makefs.c b/makefs.c new file mode 100644 index 0000000..7d5b148 --- /dev/null +++ b/makefs.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ANSI_FG_RED "\033[0;31m" +#define ANSI_FG_DEFAULT "\033[0;39m" + +int main(int argc, char **argv) +{ + for (int i = 1; i < argc; i++) + { + struct stat fileinfo; + + if (stat(argv[i], &fileinfo)) + err(-1, "couldn't stat " ANSI_FG_RED "%s" ANSI_FG_DEFAULT, + argv[i]); + + if (!S_ISREG(fileinfo.st_mode)) + errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT + " is not a regular file.", argv[i]); + + if (fileinfo.st_size > UINT32_MAX) + errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT + " is too big.", argv[i]); + + uint32_t file_size = fileinfo.st_size; + uint32_t name_size = strlen(argv[i]); + + if (fwrite(&name_size, 4, 1, stdout) != 1) + errx(-1, "error writing to stdout"); + + if (printf("%s", argv[i]) != name_size) + errx(-1, "error writing to stdout"); + + if (fwrite(&file_size, 4, 1, stdout) != 1) + errx(-1, "error writing to stdout"); + + if (fflush(stdout)) + err(-1, "couldn't flush stdout"); + + pid_t pid; + int wstatus; + switch (pid = fork()) + { + case -1: + err(-1, "couldn't fork"); + case 0: + if (execlp("cat", "cat", argv[i], NULL)) + err(-1, "couldn't execute cat"); + default: + if (wait(&wstatus) == -1) + err(-1, "error waiting for child"); + + if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) + exit(-1); + } + } + + return 0; +} + -- cgit v1.2.3