]> git.sesse.net Git - pistorm/blob - platforms/amiga/pistorm-dev/pistorm_dev_amiga/simple_interact.c
Cleanup warnings and LinuxJedi's docs
[pistorm] / platforms / amiga / pistorm-dev / pistorm_dev_amiga / simple_interact.c
1 // SPDX-License-Identifier: MIT
2
3 #include "../pistorm-dev-enums.h"
4 #include "pistorm_dev.h"
5
6 //#define SHUTUP_VSCODE
7
8 #ifdef SHUTUP_VSCODE
9 #define __stdargs
10 #else
11 #include <exec/types.h>
12 #include <exec/resident.h>
13 #include <exec/errors.h>
14 #include <exec/memory.h>
15 #include <exec/lists.h>
16 #include <exec/alerts.h>
17 #include <exec/tasks.h>
18 #include <exec/io.h>
19 #include <exec/execbase.h>
20
21 #include <devices/trackdisk.h>
22 #include <devices/timer.h>
23 #include <devices/scsidisk.h>
24
25 #include <libraries/filehandler.h>
26
27 #include <proto/exec.h>
28 #include <proto/disk.h>
29 #include <proto/expansion.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #define LOADLIB(a, b) if ((a = (struct a*)OpenLibrary(b,0L))==NULL) { \
37     printf("Failed to load %s.\n", b); \
38     return 1; \
39   }
40
41 void print_usage(char *exe);
42 int get_command(char *cmd);
43
44 extern unsigned int pistorm_base_addr;
45
46 unsigned short cmd_arg = 0;
47
48 int __stdargs main (int argc, char *argv[]) {
49     if (argc < 2) {
50         print_usage(argv[0]);
51         return 1;
52     }
53     
54     int command = get_command(argv[1]);
55     if (command == -1) {
56         printf("Unknown command %s.\n", argv[1]);
57         return 1;
58     }
59
60     pistorm_base_addr = pi_find_pistorm();
61
62     if (pistorm_base_addr == 0xFFFFFFFF) {
63         printf ("Unable to find PiStorm autoconf device.\n");
64         return 1;
65     }
66     else {
67         printf ("PiStorm autoconf device found at $%.X\n", pistorm_base_addr);
68     }
69
70     unsigned int tmpvalue = 0;
71     unsigned short tmpshort = 0;
72
73     if (tmpvalue) {};
74
75     switch (command) {
76         case PI_CMD_RESET:
77             if (argc >= 3)
78                 tmpshort = (unsigned short)atoi(argv[2]);
79             pi_reset_amiga(tmpshort);
80             break;
81         case PI_CMD_SWREV:
82             printf ("PiStorm ----------------------------\n");
83             printf ("Hardware revision: %d.%d\n", (pi_get_hw_rev() >> 8), (pi_get_hw_rev() & 0xFF));
84             printf ("Software revision: %d.%d\n", (pi_get_sw_rev() >> 8), (pi_get_sw_rev() & 0xFF));
85             printf ("RTG: %s - %s\n", (pi_get_rtg_status() & 0x01) ? "Enabled" : "Disabled", (pi_get_rtg_status() & 0x02) ? "In use" : "Not in use");
86             printf ("NET: %s\n", pi_get_net_status() ? "Enabled" : "Disabled");
87             printf ("PiSCSI: %s\n", pi_get_piscsi_status() ? "Enabled" : "Disabled");
88             break;
89         case PI_CMD_SWITCHCONFIG:
90             if (cmd_arg == PICFG_LOAD) {
91                 if (argc < 3) {
92                     printf ("User asked to load config, but no config filename specified.\n");
93                 }
94             }
95             pi_handle_config(cmd_arg, argv[2]);
96             break;
97         case PI_CMD_TRANSFERFILE:
98             if (argc < 4) {
99                 printf ("Please specify a source and destination filename in addition to the command.\n");
100                 printf ("Example: %s --transfer platforms/platform.h snakes.h\n", argv[0]);
101             }
102             if (pi_get_filesize(argv[2], &tmpvalue) == PI_RES_FILENOTFOUND) {
103                 printf ("File %s not found on the Pi side.\n", argv[2]);
104             } else {
105                 unsigned int filesize = tmpvalue;
106                 unsigned char *dest = malloc(filesize);
107
108                 if (dest == NULL) {
109                     printf ("Failed to allocate memory buffer for file. Aborting file transfer.\n");
110                 } else {
111                     printf ("Found a %u byte file on the Pi side. Eating it.\n", filesize);
112                     if (pi_transfer_file(argv[2], dest) != PI_RES_OK) {
113                         printf ("Something went horribly wrong during the file transfer.\n");
114                     } else {
115                         FILE *out = fopen(argv[3], "wb+");
116                         if (out == NULL) {
117                             printf ("Failed to open output file %s for writing.\n", argv[3]);
118                         } else {
119                             fwrite(dest, filesize, 1, out);
120                             fclose(out);
121                             printf ("%u bytes transferred to file %s.\n", filesize, argv[3]);
122                         }
123                     }
124                     free(dest);
125                 }
126             }
127             break;
128         default:
129             printf ("Unhandled command %s.\n", argv[1]);
130             return 1;
131             break;
132     }
133
134     return 0;
135 }
136
137 int get_command(char *cmd) {
138     if (strcmp(cmd, "--restart") == 0 || strcmp(cmd, "--reboot") == 0 || strcmp(cmd, "--reset") == 0) {
139         return PI_CMD_RESET;
140     }
141     if (strcmp(cmd, "--check") == 0 || strcmp(cmd, "--find") == 0 || strcmp(cmd, "--info") == 0) {
142         return PI_CMD_SWREV;
143     }
144     if (strcmp(cmd, "--config") == 0 || strcmp(cmd, "--config-file") == 0 || strcmp(cmd, "--cfg") == 0) {
145         cmd_arg = PICFG_LOAD;
146         return PI_CMD_SWITCHCONFIG;
147     }
148     if (strcmp(cmd, "--config-reload") == 0 || strcmp(cmd, "--reload-config") == 0 || strcmp(cmd, "--reloadcfg") == 0) {
149         cmd_arg = PICFG_RELOAD;
150         return PI_CMD_SWITCHCONFIG;
151     }
152     if (strcmp(cmd, "--config-default") == 0 || strcmp(cmd, "--default-config") == 0 || strcmp(cmd, "--defcfg") == 0) {
153         cmd_arg = PICFG_DEFAULT;
154         return PI_CMD_SWITCHCONFIG;
155     }
156     if (strcmp(cmd, "--transfer-file") == 0 || strcmp(cmd, "--transfer") == 0 || strcmp(cmd, "--getfile") == 0) {
157         return PI_CMD_TRANSFERFILE;
158     }
159
160     return -1;
161 }
162
163 void print_usage(char *exe) {
164     printf ("Usage: %s --[command] (arguments)\n", exe);
165     printf ("Example: %s --restart, --reboot or --reset\n", exe);
166     printf ("         Restarts the Amiga.\n");
167     printf ("         %s --check, --find or --info\n", exe);
168     printf ("         Finds the PiStorm device and prints some data.\n");
169
170     return;
171 }