gdbsend.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * (C) Copyright 2000
  3. * Murray Jensen <Murray.Jensen@csiro.au>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include "serial.h"
  15. #include "error.h"
  16. #include "remote.h"
  17. char *serialdev = "/dev/term/b";
  18. speed_t speed = B230400;
  19. int verbose = 0, docont = 0;
  20. unsigned long addr = 0x10000UL;
  21. int
  22. main(int ac, char **av)
  23. {
  24. int c, sfd, ifd;
  25. char *ifn, *image;
  26. struct stat ist;
  27. if ((pname = strrchr(av[0], '/')) == NULL)
  28. pname = av[0];
  29. else
  30. pname++;
  31. while ((c = getopt(ac, av, "a:b:cp:v")) != EOF)
  32. switch (c) {
  33. case 'a': {
  34. char *ep;
  35. addr = strtol(optarg, &ep, 0);
  36. if (ep == optarg || *ep != '\0')
  37. Error("can't decode address specified in -a option");
  38. break;
  39. }
  40. case 'b':
  41. if ((speed = cvtspeed(optarg)) == B0)
  42. Error("can't decode baud rate specified in -b option");
  43. break;
  44. case 'c':
  45. docont = 1;
  46. break;
  47. case 'p':
  48. serialdev = optarg;
  49. break;
  50. case 'v':
  51. verbose = 1;
  52. break;
  53. default:
  54. usage:
  55. fprintf(stderr,
  56. "Usage: %s [-a addr] [-b bps] [-c] [-p dev] [-v] imagefile\n",
  57. pname);
  58. exit(1);
  59. }
  60. if (optind != ac - 1)
  61. goto usage;
  62. ifn = av[optind++];
  63. if (verbose)
  64. fprintf(stderr, "Opening file and reading image...\n");
  65. if ((ifd = open(ifn, O_RDONLY)) < 0)
  66. Perror("can't open kernel image file '%s'", ifn);
  67. if (fstat(ifd, &ist) < 0)
  68. Perror("fstat '%s' failed", ifn);
  69. if ((image = (char *)malloc(ist.st_size)) == NULL)
  70. Perror("can't allocate %ld bytes for image", ist.st_size);
  71. if ((c = read(ifd, image, ist.st_size)) < 0)
  72. Perror("read of %d bytes from '%s' failed", ist.st_size, ifn);
  73. if (c != ist.st_size)
  74. Error("read of %ld bytes from '%s' failed (%d)", ist.st_size, ifn, c);
  75. if (close(ifd) < 0)
  76. Perror("close of '%s' failed", ifn);
  77. if (verbose)
  78. fprintf(stderr, "Opening serial port and sending image...\n");
  79. if ((sfd = serialopen(serialdev, speed)) < 0)
  80. Perror("open of serial device '%s' failed", serialdev);
  81. remote_desc = sfd;
  82. remote_reset();
  83. remote_write_bytes(addr, image, ist.st_size);
  84. if (docont) {
  85. if (verbose)
  86. fprintf(stderr, "[continue]");
  87. remote_continue();
  88. }
  89. if (serialclose(sfd) < 0)
  90. Perror("close of serial device '%s' failed", serialdev);
  91. if (verbose)
  92. fprintf(stderr, "Done.\n");
  93. return (0);
  94. }