gdbsend.c 2.4 KB

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