blob: 459e17db4cd332a90766e71578dedf6aa90818fd [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2014 Brian Swetland
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <sys/types.h>
31
32#include "liblkboot.h"
33
34void usage(void) {
35 fprintf(stderr,
36"usage: lkboot <hostname> <command> ...\n"
37"\n"
38" lkboot <hostname> flash <partition> <filename>\n"
39" lkboot <hostname> erase <partition>\n"
40" lkboot <hostname> remove <partition>\n"
41" lkboot <hostname> fpga <bitfile>\n"
42" lkboot <hostname> boot <binary>\n"
43" lkboot <hostname> getsysparam <name>\n"
44" lkboot <hostname> reboot\n"
45" lkboot <hostname> :<commandname> [ <arg>* ]\n"
46"\n"
47"NOTE: If <hostname> is 'jtag', lkboot will attempt to use\n"
48" a tool 'zynq-dcc' to communicate with the device.\n"
49" Make sure it is in your path.\n"
50"\n"
51 );
52 exit(1);
53}
54
55void printsysparam(void *data, int len) {
56 unsigned char *x = data;
57 int i;
58 for (i = 0; i < len; i++) {
59 if ((x[i] < ' ') || (x[1] > 127)) goto printhex;
60 }
61 write(STDERR_FILENO, "\"", 1);
62 write(STDERR_FILENO, data, len);
63 write(STDERR_FILENO, "\"\n", 2);
64 return;
65printhex:
66 fprintf(stderr, "[");
67 for (i = 0; i < len; i++) fprintf(stderr, " %02x", x[i]);
68 fprintf(stderr, " ]\n");
69}
70
71int main(int argc, char **argv) {
72 const char *host = argv[1];
73 const char *cmd = argv[2];
74 const char *args = argv[3];
75 const char *fn = NULL;
76 int fd = -1;
77
78 if (argc == 3) {
79 if (!strcmp(cmd, "reboot")) {
80 return lkboot_txn(host, cmd, fd, "");
81 } else if (cmd[0] == ':') {
82 return lkboot_txn(host, cmd + 1, fd, "");
83 } else {
84 usage();
85 }
86 }
87
88 if (argc < 4) usage();
89
90 if (!strcmp(cmd, "flash")) {
91 if (argc < 5) usage();
92 fn = argv[4];
93 } else if (!strcmp(cmd, "fpga")) {
94 fn = args;
95 args = "";
96 } else if (!strcmp(cmd, "boot")) {
97 fn = args;
98 args = "";
99 } else if (!strcmp(cmd, "erase")) {
100 } else if (!strcmp(cmd, "remove")) {
101 } else if (!strcmp(cmd, "getsysparam")) {
102 if (lkboot_txn(host, cmd, -1, args) == 0) {
103 void *rbuf = NULL;
104 printsysparam(rbuf, lkboot_get_reply(&rbuf));
105 return 0;
106 } else {
107 return -1;
108 }
109 } else if (cmd[0] == ':') {
110 return lkboot_txn(host, cmd + 1, -1, args);
111 } else {
112 usage();
113 }
114 if (fn) {
115 if ((fd = open(fn, O_RDONLY)) < 0) {
116 fprintf(stderr, "error; cannot open '%s'\n", fn);
117 return -1;
118 }
119 }
120 return lkboot_txn(host, cmd, fd, args);
121}
122
123// vim: noexpandtab