1/* 2 * Copyright (c) 2001-2007 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23/* 24 * BLSetOpenFirmwareBootDevice.c 25 * bless 26 * 27 * Created by Shantonu Sen <ssen@apple.com> on Tue Apr 17 2001. 28 * Copyright (c) 2001-2007 Apple Inc. All Rights Reserved. 29 * 30 * $Id: BLSetOpenFirmwareBootDevice.c,v 1.18 2006/02/20 22:49:57 ssen Exp $ 31 * 32 */ 33#include <stdlib.h> 34#include <stdio.h> 35#include <unistd.h> 36#include <string.h> 37#include <sys/param.h> 38#include <sys/stat.h> 39#include <sys/wait.h> 40 41#include "bless.h" 42#include "bless_private.h" 43 44#define NVRAM "/usr/sbin/nvram" 45 46int BLSetOpenFirmwareBootDevice(BLContextPtr context, const char * mntfrm) { 47 char ofString[1024]; 48 int err; 49 50 char * OFSettings[6]; 51 52 char bootdevice[1024]; 53 char bootfile[1024]; 54 char bootcommand[1024]; 55 char bootargs[1024]; // always zero out bootargs 56 57 pid_t p; 58 int status; 59 60 OFSettings[0] = NVRAM; 61 err = BLGetOpenFirmwareBootDevice(context, mntfrm, ofString); 62 if(err) { 63 contextprintf(context, kBLLogLevelError, "Can't get Open Firmware information\n" ); 64 return 1; 65 } else { 66 contextprintf(context, kBLLogLevelVerbose, "Got OF string %s\n", ofString ); 67 } 68 69 sprintf(bootargs, "boot-args="); 70 71 char oldbootargs[1024]; 72 char *restargs; 73 FILE *pop; 74 75 oldbootargs[0] = '\0'; 76 77 pop = popen("/usr/sbin/nvram boot-args", "r"); 78 if(pop) { 79 80 if(NULL == fgets(oldbootargs, (int)sizeof(oldbootargs), pop)) { 81 contextprintf(context, kBLLogLevelVerbose, "Could not parse output from /usr/sbin/nvram\n" ); 82 } 83 pclose(pop); 84 85 restargs = oldbootargs; 86 if(NULL != strsep(&restargs, "\t")) { // nvram must separate the name from the value with a tab 87 restargs[strlen(restargs)-1] = '\0'; // remove \n 88 89 err = BLPreserveBootArgs(context, restargs, bootargs+strlen(bootargs), sizeof bootargs - strlen(bootargs)); 90 } 91 } 92 93 // set them up 94 sprintf(bootdevice, "boot-device=%s", ofString); 95 sprintf(bootfile, "boot-file="); 96 sprintf(bootcommand, "boot-command=mac-boot"); 97 // bootargs initialized above, and append-to later 98 99 OFSettings[1] = bootdevice; 100 OFSettings[2] = bootfile; 101 OFSettings[3] = bootcommand; 102 OFSettings[4] = bootargs; 103 OFSettings[5] = NULL; 104 105 contextprintf(context, kBLLogLevelVerbose, "OF Setings:\n" ); 106 contextprintf(context, kBLLogLevelVerbose, "\t\tprogram: %s\n", OFSettings[0] ); 107 contextprintf(context, kBLLogLevelVerbose, "\t\t%s\n", OFSettings[1] ); 108 contextprintf(context, kBLLogLevelVerbose, "\t\t%s\n", OFSettings[2] ); 109 contextprintf(context, kBLLogLevelVerbose, "\t\t%s\n", OFSettings[3] ); 110 contextprintf(context, kBLLogLevelVerbose, "\t\t%s\n", OFSettings[4] ); 111 112 p = fork(); 113 if (p == 0) { 114 int ret = execv(NVRAM, OFSettings); 115 if(ret == -1) { 116 contextprintf(context, kBLLogLevelError, "Could not exec %s\n", NVRAM ); 117 } 118 _exit(1); 119 } 120 121 do { 122 p = wait(&status); 123 } while (p == -1 && errno == EINTR); 124 125 if(p == -1 || status) { 126 contextprintf(context, kBLLogLevelError, "%s returned non-0 exit status\n", NVRAM ); 127 return 3; 128 } 129 130 return 0; 131} 132