blob: 2f815663c77754f1e35034045803e80a9bab7a36 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardsonf1372d92010-06-11 09:15:55 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bill Richardsonf1372d92010-06-11 09:15:55 -07005#include <getopt.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07006#include <string.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07007
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "cgpt.h"
9#include "vboot_host.h"
Bill Richardsonf1372d92010-06-11 09:15:55 -070010
Bill Richardson4cb54972014-06-20 14:33:00 -070011extern const char* progname;
12
Bill Richardsonf1372d92010-06-11 09:15:55 -070013static 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. Nguyenab899592014-11-13 19:30:46 -080018 " -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 Richardsonf1372d92010-06-11 09:15:55 -070021 " -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
30int cmd_boot(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080031 CgptBootParams params;
32 memset(&params, 0, sizeof(params));
33
Bill Richardsonc4e92af2010-10-12 07:33:15 -070034
Bill Richardsonf1372d92010-06-11 09:15:55 -070035 int c;
36 int errorcnt = 0;
37 char *e = 0;
38
39 opterr = 0; // quiet, you
Nam T. Nguyenab899592014-11-13 19:30:46 -080040 while ((c=getopt(argc, argv, ":hi:b:pD:")) != -1)
Bill Richardsonf1372d92010-06-11 09:15:55 -070041 {
42 switch (c)
43 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080044 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 Richardsonf1372d92010-06-11 09:15:55 -070052 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080053 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070054 if (!*optarg || (e && *e))
55 {
56 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57 errorcnt++;
58 }
59 break;
60 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080061 params.bootfile = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070062 break;
63 case 'p':
Jay Srinivasana0581432012-01-26 21:50:05 -080064 params.create_pmbr = 1;
Bill Richardsonf1372d92010-06-11 09:15:55 -070065 break;
Bill Richardsonc4e92af2010-10-12 07:33:15 -070066
Bill Richardsonf1372d92010-06-11 09:15:55 -070067 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 Srinivasan250549d2012-02-16 17:40:45 -080094 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -070095
Bill Richardson3f806a22013-03-20 15:02:34 -070096 return CgptBoot(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -070097}