]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/rdev.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / sys-utils / rdev.c
1 /*
2
3 rdev.c - query/set root device.
4
5 -------------------------------------------------------------------------
6
7 Date: Sun, 27 Dec 1992 15:55:31 +0000
8 Subject: Re: rdev
9 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
10 To: Rik Faith <faith@cs.unc.edu>
11
12 There are quite a few versions of rdev:
13
14 - the original rootdev that only printed the current root device, by
15 Linus.
16 - rdev that does what rootdev did and that also allows you to change
17 the root (and swap) device, by me.
18 - rdev got renamed to setroot and I think even to rootdev on various
19 distributions.
20 - Peter MacDonald added video mode and RAM disk setting and included
21 this version on SLS, called rdev again. I've attached his rdev.c to
22 this mail.
23
24 -------------------------------------------------------------------------
25
26 Date: 11 Mar 92 21:37:37 GMT
27 Subject: rdev - query/set root device
28 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
29 Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
30
31 With all that socket, X11, disk driver and FS hacking going on, apparently
32 nobody has found time to address one of the minor nuisances of life: set-
33 ting the root FS device is still somewhat cumbersome. I've written a little
34 utility which can read and set the root device in boot images:
35
36 rdev accepts an optional offset argument, just in case the address should
37 ever move from 508. If called without arguments, rdev outputs an mtab line
38 for the current root FS, just like /etc/rootdev does.
39
40 ramsize sets the size of the ramdisk. If size is zero, no ramdisk is used.
41
42 vidmode sets the default video mode at bootup time. -1 uses default video
43 mode, -2 uses menu.
44
45 -------------------------------------------------------------------------
46
47 Sun Dec 27 10:42:16 1992: Minor usage changes, faith@cs.unc.edu.
48 Tue Mar 30 09:31:52 1993: rdev -Rn to set root readonly flag, sct@dcs.ed.ac.uk
49 Wed Jun 22 21:12:29 1994: Applied patches from Dave
50 (gentzel@nova.enet.dec.com) to prevent dereferencing
51 the NULL pointer, faith@cs.unc.edu
52 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
53 - added Native Language Support
54
55 -------------------------------------------------------------------------
56
57 */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include "nls.h"
62
63 /* rdev.c - query/set root device. */
64
65 static void
66 usage(void) {
67
68 puts(_("usage: rdev [ -rsv ] [ -o OFFSET ] [ IMAGE [ VALUE [ OFFSET ] ] ]"));
69 puts(_(" rdev /dev/fd0 (or rdev /linux, etc.) displays the current ROOT device"));
70 puts(_(" rdev /dev/fd0 /dev/hda2 sets ROOT to /dev/hda2"));
71 puts(_(" rdev -R /dev/fd0 1 set the ROOTFLAGS (readonly status)"));
72 puts(_(" rdev -s /dev/fd0 /dev/hda2 set the SWAP device"));
73 puts(_(" rdev -r /dev/fd0 627 set the RAMDISK size"));
74 puts(_(" rdev -v /dev/fd0 1 set the bootup VIDEOMODE"));
75 puts(_(" rdev -o N ... use the byte offset N"));
76 puts(_(" rootflags ... same as rdev -R"));
77 puts(_(" swapdev ... same as rdev -s"));
78 puts(_(" ramsize ... same as rdev -r"));
79 puts(_(" vidmode ... same as rdev -v"));
80 puts(_("Note: video modes are: -3=Ask, -2=Extended, -1=NormalVga, 1=key1, 2=key2,..."));
81 puts(_(" use -R 1 to mount root readonly, -R 0 for read/write."));
82 exit(-1);
83 }
84
85 #include <stdio.h>
86 #include <string.h>
87 #include <ctype.h>
88 #include <errno.h>
89 #include <fcntl.h>
90 #include <dirent.h>
91 #include <unistd.h>
92 #include <stdlib.h>
93 #include <sys/types.h>
94 #include <sys/stat.h>
95
96 #define DEFAULT_OFFSET 508
97
98
99 static void
100 die(char *msg) {
101 perror(msg);
102 exit(1);
103 }
104
105 /* Earlier rdev fails on /dev/ida/c0d0p1 so we allow for
106 (limited) recursion in /dev. -- Paul Clements */
107
108 #define MAX_DEPTH 1
109
110 static int
111 find_dev_recursive(char *dirnamebuf, int number, int depth) {
112 DIR *dp;
113 struct dirent *dir;
114 struct stat s;
115 int dirnamelen = 0;
116
117 if (depth < 0)
118 return 0;
119 if ((dp = opendir(dirnamebuf)) == NULL)
120 die("opendir");
121 dirnamelen = strlen(dirnamebuf);
122 while ((dir = readdir(dp)) != NULL) {
123 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
124 continue;
125 if (dirnamelen + 1 + strlen(dir->d_name) > PATH_MAX)
126 continue;
127 dirnamebuf[dirnamelen] = '/';
128 strcpy(dirnamebuf+dirnamelen+1, dir->d_name);
129 if (lstat(dirnamebuf, &s) < 0)
130 continue;
131 if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number)
132 return 1;
133 if ((s.st_mode & S_IFMT) == S_IFDIR &&
134 find_dev_recursive(dirnamebuf, number, depth-1))
135 return 1;
136 }
137 dirnamebuf[dirnamelen] = 0;
138 return 0;
139 }
140
141 static char *
142 find_dev(int number) {
143 static char name[PATH_MAX+1];
144
145 if (!number)
146 return "Boot device";
147 strcpy(name, "/dev");
148 if (find_dev_recursive(name, number, MAX_DEPTH))
149 return name;
150 sprintf(name, "0x%04x", number);
151 return name;
152 }
153
154 /* enum { RDEV, SDEV, RAMSIZE, VIDMODE }; */
155 enum { RDEV, VIDMODE, RAMSIZE, SDEV, __syssize__, ROOTFLAGS };
156 char *cmdnames[6] = { "rdev", "vidmode", "ramsize", "swapdev",
157 "", "rootflags"};
158 char *desc[6] = { "Root device", "Video mode", "Ramsize", "Swap device",
159 "", "Root flags"};
160 #define shift(n) argv+=n,argc-=n
161
162 int
163 main(int argc, char **argv) {
164 int image, offset, dev_nr, i, newoffset=-1;
165 char *device, *ptr;
166 struct stat s;
167 int cmd = 0;
168
169 setlocale(LC_ALL, "");
170 bindtextdomain(PACKAGE, LOCALEDIR);
171 textdomain(PACKAGE);
172
173 device = NULL;
174
175 /* use the command name to figure out what we have to do - ugly */
176 cmd = RDEV;
177 if ((ptr = strrchr(argv[0],'/')) != NULL)
178 ptr++;
179 else
180 ptr = argv[0];
181 for (i=0; i<=5; i++)
182 if (!strcmp(ptr,cmdnames[i])) {
183 cmd = i;
184 break;
185 }
186
187 while (argc > 1) {
188 if (argv[1][0] != '-')
189 break;
190 switch (argv[1][1]) {
191 case 'R':
192 cmd = ROOTFLAGS;
193 shift(1);
194 break;
195 case 'r':
196 cmd = RAMSIZE;
197 shift(1);
198 break;
199 case 'v':
200 cmd = VIDMODE;
201 shift(1);
202 break;
203 case 's':
204 cmd = SDEV;
205 shift(1);
206 break;
207 case 'o':
208 if (argv[1][2]) {
209 newoffset=atoi(argv[1]+2);
210 shift(1);
211 break;
212 } else if (argc > 2) {
213 newoffset=atoi(argv[2]);
214 shift(2);
215 break;
216 }
217 /* Fall through. . . */
218 default:
219 usage();
220 }
221 }
222
223 offset = DEFAULT_OFFSET-cmd*2;
224 if (newoffset >= 0)
225 offset = newoffset;
226
227 if ((cmd == RDEV) && (argc == 1 || argc > 4)) {
228 if (stat("/",&s) < 0) die("/");
229 printf("%s /\n", find_dev(s.st_dev));
230 exit(0);
231 } else if ((cmd != RDEV) && (argc == 1 || argc > 4))
232 usage();
233
234 if ((cmd == RDEV) || (cmd == SDEV)) {
235 if (argc == 4) {
236 device = argv[2];
237 offset = atoi(argv[3]);
238 } else {
239 if (argc == 3) {
240 if (isdigit(*argv[2]))
241 offset = atoi(argv[2]);
242 else
243 device = argv[2];
244 }
245 }
246 } else {
247 if (argc>=3)
248 device = argv[2];
249 if (argc==4)
250 offset = atoi(argv[3]);
251 }
252 if (device) {
253 if ((cmd == SDEV) || (cmd == RDEV)) {
254 if (stat(device,&s) < 0)
255 die(device);
256 } else
257 s.st_rdev=atoi(device);
258 if ((image = open(argv[1],O_WRONLY)) < 0) die(argv[1]);
259 if (lseek(image,offset,0) < 0) die("lseek");
260 if (write(image,(char *)&s.st_rdev,2) != 2) die(argv[1]);
261 if (close(image) < 0) die("close");
262 } else {
263 if ((image = open(argv[1],O_RDONLY)) < 0) die(argv[1]);
264 if (lseek(image,offset,0) < 0) die("lseek");
265 dev_nr = 0;
266 if (read(image,(char *)&dev_nr,2) != 2) die(argv[1]);
267 if (close(image) < 0) die("close");
268 fputs(desc[cmd], stdout);
269 if ((cmd == SDEV) || (cmd == RDEV))
270 printf(" %s\n", find_dev(dev_nr));
271 else
272 printf(" %d\n", dev_nr);
273 }
274 return 0;
275 }