Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: cgpt/cmd_boot.c

Issue 3594010: Address some security concerns in the cgpt tool. (Closed) Base URL: http://git.chromium.org/git/vboot_reference.git
Patch Set: A little more cleanup. Take one more look, please. Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cgpt/cmd_add.c ('k') | cgpt/cmd_create.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cgpt.h" 5 #include "cgpt.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <getopt.h> 9 #include <getopt.h>
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 16 matching lines...) Expand all
27 " -b FILE Install bootloader code in the PMBR\n" 27 " -b FILE Install bootloader code in the PMBR\n"
28 " -p Create legacy PMBR partition table\n" 28 " -p Create legacy PMBR partition table\n"
29 "\n" 29 "\n"
30 "With no options, it will just print the PMBR boot guid\n" 30 "With no options, it will just print the PMBR boot guid\n"
31 "\n", progname); 31 "\n", progname);
32 } 32 }
33 33
34 34
35 int cmd_boot(int argc, char *argv[]) { 35 int cmd_boot(int argc, char *argv[]) {
36 struct drive drive; 36 struct drive drive;
37 int partition = 0; 37 uint32_t partition = 0;
38 char *bootfile = 0; 38 char *bootfile = 0;
39 int create_pmbr = 0; 39 int create_pmbr = 0;
40 int retval = 1; 40 int retval = 1;
41 int gpt_retval; 41 int gpt_retval;
42 42
43 int c; 43 int c;
44 int errorcnt = 0; 44 int errorcnt = 0;
45 char *e = 0; 45 char *e = 0;
46 46
47 opterr = 0; // quiet, you 47 opterr = 0; // quiet, you
48 while ((c=getopt(argc, argv, ":hi:b:p")) != -1) 48 while ((c=getopt(argc, argv, ":hi:b:p")) != -1)
49 { 49 {
50 switch (c) 50 switch (c)
51 { 51 {
52 case 'i': 52 case 'i':
53 partition = (uint32_t)strtoul(optarg, &e, 0); 53 partition = (uint32_t)strtoul(optarg, &e, 0);
54 if (!*optarg || (e && *e)) 54 if (!*optarg || (e && *e))
55 { 55 {
56 Error("invalid argument to -%c: \"%s\"\n", c, optarg); 56 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57 errorcnt++; 57 errorcnt++;
58 } 58 }
59 break; 59 break;
60 case 'b': 60 case 'b':
61 bootfile = optarg; 61 bootfile = optarg;
62 break; 62 break;
63 case 'p': 63 case 'p':
64 create_pmbr = 1; 64 create_pmbr = 1;
65 break; 65 break;
66 66
67 case 'h': 67 case 'h':
68 Usage(); 68 Usage();
69 return CGPT_OK; 69 return CGPT_OK;
70 case '?': 70 case '?':
71 Error("unrecognized option: -%c\n", optopt); 71 Error("unrecognized option: -%c\n", optopt);
72 errorcnt++; 72 errorcnt++;
73 break; 73 break;
74 case ':': 74 case ':':
75 Error("missing argument to -%c\n", optopt); 75 Error("missing argument to -%c\n", optopt);
76 errorcnt++; 76 errorcnt++;
(...skipping 14 matching lines...) Expand all
91 return CGPT_FAILED; 91 return CGPT_FAILED;
92 } 92 }
93 93
94 if (CGPT_OK != DriveOpen(argv[optind], &drive)) 94 if (CGPT_OK != DriveOpen(argv[optind], &drive))
95 return CGPT_FAILED; 95 return CGPT_FAILED;
96 96
97 if (CGPT_OK != ReadPMBR(&drive)) { 97 if (CGPT_OK != ReadPMBR(&drive)) {
98 Error("Unable to read PMBR\n"); 98 Error("Unable to read PMBR\n");
99 goto done; 99 goto done;
100 } 100 }
101 101
102 if (create_pmbr) { 102 if (create_pmbr) {
103 drive.pmbr.magic[0] = 0x1d; 103 drive.pmbr.magic[0] = 0x1d;
104 drive.pmbr.magic[1] = 0x9a; 104 drive.pmbr.magic[1] = 0x9a;
105 drive.pmbr.sig[0] = 0x55; 105 drive.pmbr.sig[0] = 0x55;
106 drive.pmbr.sig[1] = 0xaa; 106 drive.pmbr.sig[1] = 0xaa;
107 memset(&drive.pmbr.part, 0, sizeof(drive.pmbr.part)); 107 memset(&drive.pmbr.part, 0, sizeof(drive.pmbr.part));
108 drive.pmbr.part[0].f_head = 0x00; 108 drive.pmbr.part[0].f_head = 0x00;
109 drive.pmbr.part[0].f_sect = 0x02; 109 drive.pmbr.part[0].f_sect = 0x02;
110 drive.pmbr.part[0].f_cyl = 0x00; 110 drive.pmbr.part[0].f_cyl = 0x00;
111 drive.pmbr.part[0].type = 0xee; 111 drive.pmbr.part[0].type = 0xee;
(...skipping 12 matching lines...) Expand all
124 Error("GptSanityCheck() returned %d: %s\n", 124 Error("GptSanityCheck() returned %d: %s\n",
125 gpt_retval, GptError(gpt_retval)); 125 gpt_retval, GptError(gpt_retval));
126 goto done; 126 goto done;
127 } 127 }
128 128
129 if (partition > GetNumberOfEntries(&drive.gpt)) { 129 if (partition > GetNumberOfEntries(&drive.gpt)) {
130 Error("invalid partition number: %d\n", partition); 130 Error("invalid partition number: %d\n", partition);
131 goto done; 131 goto done;
132 } 132 }
133 133
134 int index = partition - 1; 134 uint32_t index = partition - 1;
135 GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index); 135 GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index);
136 memcpy(&drive.pmbr.boot_guid, &entry->unique, sizeof(Guid)); 136 memcpy(&drive.pmbr.boot_guid, &entry->unique, sizeof(Guid));
137 } 137 }
138 138
139 if (bootfile) { 139 if (bootfile) {
140 int fd = open(bootfile, O_RDONLY); 140 int fd = open(bootfile, O_RDONLY);
141 if (fd < 0) { 141 if (fd < 0) {
142 Error("Can't read %s: %s\n", bootfile, strerror(errno)); 142 Error("Can't read %s: %s\n", bootfile, strerror(errno));
143 goto done; 143 goto done;
144 } 144 }
145 145
146 int n = read(fd, drive.pmbr.bootcode, sizeof(drive.pmbr.bootcode)); 146 int n = read(fd, drive.pmbr.bootcode, sizeof(drive.pmbr.bootcode));
147 if (n < 1) { 147 if (n < 1) {
148 Error("problem reading %s: %s\n", bootfile, strerror(errno)); 148 Error("problem reading %s: %s\n", bootfile, strerror(errno));
149 close(fd); 149 close(fd);
150 goto done; 150 goto done;
151 } 151 }
152 152
153 close(fd); 153 close(fd);
154 } 154 }
155 155
156 char buf[256]; 156 char buf[GUID_STRLEN];
157 GuidToStr(&drive.pmbr.boot_guid, buf); 157 GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf));
158 printf("%s\n", buf); 158 printf("%s\n", buf);
159 159
160 // Write it all out 160 // Write it all out
161 if (CGPT_OK == WritePMBR(&drive)) 161 if (CGPT_OK == WritePMBR(&drive))
162 retval = 0; 162 retval = 0;
163 163
164 done: 164 done:
165 (void) DriveClose(&drive, 1); 165 (void) DriveClose(&drive, 1);
166 return retval; 166 return retval;
167 } 167 }
OLDNEW
« no previous file with comments | « cgpt/cmd_add.c ('k') | cgpt/cmd_create.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698