blob: 34f59cbc8bf37dd55dc44e5fcab0013f5c776a86 [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 " -p NUM Size (in blocks) of the disk to pad between the\n"
23 " primary GPT header and its entries, default 0\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070024 "\n", progname);
25}
26
27int cmd_create(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080028 CgptCreateParams params;
29 memset(&params, 0, sizeof(params));
Bill Richardsonc4e92af2010-10-12 07:33:15 -070030
Bill Richardsonf1372d92010-06-11 09:15:55 -070031 int c;
32 int errorcnt = 0;
Albert Chaulk494646d2013-07-19 12:56:38 -070033 char *e = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070034
35 opterr = 0; // quiet, you
Nam T. Nguyen8577b532014-11-25 13:26:53 -080036 while ((c=getopt(argc, argv, ":hzp:D:")) != -1)
Bill Richardsonf1372d92010-06-11 09:15:55 -070037 {
38 switch (c)
39 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080040 case 'D':
41 params.drive_size = strtoull(optarg, &e, 0);
42 if (!*optarg || (e && *e))
43 {
44 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
45 errorcnt++;
46 }
47 break;
Bill Richardsonf1372d92010-06-11 09:15:55 -070048 case 'z':
Jay Srinivasana0581432012-01-26 21:50:05 -080049 params.zap = 1;
Bill Richardsonf1372d92010-06-11 09:15:55 -070050 break;
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070051 case 'p':
52 params.padding = strtoull(optarg, &e, 0);
Nam T. Nguyenab899592014-11-13 19:30:46 -080053 if (!*optarg || (e && *e))
54 {
55 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
56 errorcnt++;
57 }
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070058 break;
Bill Richardsonf1372d92010-06-11 09:15:55 -070059 case 'h':
60 Usage();
61 return CGPT_OK;
62 case '?':
63 Error("unrecognized option: -%c\n", optopt);
64 errorcnt++;
65 break;
66 case ':':
67 Error("missing argument to -%c\n", optopt);
68 errorcnt++;
69 break;
70 default:
71 errorcnt++;
72 break;
73 }
74 }
75 if (errorcnt)
76 {
77 Usage();
78 return CGPT_FAILED;
79 }
80
81 if (optind >= argc) {
82 Usage();
83 return CGPT_FAILED;
84 }
85
Jay Srinivasan250549d2012-02-16 17:40:45 -080086 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -070087
Bill Richardson3f806a22013-03-20 15:02:34 -070088 return CgptCreate(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -070089}