int link_closedir(link_phy_t handle, int dirp) { link_op_t op; link_reply_t reply; int err; if ( link_dev == NULL ) { return LINK_TRANSFER_ERR; } op.closedir.cmd = LINK_CMD_CLOSEDIR; op.closedir.dirp = dirp; link_debug(3, "Send op\n"); err = link_protocol_masterwrite(handle, &op, sizeof(link_closedir_t)); if ( err < 0 ) { return err; } link_debug(3, "Get Reply\n"); err = link_protocol_masterread(handle, &reply, sizeof(reply)); if ( err < 0 ) { return err; } link_errno = reply.err_number; return reply.err; }
int reset_device(link_phy_t handle, bool invoke_bootloader){ //use "/dev/core" to reset int fd; link_op_t op; fd = link_open(handle, "/dev/core", LINK_O_RDWR); if ( fd < 0 ){ return -1; } op.ioctl.cmd = LINK_CMD_IOCTL; op.ioctl.fildes = fd; op.ioctl.arg = (size_t)NULL; if( invoke_bootloader == false ){ link_debug(3, "Try to reset\n"); op.ioctl.request = I_CORE_RESET; } else { link_debug(3, "Try to invoke bootloader\n"); op.ioctl.request = I_CORE_INVOKEBOOTLOADER; } link_protocol_masterwrite(handle, &op, sizeof(link_ioctl_t)); link_phy_close(handle); return 0; }
int link_open(link_transport_mdriver_t * driver, const char * path, int flags, ...){ link_op_t op; link_reply_t reply; link_mode_t mode; int err; va_list ap; if ( flags & LINK_O_CREAT ){ va_start(ap, flags); mode = va_arg(ap, link_mode_t); va_end(ap); } else { mode = 0; } if( driver == 0 ){ return posix_open(path, convert_flags(flags) | POSIX_OPEN_FLAGS, mode); } link_debug(LINK_DEBUG_MESSAGE, "open %s 0%o 0x%X using %p", path, mode, flags, driver->dev.handle); op.open.cmd = LINK_CMD_OPEN; op.open.path_size = strlen(path) + 1; op.open.flags = flags; op.open.mode = mode; link_debug(LINK_DEBUG_MESSAGE, "Write open op (%p)", driver->dev.handle); err = link_transport_masterwrite(driver, &op, sizeof(link_open_t)); if ( err < 0 ){ link_error("failed to write open op with handle %p", driver->dev.handle); return link_handle_err(driver, err); } //Send the path on the bulk out endpoint link_debug(LINK_DEBUG_MESSAGE, "Write open path (%d bytes)", op.open.path_size); err = link_transport_masterwrite(driver, path, op.open.path_size); if ( err < 0 ){ link_error("failed to write path"); return link_handle_err(driver, err); } //read the reply to see if the file opened correctly err = link_transport_masterread(driver, &reply, sizeof(reply)); if ( err < 0 ){ link_error("failed to read the reply"); return link_handle_err(driver, err); } if ( reply.err < 0 ){ link_errno = reply.err_number; link_debug(LINK_DEBUG_WARNING, "Failed to ioctl file (%d)", link_errno); } else { link_debug(LINK_DEBUG_MESSAGE, "Opened fildes: %d", reply.err); } return reply.err; }
int link_open(link_phy_t handle, const char * path, int flags, ...){ link_op_t op; link_reply_t reply; link_mode_t mode; int err; va_list ap; link_debug(5, "open %s 0%o\n", path, flags); if ( flags & LINK_O_CREAT ){ va_start(ap, flags); mode = va_arg(ap, link_mode_t); va_end(ap); } else { mode = 0; } op.open.cmd = LINK_CMD_OPEN; op.open.path_size = strlen(path) + 1; op.open.flags = flags; op.open.mode = mode; link_debug(4, "Write open op (0x%lX)\n", (long unsigned int)link_dev); err = link_protocol_masterwrite(handle, &op, sizeof(link_open_t)); if ( err < 0 ){ link_error("failed to write open op\n"); return link_handle_err(handle, err); } //Send the path on the bulk out endpoint link_debug(4, "Write open path (%d bytes)\n", op.open.path_size); err = link_protocol_masterwrite(handle, path, op.open.path_size); if ( err < 0 ){ link_error("failed to write path\n"); return link_handle_err(handle, err); } //read the reply to see if the file opened correctly err = link_protocol_masterread(handle, &reply, sizeof(reply)); if ( err < 0 ){ link_error("failed to read the reply\n"); return link_handle_err(handle, err); } link_errno = reply.err_number; if ( reply.err < 0 ){ link_debug(1, "Failed to open file (%d)\n", link_errno); return LINK_DEV_ERROR; } return reply.err; }
int link_opendir(link_phy_t handle, const char * dirname) { link_op_t op; link_reply_t reply; int len; int err; if ( link_dev == NULL ) { link_error("No device\n"); return 0; } op.opendir.cmd = LINK_CMD_OPENDIR; op.opendir.path_size = strlen(dirname) + 1; if ( dirname == NULL ) { link_error("Directory name is NULL\n"); return 0; } link_debug(3, "Write op\n"); err = link_protocol_masterwrite(handle, &op, sizeof(link_opendir_t)); if ( err < 0 ) { link_error("Failed to transfer command\n"); return 0; } //Send the path on the bulk out endpoint link_debug(3, "Write path %s\n", dirname); len = link_protocol_masterwrite(handle, dirname, op.opendir.path_size); if ( len < 0 ) { link_error("Failed to write bulk out\n"); return 0; } link_debug(3, "Write path len is %d 0x%X\n", len, reply.err); //read the reply to see if the file opened correctly err = link_protocol_masterread(handle, &reply, sizeof(reply)); if ( err < 0 ) { link_error("Failed to read bulk in\n"); return 0; } link_errno = reply.err_number; link_debug(3, "Opened directory 0x%X\n", reply.err); return reply.err; }
int link_rmdir(link_phy_t handle, const char * path) { link_op_t op; link_reply_t reply; int len; int err; if ( link_dev == NULL ) { return LINK_TRANSFER_ERR; } link_debug(5, "rmdir %s\n", path); op.rmdir.cmd = LINK_CMD_RMDIR; op.rmdir.path_size = strlen(path) + 1; err = link_protocol_masterwrite(handle, &op, sizeof(op)); if ( err < 0 ) { return err; } //Send the path on the bulk out endpoint len = link_protocol_masterwrite(handle, path, op.rmdir.path_size); if ( len < 0 ) { return LINK_TRANSFER_ERR; } //read the reply to see if the file opened correctly err = link_protocol_masterread(handle, &reply, sizeof(reply)); if ( err < 0 ) { return err; } link_errno = reply.err_number; return reply.err; }
int link_writeflash(link_phy_t handle, int addr, const void * buf, int nbyte){ bootloader_writepage_t wattr; int page_size; int bytes_written; int err; bytes_written = 0; wattr.addr = addr; page_size = BOOTLOADER_WRITEPAGESIZE; if( page_size > nbyte ){ page_size = nbyte; } wattr.nbyte = page_size; link_debug(4, "Page size is %d (%d)\n", page_size, nbyte); do { memset(wattr.buf, 0xFF, BOOTLOADER_WRITEPAGESIZE); memcpy(wattr.buf, buf, page_size); err = link_ioctl_delay(handle, LINK_BOOTLOADER_FILDES, I_BOOTLOADER_WRITEPAGE, &wattr, 0, 0); if( err < 0 ){ return err; } wattr.addr += page_size; buf += page_size; bytes_written += page_size; } while(bytes_written < nbyte); return nbyte; }
int link_readdir_r(link_phy_t handle, int dirp, struct link_dirent * entry, struct link_dirent ** result) { link_op_t op; link_reply_t reply; int len; int ret; if ( link_dev == NULL ) { return -1; } op.readdir.cmd = LINK_CMD_READDIR; op.readdir.dirp = dirp; if ( result != NULL ) { *result = NULL; } link_debug(5, "Write op\n"); if (link_protocol_masterwrite(handle, &op, sizeof(link_readdir_t)) < 0) { return -1; } link_debug(5, "Read reply\n"); if (link_protocol_masterread(handle, &reply, sizeof(reply)) < 0) { return -1; } if( reply.err < 0 ) { link_errno = reply.err_number; return reply.err; } //Read the bulk in buffer for the result of the read link_debug(5, "Read link dirent\n"); len = link_protocol_masterread(handle, entry, sizeof(struct link_dirent)); if ( len < 0 ) { return -1; } if ( result != NULL ) { *result = entry; } return 0; }
int link_reset(link_phy_t handle){ link_op_t op; link_debug(3, "try to reset--check bootloader\n"); if( link_isbootloader(handle) ){ //execute the request op.ioctl.cmd = LINK_CMD_IOCTL; op.ioctl.fildes = LINK_BOOTLOADER_FILDES; op.ioctl.request = I_BOOTLOADER_RESET; op.ioctl.arg = 0; link_protocol_masterwrite(handle, &op, sizeof(link_ioctl_t)); link_phy_close(handle); } else { link_debug(3, "reset device with /dev/core\n"); return reset_device(handle, false); } return 0; }
int link_isbootloader(link_phy_t handle){ bootloader_attr_t attr; if( link_ioctl(handle, LINK_BOOTLOADER_FILDES, I_BOOTLOADER_GETATTR, &attr) < 0 ){ //If this fails, no bootloader link_error("Failed to respond to ioctl\n"); return 0; } link_debug(3, "Bootloader Version is 0x%X\n", attr.version); //If the above succeeds, the bootloader is present return 1; }
int link_readflash(link_phy_t handle, int addr, void * buf, int nbyte){ link_op_t op; link_reply_t reply; int err; int len; op.read.cmd = LINK_CMD_READ; op.read.addr = addr; op.read.nbyte = nbyte; link_debug(4, "write read flash op\n"); err = link_protocol_masterwrite(handle, &op, sizeof(link_read_t)); if ( err < 0 ){ return err; } link_debug(4, "read flash data\n"); len = link_protocol_masterread(handle, buf, nbyte); if ( len < 0 ){ return LINK_TRANSFER_ERR; } link_debug(4, "read reply\n"); err = link_protocol_masterread(handle, &reply, sizeof(reply)); if ( err < 0 ){ return err; } if ( reply.err < 0 ){ link_errno = reply.err_number; } link_debug(4, "Read %d bytes from device\n", reply.err); return reply.err; }
#include <stdio.h> #include <string.h> #define GEN_LIN #include "link_debug.c" #define NELEMS(a) (sizeof(a)/sizeof(*(a))) #define PARSER_SIGNATURE 0xE2F3 #define IS_PARSER(m) ( m!=NULL && m->signature==PARSER_SIGNATURE) const char *link_debug(int state); static void link_internal_debug(int old, int new, int c) { printf("%c - %s\n",c,link_debug(old)); } link_parser_t link_parser_new(void) { link_parser_t parser; parser = malloc(sizeof(*parser)); if( parser ) { memset(parser, 0, sizeof(*parser) ); parser->signature = PARSER_SIGNATURE; parser->i = parser->j = 0; parser->link_fnc = NULL ; parser->user_data = NULL;