diff options
author | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-27 22:09:21 +0100 |
---|---|---|
committer | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-27 22:09:21 +0100 |
commit | 6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99 (patch) | |
tree | 8228993133fc500170229416c7e9082f0813604f | |
parent | 19efd77944dd42c3451a19e60a6948a52bc9ccad (diff) | |
download | rpi-MMU-example-6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99.tar.gz rpi-MMU-example-6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99.zip |
add userspace tool for creating ramfs image
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | makefs.c | 67 |
3 files changed, 71 insertions, 0 deletions
@@ -2,3 +2,4 @@ *.elf *.img pipe_image +makefs @@ -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 <stdint.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <err.h> +#include <sys/stat.h> +#include <string.h> + +#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; +} + |