ARMv7-M: make DAP commands verify target is an ARMv7-M
[openocd.git] / src / ecosboard.c
index 47153490b16c411f7b211672f84b150fb1d01769..2e73585d0bedfb7f591118961ef80fd5df767182 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2007-2008 by Øyvind Harboe                              *
+ *   Copyright (C) 2007-2009 by Øyvind Harboe                              *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
 #include "config.h"
 #endif
 
-#include "log.h"
-#include "types.h"
-#include "jtag.h"
-#include "configuration.h"
-#include "xsvf.h"
-#include "target.h"
-#include "flash.h"
-#include "nand.h"
-#include "pld.h"
-
-#include "command.h"
-#include "server.h"
-#include "telnet_server.h"
-#include "gdb_server.h"
-
-#include <time_support.h>
+#include <helper/types.h>
+#include <jtag/jtag.h>
+#include <helper/ioutil.h>
+#include <helper/configuration.h>
+
+#include <server/server.h>
+#include <server/telnet_server.h>
+#include <server/gdb_server.h>
+#include <openocd.h>
+
+#include <helper/time_support.h>
+
 #include <sys/time.h>
-#include <sys/types.h>
-#include <strings.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <cyg/athttpd/handler.h>
 #include <cyg/athttpd/cgi.h>
 #include <cyg/athttpd/forms.h>
-#include <cyg/hal/hal_diag.h>
-#include <cyg/kernel/kapi.h>
-#include <cyg/io/serialio.h>
+#include <cyg/discover/discover.h>
 #include <cyg/io/io.h>
+#include <cyg/io/serialio.h>
 #include <netinet/tcp.h>
+#include <cyg/hal/hal_diag.h>
+
 #include "rom.h"
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <net/if.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <ifaddrs.h>
-#include <string.h>
 
-#include <unistd.h>
-#include <stdio.h>
+#ifdef CYGPKG_HAL_NIOS2
+#include <cyg/hal/io.h>
+#define ZY1000_SER_DEV "/dev/uart_0"
+#else
+#define ZY1000_SER_DEV "/dev/ser0"
+
+#endif
+
+
 #define MAX_IFS 64
 #if defined(CYGPKG_NET_FREEBSD_STACK)
 #include <tftp_support.h>
@@ -91,19 +79,11 @@ struct tftpd_fileops fileops =
        (int (*)(const char *, int))open,
        close,
        (int (*)(int, const void *, int))write,
-       ( int (*)(int, void *, int))read
+       (int (*)(int, void *, int))read
 };
 
 #endif
 
-#define ZYLIN_VERSION "1.45"
-#define ZYLIN_DATE __DATE__
-#define ZYLIN_TIME __TIME__
-/* hmmm....  we can't pick up the right # during build if we've checked this out
- * in Eclipse... arrggghh...*/
-#define ZYLIN_OPENOCD "$Revision$"
-#define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
-#define ZYLIN_CONFIG_DIR "/config/settings"
 
 void diag_write(char *buf, int len)
 {
@@ -117,236 +97,20 @@ void diag_write(char *buf, int len)
 static bool serialLog = true;
 static bool writeLog = true;
 
+char hwaddr[512];
 
-struct FastLoad
-{
-       u32 address;
-       u8 *data;
-       int length;
-
-};
-
-static int fastload_num;
-static struct FastLoad *fastload;
-
-static void free_fastload()
-{
-       if (fastload!=NULL)
-       {
-               int i;
-               for (i=0; i<fastload_num; i++)
-               {
-                       if (fastload[i].data)
-                               free(fastload[i].data);
-               }
-               free(fastload);
-               fastload=NULL;
-       }
-}
-
-
-int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       u8 *buffer;
-       u32 buf_cnt;
-       u32 image_size;
-       u32 min_address=0;
-       u32 max_address=0xffffffff;
-       int i;
-       int retval;
-
-       image_t image;
-
-       duration_t duration;
-       char *duration_text;
-
-       if ((argc < 1)||(argc > 5))
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
-       if (argc >= 2)
-       {
-               image.base_address_set = 1;
-               image.base_address = strtoul(args[1], NULL, 0);
-       }
-       else
-       {
-               image.base_address_set = 0;
-       }
-
-
-       image.start_address_set = 0;
-
-       if (argc>=4)
-       {
-               min_address=strtoul(args[3], NULL, 0);
-       }
-       if (argc>=5)
-       {
-               max_address=strtoul(args[4], NULL, 0)+min_address;
-       }
-
-       if (min_address>max_address)
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       duration_start_measure(&duration);
-
-       if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
-       {
-               return ERROR_OK;
-       }
-
-       image_size = 0x0;
-       retval = ERROR_OK;
-       fastload_num=image.num_sections;
-       fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
-       if (fastload==NULL)
-       {
-               image_close(&image);
-               return ERROR_FAIL;
-       }
-       memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
-       for (i = 0; i < image.num_sections; i++)
-       {
-               buffer = malloc(image.sections[i].size);
-               if (buffer == NULL)
-               {
-                       command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
-                       break;
-               }
-
-               if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
-               {
-                       free(buffer);
-                       break;
-               }
-
-               u32 offset=0;
-               u32 length=buf_cnt;
-
-
-               /* DANGER!!! beware of unsigned comparision here!!! */
-
-               if ((image.sections[i].base_address+buf_cnt>=min_address)&&
-                               (image.sections[i].base_address<max_address))
-               {
-                       if (image.sections[i].base_address<min_address)
-                       {
-                               /* clip addresses below */
-                               offset+=min_address-image.sections[i].base_address;
-                               length-=offset;
-                       }
-
-                       if (image.sections[i].base_address+buf_cnt>max_address)
-                       {
-                               length-=(image.sections[i].base_address+buf_cnt)-max_address;
-                       }
-
-                       fastload[i].address=image.sections[i].base_address+offset;
-                       fastload[i].data=malloc(length);
-                       if (fastload[i].data==NULL)
-                       {
-                               free(buffer);
-                               break;
-                       }
-                       memcpy(fastload[i].data, buffer+offset, length);
-                       fastload[i].length=length;
-
-                       image_size += length;
-                       command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
-               }
-
-               free(buffer);
-       }
-
-       duration_stop_measure(&duration, &duration_text);
-       if (retval==ERROR_OK)
-       {
-               command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
-               command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
-       }
-       free(duration_text);
-
-       image_close(&image);
-
-       if (retval!=ERROR_OK)
-       {
-               free_fastload();
-       }
-
-       return retval;
-}
-
-int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc>0)
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       if (fastload==NULL)
-       {
-               LOG_ERROR("No image in memory");
-               return ERROR_FAIL;
-       }
-       int i;
-       int ms=timeval_ms();
-       int size=0;
-       for (i=0; i<fastload_num;i++)
-       {
-               int retval;
-               target_t *target = get_current_target(cmd_ctx);
-               if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
-               {
-                       return retval;
-               }
-               size+=fastload[i].length;
-       }
-       int after=timeval_ms();
-       command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
-       return ERROR_OK;
-}
-
-
-/* Give TELNET a way to find out what version this is */
-int handle_zy1000_version_command(struct command_context_s *cmd_ctx, char *cmd,
-               char **args, int argc)
-{
-       if (argc > 1)
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-       if (argc == 0)
-       {
-               command_print(cmd_ctx, ZYLIN_OPENOCD_VERSION);
-       } else if (strcmp("openocd", args[0])==0)
-       {
-               int revision;
-               revision=atol(ZYLIN_OPENOCD+strlen("XRevision: "));
-               command_print(cmd_ctx, "%d", revision);
-       } else if (strcmp("zy1000", args[0])==0)
-       {
-               command_print(cmd_ctx, "%s", ZYLIN_VERSION);
-       } else if (strcmp("date", args[0])==0)
-       {
-               command_print(cmd_ctx, "%s", ZYLIN_DATE);
-       } else
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       return ERROR_OK;
-}
 
-extern flash_driver_t *flash_drivers[];
-extern target_type_t *target_types[];
+extern struct flash_driver *flash_drivers[];
+extern struct target_type *target_types[];
 
 #ifdef CYGPKG_PROFILE_GPROF
 #include <cyg/profile/profile.h>
 
 extern char _stext, _etext; // Defined by the linker
 
+static char *start_of_code=&_stext;
+static char *end_of_code=&_etext;
+
 void start_profile(void)
 {
        // This starts up the system-wide profiling, gathering
@@ -364,410 +128,154 @@ void start_profile(void)
        //       too large, the usefulness of the profile is reduced.
 
        // no more interrupts than 1/10ms.
-       //    profile_on(&_stext, &_etext, 16, 10000); // DRAM
        //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
-       profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
+       //      profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
+       profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
 }
 #endif
 
