summaryrefslogtreecommitdiff
path: root/cgpt/cmd_prioritize.c
blob: 3f345689baa8dd92a45548837aba3b84a1e8d22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cgpt.h"

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>

#include "cgptlib_internal.h"

static void Usage(void)
{
  printf("\nUsage: %s prioritize [OPTIONS] DRIVE\n\n"
         "Reorder the priority of all active ChromeOS Kernel partitions.\n\n"
         "Options:\n"
         "  -P NUM       Highest priority to use in the new ordering. The\n"
         "                 other partitions will be ranked in decreasing\n"
         "                 priority while preserving their original order.\n"
         "                 If necessary the lowest ranks will be coalesced.\n"
         "                 No active kernels will be lowered to priority 0.\n"
         "  -i NUM       Specify the partition to make the highest in the new\n"
         "                 order.\n"
         "  -f           Friends of the given partition (those with the same\n"
         "                 starting priority) are also updated to the new\n"
         "                 highest priority.\n"
         "\n"
         "With no options this will set the lowest active kernel to\n"
         "priority 1 while maintaining the original order.\n"
         "\n", progname);
}

//////////////////////////////////////////////////////////////////////////////
// I want a sorted list of priority groups, where each element in the list
// contains an unordered list of GPT partition numbers. This is a stupid
// implementation, but our needs are simple and don't justify the time or space
// it would take to write a "better" one.
#define MAX_GROUPS 17                   // 0-15, plus one "higher"

typedef struct {
  int priority;                         // priority of this group
  int num_parts;                        // number of partitions in this group
  uint32_t *part;                       // array of partitions in this group
} group_t;

typedef struct {
  int max_parts;                       // max number of partitions in any group
  int num_groups;                      // number of non-empty groups
  group_t group[MAX_GROUPS];           // array of groups
} group_list_t;


static group_list_t *NewGroupList(int max_p) {
  int i;
  group_list_t *gl = (group_list_t *)malloc(sizeof(group_list_t));
  require(gl);
  gl->max_parts = max_p;
  gl->num_groups = 0;
  // reserve space for the maximum number of partitions in every group
  for (i=0; i<MAX_GROUPS; i++) {
    gl->group[i].priority = -1;
    gl->group[i].num_parts = 0;
    gl->group[i].part = (uint32_t *)malloc(sizeof(uint32_t) * max_p);
    require(gl->group[i].part);
  }

  return gl;
}

static void FreeGroups(group_list_t *gl) {
  int i;
  for (i=0; i<MAX_GROUPS; i++)
    free(gl->group[i].part);
  free(gl);
}

static void AddToGroup(group_list_t *gl, int priority, int partition) {
  int i;
  // See if I've already got a group with this priority
  for (i=0; i<gl->num_groups; i++)
    if (gl->group[i].priority == priority)
      break;
  if (i == gl->num_groups) {
    // no, add a group
    require(i < MAX_GROUPS);
    gl->num_groups++;
    gl->group[i].priority = priority;
  }
  // add the partition to it
  int j = gl->group[i].num_parts;
  gl->group[i].part[j] = partition;
  gl->group[i].num_parts++;
}

static void ChangeGroup(group_list_t *gl, int old_priority, int new_priority) {
  int i;
  for (i=0; i<gl->num_groups; i++)
    if (gl->group[i].priority == old_priority) {
      gl->group[i].priority = new_priority;
      break;
    }
}

static void SortGroups(group_list_t *gl) {
  int i, j;
  group_t tmp;

  // straight insertion sort is fast enough
  for (i=1; i<gl->num_groups; i++) {
    tmp = gl->group[i];
    for (j=i; j && (gl->group[j-1].priority < tmp.priority); j--)
      gl->group[j] = gl->group[j-1];
    gl->group[j] = tmp;
  }
}


//////////////////////////////////////////////////////////////////////////////

