]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sysinstall/floppy.c
This commit was generated by cvs2svn to compensate for changes in r93526,
[FreeBSD/FreeBSD.git] / usr.sbin / sysinstall / floppy.c
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 floppy media */
40
41 #include "sysinstall.h"
42 #include <sys/fcntl.h>
43 #include <sys/stat.h>
44 #include <sys/errno.h>
45 #include <sys/param.h>
46 #include <sys/wait.h>
47 #include <unistd.h>
48 #include <grp.h>
49
50 #define MSDOSFS
51 #include <sys/mount.h>
52 #include <fs/msdosfs/msdosfsmount.h>
53 #undef MSDOSFS
54
55 #include <ufs/ufs/ufsmount.h>
56 static Boolean floppyMounted;
57
58 char *distWanted;
59 static char mountpoint[] = "/dist";
60
61 Boolean
62 mediaInitFloppy(Device *dev)
63 {
64     struct msdosfs_args dosargs;
65     struct ufs_args u_args;
66     char *mp;
67 #ifdef PC98
68     char fddev[24];
69 #endif
70
71     if (floppyMounted)
72         return TRUE;
73
74     mp = dev->private ? (char *)dev->private : mountpoint;
75     if (Mkdir(mp)) {
76         msgConfirm("Unable to make %s directory mountpoint for %s!", mp, dev->devname);
77         return FALSE;
78     }
79
80     msgDebug("Init floppy called for %s distribution.\n", distWanted ? distWanted : "some");
81
82     if (!variable_get(VAR_NONINTERACTIVE)) {
83         if (!distWanted)
84             msgConfirm("Please insert floppy in %s", dev->description);
85         else
86             msgConfirm("Please insert floppy containing %s in %s",
87                         distWanted, dev->description);
88     }
89
90     memset(&dosargs, 0, sizeof dosargs);
91 #ifdef PC98
92     dosargs.fspec = fddev;
93 #else
94     dosargs.fspec = dev->devname;
95 #endif
96     dosargs.uid = dosargs.gid = 0;
97     dosargs.mask = 0777;
98
99     memset(&u_args, 0, sizeof(u_args));
100 #ifdef PC98
101     u_args.fspec = fddev;
102 #else
103     u_args.fspec = dev->devname;
104 #endif
105
106 #ifdef PC98
107     sprintf(fddev, "%s.1200", dev->devname);
108     if (mount("msdosfs", mp, MNT_RDONLY, (caddr_t)&dosargs) != -1)
109         goto success;
110     if (mount("ufs", mp, MNT_RDONLY, (caddr_t)&u_args) != -1)
111         goto success;
112
113     sprintf(fddev, "%s.1232", dev->devname);
114     if (mount("msdosfs", mp, MNT_RDONLY, (caddr_t)&dosargs) != -1)
115         goto success;
116     if (mount("ufs", mp, MNT_RDONLY, (caddr_t)&u_args) != -1)
117         goto success;
118
119     sprintf(fddev, "%s.1440", dev->devname);
120     if (mount("msdosfs", mp, MNT_RDONLY, (caddr_t)&dosargs) != -1)
121         goto success;
122     if (mount("ufs", mp, MNT_RDONLY, (caddr_t)&u_args) != -1)
123         goto success;
124 #else
125     if (mount("msdosfs", mp, MNT_RDONLY, (caddr_t)&dosargs) != -1)
126         goto success;
127     if (mount("ufs", mp, MNT_RDONLY, (caddr_t)&u_args) != -1)
128         goto success;
129 #endif /* PC98 */
130
131     msgConfirm("Error mounting floppy %s (%s) on %s : %s",
132                dev->name, dev->devname, mp, strerror(errno));
133     return FALSE;
134
135 success:
136     floppyMounted = TRUE;
137     distWanted = NULL;
138     return TRUE;
139 }
140
141 FILE *
142 mediaGetFloppy(Device *dev, char *file, Boolean probe)
143 {
144     char        buf[PATH_MAX], *mp;
145     FILE        *fp;
146     int         nretries = 5;
147
148     /*
149      * floppies don't use mediaGenericGet() because it's too expensive
150      * to speculatively open files on a floppy disk.  Make user get it
151      * right or give up with floppies.
152      */
153     mp = dev->private ? (char *)dev->private : mountpoint;
154     snprintf(buf, PATH_MAX, "%s/%s", mp, file);
155     if (!file_readable(buf)) {
156         if (probe)
157             return NULL;
158         else {
159             while (!file_readable(buf)) {
160                 if (!--nretries) {
161                     msgConfirm("GetFloppy: Failed to get %s after retries;\ngiving up.", buf);
162                     return NULL;
163                 }
164                 distWanted = buf;
165                 mediaShutdownFloppy(dev);
166                 if (!mediaInitFloppy(dev))
167                     return NULL;
168             }
169         }
170     }
171     fp = fopen(buf, "r");
172     return fp;
173 }
174
175 void
176 mediaShutdownFloppy(Device *dev)
177 {
178     if (floppyMounted) {
179         char *mp = dev->private ? (char *)dev->private : mountpoint;
180
181         if (unmount(mp, MNT_FORCE) != 0)
182             msgDebug("Umount of floppy on %s failed: %s (%d)\n", mp, strerror(errno), errno);
183         else {
184             floppyMounted = FALSE;
185             if (!variable_get(VAR_NONINTERACTIVE) && variable_cmp(SYSTEM_STATE, "fixit"))
186                 msgConfirm("You may remove the floppy from %s", dev->description);
187         }
188     }
189 }