-// launch GDB server if a config file exists
-bool zylinjtag_parse_config_file(struct command_context_s *cmd_ctx, const char *config_file_name)
-{
-       bool foundFile = false;
-       FILE *config_file = NULL;
-       command_print(cmd_ctx, "executing config file %s", config_file_name);
-       config_file = fopen(config_file_name, "r");
-       if (config_file)
-       {
-               fclose(config_file);
-               int retval;
-               retval = command_run_linef(cmd_ctx, "script %s", config_file_name);
-               if (retval == ERROR_OK)
-               {
-                       foundFile = true;
-               }
-               else
-               {
-                       command_print(cmd_ctx, "Failed executing %s %d", config_file_name, retval);
-               }
-       }
-       else
-       {
-               command_print(cmd_ctx, "No %s found", config_file_name);
-       }
-
-       return foundFile;
-}
-
-extern int eth0_up;
 static FILE *log;
 
 static char reboot_stack[2048];
 
-
-static void
-zylinjtag_reboot(cyg_addrword_t data)
+static void zylinjtag_reboot(cyg_addrword_t data)
 {
        serialLog = true;
-       diag_printf("Rebooting in 100 ticks..\n");
-       cyg_thread_delay(100);
+       diag_printf("Rebooting in 500 ticks..\n");
+       cyg_thread_delay(500);
        diag_printf("Unmounting /config..\n");
        umount("/config");
        diag_printf("Rebooting..\n");
+#ifdef CYGPKG_HAL_NIOS2
+       /* This will reboot & reconfigure the FPGA from the bootloader
+        * and on.
+        */
+       IOWR(REMOTE_UPDATE_BASE, 0x20, 0x1);
+#else
        HAL_PLATFORM_RESET();
+#endif
 }
 static cyg_thread zylinjtag_thread_object;
 static cyg_handle_t zylinjtag_thread_handle;
 
 void reboot(void)
 {
-    cyg_thread_create(1,
-                      zylinjtag_reboot,
-                      (cyg_addrword_t)0,
-                      "reboot Thread",
-                      (void *)reboot_stack,
-                      sizeof(reboot_stack),
-                      &zylinjtag_thread_handle,
-                      &zylinjtag_thread_object);
+       cyg_thread_create(1, zylinjtag_reboot, (cyg_addrword_t) 0, "reboot Thread",
+                       (void *) reboot_stack, sizeof(reboot_stack),
+                       &zylinjtag_thread_handle, &zylinjtag_thread_object);
        cyg_thread_resume(zylinjtag_thread_handle);
 }
 
-int configuration_output_handler(struct command_context_s *context, const char* line)
+static char zylinjtag_reboot_port_stack[2048];
+static cyg_thread zylinjtag_reboot_port_thread_object;
+static cyg_handle_t zylinjtag_reboot_port_thread_handle;
+
+static void zylinjtag_reboot_port_task(cyg_addrword_t data)
 {
-       diag_printf("%s", line);
+       int so_reuseaddr_option = 1;
 
-       return ERROR_OK;
-}
+       int fd;
+       if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+       {
+               LOG_ERROR("error creating socket: %s", strerror(errno));
+               exit(-1);
+       }
 
-int zy1000_configuration_output_handler_log(struct command_context_s *context, const char* line)
-{
-       LOG_USER_N("%s", line);
+       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
+                       sizeof(int));
 
-       return ERROR_OK;
-}
+       struct sockaddr_in sin;
+       unsigned int address_size;
+       address_size = sizeof(sin);
+       memset(&sin, 0, sizeof(sin));
+       sin.sin_family = AF_INET;
+       sin.sin_addr.s_addr = INADDR_ANY;
+       sin.sin_port = htons(1234);
 
-int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
-               char **args, int argc)
-{
-       if (argc != 1)
+       if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
        {
-               command_print(cmd_ctx, "rm <filename>");
-               return ERROR_INVALID_ARGUMENTS;
+               LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
+               exit(-1);
        }
 
-       if (unlink(args[0]) != 0)
+       if (listen(fd, 1) == -1)
        {
-               command_print(cmd_ctx, "failed: %d", errno);
+               LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
+               exit(-1);
        }
+       //      socket_nonblock(fd);
 
-       return ERROR_OK;
-}
-
-int loadFile(const char *fileName, void **data, int *len);
 
-int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
-               char **args, int argc)
-{
-       if (argc != 1)
-       {
-               command_print(cmd_ctx, "cat <filename>");
-               return ERROR_INVALID_ARGUMENTS;
-       }
+       accept(fd, (struct sockaddr *) &sin, &address_size);
 
-       // NOTE!!! we only have line printing capability so we print the entire file as a single line.
-       void *data;
-       int len;
+       diag_printf("Got reboot signal on port 1234");
 
-       int retval = loadFile(args[0], &data, &len);
-       if (retval == ERROR_OK)
-       {
-               command_print(cmd_ctx, "%s", data);
-               free(data);
-       }
-       else
-       {
-               command_print(cmd_ctx, "%s not found %d", args[0], retval);
-       }
+       reboot();
 
-       return ERROR_OK;
 }
-int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
-               char **args, int argc)
+
+void reboot_port(void)
 {
-       if (argc != 1)
-       {
-               command_print(cmd_ctx, "trunc <filename>");
-               return ERROR_INVALID_ARGUMENTS;
-       }
+       cyg_thread_create(1, zylinjtag_reboot_port_task, (cyg_addrword_t) 0, "wait for reboot signal on port 1234",
+                       (void *) zylinjtag_reboot_port_stack, sizeof(zylinjtag_reboot_port_stack),
+                       &zylinjtag_reboot_port_thread_handle, &zylinjtag_reboot_port_thread_object);
+       cyg_thread_resume(zylinjtag_reboot_port_thread_handle);
+}
 
-       FILE *config_file = NULL;
-       config_file = fopen(args[0], "w");
-       if (config_file != NULL)
-               fclose(config_file);
+int configuration_output_handler(struct command_context *context,
+               const char* line)
+{
+       diag_printf("%s", line);
 
        return ERROR_OK;
 }
 
-
-int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+int zy1000_configuration_output_handler_log(struct command_context *context,
+               const char* line)
 {
-       static int prev = 0;
-       struct mallinfo info;
-
-       if (argc != 0)
-       {
-               command_print(cmd_ctx, "meminfo");
-               return ERROR_INVALID_ARGUMENTS;
-       }
-
-       info = mallinfo();
-
-       if (prev > 0)
-       {
-               command_print(cmd_ctx, "Diff:            %d", prev - info.fordblks);
-       }
-       prev = info.fordblks;
-
-       command_print(cmd_ctx, "Available ram:   %d", info.fordblks );
+       LOG_USER_N("%s", line);
 
        return ERROR_OK;
 }
 
-static bool savePower;
+#ifdef CYGPKG_PROFILE_GPROF
+//extern int64_t totaltime;
 
-static void setPower(bool power)
+static int zylinjtag_Jim_Command_profile(Jim_Interp *interp, int argc,
+               Jim_Obj * const *argv)
 {
-       savePower = power;
-       if (power)
+       if ((argc == 2) && (strcmp(Jim_GetString(argv[1], NULL), "stats")==0))
        {
-               HAL_WRITE_UINT32(0x08000014, 0x8);
+//             profile_off();
+               //LOG_USER("Stats %dms sleeping in select()", (int)totaltime);
        } else
        {
-               HAL_WRITE_UINT32(0x08000010, 0x8);
-       }
-}
-
-int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc > 1)
-       {
-               return ERROR_INVALID_ARGUMENTS;
-       }
-
-       if (argc == 1)
-       {
-               if (strcmp(args[0], "on") == 0)
-               {
-                       setPower(1);
-               }
-               else if (strcmp(args[0], "off") == 0)
-               {
-                       setPower(0);
-               } else
-               {
-                       command_print(cmd_ctx, "arg is \"on\" or \"off\"");
-                       return ERROR_INVALID_ARGUMENTS;
-               }
+               LOG_USER("Profiling started");
+               start_profile();
+               //totaltime = 0;
        }
-
-       command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");
-
        return ERROR_OK;
 }
 
-int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
-               char **args, int argc)
-{
-       if (argc < 1)
-       {
-               command_print(cmd_ctx,
-                               "append <filename> [<string1>, [<string2>, ...]]");
-               return ERROR_INVALID_ARGUMENTS;
-       }
-
-       FILE *config_file = NULL;
-       config_file = fopen(args[0], "a");
-       if (config_file != NULL)
-       {
-               int i;
-               fseek(config_file, 0, SEEK_END);
+#endif
 
-               for (i = 1; i < argc; i++)
-               {
-                       fwrite(args[i], strlen(args[i]), 1, config_file);
-                       if (i != argc - 1)
-                       {
-                               fwrite(" ", 1, 1, config_file);
-                       }
-               }
-               fwrite("\n", 1, 1, config_file);
-               fclose(config_file);
-       }
+externC void phi_init_all_network_interfaces(void);
 
-       return ERROR_OK;
-}
+struct command_context *cmd_ctx;
 
