From 9af4f1bd4402c789850f471ad572a7436e50062e Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Wed, 1 Jan 2020 21:25:48 +0100 Subject: [PATCH 1/5] No 100% working but get is implemented, casio_read modified (size_t to size_t *) --- include/libcasio/error.h | 3 +- include/libcasio/stream.h | 4 +- lib/errors.c | 3 +- lib/file/file.h | 9 +- lib/link/seven/dataflow.c | 2 +- lib/link/seven/datastream.c | 19 ++- lib/link/seven/receive.c | 5 +- lib/link/seven/scsi.c | 30 ++--- lib/link/seven_fs/list.c | 2 +- lib/link/seven_fs/open.c | 181 ++++++++++++++++++++++++++- lib/link/seven_fs/seven_fs.h | 6 + lib/link/seven_fs/topath.c | 4 +- lib/mcsfile/mcsfile.h | 19 ++- lib/stream/builtin/csum32.c | 5 +- lib/stream/builtin/file.c | 3 +- lib/stream/builtin/libusb/libusb.h | 2 +- lib/stream/builtin/libusb/read.c | 3 +- lib/stream/builtin/limited.c | 6 +- lib/stream/builtin/memory.c | 3 +- lib/stream/builtin/streams/read.c | 3 +- lib/stream/builtin/streams/streams.h | 2 +- lib/stream/read.c | 16 ++- lib/stream/seek.c | 2 +- src/p7/main.c | 53 ++++++-- src/p7os/procs/fxremote_flash.c | 2 +- 25 files changed, 321 insertions(+), 66 deletions(-) diff --git a/include/libcasio/error.h b/include/libcasio/error.h index f100da7..289dbf3 100644 --- a/include/libcasio/error.h +++ b/include/libcasio/error.h @@ -63,7 +63,8 @@ typedef int casio_error_t; # define casio_error_timeout 0x14 /* a timeout has occured. */ # define casio_error_access 0x15 /* could not access the device (perms) */ # define casio_error_noaccess 0x15 /* (alias) */ -# define casio_error_eof 0x16 /* end of file */ +# define casio_error_eof 0x16 /* end of file error */ +# define casio_error_ieof 0x17 /* info end of file error */ /* Link errors. */ diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index 5a94c05..ec2cd4e 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -101,7 +101,7 @@ typedef int casio_stream_settm_t OF((void *, const casio_timeouts_t *)); typedef int casio_stream_read_t - OF((void *, unsigned char *, size_t)); + OF((void *, unsigned char *, size_t *)); typedef int casio_stream_write_t OF((void *, const unsigned char *, size_t)); typedef int casio_stream_seek_t @@ -361,7 +361,7 @@ CASIO_EXTERN int CASIO_EXPORT casio_get_lasterr CASIO_EXTERN int CASIO_EXPORT casio_read OF((casio_stream_t *casio__stream, - void *casio__dest, size_t casio__size)); + void *casio__dest, size_t *casio__psize)); CASIO_EXTERN int CASIO_EXPORT casio_write OF((casio_stream_t *casio__stream, const void *casio__data, size_t casio__size)); diff --git a/lib/errors.c b/lib/errors.c index 928f696..c58a0da 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -39,7 +39,8 @@ const char* CASIO_EXPORT casio_error_strings[128] = { "a timeout has occurred", "could not get access to the device", "an end of file event has occured", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + "an end of file event has occured but was manage", + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, diff --git a/lib/file/file.h b/lib/file/file.h index f818d3d..5cd24fc 100644 --- a/lib/file/file.h +++ b/lib/file/file.h @@ -28,12 +28,15 @@ /* Read from a stream. */ # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + size_t size = CASIO__SZ; \ + int READ_err = casio_read(buffer, (CASIO__TO), &size); \ if (READ_err) return (READ_err); } # define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ \ - err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); + size_t size = CASIO__SZ; \ + err = casio_read(buffer, (CASIO__TO), &size); # define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ \ - if ((err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)))) \ + size_t size = CASIO__SZ; \ + if ((err = casio_read(buffer, (CASIO__TO), &size))) \ goto fail; /* Read using size of the object. */ diff --git a/lib/link/seven/dataflow.c b/lib/link/seven/dataflow.c index 3286976..1c31da1 100644 --- a/lib/link/seven/dataflow.c +++ b/lib/link/seven/dataflow.c @@ -64,7 +64,7 @@ int CASIO_EXPORT casio_seven_send_buffer(casio_link_t *handle, /* Read the big block. */ toread = min(BUFSIZE, size); - err = casio_read(buffer, buf + 8, toread); + err = casio_read(buffer, buf + 8, &toread); if (err) return (casio_error_noread); size -= toread; diff --git a/lib/link/seven/datastream.c b/lib/link/seven/datastream.c index 2ffc850..456baff 100644 --- a/lib/link/seven/datastream.c +++ b/lib/link/seven/datastream.c @@ -32,6 +32,7 @@ typedef struct { casio_link_progress_t *_disp; void *_disp_cookie; unsigned int _id, _total; + unsigned int _totalsize; /* */ unsigned int _lastsize; /* last packet size */ /* buffer management */ @@ -55,11 +56,11 @@ typedef struct { */ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, - unsigned char *data, size_t size) + unsigned char *data, size_t *psize) { - int err; size_t tocopy; + int err; size_t tocopy; size_t size = *psize; casio_link_t *handle = cookie->_link; - unsigned int lastsize; + unsigned int lastsize = 0; /* Check if the stream is faulty. */ if (cookie->_faulty) @@ -78,13 +79,18 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, /* Check if we have already finished. */ if (cookie->_total && cookie->_id == cookie->_total) - return (casio_error_eof); + return (casio_error_ieof); /* Receive packets. */ while (size) { /* Send the ack and get the answer. */ err = casio_seven_send_ack(handle, 1); if (err) goto fail; + /* If swap roles there is the end of file */ + if (response.casio_seven_packet_type == casio_seven_type_swp) { + *psize = lastsize; + return (casio_error_ieof); + } if (response.casio_seven_packet_type != casio_seven_type_data) { msg((ll_error, "Packet wasn't a data packet, wtf?")); err = casio_error_unknown; @@ -113,7 +119,7 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, lastsize = response.casio_seven_packet_data_size; if (size >= lastsize) { memcpy(data, response.casio_seven_packet_data, lastsize); - data += lastsize; size -= lastsize; + data += lastsize; size -= lastsize; cookie->_totalsize += lastsize; continue; } @@ -122,6 +128,7 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, memcpy(data, response.casio_seven_packet_data, size); memcpy(&cookie->_current[size], &response.casio_seven_packet_data[size], lastsize - size); + cookie->_totalsize += lastsize; return (0); } @@ -341,6 +348,7 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 1; cookie->_total = (unsigned int)(size / BUFSIZE) + !!cookie->_lastsize; cookie->_lastsize = (unsigned int)(size % BUFSIZE); + cookie->_totalsize = size; if (!cookie->_lastsize) cookie->_lastsize = BUFSIZE; } else { msg((ll_info, "The data stream is a read one.")); @@ -350,6 +358,7 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 0; cookie->_total = 0; cookie->_lastsize = 0; + cookie->_totalsize = size; } /* initialize the stream */ diff --git a/lib/link/seven/receive.c b/lib/link/seven/receive.c index 109bf19..ce0a111 100644 --- a/lib/link/seven/receive.c +++ b/lib/link/seven/receive.c @@ -175,9 +175,10 @@ CASIO_LOCAL const char *gettermstring(casio_seven_term_t code) #define buffer handle->casio_link_recv_buffer #define COMPLETE_PACKET(N) { \ + size_t size##__LINE__ = (size_t)N; \ int COMP_PACKET_err = casio_read(handle->casio_link_stream, \ - &buffer[received], (size_t)(N)); \ - received += (N); if (COMP_PACKET_err) return (COMP_PACKET_err); } + &buffer[received], &size##__LINE__); \ + received += size##__LINE__; if (COMP_PACKET_err) return (COMP_PACKET_err); } CASIO_LOCAL int casio_seven_decode(casio_link_t *handle, int scralign) { diff --git a/lib/link/seven/scsi.c b/lib/link/seven/scsi.c index fd3dfff..cc79567 100644 --- a/lib/link/seven/scsi.c +++ b/lib/link/seven/scsi.c @@ -84,23 +84,23 @@ typedef struct { * --- */ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, - unsigned char *buffer, size_t size) + unsigned char *buffer, size_t *size) { casio_scsi_t scsi; int err; /* Empty what's already in the buffer. */ if (cookie->left) { - if (cookie->left >= size) { - memcpy(buffer, cookie->ptr, size); - cookie->ptr += size; - cookie->left -= size; + if (cookie->left >= *size) { + memcpy(buffer, cookie->ptr, *size); + cookie->ptr += *size; + cookie->left -= *size; return (0); } memcpy(buffer, cookie->ptr, cookie->left); buffer += cookie->left; - size -= cookie->left; + *size -= cookie->left; reset_cookie(cookie); } @@ -151,14 +151,14 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, * We could also check that `avail < 0x10000` because we need to * express it later, but it is imposed by the source format. */ - if (avail > size && size <= cookie->size) { + if (avail > *size && *size <= cookie->size) { to = cookie->ptr; if (avail > cookie->size) avail = cookie->size; } else { to = buffer; - if (avail > size) - avail = size; + if (avail > *size) + avail = *size; } /* Actually get the data. */ @@ -182,16 +182,16 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, if (to == buffer) { buffer += avail; - size -= avail; + *size -= avail; } else { cookie->left = avail; - memcpy(buffer, cookie->ptr, size); - cookie->ptr += size; - cookie->left -= size; - size = 0; + memcpy(buffer, cookie->ptr, *size); + cookie->ptr += *size; + cookie->left -= *size; + *size = 0; } - } while (size); + } while (*size); return (0); } diff --git a/lib/link/seven_fs/list.c b/lib/link/seven_fs/list.c index cc56876..fed50be 100644 --- a/lib/link/seven_fs/list.c +++ b/lib/link/seven_fs/list.c @@ -46,7 +46,7 @@ int CASIO_EXPORT casio_sevenfs_list(sevenfs_cookie_t *cookie, sevenfs_path_t *pa } else if (response.casio_seven_packet_type == casio_seven_type_nak && response.casio_seven_packet_code == casio_seven_err_other) { msg((ll_fatal, "Invalid filesystem")); - return (casio_error_device); // FIXME : unsupported device error + return (casio_error_device); } else if (response.casio_seven_packet_type != casio_seven_type_ack) { msg((ll_fatal, "Didn't receive ack or known error...")); return (casio_error_unknown); diff --git a/lib/link/seven_fs/open.c b/lib/link/seven_fs/open.c index f4bb649..6a82ab0 100644 --- a/lib/link/seven_fs/open.c +++ b/lib/link/seven_fs/open.c @@ -29,7 +29,7 @@ CASIO_LOCAL casio_fsfuncs_t sevenfs_callbacks = { (casio_fs_del_t*)&casio_sevenfs_delete, NULL, (casio_fs_list_t*)&casio_sevenfs_list, - NULL, + (casio_fs_open_t*)&casio_sevenfs_open, (casio_fs_optim_t*)&casio_sevenfs_optimize }; @@ -48,3 +48,182 @@ int CASIO_EXPORT casio_open_seven_fs(casio_fs_t **fs, /* Open the filesystem. (no need for cookie allocating for now) */ return (casio_open_fs(fs, link, &sevenfs_callbacks)); } + +/** + * casio_sevenfs_open_read + * Open a file in read mode + * + * @arg cookie the sevenfs cookie + * @arg path the path to th file + * @arg stream pointer to a pointer who contain the stream + */ + +CASIO_LOCAL int casio_sevenfs_open_read(sevenfs_cookie_t *cookie, sevenfs_path_t *path, + casio_stream_t **stream) +{ + casio_link_t *handle = cookie; int err; + char *dirname = (path->sevenfs_path_dir != 0xFF) ? &path->sevenfs_path_data[path->sevenfs_path_dir] : NULL; + char *filename = &path->sevenfs_path_data[path->sevenfs_path_file]; + char *devname = &path->sevenfs_path_data[path->sevenfs_path_dev]; + + /* Make checks */ + chk_required_filename(filename); + chk_dirname(dirname); + chk_handle(handle); + chk_active(handle); + + + /* Send command packet */ + msg((ll_info, "Sending file transfer request")); + if ((err = casio_seven_send_cmdfls_reqfile(handle, dirname, filename, devname))) { + msg((ll_fatal, "Couldn't send file transfer request/didn't receive answer")); + return (err); + } + + /* Check response packet */ + if (response.casio_seven_packet_type == casio_seven_type_nak + && response.casio_seven_packet_code == casio_seven_err_other) { + msg((ll_fatal, "Requested file doesn't exist")); + err = casio_error_notfound; + return (err); + } else if (response.casio_seven_packet_type != casio_seven_type_ack) { + msg((ll_fatal, "Didn't receive ack or known error...")); + return (casio_error_unknown); + } + + /* Send swap roles */ + msg((ll_info, "Sending roleswap")); + if ((err = casio_seven_send_roleswp(handle))) + { + msg((ll_fatal, "Couldn't swap roles")); + return (err); + } else if (response.casio_seven_packet_type != casio_seven_type_cmd + || response.casio_seven_packet_code != 0x45) + { + msg((ll_fatal, "Didn't receive the excepted command")); + return (casio_error_unknown); + } + + return casio_seven_open_data_stream(stream, handle, response.casio_seven_packet_filesize, NULL, NULL); +} + +/** + * casio_sevenfs_open_write + * Open a file in write mode + * + * @arg cookie the sevenfs cookie + * @arg path the path to th file + * @arg size size of the file + * @arg stream pointer to a pointer who contain the stream + * @return the error (0 if okey) + */ + +CASIO_LOCAL int casio_sevenfs_open_write(sevenfs_cookie_t *cookie, sevenfs_path_t *path, + casio_off_t size, casio_stream_t **stream) +{ + casio_link_t *handle = cookie; int err; + char *dirname = &path->sevenfs_path_data[path->sevenfs_path_dir]; + char *filename = &path->sevenfs_path_data[path->sevenfs_path_file]; + char *devname = &path->sevenfs_path_data[path->sevenfs_path_dev]; + + /* Make checks */ + chk_required_filename(filename); + chk_dirname(dirname); + chk_handle(handle); + chk_active(handle); + + return (0); + +} + +/** + * casio_sevenfs_open: + * Open a file. + * + * @arg cookie the sevenfs cookie + * @arg path the path to the file + * @arg size size of the file if WRITE (NULL if READ) + * @arg mode the open mode + * @arg stream pointer to a pointer who contain the stream + * @return the error (0 if okey) + */ + +int CASIO_EXPORT casio_sevenfs_open(sevenfs_cookie_t *cookie, sevenfs_path_t *path, + casio_off_t size, casio_openmode_t mode, casio_stream_t **stream) +{ + casio_link_t *handle = cookie; int err; + char *dirname = &path->sevenfs_path_data[path->sevenfs_path_dir]; + char *filename = &path->sevenfs_path_data[path->sevenfs_path_file]; + char *devname = &path->sevenfs_path_data[path->sevenfs_path_dev]; + + /* Make checks (not really required) */ + chk_required_filename(filename); + chk_dirname(dirname); + chk_handle(handle); + chk_active(handle); + + if ((mode & CASIO_OPENMODE_WRITE)) + { + + } else if ((mode & CASIO_OPENMODE_READ)) + { + return (casio_sevenfs_open_read(cookie, path, stream)); + } + +#if 0 + /* Send command packet */ + msg((ll_info, "Sending file transfer request")); + if ((err = casio_seven_send_cmdfls_reqfile(handle, dirname, filename, devname))) { + msg((ll_fatal, "Couldn't send file transfer request/didn't receive answer")); + return (err); + } + + /* Check response */ + if (response.casio_seven_packet_type == casio_seven_type_nak + && response.casio_seven_packet_code == casio_seven_err_other) + { + /* If file doesn't exist and we want read : error */ + if ((mode & CASIO_OPENMODE_READ)) { + msg((ll_fatal, "Requested file doesn't exist")); + return (casio_error_notfound); + } + } else if (response.casio_seven_packet_type != casio_seven_type_ack) { + msg((ll_fatal, "Didn't receive ack or known error...")); + return (casio_error_unknown); + } + + + /* Check what happened if it is an error packet */ + if (response.casio_seven_packet_type == casio_seven_type_nak) + { + switch (response.casio_seven_packet_type) + { + + /* Calc need overwrite confirmation */ + case casio_seven_err_overwrite: + + break; + + /* Calc denie overwrite or err other */ + case casio_seven_err_dont_overwrite: + case casio_seven_err_other: + msg((ll_error, "Overwrite impossible")); + err = casio_error_device; + return (err); + break; + + /* Memory full */ + case casio_seven_err_fullmem: + msg((ll_error, "Calc mem is full")); + err = casio_error_fullmem; + return (err); + break; + + /* stfu warning */ + case casio_seven_err_resend: break; + } + } + + msg((ll_info, "WE CAN")); +#endif +} diff --git a/lib/link/seven_fs/seven_fs.h b/lib/link/seven_fs/seven_fs.h index f11e001..0b2f7f7 100644 --- a/lib/link/seven_fs/seven_fs.h +++ b/lib/link/seven_fs/seven_fs.h @@ -57,4 +57,10 @@ CASIO_EXTERN int CASIO_EXPORT casio_sevenfs_list OF((sevenfs_cookie_t *casio__cookie, sevenfs_path_t *casio__path, casio_fs_list_func_t *casio__callback, void *casio__cbcookie)); +/* Open a stream with a casio fs file */ + +CASIO_EXTERN int CASIO_EXPORT casio_sevenfs_open + OF((sevenfs_cookie_t *casio__cookie, sevenfs_path_t *casio__path, + casio_off_t casio__size, casio_openmode_t casio__mode, casio_stream_t **casio__stream)); + #endif /* LOCAL_LINK_SEVEN_FS_H */ diff --git a/lib/link/seven_fs/topath.c b/lib/link/seven_fs/topath.c index 826eedd..59e4498 100644 --- a/lib/link/seven_fs/topath.c +++ b/lib/link/seven_fs/topath.c @@ -53,7 +53,7 @@ int CASIO_EXPORT casio_make_sevenfs_path(sevenfs_cookie_t *cookie, if (!node) return (casio_error_invalid); if (node->casio_pathnode_next) { dirsz = node->casio_pathnode_size + 1; - if (dirsz == 1 || dirsz > 9) return (casio_error_invalid); + if (dirsz == 1 || dirsz > 13) return (casio_error_invalid); dirname = (const char*)node->casio_pathnode_name; node = node->casio_pathnode_next; } else { @@ -66,7 +66,7 @@ int CASIO_EXPORT casio_make_sevenfs_path(sevenfs_cookie_t *cookie, return (casio_error_invalid); } filesz = node->casio_pathnode_size + 1; - if (filesz == 1 || filesz > 9) return (casio_error_invalid); + if (filesz == 1 || filesz > 13) return (casio_error_invalid); filename = (const char*)node->casio_pathnode_name; /* Make the node. */ diff --git a/lib/mcsfile/mcsfile.h b/lib/mcsfile/mcsfile.h index 336751f..ee61550 100644 --- a/lib/mcsfile/mcsfile.h +++ b/lib/mcsfile/mcsfile.h @@ -26,14 +26,21 @@ /* Read from a stream. */ +/* FIXME : size_t size##__LINE__ = CASIO__SZ; + * Replace this by other thing that is more futur proof +*/ + # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + size_t size##__LINE__ = CASIO__SZ; \ + int READ_err = casio_read(buffer, (CASIO__TO), &size##__LINE__); \ if (READ_err) return (READ_err); } -# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ \ - err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); -# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ \ - if ((err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)))) \ - goto fail; +# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ { \ + size_t size##__LINE__ = CASIO__SZ; \ + err = casio_read(buffer, (CASIO__TO), &size##__LINE__); } +# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ { \ + size_t size##__LINE__ = CASIO__SZ; \ + if ((err = casio_read(buffer, (CASIO__TO), &size##__LINE__))) \ + goto fail; } /* Read using size of the object. */ diff --git a/lib/stream/builtin/csum32.c b/lib/stream/builtin/csum32.c index a071582..3169f28 100644 --- a/lib/stream/builtin/csum32.c +++ b/lib/stream/builtin/csum32.c @@ -42,13 +42,14 @@ struct thecookie { */ CASIO_LOCAL int csum32_read(struct thecookie *cookie, - unsigned char *dest, size_t size) + unsigned char *dest, size_t *psize) { int err; + size_t size = *psize; /* Make the call. */ - err = casio_read(cookie->_stream, dest, size); + err = casio_read(cookie->_stream, dest, &size); if (err) return (err); /* Make the checksum. */ diff --git a/lib/stream/builtin/file.c b/lib/stream/builtin/file.c index 50c9188..90fc051 100644 --- a/lib/stream/builtin/file.c +++ b/lib/stream/builtin/file.c @@ -50,9 +50,10 @@ typedef struct { */ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, - unsigned char *dest, size_t size) + unsigned char *dest, size_t *psize) { size_t recv; + size_t size = *psize; /* Main receiving loop. */ diff --git a/lib/stream/builtin/libusb/libusb.h b/lib/stream/builtin/libusb/libusb.h index b3cad60..b2dea05 100644 --- a/lib/stream/builtin/libusb/libusb.h +++ b/lib/stream/builtin/libusb/libusb.h @@ -60,7 +60,7 @@ CASIO_EXTERN int CASIO_EXPORT casio_libusb_settm CASIO_EXTERN int CASIO_EXPORT casio_libusb_read OF((cookie_libusb_t *casio__cookie, - unsigned char *casio__dest, size_t casio__size)); + unsigned char *casio__dest, size_t *casio__psize)); CASIO_EXTERN int CASIO_EXPORT casio_libusb_write OF((cookie_libusb_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/builtin/libusb/read.c b/lib/stream/builtin/libusb/read.c index 34d727f..e8badeb 100644 --- a/lib/stream/builtin/libusb/read.c +++ b/lib/stream/builtin/libusb/read.c @@ -30,10 +30,11 @@ */ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, - unsigned char *dest, size_t size) + unsigned char *dest, size_t *psize) { int libusberr; size_t tocopy; + size_t size = *psize; /* Transmit what's already in the buffer. */ diff --git a/lib/stream/builtin/limited.c b/lib/stream/builtin/limited.c index 6b847bf..5b6a244 100644 --- a/lib/stream/builtin/limited.c +++ b/lib/stream/builtin/limited.c @@ -41,10 +41,10 @@ typedef struct { */ CASIO_LOCAL int casio_limited_read(void *vcookie, unsigned char *dest, - size_t size) + size_t *psize) { int err; limited_cookie_t *cookie = (void*)vcookie; - size_t left; + size_t left; size_t size = *psize; if (size > cookie->_left) { /* First, skip the required bytes. */ @@ -60,7 +60,7 @@ CASIO_LOCAL int casio_limited_read(void *vcookie, unsigned char *dest, return (casio_error_eof); } - if ((err = casio_read(cookie->_stream, dest, size))) { + if ((err = casio_read(cookie->_stream, dest, &size))) { cookie->_left = 0; /* XXX: depends on the error? */ return (err); } diff --git a/lib/stream/builtin/memory.c b/lib/stream/builtin/memory.c index ea4936c..0fd8b5a 100644 --- a/lib/stream/builtin/memory.c +++ b/lib/stream/builtin/memory.c @@ -38,9 +38,10 @@ typedef struct { * @return the error code (0 if ok). */ -CASIO_LOCAL int casio_memory_read(void *vcookie, unsigned char *dest, size_t size) +CASIO_LOCAL int casio_memory_read(void *vcookie, unsigned char *dest, size_t *psize) { memory_cookie_t *cookie = (void*)vcookie; + size_t size = *psize; if (((size_t)-1 - cookie->_offset) < size) /* overflow */ return (casio_error_read); diff --git a/lib/stream/builtin/streams/read.c b/lib/stream/builtin/streams/read.c index 74e86e0..d3cf63a 100644 --- a/lib/stream/builtin/streams/read.c +++ b/lib/stream/builtin/streams/read.c @@ -30,9 +30,10 @@ */ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, - unsigned char *dest, size_t size) + unsigned char *dest, size_t *psize) { int fd = cookie->_readfd; + size_t size = *psize; /* Transmit what's already in the buffer. */ diff --git a/lib/stream/builtin/streams/streams.h b/lib/stream/builtin/streams/streams.h index d04b078..d1e71eb 100644 --- a/lib/stream/builtin/streams/streams.h +++ b/lib/stream/builtin/streams/streams.h @@ -53,7 +53,7 @@ CASIO_EXTERN int CASIO_EXPORT casio_streams_settm CASIO_EXTERN int CASIO_EXPORT casio_streams_read OF((streams_cookie_t *casio__cookie, - unsigned char *casio__dest, size_t casio__size)); + unsigned char *casio__dest, size_t *casio__psize)); CASIO_EXTERN int CASIO_EXPORT casio_streams_write OF((streams_cookie_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/read.c b/lib/stream/read.c index 42b5fd6..3a70350 100644 --- a/lib/stream/read.c +++ b/lib/stream/read.c @@ -28,7 +28,7 @@ * @return the error code (0 if ok). */ -int CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t size) +int CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t *psize) { int err; @@ -36,16 +36,20 @@ int CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t size) failure(~stream->casio_stream_mode & CASIO_OPENMODE_READ, casio_error_read) /* read */ - if (!size) return (0); - err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, size); - if (err) { + if (!(*psize)) return (0); + if (!psize) { + size_t size = -1; + err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, &size); + } else { + err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, psize); + } + if (err && err != casio_error_ieof) { msg((ll_error, "Stream reading failure: %s", casio_strerror(err))); goto fail; } /* move the cursor and return */ - stream->casio_stream_offset += size; - err = 0; + stream->casio_stream_offset += *psize; fail: stream->casio_stream_lasterr = err; return (err); diff --git a/lib/stream/seek.c b/lib/stream/seek.c index 376221c..419e53f 100644 --- a/lib/stream/seek.c +++ b/lib/stream/seek.c @@ -64,7 +64,7 @@ int CASIO_EXPORT casio_seek(casio_stream_t *stream, casio_off_t offset, /* Read that much from the stream. */ - if ((err = casio_read(stream, buf, rd))) + if ((err = casio_read(stream, buf, &rd))) goto fail; to_skip -= rd; diff --git a/src/p7/main.c b/src/p7/main.c index 1185b27..b87943c 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -250,7 +250,8 @@ int main(int ac, char **av) /* Check according to menu */ - casio_path_t path; + casio_path_t path = { 0 }; + casio_stream_t *fileStream = NULL; switch (args.menu) { #if 0 case mn_send: @@ -277,12 +278,6 @@ int main(int ac, char **av) args.storage, 1, args.force ? NULL : &sendfile_confirm, args.nicedisp ? &sendfile_display : NULL); break; - case mn_get: - err = casio_reqfile(handle, args.local, - args.dirname, args.filename, - args.storage, args.nicedisp && args.local != stdout - ? &sendfile_display : NULL); - break; case mn_copy: err = casio_copy(handle, args.dirname, args.filename, args.newdir, args.newname, args.storage); @@ -295,15 +290,59 @@ int main(int ac, char **av) err = casio_reset(handle, args.storage); break; #endif + case mn_get: + // Initialize the path + path.casio_path_device = args.storage; + + // Make the node + casio_make_pathnode(&path.casio_path_nodes, strlen(args.filename)); + memcpy(path.casio_path_nodes->casio_pathnode_name, args.filename, strlen(args.filename)); + // Set flags + path.casio_path_flags = casio_pathflag_rel; + + // Open 7.00 fs and open file in read only + if ((err = casio_open_seven_fs(&fs, handle)) + || (err = casio_open(fs, &fileStream, &path, 0, CASIO_OPENMODE_READ))) + break; + + char buffer[CASIO_SEVEN_MAX_RAWDATA_SIZE]; + FILE *file = fopen(args.filename, "wb"); + + if(!file) { + fprintf(stderr, "Couldn't open in write mode %s", args.filename); + err = casio_error_unknown; + break; + } + size_t size; + do + { + size = sizeof(buffer); + err = casio_read(fileStream, buffer, &size); + fwrite(buffer, 1, size, file); + if(err == casio_error_ieof) + break; + } while (err == 0); + + /* All good so close streams and clear error */ + err = 0; + fclose(file); + casio_close(fileStream); + + break; + case mn_list: + // Initialize the path path.casio_path_device = args.storage; casio_make_pathnode(&path.casio_path_nodes, 1); path.casio_path_flags = casio_pathflag_rel; + + // Open 7.00 fs and list if ((err = casio_open_seven_fs(&fs, handle)) || (err = casio_list(fs, &path, print_file_info, NULL))) break; break; + case mn_optimize: if ((err = casio_open_seven_fs(&fs, handle)) || (err = casio_optimize(fs, args.storage))) diff --git a/src/p7os/procs/fxremote_flash.c b/src/p7os/procs/fxremote_flash.c index 2eac84c..7d53bb0 100644 --- a/src/p7os/procs/fxremote_flash.c +++ b/src/p7os/procs/fxremote_flash.c @@ -83,7 +83,7 @@ static int send_sector(casio_link_t *handle, casio_stream_t *buffer, /* Prepare the block. */ block.destination = casio_be32toh(buf); block.length = casio_be32toh(len); - if ((buf_err = casio_read(buffer, block.data, len))) { + if ((buf_err = casio_read(buffer, block.data, &len))) { osdisp_interrupt(&osdisp_cookie); return (casio_error_read); } From 54ec2ce352e9c6f3e7376b3e22b3276a20806ed0 Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Wed, 1 Jan 2020 21:33:15 +0100 Subject: [PATCH 2/5] Adding osrecover.sh4 (simlo) tools for update the casiowin os --- tools/osrecover.sh4 | Bin 0 -> 10240 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tools/osrecover.sh4 diff --git a/tools/osrecover.sh4 b/tools/osrecover.sh4 new file mode 100644 index 0000000000000000000000000000000000000000..f9e16a2294a92b4c78fcb3036b789f7d3bdfb97f GIT binary patch literal 10240 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 KahBWg;{N~{IJ|QJ literal 0 HcmV?d00001 From 842122643bb3657cc7a4d6fa48e4d6e306d28328 Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Wed, 1 Jan 2020 23:39:56 +0100 Subject: [PATCH 3/5] Small fixies in p7os and changing error convention for stream read (write tomorow) --- include/libcasio/stream.h | 8 ++--- lib/file/file.h | 19 +++++----- lib/internals.h | 1 + lib/link/seven/dataflow.c | 8 +++-- lib/link/seven/datastream.c | 36 +++++++++---------- lib/link/seven/receive.c | 5 ++- lib/link/seven/scsi.c | 52 ++++++++++++++++------------ lib/mcsfile/mcsfile.h | 17 ++++----- lib/stream/builtin/csum32.c | 16 +++++---- lib/stream/builtin/file.c | 23 +++++++----- lib/stream/builtin/libusb/libusb.h | 4 +-- lib/stream/builtin/libusb/read.c | 24 ++++++++----- lib/stream/builtin/limited.c | 26 ++++++++------ lib/stream/builtin/memory.c | 16 +++++---- lib/stream/builtin/streams/read.c | 18 ++++++---- lib/stream/builtin/streams/streams.h | 4 +-- lib/stream/read.c | 31 ++++++++++------- lib/stream/seek.c | 6 ++-- src/p7/main.c | 8 ++--- src/p7/main.h | 1 + src/p7os/procs/fxremote_flash.c | 5 +-- src/p7os/procs/std_prepare.c | 2 +- 22 files changed, 185 insertions(+), 145 deletions(-) diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index ec2cd4e..77feebb 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -100,8 +100,8 @@ typedef int casio_stream_setattrs_t typedef int casio_stream_settm_t OF((void *, const casio_timeouts_t *)); -typedef int casio_stream_read_t - OF((void *, unsigned char *, size_t *)); +typedef size_t casio_stream_read_t + OF((void *, unsigned char *, size_t)); typedef int casio_stream_write_t OF((void *, const unsigned char *, size_t)); typedef int casio_stream_seek_t @@ -359,9 +359,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_get_lasterr /* Read and write data from and to a stream. */ -CASIO_EXTERN int CASIO_EXPORT casio_read +CASIO_EXTERN size_t CASIO_EXPORT casio_read OF((casio_stream_t *casio__stream, - void *casio__dest, size_t *casio__psize)); + void *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_write OF((casio_stream_t *casio__stream, const void *casio__data, size_t casio__size)); diff --git a/lib/file/file.h b/lib/file/file.h index 5cd24fc..ec14301 100644 --- a/lib/file/file.h +++ b/lib/file/file.h @@ -28,16 +28,15 @@ /* Read from a stream. */ # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - size_t size = CASIO__SZ; \ - int READ_err = casio_read(buffer, (CASIO__TO), &size); \ - if (READ_err) return (READ_err); } -# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ \ - size_t size = CASIO__SZ; \ - err = casio_read(buffer, (CASIO__TO), &size); -# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ \ - size_t size = CASIO__SZ; \ - if ((err = casio_read(buffer, (CASIO__TO), &size))) \ - goto fail; + int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if (READ_err == -1) return (errno); } +# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ { \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + err = errno; } +# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ { \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if ((err = errno)) \ + goto fail; } /* Read using size of the object. */ diff --git a/lib/internals.h b/lib/internals.h index 2823301..de00678 100644 --- a/lib/internals.h +++ b/lib/internals.h @@ -26,6 +26,7 @@ # include # include # include +# include # include "utils/endian.h" # include "log/log.h" diff --git a/lib/link/seven/dataflow.c b/lib/link/seven/dataflow.c index 1c31da1..ab158df 100644 --- a/lib/link/seven/dataflow.c +++ b/lib/link/seven/dataflow.c @@ -64,8 +64,10 @@ int CASIO_EXPORT casio_seven_send_buffer(casio_link_t *handle, /* Read the big block. */ toread = min(BUFSIZE, size); - err = casio_read(buffer, buf + 8, &toread); - if (err) return (casio_error_noread); + toread = casio_read(buffer, buf + 8, toread); + if (toread == (size_t)-1) { + return (casio_error_noread); + } size -= toread; /* Send each block. */ @@ -95,7 +97,7 @@ int CASIO_EXPORT casio_seven_send_buffer(casio_link_t *handle, /* Unshift. */ if (handle->casio_link_flags & casio_linkflag_shifted) err = casio_seven_unshift(handle); - + return (0); fail: return (err); } diff --git a/lib/link/seven/datastream.c b/lib/link/seven/datastream.c index 456baff..ea8ef2c 100644 --- a/lib/link/seven/datastream.c +++ b/lib/link/seven/datastream.c @@ -32,7 +32,6 @@ typedef struct { casio_link_progress_t *_disp; void *_disp_cookie; unsigned int _id, _total; - unsigned int _totalsize; /* */ unsigned int _lastsize; /* last packet size */ /* buffer management */ @@ -52,15 +51,16 @@ typedef struct { * @arg cookie the cookie. * @arg data the data to read. * @arg size the size of the data to read. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, - unsigned char *data, size_t *psize) +CASIO_LOCAL size_t casio_seven_data_read(seven_data_cookie_t *cookie, + unsigned char *data, size_t size) { - int err; size_t tocopy; size_t size = *psize; + int err; size_t tocopy; casio_link_t *handle = cookie->_link; unsigned int lastsize = 0; + size_t copiedsize = 0; /* Check if the stream is faulty. */ if (cookie->_faulty) @@ -72,14 +72,16 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, if (tocopy) { memcpy(data, &cookie->_current[cookie->_pos], tocopy); cookie->_pos += tocopy; - data += tocopy; size -= tocopy; + data += tocopy; size -= tocopy; copiedsize += tocopy; - if (!size) return (0); + if (size == 0) return (size); } /* Check if we have already finished. */ - if (cookie->_total && cookie->_id == cookie->_total) - return (casio_error_ieof); + if (cookie->_total && cookie->_id == cookie->_total) { + errno = casio_error_eof; + return (-1); + } /* Receive packets. */ while (size) { @@ -88,8 +90,7 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, if (err) goto fail; /* If swap roles there is the end of file */ if (response.casio_seven_packet_type == casio_seven_type_swp) { - *psize = lastsize; - return (casio_error_ieof); + return copiedsize; } if (response.casio_seven_packet_type != casio_seven_type_data) { msg((ll_error, "Packet wasn't a data packet, wtf?")); @@ -117,9 +118,10 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, /* Increment and copy. */ lastsize = response.casio_seven_packet_data_size; + copiedsize += lastsize; if (size >= lastsize) { memcpy(data, response.casio_seven_packet_data, lastsize); - data += lastsize; size -= lastsize; cookie->_totalsize += lastsize; + data += lastsize; size -= lastsize; continue; } @@ -128,15 +130,15 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, memcpy(data, response.casio_seven_packet_data, size); memcpy(&cookie->_current[size], &response.casio_seven_packet_data[size], lastsize - size); - cookie->_totalsize += lastsize; - return (0); + return copiedsize; } - return (0); + return copiedsize; fail: /* XXX: tell the distant device we have a problem? */ cookie->_faulty = 1; - return (err); + errno = err; + return (-1); } /** @@ -348,7 +350,6 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 1; cookie->_total = (unsigned int)(size / BUFSIZE) + !!cookie->_lastsize; cookie->_lastsize = (unsigned int)(size % BUFSIZE); - cookie->_totalsize = size; if (!cookie->_lastsize) cookie->_lastsize = BUFSIZE; } else { msg((ll_info, "The data stream is a read one.")); @@ -358,7 +359,6 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 0; cookie->_total = 0; cookie->_lastsize = 0; - cookie->_totalsize = size; } /* initialize the stream */ diff --git a/lib/link/seven/receive.c b/lib/link/seven/receive.c index ce0a111..43754ad 100644 --- a/lib/link/seven/receive.c +++ b/lib/link/seven/receive.c @@ -175,10 +175,9 @@ CASIO_LOCAL const char *gettermstring(casio_seven_term_t code) #define buffer handle->casio_link_recv_buffer #define COMPLETE_PACKET(N) { \ - size_t size##__LINE__ = (size_t)N; \ int COMP_PACKET_err = casio_read(handle->casio_link_stream, \ - &buffer[received], &size##__LINE__); \ - received += size##__LINE__; if (COMP_PACKET_err) return (COMP_PACKET_err); } + &buffer[received], (size_t)N); \ + received += COMP_PACKET_err; if (COMP_PACKET_err == -1) return (errno); } CASIO_LOCAL int casio_seven_decode(casio_link_t *handle, int scralign) { diff --git a/lib/link/seven/scsi.c b/lib/link/seven/scsi.c index cc79567..86ea65a 100644 --- a/lib/link/seven/scsi.c +++ b/lib/link/seven/scsi.c @@ -83,24 +83,27 @@ typedef struct { * Read and write from the stream. * --- */ -CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, - unsigned char *buffer, size_t *size) +CASIO_LOCAL size_t seven_scsi_read(seven_scsi_cookie_t *cookie, + unsigned char *buffer, size_t size) { casio_scsi_t scsi; int err; + size_t copiedsize = 0; /* Empty what's already in the buffer. */ if (cookie->left) { - if (cookie->left >= *size) { - memcpy(buffer, cookie->ptr, *size); - cookie->ptr += *size; - cookie->left -= *size; - return (0); + if (cookie->left >= size) { + memcpy(buffer, cookie->ptr, size); + cookie->ptr += size; + cookie->left -= size; + copiedsize += cookie->left; + return copiedsize; } memcpy(buffer, cookie->ptr, cookie->left); buffer += cookie->left; - *size -= cookie->left; + size -= cookie->left; + copiedsize += cookie->left; reset_cookie(cookie); } @@ -125,8 +128,10 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, scsi.casio_scsi_data = poll_data; scsi.casio_scsi_data_len = 16; - if ((err = casio_scsi_request(cookie->stream, &scsi))) - return (err); + if ((err = casio_scsi_request(cookie->stream, &scsi))) { + errno = err; + return (-1); + } mem((ll_info, poll_data, 16)); @@ -151,14 +156,14 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, * We could also check that `avail < 0x10000` because we need to * express it later, but it is imposed by the source format. */ - if (avail > *size && *size <= cookie->size) { + if (avail > size && size <= cookie->size) { to = cookie->ptr; if (avail > cookie->size) avail = cookie->size; } else { to = buffer; - if (avail > *size) - avail = *size; + if (avail > size) + avail = size; } /* Actually get the data. */ @@ -176,24 +181,27 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, scsi.casio_scsi_data = cookie->ptr; scsi.casio_scsi_data_len = avail; - if ((err = casio_scsi_request(cookie->stream, &scsi))) - return (err); + if ((err = casio_scsi_request(cookie->stream, &scsi))) { + errno = err; + return (-1); + } } if (to == buffer) { buffer += avail; - *size -= avail; + size -= avail; } else { cookie->left = avail; - memcpy(buffer, cookie->ptr, *size); - cookie->ptr += *size; - cookie->left -= *size; - *size = 0; + memcpy(buffer, cookie->ptr, size); + cookie->ptr += size; + cookie->left -= size; + copiedsize += size; + size = 0; } - } while (*size); + } while (size); - return (0); + return copiedsize; } CASIO_LOCAL int seven_scsi_write(seven_scsi_cookie_t *cookie, diff --git a/lib/mcsfile/mcsfile.h b/lib/mcsfile/mcsfile.h index ee61550..8d68e99 100644 --- a/lib/mcsfile/mcsfile.h +++ b/lib/mcsfile/mcsfile.h @@ -26,20 +26,15 @@ /* Read from a stream. */ -/* FIXME : size_t size##__LINE__ = CASIO__SZ; - * Replace this by other thing that is more futur proof -*/ - # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - int READ_err = casio_read(buffer, (CASIO__TO), &size##__LINE__); \ - if (READ_err) return (READ_err); } + int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if (READ_err == -1) return (errno); } # define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - err = casio_read(buffer, (CASIO__TO), &size##__LINE__); } + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + err = errno; } # define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - if ((err = casio_read(buffer, (CASIO__TO), &size##__LINE__))) \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if ((err = errno)) \ goto fail; } /* Read using size of the object. */ diff --git a/lib/stream/builtin/csum32.c b/lib/stream/builtin/csum32.c index 3169f28..3e3a439 100644 --- a/lib/stream/builtin/csum32.c +++ b/lib/stream/builtin/csum32.c @@ -38,24 +38,26 @@ struct thecookie { * @arg cookie the cookie. * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int csum32_read(struct thecookie *cookie, - unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t csum32_read(struct thecookie *cookie, + unsigned char *dest, size_t size) { int err; - size_t size = *psize; /* Make the call. */ - err = casio_read(cookie->_stream, dest, &size); - if (err) return (err); + size = casio_read(cookie->_stream, dest, size); + if (size == (size_t)-1) { + errno = errno; + return (-1); + } /* Make the checksum. */ *cookie->_checksum = casio_checksum32(dest, size, *cookie->_checksum); - return (0); + return size; } /** diff --git a/lib/stream/builtin/file.c b/lib/stream/builtin/file.c index 90fc051..b3deb5d 100644 --- a/lib/stream/builtin/file.c +++ b/lib/stream/builtin/file.c @@ -46,14 +46,14 @@ typedef struct { * @arg cookie the cookie. * @arg data the data pointer * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, - unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t casio_file_read(file_cookie_t *cookie, + unsigned char *dest, size_t size) { size_t recv; - size_t size = *psize; + size_t copiedsize = 0; /* Main receiving loop. */ @@ -67,6 +67,7 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, dest = (void*)((char*)dest + recv); size -= recv; + copiedsize += recv; /* Check the error. */ @@ -91,7 +92,8 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, /* End of file. */ msg((ll_error, "encountered an end of file")); - return (casio_error_eof); + errno = casio_error_eof; + return (-1); case EINTR: /* alarm */ case ETIMEDOUT: @@ -101,21 +103,24 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, /* A timeout has occurred. */ msg((ll_error, "timeout received")); - return (casio_error_timeout); + errno = casio_error_timeout; + return (-1); case ENODEV: case EPIPE: case ESPIPE: /* A device error has occured. */ msg((ll_error, "calculator was disconnected")); - return (casio_error_nocalc); + errno = casio_error_nocalc; + return (-1); default: msg((ll_fatal, "errno was %d: %s", errno, strerror(errno))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } - return (0); + return copiedsize; } /** diff --git a/lib/stream/builtin/libusb/libusb.h b/lib/stream/builtin/libusb/libusb.h index b2dea05..75196b9 100644 --- a/lib/stream/builtin/libusb/libusb.h +++ b/lib/stream/builtin/libusb/libusb.h @@ -58,9 +58,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_libusb_settm /* Character device callbacks. */ -CASIO_EXTERN int CASIO_EXPORT casio_libusb_read +CASIO_EXTERN size_t CASIO_EXPORT casio_libusb_read OF((cookie_libusb_t *casio__cookie, - unsigned char *casio__dest, size_t *casio__psize)); + unsigned char *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_libusb_write OF((cookie_libusb_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/builtin/libusb/read.c b/lib/stream/builtin/libusb/read.c index e8badeb..9859394 100644 --- a/lib/stream/builtin/libusb/read.c +++ b/lib/stream/builtin/libusb/read.c @@ -26,15 +26,15 @@ * @arg vcookie the cookie (voided) * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, - unsigned char *dest, size_t *psize) +size_t CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, + unsigned char *dest, size_t size) { int libusberr; size_t tocopy; - size_t size = *psize; + size_t copiedsize = 0; /* Transmit what's already in the buffer. */ @@ -46,6 +46,7 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, cookie->_start += tocopy; dest += tocopy; size -= tocopy; + copiedsize += tocopy; } /* Main receiving loop. */ @@ -65,15 +66,18 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, case LIBUSB_ERROR_NO_DEVICE: case LIBUSB_ERROR_IO: msg((ll_error, "The calculator is not here anymore :(")); - return (casio_error_nocalc); + errno = casio_error_nocalc; + return (-1); case LIBUSB_ERROR_TIMEOUT: - return (casio_error_timeout); + errno = casio_error_timeout; + return (-1); default: msg((ll_fatal, "libusb error was %d: %s", libusberr, libusb_strerror(libusberr))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } /* Get the current size to copy. */ @@ -88,13 +92,17 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, dest += tocopy; size -= tocopy; + /* Increment the total copied size */ + + copiedsize += tocopy; + /* Correct start and end points. */ cookie->_start = tocopy; cookie->_end = (size_t)recv - 1; } - return (0); + return copiedsize; } #endif diff --git a/lib/stream/builtin/limited.c b/lib/stream/builtin/limited.c index 5b6a244..d07a84d 100644 --- a/lib/stream/builtin/limited.c +++ b/lib/stream/builtin/limited.c @@ -37,35 +37,39 @@ typedef struct { * @arg vcookie the cookie (uncasted). * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_limited_read(void *vcookie, unsigned char *dest, - size_t *psize) +CASIO_LOCAL size_t casio_limited_read(void *vcookie, unsigned char *dest, + size_t size) { int err; limited_cookie_t *cookie = (void*)vcookie; - size_t left; size_t size = *psize; + size_t left; size_t copiedsize; if (size > cookie->_left) { /* First, skip the required bytes. */ left = cookie->_left; cookie->_left = 0; - if ((err = casio_skip(cookie->_stream, left))) - return (err); + if ((err = casio_skip(cookie->_stream, left))) { + errno = err; + return (-1); + } /* Once the skip is done successfully, we return that we * have an EOF. */ - - return (casio_error_eof); + errno = casio_error_eof; + return (-1); } - if ((err = casio_read(cookie->_stream, dest, &size))) { + size = casio_read(cookie->_stream, dest, size); + if (size == (size_t)-1) { cookie->_left = 0; /* XXX: depends on the error? */ - return (err); + errno = errno; + return (-1); } cookie->_left -= size; - return (0); + return size; } /** diff --git a/lib/stream/builtin/memory.c b/lib/stream/builtin/memory.c index 0fd8b5a..33fa8e6 100644 --- a/lib/stream/builtin/memory.c +++ b/lib/stream/builtin/memory.c @@ -35,25 +35,27 @@ typedef struct { * @arg vcookie the cookie (uncasted) * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_memory_read(void *vcookie, unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t casio_memory_read(void *vcookie, unsigned char *dest, size_t size) { memory_cookie_t *cookie = (void*)vcookie; - size_t size = *psize; - if (((size_t)-1 - cookie->_offset) < size) /* overflow */ - return (casio_error_read); + if (((size_t)-1 - cookie->_offset) < size) { /* overflow */ + errno = casio_error_read; + return (-1); + } if (cookie->_offset + (casio_off_t)size > cookie->_size) { cookie->_offset = cookie->_size - 1; - return (casio_error_eof); + errno = casio_error_eof; + return (-1); } memcpy(dest, &cookie->_memory[cookie->_offset], size); cookie->_offset += size; - return (0); + return size; } /** diff --git a/lib/stream/builtin/streams/read.c b/lib/stream/builtin/streams/read.c index d3cf63a..2f61377 100644 --- a/lib/stream/builtin/streams/read.c +++ b/lib/stream/builtin/streams/read.c @@ -26,14 +26,14 @@ * @arg cookie the cookie. * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, - unsigned char *dest, size_t *psize) +size_t CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, + unsigned char *dest, size_t size) { int fd = cookie->_readfd; - size_t size = *psize; + size_t copiedsize = 0; /* Transmit what's already in the buffer. */ @@ -45,6 +45,7 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, cookie->_start += tocopy; dest += tocopy; size -= tocopy; + copiedsize += tocopy; } /* Main receiving loop. */ @@ -68,7 +69,8 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, default: msg((ll_fatal, "error was %d: %s", errno, strerror(errno))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } /* Get the current size to copy. */ @@ -82,13 +84,17 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, dest += tocopy; size -= tocopy; + /* Increment total size copied */ + + copiedsize += tocopy; + /* Correct start and end points. */ cookie->_start = tocopy; cookie->_end = (size_t)recv - 1; } - return (0); + return copiedsize; } #endif diff --git a/lib/stream/builtin/streams/streams.h b/lib/stream/builtin/streams/streams.h index d1e71eb..d884a1c 100644 --- a/lib/stream/builtin/streams/streams.h +++ b/lib/stream/builtin/streams/streams.h @@ -51,9 +51,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_streams_settm /* Character device callbacks. */ -CASIO_EXTERN int CASIO_EXPORT casio_streams_read +CASIO_EXTERN size_t CASIO_EXPORT casio_streams_read OF((streams_cookie_t *casio__cookie, - unsigned char *casio__dest, size_t *casio__psize)); + unsigned char *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_streams_write OF((streams_cookie_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/read.c b/lib/stream/read.c index 3a70350..9c1028e 100644 --- a/lib/stream/read.c +++ b/lib/stream/read.c @@ -25,32 +25,37 @@ * @arg stream the stream to read from. * @arg dest the destination buffer. * @arg size the amount of bytes to read. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t *psize) +size_t CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t size) { - int err; + int err = casio_error_ok; /* check if we can read */ failure(~stream->casio_stream_mode & CASIO_OPENMODE_READ, casio_error_read) /* read */ - if (!(*psize)) return (0); - if (!psize) { - size_t size = -1; - err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, &size); - } else { - err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, psize); - } - if (err && err != casio_error_ieof) { + if (size == 0) return (0); + + size = (*getcb(stream, read))(stream->casio_stream_cookie, dest, size); + + if (size == (size_t)-1) { + err = errno; + if (err == casio_error_eof) { + msg((ll_info, "Stream reading get to the end (EOF)")); + goto fail; + } msg((ll_error, "Stream reading failure: %s", casio_strerror(err))); goto fail; } /* move the cursor and return */ - stream->casio_stream_offset += *psize; + if(size != 1) { + stream->casio_stream_offset += size; + } fail: stream->casio_stream_lasterr = err; - return (err); + errno = err; + return size; } diff --git a/lib/stream/seek.c b/lib/stream/seek.c index 419e53f..c688d0d 100644 --- a/lib/stream/seek.c +++ b/lib/stream/seek.c @@ -63,9 +63,11 @@ int CASIO_EXPORT casio_seek(casio_stream_t *stream, casio_off_t offset, size_t rd = min(to_skip, 128); /* Read that much from the stream. */ - - if ((err = casio_read(stream, buf, &rd))) + size_t readed = casio_read(stream, buf, rd); + if (readed == (size_t)-1) { + err = errno; goto fail; + } to_skip -= rd; } while (to_skip); diff --git a/src/p7/main.c b/src/p7/main.c index b87943c..1794a6b 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -316,11 +316,11 @@ int main(int ac, char **av) size_t size; do { - size = sizeof(buffer); - err = casio_read(fileStream, buffer, &size); + size = casio_read(fileStream, buffer, sizeof(buffer)); + if(size == (size_t)-1) + err = errno; + if(err == casio_error_eof) fwrite(buffer, 1, size, file); - if(err == casio_error_ieof) - break; } while (err == 0); /* All good so close streams and clear error */ diff --git a/src/p7/main.h b/src/p7/main.h index 2242152..f48fe23 100644 --- a/src/p7/main.h +++ b/src/p7/main.h @@ -20,6 +20,7 @@ # define MAIN_H # include # include +# include /* Menus */ typedef enum { diff --git a/src/p7os/procs/fxremote_flash.c b/src/p7os/procs/fxremote_flash.c index 7d53bb0..7fdc13c 100644 --- a/src/p7os/procs/fxremote_flash.c +++ b/src/p7os/procs/fxremote_flash.c @@ -60,7 +60,7 @@ struct data_copy_block { static int send_sector(casio_link_t *handle, casio_stream_t *buffer, unsigned long addr, unsigned long size) { - int err, buf_err; + int err; osdisp_t osdisp_cookie; /* Sending the data to the Update.Exe's. */ @@ -83,7 +83,8 @@ static int send_sector(casio_link_t *handle, casio_stream_t *buffer, /* Prepare the block. */ block.destination = casio_be32toh(buf); block.length = casio_be32toh(len); - if ((buf_err = casio_read(buffer, block.data, &len))) { + len = casio_read(buffer, block.data, len); + if (len == (size_t)-1) { osdisp_interrupt(&osdisp_cookie); return (casio_error_read); } diff --git a/src/p7os/procs/std_prepare.c b/src/p7os/procs/std_prepare.c index 6663169..5e13a80 100644 --- a/src/p7os/procs/std_prepare.c +++ b/src/p7os/procs/std_prepare.c @@ -55,7 +55,7 @@ int prepare(args_t *args) /* Sleep while the software on the calculator sets up the * communication interface. */ printf("Waiting for the Update.Exe to set up the communication...\n"); - casio_sleep(1000); + casio_sleep(3000); err = 0; fail: From d77f24cdda4363caf267158c75834102b44bf900 Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Wed, 1 Jan 2020 23:56:30 +0100 Subject: [PATCH 4/5] Delete unused error --- include/libcasio/error.h | 3 +-- lib/errors.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/libcasio/error.h b/include/libcasio/error.h index 289dbf3..f100da7 100644 --- a/include/libcasio/error.h +++ b/include/libcasio/error.h @@ -63,8 +63,7 @@ typedef int casio_error_t; # define casio_error_timeout 0x14 /* a timeout has occured. */ # define casio_error_access 0x15 /* could not access the device (perms) */ # define casio_error_noaccess 0x15 /* (alias) */ -# define casio_error_eof 0x16 /* end of file error */ -# define casio_error_ieof 0x17 /* info end of file error */ +# define casio_error_eof 0x16 /* end of file */ /* Link errors. */ diff --git a/lib/errors.c b/lib/errors.c index c58a0da..928f696 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -39,8 +39,7 @@ const char* CASIO_EXPORT casio_error_strings[128] = { "a timeout has occurred", "could not get access to the device", "an end of file event has occured", - "an end of file event has occured but was manage", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, From 13d05e6b74fae4e5097ce0649c81b5fb3a3d80e3 Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Wed, 1 Jan 2020 23:59:36 +0100 Subject: [PATCH 5/5] If read size = 0 errno is 0 --- lib/stream/read.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/stream/read.c b/lib/stream/read.c index 9c1028e..c49a9d8 100644 --- a/lib/stream/read.c +++ b/lib/stream/read.c @@ -36,8 +36,10 @@ size_t CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t size) failure(~stream->casio_stream_mode & CASIO_OPENMODE_READ, casio_error_read) /* read */ - if (size == 0) return (0); - + if (size == 0) { + errno = err; + return (0); + } size = (*getcb(stream, read))(stream->casio_stream_cookie, dest, size); if (size == (size_t)-1) {