Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 5 | #include <getopt.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 6 | #include <string.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 7 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 8 | #include "cgpt.h" |
| 9 | #include "vboot_host.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 10 | |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 11 | extern const char* progname; |
| 12 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | static void Usage(void) |
| 14 | { |
| 15 | printf("\nUsage: %s boot [OPTIONS] DRIVE\n\n" |
| 16 | "Edit the PMBR sector for legacy BIOSes\n\n" |
| 17 | "Options:\n" |
| 18 | " -i NUM Set bootable partition\n" |
| 19 | " -b FILE Install bootloader code in the PMBR\n" |
| 20 | " -p Create legacy PMBR partition table\n" |
| 21 | "\n" |
| 22 | "With no options, it will just print the PMBR boot guid\n" |
| 23 | "\n", progname); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | int cmd_boot(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 28 | CgptBootParams params; |
| 29 | memset(¶ms, 0, sizeof(params)); |
| 30 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 31 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 32 | int c; |
| 33 | int errorcnt = 0; |
| 34 | char *e = 0; |
| 35 | |
| 36 | opterr = 0; // quiet, you |
| 37 | while ((c=getopt(argc, argv, ":hi:b:p")) != -1) |
| 38 | { |
| 39 | switch (c) |
| 40 | { |
| 41 | case 'i': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 42 | params.partition = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 43 | if (!*optarg || (e && *e)) |
| 44 | { |
| 45 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 46 | errorcnt++; |
| 47 | } |
| 48 | break; |
| 49 | case 'b': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 50 | params.bootfile = optarg; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 51 | break; |
| 52 | case 'p': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 53 | params.create_pmbr = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 54 | break; |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 55 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 56 | case 'h': |
| 57 | Usage(); |
| 58 | return CGPT_OK; |
| 59 | case '?': |
| 60 | Error("unrecognized option: -%c\n", optopt); |
| 61 | errorcnt++; |
| 62 | break; |
| 63 | case ':': |
| 64 | Error("missing argument to -%c\n", optopt); |
| 65 | errorcnt++; |
| 66 | break; |
| 67 | default: |
| 68 | errorcnt++; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | if (errorcnt) |
| 73 | { |
| 74 | Usage(); |
| 75 | return CGPT_FAILED; |
| 76 | } |
| 77 | |
| 78 | if (optind >= argc) { |
| 79 | Error("missing drive argument\n"); |
| 80 | return CGPT_FAILED; |
| 81 | } |
| 82 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 83 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 84 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 85 | return CgptBoot(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 86 | } |