#include "ihe.h" #include "cmd.h" #include "core.h" static int cmd_loop = 1; extern cmd_table tab[]; /* GLOBAL VARIABLES */ static file_t *g_file = NULL; static buf_t *g_buf = NULL; static int g_flags = FD_RW; int c_version(cmd_arg *arg) { int argc = arg->argc; if ( argc != 0 ) { printf("Command should not have arguments mister\n"); return -1; } printf("Version 0.0.1\n"); return 0; } int c_arg( cmd_arg *arg ) { int i; int argc = arg->argc; char **argv = arg->argv; for (i=0;i */ int c_open( cmd_arg *arg ) { int argc = arg->argc; char **argv = arg->argv; char *fname = NULL; int fret = 0; if ( argc != 1 ) { printf("Neeed one argument\n"); return -1; } fname = argv[0]; fret = file_open_fn( g_file, fname, g_flags ); //!if failure fields could be non empty inside struct if ( fret < 0 ) { printf("Cannot open file %s\n",fname); return -1; } return 0; } /* CLOSE */ int c_close( cmd_arg *arg ) { int fret = 0; fret = file_close( g_file ); if ( fret != 0 ) { printf("Cannot close file\n"); return -1; } return 0; } /* FILE */ int c_info( cmd_arg *arg ) { if ( g_file == NULL ) { printf("no opened files\n"); } else { printf("FILE INFO:\n"); printf("NAME : %s\n", g_file->filename ); printf("FD : %d\n", g_file->fd ); printf("FLAGS : 0x%08x\n", g_file->flags ); printf("MODE : 0x%08x\n", g_file->mode ); printf("OFFSET : %zd\n", g_file->offset ); printf("POSITION: %d\n", g_file->position ); printf("SIZE : %zd\n", g_file->size ); printf("BLOCK : %u\n", g_file->blk_size ); } if ( g_buf == NULL ) { printf("buffer not initialised\n"); } else { printf("BUF:\n"); printf("ADDR : %08x\n", g_buf->buf); printf("SIZE : %d\n", g_buf->size); printf("BUFSIZE: %d\n", g_buf->buf_size); } return 0; } int c_seek( cmd_arg *arg ) { int fret; int argc = arg->argc; char **argv = arg->argv; off_t offset; if (argc != 1) { printf("One argument needed\n"); return -1; } if (g_file->fd == 0) { printf("File descriptor not set\n"); return -1; } offset = atoi( argv[0] ); //!fix that to strtol at least fret = file_seek( g_file, offset ); if ( fret != 0 ) { printf("Cannot seek postion to %zd\n", offset); return -1; } return 0; } int c_pos( cmd_arg *arg ) { int fret = 0; fret = file_pos( g_file ); if ( fret < 0) { printf("Cannot get file position\n"); return -1; } printf("POS:%d\n",fret); return 0; } int c_size( cmd_arg *arg ) { off_t size; size = file_size( g_file ); if ( size < 0 ) { printf("Cannot get file size\n"); return -1; } printf("File size %zu\n", size); return 0; } int c_blk( cmd_arg *arg ) { printf("FILE BLOCK SIZE %u\n", g_file->blk_size ); printf("BUFFER BLOCK SIZE %d (MAX %d)\n", g_buf->size, g_buf->buf_size ); return 0; } int c_read( cmd_arg *arg ) { int ret; if ( g_buf->buf == NULL ) { printf("Buffer mem not allocated\n"); return -1; } ret = file_read_blk( g_file, g_buf->buf ); printf("Readed %d bytes\n", ret); if ( (ret >= 0) && (ret <= g_buf->buf_size) ) { g_buf->size = ret; } return 0; } int c_dump( cmd_arg *arg ) { int i; if ( g_buf->buf == NULL) { printf("Buffer to print empty\n"); return -1; } for (i=0; isize; i++) { printf("%02x",(unsigned char)g_buf->buf[i]); } printf("\n"); return 0; } int c_dumpx( cmd_arg *arg ) { int i,j; if ( g_buf->buf == NULL) { printf("Buffer to print empty\n"); return -1; } for (i=0; isize; i+=16) { for (j=i; jsize ) { printf("%02x ",(unsigned char)g_buf->buf[j]); } else { printf(" "); } } for (j=i; jsize ) //wrong place move to cycle? { if ( isprint(g_buf->buf[j]) ) { printf("%c",(unsigned char)g_buf->buf[j]); } else { printf("\e[7m.\e[0m"); } } } printf("\n"); } printf("\n"); return 0; } //support masks int c_write( cmd_arg *arg ) { /* anonymous function */ uint8_t hex2u8( uint8_t *buf ) { uint8_t ret = 0x00; unsigned long int uli; char str[3]; str[0] = buf[0]; str[1] = buf[1]; str[2] = 0; uli = strtoul( str, NULL, 16 ); ret = uli; return ret; } int argc = arg->argc; char **argv = arg->argv; int i; uint8_t *buf = NULL; int fret; if ( argc != 1 ) { printf("One argument needed\n"); return -1; } if ( (strlen(argv[0])%2) != 0 ) { printf("Input string should be ( str mod 2 == 0) \n"); return -1; } for (i=0;i g_buf->size*2) { printf("Input param bigger then buffer\n"); return -1; } for (i=0; ibuf, buf, strlen(argv[0])/2 ); fret = file_write_blk( g_file, g_buf->buf ); free( buf ); if ( fret < 0) { printf("Couldnt write block to file\n"); return -1; } return 0; } //white spaces should be supported int c_writes( cmd_arg *arg ) { int argc = arg->argc; char **argv = arg->argv; int fret = 0; if ( argc != 1) { printf("Need one argument mister\n"); return -1; } if (((g_buf == NULL) || (g_file == NULL)) || (g_buf->buf == NULL)) { printf("Buffer or file not initialised"); return -1; } if ( strlen(argv[0]) <= g_buf->size ) { memcpy( g_buf->buf, argv[0], strlen(argv[0]) ); fret = file_write_blk( g_file, g_buf->buf ); if ( fret < 0 ) { printf("Couldnt write block to file\n"); return -1; } } else { printf("Input bigger then buffer buf %d input %d\n", g_buf->size, strlen(argv[0])); return -1; } return 0; } int c_flags( cmd_arg *arg ) { int argc = arg->argc; char **argv = arg->argv; if ( argc == 0 ) { printf("FLAGS: 0x%08x\n", g_flags ); return 0; } if ( argc > 1 ) { printf("Only one argument needed\n"); return -1; } if ( strncmp(argv[0],"R",2) == 0 ) { g_flags = FD_RO; } else if ( strncmp(argv[0],"W",2) == 0 ) { g_flags = FD_WO; } else if ( strncmp(argv[0],"RW",3) == 0 ) { g_flags = FD_RW; } else { printf("Unknown mode. Suported R/W/RW\n"); return -1; } return 0; } int c_manifesto( cmd_arg *arg ) { printf("""\ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\ + MANIFESTO +\n\ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\ + 1. All hardware and software that you are owner of belongs to you no software+\n\ + neither hardware patents can stop you to reverse engineer your property. +\n\ + Software/Hardware that you are owning should be obtained in legal way +\n\ + except situation under chapter 7 +\n\ + 2. Any license or patent that disagree with that is unlawfull as it against +\n\ + to personal freedomes. +\n\ + 3. Any compiled or non compiled code on your device is your property. +\n\ + 4. This software is made for any kind of knowledge gaining about software or +\n\ + hardware that are belong to you. No commercial distribution of gained +\n\ + knowledge should be made. Only free knowledge distribution allowed. +\n\ + 5. This software should not be used to harm any living been this doesnt apply+\n\ + to intelectual property of any kind. With this software personal info like+\n\ + adresses, credit cards numbers, names, surnames, passwords should not be +\n\ + gained as it may harm living beans that owns them and surands them. +\n\ + 6. This software is made to explore system, gain understanding of system and +\n\ + protect from system. +\n\ + 7. This software may be used even if any patented or licensed +\n\ + hardware/software is used in hardware or software that may treat human +\n\ + life (rockets, warships, missles, army robots, electronics optical +\n\ + devices, guns, SCADA malware and so on) physicaly doesnt belong to this +\n\ + software user but treatening this software user life. +\n\ + 8. By using this software you take all responsibity of result that may occure+\n\ + while you use it on yourself. +\n\ + 9. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+\n\ + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +\n\ + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +\n\ + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+\n\ + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +\n\ + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +\n\ + DEALINGS IN THE SOFTWARE. +\n\ + 10. If you disagree with any point that mentioned here please stop using this+\n\ + peace of software. +\n\ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\ """); return 0; } /* CMD COMMANDS */ cmd_table tab[] = { {"version", c_version }, {"arg", c_arg }, {"quit", c_quit}, {"help", c_help}, {"?", c_help}, {"open", c_open}, {"close", c_close}, {"info", c_info}, {"seek", c_seek}, {"pos", c_pos}, {"size", c_size}, {"blk", c_blk}, {"read", c_read}, {"dump", c_dump}, {"dumpx", c_dumpx}, {"write", c_write}, {"writes", c_writes}, {"flags", c_flags}, {"manifesto", c_manifesto}, {NULL, NULL } }; int main( int argc, char **argv ) { char *cmd = NULL; cmd_arg *tok = NULL; //preapre global stuff g_file = file_init(); //init basic buffer g_buf = buf_init(); buf_size( g_buf, DEFAULT_BLK_SIZE ); //read line from cmd while( cmd_loop ) { cmd = cmd_line(NULL); //printf("cmd [%s]\n", cmd); //printf("cnt %d\n",cnt_sep(cmd)); tok = cmd_parse( cmd ); free( cmd ); cmd_exec( tok, tab ); cmd_arg_free( tok ); } return 0; }