1 /* Copyright 2012 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <getopt.h>
7 #include <string.h>
8
9 #include "cgpt.h"
10 #include "vboot_host.h"
11
12 extern const char* progname;
13
Usage(void)14 static void Usage(void)
15 {
16 printf("\nUsage: %s boot [OPTIONS] DRIVE\n\n"
17 "Edit the PMBR sector for legacy BIOSes\n\n"
18 "Options:\n"
19 " -D NUM Size (in bytes) of the disk where partitions reside;\n"
20 " default 0, meaning partitions and GPT structs are\n"
21 " both on DRIVE\n"
22 " -i NUM Set bootable partition\n"
23 " -b FILE Install bootloader code in the PMBR\n"
24 " -p Create legacy PMBR partition table\n"
25 "\n"
26 "With no options, it will just print the PMBR boot guid\n"
27 "\n", progname);
28 }
29
30
cmd_boot(int argc,char * argv[])31 int cmd_boot(int argc, char *argv[]) {
32 CgptBootParams params;
33 memset(¶ms, 0, sizeof(params));
34
35
36 int c;
37 int errorcnt = 0;
38 char *e = 0;
39
40 opterr = 0; // quiet, you
41 while ((c=getopt(argc, argv, ":hi:b:pD:")) != -1)
42 {
43 switch (c)
44 {
45 case 'D':
46 params.drive_size = strtoull(optarg, &e, 0);
47 errorcnt += check_int_parse(c, e);
48 break;
49 case 'i':
50 params.partition = (uint32_t)strtoul(optarg, &e, 0);
51 errorcnt += check_int_parse(c, e);
52 break;
53 case 'b':
54 params.bootfile = optarg;
55 break;
56 case 'p':
57 params.create_pmbr = 1;
58 break;
59
60 case 'h':
61 Usage();
62 return CGPT_OK;
63 case '?':
64 Error("unrecognized option: -%c\n", optopt);
65 errorcnt++;
66 break;
67 case ':':
68 Error("missing argument to -%c\n", optopt);
69 errorcnt++;
70 break;
71 default:
72 errorcnt++;
73 break;
74 }
75 }
76 if (errorcnt)
77 {
78 Usage();
79 return CGPT_FAILED;
80 }
81
82 if (optind >= argc) {
83 Error("missing drive argument\n");
84 return CGPT_FAILED;
85 }
86
87 params.drive_name = argv[optind];
88
89 return CgptBoot(¶ms);
90 }
91