]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/rdev.c
3 rdev.c - query/set root device.
5 -------------------------------------------------------------------------
7 Date: Sun, 27 Dec 1992 15:55:31 +0000
9 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
10 To: Rik Faith <faith@cs.unc.edu>
12 There are quite a few versions of rdev:
14 - the original rootdev that only printed the current root device, by
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
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
24 -------------------------------------------------------------------------
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
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:
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.
40 ramsize sets the size of the ramdisk. If size is zero, no ramdisk is used.
42 vidmode sets the default video mode at bootup time. -1 uses default video
45 -------------------------------------------------------------------------
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
55 -------------------------------------------------------------------------
62 /* rdev.c - query/set root device. */
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."));
93 #include <sys/types.h>
96 #define DEFAULT_OFFSET 508
99 static void die(char *msg
)
106 static char *find_dev(int number
)
110 static char name
[PATH_MAX
+1];
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
;
121 sprintf(name
,"0x%04x",number
);
125 /* enum { RDEV, SDEV, RAMSIZE, VIDMODE }; */
126 enum { RDEV
, VIDMODE
, RAMSIZE
, SDEV
, __syssize__
, ROOTFLAGS
};
127 char *cmdnames
[6] = { "rdev", "vidmode", "ramsize", "swapdev",
129 char *desc
[6] = { "Root device", "Video mode", "Ramsize", "Swap device",
131 #define shift(n) argv+=n,argc-=n
133 int main(int argc
,char **argv
)
135 int image
,offset
,dev_nr
, i
, newoffset
=-1;
140 setlocale(LC_ALL
, "");
141 bindtextdomain(PACKAGE
, LOCALEDIR
);
145 if ((ptr
= strrchr(argv
[0],'/')) != NULL
)
150 if (!strcmp(ptr
,cmdnames
[i
]))
155 offset
= DEFAULT_OFFSET
-cmd
*2;
159 if (argv
[1][0] != '-')
166 offset
= DEFAULT_OFFSET
-cmd
*2;
171 offset
= DEFAULT_OFFSET
-cmd
*2;
176 offset
= DEFAULT_OFFSET
-cmd
*2;
181 offset
= DEFAULT_OFFSET
-cmd
*2;
187 newoffset
=atoi(argv
[1]+2);
190 } else if (argc
> 2) {
191 newoffset
=atoi(argv
[2]);
195 /* Fall through. . . */
203 if ((cmd
==RDEV
) && (argc
== 1 || argc
> 4)) {
204 if (stat("/",&s
) < 0) die("/");
205 printf("%s /\n",find_dev(s
.st_dev
));
207 } else if ((cmd
!= RDEV
) && (argc
== 1 || argc
> 4)) usage();
209 if ((cmd
==RDEV
) || (cmd
==SDEV
))
213 offset
= atoi(argv
[3]);
217 if (isdigit(*argv
[2])) offset
= atoi(argv
[2]);
218 else device
= argv
[2];
227 offset
= atoi(argv
[3]);
230 if ((cmd
==SDEV
) || (cmd
==RDEV
))
231 { if (stat(device
,&s
) < 0) die(device
);
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");
240 if ((image
= open(argv
[1],O_RDONLY
)) < 0) die(argv
[1]);
241 if (lseek(image
,offset
,0) < 0) die("lseek");
243 if (read(image
,(char *)&dev_nr
,2) != 2) die(argv
[1]);
244 if (close(image
) < 0) die("close");
246 if ((cmd
==SDEV
) || (cmd
==RDEV
))
247 printf(" %s\n", find_dev(dev_nr
));
249 printf(" %d\n", dev_nr
);