blob: d8976c422d6f5d599504ad70154d93714dab3a79 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 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
Jay Srinivasana0581432012-01-26 21:50:05 -08005#include <errno.h>
6#include <fcntl.h>
7#include <string.h>
Bill Richardsonf4729192012-05-02 23:30:16 -07008#include <unistd.h>
Jay Srinivasana0581432012-01-26 21:50:05 -08009
Bill Richardsonf4729192012-05-02 23:30:16 -070010#include "cgpt.h"
11#include "cgpt_params.h"
Jay Srinivasana0581432012-01-26 21:50:05 -080012#include "cgptlib_internal.h"
Bill Richardson0c3ba242013-03-29 11:09:30 -070013#include "vboot_host.h"
Jay Srinivasan250549d2012-02-16 17:40:45 -080014
Bill Richardson3f806a22013-03-20 15:02:34 -070015int CgptGetBootPartitionNumber(CgptBootParams *params) {
Jay Srinivasan250549d2012-02-16 17:40:45 -080016 struct drive drive;
17 int gpt_retval= 0;
18 int retval;
19
20 if (params == NULL)
21 return CGPT_FAILED;
22
Nam T. Nguyenab899592014-11-13 19:30:46 -080023 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY,
24 params->drive_size))
Jay Srinivasan250549d2012-02-16 17:40:45 -080025 return CGPT_FAILED;
26
27 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
28 Error("GptSanityCheck() returned %d: %s\n",
29 gpt_retval, GptError(gpt_retval));
30 retval = CGPT_FAILED;
31 goto done;
32 }
33
34 if (CGPT_OK != ReadPMBR(&drive)) {
35 Error("Unable to read PMBR\n");
Taylor Hutt5b8210b2012-07-31 11:16:23 -070036 retval = CGPT_FAILED;
Jay Srinivasan250549d2012-02-16 17:40:45 -080037 goto done;
38 }
39
40 char buf[GUID_STRLEN];
41 GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf));
42
Albert Chaulkfa6b35c2013-03-26 13:43:02 -070043 int numEntries = GetNumberOfEntries(&drive);
Jay Srinivasan250549d2012-02-16 17:40:45 -080044 int i;
45 for(i = 0; i < numEntries; i++) {
46 GptEntry *entry = GetEntry(&drive.gpt, ANY_VALID, i);
47
48 if (GuidEqual(&entry->unique, &drive.pmbr.boot_guid)) {
49 params->partition = i + 1;
50 retval = CGPT_OK;
51 goto done;
52 }
53 }
54
55 Error("Didn't find any boot partition\n");
56 params->partition = 0;
57 retval = CGPT_FAILED;
58
59done:
60 (void) DriveClose(&drive, 1);
61 return retval;
62}
63
64
Bill Richardson3f806a22013-03-20 15:02:34 -070065int CgptBoot(CgptBootParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080066 struct drive drive;
67 int retval = 1;
68 int gpt_retval= 0;
Bill Richardson23429d32012-04-30 11:33:13 -070069 int mode = O_RDONLY;
Jay Srinivasana0581432012-01-26 21:50:05 -080070
71 if (params == NULL)
72 return CGPT_FAILED;
73
Bill Richardson23429d32012-04-30 11:33:13 -070074 if (params->create_pmbr || params->partition || params->bootfile)
75 mode = O_RDWR;
76
Nam T. Nguyenab899592014-11-13 19:30:46 -080077 if (CGPT_OK != DriveOpen(params->drive_name, &drive, mode,
78 params->drive_size)) {
Jay Srinivasana0581432012-01-26 21:50:05 -080079 return CGPT_FAILED;
Bill Richardson23429d32012-04-30 11:33:13 -070080 }
Jay Srinivasana0581432012-01-26 21:50:05 -080081
82 if (CGPT_OK != ReadPMBR(&drive)) {
83 Error("Unable to read PMBR\n");
84 goto done;
85 }
86
87 if (params->create_pmbr) {
88 drive.pmbr.magic[0] = 0x1d;
89 drive.pmbr.magic[1] = 0x9a;
90 drive.pmbr.sig[0] = 0x55;
91 drive.pmbr.sig[1] = 0xaa;
92 memset(&drive.pmbr.part, 0, sizeof(drive.pmbr.part));
93 drive.pmbr.part[0].f_head = 0x00;
94 drive.pmbr.part[0].f_sect = 0x02;
95 drive.pmbr.part[0].f_cyl = 0x00;
96 drive.pmbr.part[0].type = 0xee;
97 drive.pmbr.part[0].l_head = 0xff;
98 drive.pmbr.part[0].l_sect = 0xff;
99 drive.pmbr.part[0].l_cyl = 0xff;
100 drive.pmbr.part[0].f_lba = htole32(1);
101 uint32_t max = 0xffffffff;
Dan Ehrenbergb3d38f52014-12-09 13:42:15 -0800102 if (drive.gpt.streaming_drive_sectors < 0xffffffff)
103 max = drive.gpt.streaming_drive_sectors - 1;
Jay Srinivasana0581432012-01-26 21:50:05 -0800104 drive.pmbr.part[0].num_sect = htole32(max);
105 }
106
107 if (params->partition) {
108 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
109 Error("GptSanityCheck() returned %d: %s\n",
110 gpt_retval, GptError(gpt_retval));
111 goto done;
112 }
113
Albert Chaulkfa6b35c2013-03-26 13:43:02 -0700114 if (params->partition > GetNumberOfEntries(&drive)) {
Jay Srinivasana0581432012-01-26 21:50:05 -0800115 Error("invalid partition number: %d\n", params->partition);
116 goto done;
117 }
118
119 uint32_t index = params->partition - 1;
120 GptEntry *entry = GetEntry(&drive.gpt, ANY_VALID, index);
121 memcpy(&drive.pmbr.boot_guid, &entry->unique, sizeof(Guid));
122 }
123
124 if (params->bootfile) {
125 int fd = open(params->bootfile, O_RDONLY);
126 if (fd < 0) {
127 Error("Can't read %s: %s\n", params->bootfile, strerror(errno));
128 goto done;
129 }
130
131 int n = read(fd, drive.pmbr.bootcode, sizeof(drive.pmbr.bootcode));
132 if (n < 1) {
133 Error("problem reading %s: %s\n", params->bootfile, strerror(errno));
134 close(fd);
135 goto done;
136 }
137
138 close(fd);
139 }
140
141 char buf[GUID_STRLEN];
142 GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf));
143 printf("%s\n", buf);
144
Bill Richardson23429d32012-04-30 11:33:13 -0700145 // Write it all out, if needed.
146 if (mode == O_RDONLY || CGPT_OK == WritePMBR(&drive))
Jay Srinivasana0581432012-01-26 21:50:05 -0800147 retval = 0;
148
149done:
150 (void) DriveClose(&drive, 1);
151 return retval;
152}