blob: b81bda0c9b39e40c7769f5fde845748de7367652 [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
Jay Srinivasana0581432012-01-26 21:50:05 -080011#include "cgpt_params.h"
Bill Richardson3430b322010-11-29 14:24:51 -080012
13static void Usage(void)
14{
15 printf("\nUsage: %s prioritize [OPTIONS] DRIVE\n\n"
16 "Reorder the priority of all active ChromeOS Kernel partitions.\n\n"
17 "Options:\n"
18 " -P NUM Highest priority to use in the new ordering. The\n"
19 " other partitions will be ranked in decreasing\n"
20 " priority while preserving their original order.\n"
21 " If necessary the lowest ranks will be coalesced.\n"
22 " No active kernels will be lowered to priority 0.\n"
23 " -i NUM Specify the partition to make the highest in the new\n"
24 " order.\n"
25 " -f Friends of the given partition (those with the same\n"
26 " starting priority) are also updated to the new\n"
27 " highest priority.\n"
28 "\n"
29 "With no options this will set the lowest active kernel to\n"
30 "priority 1 while maintaining the original order.\n"
31 "\n", progname);
32}
33
Bill Richardson3430b322010-11-29 14:24:51 -080034int cmd_prioritize(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080035 CgptPrioritizeParams params;
36 memset(&params, 0, sizeof(params));
37
Bill Richardson3430b322010-11-29 14:24:51 -080038 int c;
39 int errorcnt = 0;
40 char *e = 0;
41
42 opterr = 0; // quiet, you
43 while ((c=getopt(argc, argv, ":hi:fP:")) != -1)
44 {
45 switch (c)
46 {
47 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080048 params.set_partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardson3430b322010-11-29 14:24:51 -080049 if (!*optarg || (e && *e))
50 {
51 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
52 errorcnt++;
53 }
54 break;
55 case 'f':
Jay Srinivasana0581432012-01-26 21:50:05 -080056 params.set_friends = 1;
Bill Richardson3430b322010-11-29 14:24:51 -080057 break;
58 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -080059 params.max_priority = (int)strtol(optarg, &e, 0);
Bill Richardson3430b322010-11-29 14:24:51 -080060 if (!*optarg || (e && *e))
61 {
62 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
63 errorcnt++;
64 }
Jay Srinivasana0581432012-01-26 21:50:05 -080065 if (params.max_priority < 1 || params.max_priority > 15) {
Bill Richardson3430b322010-11-29 14:24:51 -080066 Error("value for -%c must be between 1 and 15\n", c);
67 errorcnt++;
68 }
69 break;
70
71 case 'h':
72 Usage();
73 return CGPT_OK;
74 case '?':
75 Error("unrecognized option: -%c\n", optopt);
76 errorcnt++;
77 break;
78 case ':':
79 Error("missing argument to -%c\n", optopt);
80 errorcnt++;
81 break;
82 default:
83 errorcnt++;
84 break;
85 }
86 }
87 if (errorcnt)
88 {
89 Usage();
90 return CGPT_FAILED;
91 }
92
Jay Srinivasana0581432012-01-26 21:50:05 -080093 if (params.set_friends && !params.set_partition) {
Bill Richardson3430b322010-11-29 14:24:51 -080094 Error("the -f option is only useful with the -i option\n");
95 Usage();
96 return CGPT_FAILED;
97 }
98
99 if (optind >= argc) {
100 Error("missing drive argument\n");
101 return CGPT_FAILED;
102 }
103
Jay Srinivasan250549d2012-02-16 17:40:45 -0800104 params.drive_name = argv[optind];
Bill Richardson3430b322010-11-29 14:24:51 -0800105
Jay Srinivasana0581432012-01-26 21:50:05 -0800106 return cgpt_prioritize(&params);
Bill Richardson3430b322010-11-29 14:24:51 -0800107}