int cmd_prioritize(int argc, char *argv[]) {
  struct drive drive;
  uint32_t set_partition = 0;
  int set_friends = 0;
  int max_priority = 0;
  int priority;
  int orig_priority = 0;
  int gpt_retval;
  GptEntry *entry;
  uint32_t index;
  uint32_t max_part;
  int num_kernels;
  int i,j;
  group_list_t *groups;

  int c;
  int errorcnt = 0;
  char *e = 0;

  opterr = 0;                     // quiet, you
  while ((c=getopt(argc, argv, ":hi:fP:")) != -1)
  {
    switch (c)
    {
    case 'i':
      set_partition = (uint32_t)strtoul(optarg, &e, 0);
      if (!*optarg || (e && *e))
      {
        Error("invalid argument to -%c: \"%s\"\n", c, optarg);
        errorcnt++;
      }
      break;
    case 'f':
      set_friends = 1;
      break;
    case 'P':
      max_priority = (int)strtol(optarg, &e, 0);
      if (!*optarg || (e && *e))
      {
        Error("invalid argument to -%c: \"%s\"\n", c, optarg);
        errorcnt++;
      }
      if (max_priority < 1 || max_priority > 15) {
        Error("value for -%c must be between 1 and 15\n", c);
        errorcnt++;
      }
      break;

    case 'h':
      Usage();
      return CGPT_OK;
    case '?':
      Error("unrecognized option: -%c\n", optopt);
      errorcnt++;
      break;
    case ':':
      Error("missing argument to -%c\n", optopt);
      errorcnt++;
      break;
    default:
      errorcnt++;
      break;
    }
  }
  if (errorcnt)
  {
    Usage();
    return CGPT_FAILED;
  }

  if (set_friends && !set_partition) {
    Error("the -f option is only useful with the -i option\n");
    Usage();
    return CGPT_FAILED;
  }

  if (optind >= argc) {
    Error("missing drive argument\n");
    return CGPT_FAILED;
  }

  if (CGPT_OK != DriveOpen(argv[optind], &drive))
    return CGPT_FAILED;

  if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
    Error("GptSanityCheck() returned %d: %s\n",
          gpt_retval, GptError(gpt_retval));
    return CGPT_FAILED;
  }

  max_part = GetNumberOfEntries(&drive.gpt);

  if (set_partition) {
    if (set_partition < 1 || set_partition > max_part) {
      Error("invalid partition number: %d (must be between 1 and %d\n",
            set_partition, max_part);
      goto bad;
    }
    index = set_partition - 1;
    // it must be a kernel
    entry = GetEntry(&drive.gpt, PRIMARY, index);
    if (!GuidEqual(&entry->type, &guid_chromeos_kernel)) {
      Error("partition %d is not a ChromeOS kernel\n", set_partition);
      goto bad;
    }
  }

  // How many kernel partitions do I have?
  num_kernels = 0;
  for (i = 0; i < max_part; i++) {
    entry = GetEntry(&drive.gpt, PRIMARY, i);
    if (GuidEqual(&entry->type, &guid_chromeos_kernel))
      num_kernels++;
  }

  if (!num_kernels)
    // nothing to do, so don't
    goto good;

  // Determine the current priority groups
  groups = NewGroupList(num_kernels);
  for (i = 0; i < max_part; i++) {
    entry = GetEntry(&drive.gpt, PRIMARY, i);
    if (!GuidEqual(&entry->type, &guid_chromeos_kernel))
      continue;

    priority = GetPriority(&drive.gpt, PRIMARY, i);

    // Is this partition special?
    if (set_partition && (i+1 == set_partition)) {
      orig_priority = priority;         // remember the original priority
      if (set_friends)
        AddToGroup(groups, priority, i); // we'll move them all later
      else
        AddToGroup(groups, 99, i);      // move only this one
    } else {
      AddToGroup(groups, priority, i);  // just remember
    }
  }

  // If we're including friends, then change the original group priority
  if (set_partition && set_friends) {
    ChangeGroup(groups, orig_priority, 99);
  }

  // Sorting gives the new order. Now we just need to reassign the
  // priorities.
  SortGroups(groups);

  // We'll never lower anything to zero, so if the last group is priority zero
  // we can ignore it.
  i = groups->num_groups;
  if (groups->group[i-1].priority == 0)
    groups->num_groups--;

  // Where do we start?
  if (max_priority)
    priority = max_priority;
  else
    priority = groups->num_groups > 15 ? 15 : groups->num_groups;

  // Figure out what the new values should be
  for (i=0; i<groups->num_groups; i++) {
    groups->group[i].priority = priority;
    if (priority > 1)
      priority--;
  }

  // Now apply the ranking to the GPT
  for (i=0; i<groups->num_groups; i++)
    for (j=0; j<groups->group[i].num_parts; j++)
      SetPriority(&drive.gpt, PRIMARY,
                  groups->group[i].part[j], groups->group[i].priority);

  FreeGroups(groups);


  // Write it all out
good:
  RepairEntries(&drive.gpt, MASK_PRIMARY);
  RepairHeader(&drive.gpt, MASK_PRIMARY);

  drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
                         GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
  UpdateCrc(&drive.gpt);

  return DriveClose(&drive, 1);

bad:
  (void) DriveClose(&drive, 0);
  return CGPT_FAILED;
}