-extern int telnet_socket;
+static bool webRunning = false;
 
-int readMore(int fd, void *data, int length)
+void keep_webserver(void)
 {
-       /* used in select() */
-       fd_set read_fds;
-
-       /* monitor sockets for acitvity */
-       int fd_max = 1;
-       FD_ZERO(&read_fds);
-       /* listen for new connections */
-       FD_SET(fd, &read_fds);
-
-       // Maximum 5 seconds.
-       struct timeval tv;
-       tv.tv_sec = 5;
-       tv.tv_usec = 0;
-
-       int retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
-       if (retval == 0)
-       {
-               diag_printf("Timed out waiting for binary payload\n");
-               return -1;
-       }
-       if (retval != 1)
-               return -1;
+       // Target initialisation is only attempted at startup, so we sleep forever and
+       // let the http server bail us out(i.e. get config files set up).
+       diag_printf("OpenOCD has invoked exit().\n"
+               "Use web server to correct any configuration settings and reboot.\n");
+       if (!webRunning)
+               reboot();
 
-       return read_socket(fd, data, length);
-}
-
-int readAll(int fd, void *data, int length)
-{
-       int pos = 0;
-       for (;;)
-       {
-               int actual = readMore(fd, ((char *) data) + pos, length - pos);
-               //              diag_printf("Read %d bytes(pos=%d, length=%d)\n", actual, pos, length);
-               if (actual <= 0)
-                       return -1;
-               pos += actual;
-               if (pos == length)
-                       break;
-       }
-       return length;
-}
-
-int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       cyg_uint32 value;
-       if (argc != 1)
-       {
-               return ERROR_INVALID_ARGUMENTS;
-       }
-       HAL_READ_UINT32(strtoul(args[0], NULL, 0), value);
-       command_print(cmd_ctx, "0x%x : 0x%x", strtoul(args[0], NULL, 0), value);
-       return ERROR_OK;
-}
-
-int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc != 2)
-       {
-               return ERROR_INVALID_ARGUMENTS;
-       }
-       HAL_WRITE_UINT32(strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0));
-       return ERROR_OK;
-}
-
-int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc != 2)
-       {
-               return ERROR_INVALID_ARGUMENTS;
-       }
-
-       // NOTE!!! we only have line printing capability so we print the entire file as a single line.
-       void *data;
-       int len;
-
-       int retval = loadFile(args[0], &data, &len);
-       if (retval != ERROR_OK)
-               return retval;
-
-       FILE *f = fopen(args[1], "wb");
-       if (f == NULL)
-               retval = ERROR_INVALID_ARGUMENTS;
-
-       int pos = 0;
-       for (;;)
-       {
-               int chunk = len - pos;
-               static const int maxChunk = 512 * 1024; // ~1/sec
-               if (chunk > maxChunk)
-               {
-                       chunk = maxChunk;
-               }
-
-               if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
-                       retval = ERROR_INVALID_ARGUMENTS;
-
-               if (retval != ERROR_OK)
-               {
-                       break;
-               }
-
-               command_print(cmd_ctx, "%d", len - pos);
-
-               pos += chunk;
-
-               if (pos == len)
-                       break;
-       }
-
-       if (retval == ERROR_OK)
-       {
-               command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
-       } else
-       {
-               command_print(cmd_ctx, "Failed: %d", retval);
-       }
-
-       if (data != NULL)
-               free(data);
-       if (f != NULL)
-               fclose(f);
-
-       if (retval != ERROR_OK)
-               unlink(args[1]);
-
-       return retval;
-}
-
-#ifdef CYGPKG_PROFILE_GPROF
-extern void start_profile();
-
-int eCosBoard_handle_eCosBoard_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       command_print(cmd_ctx, "Profiling started");
-       start_profile();
-       return ERROR_OK;
-}
-
-#endif
-
-externC void phi_init_all_network_interfaces();
-
-command_context_t *cmd_ctx;
-
-static bool webRunning = false;
-
-void keep_webserver()
-{
-       // Target initialisation is only attempted at startup, so we sleep forever and
-       // let the http server bail us out(i.e. get config files set up).
-       diag_printf("OpenOCD has invoked exit().\n"
-               "Use web server to correct any configuration settings and reboot.\n");
-       if (!webRunning)
-               reboot();
-
-       // exit() will terminate the current thread and we we'll then sleep eternally or
-       // we'll have a reboot scheduled.
+       // exit() will terminate the current thread and we we'll then sleep eternally or
+       // we'll have a reboot scheduled.
 }
 
 extern void printDccChar(char c);
@@ -794,156 +302,89 @@ void _zylinjtag_diag_write_char(char c, void **param)
                HAL_DIAG_WRITE_CHAR(c);
        }
 
+#ifdef CYGPKG_HAL_ZYLIN_PHI
        printDccChar(c);
+#endif
 }
 
-#define SHOW_RESULT(a, b) diag_printf(#a " failed %d\n", (int)b)
-
-#define IOSIZE 512
-static void copyfile(char *name2, char *name1)
-{
-
-       int err;
-       char buf[IOSIZE];
-       int fd1, fd2;
-       ssize_t done, wrote;
+void copyfile(char *name2, char *name1);
 
-       fd1 = open(name1, O_WRONLY | O_CREAT);
-       if (fd1 < 0)
-               SHOW_RESULT( open, fd1 );
+void copydir(char *name, char *destdir);
 
-       fd2 = open(name2, O_RDONLY);
-       if (fd2 < 0)
-               SHOW_RESULT( open, fd2 );
-
-       for (;;)
-       {
-               done = read(fd2, buf, IOSIZE );
-               if (done < 0)
-               {
-                       SHOW_RESULT( read, done );
-                       break;
-               }
-
-        if( done == 0 ) break;
-
-               wrote = write(fd1, buf, done);
-        if( wrote != done ) SHOW_RESULT( write, wrote );
-
-        if( wrote != done ) break;
-       }
-
-       err = close(fd1);
-    if( err < 0 ) SHOW_RESULT( close, err );
+#if 0
+MTAB_ENTRY(romfs_mte1,
+               "/rom",
+               "romfs",
+               "",
+               (CYG_ADDRWORD) &filedata[0]);
+#endif
 
-       err = close(fd2);
-    if( err < 0 ) SHOW_RESULT( close, err );
+void openocd_sleep_prelude(void)
+{
+       cyg_mutex_unlock(&httpstate.jim_lock);
+}
 
+void openocd_sleep_postlude(void)
+{
+       cyg_mutex_lock(&httpstate.jim_lock);
 }
-static void copydir(char *name, char *destdir)
+
+void format(void)
 {
-       int err;
-       DIR *dirp;
+#ifdef CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1
+       diag_printf("Formatting JFFS2...\n");
 
-       dirp = opendir(destdir);
-       if (dirp==NULL)
-       {
-               mkdir(destdir, 0777);
-       } else
+       cyg_io_handle_t handle;
+
+       Cyg_ErrNo err;
+       err = cyg_io_lookup(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, &handle);
+       if (err != ENOERR)
        {
-               err = closedir(dirp);
+               diag_printf("Flash Error cyg_io_lookup: %d\n", err);
+               reboot();
        }
 
-       dirp = opendir(name);
-    if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
-
-       for (;;)
+       cyg_uint32 len;
+       cyg_io_flash_getconfig_devsize_t ds;
+       len = sizeof(ds);
+       err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
+       if (err != ENOERR)
        {
-               struct dirent *entry = readdir(dirp);
-
-               if (entry == NULL)
-                       break;
-
-               if (strcmp(entry->d_name, ".") == 0)
-                       continue;
-               if (strcmp(entry->d_name, "..") == 0)
-                       continue;
-
-               bool isDir = false;
-               struct stat buf;
-               char fullPath[PATH_MAX];
-               strncpy(fullPath, name, PATH_MAX);
-               strcat(fullPath, "/");
-               strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
-
-               if (stat(fullPath, &buf) == -1)
-               {
-                       diag_printf("unable to read status from %s", fullPath);
-                       break;
-               }
-               isDir = S_ISDIR(buf.st_mode) != 0;
-
-               if (isDir)
-                       continue;
-
-               //        diag_printf("<INFO>: entry %14s",entry->d_name);
-               char fullname[PATH_MAX];
-               char fullname2[PATH_MAX];
+               diag_printf("Flash error cyg_io_get_config %d\n", err);
+               reboot();
+       }
 
-               strcpy(fullname, name);
-               strcat(fullname, "/");
-               strcat(fullname, entry->d_name);
+       cyg_io_flash_getconfig_erase_t e;
+       len = sizeof(e);
 
-               strcpy(fullname2, destdir);
-               strcat(fullname2, "/");
-               strcat(fullname2, entry->d_name);
-               //        diag_printf("from %s to %s\n", fullname, fullname2);
-               copyfile(fullname, fullname2);
+       e.offset = 0;
+       e.len = ds.dev_size;
 
-               //       diag_printf("\n");
+       diag_printf("Formatting 0x%08x bytes\n", (int)ds.dev_size);
+       err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE, &e, &len);
+       if (err != ENOERR)
+       {
+               diag_printf("Flash erase error %d offset 0x%08x\n", err, e.err_address);
+               reboot();
        }
 
-       err = closedir(dirp);
-    if( err < 0 ) SHOW_RESULT( stat, err );
-}
-
-#if 0
-MTAB_ENTRY( romfs_mte1,
-               "/rom",
-               "romfs",
-               "",
-               (CYG_ADDRWORD) &filedata[0] );
+       diag_printf("Flash formatted successfully\n");
 #endif
 
