You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2675 lines
59 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2008 by Øyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "log.h"
  23. #include "types.h"
  24. #include "jtag.h"
  25. #include "configuration.h"
  26. #include "xsvf.h"
  27. #include "target.h"
  28. #include "flash.h"
  29. #include "nand.h"
  30. #include "pld.h"
  31. #include "command.h"
  32. #include "server.h"
  33. #include "telnet_server.h"
  34. #include "gdb_server.h"
  35. #include <time_support.h>
  36. #include <sys/time.h>
  37. #include <sys/types.h>
  38. #include <strings.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <errno.h>
  44. #include <cyg/io/flash.h>
  45. #include <pkgconf/fs_jffs2.h> // Address of JFFS2
  46. #include <network.h>
  47. #include <fcntl.h>
  48. #include <sys/stat.h>
  49. #include <cyg/fileio/fileio.h>
  50. #include <dirent.h>
  51. #include <cyg/athttpd/http.h>
  52. #include <cyg/athttpd/socket.h>
  53. #include <cyg/athttpd/handler.h>
  54. #include <cyg/athttpd/cgi.h>
  55. #include <cyg/athttpd/forms.h>
  56. #include <cyg/discover/discover.h>
  57. #include <cyg/hal/hal_diag.h>
  58. #include <cyg/kernel/kapi.h>
  59. #include <cyg/io/serialio.h>
  60. #include <cyg/io/io.h>
  61. #include <netinet/tcp.h>
  62. #include "rom.h"
  63. #include <sys/ioctl.h>
  64. #include <sys/socket.h>
  65. #include <netinet/in.h>
  66. #include <net/if.h>
  67. #include <arpa/inet.h>
  68. #include <sys/types.h>
  69. #include <sys/socket.h>
  70. #include <netdb.h>
  71. #include <netinet/in.h>
  72. #include <unistd.h>
  73. #include <arpa/inet.h>
  74. #include <stdio.h>
  75. #include <ifaddrs.h>
  76. #include <string.h>
  77. #include <unistd.h>
  78. #include <stdio.h>
  79. #define MAX_IFS 64
  80. #if defined(CYGPKG_NET_FREEBSD_STACK)
  81. #include <tftp_support.h>
  82. /* posix compatibility broken*/
  83. struct tftpd_fileops fileops =
  84. {
  85. (int (*)(const char *, int))open,
  86. close,
  87. (int (*)(int, const void *, int))write,
  88. ( int (*)(int, void *, int))read
  89. };
  90. #endif
  91. #define ZYLIN_VERSION "1.47"
  92. #define ZYLIN_DATE __DATE__
  93. #define ZYLIN_TIME __TIME__
  94. /* hmmm.... we can't pick up the right # during build if we've checked this out
  95. * in Eclipse... arrggghh...*/
  96. #define ZYLIN_OPENOCD "$Revision$"
  97. #define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
  98. #define ZYLIN_CONFIG_DIR "/config/settings"
  99. void diag_write(char *buf, int len)
  100. {
  101. int j;
  102. for (j = 0; j < len; j++)
  103. {
  104. diag_printf("%c", buf[j]);
  105. }
  106. }
  107. static bool serialLog = true;
  108. static bool writeLog = true;
  109. char hwaddr[512];
  110. struct FastLoad
  111. {
  112. u32 address;
  113. u8 *data;
  114. int length;
  115. };
  116. static int fastload_num;
  117. static struct FastLoad *fastload;
  118. static void free_fastload()
  119. {
  120. if (fastload!=NULL)
  121. {
  122. int i;
  123. for (i=0; i<fastload_num; i++)
  124. {
  125. if (fastload[i].data)
  126. free(fastload[i].data);
  127. }
  128. free(fastload);
  129. fastload=NULL;
  130. }
  131. }
  132. int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  133. {
  134. u8 *buffer;
  135. u32 buf_cnt;
  136. u32 image_size;
  137. u32 min_address=0;
  138. u32 max_address=0xffffffff;
  139. int i;
  140. int retval;
  141. image_t image;
  142. duration_t duration;
  143. char *duration_text;
  144. if ((argc < 1)||(argc > 5))
  145. {
  146. return ERROR_COMMAND_SYNTAX_ERROR;
  147. }
  148. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  149. if (argc >= 2)
  150. {
  151. image.base_address_set = 1;
  152. image.base_address = strtoul(args[1], NULL, 0);
  153. }
  154. else
  155. {
  156. image.base_address_set = 0;
  157. }
  158. image.start_address_set = 0;
  159. if (argc>=4)
  160. {
  161. min_address=strtoul(args[3], NULL, 0);
  162. }
  163. if (argc>=5)
  164. {
  165. max_address=strtoul(args[4], NULL, 0)+min_address;
  166. }
  167. if (min_address>max_address)
  168. {
  169. return ERROR_COMMAND_SYNTAX_ERROR;
  170. }
  171. duration_start_measure(&duration);
  172. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  173. {
  174. return ERROR_OK;
  175. }
  176. image_size = 0x0;
  177. retval = ERROR_OK;
  178. fastload_num=image.num_sections;
  179. fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
  180. if (fastload==NULL)
  181. {
  182. image_close(&image);
  183. return ERROR_FAIL;
  184. }
  185. memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
  186. for (i = 0; i < image.num_sections; i++)
  187. {
  188. buffer = malloc(image.sections[i].size);
  189. if (buffer == NULL)
  190. {
  191. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  192. break;
  193. }
  194. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  195. {
  196. free(buffer);
  197. break;
  198. }
  199. u32 offset=0;
  200. u32 length=buf_cnt;
  201. /* DANGER!!! beware of unsigned comparision here!!! */
  202. if ((image.sections[i].base_address+buf_cnt>=min_address)&&
  203. (image.sections[i].base_address<max_address))
  204. {
  205. if (image.sections[i].base_address<min_address)
  206. {
  207. /* clip addresses below */
  208. offset+=min_address-image.sections[i].base_address;
  209. length-=offset;
  210. }
  211. if (image.sections[i].base_address+buf_cnt>max_address)
  212. {
  213. length-=(image.sections[i].base_address+buf_cnt)-max_address;
  214. }
  215. fastload[i].address=image.sections[i].base_address+offset;
  216. fastload[i].data=malloc(length);
  217. if (fastload[i].data==NULL)
  218. {
  219. free(buffer);
  220. break;
  221. }
  222. memcpy(fastload[i].data, buffer+offset, length);
  223. fastload[i].length=length;
  224. image_size += length;
  225. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
  226. }
  227. free(buffer);
  228. }
  229. duration_stop_measure(&duration, &duration_text);
  230. if (retval==ERROR_OK)
  231. {
  232. command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
  233. command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
  234. }
  235. free(duration_text);
  236. image_close(&image);
  237. if (retval!=ERROR_OK)
  238. {
  239. free_fastload();
  240. }
  241. return retval;
  242. }
  243. int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  244. {
  245. if (argc>0)
  246. return ERROR_COMMAND_SYNTAX_ERROR;
  247. if (fastload==NULL)
  248. {
  249. LOG_ERROR("No image in memory");
  250. return ERROR_FAIL;
  251. }
  252. int i;
  253. int ms=timeval_ms();
  254. int size=0;
  255. for (i=0; i<fastload_num;i++)
  256. {
  257. int retval;
  258. target_t *target = get_current_target(cmd_ctx);
  259. if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
  260. {
  261. return retval;
  262. }
  263. size+=fastload[i].length;
  264. }
  265. int after=timeval_ms();
  266. command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
  267. return ERROR_OK;
  268. }
  269. /* Give TELNET a way to find out what version this is */
  270. int handle_zy1000_version_command(struct command_context_s *cmd_ctx, char *cmd,
  271. char **args, int argc)
  272. {
  273. if (argc > 1)
  274. {
  275. return ERROR_COMMAND_SYNTAX_ERROR;
  276. }
  277. if (argc == 0)
  278. {
  279. command_print(cmd_ctx, ZYLIN_OPENOCD_VERSION);
  280. } else if (strcmp("openocd", args[0])==0)
  281. {
  282. int revision;
  283. revision=atol(ZYLIN_OPENOCD+strlen("XRevision: "));
  284. command_print(cmd_ctx, "%d", revision);
  285. } else if (strcmp("zy1000", args[0])==0)
  286. {
  287. command_print(cmd_ctx, "%s", ZYLIN_VERSION);
  288. } else if (strcmp("date", args[0])==0)
  289. {
  290. command_print(cmd_ctx, "%s", ZYLIN_DATE);
  291. } else
  292. {
  293. return ERROR_COMMAND_SYNTAX_ERROR;
  294. }
  295. return ERROR_OK;
  296. }
  297. extern flash_driver_t *flash_drivers[];
  298. extern target_type_t *target_types[];
  299. #ifdef CYGPKG_PROFILE_GPROF
  300. #include <cyg/profile/profile.h>
  301. extern char _stext, _etext; // Defined by the linker
  302. void start_profile(void)
  303. {
  304. // This starts up the system-wide profiling, gathering
  305. // profile information on all of the code, with a 16 byte
  306. // "bucket" size, at a rate of 100us/profile hit.
  307. // Note: a bucket size of 16 will give pretty good function
  308. // resolution. Much smaller and the buffer becomes
  309. // much too large for very little gain.
  310. // Note: a timer period of 100us is also a reasonable
  311. // compromise. Any smaller and the overhead of
  312. // handling the timter (profile) interrupt could
  313. // swamp the system. A fast processor might get
  314. // by with a smaller value, but a slow one could
  315. // even be swamped by this value. If the value is
  316. // too large, the usefulness of the profile is reduced.
  317. // no more interrupts than 1/10ms.
  318. // profile_on(&_stext, &_etext, 16, 10000); // DRAM
  319. //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
  320. profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
  321. }
  322. #endif
  323. // launch GDB server if a config file exists
  324. bool zylinjtag_parse_config_file(struct command_context_s *cmd_ctx, const char *config_file_name)
  325. {
  326. bool foundFile = false;
  327. FILE *config_file = NULL;
  328. command_print(cmd_ctx, "executing config file %s", config_file_name);
  329. config_file = fopen(config_file_name, "r");
  330. if (config_file)
  331. {
  332. fclose(config_file);
  333. int retval;
  334. retval = command_run_linef(cmd_ctx, "script %s", config_file_name);
  335. if (retval == ERROR_OK)
  336. {
  337. foundFile = true;
  338. }
  339. else
  340. {
  341. command_print(cmd_ctx, "Failed executing %s %d", config_file_name, retval);
  342. }
  343. }
  344. else
  345. {
  346. command_print(cmd_ctx, "No %s found", config_file_name);
  347. }
  348. return foundFile;
  349. }
  350. extern int eth0_up;
  351. static FILE *log;
  352. static char reboot_stack[2048];
  353. static void
  354. zylinjtag_reboot(cyg_addrword_t data)
  355. {
  356. serialLog = true;
  357. diag_printf("Rebooting in 100 ticks..\n");
  358. cyg_thread_delay(100);
  359. diag_printf("Unmounting /config..\n");
  360. umount("/config");
  361. diag_printf("Rebooting..\n");
  362. HAL_PLATFORM_RESET();
  363. }
  364. static cyg_thread zylinjtag_thread_object;
  365. static cyg_handle_t zylinjtag_thread_handle;
  366. void reboot(void)
  367. {
  368. cyg_thread_create(1,
  369. zylinjtag_reboot,
  370. (cyg_addrword_t)0,
  371. "reboot Thread",
  372. (void *)reboot_stack,
  373. sizeof(reboot_stack),
  374. &zylinjtag_thread_handle,
  375. &zylinjtag_thread_object);
  376. cyg_thread_resume(zylinjtag_thread_handle);
  377. }
  378. int configuration_output_handler(struct command_context_s *context, const char* line)
  379. {
  380. diag_printf("%s", line);
  381. return ERROR_OK;
  382. }
  383. int zy1000_configuration_output_handler_log(struct command_context_s *context, const char* line)
  384. {
  385. LOG_USER_N("%s", line);
  386. return ERROR_OK;
  387. }
  388. int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
  389. char **args, int argc)
  390. {
  391. if (argc != 1)
  392. {
  393. command_print(cmd_ctx, "rm <filename>");
  394. return ERROR_INVALID_ARGUMENTS;
  395. }
  396. if (unlink(args[0]) != 0)
  397. {
  398. command_print(cmd_ctx, "failed: %d", errno);
  399. }
  400. return ERROR_OK;
  401. }
  402. int loadFile(const char *fileName, void **data, int *len);
  403. int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
  404. char **args, int argc)
  405. {
  406. if (argc != 1)
  407. {
  408. command_print(cmd_ctx, "cat <filename>");
  409. return ERROR_INVALID_ARGUMENTS;
  410. }
  411. // NOTE!!! we only have line printing capability so we print the entire file as a single line.
  412. void *data;
  413. int len;
  414. int retval = loadFile(args[0], &data, &len);
  415. if (retval == ERROR_OK)
  416. {
  417. command_print(cmd_ctx, "%s", data);
  418. free(data);
  419. }
  420. else
  421. {
  422. command_print(cmd_ctx, "%s not found %d", args[0], retval);
  423. }
  424. return ERROR_OK;
  425. }
  426. int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
  427. char **args, int argc)
  428. {
  429. if (argc != 1)
  430. {
  431. command_print(cmd_ctx, "trunc <filename>");
  432. return ERROR_INVALID_ARGUMENTS;
  433. }
  434. FILE *config_file = NULL;
  435. config_file = fopen(args[0], "w");
  436. if (config_file != NULL)
  437. fclose(config_file);
  438. return ERROR_OK;
  439. }
  440. int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  441. {
  442. static int prev = 0;
  443. struct mallinfo info;
  444. if (argc != 0)
  445. {
  446. command_print(cmd_ctx, "meminfo");
  447. return ERROR_INVALID_ARGUMENTS;
  448. }
  449. info = mallinfo();
  450. if (prev > 0)
  451. {
  452. command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
  453. }
  454. prev = info.fordblks;
  455. command_print(cmd_ctx, "Available ram: %d", info.fordblks );
  456. return ERROR_OK;
  457. }
  458. static bool savePower;
  459. static void setPower(bool power)
  460. {
  461. savePower = power;
  462. if (power)
  463. {
  464. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x14, 0x8);
  465. } else
  466. {
  467. HAL_WRITE_UINT32(ZY1000_JTAG_BASE+0x10, 0x8);
  468. }
  469. }
  470. int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  471. {
  472. if (argc > 1)
  473. {
  474. return ERROR_INVALID_ARGUMENTS;
  475. }
  476. if (argc == 1)
  477. {
  478. if (strcmp(args[0], "on") == 0)
  479. {
  480. setPower(1);
  481. }
  482. else if (strcmp(args[0], "off") == 0)
  483. {
  484. setPower(0);
  485. } else
  486. {
  487. command_print(cmd_ctx, "arg is \"on\" or \"off\"");
  488. return ERROR_INVALID_ARGUMENTS;
  489. }
  490. }
  491. command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");
  492. return ERROR_OK;
  493. }
  494. int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
  495. char **args, int argc)
  496. {
  497. if (argc < 1)
  498. {
  499. command_print(cmd_ctx,
  500. "append <filename> [<string1>, [<string2>, ...]]");
  501. return ERROR_INVALID_ARGUMENTS;
  502. }
  503. FILE *config_file = NULL;
  504. config_file = fopen(args[0], "a");
  505. if (config_file != NULL)
  506. {
  507. int i;
  508. fseek(config_file, 0, SEEK_END);
  509. for (i = 1; i < argc; i++)
  510. {
  511. fwrite(args[i], strlen(args[i]), 1, config_file);
  512. if (i != argc - 1)
  513. {
  514. fwrite(" ", 1, 1, config_file);
  515. }
  516. }
  517. fwrite("\n", 1, 1, config_file);
  518. fclose(config_file);
  519. }
  520. return ERROR_OK;
  521. }
  522. extern int telnet_socket;
  523. int readMore(int fd, void *data, int length)
  524. {
  525. /* used in select() */
  526. fd_set read_fds;
  527. /* monitor sockets for acitvity */
  528. int fd_max = 1;
  529. FD_ZERO(&read_fds);
  530. /* listen for new connections */
  531. FD_SET(fd, &read_fds);
  532. // Maximum 5 seconds.
  533. struct timeval tv;
  534. tv.tv_sec = 5;
  535. tv.tv_usec = 0;
  536. int retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
  537. if (retval == 0)
  538. {
  539. diag_printf("Timed out waiting for binary payload\n");
  540. return -1;
  541. }
  542. if (retval != 1)
  543. return -1;
  544. return read_socket(fd, data, length);
  545. }
  546. int readAll(int fd, void *data, int length)
  547. {
  548. int pos = 0;
  549. for (;;)
  550. {
  551. int actual = readMore(fd, ((char *) data) + pos, length - pos);
  552. // diag_printf("Read %d bytes(pos=%d, length=%d)\n", actual, pos, length);
  553. if (actual <= 0)
  554. return -1;
  555. pos += actual;
  556. if (pos == length)
  557. break;
  558. }
  559. return length;
  560. }
  561. int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  562. {
  563. cyg_uint32 value;
  564. if (argc != 1)
  565. {
  566. return ERROR_INVALID_ARGUMENTS;
  567. }
  568. HAL_READ_UINT32(strtoul(args[0], NULL, 0), value);
  569. command_print(cmd_ctx, "0x%x : 0x%x", strtoul(args[0], NULL, 0), value);
  570. return ERROR_OK;
  571. }
  572. int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  573. {
  574. if (argc != 2)
  575. {
  576. return ERROR_INVALID_ARGUMENTS;
  577. }
  578. HAL_WRITE_UINT32(strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0));
  579. return ERROR_OK;
  580. }
  581. int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  582. {
  583. if (argc != 2)
  584. {
  585. return ERROR_INVALID_ARGUMENTS;
  586. }
  587. // NOTE!!! we only have line printing capability so we print the entire file as a single line.
  588. void *data;
  589. int len;
  590. int retval = loadFile(args[0], &data, &len);
  591. if (retval != ERROR_OK)
  592. return retval;
  593. FILE *f = fopen(args[1], "wb");
  594. if (f == NULL)
  595. retval = ERROR_INVALID_ARGUMENTS;
  596. int pos = 0;
  597. for (;;)
  598. {
  599. int chunk = len - pos;
  600. static const int maxChunk = 512 * 1024; // ~1/sec
  601. if (chunk > maxChunk)
  602. {
  603. chunk = maxChunk;
  604. }
  605. if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
  606. retval = ERROR_INVALID_ARGUMENTS;
  607. if (retval != ERROR_OK)
  608. {
  609. break;
  610. }
  611. command_print(cmd_ctx, "%d", len - pos);
  612. pos += chunk;
  613. if (pos == len)
  614. break;
  615. }
  616. if (retval == ERROR_OK)
  617. {
  618. command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
  619. } else
  620. {
  621. command_print(cmd_ctx, "Failed: %d", retval);
  622. }
  623. if (data != NULL)
  624. free(data);
  625. if (f != NULL)
  626. fclose(f);
  627. if (retval != ERROR_OK)
  628. unlink(args[1]);
  629. return retval;
  630. }
  631. #ifdef CYGPKG_PROFILE_GPROF
  632. extern void start_profile();
  633. int eCosBoard_handle_eCosBoard_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  634. {
  635. command_print(cmd_ctx, "Profiling started");
  636. start_profile();
  637. return ERROR_OK;
  638. }
  639. #endif
  640. externC void phi_init_all_network_interfaces();
  641. command_context_t *cmd_ctx;
  642. static bool webRunning = false;
  643. void keep_webserver()
  644. {
  645. // Target initialisation is only attempted at startup, so we sleep forever and
  646. // let the http server bail us out(i.e. get config files set up).
  647. diag_printf("OpenOCD has invoked exit().\n"
  648. "Use web server to correct any configuration settings and reboot.\n");
  649. if (!webRunning)
  650. reboot();
  651. // exit() will terminate the current thread and we we'll then sleep eternally or
  652. // we'll have a reboot scheduled.
  653. }
  654. extern void printDccChar(char c);
  655. static char logBuffer[128 * 1024];
  656. static const int logSize = sizeof(logBuffer);
  657. int writePtr = 0;
  658. int logCount = 0;
  659. void _zylinjtag_diag_write_char(char c, void **param)
  660. {
  661. if (writeLog)
  662. {
  663. logBuffer[writePtr] = c;
  664. writePtr = (writePtr + 1) % logSize;
  665. logCount++;
  666. }
  667. if (serialLog)
  668. {
  669. if (c == '\n')
  670. {
  671. HAL_DIAG_WRITE_CHAR('\r');
  672. }
  673. HAL_DIAG_WRITE_CHAR(c);
  674. }
  675. #ifdef CYGPKG_HAL_ZYLIN_PHI
  676. printDccChar(c);
  677. #endif
  678. }
  679. #define SHOW_RESULT(a, b) diag_printf(#a " failed %d\n", (int)b)
  680. #define IOSIZE 512
  681. static void copyfile(char *name2, char *name1)
  682. {
  683. int err;
  684. char buf[IOSIZE];
  685. int fd1, fd2;
  686. ssize_t done, wrote;
  687. fd1 = open(name1, O_WRONLY | O_CREAT);
  688. if (fd1 < 0)
  689. SHOW_RESULT( open, fd1 );
  690. fd2 = open(name2, O_RDONLY);
  691. if (fd2 < 0)
  692. SHOW_RESULT( open, fd2 );
  693. for (;;)
  694. {
  695. done = read(fd2, buf, IOSIZE );
  696. if (done < 0)
  697. {
  698. SHOW_RESULT( read, done );
  699. break;
  700. }
  701. if( done == 0 ) break;
  702. wrote = write(fd1, buf, done);
  703. if( wrote != done ) SHOW_RESULT( write, wrote );
  704. if( wrote != done ) break;
  705. }
  706. err = close(fd1);
  707. if( err < 0 ) SHOW_RESULT( close, err );
  708. err = close(fd2);
  709. if( err < 0 ) SHOW_RESULT( close, err );
  710. }
  711. static void copydir(char *name, char *destdir)
  712. {
  713. int err;
  714. DIR *dirp;
  715. dirp = opendir(destdir);
  716. if (dirp==NULL)
  717. {
  718. mkdir(destdir, 0777);
  719. } else
  720. {
  721. err = closedir(dirp);
  722. }
  723. dirp = opendir(name);
  724. if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
  725. for (;;)
  726. {
  727. struct dirent *entry = readdir(dirp);
  728. if (entry == NULL)
  729. break;
  730. if (strcmp(entry->d_name, ".") == 0)
  731. continue;
  732. if (strcmp(entry->d_name, "..") == 0)
  733. continue;
  734. bool isDir = false;
  735. struct stat buf;
  736. char fullPath[PATH_MAX];
  737. strncpy(fullPath, name, PATH_MAX);
  738. strcat(fullPath, "/");
  739. strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
  740. if (stat(fullPath, &buf) == -1)
  741. {
  742. diag_printf("unable to read status from %s", fullPath);
  743. break;
  744. }
  745. isDir = S_ISDIR(buf.st_mode) != 0;
  746. if (isDir)
  747. continue;
  748. // diag_printf("<INFO>: entry %14s",entry->d_name);
  749. char fullname[PATH_MAX];
  750. char fullname2[PATH_MAX];
  751. strcpy(fullname, name);
  752. strcat(fullname, "/");
  753. strcat(fullname, entry->d_name);
  754. strcpy(fullname2, destdir);
  755. strcat(fullname2, "/");
  756. strcat(fullname2, entry->d_name);
  757. // diag_printf("from %s to %s\n", fullname, fullname2);
  758. copyfile(fullname, fullname2);
  759. // diag_printf("\n");
  760. }
  761. err = closedir(dirp);
  762. if( err < 0 ) SHOW_RESULT( stat, err );
  763. }
  764. #if 0
  765. MTAB_ENTRY( romfs_mte1,
  766. "/rom",
  767. "romfs",
  768. "",
  769. (CYG_ADDRWORD) &filedata[0] );
  770. #endif
  771. void openocd_sleep_prelude()
  772. {
  773. cyg_mutex_unlock(&httpstate.jim_lock);
  774. }
  775. void openocd_sleep_postlude()
  776. {
  777. cyg_mutex_lock(&httpstate.jim_lock);
  778. }
  779. void format(void)
  780. {
  781. diag_printf("Formatting JFFS2...\n");
  782. cyg_io_handle_t handle;
  783. Cyg_ErrNo err;
  784. err = cyg_io_lookup(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, &handle);
  785. if (err != ENOERR)
  786. {
  787. diag_printf("Flash Error cyg_io_lookup: %d\n", err);
  788. reboot();
  789. }
  790. cyg_uint32 len;
  791. cyg_io_flash_getconfig_devsize_t ds;
  792. len = sizeof (ds);
  793. err = cyg_io_get_config(handle,
  794. CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
  795. if (err != ENOERR)
  796. {
  797. diag_printf("Flash error cyg_io_get_config %d\n", err);
  798. reboot();
  799. }
  800. cyg_io_flash_getconfig_erase_t e;
  801. void *err_addr;
  802. len = sizeof (e);
  803. e.offset = 0;
  804. e.len = ds.dev_size;
  805. e.err_address = &err_addr;
  806. diag_printf("Formatting 0x%08x bytes\n", ds.dev_size);
  807. err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE,
  808. &e, &len);
  809. if (err != ENOERR)
  810. {
  811. diag_printf("Flash erase error %d offset 0x%p\n", err, err_addr);
  812. reboot();
  813. }
  814. diag_printf("Flash formatted successfully\n");
  815. reboot();
  816. }
  817. static int
  818. zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp,
  819. int argc,
  820. Jim_Obj * const *argv)
  821. {
  822. if (argc != 1)
  823. {
  824. return JIM_ERR;
  825. }
  826. format();
  827. for(;;);
  828. }
  829. static int
  830. zylinjtag_Jim_Command_rm(Jim_Interp *interp,
  831. int argc,
  832. Jim_Obj * const *argv)
  833. {
  834. int del;
  835. if (argc != 2)
  836. {
  837. Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
  838. return JIM_ERR;
  839. }
  840. del = 0;
  841. if (unlink(Jim_GetString(argv[1], NULL)) == 0)
  842. del = 1;
  843. if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
  844. del = 1;
  845. return del ? JIM_OK : JIM_ERR;
  846. }
  847. static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
  848. Jim_Obj * const *argv)
  849. {
  850. cyg_handle_t thread = 0;
  851. cyg_uint16 id = 0;
  852. Jim_Obj *threads = Jim_NewListObj(interp, NULL, 0);
  853. /* Loop over the threads, and generate a table row for
  854. * each.
  855. */
  856. while (cyg_thread_get_next(&thread, &id))
  857. {
  858. Jim_Obj *threadObj = Jim_NewListObj(interp, NULL, 0);
  859. cyg_thread_info info;
  860. char *state_string;
  861. cyg_thread_get_info(thread, id, &info);
  862. if (info.name == NULL)
  863. info.name = "<no name>";
  864. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  865. info.name, strlen(info.name)));
  866. /* Translate the state into a string.
  867. */
  868. if (info.state == 0)
  869. state_string = "RUN";
  870. else if (info.state & 0x04)
  871. state_string = "SUSP";
  872. else
  873. switch (info.state & 0x1b)
  874. {
  875. case 0x01:
  876. state_string = "SLEEP";
  877. break;
  878. case 0x02:
  879. state_string = "CNTSLEEP";
  880. break;
  881. case 0x08:
  882. state_string = "CREATE";
  883. break;
  884. case 0x10:
  885. state_string = "EXIT";
  886. break;
  887. default:
  888. state_string = "????";
  889. break;
  890. }
  891. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  892. state_string, strlen(state_string)));
  893. Jim_ListAppendElement (interp, threadObj, Jim_NewIntObj(interp, id));
  894. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.set_pri));
  895. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.cur_pri));
  896. Jim_ListAppendElement(interp, threads, threadObj);
  897. }
  898. Jim_SetResult( interp, threads);
  899. return JIM_OK;
  900. }
  901. static int
  902. zylinjtag_Jim_Command_ls(Jim_Interp *interp,
  903. int argc,
  904. Jim_Obj * const *argv)
  905. {
  906. if (argc != 2)
  907. {
  908. Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
  909. return JIM_ERR;
  910. }
  911. char *name = (char*) Jim_GetString(argv[1], NULL);
  912. DIR *dirp = NULL;
  913. dirp = opendir(name);
  914. if (dirp == NULL)
  915. {
  916. return JIM_ERR;
  917. }
  918. Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
  919. for (;;)
  920. {
  921. struct dirent *entry = NULL;
  922. entry = readdir(dirp);
  923. if (entry == NULL)
  924. break;
  925. if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
  926. continue;
  927. Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
  928. }
  929. closedir(dirp);
  930. Jim_SetResult(interp, objPtr);
  931. return JIM_OK;
  932. }
  933. static int
  934. zylinjtag_Jim_Command_getmem(Jim_Interp *interp,
  935. int argc,
  936. Jim_Obj * const *argv)
  937. {
  938. if (argc != 3)
  939. {
  940. Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
  941. return JIM_ERR;
  942. }
  943. long address;
  944. long length;
  945. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  946. return JIM_ERR;
  947. if (Jim_GetLong(interp, argv[2], &length) != JIM_OK)
  948. return JIM_ERR;
  949. if (length < 0 && length > (4096 * 1024))
  950. {
  951. Jim_WrongNumArgs(interp, 1, argv, "getmem ?dir?");
  952. return JIM_ERR;
  953. }
  954. void *mem = malloc(length);
  955. if (mem == NULL)
  956. return JIM_ERR;
  957. target_t *target = get_current_target(cmd_ctx);
  958. int retval;
  959. int size = 1;
  960. int count = length;
  961. if ((address % 4 == 0) && (count % 4 == 0))
  962. {
  963. size = 4;
  964. count /= 4;
  965. }
  966. if ((retval = target->type->read_memory(target, address, size, count, mem)) != ERROR_OK)
  967. {
  968. free(mem);
  969. return JIM_ERR;
  970. }
  971. Jim_Obj *objPtr = Jim_NewStringObj(interp, mem, length);
  972. Jim_SetResult(interp, objPtr);
  973. free(mem);
  974. return JIM_OK;
  975. }
  976. static int
  977. zylinjtag_Jim_Command_peek(Jim_Interp *interp,
  978. int argc,
  979. Jim_Obj * const *argv)
  980. {
  981. if (argc != 2)
  982. {
  983. Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
  984. return JIM_ERR;
  985. }
  986. long address;
  987. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  988. return JIM_ERR;
  989. int value = *((volatile int *) address);
  990. Jim_SetResult(interp, Jim_NewIntObj(interp, value));
  991. return JIM_OK;
  992. }
  993. static int
  994. zylinjtag_Jim_Command_poke(Jim_Interp *interp,
  995. int argc,
  996. Jim_Obj * const *argv)
  997. {
  998. if (argc != 3)
  999. {
  1000. Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
  1001. return JIM_ERR;
  1002. }
  1003. long address;
  1004. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  1005. return JIM_ERR;
  1006. long value;
  1007. if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
  1008. return JIM_ERR;
  1009. *((volatile int *) address) = value;
  1010. return JIM_OK;
  1011. }
  1012. static int
  1013. zylinjtag_Jim_Command_flash(Jim_Interp *interp,
  1014. int argc,
  1015. Jim_Obj * const *argv)
  1016. {
  1017. int retval;
  1018. u32 base = 0;
  1019. flash_bank_t *t = get_flash_bank_by_num_noprobe(0);
  1020. if (t != NULL)
  1021. {
  1022. base = t->base;
  1023. retval = JIM_OK;
  1024. } else
  1025. {
  1026. retval = JIM_ERR;
  1027. }
  1028. if (retval == JIM_OK)
  1029. {
  1030. Jim_SetResult(interp, Jim_NewIntObj(interp, base));
  1031. }
  1032. return retval;
  1033. }
  1034. static int
  1035. zylinjtag_Jim_Command_log(Jim_Interp *interp,
  1036. int argc,
  1037. Jim_Obj * const *argv)
  1038. {
  1039. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  1040. if (logCount >= logSize)
  1041. {
  1042. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer+logCount%logSize, logSize-logCount%logSize);
  1043. }
  1044. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
  1045. Jim_SetResult(interp, tclOutput);
  1046. return JIM_OK;
  1047. }
  1048. static int
  1049. zylinjtag_Jim_Command_reboot(Jim_Interp *interp,
  1050. int argc,
  1051. Jim_Obj * const *argv)
  1052. {
  1053. reboot();
  1054. return JIM_OK;
  1055. }
  1056. static int
  1057. zylinjtag_Jim_Command_mac(Jim_Interp *interp,
  1058. int argc,
  1059. Jim_Obj * const *argv)
  1060. {
  1061. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  1062. Jim_AppendString(httpstate.jim_interp, tclOutput, hwaddr, strlen(hwaddr));
  1063. Jim_SetResult(interp, tclOutput);
  1064. return JIM_OK;
  1065. }
  1066. static int
  1067. zylinjtag_Jim_Command_ip(Jim_Interp *interp,
  1068. int argc,
  1069. Jim_Obj * const *argv)
  1070. {
  1071. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  1072. struct ifaddrs *ifa = NULL, *ifp = NULL;
  1073. if (getifaddrs(&ifp) < 0)
  1074. {
  1075. return JIM_ERR;
  1076. }
  1077. for (ifa = ifp; ifa; ifa = ifa->ifa_next)
  1078. {
  1079. char ip[200];
  1080. socklen_t salen;
  1081. if (ifa->ifa_addr->sa_family == AF_INET)
  1082. salen = sizeof(struct sockaddr_in);
  1083. else if (ifa->ifa_addr->sa_family == AF_INET6)
  1084. salen = sizeof(struct sockaddr_in6);
  1085. else
  1086. continue;
  1087. if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
  1088. NI_NUMERICHOST) < 0)
  1089. {
  1090. continue;
  1091. }
  1092. Jim_AppendString(httpstate.jim_interp, tclOutput, ip, strlen(ip));
  1093. break;
  1094. }
  1095. freeifaddrs(ifp);
  1096. Jim_SetResult(interp, tclOutput);
  1097. return JIM_OK;
  1098. }
  1099. extern Jim_Interp *interp;
  1100. static void zylinjtag_startNetwork()
  1101. {
  1102. // Bring TCP/IP up immediately before we're ready to accept commands.
  1103. //
  1104. // That is as soon as a PING responds, we're accepting telnet sessions.
  1105. #if defined(CYGPKG_NET_FREEBSD_STACK)
  1106. phi_init_all_network_interfaces();
  1107. #else
  1108. lwip_init();
  1109. #endif
  1110. if (!eth0_up)
  1111. {
  1112. diag_printf("Network not up and running\n");
  1113. exit(-1);
  1114. }
  1115. #if defined(CYGPKG_NET_FREEBSD_STACK)
  1116. /*start TFTP*/
  1117. tftpd_start(69, &fileops);
  1118. #endif
  1119. cyg_httpd_init_tcl_interpreter();
  1120. interp = httpstate.jim_interp;
  1121. Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log, NULL, NULL);
  1122. Jim_CreateCommand(httpstate.jim_interp, "reboot", zylinjtag_Jim_Command_reboot, NULL, NULL);
  1123. Jim_CreateCommand(httpstate.jim_interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
  1124. Jim_CreateCommand(httpstate.jim_interp, "zy1000_flash", zylinjtag_Jim_Command_flash, NULL, NULL);
  1125. Jim_CreateCommand(httpstate.jim_interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
  1126. Jim_CreateCommand(httpstate.jim_interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
  1127. Jim_CreateCommand(httpstate.jim_interp, "threads", zylinjtag_Jim_Command_threads, NULL, NULL);
  1128. Jim_CreateCommand(httpstate.jim_interp, "getmem", zylinjtag_Jim_Command_getmem, NULL, NULL);
  1129. Jim_CreateCommand(httpstate.jim_interp, "mac", zylinjtag_Jim_Command_mac, NULL, NULL);
  1130. Jim_CreateCommand(httpstate.jim_interp, "ip", zylinjtag_Jim_Command_ip, NULL, NULL);
  1131. Jim_CreateCommand(httpstate.jim_interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
  1132. Jim_CreateCommand(httpstate.jim_interp, "format_jffs2", zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
  1133. cyg_httpd_start();
  1134. webRunning = true;
  1135. diag_printf("Web server running\n");
  1136. int s;
  1137. struct ifreq ifr;
  1138. s = socket(AF_INET, SOCK_DGRAM, 0);
  1139. if (s >= 0)
  1140. {
  1141. strcpy(ifr.ifr_name, "eth0");
  1142. int res;
  1143. res = ioctl(s, SIOCGIFHWADDR, &ifr);
  1144. close(s);
  1145. if (res < 0)
  1146. {
  1147. diag_printf("Can't obtain MAC address\n");
  1148. reboot();
  1149. }
  1150. }
  1151. sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  1152. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
  1153. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
  1154. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
  1155. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
  1156. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
  1157. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
  1158. discover_message=alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
  1159. discover_launch();
  1160. }
  1161. static void
  1162. print_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)
  1163. {
  1164. writeLog = false;
  1165. serialLog = true;
  1166. char *infoStr = "unknown";
  1167. switch (exception)
  1168. {
  1169. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  1170. case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
  1171. infoStr = "undefined instruction";
  1172. break;
  1173. case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
  1174. infoStr = "software interrupt";
  1175. break;
  1176. case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
  1177. infoStr = "abort prefetch";
  1178. break;
  1179. case CYGNUM_HAL_VECTOR_ABORT_DATA:
  1180. infoStr = "abort data";
  1181. break;
  1182. #endif
  1183. default:
  1184. break;
  1185. }
  1186. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  1187. diag_printf("Dumping log\n---\n");
  1188. if (logCount >= logSize)
  1189. {
  1190. diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
  1191. }
  1192. diag_write(logBuffer, writePtr);
  1193. diag_printf("---\nLogdump complete.\n");
  1194. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  1195. diag_printf("\n---\nRebooting\n");
  1196. HAL_PLATFORM_RESET();
  1197. }
  1198. static void setHandler(cyg_code_t exception)
  1199. {
  1200. cyg_exception_handler_t *old_handler;
  1201. cyg_addrword_t old_data;
  1202. cyg_exception_set_handler(exception,
  1203. print_exception_handler,
  1204. 0,
  1205. &old_handler,
  1206. &old_data);
  1207. }
  1208. static cyg_thread zylinjtag_uart_thread_object;
  1209. static cyg_handle_t zylinjtag_uart_thread_handle;
  1210. static char uart_stack[4096];
  1211. static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
  1212. static char backwardBuffer[1024];
  1213. void setNoDelay(int session, int flag)
  1214. {
  1215. #if 1
  1216. // This decreases latency dramatically for e.g. GDB load which
  1217. // does not have a sliding window protocol
  1218. //
  1219. // Can cause *lots* of TCP/IP packets to be sent and it would have
  1220. // to be enabled/disabled on the fly to avoid the CPU being
  1221. // overloaded...
  1222. setsockopt(session, /* socket affected */
  1223. IPPROTO_TCP, /* set option at TCP level */
  1224. TCP_NODELAY, /* name of option */
  1225. (char *) &flag, /* the cast is historical
  1226. cruft */
  1227. sizeof(int)); /* length of option value */
  1228. #endif
  1229. }
  1230. struct
  1231. {
  1232. int req;
  1233. int actual;
  1234. int req2;
  1235. int actual2;
  1236. } tcpipSent[512 * 1024];
  1237. int cur;
  1238. static void
  1239. zylinjtag_uart(cyg_addrword_t data)
  1240. {
  1241. int so_reuseaddr_option = 1;
  1242. int fd;
  1243. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  1244. {
  1245. LOG_ERROR("error creating socket: %s", strerror(errno));
  1246. exit(-1);
  1247. }
  1248. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
  1249. struct sockaddr_in sin;
  1250. unsigned int address_size;
  1251. address_size = sizeof(sin);
  1252. memset(&sin, 0, sizeof(sin));
  1253. sin.sin_family = AF_INET;
  1254. sin.sin_addr.s_addr = INADDR_ANY;
  1255. sin.sin_port = htons(5555);
  1256. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  1257. {
  1258. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  1259. exit(-1);
  1260. }
  1261. if (listen(fd, 1) == -1)
  1262. {
  1263. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  1264. exit(-1);
  1265. }
  1266. // socket_nonblock(fd);
  1267. for (;;)
  1268. {
  1269. int session = accept(fd, (struct sockaddr *) &sin, &address_size);
  1270. if (session < 0)
  1271. {
  1272. continue;
  1273. }
  1274. setNoDelay(session, 1);
  1275. int oldopts = fcntl(session, F_GETFL, 0);
  1276. fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
  1277. int serHandle = open("/dev/ser0", O_RDWR | O_NONBLOCK);
  1278. if (serHandle < 0)
  1279. {
  1280. close(session);
  1281. continue;
  1282. }
  1283. #ifdef CYGPKG_PROFILE_GPROF
  1284. start_profile();
  1285. #endif
  1286. int actual = 0;
  1287. int actual2 = 0;
  1288. int pos, pos2;
  1289. pos = 0;
  1290. pos2 = 0;
  1291. cur = 0;
  1292. for (;;)
  1293. {
  1294. fd_set write_fds;
  1295. fd_set read_fds;
  1296. FD_ZERO(&write_fds);
  1297. FD_ZERO(&read_fds);
  1298. int fd_max = -1;
  1299. FD_SET(session, &read_fds);
  1300. fd_max = session;
  1301. FD_SET(serHandle, &read_fds);
  1302. if (serHandle > fd_max)
  1303. {
  1304. fd_max = serHandle;
  1305. }
  1306. /* Wait... */
  1307. cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
  1308. if ((actual == 0) && (actual2 == 0))
  1309. {
  1310. int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
  1311. if (retval <= 0)
  1312. {
  1313. break;
  1314. }
  1315. }
  1316. if (actual2 <= 0)
  1317. {
  1318. memset(backwardBuffer, 's', sizeof(backwardBuffer));
  1319. actual2=read(serHandle, backwardBuffer, sizeof(backwardBuffer));
  1320. if (actual2 < 0)
  1321. {
  1322. if (errno != EAGAIN)
  1323. {
  1324. goto closeSession;
  1325. }
  1326. actual2 = 0;
  1327. }
  1328. pos2 = 0;
  1329. }
  1330. int x = actual2;
  1331. int y = 0;
  1332. if (actual2 > 0)
  1333. {
  1334. int written = write(session, backwardBuffer + pos2, actual2);
  1335. if (written <= 0)
  1336. goto closeSession;
  1337. actual2 -= written;
  1338. pos2 += written;
  1339. y = written;
  1340. }
  1341. if (FD_ISSET(session, &read_fds)&&(sizeof(forwardBuffer)>actual))
  1342. {
  1343. // NB! Here it is important that we empty the TCP/IP read buffer
  1344. // to make transmission tick right
  1345. memmove(forwardBuffer, forwardBuffer + pos, actual);
  1346. pos = 0;
  1347. int t;
  1348. // this will block if there is no data at all
  1349. t=read_socket(session, forwardBuffer+actual, sizeof(forwardBuffer)-actual);
  1350. if (t <= 0)
  1351. {
  1352. goto closeSession;
  1353. }
  1354. actual += t;
  1355. }
  1356. int x2 = actual;
  1357. int y2 = 0;
  1358. if (actual > 0)
  1359. {
  1360. /* Do not put things into the serial buffer if it has something to send
  1361. * as that can cause a single byte to be sent at the time.
  1362. *
  1363. *
  1364. */
  1365. int written = write(serHandle, forwardBuffer + pos, actual);
  1366. if (written < 0)
  1367. {
  1368. if (errno != EAGAIN)
  1369. {
  1370. goto closeSession;
  1371. }
  1372. // The serial buffer is full
  1373. written = 0;
  1374. } else
  1375. {
  1376. actual -= written;
  1377. pos += written;
  1378. }
  1379. y2 = written;
  1380. }
  1381. if (cur < 1024)
  1382. {
  1383. tcpipSent[cur].req = x;
  1384. tcpipSent[cur].actual = y;
  1385. tcpipSent[cur].req2 = x2;
  1386. tcpipSent[cur].actual2 = y2;
  1387. cur++;
  1388. }
  1389. }
  1390. closeSession:
  1391. close(session);
  1392. close(serHandle);
  1393. int i;
  1394. for (i = 0; i < 1024; i++)
  1395. {
  1396. diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual, tcpipSent[i].req2, tcpipSent[i].actual2);
  1397. }
  1398. }
  1399. close(fd);
  1400. }
  1401. void startUart(void)
  1402. {
  1403. cyg_thread_create(1,
  1404. zylinjtag_uart,
  1405. (cyg_addrword_t)0,
  1406. "uart thread",
  1407. (void *)uart_stack,
  1408. sizeof(uart_stack),
  1409. &zylinjtag_uart_thread_handle,
  1410. &zylinjtag_uart_thread_object);
  1411. cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
  1412. cyg_thread_resume(zylinjtag_uart_thread_handle);
  1413. }
  1414. int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1415. {
  1416. static int current_baud = 38400;
  1417. if (argc == 0)
  1418. {
  1419. command_print(cmd_ctx, "%d", current_baud);
  1420. return ERROR_OK;
  1421. } else if (argc != 1)
  1422. {
  1423. return ERROR_INVALID_ARGUMENTS;
  1424. }
  1425. current_baud = atol(args[0]);
  1426. int baud;
  1427. switch (current_baud)
  1428. {
  1429. case 9600:
  1430. baud = CYGNUM_SERIAL_BAUD_9600;
  1431. break;
  1432. case 19200:
  1433. baud = CYGNUM_SERIAL_BAUD_19200;
  1434. break;
  1435. case 38400:
  1436. baud = CYGNUM_SERIAL_BAUD_38400;
  1437. break;
  1438. case 57600:
  1439. baud = CYGNUM_SERIAL_BAUD_57600;
  1440. break;
  1441. case 115200:
  1442. baud = CYGNUM_SERIAL_BAUD_115200;
  1443. break;
  1444. case 230400:
  1445. baud = CYGNUM_SERIAL_BAUD_230400;
  1446. break;
  1447. default:
  1448. command_print(cmd_ctx, "unsupported baudrate");
  1449. return ERROR_INVALID_ARGUMENTS;
  1450. }
  1451. cyg_serial_info_t buf;
  1452. cyg_uint32 len = 1;
  1453. //get existing serial configuration
  1454. len = sizeof(cyg_serial_info_t);
  1455. int err;
  1456. cyg_io_handle_t serial_handle;
  1457. err = cyg_io_lookup("/dev/ser0", &serial_handle);
  1458. if (err != ENOERR)
  1459. {
  1460. LOG_ERROR("/dev/ser0 not found\n");
  1461. return ERROR_FAIL;
  1462. }
  1463. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
  1464. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf, &len);
  1465. if (err != ENOERR)
  1466. {
  1467. command_print(cmd_ctx, "Failed to get serial port settings %d", err);
  1468. return ERROR_OK;
  1469. }
  1470. buf.baud = baud;
  1471. err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf, &len);
  1472. if (err != ENOERR)
  1473. {
  1474. command_print(cmd_ctx, "Failed to set serial port settings %d", err);
  1475. return ERROR_OK;
  1476. }
  1477. return ERROR_OK;
  1478. }
  1479. bool logAllToSerial = false;
  1480. /* boolean parameter stored on config */
  1481. bool boolParam(char *var)
  1482. {
  1483. bool result = false;
  1484. char *name = alloc_printf(ZYLIN_CONFIG_DIR "/%s", var);
  1485. if (name == NULL)
  1486. return result;
  1487. void *data;
  1488. int len;
  1489. if (loadFile(name, &data, &len) == ERROR_OK)
  1490. {
  1491. if (len > 1)
  1492. len = 1;
  1493. result = strncmp((char *) data, "1", len) == 0;
  1494. free(data);
  1495. }
  1496. free(name);
  1497. return result;
  1498. }
  1499. command_context_t *setup_command_handler();
  1500. int add_default_dirs(void)
  1501. {
  1502. add_script_search_dir(ZYLIN_CONFIG_DIR);
  1503. add_script_search_dir("/rom/lib/openocd");
  1504. add_script_search_dir("/rom");
  1505. return ERROR_OK;
  1506. }
  1507. static cyg_uint8 *ramblockdevice;
  1508. static const int ramblockdevice_size=4096*1024;
  1509. int main(int argc, char *argv[])
  1510. {
  1511. /* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
  1512. * need to allocate towards the end of the heap. */
  1513. ramblockdevice=(cyg_uint8 *)malloc(ramblockdevice_size);
  1514. memset(ramblockdevice, 0xff, ramblockdevice_size);
  1515. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  1516. setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
  1517. setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
  1518. setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
  1519. #endif
  1520. int err;
  1521. setPower(true); // on by default
  1522. atexit(keep_webserver);
  1523. err = mount("", "/ram", "ramfs");
  1524. if (err < 0)
  1525. {
  1526. diag_printf("unable to mount ramfs\n");
  1527. }
  1528. chdir("/ram");
  1529. char address[16];
  1530. sprintf(address, "%p", &filedata[0]);
  1531. err = mount(address, "/rom", "romfs");
  1532. if (err < 0)
  1533. {
  1534. diag_printf("unable to mount /rom\n");
  1535. }
  1536. err = mount("", "/log", "logfs");
  1537. if (err < 0)
  1538. {
  1539. diag_printf("unable to mount logfs\n");
  1540. }
  1541. err = mount("", "/tftp", "tftpfs");
  1542. if (err < 0)
  1543. {
  1544. diag_printf("unable to mount logfs\n");
  1545. }
  1546. log = fopen("/log/log", "w");
  1547. if (log == NULL)
  1548. {
  1549. diag_printf("Could not open log file /ram/log\n");
  1550. exit(-1);
  1551. }
  1552. diag_init_putc(_zylinjtag_diag_write_char);
  1553. // We want this in the log.
  1554. diag_printf("Zylin ZY1000. Copyright Zylin AS 2007-2008.\n");
  1555. diag_printf("%s\n", ZYLIN_OPENOCD_VERSION);
  1556. copydir("/rom", "/ram/cgi");
  1557. err = mount("/dev/flash1", "/config", "jffs2");
  1558. if (err < 0)
  1559. {
  1560. diag_printf("unable to mount jffs2, falling back to ram disk..\n");
  1561. err = mount("", "/config", "ramfs");
  1562. if (err<0)
  1563. {
  1564. diag_printf("unable to mount /config as ramdisk.\n");
  1565. reboot();
  1566. }
  1567. } else
  1568. {
  1569. /* are we using a ram disk instead of a flash disk? This is used
  1570. * for ZY1000 live demo...
  1571. *
  1572. * copy over flash disk to ram block device
  1573. */
  1574. if (boolParam("ramdisk"))
  1575. {
  1576. diag_printf("Unmounting /config from flash and using ram instead\n");
  1577. err=umount("/config");
  1578. if (err < 0)
  1579. {
  1580. diag_printf("unable to unmount jffs\n");
  1581. reboot();
  1582. }
  1583. err = mount("/dev/flash1", "/config2", "jffs2");
  1584. if (err < 0)
  1585. {
  1586. diag_printf("unable to mount jffs\n");
  1587. reboot();
  1588. }
  1589. err = mount("/dev/ram", "/config", "jffs2");
  1590. if (err < 0)
  1591. {
  1592. diag_printf("unable to mount ram block device\n");
  1593. reboot();
  1594. }
  1595. // copydir("/config2", "/config");
  1596. copyfile("/config2/ip", "/config/ip");
  1597. copydir("/config2/settings", "/config/settings");
  1598. umount("/config2");
  1599. } else
  1600. {
  1601. /* we're not going to use a ram block disk */
  1602. free(ramblockdevice);
  1603. }
  1604. }
  1605. mkdir(ZYLIN_CONFIG_DIR, 0777);
  1606. mkdir(ZYLIN_CONFIG_DIR "/target", 0777);
  1607. mkdir(ZYLIN_CONFIG_DIR "/event", 0777);
  1608. logAllToSerial = boolParam("logserial");
  1609. // We need the network & web server in case there is something wrong with
  1610. // the config files that invoke exit()
  1611. zylinjtag_startNetwork();
  1612. /* we're going to access the jim interpreter from here on... */
  1613. openocd_sleep_postlude();
  1614. startUart();
  1615. add_default_dirs();
  1616. /* initialize commandline interface */
  1617. command_context_t *cmd_ctx;
  1618. cmd_ctx = setup_command_handler();
  1619. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  1620. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  1621. register_command(cmd_ctx, NULL, "zy1000_version", handle_zy1000_version_command,
  1622. COMMAND_EXEC, "show zy1000 version numbers");
  1623. register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
  1624. "remove file");
  1625. register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
  1626. "same args as load_image, image stored in memory");
  1627. register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
  1628. "loads active fast load image to current target");
  1629. register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
  1630. "display file content");
  1631. register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
  1632. "truncate a file to 0 size");
  1633. register_command(cmd_ctx, NULL, "append_file", handle_append_command,
  1634. COMMAND_ANY, "append a variable number of strings to a file");
  1635. register_command(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
  1636. "power <on/off> - turn power switch to target on/off. No arguments - print status.");
  1637. register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
  1638. COMMAND_ANY, "display available ram memory");
  1639. register_command(cmd_ctx, NULL, "cp", handle_cp_command,
  1640. COMMAND_ANY, "copy a file <from> <to>");
  1641. #ifdef CYGPKG_PROFILE_GPROF
  1642. register_command(cmd_ctx, NULL, "ecosboard_profile", eCosBoard_handle_eCosBoard_profile_command,
  1643. COMMAND_ANY, NULL);
  1644. #endif
  1645. register_command(cmd_ctx, NULL, "uart", handle_uart_command,
  1646. COMMAND_ANY, "uart <baud> - forward uart on port 5555");
  1647. int errVal;
  1648. errVal = log_init(cmd_ctx);
  1649. if (errVal != ERROR_OK)
  1650. {
  1651. diag_printf("log_init() failed %d\n", errVal);
  1652. exit(-1);
  1653. }
  1654. set_log_output(cmd_ctx, log);
  1655. LOG_DEBUG("log init complete");
  1656. // diag_printf("Executing config files\n");
  1657. if (logAllToSerial)
  1658. {
  1659. diag_printf(ZYLIN_CONFIG_DIR "/logserial=1 => sending log output to serial port using \"debug_level 3\" as default.\n");
  1660. command_run_line(cmd_ctx, "debug_level 3");
  1661. }
  1662. zylinjtag_parse_config_file(cmd_ctx, "/rom/openocd.cfg");
  1663. // FIX!!! Yuk!
  1664. // diag_printf() is really invoked from many more places than we trust it
  1665. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  1666. //
  1667. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  1668. // fingers that it doesn't cause any crashes.
  1669. diag_printf("Init complete, GDB & telnet servers launched.\n");
  1670. command_set_output_handler(cmd_ctx, zy1000_configuration_output_handler_log, NULL);
  1671. if (!logAllToSerial)
  1672. {
  1673. serialLog = false;
  1674. }
  1675. /* handle network connections */
  1676. server_loop(cmd_ctx);
  1677. openocd_sleep_prelude();
  1678. /* shut server down */
  1679. server_quit();
  1680. /* free commandline interface */
  1681. command_done(cmd_ctx);
  1682. umount("/config");
  1683. exit(0);
  1684. for (;;);
  1685. }
  1686. cyg_int32
  1687. cyg_httpd_exec_cgi_tcl(char *file_name);
  1688. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  1689. {
  1690. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  1691. return 0;
  1692. }
  1693. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  1694. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  1695. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  1696. #include <pkgconf/system.h>
  1697. #include <pkgconf/hal.h>
  1698. #include <pkgconf/kernel.h>
  1699. #include <pkgconf/io_fileio.h>
  1700. #include <pkgconf/fs_rom.h>
  1701. #include <cyg/kernel/ktypes.h> // base kernel types
  1702. #include <cyg/infra/cyg_trac.h> // tracing macros
  1703. #include <cyg/infra/cyg_ass.h> // assertion macros
  1704. #include <unistd.h>
  1705. #include <sys/types.h>
  1706. #include <fcntl.h>
  1707. #include <sys/stat.h>
  1708. #include <errno.h>
  1709. #include <dirent.h>
  1710. #include <stdarg.h>
  1711. #include <stdio.h>
  1712. #include <stdlib.h>
  1713. #include <string.h>
  1714. #include <cyg/fileio/fileio.h>
  1715. #include <cyg/kernel/kapi.h>
  1716. #include <cyg/infra/diag.h>
  1717. //==========================================================================
  1718. // Eventually we want to eXecute In Place from the ROM in a protected
  1719. // environment, so we'll need executables to be aligned to a boundary
  1720. // suitable for MMU protection. A suitable boundary would be the 4k
  1721. // boundary in all the CPU architectures I am currently aware of.
  1722. // Forward definitions
  1723. // Filesystem operations
  1724. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1725. static int tftpfs_umount(cyg_mtab_entry *mte);
  1726. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1727. int mode, cyg_file *fte);
  1728. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1729. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1730. // File operations
  1731. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1732. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  1733. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  1734. //==========================================================================
  1735. // Filesystem table entries
  1736. // -------------------------------------------------------------------------
  1737. // Fstab entry.
  1738. // This defines the entry in the filesystem table.
  1739. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1740. // we should never block in any filesystem operations.
  1741. #if 1
  1742. FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
  1743. CYG_SYNCMODE_NONE,
  1744. tftpfs_mount,
  1745. tftpfs_umount,
  1746. tftpfs_open,
  1747. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1748. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1749. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1750. (cyg_fsop_rename *)cyg_fileio_erofs,
  1751. (cyg_fsop_link *)cyg_fileio_erofs,
  1752. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1753. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1754. (cyg_fsop_stat *)cyg_fileio_erofs,
  1755. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1756. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1757. #endif
  1758. // -------------------------------------------------------------------------
  1759. // mtab entry.
  1760. // This defines a single ROMFS loaded into ROM at the configured address
  1761. //
  1762. // MTAB_ENTRY( rom_mte, // structure name
  1763. // "/rom", // mount point
  1764. // "romfs", // FIlesystem type
  1765. // "", // hardware device
  1766. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1767. // );
  1768. // -------------------------------------------------------------------------
  1769. // File operations.
  1770. // This set of file operations are used for normal open files.
  1771. static cyg_fileops tftpfs_fileops =
  1772. {
  1773. tftpfs_fo_read,
  1774. tftpfs_fo_write,
  1775. tftpfs_fo_lseek,
  1776. (cyg_fileop_ioctl *)cyg_fileio_erofs,
  1777. cyg_fileio_seltrue,
  1778. tftpfs_fo_fsync,
  1779. tftpfs_fo_close,
  1780. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1781. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1782. (cyg_fileop_setinfo *)cyg_fileio_erofs,
  1783. };
  1784. // -------------------------------------------------------------------------
  1785. // tftpfs_mount()
  1786. // Process a mount request. This mainly finds root for the
  1787. // filesystem.
  1788. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1789. {
  1790. return ENOERR;
  1791. }
  1792. static int tftpfs_umount(cyg_mtab_entry *mte)
  1793. {
  1794. return ENOERR;
  1795. }
  1796. struct Tftp
  1797. {
  1798. int write;
  1799. int readFile;
  1800. cyg_uint8 *mem;
  1801. int actual;
  1802. char *server;
  1803. char *file;
  1804. };
  1805. static void freeTftp(struct Tftp *t)
  1806. {
  1807. if (t == NULL)
  1808. return;
  1809. if (t->mem)
  1810. free(t->mem);
  1811. if (t->server)
  1812. free(t->server);
  1813. if (t->file)
  1814. free(t->file);
  1815. free(t);
  1816. }
  1817. static const int tftpMaxSize = 8192 * 1024;
  1818. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1819. int mode, cyg_file *file)
  1820. {
  1821. struct Tftp *tftp;
  1822. tftp = malloc(sizeof(struct Tftp));
  1823. if (tftp == NULL)
  1824. return EMFILE;
  1825. memset(tftp, 0, sizeof(struct Tftp));
  1826. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1827. file->f_type = CYG_FILE_TYPE_FILE;
  1828. file->f_ops = &tftpfs_fileops;
  1829. file->f_offset = 0;
  1830. file->f_data = 0;
  1831. file->f_xops = 0;
  1832. tftp->mem = malloc(tftpMaxSize);
  1833. if (tftp->mem == NULL)
  1834. {
  1835. freeTftp(tftp);
  1836. return EMFILE;
  1837. }
  1838. char *server = strchr(name, '/');
  1839. if (server == NULL)
  1840. {
  1841. freeTftp(tftp);
  1842. return EMFILE;
  1843. }
  1844. tftp->server = malloc(server - name + 1);
  1845. if (tftp->server == NULL)
  1846. {
  1847. freeTftp(tftp);
  1848. return EMFILE;
  1849. }
  1850. strncpy(tftp->server, name, server - name);
  1851. tftp->server[server - name] = 0;
  1852. tftp->file = strdup(server + 1);
  1853. if (tftp->file == NULL)
  1854. {
  1855. freeTftp(tftp);
  1856. return EMFILE;
  1857. }
  1858. file->f_data = (CYG_ADDRWORD) tftp;
  1859. return ENOERR;
  1860. }
  1861. static int fetchTftp(struct Tftp *tftp)
  1862. {
  1863. if (!tftp->readFile)
  1864. {
  1865. int err;
  1866. tftp->actual = tftp_client_get( tftp->file, tftp->server, 0, tftp->mem, tftpMaxSize, TFTP_OCTET, &err);
  1867. if (tftp->actual < 0)
  1868. {
  1869. return EMFILE;
  1870. }
  1871. tftp->readFile = 1;
  1872. }
  1873. return ENOERR;
  1874. }
  1875. // -------------------------------------------------------------------------
  1876. // tftpfs_fo_write()
  1877. // Read data from file.
  1878. static int
  1879. tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1880. {
  1881. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1882. if (fetchTftp(tftp) != ENOERR)
  1883. return EMFILE;
  1884. int i;
  1885. off_t pos = fp->f_offset;
  1886. int resid = 0;
  1887. for (i = 0; i < uio->uio_iovcnt; i++)
  1888. {
  1889. cyg_iovec *iov = &uio->uio_iov[i];
  1890. char *buf = (char *) iov->iov_base;
  1891. off_t len = iov->iov_len;
  1892. if (len + pos > tftp->actual)
  1893. {
  1894. len = tftp->actual - pos;
  1895. }
  1896. resid += iov->iov_len - len;
  1897. memcpy(buf, tftp->mem + pos, len);
  1898. pos += len;
  1899. }
  1900. uio->uio_resid = resid;
  1901. fp->f_offset = pos;
  1902. return ENOERR;
  1903. }
  1904. static int
  1905. tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1906. {
  1907. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1908. int i;
  1909. off_t pos = fp->f_offset;
  1910. int resid = 0;
  1911. for (i = 0; i < uio->uio_iovcnt; i++)
  1912. {
  1913. cyg_iovec *iov = &uio->uio_iov[i];
  1914. char *buf = (char *) iov->iov_base;
  1915. off_t len = iov->iov_len;
  1916. if (len + pos > tftpMaxSize)
  1917. {
  1918. len = tftpMaxSize - pos;
  1919. }
  1920. resid += iov->iov_len - len;
  1921. memcpy(tftp->mem + pos, buf, len);
  1922. pos += len;
  1923. }
  1924. uio->uio_resid = resid;
  1925. fp->f_offset = pos;
  1926. tftp->write = 1;
  1927. return ENOERR;
  1928. }
  1929. static int
  1930. tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1931. {
  1932. int error = ENOERR;
  1933. return error;
  1934. }
  1935. // -------------------------------------------------------------------------
  1936. // romfs_fo_close()
  1937. // Close a file. We just clear out the data pointer.
  1938. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1939. {
  1940. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1941. int error = ENOERR;
  1942. if (tftp->write)
  1943. {
  1944. tftp_client_put( tftp->file, tftp->server, 0, tftp->mem, fp->f_offset, TFTP_OCTET, &error);
  1945. }
  1946. freeTftp(tftp);
  1947. fp->f_data = 0;
  1948. return error;
  1949. }
  1950. // -------------------------------------------------------------------------
  1951. // romfs_fo_lseek()
  1952. // Seek to a new file position.
  1953. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1954. {
  1955. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1956. off_t pos = *apos;
  1957. if (fetchTftp(tftp) != ENOERR)
  1958. return EMFILE;
  1959. switch (whence)
  1960. {
  1961. case SEEK_SET:
  1962. // Pos is already where we want to be.
  1963. break;
  1964. case SEEK_CUR:
  1965. // Add pos to current offset.
  1966. pos += fp->f_offset;
  1967. break;
  1968. case SEEK_END:
  1969. // Add pos to file size.
  1970. pos += tftp->actual;
  1971. break;
  1972. default:
  1973. return EINVAL;
  1974. }
  1975. // Check that pos is still within current file size, or at the
  1976. // very end.
  1977. if (pos < 0 || pos > tftp->actual)
  1978. return EINVAL;
  1979. // All OK, set fp offset and return new position.
  1980. *apos = fp->f_offset = pos;
  1981. return ENOERR;
  1982. }
  1983. void usleep(int us)
  1984. {
  1985. if (us > 10000)
  1986. cyg_thread_delay(us / 10000 + 1);
  1987. else
  1988. HAL_DELAY_US(us);
  1989. }
  1990. // Chunked version.
  1991. cyg_int32
  1992. show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1993. {
  1994. cyg_httpd_start_chunked("text");
  1995. if (logCount >= logSize)
  1996. {
  1997. cyg_httpd_write_chunked(logBuffer+logCount%logSize, logSize-logCount%logSize);
  1998. }
  1999. cyg_httpd_write_chunked(logBuffer, writePtr);
  2000. cyg_httpd_end_chunked();
  2001. return -1;
  2002. }
  2003. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  2004. // Filesystem operations
  2005. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  2006. static int logfs_umount(cyg_mtab_entry *mte);
  2007. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  2008. int mode, cyg_file *fte);
  2009. static int
  2010. logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  2011. // File operations
  2012. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  2013. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  2014. #include <cyg/io/devtab.h>
  2015. //==========================================================================
  2016. // Filesystem table entries
  2017. // -------------------------------------------------------------------------
  2018. // Fstab entry.
  2019. // This defines the entry in the filesystem table.
  2020. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  2021. // we should never block in any filesystem operations.
  2022. FSTAB_ENTRY( logfs_fste, "logfs", 0,
  2023. CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
  2024. logfs_mount,
  2025. logfs_umount,
  2026. logfs_open,
  2027. (cyg_fsop_unlink *)cyg_fileio_erofs,
  2028. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  2029. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  2030. (cyg_fsop_rename *)cyg_fileio_erofs,
  2031. (cyg_fsop_link *)cyg_fileio_erofs,
  2032. (cyg_fsop_opendir *)cyg_fileio_erofs,
  2033. (cyg_fsop_chdir *)cyg_fileio_erofs,
  2034. (cyg_fsop_stat *)cyg_fileio_erofs,
  2035. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  2036. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  2037. // -------------------------------------------------------------------------
  2038. // File operations.
  2039. // This set of file operations are used for normal open files.
  2040. static cyg_fileops logfs_fileops =
  2041. {
  2042. (cyg_fileop_read *)cyg_fileio_erofs,
  2043. (cyg_fileop_write *)logfs_fo_write,
  2044. (cyg_fileop_lseek *) cyg_fileio_erofs,
  2045. (cyg_fileop_ioctl *)cyg_fileio_erofs,
  2046. cyg_fileio_seltrue,
  2047. logfs_fo_fsync,
  2048. logfs_fo_close,
  2049. (cyg_fileop_fstat *)cyg_fileio_erofs,
  2050. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  2051. (cyg_fileop_setinfo *)cyg_fileio_erofs,
  2052. };
  2053. // -------------------------------------------------------------------------
  2054. // logfs_mount()
  2055. // Process a mount request. This mainly finds root for the
  2056. // filesystem.
  2057. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  2058. {
  2059. return ENOERR;
  2060. }
  2061. static int logfs_umount(cyg_mtab_entry *mte)
  2062. {
  2063. return ENOERR;
  2064. }
  2065. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  2066. int mode, cyg_file *file)
  2067. {
  2068. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  2069. file->f_type = CYG_FILE_TYPE_FILE;
  2070. file->f_ops = &logfs_fileops;
  2071. file->f_offset = 0;
  2072. file->f_data = 0;
  2073. file->f_xops = 0;
  2074. return ENOERR;
  2075. }
  2076. // -------------------------------------------------------------------------
  2077. // logfs_fo_write()
  2078. // Write data to file.
  2079. static int
  2080. logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  2081. {
  2082. int i;
  2083. for (i = 0; i < uio->uio_iovcnt; i++)
  2084. {
  2085. cyg_iovec *iov = &uio->uio_iov[i];
  2086. char *buf = (char *) iov->iov_base;
  2087. off_t len = iov->iov_len;
  2088. diag_write(buf, len);
  2089. }
  2090. uio->uio_resid = 0;
  2091. return ENOERR;
  2092. }
  2093. static int
  2094. logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  2095. {
  2096. return ENOERR;
  2097. }
  2098. // -------------------------------------------------------------------------
  2099. // romfs_fo_close()
  2100. // Close a file. We just clear out the data pointer.
  2101. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  2102. {
  2103. return ENOERR;
  2104. }
  2105. static bool
  2106. ramiodev_init( struct cyg_devtab_entry *tab )
  2107. {
  2108. return true;
  2109. }
  2110. static Cyg_ErrNo
  2111. ramiodev_bread( cyg_io_handle_t handle, void *buf, cyg_uint32 *len,
  2112. cyg_uint32 pos)
  2113. {
  2114. if (*len+pos>ramblockdevice_size)
  2115. {
  2116. *len=ramblockdevice_size-pos;
  2117. }
  2118. memcpy(buf, ramblockdevice+pos, *len);
  2119. return ENOERR;
  2120. }
  2121. static Cyg_ErrNo
  2122. ramiodev_bwrite( cyg_io_handle_t handle, const void *buf, cyg_uint32 *len,
  2123. cyg_uint32 pos )
  2124. {
  2125. if (((pos%4)!=0)||(((*len)%4)!=0))
  2126. {
  2127. diag_printf("Unaligned write %d %d!", pos, *len);
  2128. }
  2129. memcpy(ramblockdevice+pos, buf, *len);
  2130. return ENOERR;
  2131. }
  2132. static Cyg_ErrNo
  2133. ramiodev_get_config( cyg_io_handle_t handle,
  2134. cyg_uint32 key,
  2135. void* buf,
  2136. cyg_uint32* len)
  2137. {
  2138. switch (key) {
  2139. case CYG_IO_GET_CONFIG_FLASH_ERASE:
  2140. {
  2141. if ( *len != sizeof( cyg_io_flash_getconfig_erase_t ) )
  2142. return -EINVAL;
  2143. {
  2144. cyg_io_flash_getconfig_erase_t *e = (cyg_io_flash_getconfig_erase_t *)buf;
  2145. char *startpos = ramblockdevice + e->offset;
  2146. if (((e->offset%(64*1024))!=0)||((e->len%(64*1024))!=0))
  2147. {
  2148. diag_printf("Erease is not aligned %d %d\n", e->offset, e->len);
  2149. }
  2150. memset(startpos, 0xff, e->len);
  2151. e->flasherr = 0;
  2152. }
  2153. return ENOERR;
  2154. }
  2155. case CYG_IO_GET_CONFIG_FLASH_DEVSIZE:
  2156. {
  2157. if ( *len != sizeof( cyg_io_flash_getconfig_devsize_t ) )
  2158. return -EINVAL;
  2159. {
  2160. cyg_io_flash_getconfig_devsize_t *d =
  2161. (cyg_io_flash_getconfig_devsize_t *)buf;
  2162. d->dev_size = ramblockdevice_size;
  2163. }
  2164. return ENOERR;
  2165. }
  2166. case CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE:
  2167. {
  2168. cyg_io_flash_getconfig_blocksize_t *b =
  2169. (cyg_io_flash_getconfig_blocksize_t *)buf;
  2170. if ( *len != sizeof( cyg_io_flash_getconfig_blocksize_t ) )
  2171. return -EINVAL;
  2172. // offset unused for now
  2173. b->block_size = 64*1024;
  2174. return ENOERR;
  2175. }
  2176. default:
  2177. return -EINVAL;
  2178. }
  2179. }
  2180. static Cyg_ErrNo
  2181. ramiodev_set_config( cyg_io_handle_t handle,
  2182. cyg_uint32 key,
  2183. const void* buf,
  2184. cyg_uint32* len)
  2185. {
  2186. switch (key) {
  2187. default:
  2188. return -EINVAL;
  2189. }
  2190. } // ramiodev_set_config()
  2191. // get_config/set_config should be added later to provide the other flash
  2192. // operations possible, like erase etc.
  2193. BLOCK_DEVIO_TABLE( cyg_io_ramdev1_ops,
  2194. &ramiodev_bwrite,
  2195. &ramiodev_bread,
  2196. 0, // no select
  2197. &ramiodev_get_config,
  2198. &ramiodev_set_config
  2199. );
  2200. BLOCK_DEVTAB_ENTRY( cyg_io_ramdev1,
  2201. "/dev/ram",
  2202. 0,
  2203. &cyg_io_ramdev1_ops,
  2204. &ramiodev_init,
  2205. 0, // No lookup required
  2206. NULL );