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.
 
 
 
 
 
 

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