1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
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. *
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. *
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 */
27 #include "time_support.h"
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_STAT_H
57 /* loads a file and returns a pointer to it in memory. The file contains
58 * a 0 byte(sentinel) after len bytes - the length of the file. */
59 int loadFile(const char *fileName
, void **data
, size_t *len
)
61 // ensure returned length is always sane
65 pFile
= fopen(fileName
,"rb");
68 LOG_ERROR("Can't open %s\n", fileName
);
71 if (fseek(pFile
, 0, SEEK_END
) != 0)
73 LOG_ERROR("Can't open %s\n", fileName
);
77 long fsize
= ftell(pFile
);
80 LOG_ERROR("Can't open %s\n", fileName
);
86 if (fseek(pFile
, 0, SEEK_SET
) != 0)
88 LOG_ERROR("Can't open %s\n", fileName
);
92 *data
= malloc(*len
+ 1);
95 LOG_ERROR("Can't open %s\n", fileName
);
100 if (fread(*data
, 1, *len
, pFile
)!=*len
)
104 LOG_ERROR("Can't open %s\n", fileName
);
109 // 0-byte after buffer (not included in *len) serves as a sentinel
110 char *buf
= (char *)*data
;
116 COMMAND_HANDLER(handle_cat_command
)
120 command_print(CMD_CTX
, "cat <filename>");
121 return ERROR_INVALID_ARGUMENTS
;
124 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
128 int retval
= loadFile(CMD_ARGV
[0], &data
, &len
);
129 if (retval
== ERROR_OK
)
131 command_print(CMD_CTX
, "%s", (char *)data
);
136 command_print(CMD_CTX
, "%s not found %d", CMD_ARGV
[0], retval
);
142 COMMAND_HANDLER(handle_trunc_command
)
146 command_print(CMD_CTX
, "trunc <filename>");
147 return ERROR_INVALID_ARGUMENTS
;
150 FILE *config_file
= NULL
;
151 config_file
= fopen(CMD_ARGV
[0], "w");
152 if (config_file
!= NULL
)
158 COMMAND_HANDLER(handle_meminfo_command
)
161 struct mallinfo info
;
165 command_print(CMD_CTX
, "meminfo");
166 return ERROR_INVALID_ARGUMENTS
;
173 command_print(CMD_CTX
, "Diff: %d", prev
- info
.fordblks
);
175 prev
= info
.fordblks
;
177 command_print(CMD_CTX
, "Available ram: %d", info
.fordblks
);
183 COMMAND_HANDLER(handle_append_command
)
187 command_print(CMD_CTX
,
188 "append <filename> [<string1>, [<string2>, ...]]");
189 return ERROR_INVALID_ARGUMENTS
;
192 int retval
= ERROR_FAIL
;
193 FILE *config_file
= NULL
;
194 config_file
= fopen(CMD_ARGV
[0], "a");
195 if (config_file
!= NULL
)
197 fseek(config_file
, 0, SEEK_END
);
200 for (i
= 1; i
< CMD_ARGC
; i
++)
202 if (fwrite(CMD_ARGV
[i
], 1, strlen(CMD_ARGV
[i
]), config_file
) != strlen(CMD_ARGV
[i
]))
204 if (i
!= CMD_ARGC
- 1)
206 if (fwrite(" ", 1, 1, config_file
) != 1)
210 if ((i
== CMD_ARGC
) && (fwrite("\n", 1, 1, config_file
) == 1))
222 COMMAND_HANDLER(handle_cp_command
)
226 return ERROR_INVALID_ARGUMENTS
;
229 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
233 int retval
= loadFile(CMD_ARGV
[0], &data
, &len
);
234 if (retval
!= ERROR_OK
)
237 FILE *f
= fopen(CMD_ARGV
[1], "wb");
239 retval
= ERROR_INVALID_ARGUMENTS
;
244 size_t chunk
= len
- pos
;
245 static const size_t maxChunk
= 512 * 1024; // ~1/sec
246 if (chunk
> maxChunk
)
251 if ((retval
== ERROR_OK
) && (fwrite(((char *)data
) + pos
, 1, chunk
, f
) != chunk
))
252 retval
= ERROR_INVALID_ARGUMENTS
;
254 if (retval
!= ERROR_OK
)
259 command_print(CMD_CTX
, "%zu", len
- pos
);
267 if (retval
== ERROR_OK
)
269 command_print(CMD_CTX
, "Copied %s to %s", CMD_ARGV
[0], CMD_ARGV
[1]);
272 command_print(CMD_CTX
, "Failed: %d", retval
);
280 if (retval
!= ERROR_OK
)
289 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
292 void copyfile(char *name2
, char *name1
)
300 fd1
= open(name1
, O_WRONLY
| O_CREAT
, 0664);
302 SHOW_RESULT(open
, fd1
);
304 fd2
= open(name2
, O_RDONLY
);
306 SHOW_RESULT(open
, fd2
);
310 done
= read(fd2
, buf
, IOSIZE
);
313 SHOW_RESULT(read
, done
);
317 if (done
== 0) break;
319 wrote
= write(fd1
, buf
, done
);
320 if (wrote
!= done
) SHOW_RESULT(write
, wrote
);
322 if (wrote
!= done
) break;
326 if (err
< 0) SHOW_RESULT(close
, err
);
329 if (err
< 0) SHOW_RESULT(close
, err
);
333 /* utility fn to copy a directory */
334 void copydir(char *name
, char *destdir
)
339 dirp
= opendir(destdir
);
342 mkdir(destdir
, 0777);
345 err
= closedir(dirp
);
348 dirp
= opendir(name
);
349 if (dirp
== NULL
) SHOW_RESULT(opendir
, -1);
353 struct dirent
*entry
= readdir(dirp
);
358 if (strcmp(entry
->d_name
, ".") == 0)
360 if (strcmp(entry
->d_name
, "..") == 0)
365 char fullPath
[PATH_MAX
];
366 strncpy(fullPath
, name
, PATH_MAX
);
367 strcat(fullPath
, "/");
368 strncat(fullPath
, entry
->d_name
, PATH_MAX
- strlen(fullPath
));
370 if (stat(fullPath
, &buf
) == -1)
372 LOG_ERROR("unable to read status from %s", fullPath
);
375 isDir
= S_ISDIR(buf
.st_mode
) != 0;
380 // diag_printf("<INFO>: entry %14s",entry->d_name);
381 char fullname
[PATH_MAX
];
382 char fullname2
[PATH_MAX
];
384 strcpy(fullname
, name
);
385 strcat(fullname
, "/");
386 strcat(fullname
, entry
->d_name
);
388 strcpy(fullname2
, destdir
);
389 strcat(fullname2
, "/");
390 strcat(fullname2
, entry
->d_name
);
391 // diag_printf("from %s to %s\n", fullname, fullname2);
392 copyfile(fullname
, fullname2
);
394 // diag_printf("\n");
397 err
= closedir(dirp
);
398 if (err
< 0) SHOW_RESULT(stat
, err
);
405 zylinjtag_Jim_Command_rm(Jim_Interp
*interp
,
407 Jim_Obj
* const *argv
)
412 Jim_WrongNumArgs(interp
, 1, argv
, "rm ?dirorfile?");
417 if (unlink(Jim_GetString(argv
[1], NULL
)) == 0)
419 if (rmdir(Jim_GetString(argv
[1], NULL
)) == 0)
422 return del
? JIM_OK
: JIM_ERR
;
427 zylinjtag_Jim_Command_ls(Jim_Interp
*interp
,
429 Jim_Obj
* const *argv
)
433 Jim_WrongNumArgs(interp
, 1, argv
, "ls ?dir?");
437 char *name
= (char*) Jim_GetString(argv
[1], NULL
);
440 dirp
= opendir(name
);
445 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
449 struct dirent
*entry
= NULL
;
450 entry
= readdir(dirp
);
454 if ((strcmp(".", entry
->d_name
) == 0)||(strcmp("..", entry
->d_name
) == 0))
457 Jim_ListAppendElement(interp
, objPtr
, Jim_NewStringObj(interp
, entry
->d_name
, strlen(entry
->d_name
)));
461 Jim_SetResult(interp
, objPtr
);
467 zylinjtag_Jim_Command_peek(Jim_Interp
*interp
,
469 Jim_Obj
* const *argv
)
473 Jim_WrongNumArgs(interp
, 1, argv
, "peek ?address?");
478 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
481 int value
= *((volatile int *) address
);
483 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));
489 zylinjtag_Jim_Command_poke(Jim_Interp
*interp
,
491 Jim_Obj
* const *argv
)
495 Jim_WrongNumArgs(interp
, 1, argv
, "poke ?address? ?value?");
500 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
503 if (Jim_GetLong(interp
, argv
[2], &value
) != JIM_OK
)
506 *((volatile int *) address
) = value
;
512 /* not so pretty code to fish out ip number*/
513 static int zylinjtag_Jim_Command_ip(Jim_Interp
*interp
, int argc
,
514 Jim_Obj
* const *argv
)
516 #if !defined(__CYGWIN__)
517 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
519 struct ifaddrs
*ifa
= NULL
, *ifp
= NULL
;
521 if (getifaddrs(&ifp
) < 0)
526 for (ifa
= ifp
; ifa
; ifa
= ifa
->ifa_next
)
531 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
532 salen
= sizeof(struct sockaddr_in
);
533 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
534 salen
= sizeof(struct sockaddr_in6
);
538 if (getnameinfo(ifa
->ifa_addr
, salen
, ip
, sizeof(ip
), NULL
, 0,
544 Jim_AppendString(interp
, tclOutput
, ip
, strlen(ip
));
551 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "fixme!!!", 0);
552 LOG_ERROR("NOT IMPLEMENTED!!!");
554 Jim_SetResult(interp
, tclOutput
);
559 /* not so pretty code to fish out eth0 mac address */
560 static int zylinjtag_Jim_Command_mac(Jim_Interp
*interp
, int argc
,
561 Jim_Obj
* const *argv
)
565 struct ifreq
*ifr
, *ifend
;
571 SockFD
= socket(AF_INET
, SOCK_DGRAM
, 0);
577 ifc
.ifc_len
= sizeof(ifs
);
579 if (ioctl(SockFD
, SIOCGIFCONF
, &ifc
) < 0)
585 ifend
= ifs
+ (ifc
.ifc_len
/ sizeof(struct ifreq
));
586 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ifr
++)
588 //if (ifr->ifr_addr.sa_family == AF_INET)
590 if (strcmp("eth0", ifr
->ifr_name
) != 0)
592 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
, sizeof(ifreq
.ifr_name
));
593 if (ioctl(SockFD
, SIOCGIFHWADDR
, &ifreq
) < 0)
602 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
605 sprintf(buffer
, "%02x-%02x-%02x-%02x-%02x-%02x",
606 ifreq
.ifr_hwaddr
.sa_data
[0]&0xff,
607 ifreq
.ifr_hwaddr
.sa_data
[1]&0xff,
608 ifreq
.ifr_hwaddr
.sa_data
[2]&0xff,
609 ifreq
.ifr_hwaddr
.sa_data
[3]&0xff,
610 ifreq
.ifr_hwaddr
.sa_data
[4]&0xff,
611 ifreq
.ifr_hwaddr
.sa_data
[5]&0xff);
613 Jim_AppendString(interp
, tclOutput
, buffer
, strlen(buffer
));
615 Jim_SetResult(interp
, tclOutput
);
626 static const struct command_registration ioutil_command_handlers
[] = {
629 .handler
= &handle_cat_command
,
631 .help
= "display file content",
632 .usage
= "<file_name>",
636 .handler
= &handle_trunc_command
,
638 .help
= "truncate a file 0 size",
639 .usage
= "<file_name>",
643 .handler
= &handle_cp_command
,
645 .help
= "copy a file",
646 .usage
= "<src> <dst>",
649 .name
= "append_file",
650 .handler
= &handle_append_command
,
652 .help
= "append a variable number of strings to a file",
653 .usage
= "<file_name> [<string> ...]",
657 .handler
= &handle_meminfo_command
,
659 .help
= "display available ram memory",
665 .jim_handler
= &zylinjtag_Jim_Command_rm
,
666 .help
= "remove a file",
672 .jim_handler
= &zylinjtag_Jim_Command_peek
,
673 .help
= "peek at a memory address",
679 .jim_handler
= &zylinjtag_Jim_Command_poke
,
680 .help
= "poke at a memory address",
681 .usage
= "<addr> <value>",
686 .jim_handler
= &zylinjtag_Jim_Command_ls
,
687 .help
= "show a listing of files",
693 .jim_handler
= &zylinjtag_Jim_Command_mac
,
694 .help
= "show MAC address",
698 .jim_handler
= &zylinjtag_Jim_Command_ip
,
700 .help
= "show IP address",
702 COMMAND_REGISTRATION_DONE
705 int ioutil_init(struct command_context
*cmd_ctx
)
707 return register_commands(cmd_ctx
, NULL
, ioutil_command_handlers
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)