OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // Returns the best guess of system modality. |
| 6 // |
| 7 #include <stdio.h> |
| 8 #include <stdlib.h> |
| 9 #include <string.h> |
| 10 #include <getopt.h> |
| 11 #include <err.h> |
| 12 |
| 13 #include "boot_mode.h" |
| 14 #include "developer_switch.h" |
| 15 #include "active_main_firmware.h" |
| 16 #include "bootloader_type.h" |
| 17 |
| 18 void usage(const char *self) { |
| 19 fprintf(stderr, |
| 20 "Usage: %s [options]\n\n" |
| 21 "Options:\n" |
| 22 " [empty] Prints the current mode\n" |
| 23 " -unsupported_as_developer\n" |
| 24 " -u Treats an unsupported platform as developer mode\n" |
| 25 " -ignore_bootloader\n" |
| 26 " -b Ignores the bootloader configuration\n" |
| 27 " -strict_match\n" |
| 28 " -s With -m, performs a strict match\n" |
| 29 " -in_mode\n" |
| 30 " -m [mode] Tests if the given mode is active instead of printing\n" |
| 31 "\n" |
| 32 "Mode:\n" |
| 33 " normal\n" |
| 34 " normal recovery\n" |
| 35 " developer\n" |
| 36 " developer recovery\n\n" |
| 37 "If supplied, the given text will be matched as a substring against\n" |
| 38 "the current mode. If it matches, the exit code will be zero, if not it\n" |
| 39 "will be non-zero.\n" |
| 40 "If -strict_match is supplied, then the given mode text must match the\n" |
| 41 "current running mode:\n" |
| 42 " -m developer matches 'developer' or 'developer recovery'\n" |
| 43 " -s -m developer matches only 'developer'\n\n", |
| 44 self); |
| 45 } |
| 46 |
| 47 // Use getopt instead of libbase.a so that this is sane to call from |
| 48 // chromeos_startup. |
| 49 static int flag_unsupported_as_developer = 0; |
| 50 static int flag_ignore_bootloader = 0; |
| 51 static int flag_strict_match = 0; |
| 52 static int flag_help = 0; |
| 53 static char *flag_mode = NULL; |
| 54 |
| 55 static void parse_args(int argc, char **argv) { |
| 56 int c = 0; |
| 57 while (c >= 0) { |
| 58 int option_index = 0; |
| 59 static const struct option long_options[] = { |
| 60 {"b", no_argument, &flag_ignore_bootloader, 1}, |
| 61 {"ignore_bootloader", no_argument, &flag_ignore_bootloader, 1}, |
| 62 {"h", no_argument, &flag_help, 1}, |
| 63 {"help", no_argument, &flag_help, 1}, |
| 64 {"m", required_argument, NULL, 'm'}, |
| 65 {"in_mode", required_argument, NULL, 'm'}, |
| 66 {"s", no_argument, &flag_strict_match, 1}, |
| 67 {"strict_match", no_argument, &flag_strict_match, 1}, |
| 68 {"u", no_argument, &flag_unsupported_as_developer, 1}, |
| 69 {"unsupported_as_developer", no_argument, &flag_unsupported_as_developer, |
| 70 1}, |
| 71 {0, 0, 0, 0} |
| 72 }; |
| 73 |
| 74 c = getopt_long_only(argc, argv, "", long_options, &option_index); |
| 75 switch (c) { |
| 76 case 'm': |
| 77 flag_mode = optarg; |
| 78 break; |
| 79 case '?': |
| 80 flag_help = 1; |
| 81 c = -1; |
| 82 break; |
| 83 } |
| 84 } |
| 85 |
| 86 if (flag_strict_match && flag_mode == NULL) { |
| 87 flag_help = 1; |
| 88 warnx("-s requires -m [mode]"); |
| 89 return; |
| 90 } |
| 91 |
| 92 if (optind < argc) { |
| 93 fprintf(stderr, "Too many free arguments: %d\n", argc - optind); |
| 94 flag_help = 1; |
| 95 } |
| 96 } |
| 97 |
| 98 int main(int argc, char **argv) { |
| 99 // Make the exit codes readable. |
| 100 enum { kMatch = 0, kNoMatch }; |
| 101 |
| 102 parse_args(argc, argv); |
| 103 if (flag_help) { |
| 104 usage(argv[0]); |
| 105 return kNoMatch; |
| 106 } |
| 107 |
| 108 cros_boot_mode::BootMode mode; |
| 109 mode.Initialize(flag_unsupported_as_developer, !flag_ignore_bootloader); |
| 110 |
| 111 if (flag_mode) { |
| 112 bool match = false; |
| 113 // flag_mode may be normal, developer, or even "normal recovery" or |
| 114 // "developer recovery". We shorten the comparison by using the |
| 115 // test mode length. strict_match will enforce the reverse. |
| 116 if (flag_strict_match) { |
| 117 if (strlen(mode.mode_text()) != strlen(flag_mode)) |
| 118 return kNoMatch; // no match |
| 119 } |
| 120 if (strstr(mode.mode_text(), flag_mode)) |
| 121 match = true; |
| 122 |
| 123 return (match ? kMatch : kNoMatch); |
| 124 } |
| 125 |
| 126 printf("%s\n", mode.mode_text()); |
| 127 return 0; |
| 128 } |
OLD | NEW |