cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/misc/backup.c

384 lines
10 KiB
C

/* *****************************************************************************
* misc/backup.c -- backup software elements of the calculator.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libp7.
* libp7 is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3.0 of the License,
* or (at your option) any later version.
*
* libp7 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libp7; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libp7/internals.h>
#include <string.h>
/* ************************************************************************** */
/* Decode a CASIOWIN entry */
/* ************************************************************************** */
/* Raw structure */
struct casiowin_entry {
/* contains "CASIOWIN" */
unsigned char magic[0x08];
/* code that calls the subroutine at address 0x00010080 */
unsigned char call_os[0x0A];
/* alignment? */
unsigned char _align[0x0A];
/* marker for the OS updater PC program
* (appeared in 1.01, no longer used in OS2)
* - fx-9860G AU: "365E"
* - fx-9860G SD Slim: 0x80005D7C */
unsigned char marker[0x04];
/* version (added in OS 1.02): "xx.xx.xxxx" */
unsigned char version[0x0A];
/* alignment? */
unsigned char _align2[0x06];
/* unused */
unsigned char _unused[0x40];
/* syscall code */
unsigned char call_sys[0x0A];
/* alignment? */
unsigned char _align3[0x02];
/* syscall table address */
uint32_t syscall_table;
};
/**
* decode_entry:
* Decode an entry.
*
* @arg handle the handle.
* @arg entry the decoded thing.
* @arg raw the raw thing (uncasted).
* @return the error code (0 if ok)
*/
static int decode_entry(p7_handle_t *handle, p7_casiowin_entry_t *entry,
struct casiowin_entry *raw)
{
(void)handle;
/* check the maaa-gic */
if (!memcmp(raw->magic, "CASIOWIN", 8))
return (p7_error_checksum);
/* decode */
entry->version.major = p7_getascii(&raw->version[0], 2);
entry->version.minor = p7_getascii(&raw->version[3], 2);
entry->version.rev = p7_getascii(&raw->version[6], 4);
entry->syscall_table_offset = be32toh(raw->syscall_table);
/* log */
log_info("OS version is %02u.%02u.%04u",
entry->version.major, entry->version.minor, entry->version.rev);
log_info("OS syscall table offset is 0x%08" PRIxP7INT,
entry->syscall_table_offset);
/* no error */
return (0);
}
/* ************************************************************************** */
/* Get a CASIOWIN entry */
/* ************************************************************************** */
/**
* p7_backup_casiowin_entry:
* Backup the CASIOWIN entry.
*
* @arg handle the libp7 handle
* @arg entry the entry to fill (0x80 bytes).
* @return the error code (0 if ok)
*/
int p7_backup_casiowin_entry(p7_handle_t *handle, p7_casiowin_entry_t *entry)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
/* send command packet */
log_info("sending command");
if ((err = p7_send_cmdbak_reqcwe(handle))) {
log_fatal("couldn't send command/get response");
return (err);
} else if (response.type == p7_pt_error && response.code == p7_err_other) {
log_fatal("backup is unsupported.");
return (p7_error_unsupported);
} else if (response.type != p7_pt_ack) {
log_fatal("received an invalid answer");
return (p7_error_unknown);
}
/* send the roleswap */
log_info("sending the roleswap");
if ((err = p7_send_roleswp(handle))) {
log_fatal("couldn't send roleswap");
return (err);
} else if (response.type != p7_pt_cmd || response.code != 0x53) {
log_fatal("didn't receive correct command");
return (p7_error_unknown);
}
/* get the data */
struct casiowin_entry raw;
err = p7_get_data(handle, &raw, sizeof(struct casiowin_entry), 0, NULL);
if (err) return (err);
/* decode and return */
return (decode_entry(handle, entry, &raw));
}
/* ************************************************************************** */
/* Backup the bootcode */
/* ************************************************************************** */
/**
* p7_backup_boot:
* Backup the boot code.
*
* My current theory is that the bootcode size is in the bootcode, that
* the OS reads it or not, and that even if the OS accepts bootcode backup
* and reads it, the bootcode had blanked its size... so in order to make it
* work correctly, I'll have to hardcode the size of the bootcode (64 KiB
* on all of the known models until now).
*
* @arg handle the libp7 handle
* @arg buffer the destination buffer.
* @arg disp the display function
* @return the error code (0 if ok)
*/
#define bootcode_size (0x10000)
int p7_backup_boot(p7_handle_t *handle, const p7_buffer_t *buffer,
p7_disp_t disp)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
chk_bufwrite(buffer);
/* send command packet */
log_info("sending command");
if ((err = p7_send_cmdbak_reqboot(handle))) {
log_fatal("couldn't send command/get response");
return (err);
} else if (response.type == p7_pt_error && response.code == p7_err_other) {
log_fatal("backup is unsupported.");
return (p7_error_unsupported);
} else if (response.type != p7_pt_ack) {
log_fatal("received an invalid answer");
return (p7_error_unknown);
}
/* send the roleswap */
log_info("sending the roleswap");
if ((err = p7_send_roleswp(handle))) {
log_fatal("couldn't send roleswap");
return (err);
} else if (response.type != p7_pt_cmd) {
log_fatal("didn't receive command");
return (p7_error_unknown);
}
/* get the data */
if ((err = p7_get_buffer(handle, buffer, bootcode_size, 0, disp)))
return (err);
/* we're good */
return (0);
}
#ifndef P7_DISABLED_FILE
/**
* p7_backup_bootfile:
* Backup the boot to a FILE.
*
* @arg handle the handle.
* @arg file the FILE.
* @arg disp the display callback.
* @return the error code (0 if ok).
*/
int p7_backup_bootfile(p7_handle_t *handle, FILE *file, p7_disp_t disp)
{
chk_iswrite(file);
p7_buffer_t buffer = {
.cookie = file,
.write = p7_filebuffer_write
};
return (p7_backup_boot(handle, &buffer, disp));
}
#endif
/* ************************************************************************** */
/* Backup the ROM */
/* ************************************************************************** */
/**
* p7_backup_rom:
* Backup the Flash ROM.
*
* @arg handle the libp7 handle.
* @arg file the filestream.
* @arg disp the display function.
* @return the error code (0 if ok)
*/
#define rom_size (handle->_server.flash_rom_capacity)
int p7_backup_rom(p7_handle_t *handle, const p7_buffer_t *buffer,
p7_disp_t disp)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
chk_bufwrite(buffer);
/* send command packet */
log_info("sending command");
if ((err = p7_send_cmdbak_reqrom(handle))) {
log_fatal("couldn't send command/get response");
return (err);
} else if (response.type == p7_pt_error && response.code == p7_err_other) {
log_fatal("backup is unsupported.");
return (p7_error_unsupported);
} else if (response.type != p7_pt_ack) {
log_fatal("received an invalid answer");
return (p7_error_unknown);
}
/* send the roleswap */
log_info("sending the roleswap");
if ((err = p7_send_roleswp(handle))) {
log_fatal("couldn't send roleswap");
return (err);
} else if (response.type != p7_pt_cmd) {
log_fatal("didn't receive command");
return (p7_error_unknown);
}
/* get the data */
if ((err = p7_get_buffer(handle, buffer, rom_size, 0, disp)))
return (err);
/* we're good */
return (0);
}
#ifndef P7_DISABLED_FILE
/**
* p7_backup_romfile:
* Backup the ROM to a FILE.
*
* @arg handle the handle.
* @arg file the FILE.
* @arg disp the display callback.
* @return the error code (0 if ok).
*/
int p7_backup_romfile(p7_handle_t *handle, FILE *file, p7_disp_t disp)
{
chk_iswrite(file);
p7_buffer_t buffer = {
.cookie = file,
.write = p7_filebuffer_write
};
return (p7_backup_rom(handle, &buffer, disp));
}
#endif
/* ************************************************************************** */
/* Backup the RAM */
/* ************************************************************************** */
/**
* p7_backup_ram:
* Backup the RAM.
*
* @arg handle the libp7 handle.
* @arg file the filestream.
* @arg disp the display function.
* @return the error code (0 if ok)
*/
#define ram_size (0x10000)
int p7_backup_ram(p7_handle_t *handle, const p7_buffer_t *buffer,
p7_disp_t disp)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
chk_bufwrite(buffer);
/* send command packet */
log_info("sending command");
if ((err = p7_send_cmdbak_reqram(handle))) {
log_fatal("couldn't send command/get response");
return (err);
} else if (response.type == p7_pt_error && response.code == p7_err_other) {
log_fatal("backup is unsupported.");
return (p7_error_unsupported);
} else if (response.type != p7_pt_ack) {
log_fatal("received an invalid answer");
return (p7_error_unknown);
}
/* send the roleswap */
log_info("sending the roleswap");
if ((err = p7_send_roleswp(handle))) {
log_fatal("couldn't send roleswap");
return (err);
} else if (response.type != p7_pt_cmd) {
log_fatal("didn't receive command");
return (p7_error_unknown);
}
/* get the data */
if ((err = p7_get_buffer(handle, buffer, ram_size, 0, disp)))
return (err);
/* we're good */
return (0);
}
#ifndef P7_DISABLED_FILE
/**
* p7_backup_ramfile:
* Backup the RAM to a FILE.
*
* @arg handle the handle.
* @arg file the FILE.
* @arg disp the display callback.
* @return the error code (0 if ok).
*/
int p7_backup_ramfile(p7_handle_t *handle, FILE *file, p7_disp_t disp)
{
chk_iswrite(file);
p7_buffer_t buffer = {
.cookie = file,
.write = p7_filebuffer_write
};
return (p7_backup_ram(handle, &buffer, disp));
}
#endif