1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last attempt in the `sysinstall' line, the next
5 * generation being slated to essentially a complete rewrite.
6 *
7 * $FreeBSD$
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 * Copyright (c) 1995
12 * 	Gary J Palmer. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer,
19 *    verbatim and that no modifications are made prior to this
20 *    point in the file.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38
39/* These routines deal with getting things off of CDROM media */
40
41#include "sysinstall.h"
42#include <sys/stat.h>
43#include <sys/errno.h>
44#include <sys/param.h>
45#include <sys/wait.h>
46#include <sys/cdio.h>
47#include <unistd.h>
48#include <grp.h>
49#include <fcntl.h>
50#include <libutil.h>
51
52#define CD9660
53#include <sys/mount.h>
54#include <isofs/cd9660/cd9660_mount.h>
55#undef CD9660
56
57static Boolean cdromMounted;
58static Boolean previouslyMounted; /* Was the disc already mounted? */
59static char mountpoint[MAXPATHLEN] = "/dist";
60int CDROMInitQuiet;
61
62static void mediaEjectCDROM(Device *dev);
63
64static properties
65read_props(char *name)
66{
67	int fd;
68	properties n;
69
70	fd = open(name, O_RDONLY);
71	if (fd == -1)
72	    return NULL;
73	n = properties_read(fd);
74	close(fd);
75	return n;
76}
77
78Boolean
79mediaInitCDROM(Device *dev)
80{
81    struct iso_args	args;
82    properties cd_attr = NULL;
83    char *cp = NULL;
84    Boolean readInfo = TRUE;
85    static Boolean bogusCDOK = FALSE;
86    int err;
87
88    if (cdromMounted)
89	return TRUE;
90
91    Mkdir(mountpoint);
92    bzero(&args, sizeof(args));
93    args.fspec = dev->devname;
94    args.flags = 0;
95    err = mount("cd9660", mountpoint, MNT_RDONLY, (caddr_t) &args);
96    /* If disc inserted too recently first access generates EIO, try again */
97    if (err == -1 && errno == EIO)
98        err = mount("cd9660", mountpoint, MNT_RDONLY, (caddr_t) &args);
99    if (err == -1) {
100	if (errno == EINVAL) {
101	    msgConfirm("The disc in your drive looks more like an Audio disc than a FreeBSD release.");
102	    return FALSE;
103	}
104	if (errno == EBUSY) {
105	    /* Perhaps the CDROM drive is already mounted as /cdrom */
106	    if (file_readable("/cdrom/cdrom.inf")) {
107		previouslyMounted = TRUE;
108		strlcpy(mountpoint, "/cdrom", 7);
109		errno = 0;
110	    }
111	}
112	if (errno) {
113	    if (!CDROMInitQuiet)
114		msgConfirm("Error mounting %s on %s: %s (%u)", dev->devname,
115		    mountpoint, strerror(errno), errno);
116	    return FALSE;
117	}
118    }
119    cdromMounted = TRUE;
120
121    if (!file_readable(string_concat(mountpoint, "/cdrom.inf")) && !bogusCDOK) {
122	if (msgYesNo("Warning: The disc currently in the drive is either not a FreeBSD\n"
123		     "disc or it is an older (pre 2.1.5) FreeBSD CD which does not\n"
124		     "have a version number on it.  Do you wish to use this disc anyway?") != 0) {
125	    if (!previouslyMounted)
126		unmount(mountpoint, MNT_FORCE);
127	    cdromMounted = FALSE;
128	    return FALSE;
129	}
130	else {
131	    readInfo = FALSE;
132	    bogusCDOK = TRUE;
133	}
134    }
135
136    if (readInfo) {
137	if (!(cd_attr = read_props(string_concat(mountpoint, "/cdrom.inf")))
138	    || !(cp = property_find(cd_attr, "CD_VERSION"))) {
139	    msgConfirm("Unable to find a %s/cdrom.inf file.\n"
140		       "Either this is not a FreeBSD disc, there is a problem with\n"
141		       "the CDROM driver or something is wrong with your hardware.\n"
142		       "Please fix this problem (check the console logs on VTY2) and\n"
143		       "try again.", mountpoint);
144	}
145	else {
146	    if (variable_cmp(VAR_RELNAME, cp) &&
147		variable_cmp(VAR_RELNAME, "any") &&
148		strcmp(cp, "any") &&
149		!bogusCDOK) {
150		msgConfirm("Warning: The version of the FreeBSD disc currently in the drive\n"
151			   "(%s) does not match the version of the boot floppy\n"
152			   "(%s).\n\n"
153			   "If this is intentional, to avoid this message in the future\n"
154			   "please visit the Options editor to set the boot floppy version\n"
155			   "string to match that of the disc before selecting it as your\n"
156			   "installation media.", cp, variable_get(VAR_RELNAME));
157
158		if (msgYesNo("Would you like to try and use this disc anyway?") != 0) {
159	            if (!previouslyMounted)
160		        unmount(mountpoint, MNT_FORCE);
161		    cdromMounted = FALSE;
162		    properties_free(cd_attr);
163		    return FALSE;
164		}
165		else
166		    bogusCDOK = TRUE;
167	    }
168	    if ((cp = property_find(cd_attr, "CD_MACHINE_ARCH")) != NULL) {
169		if (strcmp(cp, "any") &&
170#if defined(PC98)
171		  strcmp(cp, "pc98")) {
172#elif defined(__sparc64__)
173		  strcmp(cp, "sparc64")) {
174#else
175		  strcmp(cp, "x86")) {
176#endif
177		    msgConfirm("Fatal: The FreeBSD install CD/DVD currently in the drive\n"
178			   "is for the %s architecture, not the machine you're using.\n\n"
179
180			   "Please use the correct installation CD/DVD for your machine type.", cp);
181
182	            if (!previouslyMounted)
183		        unmount(mountpoint, MNT_FORCE);
184		    cdromMounted = FALSE;
185		    properties_free(cd_attr);
186		    return FALSE;
187		}
188	    }
189	    if ((cp = property_find(cd_attr, "CD_VOLUME")) != NULL) {
190		dev->volume = atoi(cp);
191		/* XXX - Sanity check the volume here? */
192		msgDebug("CD Volume %d initialized!\n", dev->volume);
193	    } else {
194		dev->volume = 0;
195	    }
196	}
197    }
198    if (cd_attr)
199	properties_free(cd_attr);
200    return TRUE;
201}
202
203FILE *
204mediaGetCDROM(Device *dev, char *file, Boolean probe)
205{
206    return mediaGenericGet(mountpoint, file);
207}
208
209void
210mediaShutdownCDROM(Device *dev)
211{
212    if (!cdromMounted)
213	return;
214
215    if (previouslyMounted) {
216	cdromMounted = FALSE;
217	return;
218    }
219
220    if (unmount(mountpoint, MNT_FORCE) != 0)
221	msgConfirm("Could not unmount the CDROM/DVD from %s: %s", mountpoint, strerror(errno));
222    else
223	cdromMounted = FALSE;
224
225    mediaEjectCDROM(dev);
226}
227
228static void
229mediaEjectCDROM(Device *dev)
230{
231	int fd = -1;
232
233	msgDebug("Ejecting CDROM/DVD at %s", dev->devname);
234
235	fd = open(dev->devname, O_RDONLY);
236
237	if (fd < 0)
238		msgDebug("Could not eject the CDROM/DVD from %s: %s", dev->devname, strerror(errno));
239	else {
240		ioctl(fd, CDIOCALLOW);
241		ioctl(fd, CDIOCEJECT);
242		close(fd);
243	}
244}
245