Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 2 | #include <stdbool.h> |
| 3 | #include <getopt.h> |
Nico Huber | d7001cf | 2024-01-23 16:26:49 +0100 | [diff] [blame] | 4 | #include <ctype.h> |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 5 | |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 6 | #include "blockdev.h" |
Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 7 | #include "vfs.h" |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 8 | |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 9 | extern void adainit (void); |
| 10 | extern void adafinal (void); |
| 11 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 12 | struct program_options { |
| 13 | char *devname; |
| 14 | }; |
| 15 | |
| 16 | static int get_options(int argc, char* argv[], struct program_options* opt); |
| 17 | |
Nico Huber | d7001cf | 2024-01-23 16:26:49 +0100 | [diff] [blame] | 18 | static void fs_tests(void); |
| 19 | static void fs_test_file(const char *path); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 20 | |
| 21 | int main(int argc, char* argv[]) |
| 22 | { |
Nico Huber | f646c8e | 2024-01-23 16:24:57 +0100 | [diff] [blame] | 23 | struct program_options opt = { 0, }; |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 24 | int ret = -1; |
| 25 | |
| 26 | adainit(); |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 27 | |
| 28 | if (get_options(argc, argv, &opt)) { |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 29 | goto final; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 32 | |
| 33 | if (devopen(opt.devname, NULL) != 1) { |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 34 | goto final; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 37 | fs_tests(); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 38 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 39 | devclose(); |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame] | 40 | ret = 0; |
| 41 | |
| 42 | final: |
| 43 | adafinal(); |
| 44 | return ret; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | static 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 | |
| 56 | static 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 Huber | d7001cf | 2024-01-23 16:26:49 +0100 | [diff] [blame] | 86 | |
| 87 | static 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 | |
| 99 | static 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 Huber | d55fa51 | 2024-01-26 23:54:43 +0100 | [diff] [blame] | 115 | printf("%06zx: ", i); |
Nico Huber | d7001cf | 2024-01-23 16:26:49 +0100 | [diff] [blame] | 116 | 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 | } |