houskeeping
[openocd.git] / src / helper / ioutil.c
blob80e43d4ead5b331ef1fc47ea846b2ec28246657a
1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* this file contains various functionality useful to standalone systems */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "log.h"
27 #include "types.h"
28 #include "configuration.h"
29 #include "target.h"
31 #include "command.h"
33 #include <time_support.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <strings.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #if !BUILD_ECOSBOARD
42 #include <malloc.h>
43 #endif
44 #include <errno.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #include <dirent.h>
50 #include <netinet/tcp.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <net/if.h>
55 #include <arpa/inet.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netdb.h>
59 #include <netinet/in.h>
60 #include <unistd.h>
61 #include <arpa/inet.h>
62 #include <stdio.h>
63 #include <string.h>
66 #include <ifaddrs.h>
68 #include <unistd.h>
69 #include <stdio.h>
71 int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
72 char **args, int argc)
74 if (argc != 1)
76 command_print(cmd_ctx, "rm <filename>");
77 return ERROR_INVALID_ARGUMENTS;
80 if (unlink(args[0]) != 0)
82 command_print(cmd_ctx, "failed: %d", errno);
85 return ERROR_OK;
89 /* loads a file and returns a pointer to it in memory. The file contains
90 * a 0 byte(sentinel) after len bytes - the length of the file. */
91 int loadFile(const char *fileName, void **data, int *len)
93 FILE * pFile;
94 pFile = fopen(fileName,"rb");
95 if (pFile==NULL)
97 LOG_ERROR("Can't open %s\n", fileName);
98 return ERROR_FAIL;
100 if (fseek(pFile, 0, SEEK_END)!=0)
102 LOG_ERROR("Can't open %s\n", fileName);
103 fclose(pFile);
104 return ERROR_FAIL;
106 *len=ftell(pFile);
107 if (*len==-1)
109 LOG_ERROR("Can't open %s\n", fileName);
110 fclose(pFile);
111 return ERROR_FAIL;
114 if (fseek(pFile, 0, SEEK_SET)!=0)
116 LOG_ERROR("Can't open %s\n", fileName);
117 fclose(pFile);
118 return ERROR_FAIL;
120 *data=malloc(*len+1);
121 if (*data==NULL)
123 LOG_ERROR("Can't open %s\n", fileName);
124 fclose(pFile);
125 return ERROR_FAIL;
128 if (fread(*data, 1, *len, pFile)!=*len)
130 fclose(pFile);
131 free(*data);
132 LOG_ERROR("Can't open %s\n", fileName);
133 return ERROR_FAIL;
135 fclose(pFile);
136 *(((char *)(*data))+*len)=0; /* sentinel */
138 return ERROR_OK;
146 int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
147 char **args, int argc)
149 if (argc != 1)
151 command_print(cmd_ctx, "cat <filename>");
152 return ERROR_INVALID_ARGUMENTS;
155 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
156 void *data;
157 int len;
159 int retval = loadFile(args[0], &data, &len);
160 if (retval == ERROR_OK)
162 command_print(cmd_ctx, "%s", data);
163 free(data);
165 else
167 command_print(cmd_ctx, "%s not found %d", args[0], retval);
170 return ERROR_OK;
172 int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
173 char **args, int argc)
175 if (argc != 1)
177 command_print(cmd_ctx, "trunc <filename>");
178 return ERROR_INVALID_ARGUMENTS;
181 FILE *config_file = NULL;
182 config_file = fopen(args[0], "w");
183 if (config_file != NULL)
184 fclose(config_file);
186 return ERROR_OK;
190 int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
192 static int prev = 0;
193 struct mallinfo info;
195 if (argc != 0)
197 command_print(cmd_ctx, "meminfo");
198 return ERROR_INVALID_ARGUMENTS;
201 info = mallinfo();
203 if (prev > 0)
205 command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
207 prev = info.fordblks;
209 command_print(cmd_ctx, "Available ram: %d", info.fordblks );
211 return ERROR_OK;
215 int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
216 char **args, int argc)
218 if (argc < 1)
220 command_print(cmd_ctx,
221 "append <filename> [<string1>, [<string2>, ...]]");
222 return ERROR_INVALID_ARGUMENTS;
225 FILE *config_file = NULL;
226 config_file = fopen(args[0], "a");
227 if (config_file != NULL)
229 int i;
230 fseek(config_file, 0, SEEK_END);
232 for (i = 1; i < argc; i++)
234 fwrite(args[i], strlen(args[i]), 1, config_file);
235 if (i != argc - 1)
237 fwrite(" ", 1, 1, config_file);
240 fwrite("\n", 1, 1, config_file);
241 fclose(config_file);
244 return ERROR_OK;
249 int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
251 if (argc != 2)
253 return ERROR_INVALID_ARGUMENTS;
256 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
257 void *data;
258 int len;
260 int retval = loadFile(args[0], &data, &len);
261 if (retval != ERROR_OK)
262 return retval;
264 FILE *f = fopen(args[1], "wb");
265 if (f == NULL)
266 retval = ERROR_INVALID_ARGUMENTS;
268 int pos = 0;
269 for (;;)
271 int chunk = len - pos;
272 static const int maxChunk = 512 * 1024; // ~1/sec
273 if (chunk > maxChunk)
275 chunk = maxChunk;
278 if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
279 retval = ERROR_INVALID_ARGUMENTS;
281 if (retval != ERROR_OK)
283 break;
286 command_print(cmd_ctx, "%d", len - pos);
288 pos += chunk;
290 if (pos == len)
291 break;
294 if (retval == ERROR_OK)
296 command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
297 } else
299 command_print(cmd_ctx, "Failed: %d", retval);
302 if (data != NULL)
303 free(data);
304 if (f != NULL)
305 fclose(f);
307 if (retval != ERROR_OK)
308 unlink(args[1]);
310 return retval;
316 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
318 #define IOSIZE 512
319 void copyfile(char *name2, char *name1)
322 int err;
323 char buf[IOSIZE];
324 int fd1, fd2;
325 ssize_t done, wrote;
327 fd1 = open(name1, O_WRONLY | O_CREAT);
328 if (fd1 < 0)
329 SHOW_RESULT( open, fd1 );
331 fd2 = open(name2, O_RDONLY);
332 if (fd2 < 0)
333 SHOW_RESULT( open, fd2 );
335 for (;;)
337 done = read(fd2, buf, IOSIZE );
338 if (done < 0)
340 SHOW_RESULT( read, done );
341 break;
344 if( done == 0 ) break;
346 wrote = write(fd1, buf, done);
347 if( wrote != done ) SHOW_RESULT( write, wrote );
349 if( wrote != done ) break;
352 err = close(fd1);
353 if( err < 0 ) SHOW_RESULT( close, err );
355 err = close(fd2);
356 if( err < 0 ) SHOW_RESULT( close, err );
360 /* utility fn to copy a directory */
361 void copydir(char *name, char *destdir)
363 int err;
364 DIR *dirp;
366 dirp = opendir(destdir);
367 if (dirp==NULL)
369 mkdir(destdir, 0777);
370 } else
372 err = closedir(dirp);
375 dirp = opendir(name);
376 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
378 for (;;)
380 struct dirent *entry = readdir(dirp);
382 if (entry == NULL)
383 break;
385 if (strcmp(entry->d_name, ".") == 0)
386 continue;
387 if (strcmp(entry->d_name, "..") == 0)
388 continue;
390 int isDir = 0;
391 struct stat buf;
392 char fullPath[PATH_MAX];
393 strncpy(fullPath, name, PATH_MAX);
394 strcat(fullPath, "/");
395 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
397 if (stat(fullPath, &buf) == -1)
399 LOG_ERROR("unable to read status from %s", fullPath);
400 break;
402 isDir = S_ISDIR(buf.st_mode) != 0;
404 if (isDir)
405 continue;
407 // diag_printf("<INFO>: entry %14s",entry->d_name);
408 char fullname[PATH_MAX];
409 char fullname2[PATH_MAX];
411 strcpy(fullname, name);
412 strcat(fullname, "/");
413 strcat(fullname, entry->d_name);
415 strcpy(fullname2, destdir);
416 strcat(fullname2, "/");
417 strcat(fullname2, entry->d_name);
418 // diag_printf("from %s to %s\n", fullname, fullname2);
419 copyfile(fullname, fullname2);
421 // diag_printf("\n");
424 err = closedir(dirp);
425 if( err < 0 ) SHOW_RESULT( stat, err );
431 static int
432 zylinjtag_Jim_Command_rm(Jim_Interp *interp,
433 int argc,
434 Jim_Obj * const *argv)
436 int del;
437 if (argc != 2)
439 Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
440 return JIM_ERR;
443 del = 0;
444 if (unlink(Jim_GetString(argv[1], NULL)) == 0)
445 del = 1;
446 if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
447 del = 1;
449 return del ? JIM_OK : JIM_ERR;
453 static int
454 zylinjtag_Jim_Command_ls(Jim_Interp *interp,
455 int argc,
456 Jim_Obj * const *argv)
458 if (argc != 2)
460 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
461 return JIM_ERR;
464 char *name = (char*) Jim_GetString(argv[1], NULL);
466 DIR *dirp = NULL;
467 dirp = opendir(name);
468 if (dirp == NULL)
470 return JIM_ERR;
472 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
474 for (;;)
476 struct dirent *entry = NULL;
477 entry = readdir(dirp);
478 if (entry == NULL)
479 break;
481 if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
482 continue;
484 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
486 closedir(dirp);
488 Jim_SetResult(interp, objPtr);
490 return JIM_OK;
493 int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
495 if (argc != 1)
497 return ERROR_COMMAND_SYNTAX_ERROR;
499 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
500 int value=*address;
501 command_print(cmd_ctx, "0x%x : 0x%x", address, value);
502 return ERROR_OK;
505 int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
507 if (argc != 2)
509 return ERROR_INVALID_ARGUMENTS;
511 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
512 int value=strtoul(args[1], NULL, 0);
513 *address=value;
514 return ERROR_OK;
517 static int
518 zylinjtag_Jim_Command_peek(Jim_Interp *interp,
519 int argc,
520 Jim_Obj * const *argv)
522 if (argc != 2)
524 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
525 return JIM_ERR;
528 long address;
529 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
530 return JIM_ERR;
532 int value = *((volatile int *) address);
534 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
536 return JIM_OK;
539 static int
540 zylinjtag_Jim_Command_poke(Jim_Interp *interp,
541 int argc,
542 Jim_Obj * const *argv)
544 if (argc != 3)
546 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
547 return JIM_ERR;
550 long address;
551 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
552 return JIM_ERR;
553 long value;
554 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
555 return JIM_ERR;
557 *((volatile int *) address) = value;
559 return JIM_OK;
563 /* not so pretty code to fish out ip number*/
564 static int zylinjtag_Jim_Command_ip(Jim_Interp *interp, int argc,
565 Jim_Obj * const *argv)
567 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
569 struct ifaddrs *ifa = NULL, *ifp = NULL;
571 if (getifaddrs(&ifp) < 0)
573 return JIM_ERR;
576 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
578 char ip[200];
579 socklen_t salen;
581 if (ifa->ifa_addr->sa_family == AF_INET)
582 salen = sizeof(struct sockaddr_in);
583 else if (ifa->ifa_addr->sa_family == AF_INET6)
584 salen = sizeof(struct sockaddr_in6);
585 else
586 continue;
588 if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
589 NI_NUMERICHOST) < 0)
591 continue;
594 Jim_AppendString(interp, tclOutput, ip, strlen(ip));
595 break;
599 freeifaddrs(ifp);
601 Jim_SetResult(interp, tclOutput);
603 return JIM_OK;
607 /* not so pretty code to fish out eth0 mac address */
608 static int zylinjtag_Jim_Command_mac(Jim_Interp *interp, int argc,
609 Jim_Obj * const *argv)
613 struct ifreq *ifr, *ifend;
614 struct ifreq ifreq;
615 struct ifconf ifc;
616 struct ifreq ifs[5];
617 int SockFD;
619 SockFD = socket(AF_INET, SOCK_DGRAM, 0);
620 if (SockFD < 0)
622 return JIM_ERR;
625 ifc.ifc_len = sizeof(ifs);
626 ifc.ifc_req = ifs;
627 if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0)
629 close(SockFD);
630 return JIM_ERR;
633 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
634 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
636 //if (ifr->ifr_addr.sa_family == AF_INET)
638 if (strcmp("eth0", ifr->ifr_name)!=0)
639 continue;
640 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
641 if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0)
643 close(SockFD);
644 return JIM_ERR;
647 close(SockFD);
650 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
652 char buffer[256];
653 sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
654 ifreq.ifr_hwaddr.sa_data[0]&0xff,
655 ifreq.ifr_hwaddr.sa_data[1]&0xff,
656 ifreq.ifr_hwaddr.sa_data[2]&0xff,
657 ifreq.ifr_hwaddr.sa_data[3]&0xff,
658 ifreq.ifr_hwaddr.sa_data[4]&0xff,
659 ifreq.ifr_hwaddr.sa_data[5]&0xff);
661 Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
663 Jim_SetResult(interp, tclOutput);
665 return JIM_OK;
668 close(SockFD);
670 return JIM_ERR;
676 int ioutil_init(struct command_context_s *cmd_ctx)
678 register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
679 "remove file");
681 register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
682 "display file content");
684 register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
685 "truncate a file to 0 size");
687 register_command(cmd_ctx, NULL, "cp", handle_cp_command,
688 COMMAND_ANY, "copy a file <from> <to>");
690 register_command(cmd_ctx, NULL, "append_file", handle_append_command,
691 COMMAND_ANY, "append a variable number of strings to a file");
693 register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
694 COMMAND_ANY, "display available ram memory");
696 Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
698 Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
699 Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
700 Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
702 Jim_CreateCommand(interp, "mac", zylinjtag_Jim_Command_mac,
703 NULL, NULL);
705 Jim_CreateCommand(interp, "ip", zylinjtag_Jim_Command_ip,
706 NULL, NULL);
708 return ERROR_OK;