aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojtek Kosior <kwojtus@protonmail.com>2019-12-27 22:09:21 +0100
committerWojtek Kosior <kwojtus@protonmail.com>2019-12-27 22:09:21 +0100
commit6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99 (patch)
tree8228993133fc500170229416c7e9082f0813604f
parent19efd77944dd42c3451a19e60a6948a52bc9ccad (diff)
downloadrpi-MMU-example-6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99.tar.gz
rpi-MMU-example-6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99.zip
add userspace tool for creating ramfs image
-rw-r--r--.gitignore1
-rw-r--r--Makefile3
-rw-r--r--makefs.c67
3 files changed, 71 insertions, 0 deletions
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 <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;
+}
+