1 // Copyright (c) 2012 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 #include <getopt.h>
6 #include <string.h>
7 
8 #include "cgpt.h"
9 #include "vboot_host.h"
10 
11 extern const char* progname;
12 
Usage(void)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          "  -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"
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 
cmd_boot(int argc,char * argv[])30 int cmd_boot(int argc, char *argv[]) {
31   CgptBootParams params;
32   memset(&params, 0, sizeof(params));
33 
34 
35   int c;
36   int errorcnt = 0;
37   char *e = 0;
38 
39   opterr = 0;                     // quiet, you
40   while ((c=getopt(argc, argv, ":hi:b:pD:")) != -1)
41   {
42     switch (c)
43     {
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;
52     case 'i':
53       params.partition = (uint32_t)strtoul(optarg, &e, 0);
54       if (!*optarg || (e && *e))
55       {
56         Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57         errorcnt++;
58       }
59       break;
60     case 'b':
61       params.bootfile = optarg;
62       break;
63     case 'p':
64       params.create_pmbr = 1;
65       break;
66 
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 
94   params.drive_name = argv[optind];
95 
96   return CgptBoot(&params);
97 }
98