cgpt_boot.c revision 3f806a2abf07d7b801852a4a6f3a9080a4b5c427
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 5#include <errno.h> 6#include <fcntl.h> 7#include <string.h> 8#include <unistd.h> 9 10#include "cgpt.h" 11#include "cgpt_params.h" 12#include "cgptlib_internal.h" 13#include "endian.h" 14 15 16int CgptGetBootPartitionNumber(CgptBootParams *params) { 17 struct drive drive; 18 int gpt_retval= 0; 19 int retval; 20 21 if (params == NULL) 22 return CGPT_FAILED; 23 24 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY)) 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"); 36 retval = CGPT_FAILED; 37 goto done; 38 } 39 40 char buf[GUID_STRLEN]; 41 GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf)); 42 43 int numEntries = GetNumberOfEntries(&drive.gpt); 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 59done: 60 (void) DriveClose(&drive, 1); 61 return retval; 62} 63 64 65int CgptBoot(CgptBootParams *params) { 66 struct drive drive; 67 int retval = 1; 68 int gpt_retval= 0; 69 int mode = O_RDONLY; 70 71 if (params == NULL) 72 return CGPT_FAILED; 73 74 if (params->create_pmbr || params->partition || params->bootfile) 75 mode = O_RDWR; 76 77 if (CGPT_OK != DriveOpen(params->drive_name, &drive, mode)) { 78 return CGPT_FAILED; 79 } 80 81 if (CGPT_OK != ReadPMBR(&drive)) { 82 Error("Unable to read PMBR\n"); 83 goto done; 84 } 85 86 if (params->create_pmbr) { 87 drive.pmbr.magic[0] = 0x1d; 88 drive.pmbr.magic[1] = 0x9a; 89 drive.pmbr.sig[0] = 0x55; 90 drive.pmbr.sig[1] = 0xaa; 91 memset(&drive.pmbr.part, 0, sizeof(drive.pmbr.part)); 92 drive.pmbr.part[0].f_head = 0x00; 93 drive.pmbr.part[0].f_sect = 0x02; 94 drive.pmbr.part[0].f_cyl = 0x00; 95 drive.pmbr.part[0].type = 0xee; 96 drive.pmbr.part[0].l_head = 0xff; 97 drive.pmbr.part[0].l_sect = 0xff; 98 drive.pmbr.part[0].l_cyl = 0xff; 99 drive.pmbr.part[0].f_lba = htole32(1); 100 uint32_t max = 0xffffffff; 101 if (drive.gpt.drive_sectors < 0xffffffff) 102 max = drive.gpt.drive_sectors - 1; 103 drive.pmbr.part[0].num_sect = htole32(max); 104 } 105 106 if (params->partition) { 107 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { 108 Error("GptSanityCheck() returned %d: %s\n", 109 gpt_retval, GptError(gpt_retval)); 110 goto done; 111 } 112 113 if (params->partition > GetNumberOfEntries(&drive.gpt)) { 114 Error("invalid partition number: %d\n", params->partition); 115 goto done; 116 } 117 118 uint32_t index = params->partition - 1; 119 GptEntry *entry = GetEntry(&drive.gpt, ANY_VALID, index); 120 memcpy(&drive.pmbr.boot_guid, &entry->unique, sizeof(Guid)); 121 } 122 123 if (params->bootfile) { 124 int fd = open(params->bootfile, O_RDONLY); 125 if (fd < 0) { 126 Error("Can't read %s: %s\n", params->bootfile, strerror(errno)); 127 goto done; 128 } 129 130 int n = read(fd, drive.pmbr.bootcode, sizeof(drive.pmbr.bootcode)); 131 if (n < 1) { 132 Error("problem reading %s: %s\n", params->bootfile, strerror(errno)); 133 close(fd); 134 goto done; 135 } 136 137 close(fd); 138 } 139 140 char buf[GUID_STRLEN]; 141 GuidToStr(&drive.pmbr.boot_guid, buf, sizeof(buf)); 142 printf("%s\n", buf); 143 144 // Write it all out, if needed. 145 if (mode == O_RDONLY || CGPT_OK == WritePMBR(&drive)) 146 retval = 0; 147 148done: 149 (void) DriveClose(&drive, 1); 150 return retval; 151} 152