Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 1 | /* |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 2 | * Copyright 2013 The Chromium OS Authors. All rights reserved. |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "futility.h" |
| 18 | |
Bill Richardson | 6db8c75 | 2013-04-05 13:30:43 -0700 | [diff] [blame] | 19 | #define MYNAME_S MYNAME "_s" |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 20 | |
| 21 | /* File to use for logging, if present */ |
| 22 | #define LOGFILE "/tmp/futility.log" |
| 23 | |
| 24 | /* Normally logging will only happen if the logfile already exists. Uncomment |
| 25 | * this to force log file creation (and thus logging) always. */ |
| 26 | /* #define FORCE_LOGGING_ON */ |
| 27 | |
| 28 | /******************************************************************************/ |
| 29 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 30 | static const char *const usage = "\n\ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 31 | Usage: " MYNAME " PROGRAM|COMMAND [args...]\n\ |
| 32 | \n\ |
| 33 | This is the unified firmware utility, which will eventually replace\n\ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 34 | most of the distinct verified boot tools formerly produced by the\n\ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 35 | vboot_reference package.\n\ |
| 36 | \n\ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 37 | When symlinked under the name of one of those previous tools, should\n\ |
| 38 | fully implement the original behavior. It can also be invoked directly\n\ |
| 39 | as " MYNAME ", followed by the original name as the first argument.\n\ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 40 | \n\ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 41 | In either case it will append some usage information to " LOGFILE "\n\ |
| 42 | (iff that file exists), to help improve coverage and correctness.\n\ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 43 | \n"; |
| 44 | |
Bill Richardson | 7d028c4 | 2014-07-12 10:46:56 -0700 | [diff] [blame] | 45 | static int do_help(int argc, char *argv[]) |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 46 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 47 | const struct futil_cmd_t *const *cmd; |
| 48 | int i; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 49 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 50 | fputs(usage, stdout); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 51 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 52 | printf("The following commands are built-in:\n\n"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 53 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 54 | for (cmd = futil_cmds; *cmd; cmd++) |
| 55 | printf(" %-20s %s\n", (*cmd)->name, (*cmd)->shorthelp); |
| 56 | printf("\n"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 57 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 58 | if (argc) { |
| 59 | printf("FYI, you added these args that I'm ignoring:\n"); |
| 60 | for (i = 0; i < argc; i++) |
| 61 | printf("argv[%d] = %s\n", i, argv[i]); |
| 62 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 63 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 64 | return 0; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 65 | } |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 66 | |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 67 | DECLARE_FUTIL_COMMAND(help, do_help, |
| 68 | "Show a bit of help (you're looking at it)"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 69 | |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 70 | /* |
| 71 | * These are built-in functions that we'd like to abandon completely someday. |
| 72 | * TODO: If no one complains, get rid of them. |
| 73 | */ |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 74 | static const char *const dep_cmds[] = { |
| 75 | "dev_sign_file", |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 78 | static const char *const dep_usage = "\n\ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 79 | The program \"%s\" is deprecated and may go away soon.\n\ |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 80 | \n\ |
| 81 | If you feel this is in error, please open a bug at\n\ |
| 82 | \n\ |
| 83 | http://dev.chromium.org/for-testers/bug-reporting-guidelines\n\ |
| 84 | \n\ |
| 85 | In the meantime, you may continue to use the program by invoking it as\n\ |
| 86 | \n\ |
| 87 | " MYNAME " %s [...]\n\ |
| 88 | \n"; |
| 89 | |
| 90 | static void deprecated(const char *depname) |
| 91 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 92 | fprintf(stderr, dep_usage, depname, depname); |
| 93 | exit(1); |
Bill Richardson | e155044 | 2014-07-17 11:32:17 -0700 | [diff] [blame] | 94 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 95 | |
| 96 | /******************************************************************************/ |
| 97 | /* Logging stuff */ |
| 98 | |
| 99 | static int log_fd = -1; |
| 100 | |
| 101 | /* Write the string and a newline. Silently give up on errors */ |
| 102 | static void log_str(char *str) |
| 103 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 104 | int len, done, n; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 105 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 106 | if (log_fd < 0) |
| 107 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 108 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 109 | if (!str) |
| 110 | str = "(NULL)"; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 111 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 112 | len = strlen(str); |
| 113 | if (len == 0) { |
| 114 | str = "(EMPTY)"; |
| 115 | len = strlen(str); |
| 116 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 117 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 118 | for (done = 0; done < len; done += n) { |
| 119 | n = write(log_fd, str + done, len - done); |
| 120 | if (n < 0) |
| 121 | return; |
| 122 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 123 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 124 | if (write(log_fd, "\n", 1) < 0) |
| 125 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void log_close(void) |
| 129 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 130 | struct flock lock; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 131 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 132 | if (log_fd >= 0) { |
| 133 | memset(&lock, 0, sizeof(lock)); |
| 134 | lock.l_type = F_UNLCK; |
| 135 | lock.l_whence = SEEK_SET; |
| 136 | if (fcntl(log_fd, F_SETLKW, &lock)) |
| 137 | perror("Unable to unlock log file"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 138 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 139 | close(log_fd); |
| 140 | log_fd = -1; |
| 141 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void log_open(void) |
| 145 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 146 | struct flock lock; |
| 147 | int ret; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 148 | |
| 149 | #ifdef FORCE_LOGGING_ON |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 150 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 151 | #else |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 152 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 153 | #endif |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 154 | if (log_fd < 0) { |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 155 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 156 | if (errno != EACCES) |
| 157 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 158 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 159 | /* Permission problems should improve shortly ... */ |
| 160 | sleep(1); |
| 161 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666); |
| 162 | if (log_fd < 0) /* Nope, they didn't */ |
| 163 | return; |
| 164 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 165 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 166 | /* Let anyone have a turn */ |
| 167 | fchmod(log_fd, 0666); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 168 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 169 | /* But only one at a time */ |
| 170 | memset(&lock, 0, sizeof(lock)); |
| 171 | lock.l_type = F_WRLCK; |
| 172 | lock.l_whence = SEEK_END; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 173 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 174 | ret = fcntl(log_fd, F_SETLKW, &lock); /* this blocks */ |
| 175 | if (ret < 0) |
| 176 | log_close(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | #define CALLER_PREFIX "CALLER:" |
| 180 | static void log_args(int argc, char *argv[]) |
| 181 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 182 | int i; |
| 183 | ssize_t r; |
| 184 | pid_t parent; |
| 185 | char buf[80]; |
| 186 | char str_caller[PATH_MAX + sizeof(CALLER_PREFIX)] = CALLER_PREFIX; |
| 187 | char *truename = str_caller + sizeof(CALLER_PREFIX) - 1; |
| 188 | /* Note: truename starts on the \0 from CALLER_PREFIX, so we can write |
| 189 | * PATH_MAX chars into truename and still append a \0 at the end. */ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 190 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 191 | log_open(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 192 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 193 | /* delimiter */ |
| 194 | log_str("##### HEY #####"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 195 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 196 | /* Can we tell who called us? */ |
| 197 | parent = getppid(); |
| 198 | snprintf(buf, sizeof(buf), "/proc/%d/exe", parent); |
| 199 | r = readlink(buf, truename, PATH_MAX); |
| 200 | if (r >= 0) { |
| 201 | truename[r] = '\0'; |
| 202 | log_str(str_caller); |
| 203 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 204 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 205 | /* Now log the stuff about ourselves */ |
| 206 | for (i = 0; i < argc; i++) |
| 207 | log_str(argv[i]); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 208 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 209 | log_close(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 212 | /******************************************************************************/ |
| 213 | /* Here we go */ |
| 214 | |
| 215 | int main(int argc, char *argv[], char *envp[]) |
| 216 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 217 | char *fullname, *progname; |
| 218 | char truename[PATH_MAX]; |
| 219 | char buf[80]; |
| 220 | pid_t myproc; |
| 221 | ssize_t r; |
| 222 | const struct futil_cmd_t *const *cmd; |
| 223 | int i; |
| 224 | int via_symlink = 0; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 225 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 226 | log_args(argc, argv); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 227 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 228 | /* How were we invoked? */ |
| 229 | fullname = strdup(argv[0]); |
| 230 | progname = strrchr(argv[0], '/'); |
| 231 | if (progname) |
| 232 | progname++; |
| 233 | else |
| 234 | progname = argv[0]; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 235 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 236 | /* Invoked directly by name */ |
| 237 | if (0 == strcmp(progname, MYNAME) || 0 == strcmp(progname, MYNAME_S)) { |
| 238 | if (argc < 2) { /* must have an argument */ |
| 239 | do_help(0, 0); |
| 240 | exit(1); |
| 241 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 242 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 243 | /* We can just pass the rest along, then */ |
| 244 | argc--; |
| 245 | argv++; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 246 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 247 | /* So now what function do we want to invoke? */ |
| 248 | progname = strrchr(argv[0], '/'); |
| 249 | if (progname) |
| 250 | progname++; |
| 251 | else |
| 252 | progname = argv[0]; |
| 253 | } else { /* Invoked by symlink */ |
| 254 | via_symlink = 1; |
| 255 | /* Block any deprecated functions. */ |
| 256 | for (i = 0; i < ARRAY_SIZE(dep_cmds); i++) |
| 257 | if (0 == strcmp(dep_cmds[i], progname)) |
| 258 | deprecated(progname); |
| 259 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 260 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 261 | /* See if it's asking for something we know how to do ourselves */ |
| 262 | for (cmd = futil_cmds; *cmd; cmd++) |
| 263 | if (0 == strcmp((*cmd)->name, progname)) |
| 264 | return (*cmd)->handler(argc, argv); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 265 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 266 | /* Nope */ |
| 267 | if (!via_symlink) { |
| 268 | do_help(0, 0); |
| 269 | exit(1); |
| 270 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 271 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 272 | /* Complain about bogus symlink */ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 273 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 274 | myproc = getpid(); |
| 275 | snprintf(buf, sizeof(buf), "/proc/%d/exe", myproc); |
| 276 | r = readlink(buf, truename, PATH_MAX - 1); |
| 277 | if (r < 0) { |
| 278 | fprintf(stderr, "%s is lost: %s => %s: %s\n", MYNAME, argv[0], |
| 279 | buf, strerror(errno)); |
| 280 | exit(1); |
| 281 | } |
| 282 | truename[r] = '\0'; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 283 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 284 | fprintf(stderr, "\n\ |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 285 | The program\n\n %s\n\nis a symlink to\n\n %s\n\ |
| 286 | \n\ |
| 287 | However, " MYNAME " doesn't know how to implement that function.\n\ |
| 288 | \n\ |
| 289 | This is probably an error in your installation. If the problem persists\n\ |
| 290 | after a fresh checkout/build/install, please open a bug at\n\ |
| 291 | \n\ |
| 292 | http://dev.chromium.org/for-testers/bug-reporting-guidelines\n\ |
| 293 | \n", fullname, truename); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 294 | return 1; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 295 | } |