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