gdbsend.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * (C) Copyright 2000
  3. * Murray Jensen <Murray.Jensen@csiro.au>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include "serial.h"
  31. #include "error.h"
  32. #include "remote.h"
  33. char *serialdev = "/dev/term/b";
  34. speed_t speed = B230400;
  35. int verbose = 0, docont = 0;
  36. unsigned long addr = 0x10000UL;
  37. int
  38. main(int ac, char **av)
  39. {
  40. int c, sfd, ifd;
  41. char *ifn, *image;
  42. struct stat ist;
  43. if ((pname = strrchr(av[0], '/')) == NULL)
  44. pname = av[0];
  45. else
  46. pname++;
  47. while ((c = getopt(ac, av, "a:b:cp:v")) != EOF)
  48. switch (c) {
  49. case 'a': {
  50. char *ep;
  51. addr = strtol(optarg, &ep, 0);
  52. if (ep == optarg || *ep != '\0')
  53. Error("can't decode address specified in -a option");
  54. break;
  55. }
  56. case 'b':
  57. if ((speed = cvtspeed(optarg)) == B0)
  58. Error("can't decode baud rate specified in -b option");
  59. break;
  60. case 'c':
  61. docont = 1;
  62. break;
  63. case 'p':
  64. serialdev = optarg;
  65. break;
  66. case 'v':
  67. verbose = 1;
  68. break;
  69. default:
  70. usage:
  71. fprintf(stderr,
  72. "Usage: %s [-a addr] [-b bps] [-c] [-p dev] [-v] imagefile\n",
  73. pname);
  74. exit(1);
  75. }
  76. if (optind != ac - 1)
  77. goto usage;
  78. ifn = av[optind++];
  79. if (verbose)
  80. fprintf(stderr, "Opening file and reading image...\n");
  81. if ((ifd = open(ifn, O_RDONLY)) < 0)
  82. Perror("can't open kernel image file '%s'", ifn);
  83. if (fstat(ifd, &ist) < 0)
  84. Perror("fstat '%s' failed", ifn);
  85. if ((image = (char *)malloc(ist.st_size)) == NULL)
  86. Perror("can't allocate %ld bytes for image", ist.st_size);
  87. if ((c = read(ifd, image, ist.st_size)) < 0)
  88. Perror("read of %d bytes from '%s' failed", ist.st_size, ifn);
  89. if (c != ist.st_size)
  90. Error("read of %ld bytes from '%s' failed (%d)", ist.st_size, ifn, c);
  91. if (close(ifd) < 0)
  92. Perror("close of '%s' failed", ifn);
  93. if (verbose)
  94. fprintf(stderr, "Opening serial port and sending image...\n");
  95. if ((sfd = serialopen(serialdev, speed)) < 0)
  96. Perror("open of serial device '%s' failed", serialdev);
  97. remote_desc = sfd;
  98. remote_reset();
  99. remote_write_bytes(addr, image, ist.st_size);
  100. if (docont) {
  101. if (verbose)
  102. fprintf(stderr, "[continue]");
  103. remote_continue();
  104. }
  105. if (serialclose(sfd) < 0)
  106. Perror("close of serial device '%s' failed", serialdev);
  107. if (verbose)
  108. fprintf(stderr, "Done.\n");
  109. return (0);
  110. }