-void openocd_sleep_prelude()
-{
-       cyg_mutex_unlock(&httpstate.jim_lock);
-}
-
-void openocd_sleep_postlude()
-{
-       cyg_mutex_lock(&httpstate.jim_lock);
+       reboot();
 }
 
-static int
-zylinjtag_Jim_Command_rm(Jim_Interp *interp,
-                                   int argc,
+static int zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp, int argc,
                Jim_Obj * const *argv)
 {
-       int del;
-       if (argc != 2)
+       if (argc != 1)
        {
-               Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
                return JIM_ERR;
        }
 
-       del = 0;
-       if (unlink(Jim_GetString(argv[1], NULL)) == 0)
-               del = 1;
-       if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
-               del = 1;
-
-       return del ? JIM_OK : JIM_ERR;
+       format();
+       for (;;)
+               ;
 }
 
 static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
@@ -992,308 +433,51 @@ static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
                        case 0x10:
                                state_string = "EXIT";
                                break;
-                       default:
-                               state_string = "????";
-                               break;
-                       }
-
-               Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
-                               state_string, strlen(state_string)));
-
-               Jim_ListAppendElement   (interp, threadObj, Jim_NewIntObj(interp, id));
-               Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.set_pri));
-               Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.cur_pri));
-
-               Jim_ListAppendElement(interp, threads, threadObj);
-       }
-       Jim_SetResult( interp, threads);
-
-       return JIM_OK;
-}
-
-
-static int
-zylinjtag_Jim_Command_ls(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       if (argc != 2)
-       {
-               Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
-               return JIM_ERR;
-       }
-
-       char *name = (char*) Jim_GetString(argv[1], NULL);
-
-       DIR *dirp = NULL;
-       dirp = opendir(name);
-       if (dirp == NULL)
-       {
-               return JIM_ERR;
-       }
-       Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
-
-       for (;;)
-       {
-               struct dirent *entry = NULL;
-               entry = readdir(dirp);
-               if (entry == NULL)
-                       break;
-
-               if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
-                       continue;
-
-        Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
-       }
-       closedir(dirp);
-
-       Jim_SetResult(interp, objPtr);
-
-       return JIM_OK;
-}
-
-
-static int
-zylinjtag_Jim_Command_getmem(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       if (argc != 3)
-       {
-               Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
-               return JIM_ERR;
-       }
-
-       long address;
-       long length;
-       if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
-               return JIM_ERR;
-       if (Jim_GetLong(interp, argv[2], &length) != JIM_OK)
-               return JIM_ERR;
-
-       if (length < 0 && length > (4096 * 1024))
-       {
-               Jim_WrongNumArgs(interp, 1, argv, "getmem ?dir?");
-               return JIM_ERR;
-       }
-
-       void *mem = malloc(length);
-       if (mem == NULL)
-               return JIM_ERR;
-
-       target_t *target = get_current_target(cmd_ctx);
-
-       int retval;
-       int size = 1;
-       int count = length;
-       if ((address % 4 == 0) && (count % 4 == 0))
-       {
-               size = 4;
-               count /= 4;
-       }
-
-       if ((retval  = target->type->read_memory(target, address, size, count, mem)) != ERROR_OK)
-       {
-               free(mem);
-               return JIM_ERR;
-       }
-
-       Jim_Obj *objPtr = Jim_NewStringObj(interp, mem, length);
-       Jim_SetResult(interp, objPtr);
-
-       free(mem);
-
-       return JIM_OK;
-}
-
-static int
-zylinjtag_Jim_Command_peek(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       if (argc != 2)
-       {
-               Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
-               return JIM_ERR;
-       }
-
-       long address;
-       if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
-               return JIM_ERR;
-
-       int value = *((volatile int *) address);
-
-       Jim_SetResult(interp, Jim_NewIntObj(interp, value));
-
-       return JIM_OK;
-}
-
-static int
-zylinjtag_Jim_Command_poke(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       if (argc != 3)
-       {
-               Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
-               return JIM_ERR;
-       }
-
-       long address;
-       if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
-               return JIM_ERR;
-       long value;
-       if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
-               return JIM_ERR;
-
-       *((volatile int *) address) = value;
-
-       return JIM_OK;
-}
-
-
-
-static int
-zylinjtag_Jim_Command_flash(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       int retval;
-       u32 base = 0;
-       flash_bank_t *t = get_flash_bank_by_num_noprobe(0);
-       if (t != NULL)
-       {
-               base = t->base;
-               retval = JIM_OK;
-    } else
-       {
-               retval = JIM_ERR;
-       }
-
-       if (retval == JIM_OK)
-       {
-               Jim_SetResult(interp, Jim_NewIntObj(interp, base));
-       }
-
-       return retval;
-}
-
-
-
-
-
-static int
-zylinjtag_Jim_Command_log(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
-
-       if (logCount >= logSize)
-       {
-       Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer+logCount%logSize, logSize-logCount%logSize);
-       }
-       Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
-
-       Jim_SetResult(interp, tclOutput);
-       return JIM_OK;
-}
-
-static int
-zylinjtag_Jim_Command_reboot(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       reboot();
-       return JIM_OK;
-}
-
-static int
-zylinjtag_Jim_Command_mac(Jim_Interp *interp,
-                                   int argc,
-               Jim_Obj * const *argv)
-{
-       int s;
-       struct ifreq ifr;
-       s = socket(AF_INET, SOCK_DGRAM, 0);
-       if (s >= 0)
-       {
-               strcpy(ifr.ifr_name, "eth0");
-               int res;
-               res = ioctl(s, SIOCGIFHWADDR, &ifr);
-               close(s);
-
-               if (res < 0)
-               {
-                       return JIM_OK;
-               }
-       }
-
-       Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
+                       default:
+                               state_string = "????";
+                               break;
+                       }
 
-       char hwaddr[512];
-       sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
-                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
+               Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
+                               state_string, strlen(state_string)));
 
-       Jim_AppendString(httpstate.jim_interp, tclOutput, hwaddr, strlen(hwaddr));
+               Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, id));
+               Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
+                               info.set_pri));
+               Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
+                               info.cur_pri));
 
-       Jim_SetResult(interp, tclOutput);
+               Jim_ListAppendElement(interp, threads, threadObj);
+       }
+       Jim_SetResult(interp, threads);
 
        return JIM_OK;
 }
 
-static int
-zylinjtag_Jim_Command_ip(Jim_Interp *interp,
-                                   int argc,
+static int zylinjtag_Jim_Command_log(Jim_Interp *interp, int argc,
                Jim_Obj * const *argv)
 {
        Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
 
-       struct ifaddrs *ifa = NULL, *ifp = NULL;
-
-       if (getifaddrs(&ifp) < 0)
-       {
-               return JIM_ERR;
-       }
-
-       for (ifa = ifp; ifa; ifa = ifa->ifa_next)
+       if (logCount >= logSize)
        {
-               char ip[200];
-               socklen_t salen;
-
-               if (ifa->ifa_addr->sa_family == AF_INET)
-                       salen = sizeof(struct sockaddr_in);
-               else if (ifa->ifa_addr->sa_family == AF_INET6)
-                       salen = sizeof(struct sockaddr_in6);
-               else
-                       continue;
-
-               if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
-                               NI_NUMERICHOST) < 0)
-               {
-                       continue;
-               }
-
-               Jim_AppendString(httpstate.jim_interp, tclOutput, ip, strlen(ip));
-               break;
-
+               Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer + logCount
+                               % logSize, logSize - logCount % logSize);
        }
-
-       freeifaddrs(ifp);
+       Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
 
        Jim_SetResult(interp, tclOutput);
-
        return JIM_OK;
 }
 
-extern Jim_Interp *interp;
+static int zylinjtag_Jim_Command_reboot(Jim_Interp *interp, int argc,
+               Jim_Obj * const *argv)
+{
+       reboot();
+       return JIM_OK;
+}
 
-static void zylinjtag_startNetwork()
+static void zylinjtag_startNetwork(void)
 {
        // Bring TCP/IP up immediately before we're ready to accept commands.
        //
@@ -1308,6 +492,10 @@ static void zylinjtag_startNetwork()
                diag_printf("Network not up and running\n");
                exit(-1);
        }
