/* $Id$ */ /* * Copyright (c) 2011 Dimitri Sokolyuk * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include "bootloader.h" int transfer(int fd, struct page *p, int pages, int pagesize) { int n, off; unsigned char sum; fprintf(stderr, "trying to reboot device\n"); usleep(500); put('R', fd); /* try to reboot */ usleep(500); fprintf(stderr, "waiting for bootloader "); do { put('P', fd); twiddle(); } while (get(fd) != 'p'); fprintf(stderr, "\nwriting: "); for (n = 0; n < pages; n++) { fprintf(stderr, "%c", ".o"[p[n].dirty]); if (p[n].dirty) { put('D', fd); put(n, fd); sum = n; for (off = 0; off < pagesize; off++) { put(p[n].data[off], fd); sum += p[n].data[off]; } put(sum, fd); if (get(fd) != 'd') n--; /* resend */ } } fprintf(stderr, "\nrebooting\n"); put('R', fd); return 0; } int main(int argc, char **argv) { struct page *p; char *dev = "/dev/ttyU0"; int c, fd; while ((c = getopt(argc, argv, "f:")) != -1) switch (c) { case 'f': dev = strdup(optarg); break; default: break; } argc -= optind; argv += optind; if (argc != 1) return -1; p = rdhex(argv[0], PAGENUM, PAGESIZE); assert(p); assert(p[120].dirty == 0); /* protect firmware */ fd = open_tty(dev); if (fd == -1) perror(dev); transfer(fd, p, PAGENUM, PAGESIZE); close(fd); freehex(p, PAGENUM); return 0; }