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

136 lines
3.5 KiB
C
Raw Normal View History

/* *****************************************************************************
* 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/>.
* ************************************************************************** */
2016-09-28 05:24:00 +02:00
#include <libp7/internals.h>
/**
2017-02-05 20:12:25 +01:00
* p7_sendexe:
2016-11-26 20:09:17 +01:00
* Send executable stream.
2016-09-28 05:24:00 +02:00
*
* @arg handle the libp7 handle
2017-02-05 20:12:25 +01:00
* @arg buffer the update.exe to send.
2016-09-28 05:24:00 +02:00
* @arg loadaddr the load address
* @arg straddr the start address
2017-01-28 14:27:16 +01:00
* @arg disp the display callback
2016-11-26 20:09:17 +01:00
* @return 0 if it worked, error code otherwise
2016-09-28 05:24:00 +02:00
*/
2017-02-05 20:12:25 +01:00
int p7_sendexe(p7_handle_t *handle, const p7_buffer_t *buffer,
p7uint_t loadaddr, p7uint_t straddr, p7_disp_t disp)
2016-09-28 05:24:00 +02:00
{
2016-10-09 16:26:04 +02:00
int err;
2016-10-08 22:30:35 +02:00
2016-10-09 16:26:04 +02:00
/* make checks */
2017-02-05 20:12:25 +01:00
chk_bufread(buffer);
2016-10-09 16:26:04 +02:00
chk_handle(handle);
chk_active(handle);
2017-02-05 20:12:25 +01:00
chk_filesize(buffer->size);
2016-09-28 05:24:00 +02:00
/* send command */
2016-10-09 16:26:04 +02:00
log_info("sending up and run command");
2017-02-05 20:12:25 +01:00
if ((err = p7_send_cmdosu_upandrun(handle, buffer->size,
loadaddr, straddr)))
2016-10-09 16:26:04 +02:00
return (err);
2017-01-28 14:27:16 +01:00
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) {
2016-10-09 16:26:04 +02:00
log_fatal("response wasn't ack");
return (p7_error_unknown);
2016-09-28 05:24:00 +02:00
}
/* upload executable */
2017-02-05 20:12:25 +01:00
if ((err = p7_send_buffer(handle, buffer, 0, disp)))
2016-10-09 16:26:04 +02:00
return (err);
2016-09-28 05:24:00 +02:00
2016-10-09 16:26:04 +02:00
/* we're good */
return (0);
2016-09-28 05:24:00 +02:00
}
2016-11-26 20:09:17 +01:00
2017-02-05 20:12:25 +01:00
#ifndef P7_DISABLED_FILE
2016-11-26 20:09:17 +01:00
/**
2017-02-05 20:12:25 +01:00
* p7_sendexe_file:
2016-11-26 20:09:17 +01:00
* Send executable.
*
* @arg handle the libp7 handle
* @arg exe the update.exe to send
* @arg loadaddr the load address
* @arg straddr the start address
2017-01-28 14:27:16 +01:00
* @arg disp the display callback
* @return the error code
2016-11-26 20:09:17 +01:00
*/
2017-02-05 20:12:25 +01:00
int p7_sendexe_file(p7_handle_t *handle,
FILE *exe, p7uint_t loadaddr, p7uint_t straddr, p7_disp_t disp)
2016-11-26 20:09:17 +01:00
{
/* 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);
2017-02-05 20:12:25 +01:00
/* make the buffer */
p7_buffer_t buffer = {
.cookie = exe,
.size = size,
.read = p7_filebuffer_read
};
2016-11-26 20:09:17 +01:00
/* real func */
2017-02-05 20:12:25 +01:00
return (p7_sendexe(handle, &buffer, loadaddr, straddr, disp));
2016-11-26 20:09:17 +01:00
}
#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));
}