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