summaryrefslogtreecommitdiff
path: root/cgpt/cmd_add.c
blob: ef6f729da6baedb11e24c78b3427973b2bb2177c (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
/* Copyright (c) 2012 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 <getopt.h>
#include <string.h>

#include "cgpt.h"
#include "vboot_host.h"

extern const char* progname;

static void Usage(void)
{
  printf("\nUsage: %s add [OPTIONS] DRIVE\n\n"
         "Add, edit, or remove a partition entry.\n\n"
         "Options:\n"
         "  -D NUM       Size (in bytes) of the disk where partitions reside\n"
         "                 default 0, meaning partitions and GPT structs are\n"
         "                 both on DRIVE\n"
         "  -i NUM       Specify partition (default is next available)\n"
         "  -b NUM       Beginning sector\n"
         "  -s NUM       Size in sectors\n"
         "  -t GUID      Partition Type GUID\n"
         "  -u GUID      Partition Unique ID\n"
         "  -l LABEL     Label\n"
         "  -S NUM       set Successful flag (0|1)\n"
         "  -T NUM       set Tries flag (0-15)\n"
         "  -P NUM       set Priority flag (0-15)\n"
         "  -R NUM       set Required flag (0|1)\n"
         "  -B NUM       set Legacy Boot flag (0|1)\n"
         "  -A NUM       set raw 16-bit attribute value (bits 48-63)\n"
         "\n"
         "Use the -i option to modify an existing partition.\n"
         "The -b, -s, and -t options must be given for new partitions.\n"
         "\n", progname);
  PrintTypes();
}

int cmd_add(int argc, char *argv[]) {

  CgptAddParams params;
  memset(&params, 0, sizeof(params));

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

  opterr = 0;                     // quiet, you
  while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:R:B:A:D:")) != -1)
  {
    switch (c)
    {
    case 'D':
      params.drive_size = strtoull(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      break;
    case 'i':
      params.partition = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      break;
    case 'b':
      params.set_begin = 1;
      params.begin = strtoull(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      break;
    case 's':
      params.set_size = 1;
      params.size = strtoull(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      break;
    case 't':
      params.set_type = 1;
      if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
          CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
        Error("invalid argument to -%c: %s\n", c, optarg);
        errorcnt++;
      }
      break;
    case 'u':
      params.set_unique = 1;
      if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
        Error("invalid argument to -%c: %s\n", c, optarg);
        errorcnt++;
      }
      break;
    case 'l':
      params.label = optarg;
      break;
    case 'S':
      params.set_successful = 1;
      params.successful = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      errorcnt += check_int_limit(c, params.successful, 0, 1);
      break;
    case 'T':
      params.set_tries = 1;
      params.tries = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      errorcnt += check_int_limit(c, params.tries, 0, 15);
      break;
    case 'P':
      params.set_priority = 1;
      params.priority = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      errorcnt += check_int_limit(c, params.priority, 0, 15);
      break;
    case 'R':
      params.set_required = 1;
      params.required = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      errorcnt += check_int_limit(c, params.required, 0, 1);
      break;
    case 'B':
      params.set_legacy_boot = 1;
      params.legacy_boot = (uint32_t)strtoul(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      errorcnt += check_int_limit(c, params.legacy_boot, 0, 1);
      break;
    case 'A':
      params.set_raw = 1;
      params.raw_value = strtoull(optarg, &e, 0);
      errorcnt += check_int_parse(c, e);
      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 (optind >= argc)
  {
    Error("missing drive argument\n");
    return CGPT_FAILED;
  }

  params.drive_name = argv[optind];

  return CgptAdd(&params);
}