+
+       /* very first thing we want is a reboot capability */
+       reboot_port();
+
 #if defined(CYGPKG_NET_FREEBSD_STACK)
        /*start TFTP*/
        tftpd_start(69, &fileops);
@@ -1315,134 +503,74 @@ static void zylinjtag_startNetwork()
 
        cyg_httpd_init_tcl_interpreter();
 
-       interp = httpstate.jim_interp;
-
-    Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "reboot", zylinjtag_Jim_Command_reboot, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "zy1000_flash", zylinjtag_Jim_Command_flash, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "threads", zylinjtag_Jim_Command_threads, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "getmem", zylinjtag_Jim_Command_getmem, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "mac", zylinjtag_Jim_Command_mac, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "ip", zylinjtag_Jim_Command_ip, NULL, NULL);
-    Jim_CreateCommand(httpstate.jim_interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
+       Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log,
+                       NULL, NULL);
+       Jim_CreateCommand(httpstate.jim_interp, "zy1000_reboot",
+                       zylinjtag_Jim_Command_reboot, NULL, NULL);
+       Jim_CreateCommand(httpstate.jim_interp, "threads",
+                       zylinjtag_Jim_Command_threads, NULL, NULL);
+       Jim_CreateCommand(httpstate.jim_interp, "format_jffs2",
+                       zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
 
        cyg_httpd_start();
 
        webRunning = true;
 
        diag_printf("Web server running\n");
-}
-
-static bool readPowerDropout()
-{
-       cyg_uint32 state;
-       // sample and clear power dropout
-       HAL_WRITE_UINT32(0x08000010, 0x80);
-       HAL_READ_UINT32(0x08000010, state);
-       bool powerDropout;
-       powerDropout = (state & 0x80) != 0;
-       return powerDropout;
-}
-
-bool readSRST()
-{
-       cyg_uint32 state;
-       // sample and clear SRST sensing
-       HAL_WRITE_UINT32(0x08000010, 0x00000040);
-       HAL_READ_UINT32(0x08000010, state);
-       bool srstAsserted;
-       srstAsserted = (state & 0x40) != 0;
-       return srstAsserted;
-}
-
-// every 300ms we check for reset & powerdropout and issue a "reset halt" if
-// so.
-
-
-static int sense_handler(void *priv)
-{
-       struct command_context_s *cmd_ctx;
-       cmd_ctx = (struct command_context_s *) priv;
-
-       static bool prevSrstAsserted = false;
-       static bool prevPowerdropout = false;
-
-       bool powerDropout;
-       powerDropout = readPowerDropout();
-
-       bool powerRestored;
-       powerRestored = prevPowerdropout && !powerDropout;
-       if (powerRestored)
-       {
-               LOG_USER("Sensed power restore.");
-       }
-
-       cyg_tick_count_t current = cyg_current_time();
-       static cyg_tick_count_t lastPower = 0;
-       bool waitMore = lastPower + 200 > current;
-       if (powerDropout && !waitMore)
-       {
-               LOG_USER("Sensed power dropout.");
-               lastPower = current;
-       }
-
-       bool srstAsserted = readSRST();
-
-       bool srstDeasserted;
-       srstDeasserted = prevSrstAsserted && !srstAsserted;
 
-       static cyg_tick_count_t lastSrst = 0;
-       waitMore = lastSrst + 200 > current;
-       if (srstDeasserted && !waitMore)
+       int s;
+       struct ifreq ifr;
+       s = socket(AF_INET, SOCK_DGRAM, 0);
+       if (s >= 0)
        {
-               LOG_USER("Sensed nSRST deasserted");
-               lastSrst = current;
-       }
+               strcpy(ifr.ifr_name, "eth0");
+               int res;
+               res = ioctl(s, SIOCGIFHWADDR, &ifr);
+               close(s);
 
-       if (!prevSrstAsserted && srstAsserted)
-       {
-               LOG_USER("Sensed nSRST asserted");
+               if (res < 0)
+               {
+                       diag_printf("Can't obtain MAC address\n");
+                       reboot();
+               }
        }
 
-       prevSrstAsserted = srstAsserted;
-       prevPowerdropout = powerDropout;
+       sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
+                       (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
 
-       if (srstDeasserted || powerRestored)
-       {
-               /* Other than logging the event we can't do anything here.
-                * Issuing a reset is a particularly bad idea as we might
-                * be inside a reset already.
-                */
-       }
+       discover_message
+                       = alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
 
-       return ERROR_OK;
+       discover_launch();
 }
 
-
-
-static void
-print_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)
+static void print_exception_handler(cyg_addrword_t data, cyg_code_t exception,
+               cyg_addrword_t info)
 {
        writeLog = false;
        serialLog = true;
        char *infoStr = "unknown";
        switch (exception)
        {
+#ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
        case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
-               infoStr = "undefined instruction";
-               break;
+       infoStr = "undefined instruction";
+       break;
        case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
-               infoStr = "software interrupt";
-               break;
+       infoStr = "software interrupt";
+       break;
        case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
-               infoStr = "abort prefetch";
-               break;
+       infoStr = "abort prefetch";
+       break;
        case CYGNUM_HAL_VECTOR_ABORT_DATA:
-               infoStr = "abort data";
-               break;
+       infoStr = "abort data";
+       break;
+#endif
        default:
                break;
        }
@@ -1463,17 +591,16 @@ print_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_
 
 }
 
+#ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
 static void setHandler(cyg_code_t exception)
 {
        cyg_exception_handler_t *old_handler;
        cyg_addrword_t old_data;
 
-       cyg_exception_set_handler(exception,
-       print_exception_handler,
-       0,
-       &old_handler,
-       &old_data);
+       cyg_exception_set_handler(exception, print_exception_handler, 0,
+                       &old_handler, &old_data);
 }
+#endif
 
 static cyg_thread zylinjtag_uart_thread_object;
 static cyg_handle_t zylinjtag_uart_thread_handle;
@@ -1482,8 +609,6 @@ static char uart_stack[4096];
 static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
 static char backwardBuffer[1024];
 
-static cyg_io_handle_t serial_handle;
-
 void setNoDelay(int session, int flag)
 {
 #if 1
@@ -1502,6 +627,9 @@ void setNoDelay(int session, int flag)
 #endif
 }
 
+#define TEST_TCPIP() 0
+
+#if TEST_TCPIP
 struct
 {
        int req;
@@ -1510,9 +638,9 @@ struct
        int actual2;
 } tcpipSent[512 * 1024];
 int cur;
+#endif
 
-static void
-zylinjtag_uart(cyg_addrword_t data)
+static void zylinjtag_uart(cyg_addrword_t data)
 {
        int so_reuseaddr_option = 1;
 
@@ -1523,7 +651,8 @@ zylinjtag_uart(cyg_addrword_t data)
                exit(-1);
        }
 
-       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
+       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
+                       sizeof(int));
 
        struct sockaddr_in sin;
        unsigned int address_size;
@@ -1559,20 +688,24 @@ zylinjtag_uart(cyg_addrword_t data)
                int oldopts = fcntl(session, F_GETFL, 0);
                fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
 
-               int serHandle = open("/dev/ser0", O_RDWR | O_NONBLOCK);
+               int serHandle = open(ZY1000_SER_DEV, O_RDWR | O_NONBLOCK);
                if (serHandle < 0)
                {
                        close(session);
                        continue;
                }
 
+#ifdef CYGPKG_PROFILE_GPROF
                start_profile();
-               int actual = 0;
-               int actual2 = 0;
-               int pos, pos2;
+#endif
+               size_t actual = 0;
+               size_t actual2 = 0;
+               size_t pos, pos2;
                pos = 0;
                pos2 = 0;
+#if TEST_TCPIP
                cur = 0;
