blob: ab4e2e366c6e10dcb16d597550e82cc6824e4a28 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardson3430b322010-11-29 14:24:51 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bill Richardson3430b322010-11-29 14:24:51 -08005#include <getopt.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <uuid/uuid.h>
10
Bill Richardson0c3ba242013-03-29 11:09:30 -070011#include "cgpt.h"
12#include "vboot_host.h"
Bill Richardson3430b322010-11-29 14:24:51 -080013
Bill Richardson4cb54972014-06-20 14:33:00 -070014extern const char* progname;
15
Bill Richardson3430b322010-11-29 14:24:51 -080016static void Usage(void)
17{
18 printf("\nUsage: %s prioritize [OPTIONS] DRIVE\n\n"
19 "Reorder the priority of all active ChromeOS Kernel partitions.\n\n"
20 "Options:\n"
Nam T. Nguyenab899592014-11-13 19:30:46 -080021 " -D NUM Size (in bytes) of the disk where partitions reside\n"
22 " default 0, meaning partitions and GPT structs are\n"
23 " both on DRIVE\n"
Bill Richardson3430b322010-11-29 14:24:51 -080024 " -P NUM Highest priority to use in the new ordering. The\n"
25 " other partitions will be ranked in decreasing\n"
26 " priority while preserving their original order.\n"
27 " If necessary the lowest ranks will be coalesced.\n"
28 " No active kernels will be lowered to priority 0.\n"
29 " -i NUM Specify the partition to make the highest in the new\n"
30 " order.\n"
31 " -f Friends of the given partition (those with the same\n"
32 " starting priority) are also updated to the new\n"
33 " highest priority.\n"
34 "\n"
35 "With no options this will set the lowest active kernel to\n"
36 "priority 1 while maintaining the original order.\n"
37 "\n", progname);
38}
39
Bill Richardson3430b322010-11-29 14:24:51 -080040int cmd_prioritize(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080041 CgptPrioritizeParams params;
42 memset(&params, 0, sizeof(params));
43
Bill Richardson3430b322010-11-29 14:24:51 -080044 int c;
45 int errorcnt = 0;
46 char *e = 0;
47
48 opterr = 0; // quiet, you
Nam T. Nguyenab899592014-11-13 19:30:46 -080049 while ((c=getopt(argc, argv, ":hi:fP:D:")) != -1)
Bill Richardson3430b322010-11-29 14:24:51 -080050 {
51 switch (c)
52 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080053 case 'D':
54 params.drive_size = strtoull(optarg, &e, 0);
55 if (!*optarg || (e && *e))
56 {
57 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
58 errorcnt++;
59 }
60 break;
Bill Richardson3430b322010-11-29 14:24:51 -080061 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080062 params.set_partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardson3430b322010-11-29 14:24:51 -080063 if (!*optarg || (e && *e))
64 {
65 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
66 errorcnt++;
67 }
68 break;
69 case 'f':
Jay Srinivasana0581432012-01-26 21:50:05 -080070 params.set_friends = 1;
Bill Richardson3430b322010-11-29 14:24:51 -080071 break;
72 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -080073 params.max_priority = (int)strtol(optarg, &e, 0);
Bill Richardson3430b322010-11-29 14:24:51 -080074 if (!*optarg || (e && *e))
75 {
76 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
77 errorcnt++;
78 }
Jay Srinivasana0581432012-01-26 21:50:05 -080079 if (params.max_priority < 1 || params.max_priority > 15) {
Bill Richardson3430b322010-11-29 14:24:51 -080080 Error("value for -%c must be between 1 and 15\n", c);
81 errorcnt++;
82 }
83 break;
84
85 case 'h':
86 Usage();
87 return CGPT_OK;
88 case '?':
89 Error("unrecognized option: -%c\n", optopt);
90 errorcnt++;
91 break;
92 case ':':
93 Error("missing argument to -%c\n", optopt);
94 errorcnt++;
95 break;
96 default:
97 errorcnt++;
98 break;
99 }
100 }
101 if (errorcnt)
102 {
103 Usage();
104 return CGPT_FAILED;
105 }
106
Jay Srinivasana0581432012-01-26 21:50:05 -0800107 if (params.set_friends && !params.set_partition) {
Bill Richardson3430b322010-11-29 14:24:51 -0800108 Error("the -f option is only useful with the -i option\n");
109 Usage();
110 return CGPT_FAILED;
111 }
112
113 if (optind >= argc) {
114 Error("missing drive argument\n");
115 return CGPT_FAILED;
116 }
117
Jay Srinivasan250549d2012-02-16 17:40:45 -0800118 params.drive_name = argv[optind];
Bill Richardson3430b322010-11-29 14:24:51 -0800119
Bill Richardson3f806a22013-03-20 15:02:34 -0700120 return CgptPrioritize(&params);
Bill Richardson3430b322010-11-29 14:24:51 -0800121}