OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2010, Google Inc. |
| 3 * All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are |
| 7 * met: |
| 8 * |
| 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * * Redistributions in binary form must reproduce the above |
| 12 * copyright notice, this list of conditions and the following disclaimer |
| 13 * in the documentation and/or other materials provided with the |
| 14 * distribution. |
| 15 * * Neither the name of Google Inc. nor the names of its |
| 16 * contributors may be used to endorse or promote products derived from |
| 17 * this software without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 * |
| 31 * Alternatively, this software may be distributed under the terms of the |
| 32 * GNU General Public License ("GPL") version 2 as published by the Free |
| 33 * Software Foundation. |
| 34 */ |
| 35 |
| 36 /* |
| 37 * Support Chrome OS Verify Boot |
| 38 */ |
| 39 #include <common.h> |
| 40 #include <command.h> |
| 41 #include <part.h> |
| 42 #include <boot_device.h> |
| 43 |
| 44 #define USAGE(ret, cmdtp, fmt, args...) do { \ |
| 45 printf(fmt, args); \ |
| 46 cmd_usage(cmdtp); \ |
| 47 return (ret); \ |
| 48 } while (0); |
| 49 |
| 50 block_dev_desc_t *get_bootdev(void); |
| 51 int set_bootdev(char *ifname, int dev, int part); |
| 52 |
| 53 int do_cros (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 54 int do_bootdev (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 55 int do_cros_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 56 |
| 57 U_BOOT_CMD(cros, CONFIG_SYS_MAXARGS, 1, do_cros, |
| 58 "perform action (try \"cros help\")", |
| 59 "[action [args...]]\n - perform action with arguments" |
| 60 ); |
| 61 |
| 62 cmd_tbl_t cmd_cros_sub[] = { |
| 63 U_BOOT_CMD_MKENT(bootdev, 4, 1, do_bootdev, |
| 64 "show/set/read/write boot device", |
| 65 "[sub-action args...]\n - perform sub-action\n" |
| 66 "\n" |
| 67 "Subactions:\n" |
| 68 " - show boot device (when no sub-action)\n" |
| 69 "set iface dev [part]\n - set boot device\n" |
| 70 "read addr block count\n - read from boot device\n" |
| 71 "write addr block count\n - write to boot device"), |
| 72 U_BOOT_CMD_MKENT(help, 1, 1, do_cros_help, |
| 73 "show this message", |
| 74 "[action]") |
| 75 }; |
| 76 |
| 77 int do_cros(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 78 { |
| 79 cmd_tbl_t *c; |
| 80 |
| 81 if (argc < 2) |
| 82 USAGE(0, cmdtp, "Missing action\n"); |
| 83 |
| 84 c = find_cmd_tbl(argv[1], &cmd_cros_sub[0], ARRAY_SIZE(cmd_cros_sub)); |
| 85 if (!c) |
| 86 USAGE(1, cmdtp, "Unrecognized action: %s\n", argv[1]); |
| 87 |
| 88 return c->cmd(c, flag, argc - 1, argv + 1); |
| 89 } |
| 90 |
| 91 int do_bootdev(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 92 { |
| 93 enum { |
| 94 SET, READ, WRITE |
| 95 } opcode; |
| 96 |
| 97 block_dev_desc_t *dev_desc; |
| 98 int dev, part, retcode; |
| 99 uint64_t blk, cnt; |
| 100 void *buf; |
| 101 |
| 102 if (argc < 2) { /* show boot device information */ |
| 103 if ((dev_desc = get_bootdev()) == NULL) |
| 104 puts("No boot device set\n"); |
| 105 else |
| 106 dev_print(dev_desc); |
| 107 return 0; |
| 108 } |
| 109 |
| 110 if (!strcmp(argv[1], "set")) |
| 111 opcode = SET; |
| 112 else if (!strcmp(argv[1], "read")) |
| 113 opcode = READ; |
| 114 else if (!strcmp(argv[1], "write")) |
| 115 opcode = WRITE; |
| 116 else |
| 117 USAGE(1, cmdpt, "Unrecognized action: %s\n", argv[1]); |
| 118 |
| 119 /* apply De Morgan's laws on |
| 120 * !((argc == 4 && opcode == SET) || argc == 5) */ |
| 121 if ((argc != 4 || opcode != SET) && argc != 5) |
| 122 USAGE(1, cmdtp, "Wrong number of arguments\n"); |
| 123 |
| 124 if (opcode == SET) { |
| 125 dev = (int) simple_strtoul(argv[3], NULL, 16); |
| 126 part = (argc == 5) ? |
| 127 0 : (int) simple_strtoul(argv[4], NULL, 16); |
| 128 |
| 129 if (set_bootdev(argv[2], dev, part)) { |
| 130 puts("Set bootdev failed\n"); |
| 131 return 1; |
| 132 } |
| 133 |
| 134 return 0; |
| 135 } |
| 136 |
| 137 /* assert(opcode == READ || opcode == WRITE); */ |
| 138 |
| 139 buf = (void *) simple_strtoul(argv[2], NULL, 16); |
| 140 blk = (uint64_t) simple_strtoul(argv[3], NULL, 16); |
| 141 cnt = (uint64_t) simple_strtoul(argv[4], NULL, 16); |
| 142 |
| 143 retcode = (opcode == READ) ? |
| 144 BootDeviceReadLBA(blk, cnt, buf) : |
| 145 BootDeviceWriteLBA(blk, cnt, buf); |
| 146 |
| 147 if (retcode) |
| 148 USAGE(1, cmdtp, opcode == READ ? |
| 149 "Read failed\n" : "Write failed\n"); |
| 150 |
| 151 return 0; |
| 152 } |
| 153 |
| 154 int do_cros_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 155 { |
| 156 cmd_tbl_t *c; |
| 157 |
| 158 if (argc < 2) |
| 159 return _do_help(&cmd_cros_sub[0], ARRAY_SIZE(cmd_cros_sub), |
| 160 cmdtp, flag, argc, argv); |
| 161 |
| 162 c = find_cmd_tbl(argv[1], &cmd_cros_sub[0], ARRAY_SIZE(cmd_cros_sub)); |
| 163 if (!c) |
| 164 USAGE(1, cmdtp, "Unrecognized action: %s\n", argv[1]); |
| 165 |
| 166 cmd_usage(c); |
| 167 return 0; |
| 168 } |
OLD | NEW |