+#endif
                for (;;)
                {
                        fd_set write_fds;
@@ -1602,8 +735,11 @@ zylinjtag_uart(cyg_addrword_t data)
                        if (actual2 <= 0)
                        {
                                memset(backwardBuffer, 's', sizeof(backwardBuffer));
-                               actual2=read(serHandle, backwardBuffer, sizeof(backwardBuffer));
-                               if (actual2 < 0)
+                               int t;
+                               t = read(serHandle, backwardBuffer,
+                                               sizeof(backwardBuffer));
+                               actual2 = t;
+                               if (t < 0)
                                {
                                        if (errno != EAGAIN)
                                        {
@@ -1614,8 +750,7 @@ zylinjtag_uart(cyg_addrword_t data)
                                pos2 = 0;
                        }
 
-                       int x = actual2;
-                       int y = 0;
+                       size_t y = 0;
                        if (actual2 > 0)
                        {
                                int written = write(session, backwardBuffer + pos2, actual2);
@@ -1626,7 +761,8 @@ zylinjtag_uart(cyg_addrword_t data)
                                y = written;
                        }
 
-                       if (FD_ISSET(session, &read_fds)&&(sizeof(forwardBuffer)>actual))
+                       if (FD_ISSET(session, &read_fds)
+                                       && (sizeof(forwardBuffer) > actual))
                        {
                                // NB! Here it is important that we empty the TCP/IP read buffer
                                // to make transmission tick right
@@ -1634,7 +770,8 @@ zylinjtag_uart(cyg_addrword_t data)
                                pos = 0;
                                int t;
                                // this will block if there is no data at all
-                               t=read_socket(session, forwardBuffer+actual, sizeof(forwardBuffer)-actual);
+                               t = read_socket(session, forwardBuffer + actual,
+                                               sizeof(forwardBuffer) - actual);
                                if (t <= 0)
                                {
                                        goto closeSession;
@@ -1642,7 +779,6 @@ zylinjtag_uart(cyg_addrword_t data)
                                actual += t;
                        }
 
-                       int x2 = actual;
                        int y2 = 0;
                        if (actual > 0)
                        {
@@ -1660,13 +796,15 @@ zylinjtag_uart(cyg_addrword_t data)
                                        }
                                        // The serial buffer is full
                                        written = 0;
-                               } else
+                               }
+                               else
                                {
                                        actual -= written;
                                        pos += written;
                                }
                                y2 = written;
                        }
+#if TEST_TCPIP
                        if (cur < 1024)
                        {
                                tcpipSent[cur].req = x;
@@ -1675,18 +813,20 @@ zylinjtag_uart(cyg_addrword_t data)
                                tcpipSent[cur].actual2 = y2;
                                cur++;
                        }
-
+#endif
                }
-           closeSession:
-           close(session);
+               closeSession: close(session);
                close(serHandle);
 
+#if TEST_TCPIP
                int i;
                for (i = 0; i < 1024; i++)
                {
-               diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual, tcpipSent[i].req2, tcpipSent[i].actual2);
+                       diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual,
+                                       tcpipSent[i].req2, tcpipSent[i].actual2);
 
                }
+#endif
        }
        close(fd);
 
@@ -1694,31 +834,35 @@ zylinjtag_uart(cyg_addrword_t data)
 
 void startUart(void)
 {
-    cyg_thread_create(1,
-                      zylinjtag_uart,
-                      (cyg_addrword_t)0,
-                      "uart thread",
-                      (void *)uart_stack,
-                      sizeof(uart_stack),
-                      &zylinjtag_uart_thread_handle,
-                      &zylinjtag_uart_thread_object);
+       cyg_thread_create(1, zylinjtag_uart, (cyg_addrword_t) 0, "uart thread",
+                       (void *) uart_stack, sizeof(uart_stack),
+                       &zylinjtag_uart_thread_handle, &zylinjtag_uart_thread_object);
        cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
        cyg_thread_resume(zylinjtag_uart_thread_handle);
 }
 
-
-
-int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+static int zylinjtag_Jim_Command_uart(Jim_Interp *interp, int argc,
+               Jim_Obj * const *argv)
 {
-       if (argc != 1)
+       static int current_baud = 38400;
+       if (argc == 1)
        {
-               command_print(cmd_ctx, "usage: uart <baudrate>");
-               return ERROR_INVALID_ARGUMENTS;
+               command_print(cmd_ctx, "%d", current_baud);
+               return JIM_OK;
+       }
+       else if (argc != 2)
+       {
+               return JIM_ERR;
        }
 
-       int baud = atol(args[0]);
+       long new_baudrate;
+       if (Jim_GetLong(interp, argv[1], &new_baudrate) != JIM_OK)
+               return JIM_ERR;
+
+       current_baud = new_baudrate;
 
-       switch (baud)
+       int baud;
+       switch (current_baud)
        {
        case 9600:
                baud = CYGNUM_SERIAL_BAUD_9600;
@@ -1748,84 +892,76 @@ int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd, char **arg
        //get existing serial configuration
        len = sizeof(cyg_serial_info_t);
        int err;
-       err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
-       err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf, &len);
+       cyg_io_handle_t serial_handle;
+
+       err = cyg_io_lookup(ZY1000_SER_DEV, &serial_handle);
        if (err != ENOERR)
        {
-               command_print(cmd_ctx, "Failed to get serial port settings %d", err);
-               return ERROR_OK;
+               LOG_ERROR("Could not open serial port\n");
+               return JIM_ERR;
+       }
+
+       err = cyg_io_get_config(serial_handle,
+                       CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
+       err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf,
+                       &len);
+       if (err != ENOERR)
+       {
+               LOG_ERROR("Failed to get serial port settings %d", err);
+               return JIM_ERR;
        }
        buf.baud = baud;
 
-       err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf, &len);
+       err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf,
+                       &len);
        if (err != ENOERR)
        {
-               command_print(cmd_ctx, "Failed to set serial port settings %d", err);
-               return ERROR_OK;
+               LOG_ERROR("Failed to set serial port settings %d", err);
+               return JIM_ERR;
        }
 
-       return ERROR_OK;
+       return JIM_OK;
 }
 
 bool logAllToSerial = false;
 
-/* boolean parameter stored on config */
-bool boolParam(char *var)
-{
-       bool result = false;
-       char *name = alloc_printf(ZYLIN_CONFIG_DIR "/%s", var);
-       if (name == NULL)
-               return result;
 
-       void *data;
-       int len;
-       if (loadFile(name, &data, &len) == ERROR_OK)
-       {
-               if (len > 1)
-                       len = 1;
-               result = strncmp((char *) data, "1", len) == 0;
-               free(data);
-       }
-       free(name);
-       return result;
-}
+int boolParam(char *var);
 
-command_context_t *setup_command_handler();
 
-int add_default_dirs(void)
+static const char *zylin_config_dir="/config/settings";
+
+static int add_default_dirs(void)
 {
-       add_script_search_dir(ZYLIN_CONFIG_DIR);
+       add_script_search_dir(zylin_config_dir);
        add_script_search_dir("/rom/lib/openocd");
        add_script_search_dir("/rom");
        return ERROR_OK;
 }
 
-static cyg_uint8 *ramblockdevice;
-static const int ramblockdevice_size=4096*1024;
 int main(int argc, char *argv[])
 {
        /* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
         * need to allocate towards the end of the heap.  */
 
-       ramblockdevice=(cyg_uint8 *)malloc(ramblockdevice_size);
-       memset(ramblockdevice, 0xff, ramblockdevice_size);
-
+#ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
        setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
        setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
        setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
+#endif
 
        int err;
-       err = cyg_io_lookup("/dev/ser0", &serial_handle);
-       if (err != ENOERR)
-       {
-               diag_printf("/dev/ser0 not found\n");
-               reboot();
-       }
-
-       setPower(true); // on by default
 
        atexit(keep_webserver);
 
+       diag_init_putc(_zylinjtag_diag_write_char);
+       // We want this in the log.
+#ifdef CYGPKG_HAL_NIOS2
+       diag_printf("Zylin ZY1000 PCB revc.\n");
+#else
+       diag_printf("Zylin ZY1000 PCB revb.\n");
+#endif
+
        err = mount("", "/ram", "ramfs");
        if (err < 0)
        {
@@ -1860,65 +996,83 @@ int main(int argc, char *argv[])
                exit(-1);
        }
 
-       diag_init_putc(_zylinjtag_diag_write_char);
-
-       // We want this in the log.
-       diag_printf("Zylin ZY1000. Copyright Zylin AS 2007-2008.\n");
-       diag_printf("%s\n", ZYLIN_OPENOCD_VERSION);
 
        copydir("/rom", "/ram/cgi");
 
-       err = mount("/dev/flash1", "/config", "jffs2");
-       if (err < 0)
+#ifdef CYGPKG_HAL_NIOS2
+       cyg_flashaddr_t err_address;
+#define UNCACHED_EXT_FLASH_BASE (0x80000000 + EXT_FLASH_BASE)
+       /* The revc flash is locked upon reset, unlock it */
+#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
+       if ((err = flash_unlock((void *) UNCACHED_EXT_FLASH_BASE, EXT_FLASH_SPAN,
+                       (void **) &err_address)) != 0)
        {
-               diag_printf("unable to mount jffs\n");
-               reboot();
+               diag_printf("Error: could not unlock flash\n");
        }
+#endif
+#endif
 
-       /* are we using a ram disk instead of a flash disk? This is used
-        * for ZY1000 live demo...
-        *
-        * copy over flash disk to ram block device
-        */
-       if (boolParam("ramdisk"))
+
+       err = mount("/dev/flash1", "/config", "jffs2");
+       if (err < 0)
        {
-               diag_printf("Unmounting /config from flash and using ram instead\n");
-               err=umount("/config");
+               diag_printf("unable to mount jffs2, falling back to ram disk..\n");
+               err = mount("", "/config", "ramfs");
                if (err < 0)
                {
-                       diag_printf("unable to unmount jffs\n");
+                       diag_printf("unable to mount /config as ramdisk.\n");
                        reboot();
                }
-
-               err = mount("/dev/flash1", "/config2", "jffs2");
-               if (err < 0)
+       }
+       else
+       {
+               /* are we using a ram disk instead of a flash disk? This is used
+                * for ZY1000 live demo...
+                *
+                * copy over flash disk to ram block device
+                */
+               if (boolParam("ramdisk"))
                {
-                       diag_printf("unable to mount jffs\n");
-                       reboot();
-               }
+                       diag_printf("Unmounting /config from flash and using ram instead\n");
+                       err = umount("/config");
+                       if (err < 0)
+                       {
+                               diag_printf("unable to unmount jffs\n");
+                               reboot();
+                       }
 
-               err = mount("/dev/ram", "/config", "jffs2");
-               if (err < 0)
-               {
-                       diag_printf("unable to mount ram block device\n");
-                       reboot();
-               }
+                       err = mount("/dev/flash1", "/config2", "jffs2");
+                       if (err < 0)
+                       {
+                               diag_printf("unable to mount jffs\n");
+                               reboot();
+                       }
 
-//             copydir("/config2", "/config");
-               copyfile("/config2/ip", "/config/ip");
-               copydir("/config2/settings", "/config/settings");
+                       err = mount("", "/config", "ramfs");
+                       if (err < 0)
+                       {
+                               diag_printf("unable to mount ram block device\n");
+                               reboot();
+                       }
 
-               umount("/config2");
-       } else
-       {
-               /* we're not going to use a ram block disk */
-               free(ramblockdevice);
-       }
+                       //              copydir("/config2", "/config");
+                       copyfile("/config2/ip", "/config/ip");
+                       copydir("/config2/settings", "/config/settings");
 
+                       umount("/config2");
+               }
+       }
 
