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