aboutsummaryrefslogtreecommitdiff
path: root/bootloader/bootloader.c
diff options
context:
space:
mode:
Diffstat (limited to 'bootloader/bootloader.c')
-rw-r--r--bootloader/bootloader.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/bootloader/bootloader.c b/bootloader/bootloader.c
new file mode 100644
index 0000000..d83db57
--- /dev/null
+++ b/bootloader/bootloader.c
@@ -0,0 +1,94 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2011 Dimitri Sokolyuk <demon@dim13.org>
+ *
+ * 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 <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include "bootloader.h"
+
+int
+transfer(int fd, struct page *p, int pages, int pagesize)
+{
+ int n, off;
+
+ 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);
+ for (off = 0; off < pagesize; off++)
+ put(p[n].data[off], fd);
+ assert (get(fd) == 'd');
+ }
+ }
+ 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;
+}