N64FlashcartMenu/src/menu/cpak_handler.c

80 lines
1.7 KiB
C
Raw Normal View History

2024-03-18 01:15:47 +01:00
#include <libdragon.h>
2024-04-25 17:09:01 +02:00
#include <stdio.h>
2024-03-18 01:15:47 +01:00
#include "../utils/fs.h"
#include "cpak_handler.h"
2024-03-18 02:14:09 +01:00
#define CPAK_BLOCKS 128
#define CPAK_BLOCK_SIZE MEMPAK_BLOCK_SIZE
2024-03-18 01:15:47 +01:00
2024-03-19 17:42:06 +01:00
cpak_err_t cpak_info_load(uint8_t port, cpak_info_t *cpak_info)
{
int res = validate_mempak(port);
if (res != CONTROLLER_PAK_OK) {
return CONTROLLER_PAK_ERR_INVALID;
}
2024-03-19 20:14:04 +01:00
for (int j = 0; j < 16; j++)
{
entry_structure_t entry;
get_mempak_entry(port, j, &entry);
cpak_info->entries[j] = entry;
}
cpak_info->free_space = get_mempak_free_space(port);
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_OK;
}
int cpak_clone_contents_to_file(char *path, uint8_t port) {
2024-03-18 02:14:09 +01:00
uint8_t cpak_data[CPAK_BLOCKS * CPAK_BLOCK_SIZE];
2024-03-18 01:15:47 +01:00
int err;
2024-03-18 02:14:09 +01:00
for (int i = 0; i < CPAK_BLOCKS; i++) {
err = read_mempak_sector(port, i, &cpak_data[i * CPAK_BLOCK_SIZE]);
2024-03-18 01:15:47 +01:00
if (err) {
return err;
}
}
2024-04-25 17:09:01 +02:00
FILE *f;
if ((f = fopen(strip_fs_prefix(path), "wb")) == NULL) {
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_ERR_IO;
2024-03-18 01:15:47 +01:00
}
2024-04-25 17:09:01 +02:00
if (fwrite(&cpak_data, 1, sizeof(cpak_data), f) != sizeof(cpak_data)) {
err = 3;
}
fclose(f);
2024-03-18 01:15:47 +01:00
2024-04-25 17:09:01 +02:00
if (err) {
return err;
2024-03-18 01:15:47 +01:00
}
else {
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_OK;
2024-03-18 01:15:47 +01:00
}
}
2024-03-18 02:14:09 +01:00
2024-03-19 17:42:06 +01:00
cpak_err_t cpak_overwrite_contents_from_file(char *path, uint8_t port) {
// TODO: implementation.
return CONTROLLER_PAK_ERR_IO;
}
2024-03-18 02:14:09 +01:00
2024-03-20 00:00:32 +01:00
cpak_err_t cpak_attempt_repair(uint8_t port, bool allow_formatting) {
int result = validate_mempak(port);
if (result == CONTROLLER_PAK_OK) {
return CONTROLLER_PAK_OK;
}
// TODO: repair implementation.
// last resort would be to format it completely!
if (result == -3 && allow_formatting) {
result = format_mempak(port);
}
return result;
2024-03-19 17:42:06 +01:00
}