summaryrefslogtreecommitdiff
path: root/usr/src/cmd/luxadm/setboot.c
blob: 909d3121112a0b6ee9517ab056aeac198e3fe8e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * I18N message number ranges
 *  This file: 6000 - 6499
 *  Shared common messages: 1 - 1999
 */



#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/mnttab.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/openpromio.h>


/*
 * For i18n
 */
#include <stgcom.h>


/*
 * 128 is the size of the largest (currently) property name
 * 8192 - MAXPROPSIZE - sizeof (int) is the size of the largest
 * (currently) property value, viz. nvramrc.
 * the sizeof(uint_t) is from struct openpromio
 */
#define	MAXPROPSIZE		128
#define	MAXVALSIZE		(8192 - MAXPROPSIZE - sizeof (uint_t))

#define	BOOTDEV_PROP_NAME	"boot-device"

static int getbootdevname(char *, char *);
static int setprom(unsigned, unsigned, char *);
extern int devfs_dev_to_prom_name(char *, char *);

/*
 * Call getbootdevname() to get the absolute pathname of boot device
 * and call setprom() to set the boot-device variable.
 */
int
setboot(unsigned int yes, unsigned int verbose, char *fname)
{
	char	bdev[MAXPATHLEN];

	if (!getbootdevname(fname, bdev)) {
		(void) fprintf(stderr, MSGSTR(6000,
			"Cannot determine device name for %s\n"),
			fname);
		return (errno);
	}

	return (setprom(yes, verbose, bdev));
}

/*
 * Read the mnttab and resolve the special device of the fs we are
 * interested in, into an absolute pathname
 */
static int
getbootdevname(char *bootfs, char *bdev)
{
	FILE *f;
	char *fname;
	char *devname;
	struct mnttab m;
	struct stat sbuf;
	int mountpt = 0;
	int found = 0;

	devname = bootfs;

	if (stat(bootfs, &sbuf) < 0) {
		perror(MSGSTR(6001, "stat"));
		return (0);
	}

	switch (sbuf.st_mode & S_IFMT) {
		case S_IFBLK:
			break;
		default:
			mountpt = 1;
			break;
	}

	if (mountpt) {
		fname = MNTTAB;
		f = fopen(fname, "r");
		if (f == NULL) {
			perror(fname);
			return (0);
		}

		while (getmntent(f, &m) == 0) {
			if (strcmp(m.mnt_mountp, bootfs))
				continue;
			else {
				found = 1;
				break;
			}
		}

		(void) fclose(f);

		if (!found) {
			return (0);
		}
		devname = m.mnt_special;
	}

	if (devfs_dev_to_prom_name(devname, bdev) != 0) {
		perror(devname);
		return (0);
	}

	return (1);
}

/*
 * setprom() - use /dev/openprom to read the "boot_device" variable and set
 * it to the new value.
 */
static int
setprom(unsigned yes, unsigned verbose, char *bdev)
{
	struct openpromio	*pio;
	int			fd;
	char			save_bootdev[MAXVALSIZE];

	if ((fd = open("/dev/openprom", O_RDWR)) < 0) {
		perror(MSGSTR(6002, "Could not open openprom dev"));
		return (errno);
	}

	pio = (struct openpromio *)malloc(sizeof (struct openpromio) +
					MAXVALSIZE + MAXPROPSIZE);

	if (pio == (struct openpromio *)NULL) {
		perror(MSGSTR(6003, " Error: Unable to allocate memory."));
		return (errno);
	}

	pio->oprom_size = MAXVALSIZE;
	(void) strcpy(pio->oprom_array, BOOTDEV_PROP_NAME);

	if (ioctl(fd, OPROMGETOPT, pio) < 0) {
		perror(MSGSTR(6004, "openprom getopt ioctl"));
		return (errno);
	}

	/*
	 * save the existing boot-device, so we can use it if setting
	 * to new value fails.
	 */
	(void) strcpy(save_bootdev, pio->oprom_array);

	if (verbose) {
		(void) fprintf(stdout,
			MSGSTR(6005,
			"Current boot-device = %s\n"), pio->oprom_array);
		(void) fprintf(stdout, MSGSTR(6006,
			"New boot-device = %s\n"), bdev);
	}

	if (!yes) {
		(void) fprintf(stdout, MSGSTR(6007,
			"Do you want to change boot-device "
			"to the new setting? (y/n) "));
		switch (getchar()) {
			case 'Y':
			case 'y':
				break;
			default:
				return (0);
		}
	}

	/* set the new value for boot-device */

	pio->oprom_size = (int)strlen(BOOTDEV_PROP_NAME) + 1 +
				(int)strlen(bdev);

	(void) strcpy(pio->oprom_array, BOOTDEV_PROP_NAME);
	(void) strcpy(pio->oprom_array + (int)strlen(BOOTDEV_PROP_NAME) + 1,
					bdev);

	if (ioctl(fd, OPROMSETOPT, pio) < 0) {
		perror(MSGSTR(6008, "openprom setopt ioctl"));
		return (errno);
	}

	/* read back the value that was set */

	pio->oprom_size = MAXVALSIZE;
	(void) strcpy(pio->oprom_array, BOOTDEV_PROP_NAME);

	if (ioctl(fd, OPROMGETOPT, pio) < 0) {
		perror(MSGSTR(6009, "openprom getopt ioctl"));
		return (errno);
	}

	if (strcmp(bdev, pio->oprom_array)) {

		/* could not  set the new device name, set the old one back */

		perror(MSGSTR(6010,
			"Could not set boot-device, reverting to old value"));
		pio->oprom_size = (int)strlen(BOOTDEV_PROP_NAME) + 1 +
			(int)strlen(save_bootdev);

		(void) strcpy(pio->oprom_array, BOOTDEV_PROP_NAME);
			(void) strcpy(pio->oprom_array +
				(int)strlen(BOOTDEV_PROP_NAME) + 1,
				save_bootdev);

		if (ioctl(fd, OPROMSETOPT, pio) < 0) {
			perror(MSGSTR(6011, "openprom setopt ioctl"));
			return (errno);
		}

	}

	(void) close(fd);

	return (0);
}