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

Side by Side Diff: cgpt/cmd_boot.c

Issue 2719008: Nearly complete rewrite of cgpt tool. (Closed) Base URL: ssh://git@chromiumos-git//vboot_reference.git
Patch Set: Created 10 years, 6 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
« 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "cgpt.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <getopt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <uuid/uuid.h>
17
18 #include "cgptlib_internal.h"
19 #include "endian.h"
20
21 static void Usage(void)
22 {
23 printf("\nUsage: %s boot [OPTIONS] DRIVE\n\n"
24 "Edit the PMBR sector for legacy BIOSes\n\n"
25 "Options:\n"
26 " -i NUM Set bootable partition\n"
27 " -b FILE Install bootloader code in the PMBR\n"
28 " -p Create legacy PMBR partition table\n"
29 "\n"
30 "With no options, it will just print the PMBR boot guid\n"
31 "\n", progname);
32 }
33
34
35 int cmd_boot(int argc, char *argv[]) {
36 struct drive drive;
37 int partition = 0;
38 char *bootfile = 0;
39 int create_pmbr = 0;
40 int retval = 1;
41 int gpt_retval;
42
43 int c;
44 int errorcnt = 0;
45 char *e = 0;
46
47 opterr = 0; // quiet, you
48 while ((c=getopt(argc, argv, ":hi:b:p")) != -1)
49 {
50 switch (c)
51 {
52 case 'i':
53 partition = (uint32_t)strtoul(optarg, &e, 0);
54 if (!*optarg || (e && *e))
55 {
56 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57 errorcnt++;
58 }
59 break;
60 case 'b':
61 bootfile = optarg;
62 break;
63 case 'p':
64 create_pmbr = 1;
65 break;
66
67 case 'h':
68 Usage();
69 return CGPT_OK;
70 case '?':
71 Error("unrecognized option: -%c\n", optopt);
72 errorcnt++;
73 break;
74 case ':':
75 Error("missing argument to -%c\n", optopt);
76 errorcnt++;
77 break;
78 default:
79 errorcnt++;
80 break;
81 }
82 }
83 if (errorcnt)
84 {
85 Usage();
86 return CGPT_FAILED;
87 }
88
89 if (optind >= argc) {
90 Error("missing drive argument\n");
91 return CGPT_FAILED;
92 }
93
94 if (CGPT_OK != DriveOpen(argv[optind], &drive))
95 return CGPT_FAILED;
96
97 if (CGPT_OK != ReadPMBR(&drive)) {
98 Error("Unable to read PMBR\n");
99 goto done;
100 }
101
102 if (create_pmbr) {
103 drive.pmbr.magic[0] = 0x1d;
104 drive.pmbr.magic[1] = 0x9a;
105 drive.pmbr.sig[0] = 0x55;
106 drive.pmbr.sig[1] = 0xaa;
107 memset(&drive.pmbr.part, 0, sizeof(drive.pmbr.part));
108 drive.pmbr.part[0].f_head = 0x00;
109 drive.pmbr.part[0].f_sect = 0x02;
110 drive.pmbr.part[0].f_cyl = 0x00;
111 drive.pmbr.part[0].type = 0xee;
112 drive.pmbr.part[0].l_head = 0xff;
113 drive.pmbr.part[0].l_sect = 0xff;
114 drive.pmbr.part[0].l_cyl = 0xff;
115 drive.pmbr.part[0].f_lba = htole32(1);
116 uint32_t max = 0xffffffff;
117 if (drive.gpt.drive_sectors < 0xffffffff)
118 max = drive.gpt.drive_sectors - 1;
119 drive.pmbr.part[0].num_sect = htole32(max);
120 }
121
122 if (partition) {
123 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
124 Error("GptSanityCheck() returned %d: %s\n",
125 gpt_retval, GptError(gpt_retval));
126 goto done;
127 }
128
129 if (partition > GetNumberOfEntries(&drive.gpt)) {
130 Error("invalid partition number: %d\n", partition);
131 goto done;
132 }
133
134 int index = partition - 1;
135 GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index);
136 memcpy(&drive.pmbr.boot_guid, &entry->unique, sizeof(Guid));
137 }
138
139 if (bootfile) {
140 int fd = open(bootfile, O_RDONLY);
141 if (fd < 0) {
142 Error("Can't read %s: %s\n", bootfile, strerror(errno));
143 goto done;
144 }
145
146 int n = read(fd, drive.pmbr.bootcode, sizeof(drive.pmbr.bootcode));
147 if (n < 1) {
148 Error("problem reading %s: %s\n", bootfile, strerror(errno));
149 close(fd);
150 goto done;
151 }
152
153 close(fd);
154 }
155
156 char buf[256];
157 GuidToStr(&drive.pmbr.boot_guid, buf);
158 printf("%s\n", buf);
159
160 // Write it all out
161 if (CGPT_OK == WritePMBR(&drive))
162 retval = 0;
163
164 done:
165 (void) DriveClose(&drive, 1);
166 return retval;
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