blob: 2a65f20251a98ca46f4b978025b9d2eec09ed12e [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"
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
27int cmd_boot(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080028 CgptBootParams params;
29 memset(&params, 0, sizeof(params));
30
Bill Richardsonc4e92af2010-10-12 07:33:15 -070031
Bill Richardsonf1372d92010-06-11 09:15:55 -070032 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 Srinivasana0581432012-01-26 21:50:05 -080042 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070043 if (!*optarg || (e && *e))
44 {
45 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
46 errorcnt++;
47 }
48 break;
49 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080050 params.bootfile = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070051 break;
52 case 'p':
Jay Srinivasana0581432012-01-26 21:50:05 -080053 params.create_pmbr = 1;
Bill Richardsonf1372d92010-06-11 09:15:55 -070054 break;
Bill Richardsonc4e92af2010-10-12 07:33:15 -070055
Bill Richardsonf1372d92010-06-11 09:15:55 -070056 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 Srinivasan250549d2012-02-16 17:40:45 -080083 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -070084
Bill Richardson3f806a22013-03-20 15:02:34 -070085 return CgptBoot(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -070086}