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/usage/sendexe.c

140 lines
3.8 KiB
C

/* *****************************************************************************
* usage/sendexe.c -- send an update.exe to 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>
/**
* p7_sendexe:
* Send executable stream.
*
* @arg handle the libp7 handle
* @arg buffer the update.exe to send.
* @arg loadaddr the load address
* @arg straddr the start address
* @arg disp the display callback
* @return 0 if it worked, error code otherwise
*/
p7_define_ufunc(p7_sendexe, p7_attrs_sendexe,
p7_handle_t *handle, const p7_buffer_t *buffer,
p7uint_t loadaddr, p7uint_t straddr, p7_disp_t *disp)
{
int err;
/* make checks */
chk_bufread(buffer);
chk_handle(handle);
chk_seven(handle);
chk_active(handle);
chk_filesize(buffer->p7_buffer_size);
/* send command */
log_info("sending up and run command");
if ((err = p7_seven_send_cmdosu_upandrun(handle, buffer->p7_buffer_size,
loadaddr, straddr)))
return (err);
else if (response.p7_seven_packet_type == p7_seven_type_error
&& response.p7_seven_packet_code == p7_seven_err_other) {
log_fatal("0x56 is non implemented!");
return (p7_error_unsupported);
} else if (response.p7_seven_packet_type != p7_seven_type_ack) {
log_fatal("response wasn't ack");
return (p7_error_unknown);
}
/* upload executable */
if ((err = p7_seven_send_buffer(handle, buffer, 0, disp)))
return (err);
/* we're good */
return (0);
}
#ifndef P7_DISABLED_FILE
/**
* p7_sendexe_file:
* Send executable.
*
* @arg handle the libp7 handle
* @arg exe the update.exe to send
* @arg loadaddr the load address
* @arg straddr the start address
* @arg disp the display callback
* @return the error code
*/
p7_define_ufunc(p7_sendexe_file, p7_attrs_sendexe_file,
p7_handle_t *handle, FILE *exe,
p7uint_t loadaddr, p7uint_t straddr, p7_disp_t *disp)
{
/* make checks */
chk_isread(exe);
/* calculate exe size */
p7uint_t size;
if (fseek(exe, 0, SEEK_END)) {
log_fatal("seek failed!");
return (p7_error_noseek);
}
size = (p7uint_t)ftell(exe);
rewind(exe);
/* make the buffer */
p7_buffer_t buffer = {
.p7_buffer_cookie = exe,
.p7_buffer_size = size,
.p7_buffer_read = p7_filebuffer_read
};
/* real func */
return (p7_sendexe(handle, &buffer, loadaddr, straddr, disp));
}
#endif
/**
* p7_sendexe_mem:
* Send executable, read from memory.
*
* @arg handle the libp7 handle
* @arg mem the buffer containing the update.exe
* @arg size the size of the update.exe to send
* @arg loadaddr the load address
* @arg straddr the start address
* @arg disp the display callback
* @return the error code.
*/
p7_define_ufunc(p7_sendexe_mem, p7_attrs_sendexe_mem,
p7_handle_t *handle, const void *mem, size_t size,
p7uint_t loadaddr, p7uint_t straddr, p7_disp_t *disp)
{
chk_mem(mem);
chk_filesize(size);
/* make the buffer */
p7_cursor_t cursor = {.mem = mem};
p7_buffer_t buffer = {
.p7_buffer_cookie = &cursor,
.p7_buffer_size = size,
.p7_buffer_read = p7_membuffer_read
};
/* real func */
return (p7_sendexe(handle, &buffer, loadaddr, straddr, disp));
}