136 lines
3.5 KiB
C
136 lines
3.5 KiB
C
/* *****************************************************************************
|
|
* misc/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
|
|
*/
|
|
|
|
int p7_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_active(handle);
|
|
chk_filesize(buffer->size);
|
|
|
|
/* send command */
|
|
log_info("sending up and run command");
|
|
if ((err = p7_send_cmdosu_upandrun(handle, buffer->size,
|
|
loadaddr, straddr)))
|
|
return (err);
|
|
else if (response.type == p7_pt_error && response.code == p7_err_other) {
|
|
log_fatal("0x56 is non implemented!");
|
|
return (p7_error_unsupported);
|
|
} else if (response.type != p7_pt_ack) {
|
|
log_fatal("response wasn't ack");
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* upload executable */
|
|
if ((err = p7_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
|
|
*/
|
|
|
|
int p7_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 = {
|
|
.cookie = exe,
|
|
.size = size,
|
|
.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.
|
|
*/
|
|
|
|
int p7_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 = {
|
|
.cookie = &cursor,
|
|
.size = size,
|
|
.read = p7_membuffer_read
|
|
};
|
|
|
|
/* real func */
|
|
return (p7_sendexe(handle, &buffer, loadaddr, straddr, disp));
|
|
}
|