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" |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 18 | " -D NUM Size (in bytes) of the disk where partitions reside\n" |
| 19 | " default 0, meaning partitions and GPT structs are\n" |
| 20 | " both on DRIVE\n" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 21 | " -i NUM Set bootable partition\n" |
| 22 | " -b FILE Install bootloader code in the PMBR\n" |
| 23 | " -p Create legacy PMBR partition table\n" |
| 24 | "\n" |
| 25 | "With no options, it will just print the PMBR boot guid\n" |
| 26 | "\n", progname); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | int cmd_boot(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 31 | CgptBootParams params; |
| 32 | memset(¶ms, 0, sizeof(params)); |
| 33 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 34 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 35 | int c; |
| 36 | int errorcnt = 0; |
| 37 | char *e = 0; |
| 38 | |
| 39 | opterr = 0; // quiet, you |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 40 | while ((c=getopt(argc, argv, ":hi:b:pD:")) != -1) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 41 | { |
| 42 | switch (c) |
| 43 | { |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 44 | case 'D': |
| 45 | params.drive_size = strtoull(optarg, &e, 0); |
| 46 | if (!*optarg || (e && *e)) |
| 47 | { |
| 48 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 49 | errorcnt++; |
| 50 | } |
| 51 | break; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 52 | case 'i': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 53 | params.partition = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 54 | if (!*optarg || (e && *e)) |
| 55 | { |
| 56 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 57 | errorcnt++; |
| 58 | } |
| 59 | break; |
| 60 | case 'b': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 61 | params.bootfile = optarg; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 62 | break; |
| 63 | case 'p': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 64 | params.create_pmbr = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 65 | break; |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 66 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 67 | case 'h': |
| 68 | Usage(); |
| 69 | return CGPT_OK; |
| 70 | case '?': |
| 71 | Error("unrecognized option: -%c\n", optopt); |
| 72 | errorcnt++; |
| 73 | break; |
| 74 | case ':': |
| 75 | Error("missing argument to -%c\n", optopt); |
| 76 | errorcnt++; |
| 77 | break; |
| 78 | default: |
| 79 | errorcnt++; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if (errorcnt) |
| 84 | { |
| 85 | Usage(); |
| 86 | return CGPT_FAILED; |
| 87 | } |
| 88 | |
| 89 | if (optind >= argc) { |
| 90 | Error("missing drive argument\n"); |
| 91 | return CGPT_FAILED; |
| 92 | } |
| 93 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 94 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 95 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 96 | return CgptBoot(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 97 | } |