blob: 91170e4c37f1e8e62ec132eeb72774e6a161a875 [file] [log] [blame]
Thomas Heijligend1e04572023-11-27 14:28:55 +00001#include <stdio.h>
Thomas Heijligenb00b3162023-11-29 10:02:31 +00002#include <stdbool.h>
3#include <getopt.h>
Nico Huberd7001cf2024-01-23 16:26:49 +01004#include <ctype.h>
Thomas Heijligend1e04572023-11-27 14:28:55 +00005
Thomas Heijligen62268ee2023-11-27 15:10:41 +00006#include "blockdev.h"
Thomas Heijligen75d1ff32023-12-04 13:40:11 +00007#include "vfs.h"
Thomas Heijligenb00b3162023-11-29 10:02:31 +00008
Nico Huber9775da22024-01-23 16:23:47 +01009extern void adainit (void);
10extern void adafinal (void);
11
Thomas Heijligenb00b3162023-11-29 10:02:31 +000012struct program_options {
13 char *devname;
14};
15
16static int get_options(int argc, char* argv[], struct program_options* opt);
17
Nico Huberd7001cf2024-01-23 16:26:49 +010018static void fs_tests(void);
19static void fs_test_file(const char *path);
Thomas Heijligend1e04572023-11-27 14:28:55 +000020
21int main(int argc, char* argv[])
22{
Nico Huberf646c8e2024-01-23 16:24:57 +010023 struct program_options opt = { 0, };
Nico Huber9775da22024-01-23 16:23:47 +010024 int ret = -1;
25
26 adainit();
Thomas Heijligenb00b3162023-11-29 10:02:31 +000027
28 if (get_options(argc, argv, &opt)) {
Nico Huber9775da22024-01-23 16:23:47 +010029 goto final;
Thomas Heijligend1e04572023-11-27 14:28:55 +000030 }
31
Thomas Heijligenb00b3162023-11-29 10:02:31 +000032
33 if (devopen(opt.devname, NULL) != 1) {
Nico Huber9775da22024-01-23 16:23:47 +010034 goto final;
Thomas Heijligend1e04572023-11-27 14:28:55 +000035 }
36
Nico Huber9775da22024-01-23 16:23:47 +010037 fs_tests();
Thomas Heijligend1e04572023-11-27 14:28:55 +000038
Thomas Heijligend1e04572023-11-27 14:28:55 +000039 devclose();
Nico Huber9775da22024-01-23 16:23:47 +010040 ret = 0;
41
42final:
43 adafinal();
44 return ret;
Thomas Heijligend1e04572023-11-27 14:28:55 +000045}
46
Thomas Heijligenb00b3162023-11-29 10:02:31 +000047
48
49static void fprint_help(FILE* to, char * pname)
50{
51 fprintf(to, "Usage: %s\n"
52 " -d | --dev <path> \t File to use as block device\n"
53 , pname);
54}
55
56static int get_options(int argc, char* argv[], struct program_options *opt){
57 const struct option long_options[] = {
58 {"dev", required_argument, NULL, 'd'},
59 {NULL, 0, NULL, 0},
60 };
61
62 int c, option_index = 0;
63 while ((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1) {
64 switch (c) {
65 case 'd':
66 opt->devname = optarg;
67 break;
68 default:
69 fprint_help(stderr, argv[0]);
70 return -1;
71 }
72 }
73
74 if (argc == 1) {
75 fprintf(stderr, "%s needs arguments.\n", argv[0]);
76 fprint_help(stderr, argv[0]);
77 return -1;
78 }
79
80 if (opt->devname == NULL) {
81 fprintf(stderr, "Argument \"--dev/-d\" missing.\n");
82 return -1;
83 }
84 return 0;
85}
Nico Huberd7001cf2024-01-23 16:26:49 +010086
87static void fs_tests(void)
88{
89 if (!mount_fs(NULL)) {
90 fprintf(stderr, "ERROR: Failed to mount FS.\n");
91 return;
92 }
93
94 fs_test_file("/test");
95 fs_test_file("/dir/test");
96 fs_test_file("/dir/subdir/test");
97}
98
99static void fs_test_file(const char *path)
100{
101 if (!file_open(path)) {
102 fprintf(stderr, "ERROR: Failed to open `%s'.\n", path);
103 return;
104 }
105
106 unsigned char motd[1024];
107 const int read = file_read(motd, sizeof(motd));
108 if (read < 0) {
109 fprintf(stderr, "ERROR: Failed to read from `%s'.\n", path);
110 return;
111 }
112
113 for (size_t i = 0; i < (size_t)(read + 15) / 16 * 16; ++i) {
114 if (i % 16 == 0)
Nico Huberd55fa512024-01-26 23:54:43 +0100115 printf("%06zx: ", i);
Nico Huberd7001cf2024-01-23 16:26:49 +0100116 else if (i % 8 == 0)
117 printf(" ");
118 if (i < read)
119 printf("%02x ", motd[i]);
120 else
121 printf(" ");
122 if (i % 16 == 15) {
123 printf(" |");
124 for (size_t j = i - 15; j <= (i < read ? i : read - 1); ++j)
125 printf("%c", isprint(motd[j]) ? motd[j] : '.');
126 for (size_t j = i % 16 + 1; j < 16; ++j)
127 printf(" ");
128 printf("|\n");
129 }
130 }
131 printf("\n");
132
133 file_close();
134}