blob: d0f048f2294c296f4a64d887e336a15d08c2e655 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardson4a209312010-07-02 11:34:38 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bill Richardson4a209312010-07-02 11:34:38 -07005#include <getopt.h>
Bill Richardson4a209312010-07-02 11:34:38 -07006#include <string.h>
Bill Richardson4a209312010-07-02 11:34:38 -07007
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "cgpt.h"
9#include "vboot_host.h"
Bill Richardson4a209312010-07-02 11:34:38 -070010
Bill Richardson4cb54972014-06-20 14:33:00 -070011extern const char* progname;
12
Bill Richardson4a209312010-07-02 11:34:38 -070013static void Usage(void)
14{
15 printf("\nUsage: %s find [OPTIONS] [DRIVE]\n\n"
16 "Find a partition by its UUID or label. With no specified DRIVE\n"
17 "it scans all physical drives.\n\n"
18 "Options:\n"
Nam T. Nguyenab899592014-11-13 19:30:46 -080019 " -D NUM Size (in bytes) of the disk where partitions reside\n"
20 " default 0, meaning partitions and GPT structs are\n"
21 " both on DRIVE\n"
Bill Richardson4a209312010-07-02 11:34:38 -070022 " -t GUID Search for Partition Type GUID\n"
23 " -u GUID Search for Partition Unique ID\n"
24 " -l LABEL Search for Label\n"
25 " -v Be verbose in displaying matches (repeatable)\n"
26 " -n Numeric output only\n"
27 " -1 Fail if more than one match is found\n"
Bill Richardson0697e3f2010-08-17 16:58:46 -070028 " -M FILE"
29 " Matching partition data must also contain FILE content\n"
30 " -O NUM"
31 " Byte offset into partition to match content (default 0)\n"
Bill Richardson4a209312010-07-02 11:34:38 -070032 "\n", progname);
33 PrintTypes();
34}
35
Bill Richardson0697e3f2010-08-17 16:58:46 -070036// read a file into a buffer, return buffer and update size
37static uint8_t *ReadFile(const char *filename, uint64_t *size) {
38 FILE *f;
39 uint8_t *buf;
40
41 f = fopen(filename, "rb");
42 if (!f) {
43 return NULL;
44 }
45
46 fseek(f, 0, SEEK_END);
47 *size = ftell(f);
48 rewind(f);
49
50 buf = malloc(*size);
51 if (!buf) {
52 fclose(f);
53 return NULL;
54 }
55
56 if(1 != fread(buf, *size, 1, f)) {
57 fclose(f);
58 free(buf);
59 return NULL;
60 }
61
62 fclose(f);
63 return buf;
64}
65
Bill Richardson4a209312010-07-02 11:34:38 -070066int cmd_find(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080067
68 CgptFindParams params;
69 memset(&params, 0, sizeof(params));
70
Bill Richardson4a209312010-07-02 11:34:38 -070071 int i;
Bill Richardson4a209312010-07-02 11:34:38 -070072 int errorcnt = 0;
Bill Richardson0697e3f2010-08-17 16:58:46 -070073 char *e = 0;
Bill Richardson4a209312010-07-02 11:34:38 -070074 int c;
Bill Richardson0697e3f2010-08-17 16:58:46 -070075
Bill Richardson4a209312010-07-02 11:34:38 -070076 opterr = 0; // quiet, you
Nam T. Nguyenab899592014-11-13 19:30:46 -080077 while ((c=getopt(argc, argv, ":hv1nt:u:l:M:O:D:")) != -1)
Bill Richardson4a209312010-07-02 11:34:38 -070078 {
79 switch (c)
80 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080081 case 'D':
82 params.drive_size = strtoull(optarg, &e, 0);
83 if (!*optarg || (e && *e))
84 {
85 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
86 errorcnt++;
87 }
88 break;
Bill Richardson4a209312010-07-02 11:34:38 -070089 case 'v':
Jay Srinivasana0581432012-01-26 21:50:05 -080090 params.verbose++;
Bill Richardson4a209312010-07-02 11:34:38 -070091 break;
92 case 'n':
Jay Srinivasana0581432012-01-26 21:50:05 -080093 params.numeric = 1;
Bill Richardson4a209312010-07-02 11:34:38 -070094 break;
95 case '1':
Jay Srinivasana0581432012-01-26 21:50:05 -080096 params.oneonly = 1;
Bill Richardson4a209312010-07-02 11:34:38 -070097 break;
98 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -080099 params.set_label = 1;
100 params.label = optarg;
Bill Richardson4a209312010-07-02 11:34:38 -0700101 break;
102 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -0800103 params.set_type = 1;
104 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
105 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardson4a209312010-07-02 11:34:38 -0700106 Error("invalid argument to -%c: %s\n", c, optarg);
107 errorcnt++;
108 }
109 break;
110 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -0800111 params.set_unique = 1;
112 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardson4a209312010-07-02 11:34:38 -0700113 Error("invalid argument to -%c: %s\n", c, optarg);
114 errorcnt++;
115 }
116 break;
Bill Richardson0697e3f2010-08-17 16:58:46 -0700117 case 'M':
Jay Srinivasana0581432012-01-26 21:50:05 -0800118 params.matchbuf = ReadFile(optarg, &params.matchlen);
119 if (!params.matchbuf || !params.matchlen) {
Bill Richardson0697e3f2010-08-17 16:58:46 -0700120 Error("Unable to read from %s\n", optarg);
121 errorcnt++;
122 }
123 // Go ahead and allocate space for the comparison too
Jay Srinivasana0581432012-01-26 21:50:05 -0800124 params.comparebuf = (uint8_t *)malloc(params.matchlen);
125 if (!params.comparebuf) {
Bill Richardson0697e3f2010-08-17 16:58:46 -0700126 Error("Unable to allocate %" PRIu64 "bytes for comparison buffer\n",
Jay Srinivasana0581432012-01-26 21:50:05 -0800127 params.matchlen);
Bill Richardson0697e3f2010-08-17 16:58:46 -0700128 errorcnt++;
129 }
130 break;
131 case 'O':
Jay Srinivasana0581432012-01-26 21:50:05 -0800132 params.matchoffset = strtoull(optarg, &e, 0);
Bill Richardson0697e3f2010-08-17 16:58:46 -0700133 if (!*optarg || (e && *e)) {
134 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
135 errorcnt++;
136 }
137 break;
Bill Richardson4a209312010-07-02 11:34:38 -0700138
139 case 'h':
140 Usage();
141 return CGPT_OK;
142 case '?':
143 Error("unrecognized option: -%c\n", optopt);
144 errorcnt++;
145 break;
146 case ':':
147 Error("missing argument to -%c\n", optopt);
148 errorcnt++;
149 break;
150 default:
151 errorcnt++;
152 break;
153 }
154 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800155 if (!params.set_unique && !params.set_type && !params.set_label) {
Bill Richardson0697e3f2010-08-17 16:58:46 -0700156 Error("You must specify at least one of -t, -u, or -l\n");
157 errorcnt++;
158 }
Bill Richardson4a209312010-07-02 11:34:38 -0700159 if (errorcnt)
160 {
161 Usage();
162 return CGPT_FAILED;
163 }
164
Bill Richardson4a209312010-07-02 11:34:38 -0700165 if (optind < argc) {
Jay Srinivasana0581432012-01-26 21:50:05 -0800166 for (i=optind; i<argc; i++) {
Jay Srinivasan250549d2012-02-16 17:40:45 -0800167 params.drive_name = argv[i];
Bill Richardson3f806a22013-03-20 15:02:34 -0700168 CgptFind(&params);
Jay Srinivasana0581432012-01-26 21:50:05 -0800169 }
Bill Richardson4a209312010-07-02 11:34:38 -0700170 } else {
Bill Richardson3f806a22013-03-20 15:02:34 -0700171 CgptFind(&params);
Bill Richardson4a209312010-07-02 11:34:38 -0700172 }
173
Jay Srinivasana0581432012-01-26 21:50:05 -0800174 if (params.oneonly && params.hits != 1) {
Bill Richardson4a209312010-07-02 11:34:38 -0700175 return CGPT_FAILED;
176 }
177
Jay Srinivasana0581432012-01-26 21:50:05 -0800178 if (params.match_partnum) {
Bill Richardson4a209312010-07-02 11:34:38 -0700179 return CGPT_OK;
180 }
181
182 return CGPT_FAILED;
183}