-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathudump.c
More file actions
241 lines (216 loc) · 6.27 KB
/
Copy pathudump.c
File metadata and controls
241 lines (216 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* This file is part of uDump.
*
* uDump is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* uDump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uDump. If not, see <https://www.gnu.org/licenses/>.
*
* (c) 2018-2021 Miro Kropacek; miro.kropacek@gmail.com
*/
#include <mint/cookie.h>
#include <mint/osbind.h>
#include <mint/sysvars.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static long get_tos_header(void)
{
return *_sysbase;
}
static long get_memory_size(void)
{
return *phystop;
}
static const char country_codes[] =
"usdefrukesitsefsgstrfinodksanlczhuplltrueebyuaskrobgslhrcscsmkgrlvilzaptbejpcnkpvninirmnnplakhidbd";
static const char* countries[] = {
"United States",
"Germany",
"France",
"United Kingdom",
"Spain",
"Italy",
"Sweden",
"Switzerland (French)",
"Switzerland (German)",
"Turkey",
"Finland",
"Norway",
"Denmark",
"Saudi Arabia",
"Netherlands",
"Czech Republic",
"Hungary",
"Poland",
"Lithuania",
"Russia",
"Estonia",
"Belarus",
"Ukraine",
"Slovak Republic",
"Romania",
"Bulgaria",
"Slovenia",
"Croatia",
"Serbia",
"Montenegro",
"Macedonia",
"Greece",
"Latvia",
"Israel",
"South Africa",
"Portugal",
"Belgium",
"Japan",
"China",
"Korea",
"Vietnam",
"India",
"Iran",
"Mongolia",
"Nepal",
"Lao People's Democratic Republic",
"Cambodia",
"Indonesia",
"Bangladesh"
};
static const size_t countries_size = sizeof(countries) / sizeof(countries[0]);
int main(int argc, const char* argv[])
{
OSHEADER* tos_header;
uintptr_t tos_base;
uint16_t tos_version;
uint32_t tos_build_date;
uint8_t tos_country;
int is_emutos;
long mch_value = 0;
long ct60_value = 0;
char* machine;
uint32_t machine_mem;
size_t tos_size = 0;
char tos_filename[8+1+3+1] = "filename.ext";
char country_code[2+1] = "";
char* error_str;
FILE* f;
tos_header = (OSHEADER*)(Supexec(get_tos_header));
tos_base = (uintptr_t)tos_header;
tos_version = tos_header->os_version;
tos_build_date = tos_header->os_date;
tos_country = tos_header->os_conf >> 1;
is_emutos = (uint32_t)tos_header->p_rsv2 == 0x45544f53UL; /* 'ETOS' */
if (is_emutos) {
/*
* EmuTOS before 2011-04-27 had the date in format YYYY-MM-DD
* but TOS ROMs expect it to be in MM-DD-YYYY
*/
if ((tos_build_date & 0xff000000UL) >= 0x19000000UL) {
tos_build_date = ((tos_build_date & 0xffff) << 16) | ((tos_build_date >> 16) & 0xffff);
}
}
Getcookie(0x5f4d4348UL, &mch_value); /* '_MCH' */
Getcookie(0x43543630UL, &ct60_value); /* 'CT60' */
if (tos_country < countries_size) {
country_code[0] = country_codes[tos_country*2];
country_code[1] = country_codes[tos_country*2+1];
country_code[2] = '\0';
}
switch (mch_value) {
case 0x00000000UL:
machine = "Atari ST";
break;
case 0x00010000UL:
machine = "Atari STE";
break;
case 0x00010001UL:
machine = "ST Book";
break;
case 0x00010010UL:
machine = "Atari Mega STE";
break;
case 0x00010100UL:
machine = "Atari Sparrow";
break;
case 0x00020000UL:
machine = "Atari TT/Hades";
break;
case 0x00030000UL:
machine = (ct60_value == 0) ? "Atari Falcon" : "Atari Falcon (CT60)";
break;
case 0x00040000UL:
machine = "Milan";
break;
case 0x00050000UL:
machine = "ARAnyM";
break;
default:
machine = "unknown";
}
machine_mem = Supexec(get_memory_size);
printf("%s %02x.%02x%s\r\n\r\n"
"Build date: %02x.%02x.%04x\r\n"
"Country: %s\r\n"
"Machine: %s\r\n"
"Memory: %d KiB\r\n",
is_emutos ? "EmuTOS" : "TOS",
tos_version >> 8, tos_version & 0x00ff,
country_code,
(tos_build_date >> 16) & 0xff, tos_build_date >> 24, tos_build_date & 0xffff,
tos_country < countries_size ? countries[tos_country] : "n/a",
machine,
machine_mem / 1024);
sprintf(tos_filename, "tos%03x%s.img", tos_version, country_code);
if (tos_version < 0x0106) {
tos_size = 192 * 1024;
} else if (tos_version < 0x0300) {
tos_size = 256 * 1024;
} else if (tos_version < 0x0500) {
tos_size = 512 * 1024;
if (ct60_value != 0) {
tos_size *= 2;
}
}
if (is_emutos && tos_version == 0x0206) {
/* a humble attempt to correct EmuTOS ROM size ... */
if ((mch_value & 0xffff0000UL) > 0x00010000UL) {
tos_size = 512 * 1024;
}
}
if (tos_size == 0) {
fprintf(stderr, "Couldn't determine TOS size,\r\npress a key to exit.");
Cconin();
return 1;
}
printf("\r\n");
printf("Saving %ld KiB to %s ... ", tos_size / 1024, tos_filename);
f = fopen(tos_filename, "wb");
error_str = "fail";
if (f != NULL) {
error_str = "done";
#define BUF_SIZE 16*1024
static uint8_t buf[BUF_SIZE];
for (const uint8_t* p = (const uint8_t*)tos_base; p < (const uint8_t*)tos_base + tos_size; p += BUF_SIZE) {
memcpy(buf, p, BUF_SIZE-1);
/* memcpy() usually uses movem.l which reads one extra cycle
* causing a bus error (!)
*/
buf[BUF_SIZE-1] = p[BUF_SIZE-1];
if (fwrite(buf, sizeof(*buf), BUF_SIZE, f) != BUF_SIZE) {
error_str = "fail";
break;
}
}
fclose(f);
}
printf("%s,\r\npress a key to exit.", error_str);
Cconin();
return 0;
}