-       mkdir(ZYLIN_CONFIG_DIR, 0777);
-       mkdir(ZYLIN_CONFIG_DIR "/target", 0777);
-       mkdir(ZYLIN_CONFIG_DIR "/event", 0777);
+       mkdir(zylin_config_dir, 0777);
+       char *dirname = alloc_printf("%s/target", zylin_config_dir);
+       mkdir(dirname, 0777);
+       free(dirname);
+       dirname = alloc_printf("%s/board", zylin_config_dir);
+       mkdir(dirname, 0777);
+       free(dirname);
+       dirname = alloc_printf("%s/event", zylin_config_dir);
+       mkdir(dirname, 0777);
+       free(dirname);
 
        logAllToSerial = boolParam("logserial");
 
@@ -1933,57 +1087,24 @@ int main(int argc, char *argv[])
        add_default_dirs();
 
        /* initialize commandline interface */
-       command_context_t *cmd_ctx;
-       cmd_ctx = setup_command_handler();
+       struct command_context * cmd_ctx;
+       struct command_context *setup_command_handler(Jim_Interp *interp);
+       cmd_ctx = setup_command_handler(httpstate.jim_interp);
        command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
        command_context_mode(cmd_ctx, COMMAND_CONFIG);
 
-
-       register_command(cmd_ctx, NULL, "zy1000_version", handle_zy1000_version_command,
-                       COMMAND_EXEC, "show zy1000 version numbers");
-
-       register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
-                       "remove file");
-
-       register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
-                       "same args as load_image, image stored in memory");
-
-       register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
-                       "loads active fast load image to current target");
-
-       register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
-                       "display file content");
-
-       register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
-                       "truncate a file to 0 size");
-
-       register_command(cmd_ctx, NULL, "append_file", handle_append_command,
-                       COMMAND_ANY, "append a variable number of strings to a file");
-
-       register_command(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
-                       "power <on/off> - turn power switch to target on/off. No arguments - print status.");
-
-       register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
-                       COMMAND_ANY, "display available ram memory");
-
-       register_command(cmd_ctx, NULL, "cp", handle_cp_command,
-                                        COMMAND_ANY, "copy a file <from> <to>");
+       if (ioutil_init(cmd_ctx) != ERROR_OK)
+               return EXIT_FAILURE;
 
 #ifdef CYGPKG_PROFILE_GPROF
-       register_command(cmd_ctx, NULL, "ecosboard_profile", eCosBoard_handle_eCosBoard_profile_command,
-                       COMMAND_ANY, NULL);
+       Jim_CreateCommand(httpstate.jim_interp, "zy1000_profile", zylinjtag_Jim_Command_profile,
+                       NULL, NULL);
 #endif
-       register_command(cmd_ctx, NULL, "uart", handle_uart_command,
-                                        COMMAND_ANY, "uart <baud>  - forward uart on port 5555");
 
+       Jim_CreateCommand(httpstate.jim_interp, "zy1000_uart", zylinjtag_Jim_Command_uart, NULL, NULL);
 
-       int errVal;
-       errVal = log_init(cmd_ctx);
-       if (errVal != ERROR_OK)
-       {
-               diag_printf("log_init() failed %d\n", errVal);
-               exit(-1);
-       }
+
+       log_init();
 
        set_log_output(cmd_ctx, log);
 
