Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 1 | // 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 Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <string.h> |
Bill Richardson | f472919 | 2012-05-02 23:30:16 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 9 | |
Bill Richardson | f472919 | 2012-05-02 23:30:16 -0700 | [diff] [blame] | 10 | #include "cgpt.h" |
| 11 | #include "cgpt_params.h" |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 12 | #include "cgptlib_internal.h" |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 13 | #include "vboot_host.h" |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 14 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 15 | int CgptGetBootPartitionNumber(CgptBootParams *params) { |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 16 | struct drive drive; |
| 17 | int gpt_retval= 0; |
| 18 | int retval; |
| 19 | |
| 20 | if (params == NULL) |
| 21 | return CGPT_FAILED; |
| 22 | |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 23 | if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY, |
| 24 | params->drive_size)) |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 25 | 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 Hutt | 5b8210b | 2012-07-31 11:16:23 -0700 | [diff] [blame] | 36 | retval = CGPT_FAILED; |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 37 | goto done; |
| 38 | } |
| 39 | |
| 40 | char buf[GUID_STRLEN]; |
| 41 | GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf)); |
| 42 | |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 43 | int numEntries = GetNumberOfEntries(&drive); |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 44 | 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 | |
| 59 | done: |
| 60 | (void) DriveClose(&drive, 1); |
| 61 | return retval; |
| 62 | } |
| 63 | |
| 64 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 65 | int CgptBoot(CgptBootParams *params) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 66 | struct drive drive; |
| 67 | int retval = 1; |
| 68 | int gpt_retval= 0; |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 69 | int mode = O_RDONLY; |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 70 | |
| 71 | if (params == NULL) |
| 72 | return CGPT_FAILED; |
| 73 | |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 74 | if (params->create_pmbr || params->partition || params->bootfile) |
| 75 | mode = O_RDWR; |
| 76 | |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 77 | if (CGPT_OK != DriveOpen(params->drive_name, &drive, mode, |
| 78 | params->drive_size)) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 79 | return CGPT_FAILED; |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 80 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 81 | |
| 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; |
| 102 | if (drive.gpt.drive_sectors < 0xffffffff) |
| 103 | max = drive.gpt.drive_sectors - 1; |
| 104 | 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 Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 114 | if (params->partition > GetNumberOfEntries(&drive)) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 115 | 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 Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 145 | // Write it all out, if needed. |
| 146 | if (mode == O_RDONLY || CGPT_OK == WritePMBR(&drive)) |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 147 | retval = 0; |
| 148 | |
| 149 | done: |
| 150 | (void) DriveClose(&drive, 1); |
| 151 | return retval; |
| 152 | } |