1 /*
2 * Copyright (c) 2014 Brian Swetland
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16
17 #include "liblkboot.h"
18
usage(void)19 void usage(void) {
20 fprintf(stderr,
21 "usage: lkboot <hostname> <command> ...\n"
22 "\n"
23 " lkboot <hostname> flash <partition> <filename>\n"
24 " lkboot <hostname> erase <partition>\n"
25 " lkboot <hostname> remove <partition>\n"
26 " lkboot <hostname> fpga <bitfile>\n"
27 " lkboot <hostname> boot <binary>\n"
28 " lkboot <hostname> getsysparam <name>\n"
29 " lkboot <hostname> reboot\n"
30 " lkboot <hostname> :<commandname> [ <arg>* ]\n"
31 "\n"
32 "NOTE: If <hostname> is 'jtag', lkboot will attempt to use\n"
33 " a tool 'zynq-dcc' to communicate with the device.\n"
34 " Make sure it is in your path.\n"
35 "\n"
36 );
37 exit(1);
38 }
39
printsysparam(void * data,int len)40 void printsysparam(void *data, int len) {
41 unsigned char *x = data;
42 int i;
43 for (i = 0; i < len; i++) {
44 if ((x[i] < ' ') || (x[1] > 127)) goto printhex;
45 }
46 write(STDERR_FILENO, "\"", 1);
47 write(STDERR_FILENO, data, len);
48 write(STDERR_FILENO, "\"\n", 2);
49 return;
50 printhex:
51 fprintf(stderr, "[");
52 for (i = 0; i < len; i++) fprintf(stderr, " %02x", x[i]);
53 fprintf(stderr, " ]\n");
54 }
55
main(int argc,char ** argv)56 int main(int argc, char **argv) {
57 const char *host = argv[1];
58 const char *cmd = argv[2];
59 const char *args = argv[3];
60 const char *fn = NULL;
61 int fd = -1;
62
63 if (argc == 3) {
64 if (!strcmp(cmd, "reboot")) {
65 return lkboot_txn(host, cmd, fd, "");
66 } else if (cmd[0] == ':') {
67 return lkboot_txn(host, cmd + 1, fd, "");
68 } else {
69 usage();
70 }
71 }
72
73 if (argc < 4) usage();
74
75 if (!strcmp(cmd, "flash")) {
76 if (argc < 5) usage();
77 fn = argv[4];
78 } else if (!strcmp(cmd, "fpga")) {
79 fn = args;
80 args = "";
81 } else if (!strcmp(cmd, "boot")) {
82 fn = args;
83 args = "";
84 } else if (!strcmp(cmd, "erase")) {
85 } else if (!strcmp(cmd, "remove")) {
86 } else if (!strcmp(cmd, "getsysparam")) {
87 if (lkboot_txn(host, cmd, -1, args) == 0) {
88 void *rbuf = NULL;
89 printsysparam(rbuf, lkboot_get_reply(&rbuf));
90 return 0;
91 } else {
92 return -1;
93 }
94 } else if (cmd[0] == ':') {
95 return lkboot_txn(host, cmd + 1, -1, args);
96 } else {
97 usage();
98 }
99 if (fn) {
100 if ((fd = open(fn, O_RDONLY)) < 0) {
101 fprintf(stderr, "error; cannot open '%s'\n", fn);
102 return -1;
103 }
104 }
105 return lkboot_txn(host, cmd, fd, args);
106 }
107