blob: 47137bdc841167789a19096b43688c119b121ca3 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardsonf1372d92010-06-11 09:15:55 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bill Richardsonf1372d92010-06-11 09:15:55 -07005#include <getopt.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07006#include <string.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07007
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "cgpt.h"
9#include "vboot_host.h"
Bill Richardsonf1372d92010-06-11 09:15:55 -070010
Bill Richardson4cb54972014-06-20 14:33:00 -070011extern const char* progname;
12
Bill Richardsonf1372d92010-06-11 09:15:55 -070013static void Usage(void)
14{
15 printf("\nUsage: %s create [OPTIONS] DRIVE\n\n"
16 "Create or reset an empty GPT.\n\n"
17 "Options:\n"
Nam T. Nguyenab899592014-11-13 19:30:46 -080018 " -D NUM Size (in bytes) of the disk where partitions reside\n"
19 " default 0, meaning partitions and GPT structs are\n"
20 " both on DRIVE\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070021 " -z Zero the sectors of the GPT table and entries\n"
Nam T. Nguyenab899592014-11-13 19:30:46 -080022 " -s NUM Size (in bytes) of the disk (MTD only)\n"
23 " -p NUM Size (in blocks) of the disk to pad between the\n"
24 " primary GPT header and its entries, default 0\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070025 "\n", progname);
26}
27
28int cmd_create(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080029 CgptCreateParams params;
30 memset(&params, 0, sizeof(params));
Bill Richardsonc4e92af2010-10-12 07:33:15 -070031
Bill Richardsonf1372d92010-06-11 09:15:55 -070032 int c;
33 int errorcnt = 0;
Albert Chaulk494646d2013-07-19 12:56:38 -070034 char *e = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070035
36 opterr = 0; // quiet, you
Nam T. Nguyenab899592014-11-13 19:30:46 -080037 while ((c=getopt(argc, argv, ":hzs:p:D:")) != -1)
Bill Richardsonf1372d92010-06-11 09:15:55 -070038 {
39 switch (c)
40 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080041 case 'D':
42 params.drive_size = strtoull(optarg, &e, 0);
43 if (!*optarg || (e && *e))
44 {
45 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
46 errorcnt++;
47 }
48 break;
Bill Richardsonf1372d92010-06-11 09:15:55 -070049 case 'z':
Jay Srinivasana0581432012-01-26 21:50:05 -080050 params.zap = 1;
Bill Richardsonf1372d92010-06-11 09:15:55 -070051 break;
Albert Chaulk494646d2013-07-19 12:56:38 -070052 case 's':
53 params.size = strtoull(optarg, &e, 0);
54 break;
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070055 case 'p':
56 params.padding = strtoull(optarg, &e, 0);
Nam T. Nguyenab899592014-11-13 19:30:46 -080057 if (!*optarg || (e && *e))
58 {
59 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
60 errorcnt++;
61 }
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070062 break;
Bill Richardsonf1372d92010-06-11 09:15:55 -070063 case 'h':
64 Usage();
65 return CGPT_OK;
66 case '?':
67 Error("unrecognized option: -%c\n", optopt);
68 errorcnt++;
69 break;
70 case ':':
71 Error("missing argument to -%c\n", optopt);
72 errorcnt++;
73 break;
74 default:
75 errorcnt++;
76 break;
77 }
78 }
79 if (errorcnt)
80 {
81 Usage();
82 return CGPT_FAILED;
83 }
84
85 if (optind >= argc) {
86 Usage();
87 return CGPT_FAILED;
88 }
89
Jay Srinivasan250549d2012-02-16 17:40:45 -080090 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -070091
Bill Richardson3f806a22013-03-20 15:02:34 -070092 return CgptCreate(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -070093}