all repos — mgba @ fa884d071ecaa3e05ff20b45a67bf9500dd3d6b6

mGBA Game Boy Advance Emulator

src/gba/sharkport.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/internal/gba/sharkport.h>
  7
  8#include <mgba/internal/arm/macros.h>
  9#include <mgba/internal/gba/gba.h>
 10#include <mgba-util/vfs.h>
 11
 12static const char* const SHARKPORT_HEADER = "SharkPortSave";
 13
 14bool GBASavedataImportSharkPort(struct GBA* gba, struct VFile* vf, bool testChecksum) {
 15	union {
 16		char c[0x1C];
 17		int32_t i;
 18	} buffer;
 19	if (vf->read(vf, &buffer.i, 4) < 4) {
 20		return false;
 21	}
 22	int32_t size;
 23	LOAD_32(size, 0, &buffer.i);
 24	if (size != (int32_t) strlen(SHARKPORT_HEADER)) {
 25		return false;
 26	}
 27	if (vf->read(vf, buffer.c, size) < size) {
 28		return false;
 29	}
 30	if (memcmp(SHARKPORT_HEADER, buffer.c, size) != 0) {
 31		return false;
 32	}
 33	if (vf->read(vf, &buffer.i, 4) < 4) {
 34		return false;
 35	}
 36	LOAD_32(size, 0, &buffer.i);
 37	if (size != 0x000F0000) {
 38		// What is this value?
 39		return false;
 40	}
 41
 42	// Skip first three fields
 43	if (vf->read(vf, &buffer.i, 4) < 4) {
 44		return false;
 45	}
 46	LOAD_32(size, 0, &buffer.i);
 47	if (vf->seek(vf, size, SEEK_CUR) < 0) {
 48		return false;
 49	}
 50
 51	if (vf->read(vf, &buffer.i, 4) < 4) {
 52		return false;
 53	}
 54	LOAD_32(size, 0, &buffer.i);
 55	if (vf->seek(vf, size, SEEK_CUR) < 0) {
 56		return false;
 57	}
 58
 59	if (vf->read(vf, &buffer.i, 4) < 4) {
 60		return false;
 61	}
 62	LOAD_32(size, 0, &buffer.i);
 63	if (vf->seek(vf, size, SEEK_CUR) < 0) {
 64		return false;
 65	}
 66
 67	// Read payload
 68	if (vf->read(vf, &buffer.i, 4) < 4) {
 69		return false;
 70	}
 71	LOAD_32(size, 0, &buffer.i);
 72	if (size < 0x1C || size > SIZE_CART_FLASH1M + 0x1C) {
 73		return false;
 74	}
 75	char* payload = malloc(size);
 76	if (vf->read(vf, payload, size) < size) {
 77		goto cleanup;
 78	}
 79
 80	struct GBACartridge* cart = (struct GBACartridge*) gba->memory.rom;
 81	memcpy(buffer.c, &cart->title, 16);
 82	buffer.c[0x10] = 0;
 83	buffer.c[0x11] = 0;
 84	buffer.c[0x12] = cart->checksum;
 85	buffer.c[0x13] = cart->maker;
 86	buffer.c[0x14] = 1;
 87	buffer.c[0x15] = 0;
 88	buffer.c[0x16] = 0;
 89	buffer.c[0x17] = 0;
 90	buffer.c[0x18] = 0;
 91	buffer.c[0x19] = 0;
 92	buffer.c[0x1A] = 0;
 93	buffer.c[0x1B] = 0;
 94	if (memcmp(buffer.c, payload, 0x1C) != 0) {
 95		goto cleanup;
 96	}
 97
 98	uint32_t checksum;
 99	if (vf->read(vf, &buffer.i, 4) < 4) {
100		goto cleanup;
101	}
102	LOAD_32(checksum, 0, &buffer.i);
103
104	if (testChecksum) {
105		uint32_t calcChecksum = 0;
106		int i;
107		for (i = 0; i < size; ++i) {
108			calcChecksum += ((int32_t) payload[i]) << (calcChecksum % 24);
109		}
110
111		if (calcChecksum != checksum) {
112			goto cleanup;
113		}
114	}
115
116	uint32_t copySize = size - 0x1C;
117	switch (gba->memory.savedata.type) {
118	case SAVEDATA_SRAM:
119		if (copySize > SIZE_CART_SRAM) {
120			copySize = SIZE_CART_SRAM;
121		}
122		break;
123	case SAVEDATA_FLASH512:
124		if (copySize > SIZE_CART_FLASH512) {
125			GBASavedataForceType(&gba->memory.savedata, SAVEDATA_FLASH1M, gba->memory.savedata.realisticTiming);
126		}
127	// Fall through
128	case SAVEDATA_FLASH1M:
129		if (copySize > SIZE_CART_FLASH1M) {
130			copySize = SIZE_CART_FLASH1M;
131		}
132		break;
133	case SAVEDATA_EEPROM:
134		if (copySize > SIZE_CART_EEPROM) {
135			copySize = SAVEDATA_EEPROM;
136		}
137		break;
138	case SAVEDATA_FORCE_NONE:
139	case SAVEDATA_AUTODETECT:
140		goto cleanup;
141	}
142
143	memcpy(gba->memory.savedata.data, &payload[0x1C], copySize);
144
145	free(payload);
146	return true;
147
148cleanup:
149	free(payload);
150	return false;
151}
152
153bool GBASavedataExportSharkPort(const struct GBA* gba, struct VFile* vf) {
154	union {
155		char c[0x1C];
156		int32_t i;
157	} buffer;
158	int32_t size = strlen(SHARKPORT_HEADER);
159	STORE_32(size, 0, &buffer.i);
160	if (vf->write(vf, &buffer.i, 4) < 4) {
161		return false;
162	}
163	if (vf->write(vf, SHARKPORT_HEADER, size) < size) {
164		return false;
165	}
166
167	size = 0x000F0000;
168	STORE_32(size, 0, &buffer.i);
169	if (vf->write(vf, &buffer.i, 4) < 4) {
170		return false;
171	}
172
173	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
174	size = sizeof(cart->title);
175	STORE_32(size, 0, &buffer.i);
176	if (vf->write(vf, &buffer.i, 4) < 4) {
177		return false;
178	}
179	if (vf->write(vf, cart->title, size) < 4) {
180		return false;
181	}
182
183	time_t t = time(0);
184	struct tm* tm = localtime(&t);
185	size = strftime(&buffer.c[4], sizeof(buffer.c) - 4, "%m/%d/%Y %I:%M:%S %p", tm);
186	STORE_32(size, 0, &buffer.i);
187	if (vf->write(vf, buffer.c, size + 4) < size + 4) {
188		return false;
189	}
190
191	// Last field is blank
192	size = 0;
193	STORE_32(size, 0, &buffer.i);
194	if (vf->write(vf, &buffer.i, 4) < 4) {
195		return false;
196	}
197
198	// Write payload
199	size = 0x1C;
200	switch (gba->memory.savedata.type) {
201	case SAVEDATA_SRAM:
202		size += SIZE_CART_SRAM;
203		break;
204	case SAVEDATA_FLASH512:
205		size += SIZE_CART_FLASH512;
206		break;
207	case SAVEDATA_FLASH1M:
208		size += SIZE_CART_FLASH1M;
209		break;
210	case SAVEDATA_EEPROM:
211		size += SIZE_CART_EEPROM;
212		break;
213	case SAVEDATA_FORCE_NONE:
214	case SAVEDATA_AUTODETECT:
215		return false;
216	}
217	STORE_32(size, 0, &buffer.i);
218	if (vf->write(vf, &buffer.i, 4) < 4) {
219		return false;
220	}
221	size -= 0x1C;
222
223	memcpy(buffer.c, &cart->title, 16);
224	buffer.c[0x10] = 0;
225	buffer.c[0x11] = 0;
226	buffer.c[0x12] = cart->checksum;
227	buffer.c[0x13] = cart->maker;
228	buffer.c[0x14] = 1;
229	buffer.c[0x15] = 0;
230	buffer.c[0x16] = 0;
231	buffer.c[0x17] = 0;
232	buffer.c[0x18] = 0;
233	buffer.c[0x19] = 0;
234	buffer.c[0x1A] = 0;
235	buffer.c[0x1B] = 0;
236	if (vf->write(vf, buffer.c, 0x1C) < 0x1C) {
237		return false;
238	}
239
240	uint32_t checksum = 0;
241	int i;
242	for (i = 0; i < 0x1C; ++i) {
243		checksum += buffer.c[i] << (checksum % 24);
244	}
245
246	if (vf->write(vf, gba->memory.savedata.data, size) < size) {
247		return false;
248	}
249
250	for (i = 0; i < size; ++i) {
251		checksum += ((char) gba->memory.savedata.data[i]) << (checksum % 24);
252	}
253
254	STORE_32(checksum, 0, &buffer.i);
255	if (vf->write(vf, &buffer.i, 4) < 4) {
256		return false;
257	}
258
259	return true;
260}