@@ -1993,13 +1114,20 @@ int main(int argc, char *argv[])
 
        if (logAllToSerial)
        {
-               diag_printf(ZYLIN_CONFIG_DIR "/logserial=1 => sending log output to serial port using \"debug_level 3\" as default.\n");
+               diag_printf(
+                                "%s/logserial = 1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
                command_run_line(cmd_ctx, "debug_level 3");
        }
 
-       zylinjtag_parse_config_file(cmd_ctx, "/rom/openocd.cfg");
+       command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
 
-       target_register_timer_callback(sense_handler, 200, 1, cmd_ctx);
+       int ret;
+       ret = server_init(cmd_ctx);
+       if (ERROR_OK != ret)
+               return EXIT_FAILURE;
+
+       /* we MUST always run the init command as it will launch telnet sessions */
+       command_run_line(cmd_ctx, "init");
 
        // FIX!!!  Yuk!
        // diag_printf() is really invoked from many more places than we trust it
@@ -2008,7 +1136,8 @@ int main(int argc, char *argv[])
        // Disabling it here is safe and gives us enough logged debug output for now. Crossing
        // fingers that it doesn't cause any crashes.
        diag_printf("Init complete, GDB & telnet servers launched.\n");
-       command_set_output_handler(cmd_ctx, zy1000_configuration_output_handler_log, NULL);
+       command_set_output_handler(cmd_ctx,
+                       zy1000_configuration_output_handler_log, NULL);
        if (!logAllToSerial)
        {
                serialLog = false;
@@ -2026,13 +1155,11 @@ int main(int argc, char *argv[])
        umount("/config");
 
        exit(0);
-       for (;;);
+       for (;;)
+               ;
 }
 
-
-
-cyg_int32
-cyg_httpd_exec_cgi_tcl(char *file_name);
+cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
 cyg_int32 homeForm(CYG_HTTPD_STATE *p)
 {
        cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
@@ -2053,20 +1180,7 @@ CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
 #include <cyg/kernel/ktypes.h>         // base kernel types
 #include <cyg/infra/cyg_trac.h>        // tracing macros
 #include <cyg/infra/cyg_ass.h>         // assertion macros
-#include <unistd.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <dirent.h>
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include <cyg/fileio/fileio.h>
-
 #include <cyg/kernel/kapi.h>
 #include <cyg/infra/diag.h>
 
@@ -2100,7 +1214,7 @@ static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
 // For simplicity we use _FILESYSTEM synchronization for all accesses since
 // we should never block in any filesystem operations.
 #if 1
-FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
+FSTAB_ENTRY(tftpfs_fste, "tftpfs", 0,
                CYG_SYNCMODE_NONE,
                tftpfs_mount,
                tftpfs_umount,
@@ -2121,12 +1235,12 @@ FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
 // mtab entry.
 // This defines a single ROMFS loaded into ROM at the configured address
 //
-// MTAB_ENTRY( rom_mte,        // structure name
+// MTAB_ENTRY(rom_mte, // structure name
 //             "/rom",         // mount point
 //             "romfs",        // FIlesystem type
 //             "",             // hardware device
 //  (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS  // Address in ROM
-//           );
+//);
 
 
 // -------------------------------------------------------------------------
@@ -2134,18 +1248,12 @@ FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
 // This set of file operations are used for normal open files.
 
 static cyg_fileops tftpfs_fileops =
-{
-       tftpfs_fo_read,
-       tftpfs_fo_write,
-       tftpfs_fo_lseek,
-       (cyg_fileop_ioctl *)cyg_fileio_erofs,
-    cyg_fileio_seltrue,
-    tftpfs_fo_fsync,
-    tftpfs_fo_close,
+{ tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
+               (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
+               tftpfs_fo_fsync, tftpfs_fo_close,
                (cyg_fileop_fstat *) cyg_fileio_erofs,
                (cyg_fileop_getinfo *) cyg_fileio_erofs,
-       (cyg_fileop_setinfo *)cyg_fileio_erofs,
-};
+               (cyg_fileop_setinfo *) cyg_fileio_erofs, };
 
 // -------------------------------------------------------------------------
 // tftpfs_mount()
@@ -2242,7 +1350,8 @@ static int fetchTftp(struct Tftp *tftp)
        if (!tftp->readFile)
        {
                int err;
-           tftp->actual = tftp_client_get( tftp->file, tftp->server, 0, tftp->mem, tftpMaxSize,   TFTP_OCTET, &err);
+               tftp->actual = tftp_client_get(tftp->file, tftp->server, 0, tftp->mem,
+                               tftpMaxSize, TFTP_OCTET, &err);
 
                if (tftp->actual < 0)
                {
@@ -2257,8 +1366,7 @@ static int fetchTftp(struct Tftp *tftp)
 // tftpfs_fo_write()
 // Read data from file.
 
-static int
-tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
+static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
 {
        struct Tftp *tftp = (struct Tftp *) fp->f_data;
 
@@ -2290,9 +1398,7 @@ tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
        return ENOERR;
 }
 
-
-static int
-tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
+static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
 {
        struct Tftp *tftp = (struct Tftp *) fp->f_data;
 
@@ -2323,8 +1429,7 @@ tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
        return ENOERR;
 }
 
-static int
-tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
+static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
 {
        int error = ENOERR;
        return error;
@@ -2341,7 +1446,8 @@ static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
 
        if (tftp->write)
        {
-           tftp_client_put( tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,   TFTP_OCTET, &error);
+               tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
+                               TFTP_OCTET, &error);
        }
 
        freeTftp(tftp);
@@ -2401,13 +1507,13 @@ void usleep(int us)
 }
 
 // Chunked version.
-cyg_int32
-show_log_entry(CYG_HTTPD_STATE *phttpstate)
+cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
 {
        cyg_httpd_start_chunked("text");
        if (logCount >= logSize)
        {
-        cyg_httpd_write_chunked(logBuffer+logCount%logSize, logSize-logCount%logSize);
+               cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
+                               - logCount % logSize);
        }
        cyg_httpd_write_chunked(logBuffer, writePtr);
        cyg_httpd_end_chunked();
@@ -2421,8 +1527,7 @@ static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
 static int logfs_umount(cyg_mtab_entry *mte);
 static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                int mode, cyg_file *fte);
-static int
-logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
+static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
 
 // File operations
 static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
@@ -2438,8 +1543,8 @@ static int logfs_fo_close(struct CYG_FILE_TAG *fp);
 // This defines the entry in the filesystem table.
 // For simplicity we use _FILESYSTEM synchronization for all accesses since
 // we should never block in any filesystem operations.
-FSTAB_ENTRY( logfs_fste, "logfs", 0,
-               CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
+FSTAB_ENTRY(logfs_fste, "logfs", 0,
+               CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
                logfs_mount,
                logfs_umount,
                logfs_open,
@@ -2459,18 +1564,12 @@ FSTAB_ENTRY( logfs_fste, "logfs", 0,
 // This set of file operations are used for normal open files.
 
 static cyg_fileops logfs_fileops =
-{
-       (cyg_fileop_read *)cyg_fileio_erofs,
-    (cyg_fileop_write *)logfs_fo_write,
+{ (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
                (cyg_fileop_lseek *) cyg_fileio_erofs,
-       (cyg_fileop_ioctl *)cyg_fileio_erofs,
-    cyg_fileio_seltrue,
-    logfs_fo_fsync,
-    logfs_fo_close,
-       (cyg_fileop_fstat *)cyg_fileio_erofs,
+               (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
+               logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
                (cyg_fileop_getinfo *) cyg_fileio_erofs,
-       (cyg_fileop_setinfo *)cyg_fileio_erofs,
-};
+               (cyg_fileop_setinfo *) cyg_fileio_erofs, };
 
 // -------------------------------------------------------------------------
 // logfs_mount()
@@ -2503,8 +1602,7 @@ static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
 // logfs_fo_write()
 // Write data to file.
 
-static int
-logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
+static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
 {
        int i;
        for (i = 0; i < uio->uio_iovcnt; i++)
@@ -2519,8 +1617,7 @@ logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
 
        return ENOERR;
 }
-static int
-logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
+static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
 {
        return ENOERR;
 }
@@ -2534,122 +1631,26 @@ static int logfs_fo_close(struct CYG_FILE_TAG *fp)
        return ENOERR;
 }
 
-static bool
-ramiodev_init( struct cyg_devtab_entry *tab )
-{
-       return true;
-}
+int loadFile(const char *fileName, void **data, int *len);
 
-static Cyg_ErrNo
-ramiodev_bread( cyg_io_handle_t handle, void *buf, cyg_uint32 *len,
-                  cyg_uint32 pos)
+/* boolean parameter stored on config */
+int boolParam(char *var)
 {
-       if (*len+pos>ramblockdevice_size)
-       {
-               *len=ramblockdevice_size-pos;
-       }
-       memcpy(buf, ramblockdevice+pos, *len);
-       return ENOERR;
-}
+       bool result = false;
+       char *name = alloc_printf("%s/%s", zylin_config_dir, var);
+       if (name == NULL)
+               return result;
 
-static Cyg_ErrNo
-ramiodev_bwrite( cyg_io_handle_t handle, const void *buf, cyg_uint32 *len,
-                   cyg_uint32 pos )
-{
-       if (((pos%4)!=0)||(((*len)%4)!=0))
+       void *data;
+       int len;
+       if (loadFile(name, &data, &len) == ERROR_OK)
        {
-               diag_printf("Unaligned write %d %d!", pos, *len);
+               if (len > 1)
+                       len = 1;
+               result = strncmp((char *) data, "1", len) == 0;
+               free(data);
        }
-
-       memcpy(ramblockdevice+pos, buf, *len);
-       return ENOERR;
-}
-
-static Cyg_ErrNo
-ramiodev_get_config( cyg_io_handle_t handle,
-                       cyg_uint32 key,
-                       void* buf,
-                       cyg_uint32* len)
-{
-    switch (key) {
-    case CYG_IO_GET_CONFIG_FLASH_ERASE:
-    {
-        if ( *len != sizeof( cyg_io_flash_getconfig_erase_t ) )
-             return -EINVAL;
-        {
-            cyg_io_flash_getconfig_erase_t *e = (cyg_io_flash_getconfig_erase_t *)buf;
-            char *startpos = ramblockdevice + e->offset;
-
-            if (((e->offset%(64*1024))!=0)||((e->len%(64*1024))!=0))
-            {
-               diag_printf("Erease is not aligned %d %d\n", e->offset, e->len);
-            }
-
-            memset(startpos, 0xff, e->len);
-
-            e->flasherr = 0;
-        }
-        return ENOERR;
-    }
-    case CYG_IO_GET_CONFIG_FLASH_DEVSIZE:
-    {
-        if ( *len != sizeof( cyg_io_flash_getconfig_devsize_t ) )
-             return -EINVAL;
-        {
-            cyg_io_flash_getconfig_devsize_t *d =
-                (cyg_io_flash_getconfig_devsize_t *)buf;
-
-                       d->dev_size = ramblockdevice_size;
-        }
-        return ENOERR;
-    }
-
-    case CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE:
-    {
-        cyg_io_flash_getconfig_blocksize_t *b =
-            (cyg_io_flash_getconfig_blocksize_t *)buf;
-        if ( *len != sizeof( cyg_io_flash_getconfig_blocksize_t ) )
-             return -EINVAL;
-
-        // offset unused for now
-               b->block_size = 64*1024;
-        return ENOERR;
-    }
-
-    default:
-        return -EINVAL;
-    }
+       free(name);
+       return result;
 }
 
-static Cyg_ErrNo
-ramiodev_set_config( cyg_io_handle_t handle,
-                       cyg_uint32 key,
-                       const void* buf,
-                       cyg_uint32* len)
-{
-
-    switch (key) {
-    default:
-        return -EINVAL;
-    }
-} // ramiodev_set_config()
-
-// get_config/set_config should be added later to provide the other flash
-// operations possible, like erase etc.
-
-BLOCK_DEVIO_TABLE( cyg_io_ramdev1_ops,
-                   &ramiodev_bwrite,
-                   &ramiodev_bread,
-                   0, // no select
-                   &ramiodev_get_config,
-                   &ramiodev_set_config
-    );
-
-
-BLOCK_DEVTAB_ENTRY( cyg_io_ramdev1,
-                    "/dev/ram",
-                    0,
-                    &cyg_io_ramdev1_ops,
-                    &ramiodev_init,
-                    0, // No lookup required
-                    NULL );

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)