]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/rdev.c
Imported from util-linux-2.9v 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 "nls.h"
61
62 /* rdev.c - query/set root device. */
63
64 void
65 usage()
66 {
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 die(char *msg)
100 {
101 perror(msg);
102 exit(1);
103 }
104
105
106 static char *find_dev(int number)
107 {
108 DIR *dp;
109 struct dirent *dir;
110 static char name[PATH_MAX+1];
111 struct stat s;
112
113 if (!number) return "Boot device";
114 if ((dp = opendir("/dev")) == NULL) die("opendir /dev");
115 strcpy(name,"/dev/");
116 while ((dir = readdir(dp)) != NULL) {
117 strcpy(name+5,dir->d_name);
118 if (stat(name,&s) < 0) die(name);
119 if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number) return name;
120 }
121 sprintf(name,"0x%04x",number);
122 return name;
123 }
124
125 /* enum { RDEV, SDEV, RAMSIZE, VIDMODE }; */
126 enum { RDEV, VIDMODE, RAMSIZE, SDEV, __syssize__, ROOTFLAGS };
127 char *cmdnames[6] = { "rdev", "vidmode", "ramsize", "swapdev",
128 "", "rootflags"};
129 char *desc[6] = { "Root device", "Video mode", "Ramsize", "Swap device",
130 "", "Root flags"};
131 #define shift(n) argv+=n,argc-=n
132
133 int main(int argc,char **argv)
134 {
135 int image,offset,dev_nr, i, newoffset=-1;
136 char *device, *ptr;
137 struct stat s;
138 int cmd = 0;
139
140 setlocale(LC_ALL, "");
141 bindtextdomain(PACKAGE, LOCALEDIR);
142 textdomain(PACKAGE);
143
144 device = NULL;
145 if ((ptr = strrchr(argv[0],'/')) != NULL)
146 ptr++;
147 else
148 ptr = argv[0];
149 for (i=0; i<=5; i++)
150 if (!strcmp(ptr,cmdnames[i]))
151 break;
152 cmd = i;
153 if (cmd>5)
154 cmd=RDEV;
155 offset = DEFAULT_OFFSET-cmd*2;
156
157 while (argc > 1)
158 {
159 if (argv[1][0] != '-')
160 break;
161 else
162 switch (argv[1][1])
163 {
164 case 'R':
165 cmd=ROOTFLAGS;
166 offset = DEFAULT_OFFSET-cmd*2;
167 shift(1);
168 break;
169 case 'r':
170 cmd=RAMSIZE;
171 offset = DEFAULT_OFFSET-cmd*2;
172 shift(1);
173 break;
174 case 'v':
175 cmd=VIDMODE;
176 offset = DEFAULT_OFFSET-cmd*2;
177 shift(1);
178 break;
179 case 's':
180 cmd=SDEV;
181 offset = DEFAULT_OFFSET-cmd*2;
182 shift(1);
183 break;
184 case 'o':
185 if (argv[1][2])
186 {
187 newoffset=atoi(argv[1]+2);
188 shift(1);
189 break;
190 } else if (argc > 2) {
191 newoffset=atoi(argv[2]);
192 shift(2);
193 break;
194 }
195 /* Fall through. . . */
196 default:
197 usage();
198 }
199 }
200 if (newoffset >= 0)
201 offset = newoffset;
202
203 if ((cmd==RDEV) && (argc == 1 || argc > 4)) {
204 if (stat("/",&s) < 0) die("/");
205 printf("%s /\n",find_dev(s.st_dev));
206 exit(0);
207 } else if ((cmd != RDEV) && (argc == 1 || argc > 4)) usage();
208
209 if ((cmd==RDEV) || (cmd==SDEV))
210 {
211 if (argc == 4) {
212 device = argv[2];
213 offset = atoi(argv[3]);
214 }
215 else {
216 if (argc == 3) {
217 if (isdigit(*argv[2])) offset = atoi(argv[2]);
218 else device = argv[2];
219 }
220 }
221 }
222 else
223 {
224 if (argc>=3)
225 device = argv[2];
226 if (argc==4)
227 offset = atoi(argv[3]);
228 }
229 if (device) {
230 if ((cmd==SDEV) || (cmd==RDEV))
231 { if (stat(device,&s) < 0) die(device);
232 } else
233 s.st_rdev=atoi(device);
234 if ((image = open(argv[1],O_WRONLY)) < 0) die(argv[1]);
235 if (lseek(image,offset,0) < 0) die("lseek");
236 if (write(image,(char *)&s.st_rdev,2) != 2) die(argv[1]);
237 if (close(image) < 0) die("close");
238 }
239 else {
240 if ((image = open(argv[1],O_RDONLY)) < 0) die(argv[1]);
241 if (lseek(image,offset,0) < 0) die("lseek");
242 dev_nr = 0;
243 if (read(image,(char *)&dev_nr,2) != 2) die(argv[1]);
244 if (close(image) < 0) die("close");
245 printf(desc[cmd]);
246 if ((cmd==SDEV) || (cmd==RDEV))
247 printf(" %s\n", find_dev(dev_nr));
248 else
249 printf(" %d\n", dev_nr);
250 }
251 return 0;
252 }
253
254