xref: /aosp_15_r20/external/vboot_reference/cgpt/cmd_prioritize.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2012 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <getopt.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <uuid/uuid.h>
11 
12 #include "cgpt.h"
13 #include "vboot_host.h"
14 
15 extern const char* progname;
16 
Usage(void)17 static void Usage(void)
18 {
19   printf("\nUsage: %s prioritize [OPTIONS] DRIVE\n\n"
20          "Reorder the priority of all active ChromeOS Kernel partitions.\n\n"
21          "Options:\n"
22          "  -D NUM       Size (in bytes) of the disk where partitions reside;\n"
23          "                 default 0, meaning partitions and GPT structs are\n"
24          "                 both on DRIVE\n"
25          "  -P NUM       Highest priority to use in the new ordering. The\n"
26          "                 other partitions will be ranked in decreasing\n"
27          "                 priority while preserving their original order.\n"
28          "                 If necessary the lowest ranks will be coalesced.\n"
29          "                 No active kernels will be lowered to priority 0.\n"
30          "  -i NUM       Specify the partition to make the highest in the new\n"
31          "                 order.\n"
32          "  -f           Friends of the given partition (those with the same\n"
33          "                 starting priority) are also updated to the new\n"
34          "                 highest priority.\n"
35          "\n"
36          "With no options this will set the lowest active kernel to\n"
37          "priority 1 while maintaining the original order.\n"
38          "\n", progname);
39 }
40 
cmd_prioritize(int argc,char * argv[])41 int cmd_prioritize(int argc, char *argv[]) {
42   CgptPrioritizeParams params;
43   memset(&params, 0, sizeof(params));
44 
45   int c;
46   int errorcnt = 0;
47   char *e = 0;
48 
49   opterr = 0;                     // quiet, you
50   while ((c=getopt(argc, argv, ":hi:fP:D:")) != -1)
51   {
52     switch (c)
53     {
54     case 'D':
55       params.drive_size = strtoull(optarg, &e, 0);
56       errorcnt += check_int_parse(c, e);
57       break;
58     case 'i':
59       params.set_partition = (uint32_t)strtoul(optarg, &e, 0);
60       errorcnt += check_int_parse(c, e);
61       break;
62     case 'f':
63       params.set_friends = 1;
64       break;
65     case 'P':
66       params.max_priority = (int)strtol(optarg, &e, 0);
67       errorcnt += check_int_parse(c, e);
68       errorcnt += check_int_limit(c, params.max_priority, 1, 15);
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 
93   if (params.set_friends && !params.set_partition) {
94     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 
104   params.drive_name = argv[optind];
105 
106   return CgptPrioritize(&params);
107 }
108